blob: ab2704237ffcc7cdead62988ae5b5602d1aac525 [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);
Vaibhav Devmurari5c4f7982024-10-07 09:41:23 +0000587 if (mKeyboardClassifier->getKeyboardType(deviceId) == KeyboardType::ALPHABETIC) {
588 updateLedMetaStateLocked(mLedMetaState ^ AMETA_CAPS_LOCK_ON);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800589 }
Andrii Kulian763a3a42016-03-08 10:46:16 -0800590}
591
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700592bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
593 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000594 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700596 memset(outFlags, 0, keyCodes.size());
597 return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598}
599
600bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700601 const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700602 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800603 bool result = false;
604 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800605 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800606 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700607 result = device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608 }
609 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000610 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000611 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700612 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700613 result |= device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614 }
615 }
616 }
617 return result;
618}
619
Philip Junker4af3b3d2021-12-14 10:36:55 +0100620int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
621 std::scoped_lock _l(mLock);
622
623 InputDevice* device = findInputDeviceLocked(deviceId);
624 if (device == nullptr) {
625 ALOGW("Failed to get key code for key location: Input device with id %d not found",
626 deviceId);
627 return AKEYCODE_UNKNOWN;
628 }
629 return device->getKeyCodeForKeyLocation(locationKeyCode);
630}
631
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000632void InputReader::requestRefreshConfiguration(ConfigurationChanges changes) {
Chris Ye87143712020-11-10 05:05:58 +0000633 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000635 if (changes.any()) {
636 bool needWake = !mConfigurationChangesToRefresh.any();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 mConfigurationChangesToRefresh |= changes;
638
639 if (needWake) {
640 mEventHub->wake();
641 }
642 }
643}
644
Chris Ye87143712020-11-10 05:05:58 +0000645void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
646 int32_t token) {
647 std::scoped_lock _l(mLock);
648
Chris Ye1c2e0892020-11-30 21:41:44 -0800649 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800650 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700651 mPendingArgs += device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652 }
653}
654
655void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000656 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657
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->cancelVibrate(token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 }
662}
663
Chris Ye87143712020-11-10 05:05:58 +0000664bool InputReader::isVibrating(int32_t deviceId) {
665 std::scoped_lock _l(mLock);
666
667 InputDevice* device = findInputDeviceLocked(deviceId);
668 if (device) {
669 return device->isVibrating();
670 }
671 return false;
672}
673
674std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
675 std::scoped_lock _l(mLock);
676
677 InputDevice* device = findInputDeviceLocked(deviceId);
678 if (device) {
679 return device->getVibratorIds();
680 }
681 return {};
682}
683
Chris Yef59a2f42020-10-16 12:55:26 -0700684void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
685 std::scoped_lock _l(mLock);
686
687 InputDevice* device = findInputDeviceLocked(deviceId);
688 if (device) {
689 device->disableSensor(sensorType);
690 }
691}
692
693bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
694 std::chrono::microseconds samplingPeriod,
695 std::chrono::microseconds maxBatchReportLatency) {
696 std::scoped_lock _l(mLock);
697
698 InputDevice* device = findInputDeviceLocked(deviceId);
699 if (device) {
700 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
701 }
702 return false;
703}
704
705void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
706 std::scoped_lock _l(mLock);
707
708 InputDevice* device = findInputDeviceLocked(deviceId);
709 if (device) {
710 device->flushSensor(sensorType);
711 }
712}
713
Kim Low03ea0352020-11-06 12:45:07 -0800714std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400715 std::optional<int32_t> eventHubId;
716 {
717 // Do not query the battery state while holding the lock. For some peripheral devices,
718 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
719 // would block all other event processing during this time. For now, we assume this
720 // call never happens on the InputReader thread and get the battery state outside the
721 // lock to prevent event processing from being blocked by this call.
722 std::scoped_lock _l(mLock);
723 InputDevice* device = findInputDeviceLocked(deviceId);
724 if (!device) return {};
725 eventHubId = device->getBatteryEventHubId();
726 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800727
Andy Chenf9f1a022022-08-29 20:07:10 -0400728 if (!eventHubId) return {};
729 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000730 if (batteryIds.empty()) {
731 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
732 return {};
733 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400734 return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800735}
736
737std::optional<int32_t> InputReader::getBatteryStatus(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->getBatteryStatus(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800758}
759
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000760std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) {
761 std::scoped_lock _l(mLock);
762
763 InputDevice* device = findInputDeviceLocked(deviceId);
764 if (!device) return {};
765
766 std::optional<int32_t> eventHubId = device->getBatteryEventHubId();
767 if (!eventHubId) return {};
768 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
769 if (batteryIds.empty()) {
770 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
771 return {};
772 }
773 const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front());
774 if (!batteryInfo) {
775 ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__,
776 batteryIds.front(), *eventHubId);
777 return {};
778 }
779 return batteryInfo->path;
780}
781
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000782std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800783 std::scoped_lock _l(mLock);
784
785 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000786 if (device == nullptr) {
787 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800788 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000789
790 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800791}
792
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000793std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800794 std::scoped_lock _l(mLock);
795
796 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000797 if (device == nullptr) {
798 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800799 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000800
801 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800802}
803
Omar Abdelmonem5e70e962024-08-06 09:38:42 +0000804std::optional<HardwareProperties> InputReader::getTouchpadHardwareProperties(int32_t deviceId) {
805 std::scoped_lock _l(mLock);
806
807 InputDevice* device = findInputDeviceLocked(deviceId);
808
809 if (device == nullptr) {
810 return {};
811 }
812
813 return device->getTouchpadHardwareProperties();
814}
815
Chris Ye3fdbfef2021-01-06 18:45:18 -0800816bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
817 std::scoped_lock _l(mLock);
818
819 InputDevice* device = findInputDeviceLocked(deviceId);
820 if (device) {
821 return device->setLightColor(lightId, color);
822 }
823 return false;
824}
825
826bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
827 std::scoped_lock _l(mLock);
828
829 InputDevice* device = findInputDeviceLocked(deviceId);
830 if (device) {
831 return device->setLightPlayerId(lightId, playerId);
832 }
833 return false;
834}
835
836std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
837 std::scoped_lock _l(mLock);
838
839 InputDevice* device = findInputDeviceLocked(deviceId);
840 if (device) {
841 return device->getLightColor(lightId);
842 }
843 return std::nullopt;
844}
845
846std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
847 std::scoped_lock _l(mLock);
848
849 InputDevice* device = findInputDeviceLocked(deviceId);
850 if (device) {
851 return device->getLightPlayerId(lightId);
852 }
853 return std::nullopt;
854}
855
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000856std::optional<std::string> InputReader::getBluetoothAddress(int32_t deviceId) const {
857 std::scoped_lock _l(mLock);
858
859 InputDevice* device = findInputDeviceLocked(deviceId);
860 if (device) {
861 return device->getBluetoothAddress();
862 }
863 return std::nullopt;
864}
865
Linnan Li13bf76a2024-05-05 19:18:02 +0800866bool InputReader::canDispatchToDisplay(int32_t deviceId, ui::LogicalDisplayId displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000867 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800868
Chris Ye1c2e0892020-11-30 21:41:44 -0800869 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800870 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800871 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
872 return false;
873 }
874
Arthur Hung2c9a3342019-07-23 14:18:59 +0800875 if (!device->isEnabled()) {
876 ALOGW("Ignoring disabled device %s", device->getName().c_str());
877 return false;
878 }
879
Linnan Li13bf76a2024-05-05 19:18:02 +0800880 std::optional<ui::LogicalDisplayId> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800881 // No associated display. By default, can dispatch to all displays.
Linnan Li13bf76a2024-05-05 19:18:02 +0800882 if (!associatedDisplayId || !associatedDisplayId->isValid()) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800883 return true;
884 }
885
886 return *associatedDisplayId == displayId;
887}
888
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000889void InputReader::sysfsNodeChanged(const std::string& sysfsNodePath) {
890 mEventHub->sysfsNodeChanged(sysfsNodePath);
891}
892
Prabir Pradhan018faea2024-05-08 21:52:54 +0000893DeviceId InputReader::getLastUsedInputDeviceId() {
894 std::scoped_lock _l(mLock);
895 return mLastUsedDeviceId;
896}
897
Arpit Singh849beb42024-06-06 07:14:17 +0000898void InputReader::notifyMouseCursorFadedOnTyping() {
899 std::scoped_lock _l(mLock);
900 // disable touchpad taps when cursor has faded due to typing
901 mPreventingTouchpadTaps = true;
902}
903
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800904void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000905 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906
907 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800908 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909
Chris Yee7310032020-09-22 15:36:28 -0700910 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
911 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912
Chris Yee7310032020-09-22 15:36:28 -0700913 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
914 const std::shared_ptr<InputDevice>& device = devicePair.first;
915 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
916 for (const auto& eId : devicePair.second) {
917 eventHubDevStr += StringPrintf("%d ", eId);
918 }
919 eventHubDevStr += "] \n";
920 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 }
922
Harry Cutts8c7cb592023-08-23 17:20:13 +0000923 dump += StringPrintf(INDENT "NextTimeout: %" PRId64 "\n", mNextTimeout);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800924 dump += INDENT "Configuration:\n";
925 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
927 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800928 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100930 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800931 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800932 dump += "]\n";
933 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700934 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800936 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700937 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
938 "acceleration=%0.3f\n",
939 mConfig.pointerVelocityControlParameters.scale,
940 mConfig.pointerVelocityControlParameters.lowThreshold,
941 mConfig.pointerVelocityControlParameters.highThreshold,
942 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800944 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700945 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
946 "acceleration=%0.3f\n",
947 mConfig.wheelVelocityControlParameters.scale,
948 mConfig.wheelVelocityControlParameters.lowThreshold,
949 mConfig.wheelVelocityControlParameters.highThreshold,
950 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800952 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700953 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800954 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700955 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800956 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700957 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800958 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700959 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800960 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700961 mConfig.pointerGestureTapDragInterval * 0.000001f);
962 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800963 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700964 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800965 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700966 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800967 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700968 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800969 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700970 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800971 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700972 mConfig.pointerGestureMovementSpeedRatio);
973 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700974
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800975 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700976 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977}
978
979void InputReader::monitor() {
980 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800981 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800983 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 // Check the EventHub
985 mEventHub->monitor();
986}
987
Michael Wrightd02c5b62014-02-10 15:10:22 -0800988// --- InputReader::ContextImpl ---
989
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800990InputReader::ContextImpl::ContextImpl(InputReader* reader)
991 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800992
993void InputReader::ContextImpl::updateGlobalMetaState() {
994 // lock is already held by the input loop
995 mReader->updateGlobalMetaStateLocked();
996}
997
998int32_t InputReader::ContextImpl::getGlobalMetaState() {
999 // lock is already held by the input loop
1000 return mReader->getGlobalMetaStateLocked();
1001}
1002
arthurhungc903df12020-08-11 15:08:42 +08001003void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
1004 // lock is already held by the input loop
1005 mReader->updateLedMetaStateLocked(metaState);
1006}
1007
1008int32_t InputReader::ContextImpl::getLedMetaState() {
1009 // lock is already held by the input loop
1010 return mReader->getLedMetaStateLocked();
1011}
1012
Arpit Singha5ea7c12023-07-05 15:39:25 +00001013void InputReader::ContextImpl::setPreventingTouchpadTaps(bool prevent) {
1014 // lock is already held by the input loop
1015 mReader->mPreventingTouchpadTaps = prevent;
1016}
1017
1018bool InputReader::ContextImpl::isPreventingTouchpadTaps() {
1019 // lock is already held by the input loop
1020 return mReader->mPreventingTouchpadTaps;
1021}
1022
Arpit Singh82e413e2023-10-10 19:30:58 +00001023void InputReader::ContextImpl::setLastKeyDownTimestamp(nsecs_t when) {
1024 mReader->mLastKeyDownTimestamp = when;
1025}
1026
1027nsecs_t InputReader::ContextImpl::getLastKeyDownTimestamp() {
1028 return mReader->mLastKeyDownTimestamp;
1029}
1030
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
1032 // lock is already held by the input loop
1033 mReader->disableVirtualKeysUntilLocked(time);
1034}
1035
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001036bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
1037 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001039 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001040}
1041
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
1043 // lock is already held by the input loop
1044 mReader->requestTimeoutAtTimeLocked(when);
1045}
1046
1047int32_t InputReader::ContextImpl::bumpGeneration() {
1048 // lock is already held by the input loop
1049 return mReader->bumpGenerationLocked();
1050}
1051
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001052void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -07001053 // lock is already held by whatever called refreshConfigurationLocked
1054 mReader->getExternalStylusDevicesLocked(outDevices);
1055}
1056
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001057std::list<NotifyArgs> InputReader::ContextImpl::dispatchExternalStylusState(
1058 const StylusState& state) {
1059 return mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -07001060}
1061
Michael Wrightd02c5b62014-02-10 15:10:22 -08001062InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
1063 return mReader->mPolicy.get();
1064}
1065
Michael Wrightd02c5b62014-02-10 15:10:22 -08001066EventHubInterface* InputReader::ContextImpl::getEventHub() {
1067 return mReader->mEventHub.get();
1068}
1069
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00001070int32_t InputReader::ContextImpl::getNextId() {
1071 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -08001072}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00001074KeyboardClassifier& InputReader::ContextImpl::getKeyboardClassifier() {
1075 return *mReader->mKeyboardClassifier;
1076}
1077
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078} // namespace android