blob: ada6653f6543a604eaa6835d1caa5c6fcbc291c6 [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>
Yanye Li0dd9a4e2024-11-15 16:09:19 +000022#include <com_android_input_flags.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070023#include <errno.h>
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080024#include <input/Keyboard.h>
25#include <input/VirtualKeyMap.h>
Michael Wright842500e2015-03-13 17:32:02 -070026#include <inttypes.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070027#include <limits.h>
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080028#include <log/log.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070029#include <math.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030#include <stddef.h>
31#include <stdlib.h>
32#include <unistd.h>
Prabir Pradhan28efc192019-11-05 01:10:04 +000033#include <utils/Errors.h>
Prabir Pradhan28efc192019-11-05 01:10:04 +000034#include <utils/Thread.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080036#include "InputDevice.h"
Omar Abdelmonem5e70e962024-08-06 09:38:42 +000037#include "include/gestures.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080038
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080039using android::base::StringPrintf;
40
Michael Wrightd02c5b62014-02-10 15:10:22 -080041namespace android {
42
Prabir Pradhan018faea2024-05-08 21:52:54 +000043namespace {
44
Josh Bartel938632f2022-07-19 15:34:22 -050045/**
46 * Determines if the identifiers passed are a sub-devices. Sub-devices are physical devices
47 * that expose multiple input device paths such a keyboard that also has a touchpad input.
48 * These are separate devices with unique descriptors in EventHub, but InputReader should
49 * create a single InputDevice for them.
50 * Sub-devices are detected by the following criteria:
51 * 1. The vendor, product, bus, version, and unique id match
52 * 2. The location matches. The location is used to distinguish a single device with multiple
53 * inputs versus the same device plugged into multiple ports.
54 */
55
Prabir Pradhan018faea2024-05-08 21:52:54 +000056bool isSubDevice(const InputDeviceIdentifier& identifier1,
57 const InputDeviceIdentifier& identifier2) {
Josh Bartel938632f2022-07-19 15:34:22 -050058 return (identifier1.vendor == identifier2.vendor &&
59 identifier1.product == identifier2.product && identifier1.bus == identifier2.bus &&
60 identifier1.version == identifier2.version &&
61 identifier1.uniqueId == identifier2.uniqueId &&
62 identifier1.location == identifier2.location);
63}
64
Prabir Pradhan018faea2024-05-08 21:52:54 +000065bool isStylusPointerGestureStart(const NotifyMotionArgs& motionArgs) {
Prabir Pradhanda20b172022-09-26 17:01:18 +000066 const auto actionMasked = MotionEvent::getActionMasked(motionArgs.action);
67 if (actionMasked != AMOTION_EVENT_ACTION_HOVER_ENTER &&
68 actionMasked != AMOTION_EVENT_ACTION_DOWN &&
69 actionMasked != AMOTION_EVENT_ACTION_POINTER_DOWN) {
70 return false;
71 }
72 const auto actionIndex = MotionEvent::getActionIndex(motionArgs.action);
Prabir Pradhane5626962022-10-27 20:30:53 +000073 return isStylusToolType(motionArgs.pointerProperties[actionIndex].toolType);
Prabir Pradhanda20b172022-09-26 17:01:18 +000074}
75
Prabir Pradhan018faea2024-05-08 21:52:54 +000076bool isNewGestureStart(const NotifyMotionArgs& motion) {
77 return motion.action == AMOTION_EVENT_ACTION_DOWN ||
78 motion.action == AMOTION_EVENT_ACTION_HOVER_ENTER;
79}
80
81bool isNewGestureStart(const NotifyKeyArgs& key) {
82 return key.action == AKEY_EVENT_ACTION_DOWN;
83}
84
85// Return the event's device ID if it marks the start of a new gesture.
86std::optional<DeviceId> getDeviceIdOfNewGesture(const NotifyArgs& args) {
87 if (const auto* motion = std::get_if<NotifyMotionArgs>(&args); motion != nullptr) {
88 return isNewGestureStart(*motion) ? std::make_optional(motion->deviceId) : std::nullopt;
89 }
90 if (const auto* key = std::get_if<NotifyKeyArgs>(&args); key != nullptr) {
91 return isNewGestureStart(*key) ? std::make_optional(key->deviceId) : std::nullopt;
92 }
93 return std::nullopt;
94}
95
96} // namespace
97
Prabir Pradhan28efc192019-11-05 01:10:04 +000098// --- InputReader ---
99
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700100InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
101 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700102 InputListenerInterface& listener)
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700103 : mContext(this),
104 mEventHub(eventHub),
105 mPolicy(policy),
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700106 mNextListener(listener),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000107 mKeyboardClassifier(std::make_unique<KeyboardClassifier>()),
Arthur Hung95f68612022-04-07 14:08:22 +0800108 mGlobalMetaState(AMETA_NONE),
109 mLedMetaState(AMETA_NONE),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700110 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800111 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700112 mDisableVirtualKeysTimeout(LLONG_MIN),
113 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114 mConfigurationChangesToRefresh(0) {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000115 refreshConfigurationLocked(/*changes=*/{});
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700116 updateGlobalMetaStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117}
118
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000119InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120
Prabir Pradhan28efc192019-11-05 01:10:04 +0000121status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700122 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000123 return ALREADY_EXISTS;
124 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700125 mThread = std::make_unique<InputThread>(
Siarhei Vishniakouf53fa6b2024-09-19 17:42:42 -0700126 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); },
127 /*isInCriticalPath=*/true);
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700128 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000129}
130
131status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700132 if (mThread && mThread->isCallingThread()) {
133 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +0000134 return INVALID_OPERATION;
135 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700136 mThread.reset();
137 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000138}
139
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140void InputReader::loopOnce() {
141 int32_t oldGeneration;
142 int32_t timeoutMillis;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000143 // Copy some state so that we can access it outside the lock later.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -0800145 std::vector<InputDeviceInfo> inputDevices;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000146 std::list<NotifyArgs> notifyArgs;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000148 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149
150 oldGeneration = mGeneration;
151 timeoutMillis = -1;
152
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000153 auto changes = mConfigurationChangesToRefresh;
154 if (changes.any()) {
155 mConfigurationChangesToRefresh.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156 timeoutMillis = 0;
157 refreshConfigurationLocked(changes);
158 } else if (mNextTimeout != LLONG_MAX) {
159 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
160 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
161 }
162 } // release lock
163
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700164 std::vector<RawEvent> events = mEventHub->getEvents(timeoutMillis);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
166 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000167 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800168 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700170 if (!events.empty()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700171 mPendingArgs += processEventsLocked(events.data(), events.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 }
173
174 if (mNextTimeout != LLONG_MAX) {
175 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
176 if (now >= mNextTimeout) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000177 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800178 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
179 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 mNextTimeout = LLONG_MAX;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700181 mPendingArgs += timeoutExpiredLocked(now);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800182 }
183 }
184
185 if (oldGeneration != mGeneration) {
Liana Kazanova5b8217b2024-07-18 17:44:51 +0000186 // Reset global meta state because it depends on connected input devices.
187 updateGlobalMetaStateLocked();
188
Michael Wrightd02c5b62014-02-10 15:10:22 -0800189 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800190 inputDevices = getInputDevicesLocked();
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700191 mPendingArgs.emplace_back(
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000192 NotifyInputDevicesChangedArgs{mContext.getNextId(), inputDevices});
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700194
195 std::swap(notifyArgs, mPendingArgs);
Prabir Pradhan018faea2024-05-08 21:52:54 +0000196
197 // Keep track of the last used device
198 for (const NotifyArgs& args : notifyArgs) {
199 mLastUsedDeviceId = getDeviceIdOfNewGesture(args).value_or(mLastUsedDeviceId);
200 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800201 } // release lock
202
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 // Flush queued events out to the listener.
204 // This must happen outside of the lock because the listener could potentially call
205 // back into the InputReader's methods, such as getScanCodeState, or become blocked
206 // on another thread similarly waiting to acquire the InputReader lock thereby
207 // resulting in a deadlock. This situation is actually quite plausible because the
208 // listener is actually the input dispatcher, which calls into the window manager,
209 // which occasionally calls into the input reader.
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700210 for (const NotifyArgs& args : notifyArgs) {
211 mNextListener.notify(args);
212 }
Prabir Pradhanc3a92472024-02-06 20:08:05 +0000213
214 // Notify the policy that input devices have changed.
215 // This must be done after flushing events down the listener chain to ensure that the rest of
216 // the listeners are synchronized with the changes before the policy reacts to them.
217 if (inputDevicesChanged) {
218 mPolicy->notifyInputDevicesChanged(inputDevices);
219 }
220
221 // Notify the policy of the start of every new stylus gesture.
222 for (const auto& args : notifyArgs) {
223 const auto* motionArgs = std::get_if<NotifyMotionArgs>(&args);
224 if (motionArgs != nullptr && isStylusPointerGestureStart(*motionArgs)) {
225 mPolicy->notifyStylusGestureStarted(motionArgs->deviceId, motionArgs->eventTime);
226 }
227 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228}
229
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700230std::list<NotifyArgs> InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
231 std::list<NotifyArgs> out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232 for (const RawEvent* rawEvent = rawEvents; count;) {
233 int32_t type = rawEvent->type;
234 size_t batchSize = 1;
235 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
236 int32_t deviceId = rawEvent->deviceId;
237 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700238 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
239 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240 break;
241 }
242 batchSize += 1;
243 }
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000244 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800245 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
246 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700247 out += processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 } else {
249 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700250 case EventHubInterface::DEVICE_ADDED:
251 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
252 break;
253 case EventHubInterface::DEVICE_REMOVED:
254 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
255 break;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700256 default:
257 ALOG_ASSERT(false); // can't happen
258 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 }
260 }
261 count -= batchSize;
262 rawEvent += batchSize;
263 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700264 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265}
266
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800267void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
268 if (mDevices.find(eventHubId) != mDevices.end()) {
269 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 return;
271 }
272
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800273 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
Arpit Singh82f29a12023-06-13 15:05:53 +0000274 std::shared_ptr<InputDevice> device = createDeviceLocked(when, eventHubId, identifier);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700275
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700276 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
277 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800278
279 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800280 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
281 "(ignored non-input device)",
282 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000284 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800285 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000286 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800289 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700290 // Add device to device to EventHub ids map.
291 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
292 if (mapIt == mDeviceToEventHubIdsMap.end()) {
293 std::vector<int32_t> ids = {eventHubId};
294 mDeviceToEventHubIdsMap.emplace(device, ids);
295 } else {
296 mapIt->second.push_back(eventHubId);
297 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700299
Chris Ye1b0c7342020-07-28 21:57:03 -0700300 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800301 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700302 }
Chris Yef59a2f42020-10-16 12:55:26 -0700303
304 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800305 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
306 // device class to disable SENSOR sub device only.
307 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700308 mEventHub->disableDevice(eventHubId);
309 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310}
311
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800312void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
313 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000314 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800315 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800316 return;
317 }
318
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000319 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000320 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700321 // Erase device from device to EventHub ids map.
322 auto mapIt = mDeviceToEventHubIdsMap.find(device);
323 if (mapIt != mDeviceToEventHubIdsMap.end()) {
324 std::vector<int32_t>& eventHubIds = mapIt->second;
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -0800325 std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
Chris Yee7310032020-09-22 15:36:28 -0700326 if (eventHubIds.size() == 0) {
327 mDeviceToEventHubIdsMap.erase(mapIt);
328 }
329 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 bumpGenerationLocked();
331
332 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800333 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
334 "(ignored non-input device)",
335 device->getId(), eventHubId, device->getName().c_str(),
336 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000338 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800339 device->getId(), eventHubId, device->getName().c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000340 device->getDescriptor().c_str(),
341 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342 }
343
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800344 device->removeEventHubDevice(eventHubId);
345
Chris Ye1b0c7342020-07-28 21:57:03 -0700346 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800347 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700348 }
349
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800350 if (device->hasEventHubDevices()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700351 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800352 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700353 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800354}
355
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000356std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Arpit Singh82f29a12023-06-13 15:05:53 +0000357 nsecs_t when, int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800358 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
Josh Bartel938632f2022-07-19 15:34:22 -0500359 const InputDeviceIdentifier identifier2 =
360 devicePair.second->getDeviceInfo().getIdentifier();
361 return isSubDevice(identifier, identifier2);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800362 });
363
364 std::shared_ptr<InputDevice> device;
365 if (deviceIt != mDevices.end()) {
366 device = deviceIt->second;
367 } else {
368 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
369 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
370 identifier);
371 }
Arpit Singh82f29a12023-06-13 15:05:53 +0000372 mPendingArgs += device->addEventHubDevice(when, eventHubId, mConfig);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373 return device;
374}
375
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700376std::list<NotifyArgs> InputReader::processEventsForDeviceLocked(int32_t eventHubId,
377 const RawEvent* rawEvents,
378 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800379 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000380 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800381 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700382 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383 }
384
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000385 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700387 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700388 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389 }
390
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700391 return device->process(rawEvents, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392}
393
Philip Junker4af3b3d2021-12-14 10:36:55 +0100394InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800395 auto deviceIt =
396 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
397 return devicePair.second->getId() == deviceId;
398 });
399 if (deviceIt != mDevices.end()) {
400 return deviceIt->second.get();
401 }
402 return nullptr;
403}
404
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700405std::list<NotifyArgs> InputReader::timeoutExpiredLocked(nsecs_t when) {
406 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000407 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000408 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409 if (!device->isIgnored()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700410 out += device->timeoutExpired(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 }
412 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700413 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414}
415
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800416int32_t InputReader::nextInputDeviceIdLocked() {
417 return ++mNextInputDeviceId;
418}
419
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000420void InputReader::refreshConfigurationLocked(ConfigurationChanges changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421 mPolicy->getReaderConfiguration(&mConfig);
422 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
423
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000424 using Change = InputReaderConfiguration::Change;
425 if (!changes.any()) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800426
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000427 ALOGI("Reconfiguring input devices, changes=%s", changes.string().c_str());
Prabir Pradhan7e186182020-11-10 13:56:45 -0800428 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800429
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000430 if (changes.test(Change::MUST_REOPEN)) {
Prabir Pradhan7e186182020-11-10 13:56:45 -0800431 mEventHub->requestReopenDevices();
432 } else {
433 for (auto& devicePair : mDevices) {
434 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700435 mPendingArgs += device->configure(now, mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436 }
437 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800438
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000439 if (changes.test(Change::POINTER_CAPTURE)) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000440 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
441 ALOGV("Skipping notifying pointer capture changes: "
442 "There was no change in the pointer capture state.");
443 } else {
444 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700445 mPendingArgs.emplace_back(
446 NotifyPointerCaptureChangedArgs{mContext.getNextId(), now,
447 mCurrentPointerCaptureRequest});
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000448 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800449 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800450}
451
452void InputReader::updateGlobalMetaStateLocked() {
453 mGlobalMetaState = 0;
454
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000455 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000456 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457 mGlobalMetaState |= device->getMetaState();
458 }
459}
460
461int32_t InputReader::getGlobalMetaStateLocked() {
462 return mGlobalMetaState;
463}
464
arthurhungc903df12020-08-11 15:08:42 +0800465void InputReader::updateLedMetaStateLocked(int32_t metaState) {
466 mLedMetaState = metaState;
467 for (auto& devicePair : mDevices) {
468 std::shared_ptr<InputDevice>& device = devicePair.second;
469 device->updateLedState(false);
470 }
471}
472
473int32_t InputReader::getLedMetaStateLocked() {
474 return mLedMetaState;
475}
476
Chris Ye1c2e0892020-11-30 21:41:44 -0800477void InputReader::notifyExternalStylusPresenceChangedLocked() {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000478 refreshConfigurationLocked(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Michael Wright842500e2015-03-13 17:32:02 -0700479}
480
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800481void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000482 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000483 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700484 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000485 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700486 }
487 }
488}
489
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700490std::list<NotifyArgs> InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
491 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000492 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000493 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700494 out += device->updateExternalStylusState(state);
Michael Wright842500e2015-03-13 17:32:02 -0700495 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700496 return out;
Michael Wright842500e2015-03-13 17:32:02 -0700497}
498
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
500 mDisableVirtualKeysTimeout = time;
501}
502
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800503bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800505 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700506 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800507 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800508 return true;
509 } else {
510 return false;
511 }
512}
513
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
515 if (when < mNextTimeout) {
516 mNextTimeout = when;
517 mEventHub->wake();
518 }
519}
520
521int32_t InputReader::bumpGenerationLocked() {
522 return ++mGeneration;
523}
524
Chris Ye98d3f532020-10-01 21:48:59 -0700525std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000526 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700527 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528}
529
Chris Ye98d3f532020-10-01 21:48:59 -0700530std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
531 std::vector<InputDeviceInfo> outInputDevices;
532 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533
Chris Yee7310032020-09-22 15:36:28 -0700534 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000536 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537 }
538 }
Chris Ye98d3f532020-10-01 21:48:59 -0700539 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540}
541
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700542int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000543 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544
545 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
546}
547
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700548int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000549 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550
551 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
552}
553
554int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000555 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556
557 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
558}
559
560int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700561 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 int32_t result = AKEY_STATE_UNKNOWN;
563 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800564 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800565 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
566 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567 }
568 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000569 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000570 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700571 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
573 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000574 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800575 if (currentResult >= AKEY_STATE_DOWN) {
576 return currentResult;
577 } else if (currentResult == AKEY_STATE_UP) {
578 result = currentResult;
579 }
580 }
581 }
582 }
583 return result;
584}
585
Andrii Kulian763a3a42016-03-08 10:46:16 -0800586void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000587 std::scoped_lock _l(mLock);
Vaibhav Devmurari5c4f7982024-10-07 09:41:23 +0000588 if (mKeyboardClassifier->getKeyboardType(deviceId) == KeyboardType::ALPHABETIC) {
589 updateLedMetaStateLocked(mLedMetaState ^ AMETA_CAPS_LOCK_ON);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800590 }
Andrii Kulian763a3a42016-03-08 10:46:16 -0800591}
592
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700593bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
594 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000595 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700597 memset(outFlags, 0, keyCodes.size());
598 return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599}
600
601bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700602 const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700603 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604 bool result = false;
605 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800606 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800607 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700608 result = device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000611 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000612 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700613 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700614 result |= device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615 }
616 }
617 }
618 return result;
619}
620
Philip Junker4af3b3d2021-12-14 10:36:55 +0100621int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
622 std::scoped_lock _l(mLock);
623
624 InputDevice* device = findInputDeviceLocked(deviceId);
625 if (device == nullptr) {
626 ALOGW("Failed to get key code for key location: Input device with id %d not found",
627 deviceId);
628 return AKEYCODE_UNKNOWN;
629 }
630 return device->getKeyCodeForKeyLocation(locationKeyCode);
631}
632
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000633void InputReader::requestRefreshConfiguration(ConfigurationChanges changes) {
Chris Ye87143712020-11-10 05:05:58 +0000634 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000636 if (changes.any()) {
637 bool needWake = !mConfigurationChangesToRefresh.any();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 mConfigurationChangesToRefresh |= changes;
639
640 if (needWake) {
641 mEventHub->wake();
642 }
643 }
644}
645
Chris Ye87143712020-11-10 05:05:58 +0000646void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
647 int32_t token) {
648 std::scoped_lock _l(mLock);
649
Chris Ye1c2e0892020-11-30 21:41:44 -0800650 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800651 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700652 mPendingArgs += device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800653 }
654}
655
656void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000657 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658
Chris Ye1c2e0892020-11-30 21:41:44 -0800659 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800660 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700661 mPendingArgs += device->cancelVibrate(token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800662 }
663}
664
Chris Ye87143712020-11-10 05:05:58 +0000665bool InputReader::isVibrating(int32_t deviceId) {
666 std::scoped_lock _l(mLock);
667
668 InputDevice* device = findInputDeviceLocked(deviceId);
669 if (device) {
670 return device->isVibrating();
671 }
672 return false;
673}
674
675std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
676 std::scoped_lock _l(mLock);
677
678 InputDevice* device = findInputDeviceLocked(deviceId);
679 if (device) {
680 return device->getVibratorIds();
681 }
682 return {};
683}
684
Chris Yef59a2f42020-10-16 12:55:26 -0700685void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
686 std::scoped_lock _l(mLock);
687
688 InputDevice* device = findInputDeviceLocked(deviceId);
689 if (device) {
690 device->disableSensor(sensorType);
691 }
692}
693
694bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
695 std::chrono::microseconds samplingPeriod,
696 std::chrono::microseconds maxBatchReportLatency) {
697 std::scoped_lock _l(mLock);
698
699 InputDevice* device = findInputDeviceLocked(deviceId);
700 if (device) {
701 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
702 }
703 return false;
704}
705
706void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
707 std::scoped_lock _l(mLock);
708
709 InputDevice* device = findInputDeviceLocked(deviceId);
710 if (device) {
711 device->flushSensor(sensorType);
712 }
713}
714
Kim Low03ea0352020-11-06 12:45:07 -0800715std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400716 std::optional<int32_t> eventHubId;
717 {
718 // Do not query the battery state while holding the lock. For some peripheral devices,
719 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
720 // would block all other event processing during this time. For now, we assume this
721 // call never happens on the InputReader thread and get the battery state outside the
722 // lock to prevent event processing from being blocked by this call.
723 std::scoped_lock _l(mLock);
724 InputDevice* device = findInputDeviceLocked(deviceId);
725 if (!device) return {};
726 eventHubId = device->getBatteryEventHubId();
727 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800728
Andy Chenf9f1a022022-08-29 20:07:10 -0400729 if (!eventHubId) return {};
730 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000731 if (batteryIds.empty()) {
732 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
733 return {};
734 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400735 return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800736}
737
738std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400739 std::optional<int32_t> eventHubId;
740 {
741 // Do not query the battery state while holding the lock. For some peripheral devices,
742 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
743 // would block all other event processing during this time. For now, we assume this
744 // call never happens on the InputReader thread and get the battery state outside the
745 // lock to prevent event processing from being blocked by this call.
746 std::scoped_lock _l(mLock);
747 InputDevice* device = findInputDeviceLocked(deviceId);
748 if (!device) return {};
749 eventHubId = device->getBatteryEventHubId();
750 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800751
Andy Chenf9f1a022022-08-29 20:07:10 -0400752 if (!eventHubId) return {};
753 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000754 if (batteryIds.empty()) {
755 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
756 return {};
757 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400758 return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800759}
760
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000761std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) {
762 std::scoped_lock _l(mLock);
763
764 InputDevice* device = findInputDeviceLocked(deviceId);
765 if (!device) return {};
766
767 std::optional<int32_t> eventHubId = device->getBatteryEventHubId();
768 if (!eventHubId) return {};
769 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
770 if (batteryIds.empty()) {
771 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
772 return {};
773 }
774 const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front());
775 if (!batteryInfo) {
776 ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__,
777 batteryIds.front(), *eventHubId);
778 return {};
779 }
780 return batteryInfo->path;
781}
782
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000783std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800784 std::scoped_lock _l(mLock);
785
786 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000787 if (device == nullptr) {
788 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800789 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000790
791 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800792}
793
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000794std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800795 std::scoped_lock _l(mLock);
796
797 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000798 if (device == nullptr) {
799 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800800 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000801
802 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800803}
804
Omar Abdelmonem5e70e962024-08-06 09:38:42 +0000805std::optional<HardwareProperties> InputReader::getTouchpadHardwareProperties(int32_t deviceId) {
806 std::scoped_lock _l(mLock);
807
808 InputDevice* device = findInputDeviceLocked(deviceId);
809
810 if (device == nullptr) {
811 return {};
812 }
813
814 return device->getTouchpadHardwareProperties();
815}
816
Chris Ye3fdbfef2021-01-06 18:45:18 -0800817bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
818 std::scoped_lock _l(mLock);
819
820 InputDevice* device = findInputDeviceLocked(deviceId);
821 if (device) {
822 return device->setLightColor(lightId, color);
823 }
824 return false;
825}
826
827bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
828 std::scoped_lock _l(mLock);
829
830 InputDevice* device = findInputDeviceLocked(deviceId);
831 if (device) {
832 return device->setLightPlayerId(lightId, playerId);
833 }
834 return false;
835}
836
837std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
838 std::scoped_lock _l(mLock);
839
840 InputDevice* device = findInputDeviceLocked(deviceId);
841 if (device) {
842 return device->getLightColor(lightId);
843 }
844 return std::nullopt;
845}
846
847std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
848 std::scoped_lock _l(mLock);
849
850 InputDevice* device = findInputDeviceLocked(deviceId);
851 if (device) {
852 return device->getLightPlayerId(lightId);
853 }
854 return std::nullopt;
855}
856
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000857std::optional<std::string> InputReader::getBluetoothAddress(int32_t deviceId) const {
858 std::scoped_lock _l(mLock);
859
860 InputDevice* device = findInputDeviceLocked(deviceId);
861 if (device) {
862 return device->getBluetoothAddress();
863 }
864 return std::nullopt;
865}
866
Linnan Li13bf76a2024-05-05 19:18:02 +0800867bool InputReader::canDispatchToDisplay(int32_t deviceId, ui::LogicalDisplayId displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000868 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800869
Chris Ye1c2e0892020-11-30 21:41:44 -0800870 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800871 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800872 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
873 return false;
874 }
875
Arthur Hung2c9a3342019-07-23 14:18:59 +0800876 if (!device->isEnabled()) {
877 ALOGW("Ignoring disabled device %s", device->getName().c_str());
878 return false;
879 }
880
Linnan Li13bf76a2024-05-05 19:18:02 +0800881 std::optional<ui::LogicalDisplayId> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800882 // No associated display. By default, can dispatch to all displays.
Linnan Li13bf76a2024-05-05 19:18:02 +0800883 if (!associatedDisplayId || !associatedDisplayId->isValid()) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800884 return true;
885 }
886
887 return *associatedDisplayId == displayId;
888}
889
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000890void InputReader::sysfsNodeChanged(const std::string& sysfsNodePath) {
891 mEventHub->sysfsNodeChanged(sysfsNodePath);
892}
893
Prabir Pradhan018faea2024-05-08 21:52:54 +0000894DeviceId InputReader::getLastUsedInputDeviceId() {
895 std::scoped_lock _l(mLock);
896 return mLastUsedDeviceId;
897}
898
Arpit Singh849beb42024-06-06 07:14:17 +0000899void InputReader::notifyMouseCursorFadedOnTyping() {
900 std::scoped_lock _l(mLock);
901 // disable touchpad taps when cursor has faded due to typing
902 mPreventingTouchpadTaps = true;
903}
904
Yanye Li81a590c2024-10-22 19:25:54 +0000905bool InputReader::setKernelWakeEnabled(int32_t deviceId, bool enabled) {
906 std::scoped_lock _l(mLock);
Yanye Li0dd9a4e2024-11-15 16:09:19 +0000907 if (!com::android::input::flags::set_input_device_kernel_wake()){
908 return false;
909 }
Yanye Li81a590c2024-10-22 19:25:54 +0000910 InputDevice* device = findInputDeviceLocked(deviceId);
911 if (device) {
912 return device->setKernelWakeEnabled(enabled);
913 }
914 return false;
915}
916
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800917void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000918 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800919
920 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800921 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922
Chris Yee7310032020-09-22 15:36:28 -0700923 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
924 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925
Chris Yee7310032020-09-22 15:36:28 -0700926 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
927 const std::shared_ptr<InputDevice>& device = devicePair.first;
928 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
929 for (const auto& eId : devicePair.second) {
930 eventHubDevStr += StringPrintf("%d ", eId);
931 }
932 eventHubDevStr += "] \n";
933 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 }
935
Harry Cutts8c7cb592023-08-23 17:20:13 +0000936 dump += StringPrintf(INDENT "NextTimeout: %" PRId64 "\n", mNextTimeout);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800937 dump += INDENT "Configuration:\n";
938 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
940 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800941 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100943 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800945 dump += "]\n";
946 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700947 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800948
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800949 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700950 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
951 "acceleration=%0.3f\n",
952 mConfig.pointerVelocityControlParameters.scale,
953 mConfig.pointerVelocityControlParameters.lowThreshold,
954 mConfig.pointerVelocityControlParameters.highThreshold,
955 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800956
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800957 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700958 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
959 "acceleration=%0.3f\n",
960 mConfig.wheelVelocityControlParameters.scale,
961 mConfig.wheelVelocityControlParameters.lowThreshold,
962 mConfig.wheelVelocityControlParameters.highThreshold,
963 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800965 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700966 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800967 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700968 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800969 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700970 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800971 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700972 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800973 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700974 mConfig.pointerGestureTapDragInterval * 0.000001f);
975 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800976 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700977 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800978 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700979 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800980 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700981 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800982 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700983 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800984 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700985 mConfig.pointerGestureMovementSpeedRatio);
986 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700987
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800988 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700989 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990}
991
992void InputReader::monitor() {
993 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800994 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800996 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997 // Check the EventHub
998 mEventHub->monitor();
999}
1000
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001// --- InputReader::ContextImpl ---
1002
Garfield Tanff1f1bb2020-01-28 13:24:04 -08001003InputReader::ContextImpl::ContextImpl(InputReader* reader)
1004 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005
1006void InputReader::ContextImpl::updateGlobalMetaState() {
1007 // lock is already held by the input loop
1008 mReader->updateGlobalMetaStateLocked();
1009}
1010
1011int32_t InputReader::ContextImpl::getGlobalMetaState() {
1012 // lock is already held by the input loop
1013 return mReader->getGlobalMetaStateLocked();
1014}
1015
arthurhungc903df12020-08-11 15:08:42 +08001016void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
1017 // lock is already held by the input loop
1018 mReader->updateLedMetaStateLocked(metaState);
1019}
1020
1021int32_t InputReader::ContextImpl::getLedMetaState() {
1022 // lock is already held by the input loop
1023 return mReader->getLedMetaStateLocked();
1024}
1025
Arpit Singha5ea7c12023-07-05 15:39:25 +00001026void InputReader::ContextImpl::setPreventingTouchpadTaps(bool prevent) {
1027 // lock is already held by the input loop
1028 mReader->mPreventingTouchpadTaps = prevent;
1029}
1030
1031bool InputReader::ContextImpl::isPreventingTouchpadTaps() {
1032 // lock is already held by the input loop
1033 return mReader->mPreventingTouchpadTaps;
1034}
1035
Arpit Singh82e413e2023-10-10 19:30:58 +00001036void InputReader::ContextImpl::setLastKeyDownTimestamp(nsecs_t when) {
1037 mReader->mLastKeyDownTimestamp = when;
1038}
1039
1040nsecs_t InputReader::ContextImpl::getLastKeyDownTimestamp() {
1041 return mReader->mLastKeyDownTimestamp;
1042}
1043
Michael Wrightd02c5b62014-02-10 15:10:22 -08001044void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
1045 // lock is already held by the input loop
1046 mReader->disableVirtualKeysUntilLocked(time);
1047}
1048
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001049bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
1050 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001052 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001053}
1054
Michael Wrightd02c5b62014-02-10 15:10:22 -08001055void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
1056 // lock is already held by the input loop
1057 mReader->requestTimeoutAtTimeLocked(when);
1058}
1059
1060int32_t InputReader::ContextImpl::bumpGeneration() {
1061 // lock is already held by the input loop
1062 return mReader->bumpGenerationLocked();
1063}
1064
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001065void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -07001066 // lock is already held by whatever called refreshConfigurationLocked
1067 mReader->getExternalStylusDevicesLocked(outDevices);
1068}
1069
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001070std::list<NotifyArgs> InputReader::ContextImpl::dispatchExternalStylusState(
1071 const StylusState& state) {
1072 return mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -07001073}
1074
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
1076 return mReader->mPolicy.get();
1077}
1078
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079EventHubInterface* InputReader::ContextImpl::getEventHub() {
1080 return mReader->mEventHub.get();
1081}
1082
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00001083int32_t InputReader::ContextImpl::getNextId() {
1084 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -08001085}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001086
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00001087KeyboardClassifier& InputReader::ContextImpl::getKeyboardClassifier() {
1088 return *mReader->mKeyboardClassifier;
1089}
1090
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091} // namespace android