blob: 8a33dff86852ed6c057e81f54ba3b0e51624c795 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070017#include "Macros.h"
Michael Wright842500e2015-03-13 17:32:02 -070018
Michael Wrightd02c5b62014-02-10 15:10:22 -080019#include "InputReader.h"
20
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080021#include <android-base/stringprintf.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070022#include <errno.h>
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080023#include <input/Keyboard.h>
24#include <input/VirtualKeyMap.h>
Michael Wright842500e2015-03-13 17:32:02 -070025#include <inttypes.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070026#include <limits.h>
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080027#include <log/log.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070028#include <math.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <stddef.h>
30#include <stdlib.h>
31#include <unistd.h>
Prabir Pradhan28efc192019-11-05 01:10:04 +000032#include <utils/Errors.h>
Prabir Pradhan28efc192019-11-05 01:10:04 +000033#include <utils/Thread.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080035#include "InputDevice.h"
36
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080037using android::base::StringPrintf;
38
Michael Wrightd02c5b62014-02-10 15:10:22 -080039namespace android {
40
Josh Bartel938632f2022-07-19 15:34:22 -050041/**
42 * Determines if the identifiers passed are a sub-devices. Sub-devices are physical devices
43 * that expose multiple input device paths such a keyboard that also has a touchpad input.
44 * These are separate devices with unique descriptors in EventHub, but InputReader should
45 * create a single InputDevice for them.
46 * Sub-devices are detected by the following criteria:
47 * 1. The vendor, product, bus, version, and unique id match
48 * 2. The location matches. The location is used to distinguish a single device with multiple
49 * inputs versus the same device plugged into multiple ports.
50 */
51
52static bool isSubDevice(const InputDeviceIdentifier& identifier1,
53 const InputDeviceIdentifier& identifier2) {
54 return (identifier1.vendor == identifier2.vendor &&
55 identifier1.product == identifier2.product && identifier1.bus == identifier2.bus &&
56 identifier1.version == identifier2.version &&
57 identifier1.uniqueId == identifier2.uniqueId &&
58 identifier1.location == identifier2.location);
59}
60
Prabir Pradhanda20b172022-09-26 17:01:18 +000061static bool isStylusPointerGestureStart(const NotifyMotionArgs& motionArgs) {
62 const auto actionMasked = MotionEvent::getActionMasked(motionArgs.action);
63 if (actionMasked != AMOTION_EVENT_ACTION_HOVER_ENTER &&
64 actionMasked != AMOTION_EVENT_ACTION_DOWN &&
65 actionMasked != AMOTION_EVENT_ACTION_POINTER_DOWN) {
66 return false;
67 }
68 const auto actionIndex = MotionEvent::getActionIndex(motionArgs.action);
Prabir Pradhane5626962022-10-27 20:30:53 +000069 return isStylusToolType(motionArgs.pointerProperties[actionIndex].toolType);
Prabir Pradhanda20b172022-09-26 17:01:18 +000070}
71
Prabir Pradhan28efc192019-11-05 01:10:04 +000072// --- InputReader ---
73
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070074InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
75 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070076 InputListenerInterface& listener)
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070077 : mContext(this),
78 mEventHub(eventHub),
79 mPolicy(policy),
Siarhei Vishniakou18050092021-09-01 13:32:49 -070080 mQueuedListener(listener),
Arthur Hung95f68612022-04-07 14:08:22 +080081 mGlobalMetaState(AMETA_NONE),
82 mLedMetaState(AMETA_NONE),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070083 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080084 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070085 mDisableVirtualKeysTimeout(LLONG_MIN),
86 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -080087 mConfigurationChangesToRefresh(0) {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000088 refreshConfigurationLocked(/*changes=*/{});
Siarhei Vishniakou18050092021-09-01 13:32:49 -070089 updateGlobalMetaStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -080090}
91
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +000092InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
Prabir Pradhan28efc192019-11-05 01:10:04 +000094status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070095 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000096 return ALREADY_EXISTS;
97 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070098 mThread = std::make_unique<InputThread>(
99 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
100 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000101}
102
103status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700104 if (mThread && mThread->isCallingThread()) {
105 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +0000106 return INVALID_OPERATION;
107 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700108 mThread.reset();
109 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000110}
111
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112void InputReader::loopOnce() {
113 int32_t oldGeneration;
114 int32_t timeoutMillis;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000115 // Copy some state so that we can access it outside the lock later.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -0800117 std::vector<InputDeviceInfo> inputDevices;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000118 std::list<NotifyArgs> notifyArgs;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000120 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800121
122 oldGeneration = mGeneration;
123 timeoutMillis = -1;
124
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000125 auto changes = mConfigurationChangesToRefresh;
126 if (changes.any()) {
127 mConfigurationChangesToRefresh.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128 timeoutMillis = 0;
129 refreshConfigurationLocked(changes);
130 } else if (mNextTimeout != LLONG_MAX) {
131 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
132 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
133 }
134 } // release lock
135
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700136 std::vector<RawEvent> events = mEventHub->getEvents(timeoutMillis);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137
138 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000139 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800140 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700142 if (!events.empty()) {
Prabir Pradhanda20b172022-09-26 17:01:18 +0000143 notifyArgs += processEventsLocked(events.data(), events.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144 }
145
146 if (mNextTimeout != LLONG_MAX) {
147 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
148 if (now >= mNextTimeout) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000149 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800150 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
151 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152 mNextTimeout = LLONG_MAX;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000153 notifyArgs += timeoutExpiredLocked(now);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 }
155 }
156
157 if (oldGeneration != mGeneration) {
158 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800159 inputDevices = getInputDevicesLocked();
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000160 notifyArgs.emplace_back(
161 NotifyInputDevicesChangedArgs{mContext.getNextId(), inputDevices});
Michael Wrightd02c5b62014-02-10 15:10:22 -0800162 }
163 } // release lock
164
165 // Send out a message that the describes the changed input devices.
166 if (inputDevicesChanged) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800167 mPolicy->notifyInputDevicesChanged(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168 }
169
Prabir Pradhanda20b172022-09-26 17:01:18 +0000170 // Notify the policy of the start of every new stylus gesture outside the lock.
171 for (const auto& args : notifyArgs) {
172 const auto* motionArgs = std::get_if<NotifyMotionArgs>(&args);
173 if (motionArgs != nullptr && isStylusPointerGestureStart(*motionArgs)) {
174 mPolicy->notifyStylusGestureStarted(motionArgs->deviceId, motionArgs->eventTime);
175 }
176 }
177
178 notifyAll(std::move(notifyArgs));
179
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 // Flush queued events out to the listener.
181 // This must happen outside of the lock because the listener could potentially call
182 // back into the InputReader's methods, such as getScanCodeState, or become blocked
183 // on another thread similarly waiting to acquire the InputReader lock thereby
184 // resulting in a deadlock. This situation is actually quite plausible because the
185 // listener is actually the input dispatcher, which calls into the window manager,
186 // which occasionally calls into the input reader.
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700187 mQueuedListener.flush();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188}
189
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700190std::list<NotifyArgs> InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
191 std::list<NotifyArgs> out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 for (const RawEvent* rawEvent = rawEvents; count;) {
193 int32_t type = rawEvent->type;
194 size_t batchSize = 1;
195 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
196 int32_t deviceId = rawEvent->deviceId;
197 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700198 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
199 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200 break;
201 }
202 batchSize += 1;
203 }
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000204 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800205 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
206 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700207 out += processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208 } else {
209 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700210 case EventHubInterface::DEVICE_ADDED:
211 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
212 break;
213 case EventHubInterface::DEVICE_REMOVED:
214 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
215 break;
216 case EventHubInterface::FINISHED_DEVICE_SCAN:
217 handleConfigurationChangedLocked(rawEvent->when);
218 break;
219 default:
220 ALOG_ASSERT(false); // can't happen
221 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800222 }
223 }
224 count -= batchSize;
225 rawEvent += batchSize;
226 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700227 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228}
229
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800230void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
231 if (mDevices.find(eventHubId) != mDevices.end()) {
232 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800233 return;
234 }
235
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800236 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
237 std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700238
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000239 notifyAll(device->configure(when, mConfig, /*changes=*/{}));
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700240 notifyAll(device->reset(when));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800241
242 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800243 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
244 "(ignored non-input device)",
245 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000247 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800248 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000249 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250 }
251
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800252 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700253 // Add device to device to EventHub ids map.
254 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
255 if (mapIt == mDeviceToEventHubIdsMap.end()) {
256 std::vector<int32_t> ids = {eventHubId};
257 mDeviceToEventHubIdsMap.emplace(device, ids);
258 } else {
259 mapIt->second.push_back(eventHubId);
260 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700262
Chris Ye1b0c7342020-07-28 21:57:03 -0700263 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800264 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700265 }
Chris Yef59a2f42020-10-16 12:55:26 -0700266
267 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800268 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
269 // device class to disable SENSOR sub device only.
270 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700271 mEventHub->disableDevice(eventHubId);
272 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273}
274
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800275void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
276 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000277 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800278 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 return;
280 }
281
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000282 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000283 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700284 // Erase device from device to EventHub ids map.
285 auto mapIt = mDeviceToEventHubIdsMap.find(device);
286 if (mapIt != mDeviceToEventHubIdsMap.end()) {
287 std::vector<int32_t>& eventHubIds = mapIt->second;
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -0800288 std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
Chris Yee7310032020-09-22 15:36:28 -0700289 if (eventHubIds.size() == 0) {
290 mDeviceToEventHubIdsMap.erase(mapIt);
291 }
292 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 bumpGenerationLocked();
294
295 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800296 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
297 "(ignored non-input device)",
298 device->getId(), eventHubId, device->getName().c_str(),
299 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800300 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000301 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800302 device->getId(), eventHubId, device->getName().c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000303 device->getDescriptor().c_str(),
304 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800305 }
306
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800307 device->removeEventHubDevice(eventHubId);
308
Chris Ye1b0c7342020-07-28 21:57:03 -0700309 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800310 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700311 }
312
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700313 std::list<NotifyArgs> resetEvents;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800314 if (device->hasEventHubDevices()) {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000315 resetEvents += device->configure(when, mConfig, /*changes=*/{});
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800316 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700317 resetEvents += device->reset(when);
318 notifyAll(std::move(resetEvents));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319}
320
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000321std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800322 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
323 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
Josh Bartel938632f2022-07-19 15:34:22 -0500324 const InputDeviceIdentifier identifier2 =
325 devicePair.second->getDeviceInfo().getIdentifier();
326 return isSubDevice(identifier, identifier2);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800327 });
328
329 std::shared_ptr<InputDevice> device;
330 if (deviceIt != mDevices.end()) {
331 device = deviceIt->second;
332 } else {
333 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
334 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
335 identifier);
336 }
Arpit Singh7f4dd512023-05-18 13:22:12 +0000337 device->addEmptyEventHubDevice(eventHubId);
338 auto unused = device->configure(systemTime(SYSTEM_TIME_MONOTONIC), mConfig, /*changes=*/{});
339 device->populateMappers(eventHubId, mConfig);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340 return device;
341}
342
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700343std::list<NotifyArgs> InputReader::processEventsForDeviceLocked(int32_t eventHubId,
344 const RawEvent* rawEvents,
345 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800346 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000347 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800348 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700349 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350 }
351
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000352 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700354 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700355 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356 }
357
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700358 return device->process(rawEvents, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359}
360
Philip Junker4af3b3d2021-12-14 10:36:55 +0100361InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800362 auto deviceIt =
363 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
364 return devicePair.second->getId() == deviceId;
365 });
366 if (deviceIt != mDevices.end()) {
367 return deviceIt->second.get();
368 }
369 return nullptr;
370}
371
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700372std::list<NotifyArgs> InputReader::timeoutExpiredLocked(nsecs_t when) {
373 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000374 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000375 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376 if (!device->isIgnored()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700377 out += device->timeoutExpired(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378 }
379 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700380 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381}
382
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800383int32_t InputReader::nextInputDeviceIdLocked() {
384 return ++mNextInputDeviceId;
385}
386
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
388 // Reset global meta state because it depends on the list of all configured devices.
389 updateGlobalMetaStateLocked();
390
391 // Enqueue configuration changed.
Prabir Pradhan678438e2023-04-13 19:32:51 +0000392 mQueuedListener.notifyConfigurationChanged({mContext.getNextId(), when});
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393}
394
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000395void InputReader::refreshConfigurationLocked(ConfigurationChanges changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800396 mPolicy->getReaderConfiguration(&mConfig);
397 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
398
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000399 using Change = InputReaderConfiguration::Change;
400 if (!changes.any()) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000402 ALOGI("Reconfiguring input devices, changes=%s", changes.string().c_str());
Prabir Pradhan7e186182020-11-10 13:56:45 -0800403 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800404
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000405 if (changes.test(Change::DISPLAY_INFO)) {
Prabir Pradhan7e186182020-11-10 13:56:45 -0800406 updatePointerDisplayLocked();
407 }
408
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000409 if (changes.test(Change::MUST_REOPEN)) {
Prabir Pradhan7e186182020-11-10 13:56:45 -0800410 mEventHub->requestReopenDevices();
411 } else {
412 for (auto& devicePair : mDevices) {
413 std::shared_ptr<InputDevice>& device = devicePair.second;
Arpit Singhed6c3de2023-04-05 19:24:37 +0000414 notifyAll(device->configure(now, mConfig, changes));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415 }
416 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800417
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000418 if (changes.test(Change::POINTER_CAPTURE)) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000419 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
420 ALOGV("Skipping notifying pointer capture changes: "
421 "There was no change in the pointer capture state.");
422 } else {
423 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000424 mQueuedListener.notifyPointerCaptureChanged(
425 {mContext.getNextId(), now, mCurrentPointerCaptureRequest});
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000426 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800427 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800428}
429
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700430void InputReader::notifyAll(std::list<NotifyArgs>&& argsList) {
431 for (const NotifyArgs& args : argsList) {
432 mQueuedListener.notify(args);
433 }
434}
435
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436void InputReader::updateGlobalMetaStateLocked() {
437 mGlobalMetaState = 0;
438
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000439 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000440 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441 mGlobalMetaState |= device->getMetaState();
442 }
443}
444
445int32_t InputReader::getGlobalMetaStateLocked() {
446 return mGlobalMetaState;
447}
448
arthurhungc903df12020-08-11 15:08:42 +0800449void InputReader::updateLedMetaStateLocked(int32_t metaState) {
450 mLedMetaState = metaState;
451 for (auto& devicePair : mDevices) {
452 std::shared_ptr<InputDevice>& device = devicePair.second;
453 device->updateLedState(false);
454 }
455}
456
457int32_t InputReader::getLedMetaStateLocked() {
458 return mLedMetaState;
459}
460
Chris Ye1c2e0892020-11-30 21:41:44 -0800461void InputReader::notifyExternalStylusPresenceChangedLocked() {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000462 refreshConfigurationLocked(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Michael Wright842500e2015-03-13 17:32:02 -0700463}
464
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800465void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000466 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000467 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700468 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000469 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700470 }
471 }
472}
473
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700474std::list<NotifyArgs> InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
475 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000476 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000477 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700478 out += device->updateExternalStylusState(state);
Michael Wright842500e2015-03-13 17:32:02 -0700479 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700480 return out;
Michael Wright842500e2015-03-13 17:32:02 -0700481}
482
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
484 mDisableVirtualKeysTimeout = time;
485}
486
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800487bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800489 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700490 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800491 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492 return true;
493 } else {
494 return false;
495 }
496}
497
Michael Wright17db18e2020-06-26 20:51:44 +0100498std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked(
499 int32_t deviceId) {
500 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800501 if (controller == nullptr) {
502 controller = mPolicy->obtainPointerController(deviceId);
503 mPointerController = controller;
504 updatePointerDisplayLocked();
505 }
506 return controller;
507}
508
509void InputReader::updatePointerDisplayLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100510 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800511 if (controller == nullptr) {
512 return;
513 }
514
515 std::optional<DisplayViewport> viewport =
516 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
517 if (!viewport) {
518 ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input "
519 "mapper. Fall back to default display",
520 mConfig.defaultPointerDisplayId);
521 viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT);
522 }
523 if (!viewport) {
524 ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to"
525 " PointerController.");
526 return;
527 }
528
529 controller->setDisplayViewport(*viewport);
530}
531
Michael Wrightd02c5b62014-02-10 15:10:22 -0800532void InputReader::fadePointerLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100533 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800534 if (controller != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +0100535 controller->fade(PointerControllerInterface::Transition::GRADUAL);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800536 }
537}
538
539void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
540 if (when < mNextTimeout) {
541 mNextTimeout = when;
542 mEventHub->wake();
543 }
544}
545
546int32_t InputReader::bumpGenerationLocked() {
547 return ++mGeneration;
548}
549
Chris Ye98d3f532020-10-01 21:48:59 -0700550std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000551 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700552 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553}
554
Chris Ye98d3f532020-10-01 21:48:59 -0700555std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
556 std::vector<InputDeviceInfo> outInputDevices;
557 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558
Chris Yee7310032020-09-22 15:36:28 -0700559 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800560 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000561 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 }
563 }
Chris Ye98d3f532020-10-01 21:48:59 -0700564 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565}
566
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700567int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000568 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800569
570 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
571}
572
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700573int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000574 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800575
576 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
577}
578
579int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000580 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581
582 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
583}
584
585int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700586 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587 int32_t result = AKEY_STATE_UNKNOWN;
588 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800589 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800590 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
591 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 }
593 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000594 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000595 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700596 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
598 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000599 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 if (currentResult >= AKEY_STATE_DOWN) {
601 return currentResult;
602 } else if (currentResult == AKEY_STATE_UP) {
603 result = currentResult;
604 }
605 }
606 }
607 }
608 return result;
609}
610
Andrii Kulian763a3a42016-03-08 10:46:16 -0800611void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000612 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800613 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800614 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800615 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
616 return;
617 }
618
Andrii Kulian763a3a42016-03-08 10:46:16 -0800619 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000620 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800621 return;
622 }
623
624 device->updateMetaState(AKEYCODE_CAPS_LOCK);
625}
626
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700627bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
628 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000629 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700631 memset(outFlags, 0, keyCodes.size());
632 return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800633}
634
635bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700636 const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700637 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 bool result = false;
639 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800640 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800641 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700642 result = device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 }
644 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000645 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000646 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700647 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700648 result |= device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649 }
650 }
651 }
652 return result;
653}
654
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000655void InputReader::addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const {
656 std::scoped_lock _l(mLock);
657
658 InputDevice* device = findInputDeviceLocked(deviceId);
659 if (device != nullptr) {
660 device->addKeyRemapping(fromKeyCode, toKeyCode);
661 }
662}
663
Philip Junker4af3b3d2021-12-14 10:36:55 +0100664int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
665 std::scoped_lock _l(mLock);
666
667 InputDevice* device = findInputDeviceLocked(deviceId);
668 if (device == nullptr) {
669 ALOGW("Failed to get key code for key location: Input device with id %d not found",
670 deviceId);
671 return AKEYCODE_UNKNOWN;
672 }
673 return device->getKeyCodeForKeyLocation(locationKeyCode);
674}
675
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000676void InputReader::requestRefreshConfiguration(ConfigurationChanges changes) {
Chris Ye87143712020-11-10 05:05:58 +0000677 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800678
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000679 if (changes.any()) {
680 bool needWake = !mConfigurationChangesToRefresh.any();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681 mConfigurationChangesToRefresh |= changes;
682
683 if (needWake) {
684 mEventHub->wake();
685 }
686 }
687}
688
Chris Ye87143712020-11-10 05:05:58 +0000689void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
690 int32_t token) {
691 std::scoped_lock _l(mLock);
692
Chris Ye1c2e0892020-11-30 21:41:44 -0800693 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800694 if (device) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700695 notifyAll(device->vibrate(sequence, repeat, token));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 }
697}
698
699void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000700 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800701
Chris Ye1c2e0892020-11-30 21:41:44 -0800702 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800703 if (device) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700704 notifyAll(device->cancelVibrate(token));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 }
706}
707
Chris Ye87143712020-11-10 05:05:58 +0000708bool InputReader::isVibrating(int32_t deviceId) {
709 std::scoped_lock _l(mLock);
710
711 InputDevice* device = findInputDeviceLocked(deviceId);
712 if (device) {
713 return device->isVibrating();
714 }
715 return false;
716}
717
718std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
719 std::scoped_lock _l(mLock);
720
721 InputDevice* device = findInputDeviceLocked(deviceId);
722 if (device) {
723 return device->getVibratorIds();
724 }
725 return {};
726}
727
Chris Yef59a2f42020-10-16 12:55:26 -0700728void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
729 std::scoped_lock _l(mLock);
730
731 InputDevice* device = findInputDeviceLocked(deviceId);
732 if (device) {
733 device->disableSensor(sensorType);
734 }
735}
736
737bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
738 std::chrono::microseconds samplingPeriod,
739 std::chrono::microseconds maxBatchReportLatency) {
740 std::scoped_lock _l(mLock);
741
742 InputDevice* device = findInputDeviceLocked(deviceId);
743 if (device) {
744 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
745 }
746 return false;
747}
748
749void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
750 std::scoped_lock _l(mLock);
751
752 InputDevice* device = findInputDeviceLocked(deviceId);
753 if (device) {
754 device->flushSensor(sensorType);
755 }
756}
757
Kim Low03ea0352020-11-06 12:45:07 -0800758std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400759 std::optional<int32_t> eventHubId;
760 {
761 // Do not query the battery state while holding the lock. For some peripheral devices,
762 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
763 // would block all other event processing during this time. For now, we assume this
764 // call never happens on the InputReader thread and get the battery state outside the
765 // lock to prevent event processing from being blocked by this call.
766 std::scoped_lock _l(mLock);
767 InputDevice* device = findInputDeviceLocked(deviceId);
768 if (!device) return {};
769 eventHubId = device->getBatteryEventHubId();
770 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800771
Andy Chenf9f1a022022-08-29 20:07:10 -0400772 if (!eventHubId) return {};
773 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000774 if (batteryIds.empty()) {
775 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
776 return {};
777 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400778 return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800779}
780
781std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400782 std::optional<int32_t> eventHubId;
783 {
784 // Do not query the battery state while holding the lock. For some peripheral devices,
785 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
786 // would block all other event processing during this time. For now, we assume this
787 // call never happens on the InputReader thread and get the battery state outside the
788 // lock to prevent event processing from being blocked by this call.
789 std::scoped_lock _l(mLock);
790 InputDevice* device = findInputDeviceLocked(deviceId);
791 if (!device) return {};
792 eventHubId = device->getBatteryEventHubId();
793 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800794
Andy Chenf9f1a022022-08-29 20:07:10 -0400795 if (!eventHubId) return {};
796 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000797 if (batteryIds.empty()) {
798 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
799 return {};
800 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400801 return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800802}
803
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000804std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) {
805 std::scoped_lock _l(mLock);
806
807 InputDevice* device = findInputDeviceLocked(deviceId);
808 if (!device) return {};
809
810 std::optional<int32_t> eventHubId = device->getBatteryEventHubId();
811 if (!eventHubId) return {};
812 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
813 if (batteryIds.empty()) {
814 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
815 return {};
816 }
817 const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front());
818 if (!batteryInfo) {
819 ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__,
820 batteryIds.front(), *eventHubId);
821 return {};
822 }
823 return batteryInfo->path;
824}
825
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000826std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800827 std::scoped_lock _l(mLock);
828
829 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000830 if (device == nullptr) {
831 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800832 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000833
834 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800835}
836
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000837std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800838 std::scoped_lock _l(mLock);
839
840 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000841 if (device == nullptr) {
842 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800843 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000844
845 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800846}
847
848bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
849 std::scoped_lock _l(mLock);
850
851 InputDevice* device = findInputDeviceLocked(deviceId);
852 if (device) {
853 return device->setLightColor(lightId, color);
854 }
855 return false;
856}
857
858bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
859 std::scoped_lock _l(mLock);
860
861 InputDevice* device = findInputDeviceLocked(deviceId);
862 if (device) {
863 return device->setLightPlayerId(lightId, playerId);
864 }
865 return false;
866}
867
868std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
869 std::scoped_lock _l(mLock);
870
871 InputDevice* device = findInputDeviceLocked(deviceId);
872 if (device) {
873 return device->getLightColor(lightId);
874 }
875 return std::nullopt;
876}
877
878std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
879 std::scoped_lock _l(mLock);
880
881 InputDevice* device = findInputDeviceLocked(deviceId);
882 if (device) {
883 return device->getLightPlayerId(lightId);
884 }
885 return std::nullopt;
886}
887
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000888std::optional<std::string> InputReader::getBluetoothAddress(int32_t deviceId) const {
889 std::scoped_lock _l(mLock);
890
891 InputDevice* device = findInputDeviceLocked(deviceId);
892 if (device) {
893 return device->getBluetoothAddress();
894 }
895 return std::nullopt;
896}
897
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700898bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000899 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700900
Chris Ye1c2e0892020-11-30 21:41:44 -0800901 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800902 if (device) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700903 return device->isEnabled();
904 }
905 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
906 return false;
907}
908
Arthur Hungc23540e2018-11-29 20:42:11 +0800909bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000910 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800911
Chris Ye1c2e0892020-11-30 21:41:44 -0800912 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800913 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800914 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
915 return false;
916 }
917
Arthur Hung2c9a3342019-07-23 14:18:59 +0800918 if (!device->isEnabled()) {
919 ALOGW("Ignoring disabled device %s", device->getName().c_str());
920 return false;
921 }
922
923 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800924 // No associated display. By default, can dispatch to all displays.
Weilun Dud00847d2021-12-08 10:55:58 -0800925 if (!associatedDisplayId ||
926 *associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800927 return true;
928 }
929
930 return *associatedDisplayId == displayId;
931}
932
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000933void InputReader::sysfsNodeChanged(const std::string& sysfsNodePath) {
934 mEventHub->sysfsNodeChanged(sysfsNodePath);
935}
936
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800937void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000938 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939
940 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800941 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942
Chris Yee7310032020-09-22 15:36:28 -0700943 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
944 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945
Chris Yee7310032020-09-22 15:36:28 -0700946 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
947 const std::shared_ptr<InputDevice>& device = devicePair.first;
948 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
949 for (const auto& eId : devicePair.second) {
950 eventHubDevStr += StringPrintf("%d ", eId);
951 }
952 eventHubDevStr += "] \n";
953 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954 }
955
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800956 dump += INDENT "Configuration:\n";
957 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
959 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800960 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100962 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800963 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800964 dump += "]\n";
965 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700966 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800968 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700969 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
970 "acceleration=%0.3f\n",
971 mConfig.pointerVelocityControlParameters.scale,
972 mConfig.pointerVelocityControlParameters.lowThreshold,
973 mConfig.pointerVelocityControlParameters.highThreshold,
974 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800975
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800976 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700977 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
978 "acceleration=%0.3f\n",
979 mConfig.wheelVelocityControlParameters.scale,
980 mConfig.wheelVelocityControlParameters.lowThreshold,
981 mConfig.wheelVelocityControlParameters.highThreshold,
982 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800984 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700985 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800986 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700987 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800988 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700989 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800990 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700991 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800992 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700993 mConfig.pointerGestureTapDragInterval * 0.000001f);
994 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800995 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700996 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800997 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700998 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800999 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001000 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001001 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001002 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001003 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -07001004 mConfig.pointerGestureMovementSpeedRatio);
1005 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001006
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001007 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -07001008 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009}
1010
1011void InputReader::monitor() {
1012 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -08001013 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001014 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -08001015 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001016 // Check the EventHub
1017 mEventHub->monitor();
1018}
1019
Michael Wrightd02c5b62014-02-10 15:10:22 -08001020// --- InputReader::ContextImpl ---
1021
Garfield Tanff1f1bb2020-01-28 13:24:04 -08001022InputReader::ContextImpl::ContextImpl(InputReader* reader)
1023 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001024
1025void InputReader::ContextImpl::updateGlobalMetaState() {
1026 // lock is already held by the input loop
1027 mReader->updateGlobalMetaStateLocked();
1028}
1029
1030int32_t InputReader::ContextImpl::getGlobalMetaState() {
1031 // lock is already held by the input loop
1032 return mReader->getGlobalMetaStateLocked();
1033}
1034
arthurhungc903df12020-08-11 15:08:42 +08001035void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
1036 // lock is already held by the input loop
1037 mReader->updateLedMetaStateLocked(metaState);
1038}
1039
1040int32_t InputReader::ContextImpl::getLedMetaState() {
1041 // lock is already held by the input loop
1042 return mReader->getLedMetaStateLocked();
1043}
1044
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
1046 // lock is already held by the input loop
1047 mReader->disableVirtualKeysUntilLocked(time);
1048}
1049
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001050bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
1051 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001053 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001054}
1055
1056void InputReader::ContextImpl::fadePointer() {
1057 // lock is already held by the input loop
1058 mReader->fadePointerLocked();
1059}
1060
Michael Wright17db18e2020-06-26 20:51:44 +01001061std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController(
1062 int32_t deviceId) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001063 // lock is already held by the input loop
1064 return mReader->getPointerControllerLocked(deviceId);
1065}
1066
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
1068 // lock is already held by the input loop
1069 mReader->requestTimeoutAtTimeLocked(when);
1070}
1071
1072int32_t InputReader::ContextImpl::bumpGeneration() {
1073 // lock is already held by the input loop
1074 return mReader->bumpGenerationLocked();
1075}
1076
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001077void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -07001078 // lock is already held by whatever called refreshConfigurationLocked
1079 mReader->getExternalStylusDevicesLocked(outDevices);
1080}
1081
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001082std::list<NotifyArgs> InputReader::ContextImpl::dispatchExternalStylusState(
1083 const StylusState& state) {
1084 return mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -07001085}
1086
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
1088 return mReader->mPolicy.get();
1089}
1090
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091EventHubInterface* InputReader::ContextImpl::getEventHub() {
1092 return mReader->mEventHub.get();
1093}
1094
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00001095int32_t InputReader::ContextImpl::getNextId() {
1096 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -08001097}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001098
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099} // namespace android