blob: 8b664d5c4e9771fc5128fb43c4249c419822a5f7 [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"
Omar Abdelmonem5e70e962024-08-06 09:38:42 +000036#include "include/gestures.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080037
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080038using android::base::StringPrintf;
39
Michael Wrightd02c5b62014-02-10 15:10:22 -080040namespace android {
41
Prabir Pradhan018faea2024-05-08 21:52:54 +000042namespace {
43
Josh Bartel938632f2022-07-19 15:34:22 -050044/**
45 * Determines if the identifiers passed are a sub-devices. Sub-devices are physical devices
46 * that expose multiple input device paths such a keyboard that also has a touchpad input.
47 * These are separate devices with unique descriptors in EventHub, but InputReader should
48 * create a single InputDevice for them.
49 * Sub-devices are detected by the following criteria:
50 * 1. The vendor, product, bus, version, and unique id match
51 * 2. The location matches. The location is used to distinguish a single device with multiple
52 * inputs versus the same device plugged into multiple ports.
53 */
54
Prabir Pradhan018faea2024-05-08 21:52:54 +000055bool isSubDevice(const InputDeviceIdentifier& identifier1,
56 const InputDeviceIdentifier& identifier2) {
Josh Bartel938632f2022-07-19 15:34:22 -050057 return (identifier1.vendor == identifier2.vendor &&
58 identifier1.product == identifier2.product && identifier1.bus == identifier2.bus &&
59 identifier1.version == identifier2.version &&
60 identifier1.uniqueId == identifier2.uniqueId &&
61 identifier1.location == identifier2.location);
62}
63
Prabir Pradhan018faea2024-05-08 21:52:54 +000064bool isStylusPointerGestureStart(const NotifyMotionArgs& motionArgs) {
Prabir Pradhanda20b172022-09-26 17:01:18 +000065 const auto actionMasked = MotionEvent::getActionMasked(motionArgs.action);
66 if (actionMasked != AMOTION_EVENT_ACTION_HOVER_ENTER &&
67 actionMasked != AMOTION_EVENT_ACTION_DOWN &&
68 actionMasked != AMOTION_EVENT_ACTION_POINTER_DOWN) {
69 return false;
70 }
71 const auto actionIndex = MotionEvent::getActionIndex(motionArgs.action);
Prabir Pradhane5626962022-10-27 20:30:53 +000072 return isStylusToolType(motionArgs.pointerProperties[actionIndex].toolType);
Prabir Pradhanda20b172022-09-26 17:01:18 +000073}
74
Prabir Pradhan018faea2024-05-08 21:52:54 +000075bool isNewGestureStart(const NotifyMotionArgs& motion) {
76 return motion.action == AMOTION_EVENT_ACTION_DOWN ||
77 motion.action == AMOTION_EVENT_ACTION_HOVER_ENTER;
78}
79
80bool isNewGestureStart(const NotifyKeyArgs& key) {
81 return key.action == AKEY_EVENT_ACTION_DOWN;
82}
83
84// Return the event's device ID if it marks the start of a new gesture.
85std::optional<DeviceId> getDeviceIdOfNewGesture(const NotifyArgs& args) {
86 if (const auto* motion = std::get_if<NotifyMotionArgs>(&args); motion != nullptr) {
87 return isNewGestureStart(*motion) ? std::make_optional(motion->deviceId) : std::nullopt;
88 }
89 if (const auto* key = std::get_if<NotifyKeyArgs>(&args); key != nullptr) {
90 return isNewGestureStart(*key) ? std::make_optional(key->deviceId) : std::nullopt;
91 }
92 return std::nullopt;
93}
94
95} // namespace
96
Prabir Pradhan28efc192019-11-05 01:10:04 +000097// --- InputReader ---
98
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070099InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
100 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700101 InputListenerInterface& listener)
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700102 : mContext(this),
103 mEventHub(eventHub),
104 mPolicy(policy),
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700105 mNextListener(listener),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000106 mKeyboardClassifier(std::make_unique<KeyboardClassifier>()),
Arthur Hung95f68612022-04-07 14:08:22 +0800107 mGlobalMetaState(AMETA_NONE),
108 mLedMetaState(AMETA_NONE),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700109 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800110 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700111 mDisableVirtualKeysTimeout(LLONG_MIN),
112 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113 mConfigurationChangesToRefresh(0) {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000114 refreshConfigurationLocked(/*changes=*/{});
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700115 updateGlobalMetaStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116}
117
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000118InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119
Prabir Pradhan28efc192019-11-05 01:10:04 +0000120status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700121 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000122 return ALREADY_EXISTS;
123 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700124 mThread = std::make_unique<InputThread>(
Siarhei Vishniakouf53fa6b2024-09-19 17:42:42 -0700125 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); },
126 /*isInCriticalPath=*/true);
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700127 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000128}
129
130status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700131 if (mThread && mThread->isCallingThread()) {
132 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +0000133 return INVALID_OPERATION;
134 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700135 mThread.reset();
136 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000137}
138
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139void InputReader::loopOnce() {
140 int32_t oldGeneration;
141 int32_t timeoutMillis;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000142 // Copy some state so that we can access it outside the lock later.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -0800144 std::vector<InputDeviceInfo> inputDevices;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000145 std::list<NotifyArgs> notifyArgs;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000147 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148
149 oldGeneration = mGeneration;
150 timeoutMillis = -1;
151
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000152 auto changes = mConfigurationChangesToRefresh;
153 if (changes.any()) {
154 mConfigurationChangesToRefresh.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155 timeoutMillis = 0;
156 refreshConfigurationLocked(changes);
157 } else if (mNextTimeout != LLONG_MAX) {
158 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
159 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
160 }
161 } // release lock
162
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700163 std::vector<RawEvent> events = mEventHub->getEvents(timeoutMillis);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
165 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000166 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800167 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700169 if (!events.empty()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700170 mPendingArgs += processEventsLocked(events.data(), events.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 }
172
173 if (mNextTimeout != LLONG_MAX) {
174 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
175 if (now >= mNextTimeout) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000176 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800177 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
178 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 mNextTimeout = LLONG_MAX;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700180 mPendingArgs += timeoutExpiredLocked(now);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 }
182 }
183
184 if (oldGeneration != mGeneration) {
Liana Kazanova5b8217b2024-07-18 17:44:51 +0000185 // Reset global meta state because it depends on connected input devices.
186 updateGlobalMetaStateLocked();
187
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800189 inputDevices = getInputDevicesLocked();
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700190 mPendingArgs.emplace_back(
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000191 NotifyInputDevicesChangedArgs{mContext.getNextId(), inputDevices});
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700193
194 std::swap(notifyArgs, mPendingArgs);
Prabir Pradhan018faea2024-05-08 21:52:54 +0000195
196 // Keep track of the last used device
197 for (const NotifyArgs& args : notifyArgs) {
198 mLastUsedDeviceId = getDeviceIdOfNewGesture(args).value_or(mLastUsedDeviceId);
199 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200 } // release lock
201
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202 // Flush queued events out to the listener.
203 // This must happen outside of the lock because the listener could potentially call
204 // back into the InputReader's methods, such as getScanCodeState, or become blocked
205 // on another thread similarly waiting to acquire the InputReader lock thereby
206 // resulting in a deadlock. This situation is actually quite plausible because the
207 // listener is actually the input dispatcher, which calls into the window manager,
208 // which occasionally calls into the input reader.
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700209 for (const NotifyArgs& args : notifyArgs) {
210 mNextListener.notify(args);
211 }
Prabir Pradhanc3a92472024-02-06 20:08:05 +0000212
213 // Notify the policy that input devices have changed.
214 // This must be done after flushing events down the listener chain to ensure that the rest of
215 // the listeners are synchronized with the changes before the policy reacts to them.
216 if (inputDevicesChanged) {
217 mPolicy->notifyInputDevicesChanged(inputDevices);
218 }
219
220 // Notify the policy of the start of every new stylus gesture.
221 for (const auto& args : notifyArgs) {
222 const auto* motionArgs = std::get_if<NotifyMotionArgs>(&args);
223 if (motionArgs != nullptr && isStylusPointerGestureStart(*motionArgs)) {
224 mPolicy->notifyStylusGestureStarted(motionArgs->deviceId, motionArgs->eventTime);
225 }
226 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227}
228
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700229std::list<NotifyArgs> InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
230 std::list<NotifyArgs> out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 for (const RawEvent* rawEvent = rawEvents; count;) {
232 int32_t type = rawEvent->type;
233 size_t batchSize = 1;
234 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
235 int32_t deviceId = rawEvent->deviceId;
236 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700237 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
238 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239 break;
240 }
241 batchSize += 1;
242 }
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000243 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800244 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
245 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700246 out += processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247 } else {
248 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700249 case EventHubInterface::DEVICE_ADDED:
250 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
251 break;
252 case EventHubInterface::DEVICE_REMOVED:
253 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
254 break;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700255 default:
256 ALOG_ASSERT(false); // can't happen
257 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258 }
259 }
260 count -= batchSize;
261 rawEvent += batchSize;
262 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700263 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264}
265
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800266void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
267 if (mDevices.find(eventHubId) != mDevices.end()) {
268 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 return;
270 }
271
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800272 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
Arpit Singh82f29a12023-06-13 15:05:53 +0000273 std::shared_ptr<InputDevice> device = createDeviceLocked(when, eventHubId, identifier);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700274
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700275 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
276 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800277
278 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800279 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
280 "(ignored non-input device)",
281 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000283 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800284 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000285 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800286 }
287
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800288 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700289 // Add device to device to EventHub ids map.
290 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
291 if (mapIt == mDeviceToEventHubIdsMap.end()) {
292 std::vector<int32_t> ids = {eventHubId};
293 mDeviceToEventHubIdsMap.emplace(device, ids);
294 } else {
295 mapIt->second.push_back(eventHubId);
296 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700298
Chris Ye1b0c7342020-07-28 21:57:03 -0700299 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800300 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700301 }
Chris Yef59a2f42020-10-16 12:55:26 -0700302
303 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800304 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
305 // device class to disable SENSOR sub device only.
306 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700307 mEventHub->disableDevice(eventHubId);
308 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309}
310
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800311void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
312 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000313 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800314 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800315 return;
316 }
317
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000318 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000319 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700320 // Erase device from device to EventHub ids map.
321 auto mapIt = mDeviceToEventHubIdsMap.find(device);
322 if (mapIt != mDeviceToEventHubIdsMap.end()) {
323 std::vector<int32_t>& eventHubIds = mapIt->second;
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -0800324 std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
Chris Yee7310032020-09-22 15:36:28 -0700325 if (eventHubIds.size() == 0) {
326 mDeviceToEventHubIdsMap.erase(mapIt);
327 }
328 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 bumpGenerationLocked();
330
331 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800332 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
333 "(ignored non-input device)",
334 device->getId(), eventHubId, device->getName().c_str(),
335 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000337 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800338 device->getId(), eventHubId, device->getName().c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000339 device->getDescriptor().c_str(),
340 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 }
342
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800343 device->removeEventHubDevice(eventHubId);
344
Chris Ye1b0c7342020-07-28 21:57:03 -0700345 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800346 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700347 }
348
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800349 if (device->hasEventHubDevices()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700350 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800351 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700352 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353}
354
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000355std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Arpit Singh82f29a12023-06-13 15:05:53 +0000356 nsecs_t when, int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800357 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
Josh Bartel938632f2022-07-19 15:34:22 -0500358 const InputDeviceIdentifier identifier2 =
359 devicePair.second->getDeviceInfo().getIdentifier();
360 return isSubDevice(identifier, identifier2);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800361 });
362
363 std::shared_ptr<InputDevice> device;
364 if (deviceIt != mDevices.end()) {
365 device = deviceIt->second;
366 } else {
367 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
368 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
369 identifier);
370 }
Arpit Singh82f29a12023-06-13 15:05:53 +0000371 mPendingArgs += device->addEventHubDevice(when, eventHubId, mConfig);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372 return device;
373}
374
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700375std::list<NotifyArgs> InputReader::processEventsForDeviceLocked(int32_t eventHubId,
376 const RawEvent* rawEvents,
377 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800378 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000379 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800380 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700381 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 }
383
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000384 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700386 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700387 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388 }
389
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700390 return device->process(rawEvents, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391}
392
Philip Junker4af3b3d2021-12-14 10:36:55 +0100393InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800394 auto deviceIt =
395 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
396 return devicePair.second->getId() == deviceId;
397 });
398 if (deviceIt != mDevices.end()) {
399 return deviceIt->second.get();
400 }
401 return nullptr;
402}
403
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700404std::list<NotifyArgs> InputReader::timeoutExpiredLocked(nsecs_t when) {
405 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000406 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000407 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 if (!device->isIgnored()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700409 out += device->timeoutExpired(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410 }
411 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700412 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413}
414
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800415int32_t InputReader::nextInputDeviceIdLocked() {
416 return ++mNextInputDeviceId;
417}
418
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000419void InputReader::refreshConfigurationLocked(ConfigurationChanges changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 mPolicy->getReaderConfiguration(&mConfig);
421 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
422
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000423 using Change = InputReaderConfiguration::Change;
424 if (!changes.any()) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000426 ALOGI("Reconfiguring input devices, changes=%s", changes.string().c_str());
Prabir Pradhan7e186182020-11-10 13:56:45 -0800427 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800428
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000429 if (changes.test(Change::MUST_REOPEN)) {
Prabir Pradhan7e186182020-11-10 13:56:45 -0800430 mEventHub->requestReopenDevices();
431 } else {
432 for (auto& devicePair : mDevices) {
433 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700434 mPendingArgs += device->configure(now, mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435 }
436 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800437
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000438 if (changes.test(Change::POINTER_CAPTURE)) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000439 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
440 ALOGV("Skipping notifying pointer capture changes: "
441 "There was no change in the pointer capture state.");
442 } else {
443 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700444 mPendingArgs.emplace_back(
445 NotifyPointerCaptureChangedArgs{mContext.getNextId(), now,
446 mCurrentPointerCaptureRequest});
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000447 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800448 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449}
450
451void InputReader::updateGlobalMetaStateLocked() {
452 mGlobalMetaState = 0;
453
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000454 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000455 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800456 mGlobalMetaState |= device->getMetaState();
457 }
458}
459
460int32_t InputReader::getGlobalMetaStateLocked() {
461 return mGlobalMetaState;
462}
463
arthurhungc903df12020-08-11 15:08:42 +0800464void InputReader::updateLedMetaStateLocked(int32_t metaState) {
465 mLedMetaState = metaState;
466 for (auto& devicePair : mDevices) {
467 std::shared_ptr<InputDevice>& device = devicePair.second;
468 device->updateLedState(false);
469 }
470}
471
472int32_t InputReader::getLedMetaStateLocked() {
473 return mLedMetaState;
474}
475
Chris Ye1c2e0892020-11-30 21:41:44 -0800476void InputReader::notifyExternalStylusPresenceChangedLocked() {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000477 refreshConfigurationLocked(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Michael Wright842500e2015-03-13 17:32:02 -0700478}
479
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800480void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000481 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000482 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700483 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000484 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700485 }
486 }
487}
488
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700489std::list<NotifyArgs> InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
490 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000491 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000492 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700493 out += device->updateExternalStylusState(state);
Michael Wright842500e2015-03-13 17:32:02 -0700494 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700495 return out;
Michael Wright842500e2015-03-13 17:32:02 -0700496}
497
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
499 mDisableVirtualKeysTimeout = time;
500}
501
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800502bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800504 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700505 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800506 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 return true;
508 } else {
509 return false;
510 }
511}
512
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
514 if (when < mNextTimeout) {
515 mNextTimeout = when;
516 mEventHub->wake();
517 }
518}
519
520int32_t InputReader::bumpGenerationLocked() {
521 return ++mGeneration;
522}
523
Chris Ye98d3f532020-10-01 21:48:59 -0700524std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000525 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700526 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800527}
528
Chris Ye98d3f532020-10-01 21:48:59 -0700529std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
530 std::vector<InputDeviceInfo> outInputDevices;
531 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800532
Chris Yee7310032020-09-22 15:36:28 -0700533 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000535 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800536 }
537 }
Chris Ye98d3f532020-10-01 21:48:59 -0700538 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539}
540
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700541int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000542 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800543
544 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
545}
546
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700547int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
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, scanCode, &InputDevice::getScanCodeState);
551}
552
553int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
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, switchCode, &InputDevice::getSwitchState);
557}
558
559int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700560 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800561 int32_t result = AKEY_STATE_UNKNOWN;
562 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800563 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800564 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
565 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 }
567 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000568 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000569 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700570 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
572 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000573 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 if (currentResult >= AKEY_STATE_DOWN) {
575 return currentResult;
576 } else if (currentResult == AKEY_STATE_UP) {
577 result = currentResult;
578 }
579 }
580 }
581 }
582 return result;
583}
584
Andrii Kulian763a3a42016-03-08 10:46:16 -0800585void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000586 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800587 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800588 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800589 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
590 return;
591 }
592
Andrii Kulian763a3a42016-03-08 10:46:16 -0800593 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000594 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800595 return;
596 }
597
598 device->updateMetaState(AKEYCODE_CAPS_LOCK);
599}
600
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700601bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
602 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000603 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700605 memset(outFlags, 0, keyCodes.size());
606 return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800607}
608
609bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700610 const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700611 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 bool result = false;
613 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800614 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800615 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700616 result = device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 }
618 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000619 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000620 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700621 if (!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 }
625 }
626 return result;
627}
628
Philip Junker4af3b3d2021-12-14 10:36:55 +0100629int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
630 std::scoped_lock _l(mLock);
631
632 InputDevice* device = findInputDeviceLocked(deviceId);
633 if (device == nullptr) {
634 ALOGW("Failed to get key code for key location: Input device with id %d not found",
635 deviceId);
636 return AKEYCODE_UNKNOWN;
637 }
638 return device->getKeyCodeForKeyLocation(locationKeyCode);
639}
640
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000641void InputReader::requestRefreshConfiguration(ConfigurationChanges changes) {
Chris Ye87143712020-11-10 05:05:58 +0000642 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000644 if (changes.any()) {
645 bool needWake = !mConfigurationChangesToRefresh.any();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 mConfigurationChangesToRefresh |= changes;
647
648 if (needWake) {
649 mEventHub->wake();
650 }
651 }
652}
653
Chris Ye87143712020-11-10 05:05:58 +0000654void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
655 int32_t token) {
656 std::scoped_lock _l(mLock);
657
Chris Ye1c2e0892020-11-30 21:41:44 -0800658 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800659 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700660 mPendingArgs += device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 }
662}
663
664void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000665 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666
Chris Ye1c2e0892020-11-30 21:41:44 -0800667 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800668 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700669 mPendingArgs += device->cancelVibrate(token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 }
671}
672
Chris Ye87143712020-11-10 05:05:58 +0000673bool InputReader::isVibrating(int32_t deviceId) {
674 std::scoped_lock _l(mLock);
675
676 InputDevice* device = findInputDeviceLocked(deviceId);
677 if (device) {
678 return device->isVibrating();
679 }
680 return false;
681}
682
683std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
684 std::scoped_lock _l(mLock);
685
686 InputDevice* device = findInputDeviceLocked(deviceId);
687 if (device) {
688 return device->getVibratorIds();
689 }
690 return {};
691}
692
Chris Yef59a2f42020-10-16 12:55:26 -0700693void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
694 std::scoped_lock _l(mLock);
695
696 InputDevice* device = findInputDeviceLocked(deviceId);
697 if (device) {
698 device->disableSensor(sensorType);
699 }
700}
701
702bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
703 std::chrono::microseconds samplingPeriod,
704 std::chrono::microseconds maxBatchReportLatency) {
705 std::scoped_lock _l(mLock);
706
707 InputDevice* device = findInputDeviceLocked(deviceId);
708 if (device) {
709 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
710 }
711 return false;
712}
713
714void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
715 std::scoped_lock _l(mLock);
716
717 InputDevice* device = findInputDeviceLocked(deviceId);
718 if (device) {
719 device->flushSensor(sensorType);
720 }
721}
722
Kim Low03ea0352020-11-06 12:45:07 -0800723std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400724 std::optional<int32_t> eventHubId;
725 {
726 // Do not query the battery state while holding the lock. For some peripheral devices,
727 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
728 // would block all other event processing during this time. For now, we assume this
729 // call never happens on the InputReader thread and get the battery state outside the
730 // lock to prevent event processing from being blocked by this call.
731 std::scoped_lock _l(mLock);
732 InputDevice* device = findInputDeviceLocked(deviceId);
733 if (!device) return {};
734 eventHubId = device->getBatteryEventHubId();
735 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800736
Andy Chenf9f1a022022-08-29 20:07:10 -0400737 if (!eventHubId) return {};
738 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000739 if (batteryIds.empty()) {
740 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
741 return {};
742 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400743 return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800744}
745
746std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400747 std::optional<int32_t> eventHubId;
748 {
749 // Do not query the battery state while holding the lock. For some peripheral devices,
750 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
751 // would block all other event processing during this time. For now, we assume this
752 // call never happens on the InputReader thread and get the battery state outside the
753 // lock to prevent event processing from being blocked by this call.
754 std::scoped_lock _l(mLock);
755 InputDevice* device = findInputDeviceLocked(deviceId);
756 if (!device) return {};
757 eventHubId = device->getBatteryEventHubId();
758 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800759
Andy Chenf9f1a022022-08-29 20:07:10 -0400760 if (!eventHubId) return {};
761 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000762 if (batteryIds.empty()) {
763 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
764 return {};
765 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400766 return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800767}
768
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000769std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) {
770 std::scoped_lock _l(mLock);
771
772 InputDevice* device = findInputDeviceLocked(deviceId);
773 if (!device) return {};
774
775 std::optional<int32_t> eventHubId = device->getBatteryEventHubId();
776 if (!eventHubId) return {};
777 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
778 if (batteryIds.empty()) {
779 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
780 return {};
781 }
782 const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front());
783 if (!batteryInfo) {
784 ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__,
785 batteryIds.front(), *eventHubId);
786 return {};
787 }
788 return batteryInfo->path;
789}
790
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000791std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800792 std::scoped_lock _l(mLock);
793
794 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000795 if (device == nullptr) {
796 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800797 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000798
799 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800800}
801
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000802std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800803 std::scoped_lock _l(mLock);
804
805 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000806 if (device == nullptr) {
807 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800808 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000809
810 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800811}
812
Omar Abdelmonem5e70e962024-08-06 09:38:42 +0000813std::optional<HardwareProperties> InputReader::getTouchpadHardwareProperties(int32_t deviceId) {
814 std::scoped_lock _l(mLock);
815
816 InputDevice* device = findInputDeviceLocked(deviceId);
817
818 if (device == nullptr) {
819 return {};
820 }
821
822 return device->getTouchpadHardwareProperties();
823}
824
Chris Ye3fdbfef2021-01-06 18:45:18 -0800825bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
826 std::scoped_lock _l(mLock);
827
828 InputDevice* device = findInputDeviceLocked(deviceId);
829 if (device) {
830 return device->setLightColor(lightId, color);
831 }
832 return false;
833}
834
835bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
836 std::scoped_lock _l(mLock);
837
838 InputDevice* device = findInputDeviceLocked(deviceId);
839 if (device) {
840 return device->setLightPlayerId(lightId, playerId);
841 }
842 return false;
843}
844
845std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
846 std::scoped_lock _l(mLock);
847
848 InputDevice* device = findInputDeviceLocked(deviceId);
849 if (device) {
850 return device->getLightColor(lightId);
851 }
852 return std::nullopt;
853}
854
855std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
856 std::scoped_lock _l(mLock);
857
858 InputDevice* device = findInputDeviceLocked(deviceId);
859 if (device) {
860 return device->getLightPlayerId(lightId);
861 }
862 return std::nullopt;
863}
864
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000865std::optional<std::string> InputReader::getBluetoothAddress(int32_t deviceId) const {
866 std::scoped_lock _l(mLock);
867
868 InputDevice* device = findInputDeviceLocked(deviceId);
869 if (device) {
870 return device->getBluetoothAddress();
871 }
872 return std::nullopt;
873}
874
Linnan Li13bf76a2024-05-05 19:18:02 +0800875bool InputReader::canDispatchToDisplay(int32_t deviceId, ui::LogicalDisplayId displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000876 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800877
Chris Ye1c2e0892020-11-30 21:41:44 -0800878 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800879 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800880 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
881 return false;
882 }
883
Arthur Hung2c9a3342019-07-23 14:18:59 +0800884 if (!device->isEnabled()) {
885 ALOGW("Ignoring disabled device %s", device->getName().c_str());
886 return false;
887 }
888
Linnan Li13bf76a2024-05-05 19:18:02 +0800889 std::optional<ui::LogicalDisplayId> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800890 // No associated display. By default, can dispatch to all displays.
Linnan Li13bf76a2024-05-05 19:18:02 +0800891 if (!associatedDisplayId || !associatedDisplayId->isValid()) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800892 return true;
893 }
894
895 return *associatedDisplayId == displayId;
896}
897
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000898void InputReader::sysfsNodeChanged(const std::string& sysfsNodePath) {
899 mEventHub->sysfsNodeChanged(sysfsNodePath);
900}
901
Prabir Pradhan018faea2024-05-08 21:52:54 +0000902DeviceId InputReader::getLastUsedInputDeviceId() {
903 std::scoped_lock _l(mLock);
904 return mLastUsedDeviceId;
905}
906
Arpit Singh849beb42024-06-06 07:14:17 +0000907void InputReader::notifyMouseCursorFadedOnTyping() {
908 std::scoped_lock _l(mLock);
909 // disable touchpad taps when cursor has faded due to typing
910 mPreventingTouchpadTaps = true;
911}
912
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800913void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000914 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915
916 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800917 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918
Chris Yee7310032020-09-22 15:36:28 -0700919 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
920 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921
Chris Yee7310032020-09-22 15:36:28 -0700922 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
923 const std::shared_ptr<InputDevice>& device = devicePair.first;
924 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
925 for (const auto& eId : devicePair.second) {
926 eventHubDevStr += StringPrintf("%d ", eId);
927 }
928 eventHubDevStr += "] \n";
929 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930 }
931
Harry Cutts8c7cb592023-08-23 17:20:13 +0000932 dump += StringPrintf(INDENT "NextTimeout: %" PRId64 "\n", mNextTimeout);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800933 dump += INDENT "Configuration:\n";
934 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
936 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800937 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100939 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800941 dump += "]\n";
942 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700943 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800945 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700946 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
947 "acceleration=%0.3f\n",
948 mConfig.pointerVelocityControlParameters.scale,
949 mConfig.pointerVelocityControlParameters.lowThreshold,
950 mConfig.pointerVelocityControlParameters.highThreshold,
951 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800953 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700954 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
955 "acceleration=%0.3f\n",
956 mConfig.wheelVelocityControlParameters.scale,
957 mConfig.wheelVelocityControlParameters.lowThreshold,
958 mConfig.wheelVelocityControlParameters.highThreshold,
959 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800961 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700962 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800963 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700964 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800965 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700966 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800967 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700968 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800969 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700970 mConfig.pointerGestureTapDragInterval * 0.000001f);
971 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800972 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700973 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800974 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700975 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800976 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700977 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800978 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700979 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800980 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700981 mConfig.pointerGestureMovementSpeedRatio);
982 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700983
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800984 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700985 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986}
987
988void InputReader::monitor() {
989 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800990 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800991 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800992 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 // Check the EventHub
994 mEventHub->monitor();
995}
996
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997// --- InputReader::ContextImpl ---
998
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800999InputReader::ContextImpl::ContextImpl(InputReader* reader)
1000 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001
1002void InputReader::ContextImpl::updateGlobalMetaState() {
1003 // lock is already held by the input loop
1004 mReader->updateGlobalMetaStateLocked();
1005}
1006
1007int32_t InputReader::ContextImpl::getGlobalMetaState() {
1008 // lock is already held by the input loop
1009 return mReader->getGlobalMetaStateLocked();
1010}
1011
arthurhungc903df12020-08-11 15:08:42 +08001012void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
1013 // lock is already held by the input loop
1014 mReader->updateLedMetaStateLocked(metaState);
1015}
1016
1017int32_t InputReader::ContextImpl::getLedMetaState() {
1018 // lock is already held by the input loop
1019 return mReader->getLedMetaStateLocked();
1020}
1021
Arpit Singha5ea7c12023-07-05 15:39:25 +00001022void InputReader::ContextImpl::setPreventingTouchpadTaps(bool prevent) {
1023 // lock is already held by the input loop
1024 mReader->mPreventingTouchpadTaps = prevent;
1025}
1026
1027bool InputReader::ContextImpl::isPreventingTouchpadTaps() {
1028 // lock is already held by the input loop
1029 return mReader->mPreventingTouchpadTaps;
1030}
1031
Arpit Singh82e413e2023-10-10 19:30:58 +00001032void InputReader::ContextImpl::setLastKeyDownTimestamp(nsecs_t when) {
1033 mReader->mLastKeyDownTimestamp = when;
1034}
1035
1036nsecs_t InputReader::ContextImpl::getLastKeyDownTimestamp() {
1037 return mReader->mLastKeyDownTimestamp;
1038}
1039
Michael Wrightd02c5b62014-02-10 15:10:22 -08001040void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
1041 // lock is already held by the input loop
1042 mReader->disableVirtualKeysUntilLocked(time);
1043}
1044
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001045bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
1046 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001048 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049}
1050
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
1052 // lock is already held by the input loop
1053 mReader->requestTimeoutAtTimeLocked(when);
1054}
1055
1056int32_t InputReader::ContextImpl::bumpGeneration() {
1057 // lock is already held by the input loop
1058 return mReader->bumpGenerationLocked();
1059}
1060
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001061void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -07001062 // lock is already held by whatever called refreshConfigurationLocked
1063 mReader->getExternalStylusDevicesLocked(outDevices);
1064}
1065
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001066std::list<NotifyArgs> InputReader::ContextImpl::dispatchExternalStylusState(
1067 const StylusState& state) {
1068 return mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -07001069}
1070
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
1072 return mReader->mPolicy.get();
1073}
1074
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075EventHubInterface* InputReader::ContextImpl::getEventHub() {
1076 return mReader->mEventHub.get();
1077}
1078
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00001079int32_t InputReader::ContextImpl::getNextId() {
1080 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -08001081}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00001083KeyboardClassifier& InputReader::ContextImpl::getKeyboardClassifier() {
1084 return *mReader->mKeyboardClassifier;
1085}
1086
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087} // namespace android