blob: 69555f89617401f610b4458329a54e1711b1238a [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),
Arthur Hung95f68612022-04-07 14:08:22 +0800105 mGlobalMetaState(AMETA_NONE),
106 mLedMetaState(AMETA_NONE),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700107 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800108 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700109 mDisableVirtualKeysTimeout(LLONG_MIN),
110 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111 mConfigurationChangesToRefresh(0) {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000112 refreshConfigurationLocked(/*changes=*/{});
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700113 updateGlobalMetaStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114}
115
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000116InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117
Prabir Pradhan28efc192019-11-05 01:10:04 +0000118status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700119 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000120 return ALREADY_EXISTS;
121 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700122 mThread = std::make_unique<InputThread>(
123 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
124 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000125}
126
127status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700128 if (mThread && mThread->isCallingThread()) {
129 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +0000130 return INVALID_OPERATION;
131 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700132 mThread.reset();
133 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000134}
135
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136void InputReader::loopOnce() {
137 int32_t oldGeneration;
138 int32_t timeoutMillis;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000139 // Copy some state so that we can access it outside the lock later.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -0800141 std::vector<InputDeviceInfo> inputDevices;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000142 std::list<NotifyArgs> notifyArgs;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000144 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145
146 oldGeneration = mGeneration;
147 timeoutMillis = -1;
148
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000149 auto changes = mConfigurationChangesToRefresh;
150 if (changes.any()) {
151 mConfigurationChangesToRefresh.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152 timeoutMillis = 0;
153 refreshConfigurationLocked(changes);
154 } else if (mNextTimeout != LLONG_MAX) {
155 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
156 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
157 }
158 } // release lock
159
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700160 std::vector<RawEvent> events = mEventHub->getEvents(timeoutMillis);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161
162 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000163 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800164 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700166 if (!events.empty()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700167 mPendingArgs += processEventsLocked(events.data(), events.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168 }
169
170 if (mNextTimeout != LLONG_MAX) {
171 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
172 if (now >= mNextTimeout) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000173 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800174 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
175 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176 mNextTimeout = LLONG_MAX;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700177 mPendingArgs += timeoutExpiredLocked(now);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178 }
179 }
180
181 if (oldGeneration != mGeneration) {
182 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800183 inputDevices = getInputDevicesLocked();
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700184 mPendingArgs.emplace_back(
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000185 NotifyInputDevicesChangedArgs{mContext.getNextId(), inputDevices});
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700187
188 std::swap(notifyArgs, mPendingArgs);
Prabir Pradhan018faea2024-05-08 21:52:54 +0000189
190 // Keep track of the last used device
191 for (const NotifyArgs& args : notifyArgs) {
192 mLastUsedDeviceId = getDeviceIdOfNewGesture(args).value_or(mLastUsedDeviceId);
193 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800194 } // release lock
195
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 // Flush queued events out to the listener.
197 // This must happen outside of the lock because the listener could potentially call
198 // back into the InputReader's methods, such as getScanCodeState, or become blocked
199 // on another thread similarly waiting to acquire the InputReader lock thereby
200 // resulting in a deadlock. This situation is actually quite plausible because the
201 // listener is actually the input dispatcher, which calls into the window manager,
202 // which occasionally calls into the input reader.
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700203 for (const NotifyArgs& args : notifyArgs) {
204 mNextListener.notify(args);
205 }
Prabir Pradhanc3a92472024-02-06 20:08:05 +0000206
207 // Notify the policy that input devices have changed.
208 // This must be done after flushing events down the listener chain to ensure that the rest of
209 // the listeners are synchronized with the changes before the policy reacts to them.
210 if (inputDevicesChanged) {
211 mPolicy->notifyInputDevicesChanged(inputDevices);
212 }
213
214 // Notify the policy of the start of every new stylus gesture.
215 for (const auto& args : notifyArgs) {
216 const auto* motionArgs = std::get_if<NotifyMotionArgs>(&args);
217 if (motionArgs != nullptr && isStylusPointerGestureStart(*motionArgs)) {
218 mPolicy->notifyStylusGestureStarted(motionArgs->deviceId, motionArgs->eventTime);
219 }
220 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800221}
222
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700223std::list<NotifyArgs> InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
224 std::list<NotifyArgs> out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225 for (const RawEvent* rawEvent = rawEvents; count;) {
226 int32_t type = rawEvent->type;
227 size_t batchSize = 1;
228 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
229 int32_t deviceId = rawEvent->deviceId;
230 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700231 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
232 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800233 break;
234 }
235 batchSize += 1;
236 }
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000237 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800238 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
239 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700240 out += processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800241 } else {
242 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700243 case EventHubInterface::DEVICE_ADDED:
244 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
245 break;
246 case EventHubInterface::DEVICE_REMOVED:
247 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
248 break;
249 case EventHubInterface::FINISHED_DEVICE_SCAN:
250 handleConfigurationChangedLocked(rawEvent->when);
251 break;
252 default:
253 ALOG_ASSERT(false); // can't happen
254 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800255 }
256 }
257 count -= batchSize;
258 rawEvent += batchSize;
259 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700260 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261}
262
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800263void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
264 if (mDevices.find(eventHubId) != mDevices.end()) {
265 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800266 return;
267 }
268
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800269 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
Arpit Singh82f29a12023-06-13 15:05:53 +0000270 std::shared_ptr<InputDevice> device = createDeviceLocked(when, eventHubId, identifier);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700271
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700272 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
273 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274
275 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800276 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
277 "(ignored non-input device)",
278 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000280 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800281 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000282 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 }
284
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800285 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700286 // Add device to device to EventHub ids map.
287 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
288 if (mapIt == mDeviceToEventHubIdsMap.end()) {
289 std::vector<int32_t> ids = {eventHubId};
290 mDeviceToEventHubIdsMap.emplace(device, ids);
291 } else {
292 mapIt->second.push_back(eventHubId);
293 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700295
Chris Ye1b0c7342020-07-28 21:57:03 -0700296 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800297 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700298 }
Chris Yef59a2f42020-10-16 12:55:26 -0700299
300 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800301 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
302 // device class to disable SENSOR sub device only.
303 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700304 mEventHub->disableDevice(eventHubId);
305 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800306}
307
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800308void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
309 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000310 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800311 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800312 return;
313 }
314
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000315 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000316 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700317 // Erase device from device to EventHub ids map.
318 auto mapIt = mDeviceToEventHubIdsMap.find(device);
319 if (mapIt != mDeviceToEventHubIdsMap.end()) {
320 std::vector<int32_t>& eventHubIds = mapIt->second;
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -0800321 std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
Chris Yee7310032020-09-22 15:36:28 -0700322 if (eventHubIds.size() == 0) {
323 mDeviceToEventHubIdsMap.erase(mapIt);
324 }
325 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 bumpGenerationLocked();
327
328 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800329 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
330 "(ignored non-input device)",
331 device->getId(), eventHubId, device->getName().c_str(),
332 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000334 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800335 device->getId(), eventHubId, device->getName().c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000336 device->getDescriptor().c_str(),
337 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 }
339
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800340 device->removeEventHubDevice(eventHubId);
341
Chris Ye1b0c7342020-07-28 21:57:03 -0700342 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800343 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700344 }
345
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800346 if (device->hasEventHubDevices()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700347 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800348 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700349 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350}
351
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000352std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Arpit Singh82f29a12023-06-13 15:05:53 +0000353 nsecs_t when, int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800354 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
Josh Bartel938632f2022-07-19 15:34:22 -0500355 const InputDeviceIdentifier identifier2 =
356 devicePair.second->getDeviceInfo().getIdentifier();
357 return isSubDevice(identifier, identifier2);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800358 });
359
360 std::shared_ptr<InputDevice> device;
361 if (deviceIt != mDevices.end()) {
362 device = deviceIt->second;
363 } else {
364 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
365 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
366 identifier);
367 }
Arpit Singh82f29a12023-06-13 15:05:53 +0000368 mPendingArgs += device->addEventHubDevice(when, eventHubId, mConfig);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369 return device;
370}
371
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700372std::list<NotifyArgs> InputReader::processEventsForDeviceLocked(int32_t eventHubId,
373 const RawEvent* rawEvents,
374 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800375 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000376 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800377 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700378 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800379 }
380
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000381 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700383 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700384 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 }
386
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700387 return device->process(rawEvents, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388}
389
Philip Junker4af3b3d2021-12-14 10:36:55 +0100390InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800391 auto deviceIt =
392 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
393 return devicePair.second->getId() == deviceId;
394 });
395 if (deviceIt != mDevices.end()) {
396 return deviceIt->second.get();
397 }
398 return nullptr;
399}
400
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700401std::list<NotifyArgs> InputReader::timeoutExpiredLocked(nsecs_t when) {
402 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000403 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000404 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 if (!device->isIgnored()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700406 out += device->timeoutExpired(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 }
408 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700409 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410}
411
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800412int32_t InputReader::nextInputDeviceIdLocked() {
413 return ++mNextInputDeviceId;
414}
415
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
417 // Reset global meta state because it depends on the list of all configured devices.
418 updateGlobalMetaStateLocked();
419
420 // Enqueue configuration changed.
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700421 mPendingArgs.emplace_back(NotifyConfigurationChangedArgs{mContext.getNextId(), when});
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422}
423
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000424void InputReader::refreshConfigurationLocked(ConfigurationChanges changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425 mPolicy->getReaderConfiguration(&mConfig);
426 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
427
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000428 using Change = InputReaderConfiguration::Change;
429 if (!changes.any()) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800430
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000431 ALOGI("Reconfiguring input devices, changes=%s", changes.string().c_str());
Prabir Pradhan7e186182020-11-10 13:56:45 -0800432 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800433
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000434 if (changes.test(Change::MUST_REOPEN)) {
Prabir Pradhan7e186182020-11-10 13:56:45 -0800435 mEventHub->requestReopenDevices();
436 } else {
437 for (auto& devicePair : mDevices) {
438 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700439 mPendingArgs += device->configure(now, mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440 }
441 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800442
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000443 if (changes.test(Change::POINTER_CAPTURE)) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000444 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
445 ALOGV("Skipping notifying pointer capture changes: "
446 "There was no change in the pointer capture state.");
447 } else {
448 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700449 mPendingArgs.emplace_back(
450 NotifyPointerCaptureChangedArgs{mContext.getNextId(), now,
451 mCurrentPointerCaptureRequest});
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000452 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800453 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800454}
455
456void InputReader::updateGlobalMetaStateLocked() {
457 mGlobalMetaState = 0;
458
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000459 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000460 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 mGlobalMetaState |= device->getMetaState();
462 }
463}
464
465int32_t InputReader::getGlobalMetaStateLocked() {
466 return mGlobalMetaState;
467}
468
arthurhungc903df12020-08-11 15:08:42 +0800469void InputReader::updateLedMetaStateLocked(int32_t metaState) {
470 mLedMetaState = metaState;
471 for (auto& devicePair : mDevices) {
472 std::shared_ptr<InputDevice>& device = devicePair.second;
473 device->updateLedState(false);
474 }
475}
476
477int32_t InputReader::getLedMetaStateLocked() {
478 return mLedMetaState;
479}
480
Chris Ye1c2e0892020-11-30 21:41:44 -0800481void InputReader::notifyExternalStylusPresenceChangedLocked() {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000482 refreshConfigurationLocked(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Michael Wright842500e2015-03-13 17:32:02 -0700483}
484
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800485void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000486 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000487 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700488 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000489 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700490 }
491 }
492}
493
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700494std::list<NotifyArgs> InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
495 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000496 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000497 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700498 out += device->updateExternalStylusState(state);
Michael Wright842500e2015-03-13 17:32:02 -0700499 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700500 return out;
Michael Wright842500e2015-03-13 17:32:02 -0700501}
502
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
504 mDisableVirtualKeysTimeout = time;
505}
506
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800507bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800508 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800509 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700510 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800511 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512 return true;
513 } else {
514 return false;
515 }
516}
517
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
519 if (when < mNextTimeout) {
520 mNextTimeout = when;
521 mEventHub->wake();
522 }
523}
524
525int32_t InputReader::bumpGenerationLocked() {
526 return ++mGeneration;
527}
528
Chris Ye98d3f532020-10-01 21:48:59 -0700529std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000530 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700531 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800532}
533
Chris Ye98d3f532020-10-01 21:48:59 -0700534std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
535 std::vector<InputDeviceInfo> outInputDevices;
536 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537
Chris Yee7310032020-09-22 15:36:28 -0700538 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000540 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800541 }
542 }
Chris Ye98d3f532020-10-01 21:48:59 -0700543 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544}
545
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700546int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000547 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800548
549 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
550}
551
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700552int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000553 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554
555 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
556}
557
558int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000559 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800560
561 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
562}
563
564int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700565 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 int32_t result = AKEY_STATE_UNKNOWN;
567 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800568 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800569 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
570 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571 }
572 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000573 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000574 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700575 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
577 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000578 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 if (currentResult >= AKEY_STATE_DOWN) {
580 return currentResult;
581 } else if (currentResult == AKEY_STATE_UP) {
582 result = currentResult;
583 }
584 }
585 }
586 }
587 return result;
588}
589
Andrii Kulian763a3a42016-03-08 10:46:16 -0800590void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000591 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800592 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800593 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800594 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
595 return;
596 }
597
Andrii Kulian763a3a42016-03-08 10:46:16 -0800598 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000599 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800600 return;
601 }
602
603 device->updateMetaState(AKEYCODE_CAPS_LOCK);
604}
605
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700606bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
607 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000608 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700610 memset(outFlags, 0, keyCodes.size());
611 return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612}
613
614bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700615 const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700616 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 bool result = false;
618 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800619 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800620 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700621 result = device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 }
623 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000624 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000625 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700626 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700627 result |= device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628 }
629 }
630 }
631 return result;
632}
633
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000634void InputReader::addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const {
635 std::scoped_lock _l(mLock);
636
637 InputDevice* device = findInputDeviceLocked(deviceId);
638 if (device != nullptr) {
639 device->addKeyRemapping(fromKeyCode, toKeyCode);
640 }
641}
642
Philip Junker4af3b3d2021-12-14 10:36:55 +0100643int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
644 std::scoped_lock _l(mLock);
645
646 InputDevice* device = findInputDeviceLocked(deviceId);
647 if (device == nullptr) {
648 ALOGW("Failed to get key code for key location: Input device with id %d not found",
649 deviceId);
650 return AKEYCODE_UNKNOWN;
651 }
652 return device->getKeyCodeForKeyLocation(locationKeyCode);
653}
654
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000655void InputReader::requestRefreshConfiguration(ConfigurationChanges changes) {
Chris Ye87143712020-11-10 05:05:58 +0000656 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000658 if (changes.any()) {
659 bool needWake = !mConfigurationChangesToRefresh.any();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800660 mConfigurationChangesToRefresh |= changes;
661
662 if (needWake) {
663 mEventHub->wake();
664 }
665 }
666}
667
Chris Ye87143712020-11-10 05:05:58 +0000668void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
669 int32_t token) {
670 std::scoped_lock _l(mLock);
671
Chris Ye1c2e0892020-11-30 21:41:44 -0800672 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800673 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700674 mPendingArgs += device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 }
676}
677
678void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000679 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680
Chris Ye1c2e0892020-11-30 21:41:44 -0800681 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800682 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700683 mPendingArgs += device->cancelVibrate(token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800684 }
685}
686
Chris Ye87143712020-11-10 05:05:58 +0000687bool InputReader::isVibrating(int32_t deviceId) {
688 std::scoped_lock _l(mLock);
689
690 InputDevice* device = findInputDeviceLocked(deviceId);
691 if (device) {
692 return device->isVibrating();
693 }
694 return false;
695}
696
697std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
698 std::scoped_lock _l(mLock);
699
700 InputDevice* device = findInputDeviceLocked(deviceId);
701 if (device) {
702 return device->getVibratorIds();
703 }
704 return {};
705}
706
Chris Yef59a2f42020-10-16 12:55:26 -0700707void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
708 std::scoped_lock _l(mLock);
709
710 InputDevice* device = findInputDeviceLocked(deviceId);
711 if (device) {
712 device->disableSensor(sensorType);
713 }
714}
715
716bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
717 std::chrono::microseconds samplingPeriod,
718 std::chrono::microseconds maxBatchReportLatency) {
719 std::scoped_lock _l(mLock);
720
721 InputDevice* device = findInputDeviceLocked(deviceId);
722 if (device) {
723 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
724 }
725 return false;
726}
727
728void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
729 std::scoped_lock _l(mLock);
730
731 InputDevice* device = findInputDeviceLocked(deviceId);
732 if (device) {
733 device->flushSensor(sensorType);
734 }
735}
736
Kim Low03ea0352020-11-06 12:45:07 -0800737std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400738 std::optional<int32_t> eventHubId;
739 {
740 // Do not query the battery state while holding the lock. For some peripheral devices,
741 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
742 // would block all other event processing during this time. For now, we assume this
743 // call never happens on the InputReader thread and get the battery state outside the
744 // lock to prevent event processing from being blocked by this call.
745 std::scoped_lock _l(mLock);
746 InputDevice* device = findInputDeviceLocked(deviceId);
747 if (!device) return {};
748 eventHubId = device->getBatteryEventHubId();
749 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800750
Andy Chenf9f1a022022-08-29 20:07:10 -0400751 if (!eventHubId) return {};
752 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000753 if (batteryIds.empty()) {
754 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
755 return {};
756 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400757 return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800758}
759
760std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400761 std::optional<int32_t> eventHubId;
762 {
763 // Do not query the battery state while holding the lock. For some peripheral devices,
764 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
765 // would block all other event processing during this time. For now, we assume this
766 // call never happens on the InputReader thread and get the battery state outside the
767 // lock to prevent event processing from being blocked by this call.
768 std::scoped_lock _l(mLock);
769 InputDevice* device = findInputDeviceLocked(deviceId);
770 if (!device) return {};
771 eventHubId = device->getBatteryEventHubId();
772 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800773
Andy Chenf9f1a022022-08-29 20:07:10 -0400774 if (!eventHubId) return {};
775 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000776 if (batteryIds.empty()) {
777 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
778 return {};
779 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400780 return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800781}
782
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000783std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) {
784 std::scoped_lock _l(mLock);
785
786 InputDevice* device = findInputDeviceLocked(deviceId);
787 if (!device) return {};
788
789 std::optional<int32_t> eventHubId = device->getBatteryEventHubId();
790 if (!eventHubId) return {};
791 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
792 if (batteryIds.empty()) {
793 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
794 return {};
795 }
796 const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front());
797 if (!batteryInfo) {
798 ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__,
799 batteryIds.front(), *eventHubId);
800 return {};
801 }
802 return batteryInfo->path;
803}
804
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000805std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800806 std::scoped_lock _l(mLock);
807
808 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000809 if (device == nullptr) {
810 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800811 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000812
813 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800814}
815
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000816std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800817 std::scoped_lock _l(mLock);
818
819 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000820 if (device == nullptr) {
821 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800822 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000823
824 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800825}
826
827bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
828 std::scoped_lock _l(mLock);
829
830 InputDevice* device = findInputDeviceLocked(deviceId);
831 if (device) {
832 return device->setLightColor(lightId, color);
833 }
834 return false;
835}
836
837bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
838 std::scoped_lock _l(mLock);
839
840 InputDevice* device = findInputDeviceLocked(deviceId);
841 if (device) {
842 return device->setLightPlayerId(lightId, playerId);
843 }
844 return false;
845}
846
847std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
848 std::scoped_lock _l(mLock);
849
850 InputDevice* device = findInputDeviceLocked(deviceId);
851 if (device) {
852 return device->getLightColor(lightId);
853 }
854 return std::nullopt;
855}
856
857std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
858 std::scoped_lock _l(mLock);
859
860 InputDevice* device = findInputDeviceLocked(deviceId);
861 if (device) {
862 return device->getLightPlayerId(lightId);
863 }
864 return std::nullopt;
865}
866
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000867std::optional<std::string> InputReader::getBluetoothAddress(int32_t deviceId) const {
868 std::scoped_lock _l(mLock);
869
870 InputDevice* device = findInputDeviceLocked(deviceId);
871 if (device) {
872 return device->getBluetoothAddress();
873 }
874 return std::nullopt;
875}
876
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700877bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000878 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700879
Chris Ye1c2e0892020-11-30 21:41:44 -0800880 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800881 if (device) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700882 return device->isEnabled();
883 }
884 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
885 return false;
886}
887
Linnan Li13bf76a2024-05-05 19:18:02 +0800888bool InputReader::canDispatchToDisplay(int32_t deviceId, ui::LogicalDisplayId displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000889 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800890
Chris Ye1c2e0892020-11-30 21:41:44 -0800891 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800892 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800893 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
894 return false;
895 }
896
Arthur Hung2c9a3342019-07-23 14:18:59 +0800897 if (!device->isEnabled()) {
898 ALOGW("Ignoring disabled device %s", device->getName().c_str());
899 return false;
900 }
901
Linnan Li13bf76a2024-05-05 19:18:02 +0800902 std::optional<ui::LogicalDisplayId> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800903 // No associated display. By default, can dispatch to all displays.
Linnan Li13bf76a2024-05-05 19:18:02 +0800904 if (!associatedDisplayId || !associatedDisplayId->isValid()) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800905 return true;
906 }
907
908 return *associatedDisplayId == displayId;
909}
910
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000911void InputReader::sysfsNodeChanged(const std::string& sysfsNodePath) {
912 mEventHub->sysfsNodeChanged(sysfsNodePath);
913}
914
Prabir Pradhan018faea2024-05-08 21:52:54 +0000915DeviceId InputReader::getLastUsedInputDeviceId() {
916 std::scoped_lock _l(mLock);
917 return mLastUsedDeviceId;
918}
919
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800920void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000921 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922
923 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800924 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925
Chris Yee7310032020-09-22 15:36:28 -0700926 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
927 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928
Chris Yee7310032020-09-22 15:36:28 -0700929 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
930 const std::shared_ptr<InputDevice>& device = devicePair.first;
931 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
932 for (const auto& eId : devicePair.second) {
933 eventHubDevStr += StringPrintf("%d ", eId);
934 }
935 eventHubDevStr += "] \n";
936 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 }
938
Harry Cutts8c7cb592023-08-23 17:20:13 +0000939 dump += StringPrintf(INDENT "NextTimeout: %" PRId64 "\n", mNextTimeout);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800940 dump += INDENT "Configuration:\n";
941 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
943 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800944 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100946 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800948 dump += "]\n";
949 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700950 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800952 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700953 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
954 "acceleration=%0.3f\n",
955 mConfig.pointerVelocityControlParameters.scale,
956 mConfig.pointerVelocityControlParameters.lowThreshold,
957 mConfig.pointerVelocityControlParameters.highThreshold,
958 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800960 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700961 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
962 "acceleration=%0.3f\n",
963 mConfig.wheelVelocityControlParameters.scale,
964 mConfig.wheelVelocityControlParameters.lowThreshold,
965 mConfig.wheelVelocityControlParameters.highThreshold,
966 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800968 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700969 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800970 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700971 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800972 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700973 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800974 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700975 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800976 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700977 mConfig.pointerGestureTapDragInterval * 0.000001f);
978 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800979 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700980 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800981 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700982 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800983 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700984 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800985 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700986 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800987 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700988 mConfig.pointerGestureMovementSpeedRatio);
989 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700990
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800991 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700992 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993}
994
995void InputReader::monitor() {
996 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800997 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800999 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000 // Check the EventHub
1001 mEventHub->monitor();
1002}
1003
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004// --- InputReader::ContextImpl ---
1005
Garfield Tanff1f1bb2020-01-28 13:24:04 -08001006InputReader::ContextImpl::ContextImpl(InputReader* reader)
1007 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001008
1009void InputReader::ContextImpl::updateGlobalMetaState() {
1010 // lock is already held by the input loop
1011 mReader->updateGlobalMetaStateLocked();
1012}
1013
1014int32_t InputReader::ContextImpl::getGlobalMetaState() {
1015 // lock is already held by the input loop
1016 return mReader->getGlobalMetaStateLocked();
1017}
1018
arthurhungc903df12020-08-11 15:08:42 +08001019void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
1020 // lock is already held by the input loop
1021 mReader->updateLedMetaStateLocked(metaState);
1022}
1023
1024int32_t InputReader::ContextImpl::getLedMetaState() {
1025 // lock is already held by the input loop
1026 return mReader->getLedMetaStateLocked();
1027}
1028
Arpit Singha5ea7c12023-07-05 15:39:25 +00001029void InputReader::ContextImpl::setPreventingTouchpadTaps(bool prevent) {
1030 // lock is already held by the input loop
1031 mReader->mPreventingTouchpadTaps = prevent;
1032}
1033
1034bool InputReader::ContextImpl::isPreventingTouchpadTaps() {
1035 // lock is already held by the input loop
1036 return mReader->mPreventingTouchpadTaps;
1037}
1038
Arpit Singh82e413e2023-10-10 19:30:58 +00001039void InputReader::ContextImpl::setLastKeyDownTimestamp(nsecs_t when) {
1040 mReader->mLastKeyDownTimestamp = when;
1041}
1042
1043nsecs_t InputReader::ContextImpl::getLastKeyDownTimestamp() {
1044 return mReader->mLastKeyDownTimestamp;
1045}
1046
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
1048 // lock is already held by the input loop
1049 mReader->disableVirtualKeysUntilLocked(time);
1050}
1051
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001052bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
1053 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001054 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001055 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001056}
1057
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
1059 // lock is already held by the input loop
1060 mReader->requestTimeoutAtTimeLocked(when);
1061}
1062
1063int32_t InputReader::ContextImpl::bumpGeneration() {
1064 // lock is already held by the input loop
1065 return mReader->bumpGenerationLocked();
1066}
1067
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001068void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -07001069 // lock is already held by whatever called refreshConfigurationLocked
1070 mReader->getExternalStylusDevicesLocked(outDevices);
1071}
1072
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001073std::list<NotifyArgs> InputReader::ContextImpl::dispatchExternalStylusState(
1074 const StylusState& state) {
1075 return mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -07001076}
1077
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
1079 return mReader->mPolicy.get();
1080}
1081
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082EventHubInterface* InputReader::ContextImpl::getEventHub() {
1083 return mReader->mEventHub.get();
1084}
1085
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00001086int32_t InputReader::ContextImpl::getNextId() {
1087 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -08001088}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001089
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090} // namespace android