blob: ab13ad489b34f4e55ce6e7d893d6c6879c2f658c [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
Prabir Pradhan018faea2024-05-08 21:52:54 +000041namespace {
42
Josh Bartel938632f2022-07-19 15:34:22 -050043/**
44 * Determines if the identifiers passed are a sub-devices. Sub-devices are physical devices
45 * that expose multiple input device paths such a keyboard that also has a touchpad input.
46 * These are separate devices with unique descriptors in EventHub, but InputReader should
47 * create a single InputDevice for them.
48 * Sub-devices are detected by the following criteria:
49 * 1. The vendor, product, bus, version, and unique id match
50 * 2. The location matches. The location is used to distinguish a single device with multiple
51 * inputs versus the same device plugged into multiple ports.
52 */
53
Prabir Pradhan018faea2024-05-08 21:52:54 +000054bool isSubDevice(const InputDeviceIdentifier& identifier1,
55 const InputDeviceIdentifier& identifier2) {
Josh Bartel938632f2022-07-19 15:34:22 -050056 return (identifier1.vendor == identifier2.vendor &&
57 identifier1.product == identifier2.product && identifier1.bus == identifier2.bus &&
58 identifier1.version == identifier2.version &&
59 identifier1.uniqueId == identifier2.uniqueId &&
60 identifier1.location == identifier2.location);
61}
62
Prabir Pradhan018faea2024-05-08 21:52:54 +000063bool isStylusPointerGestureStart(const NotifyMotionArgs& motionArgs) {
Prabir Pradhanda20b172022-09-26 17:01:18 +000064 const auto actionMasked = MotionEvent::getActionMasked(motionArgs.action);
65 if (actionMasked != AMOTION_EVENT_ACTION_HOVER_ENTER &&
66 actionMasked != AMOTION_EVENT_ACTION_DOWN &&
67 actionMasked != AMOTION_EVENT_ACTION_POINTER_DOWN) {
68 return false;
69 }
70 const auto actionIndex = MotionEvent::getActionIndex(motionArgs.action);
Prabir Pradhane5626962022-10-27 20:30:53 +000071 return isStylusToolType(motionArgs.pointerProperties[actionIndex].toolType);
Prabir Pradhanda20b172022-09-26 17:01:18 +000072}
73
Prabir Pradhan018faea2024-05-08 21:52:54 +000074bool isNewGestureStart(const NotifyMotionArgs& motion) {
75 return motion.action == AMOTION_EVENT_ACTION_DOWN ||
76 motion.action == AMOTION_EVENT_ACTION_HOVER_ENTER;
77}
78
79bool isNewGestureStart(const NotifyKeyArgs& key) {
80 return key.action == AKEY_EVENT_ACTION_DOWN;
81}
82
83// Return the event's device ID if it marks the start of a new gesture.
84std::optional<DeviceId> getDeviceIdOfNewGesture(const NotifyArgs& args) {
85 if (const auto* motion = std::get_if<NotifyMotionArgs>(&args); motion != nullptr) {
86 return isNewGestureStart(*motion) ? std::make_optional(motion->deviceId) : std::nullopt;
87 }
88 if (const auto* key = std::get_if<NotifyKeyArgs>(&args); key != nullptr) {
89 return isNewGestureStart(*key) ? std::make_optional(key->deviceId) : std::nullopt;
90 }
91 return std::nullopt;
92}
93
94} // namespace
95
Prabir Pradhan28efc192019-11-05 01:10:04 +000096// --- InputReader ---
97
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070098InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
99 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700100 InputListenerInterface& listener)
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700101 : mContext(this),
102 mEventHub(eventHub),
103 mPolicy(policy),
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700104 mNextListener(listener),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000105 mKeyboardClassifier(std::make_unique<KeyboardClassifier>()),
Arthur Hung95f68612022-04-07 14:08:22 +0800106 mGlobalMetaState(AMETA_NONE),
107 mLedMetaState(AMETA_NONE),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700108 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800109 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700110 mDisableVirtualKeysTimeout(LLONG_MIN),
111 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112 mConfigurationChangesToRefresh(0) {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000113 refreshConfigurationLocked(/*changes=*/{});
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700114 updateGlobalMetaStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115}
116
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000117InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Prabir Pradhan28efc192019-11-05 01:10:04 +0000119status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700120 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000121 return ALREADY_EXISTS;
122 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700123 mThread = std::make_unique<InputThread>(
124 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
125 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000126}
127
128status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700129 if (mThread && mThread->isCallingThread()) {
130 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +0000131 return INVALID_OPERATION;
132 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700133 mThread.reset();
134 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000135}
136
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137void InputReader::loopOnce() {
138 int32_t oldGeneration;
139 int32_t timeoutMillis;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000140 // Copy some state so that we can access it outside the lock later.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -0800142 std::vector<InputDeviceInfo> inputDevices;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000143 std::list<NotifyArgs> notifyArgs;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000145 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146
147 oldGeneration = mGeneration;
148 timeoutMillis = -1;
149
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000150 auto changes = mConfigurationChangesToRefresh;
151 if (changes.any()) {
152 mConfigurationChangesToRefresh.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153 timeoutMillis = 0;
154 refreshConfigurationLocked(changes);
155 } else if (mNextTimeout != LLONG_MAX) {
156 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
157 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
158 }
159 } // release lock
160
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700161 std::vector<RawEvent> events = mEventHub->getEvents(timeoutMillis);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800162
163 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000164 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800165 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700167 if (!events.empty()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700168 mPendingArgs += processEventsLocked(events.data(), events.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169 }
170
171 if (mNextTimeout != LLONG_MAX) {
172 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
173 if (now >= mNextTimeout) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000174 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800175 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
176 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177 mNextTimeout = LLONG_MAX;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700178 mPendingArgs += timeoutExpiredLocked(now);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 }
180 }
181
182 if (oldGeneration != mGeneration) {
183 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800184 inputDevices = getInputDevicesLocked();
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700185 mPendingArgs.emplace_back(
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000186 NotifyInputDevicesChangedArgs{mContext.getNextId(), inputDevices});
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700188
189 std::swap(notifyArgs, mPendingArgs);
Prabir Pradhan018faea2024-05-08 21:52:54 +0000190
191 // Keep track of the last used device
192 for (const NotifyArgs& args : notifyArgs) {
193 mLastUsedDeviceId = getDeviceIdOfNewGesture(args).value_or(mLastUsedDeviceId);
194 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 } // release lock
196
Michael Wrightd02c5b62014-02-10 15:10:22 -0800197 // Flush queued events out to the listener.
198 // This must happen outside of the lock because the listener could potentially call
199 // back into the InputReader's methods, such as getScanCodeState, or become blocked
200 // on another thread similarly waiting to acquire the InputReader lock thereby
201 // resulting in a deadlock. This situation is actually quite plausible because the
202 // listener is actually the input dispatcher, which calls into the window manager,
203 // which occasionally calls into the input reader.
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700204 for (const NotifyArgs& args : notifyArgs) {
205 mNextListener.notify(args);
206 }
Prabir Pradhanc3a92472024-02-06 20:08:05 +0000207
208 // Notify the policy that input devices have changed.
209 // This must be done after flushing events down the listener chain to ensure that the rest of
210 // the listeners are synchronized with the changes before the policy reacts to them.
211 if (inputDevicesChanged) {
212 mPolicy->notifyInputDevicesChanged(inputDevices);
213 }
214
215 // Notify the policy of the start of every new stylus gesture.
216 for (const auto& args : notifyArgs) {
217 const auto* motionArgs = std::get_if<NotifyMotionArgs>(&args);
218 if (motionArgs != nullptr && isStylusPointerGestureStart(*motionArgs)) {
219 mPolicy->notifyStylusGestureStarted(motionArgs->deviceId, motionArgs->eventTime);
220 }
221 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800222}
223
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700224std::list<NotifyArgs> InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
225 std::list<NotifyArgs> out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226 for (const RawEvent* rawEvent = rawEvents; count;) {
227 int32_t type = rawEvent->type;
228 size_t batchSize = 1;
229 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
230 int32_t deviceId = rawEvent->deviceId;
231 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700232 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
233 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 break;
235 }
236 batchSize += 1;
237 }
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000238 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800239 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
240 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700241 out += processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242 } else {
243 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700244 case EventHubInterface::DEVICE_ADDED:
245 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
246 break;
247 case EventHubInterface::DEVICE_REMOVED:
248 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
249 break;
250 case EventHubInterface::FINISHED_DEVICE_SCAN:
251 handleConfigurationChangedLocked(rawEvent->when);
252 break;
253 default:
254 ALOG_ASSERT(false); // can't happen
255 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256 }
257 }
258 count -= batchSize;
259 rawEvent += batchSize;
260 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700261 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262}
263
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800264void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
265 if (mDevices.find(eventHubId) != mDevices.end()) {
266 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267 return;
268 }
269
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800270 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
Arpit Singh82f29a12023-06-13 15:05:53 +0000271 std::shared_ptr<InputDevice> device = createDeviceLocked(when, eventHubId, identifier);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700272
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700273 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
274 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275
276 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800277 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
278 "(ignored non-input device)",
279 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000281 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800282 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000283 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 }
285
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800286 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700287 // Add device to device to EventHub ids map.
288 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
289 if (mapIt == mDeviceToEventHubIdsMap.end()) {
290 std::vector<int32_t> ids = {eventHubId};
291 mDeviceToEventHubIdsMap.emplace(device, ids);
292 } else {
293 mapIt->second.push_back(eventHubId);
294 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700296
Chris Ye1b0c7342020-07-28 21:57:03 -0700297 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800298 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700299 }
Chris Yef59a2f42020-10-16 12:55:26 -0700300
301 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800302 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
303 // device class to disable SENSOR sub device only.
304 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700305 mEventHub->disableDevice(eventHubId);
306 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307}
308
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800309void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
310 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000311 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800312 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 return;
314 }
315
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000316 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000317 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700318 // Erase device from device to EventHub ids map.
319 auto mapIt = mDeviceToEventHubIdsMap.find(device);
320 if (mapIt != mDeviceToEventHubIdsMap.end()) {
321 std::vector<int32_t>& eventHubIds = mapIt->second;
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -0800322 std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
Chris Yee7310032020-09-22 15:36:28 -0700323 if (eventHubIds.size() == 0) {
324 mDeviceToEventHubIdsMap.erase(mapIt);
325 }
326 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 bumpGenerationLocked();
328
329 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800330 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
331 "(ignored non-input device)",
332 device->getId(), eventHubId, device->getName().c_str(),
333 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000335 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800336 device->getId(), eventHubId, device->getName().c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000337 device->getDescriptor().c_str(),
338 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339 }
340
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800341 device->removeEventHubDevice(eventHubId);
342
Chris Ye1b0c7342020-07-28 21:57:03 -0700343 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800344 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700345 }
346
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800347 if (device->hasEventHubDevices()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700348 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800349 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700350 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351}
352
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000353std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Arpit Singh82f29a12023-06-13 15:05:53 +0000354 nsecs_t when, int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800355 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
Josh Bartel938632f2022-07-19 15:34:22 -0500356 const InputDeviceIdentifier identifier2 =
357 devicePair.second->getDeviceInfo().getIdentifier();
358 return isSubDevice(identifier, identifier2);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800359 });
360
361 std::shared_ptr<InputDevice> device;
362 if (deviceIt != mDevices.end()) {
363 device = deviceIt->second;
364 } else {
365 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
366 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
367 identifier);
368 }
Arpit Singh82f29a12023-06-13 15:05:53 +0000369 mPendingArgs += device->addEventHubDevice(when, eventHubId, mConfig);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800370 return device;
371}
372
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700373std::list<NotifyArgs> InputReader::processEventsForDeviceLocked(int32_t eventHubId,
374 const RawEvent* rawEvents,
375 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800376 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000377 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800378 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700379 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800380 }
381
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000382 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700384 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700385 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386 }
387
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700388 return device->process(rawEvents, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389}
390
Philip Junker4af3b3d2021-12-14 10:36:55 +0100391InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800392 auto deviceIt =
393 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
394 return devicePair.second->getId() == deviceId;
395 });
396 if (deviceIt != mDevices.end()) {
397 return deviceIt->second.get();
398 }
399 return nullptr;
400}
401
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700402std::list<NotifyArgs> InputReader::timeoutExpiredLocked(nsecs_t when) {
403 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000404 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000405 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800406 if (!device->isIgnored()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700407 out += device->timeoutExpired(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 }
409 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700410 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411}
412
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800413int32_t InputReader::nextInputDeviceIdLocked() {
414 return ++mNextInputDeviceId;
415}
416
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
418 // Reset global meta state because it depends on the list of all configured devices.
419 updateGlobalMetaStateLocked();
420
421 // Enqueue configuration changed.
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700422 mPendingArgs.emplace_back(NotifyConfigurationChangedArgs{mContext.getNextId(), when});
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423}
424
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000425void InputReader::refreshConfigurationLocked(ConfigurationChanges changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800426 mPolicy->getReaderConfiguration(&mConfig);
427 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
428
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000429 using Change = InputReaderConfiguration::Change;
430 if (!changes.any()) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000432 ALOGI("Reconfiguring input devices, changes=%s", changes.string().c_str());
Prabir Pradhan7e186182020-11-10 13:56:45 -0800433 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800434
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000435 if (changes.test(Change::MUST_REOPEN)) {
Prabir Pradhan7e186182020-11-10 13:56:45 -0800436 mEventHub->requestReopenDevices();
437 } else {
438 for (auto& devicePair : mDevices) {
439 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700440 mPendingArgs += device->configure(now, mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441 }
442 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800443
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000444 if (changes.test(Change::POINTER_CAPTURE)) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000445 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
446 ALOGV("Skipping notifying pointer capture changes: "
447 "There was no change in the pointer capture state.");
448 } else {
449 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700450 mPendingArgs.emplace_back(
451 NotifyPointerCaptureChangedArgs{mContext.getNextId(), now,
452 mCurrentPointerCaptureRequest});
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000453 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800454 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455}
456
457void InputReader::updateGlobalMetaStateLocked() {
458 mGlobalMetaState = 0;
459
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000460 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000461 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462 mGlobalMetaState |= device->getMetaState();
463 }
464}
465
466int32_t InputReader::getGlobalMetaStateLocked() {
467 return mGlobalMetaState;
468}
469
arthurhungc903df12020-08-11 15:08:42 +0800470void InputReader::updateLedMetaStateLocked(int32_t metaState) {
471 mLedMetaState = metaState;
472 for (auto& devicePair : mDevices) {
473 std::shared_ptr<InputDevice>& device = devicePair.second;
474 device->updateLedState(false);
475 }
476}
477
478int32_t InputReader::getLedMetaStateLocked() {
479 return mLedMetaState;
480}
481
Chris Ye1c2e0892020-11-30 21:41:44 -0800482void InputReader::notifyExternalStylusPresenceChangedLocked() {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000483 refreshConfigurationLocked(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Michael Wright842500e2015-03-13 17:32:02 -0700484}
485
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800486void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000487 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000488 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700489 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000490 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700491 }
492 }
493}
494
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700495std::list<NotifyArgs> InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
496 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000497 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000498 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700499 out += device->updateExternalStylusState(state);
Michael Wright842500e2015-03-13 17:32:02 -0700500 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700501 return out;
Michael Wright842500e2015-03-13 17:32:02 -0700502}
503
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
505 mDisableVirtualKeysTimeout = time;
506}
507
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800508bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800509 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800510 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700511 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800512 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513 return true;
514 } else {
515 return false;
516 }
517}
518
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
520 if (when < mNextTimeout) {
521 mNextTimeout = when;
522 mEventHub->wake();
523 }
524}
525
526int32_t InputReader::bumpGenerationLocked() {
527 return ++mGeneration;
528}
529
Chris Ye98d3f532020-10-01 21:48:59 -0700530std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000531 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700532 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533}
534
Chris Ye98d3f532020-10-01 21:48:59 -0700535std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
536 std::vector<InputDeviceInfo> outInputDevices;
537 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538
Chris Yee7310032020-09-22 15:36:28 -0700539 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000541 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800542 }
543 }
Chris Ye98d3f532020-10-01 21:48:59 -0700544 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545}
546
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700547int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000548 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549
550 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
551}
552
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700553int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000554 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555
556 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
557}
558
559int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000560 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800561
562 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
563}
564
565int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700566 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567 int32_t result = AKEY_STATE_UNKNOWN;
568 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800569 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800570 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
571 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572 }
573 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000574 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000575 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700576 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
578 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000579 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580 if (currentResult >= AKEY_STATE_DOWN) {
581 return currentResult;
582 } else if (currentResult == AKEY_STATE_UP) {
583 result = currentResult;
584 }
585 }
586 }
587 }
588 return result;
589}
590
Andrii Kulian763a3a42016-03-08 10:46:16 -0800591void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000592 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800593 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800594 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800595 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
596 return;
597 }
598
Andrii Kulian763a3a42016-03-08 10:46:16 -0800599 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000600 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800601 return;
602 }
603
604 device->updateMetaState(AKEYCODE_CAPS_LOCK);
605}
606
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700607bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
608 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000609 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700611 memset(outFlags, 0, keyCodes.size());
612 return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613}
614
615bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700616 const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700617 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 bool result = false;
619 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800620 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700622 result = device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623 }
624 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000625 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000626 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700627 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700628 result |= device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 }
630 }
631 }
632 return result;
633}
634
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000635void InputReader::addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const {
636 std::scoped_lock _l(mLock);
637
638 InputDevice* device = findInputDeviceLocked(deviceId);
639 if (device != nullptr) {
640 device->addKeyRemapping(fromKeyCode, toKeyCode);
641 }
642}
643
Philip Junker4af3b3d2021-12-14 10:36:55 +0100644int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
645 std::scoped_lock _l(mLock);
646
647 InputDevice* device = findInputDeviceLocked(deviceId);
648 if (device == nullptr) {
649 ALOGW("Failed to get key code for key location: Input device with id %d not found",
650 deviceId);
651 return AKEYCODE_UNKNOWN;
652 }
653 return device->getKeyCodeForKeyLocation(locationKeyCode);
654}
655
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000656void InputReader::requestRefreshConfiguration(ConfigurationChanges changes) {
Chris Ye87143712020-11-10 05:05:58 +0000657 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000659 if (changes.any()) {
660 bool needWake = !mConfigurationChangesToRefresh.any();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 mConfigurationChangesToRefresh |= changes;
662
663 if (needWake) {
664 mEventHub->wake();
665 }
666 }
667}
668
Chris Ye87143712020-11-10 05:05:58 +0000669void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
670 int32_t token) {
671 std::scoped_lock _l(mLock);
672
Chris Ye1c2e0892020-11-30 21:41:44 -0800673 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800674 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700675 mPendingArgs += device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676 }
677}
678
679void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000680 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681
Chris Ye1c2e0892020-11-30 21:41:44 -0800682 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800683 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700684 mPendingArgs += device->cancelVibrate(token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685 }
686}
687
Chris Ye87143712020-11-10 05:05:58 +0000688bool InputReader::isVibrating(int32_t deviceId) {
689 std::scoped_lock _l(mLock);
690
691 InputDevice* device = findInputDeviceLocked(deviceId);
692 if (device) {
693 return device->isVibrating();
694 }
695 return false;
696}
697
698std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
699 std::scoped_lock _l(mLock);
700
701 InputDevice* device = findInputDeviceLocked(deviceId);
702 if (device) {
703 return device->getVibratorIds();
704 }
705 return {};
706}
707
Chris Yef59a2f42020-10-16 12:55:26 -0700708void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
709 std::scoped_lock _l(mLock);
710
711 InputDevice* device = findInputDeviceLocked(deviceId);
712 if (device) {
713 device->disableSensor(sensorType);
714 }
715}
716
717bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
718 std::chrono::microseconds samplingPeriod,
719 std::chrono::microseconds maxBatchReportLatency) {
720 std::scoped_lock _l(mLock);
721
722 InputDevice* device = findInputDeviceLocked(deviceId);
723 if (device) {
724 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
725 }
726 return false;
727}
728
729void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
730 std::scoped_lock _l(mLock);
731
732 InputDevice* device = findInputDeviceLocked(deviceId);
733 if (device) {
734 device->flushSensor(sensorType);
735 }
736}
737
Kim Low03ea0352020-11-06 12:45:07 -0800738std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400739 std::optional<int32_t> eventHubId;
740 {
741 // Do not query the battery state while holding the lock. For some peripheral devices,
742 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
743 // would block all other event processing during this time. For now, we assume this
744 // call never happens on the InputReader thread and get the battery state outside the
745 // lock to prevent event processing from being blocked by this call.
746 std::scoped_lock _l(mLock);
747 InputDevice* device = findInputDeviceLocked(deviceId);
748 if (!device) return {};
749 eventHubId = device->getBatteryEventHubId();
750 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800751
Andy Chenf9f1a022022-08-29 20:07:10 -0400752 if (!eventHubId) return {};
753 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000754 if (batteryIds.empty()) {
755 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
756 return {};
757 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400758 return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800759}
760
761std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400762 std::optional<int32_t> eventHubId;
763 {
764 // Do not query the battery state while holding the lock. For some peripheral devices,
765 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
766 // would block all other event processing during this time. For now, we assume this
767 // call never happens on the InputReader thread and get the battery state outside the
768 // lock to prevent event processing from being blocked by this call.
769 std::scoped_lock _l(mLock);
770 InputDevice* device = findInputDeviceLocked(deviceId);
771 if (!device) return {};
772 eventHubId = device->getBatteryEventHubId();
773 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800774
Andy Chenf9f1a022022-08-29 20:07:10 -0400775 if (!eventHubId) return {};
776 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000777 if (batteryIds.empty()) {
778 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
779 return {};
780 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400781 return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800782}
783
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000784std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) {
785 std::scoped_lock _l(mLock);
786
787 InputDevice* device = findInputDeviceLocked(deviceId);
788 if (!device) return {};
789
790 std::optional<int32_t> eventHubId = device->getBatteryEventHubId();
791 if (!eventHubId) return {};
792 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
793 if (batteryIds.empty()) {
794 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
795 return {};
796 }
797 const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front());
798 if (!batteryInfo) {
799 ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__,
800 batteryIds.front(), *eventHubId);
801 return {};
802 }
803 return batteryInfo->path;
804}
805
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000806std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800807 std::scoped_lock _l(mLock);
808
809 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000810 if (device == nullptr) {
811 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800812 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000813
814 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800815}
816
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000817std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800818 std::scoped_lock _l(mLock);
819
820 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000821 if (device == nullptr) {
822 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800823 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000824
825 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800826}
827
828bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
829 std::scoped_lock _l(mLock);
830
831 InputDevice* device = findInputDeviceLocked(deviceId);
832 if (device) {
833 return device->setLightColor(lightId, color);
834 }
835 return false;
836}
837
838bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
839 std::scoped_lock _l(mLock);
840
841 InputDevice* device = findInputDeviceLocked(deviceId);
842 if (device) {
843 return device->setLightPlayerId(lightId, playerId);
844 }
845 return false;
846}
847
848std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
849 std::scoped_lock _l(mLock);
850
851 InputDevice* device = findInputDeviceLocked(deviceId);
852 if (device) {
853 return device->getLightColor(lightId);
854 }
855 return std::nullopt;
856}
857
858std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
859 std::scoped_lock _l(mLock);
860
861 InputDevice* device = findInputDeviceLocked(deviceId);
862 if (device) {
863 return device->getLightPlayerId(lightId);
864 }
865 return std::nullopt;
866}
867
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000868std::optional<std::string> InputReader::getBluetoothAddress(int32_t deviceId) const {
869 std::scoped_lock _l(mLock);
870
871 InputDevice* device = findInputDeviceLocked(deviceId);
872 if (device) {
873 return device->getBluetoothAddress();
874 }
875 return std::nullopt;
876}
877
Linnan Li13bf76a2024-05-05 19:18:02 +0800878bool InputReader::canDispatchToDisplay(int32_t deviceId, ui::LogicalDisplayId displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000879 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800880
Chris Ye1c2e0892020-11-30 21:41:44 -0800881 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800882 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800883 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
884 return false;
885 }
886
Arthur Hung2c9a3342019-07-23 14:18:59 +0800887 if (!device->isEnabled()) {
888 ALOGW("Ignoring disabled device %s", device->getName().c_str());
889 return false;
890 }
891
Linnan Li13bf76a2024-05-05 19:18:02 +0800892 std::optional<ui::LogicalDisplayId> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800893 // No associated display. By default, can dispatch to all displays.
Linnan Li13bf76a2024-05-05 19:18:02 +0800894 if (!associatedDisplayId || !associatedDisplayId->isValid()) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800895 return true;
896 }
897
898 return *associatedDisplayId == displayId;
899}
900
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000901void InputReader::sysfsNodeChanged(const std::string& sysfsNodePath) {
902 mEventHub->sysfsNodeChanged(sysfsNodePath);
903}
904
Prabir Pradhan018faea2024-05-08 21:52:54 +0000905DeviceId InputReader::getLastUsedInputDeviceId() {
906 std::scoped_lock _l(mLock);
907 return mLastUsedDeviceId;
908}
909
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800910void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000911 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912
913 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800914 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915
Chris Yee7310032020-09-22 15:36:28 -0700916 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
917 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918
Chris Yee7310032020-09-22 15:36:28 -0700919 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
920 const std::shared_ptr<InputDevice>& device = devicePair.first;
921 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
922 for (const auto& eId : devicePair.second) {
923 eventHubDevStr += StringPrintf("%d ", eId);
924 }
925 eventHubDevStr += "] \n";
926 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927 }
928
Harry Cutts8c7cb592023-08-23 17:20:13 +0000929 dump += StringPrintf(INDENT "NextTimeout: %" PRId64 "\n", mNextTimeout);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800930 dump += INDENT "Configuration:\n";
931 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
933 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800934 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100936 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800938 dump += "]\n";
939 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700940 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800942 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700943 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
944 "acceleration=%0.3f\n",
945 mConfig.pointerVelocityControlParameters.scale,
946 mConfig.pointerVelocityControlParameters.lowThreshold,
947 mConfig.pointerVelocityControlParameters.highThreshold,
948 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800950 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700951 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
952 "acceleration=%0.3f\n",
953 mConfig.wheelVelocityControlParameters.scale,
954 mConfig.wheelVelocityControlParameters.lowThreshold,
955 mConfig.wheelVelocityControlParameters.highThreshold,
956 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800957
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800958 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700959 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800960 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700961 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800962 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700963 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800964 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700965 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800966 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700967 mConfig.pointerGestureTapDragInterval * 0.000001f);
968 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800969 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700970 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800971 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700972 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800973 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700974 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800975 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700976 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800977 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700978 mConfig.pointerGestureMovementSpeedRatio);
979 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700980
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800981 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700982 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983}
984
985void InputReader::monitor() {
986 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800987 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800988 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800989 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 // Check the EventHub
991 mEventHub->monitor();
992}
993
Michael Wrightd02c5b62014-02-10 15:10:22 -0800994// --- InputReader::ContextImpl ---
995
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800996InputReader::ContextImpl::ContextImpl(InputReader* reader)
997 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998
999void InputReader::ContextImpl::updateGlobalMetaState() {
1000 // lock is already held by the input loop
1001 mReader->updateGlobalMetaStateLocked();
1002}
1003
1004int32_t InputReader::ContextImpl::getGlobalMetaState() {
1005 // lock is already held by the input loop
1006 return mReader->getGlobalMetaStateLocked();
1007}
1008
arthurhungc903df12020-08-11 15:08:42 +08001009void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
1010 // lock is already held by the input loop
1011 mReader->updateLedMetaStateLocked(metaState);
1012}
1013
1014int32_t InputReader::ContextImpl::getLedMetaState() {
1015 // lock is already held by the input loop
1016 return mReader->getLedMetaStateLocked();
1017}
1018
Arpit Singha5ea7c12023-07-05 15:39:25 +00001019void InputReader::ContextImpl::setPreventingTouchpadTaps(bool prevent) {
1020 // lock is already held by the input loop
1021 mReader->mPreventingTouchpadTaps = prevent;
1022}
1023
1024bool InputReader::ContextImpl::isPreventingTouchpadTaps() {
1025 // lock is already held by the input loop
1026 return mReader->mPreventingTouchpadTaps;
1027}
1028
Arpit Singh82e413e2023-10-10 19:30:58 +00001029void InputReader::ContextImpl::setLastKeyDownTimestamp(nsecs_t when) {
1030 mReader->mLastKeyDownTimestamp = when;
1031}
1032
1033nsecs_t InputReader::ContextImpl::getLastKeyDownTimestamp() {
1034 return mReader->mLastKeyDownTimestamp;
1035}
1036
Michael Wrightd02c5b62014-02-10 15:10:22 -08001037void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
1038 // lock is already held by the input loop
1039 mReader->disableVirtualKeysUntilLocked(time);
1040}
1041
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001042bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
1043 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001044 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001045 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046}
1047
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
1049 // lock is already held by the input loop
1050 mReader->requestTimeoutAtTimeLocked(when);
1051}
1052
1053int32_t InputReader::ContextImpl::bumpGeneration() {
1054 // lock is already held by the input loop
1055 return mReader->bumpGenerationLocked();
1056}
1057
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001058void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -07001059 // lock is already held by whatever called refreshConfigurationLocked
1060 mReader->getExternalStylusDevicesLocked(outDevices);
1061}
1062
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001063std::list<NotifyArgs> InputReader::ContextImpl::dispatchExternalStylusState(
1064 const StylusState& state) {
1065 return mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -07001066}
1067
Michael Wrightd02c5b62014-02-10 15:10:22 -08001068InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
1069 return mReader->mPolicy.get();
1070}
1071
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072EventHubInterface* InputReader::ContextImpl::getEventHub() {
1073 return mReader->mEventHub.get();
1074}
1075
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00001076int32_t InputReader::ContextImpl::getNextId() {
1077 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -08001078}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00001080KeyboardClassifier& InputReader::ContextImpl::getKeyboardClassifier() {
1081 return *mReader->mKeyboardClassifier;
1082}
1083
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084} // namespace android