blob: a5b12490a34d659aa2ced353e9bd46305ec3d6cc [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>(
125 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
126 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000127}
128
129status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700130 if (mThread && mThread->isCallingThread()) {
131 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +0000132 return INVALID_OPERATION;
133 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700134 mThread.reset();
135 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000136}
137
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138void InputReader::loopOnce() {
139 int32_t oldGeneration;
140 int32_t timeoutMillis;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000141 // Copy some state so that we can access it outside the lock later.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800142 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -0800143 std::vector<InputDeviceInfo> inputDevices;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000144 std::list<NotifyArgs> notifyArgs;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000146 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147
148 oldGeneration = mGeneration;
149 timeoutMillis = -1;
150
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000151 auto changes = mConfigurationChangesToRefresh;
152 if (changes.any()) {
153 mConfigurationChangesToRefresh.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 timeoutMillis = 0;
155 refreshConfigurationLocked(changes);
156 } else if (mNextTimeout != LLONG_MAX) {
157 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
158 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
159 }
160 } // release lock
161
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700162 std::vector<RawEvent> events = mEventHub->getEvents(timeoutMillis);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163
164 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000165 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800166 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700168 if (!events.empty()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700169 mPendingArgs += processEventsLocked(events.data(), events.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 }
171
172 if (mNextTimeout != LLONG_MAX) {
173 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
174 if (now >= mNextTimeout) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000175 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800176 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
177 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178 mNextTimeout = LLONG_MAX;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700179 mPendingArgs += timeoutExpiredLocked(now);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 }
181 }
182
183 if (oldGeneration != mGeneration) {
Liana Kazanova5b8217b2024-07-18 17:44:51 +0000184 // Reset global meta state because it depends on connected input devices.
185 updateGlobalMetaStateLocked();
186
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800188 inputDevices = getInputDevicesLocked();
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700189 mPendingArgs.emplace_back(
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000190 NotifyInputDevicesChangedArgs{mContext.getNextId(), inputDevices});
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700192
193 std::swap(notifyArgs, mPendingArgs);
Prabir Pradhan018faea2024-05-08 21:52:54 +0000194
195 // Keep track of the last used device
196 for (const NotifyArgs& args : notifyArgs) {
197 mLastUsedDeviceId = getDeviceIdOfNewGesture(args).value_or(mLastUsedDeviceId);
198 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199 } // release lock
200
Michael Wrightd02c5b62014-02-10 15:10:22 -0800201 // Flush queued events out to the listener.
202 // This must happen outside of the lock because the listener could potentially call
203 // back into the InputReader's methods, such as getScanCodeState, or become blocked
204 // on another thread similarly waiting to acquire the InputReader lock thereby
205 // resulting in a deadlock. This situation is actually quite plausible because the
206 // listener is actually the input dispatcher, which calls into the window manager,
207 // which occasionally calls into the input reader.
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700208 for (const NotifyArgs& args : notifyArgs) {
209 mNextListener.notify(args);
210 }
Prabir Pradhanc3a92472024-02-06 20:08:05 +0000211
212 // Notify the policy that input devices have changed.
213 // This must be done after flushing events down the listener chain to ensure that the rest of
214 // the listeners are synchronized with the changes before the policy reacts to them.
215 if (inputDevicesChanged) {
216 mPolicy->notifyInputDevicesChanged(inputDevices);
217 }
218
219 // Notify the policy of the start of every new stylus gesture.
220 for (const auto& args : notifyArgs) {
221 const auto* motionArgs = std::get_if<NotifyMotionArgs>(&args);
222 if (motionArgs != nullptr && isStylusPointerGestureStart(*motionArgs)) {
223 mPolicy->notifyStylusGestureStarted(motionArgs->deviceId, motionArgs->eventTime);
224 }
225 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226}
227
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700228std::list<NotifyArgs> InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
229 std::list<NotifyArgs> out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 for (const RawEvent* rawEvent = rawEvents; count;) {
231 int32_t type = rawEvent->type;
232 size_t batchSize = 1;
233 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
234 int32_t deviceId = rawEvent->deviceId;
235 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700236 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
237 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238 break;
239 }
240 batchSize += 1;
241 }
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000242 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800243 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
244 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700245 out += processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246 } else {
247 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700248 case EventHubInterface::DEVICE_ADDED:
249 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
250 break;
251 case EventHubInterface::DEVICE_REMOVED:
252 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
253 break;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700254 default:
255 ALOG_ASSERT(false); // can't happen
256 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257 }
258 }
259 count -= batchSize;
260 rawEvent += batchSize;
261 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700262 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263}
264
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800265void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
266 if (mDevices.find(eventHubId) != mDevices.end()) {
267 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 return;
269 }
270
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800271 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
Arpit Singh82f29a12023-06-13 15:05:53 +0000272 std::shared_ptr<InputDevice> device = createDeviceLocked(when, eventHubId, identifier);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700273
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700274 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
275 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276
277 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800278 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
279 "(ignored non-input device)",
280 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000282 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800283 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000284 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285 }
286
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800287 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700288 // Add device to device to EventHub ids map.
289 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
290 if (mapIt == mDeviceToEventHubIdsMap.end()) {
291 std::vector<int32_t> ids = {eventHubId};
292 mDeviceToEventHubIdsMap.emplace(device, ids);
293 } else {
294 mapIt->second.push_back(eventHubId);
295 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700297
Chris Ye1b0c7342020-07-28 21:57:03 -0700298 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800299 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700300 }
Chris Yef59a2f42020-10-16 12:55:26 -0700301
302 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800303 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
304 // device class to disable SENSOR sub device only.
305 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700306 mEventHub->disableDevice(eventHubId);
307 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308}
309
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800310void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
311 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000312 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800313 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 return;
315 }
316
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000317 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000318 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700319 // Erase device from device to EventHub ids map.
320 auto mapIt = mDeviceToEventHubIdsMap.find(device);
321 if (mapIt != mDeviceToEventHubIdsMap.end()) {
322 std::vector<int32_t>& eventHubIds = mapIt->second;
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -0800323 std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
Chris Yee7310032020-09-22 15:36:28 -0700324 if (eventHubIds.size() == 0) {
325 mDeviceToEventHubIdsMap.erase(mapIt);
326 }
327 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 bumpGenerationLocked();
329
330 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800331 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
332 "(ignored non-input device)",
333 device->getId(), eventHubId, device->getName().c_str(),
334 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000336 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800337 device->getId(), eventHubId, device->getName().c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000338 device->getDescriptor().c_str(),
339 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340 }
341
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800342 device->removeEventHubDevice(eventHubId);
343
Chris Ye1b0c7342020-07-28 21:57:03 -0700344 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800345 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700346 }
347
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800348 if (device->hasEventHubDevices()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700349 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800350 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700351 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800352}
353
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000354std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Arpit Singh82f29a12023-06-13 15:05:53 +0000355 nsecs_t when, int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800356 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
Josh Bartel938632f2022-07-19 15:34:22 -0500357 const InputDeviceIdentifier identifier2 =
358 devicePair.second->getDeviceInfo().getIdentifier();
359 return isSubDevice(identifier, identifier2);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800360 });
361
362 std::shared_ptr<InputDevice> device;
363 if (deviceIt != mDevices.end()) {
364 device = deviceIt->second;
365 } else {
366 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
367 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
368 identifier);
369 }
Arpit Singh82f29a12023-06-13 15:05:53 +0000370 mPendingArgs += device->addEventHubDevice(when, eventHubId, mConfig);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371 return device;
372}
373
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700374std::list<NotifyArgs> InputReader::processEventsForDeviceLocked(int32_t eventHubId,
375 const RawEvent* rawEvents,
376 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800377 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000378 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800379 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700380 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381 }
382
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000383 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800384 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700385 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700386 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 }
388
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700389 return device->process(rawEvents, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390}
391
Philip Junker4af3b3d2021-12-14 10:36:55 +0100392InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800393 auto deviceIt =
394 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
395 return devicePair.second->getId() == deviceId;
396 });
397 if (deviceIt != mDevices.end()) {
398 return deviceIt->second.get();
399 }
400 return nullptr;
401}
402
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700403std::list<NotifyArgs> InputReader::timeoutExpiredLocked(nsecs_t when) {
404 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000405 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000406 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 if (!device->isIgnored()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700408 out += device->timeoutExpired(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409 }
410 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700411 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800412}
413
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800414int32_t InputReader::nextInputDeviceIdLocked() {
415 return ++mNextInputDeviceId;
416}
417
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000418void InputReader::refreshConfigurationLocked(ConfigurationChanges changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800419 mPolicy->getReaderConfiguration(&mConfig);
420 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
421
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000422 using Change = InputReaderConfiguration::Change;
423 if (!changes.any()) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800424
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000425 ALOGI("Reconfiguring input devices, changes=%s", changes.string().c_str());
Prabir Pradhan7e186182020-11-10 13:56:45 -0800426 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800427
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000428 if (changes.test(Change::MUST_REOPEN)) {
Prabir Pradhan7e186182020-11-10 13:56:45 -0800429 mEventHub->requestReopenDevices();
430 } else {
431 for (auto& devicePair : mDevices) {
432 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700433 mPendingArgs += device->configure(now, mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800434 }
435 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800436
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000437 if (changes.test(Change::POINTER_CAPTURE)) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000438 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
439 ALOGV("Skipping notifying pointer capture changes: "
440 "There was no change in the pointer capture state.");
441 } else {
442 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700443 mPendingArgs.emplace_back(
444 NotifyPointerCaptureChangedArgs{mContext.getNextId(), now,
445 mCurrentPointerCaptureRequest});
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000446 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800447 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800448}
449
450void InputReader::updateGlobalMetaStateLocked() {
451 mGlobalMetaState = 0;
452
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000453 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000454 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455 mGlobalMetaState |= device->getMetaState();
456 }
457}
458
459int32_t InputReader::getGlobalMetaStateLocked() {
460 return mGlobalMetaState;
461}
462
arthurhungc903df12020-08-11 15:08:42 +0800463void InputReader::updateLedMetaStateLocked(int32_t metaState) {
464 mLedMetaState = metaState;
465 for (auto& devicePair : mDevices) {
466 std::shared_ptr<InputDevice>& device = devicePair.second;
467 device->updateLedState(false);
468 }
469}
470
471int32_t InputReader::getLedMetaStateLocked() {
472 return mLedMetaState;
473}
474
Chris Ye1c2e0892020-11-30 21:41:44 -0800475void InputReader::notifyExternalStylusPresenceChangedLocked() {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000476 refreshConfigurationLocked(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Michael Wright842500e2015-03-13 17:32:02 -0700477}
478
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800479void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000480 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000481 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700482 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000483 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700484 }
485 }
486}
487
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700488std::list<NotifyArgs> InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
489 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000490 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000491 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700492 out += device->updateExternalStylusState(state);
Michael Wright842500e2015-03-13 17:32:02 -0700493 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700494 return out;
Michael Wright842500e2015-03-13 17:32:02 -0700495}
496
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
498 mDisableVirtualKeysTimeout = time;
499}
500
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800501bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800503 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700504 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800505 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506 return true;
507 } else {
508 return false;
509 }
510}
511
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
513 if (when < mNextTimeout) {
514 mNextTimeout = when;
515 mEventHub->wake();
516 }
517}
518
519int32_t InputReader::bumpGenerationLocked() {
520 return ++mGeneration;
521}
522
Chris Ye98d3f532020-10-01 21:48:59 -0700523std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000524 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700525 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526}
527
Chris Ye98d3f532020-10-01 21:48:59 -0700528std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
529 std::vector<InputDeviceInfo> outInputDevices;
530 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531
Chris Yee7310032020-09-22 15:36:28 -0700532 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000534 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 }
536 }
Chris Ye98d3f532020-10-01 21:48:59 -0700537 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538}
539
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700540int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000541 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800542
543 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
544}
545
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700546int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000547 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800548
549 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
550}
551
552int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000553 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554
555 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
556}
557
558int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700559 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800560 int32_t result = AKEY_STATE_UNKNOWN;
561 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800562 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800563 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
564 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565 }
566 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000567 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000568 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700569 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
571 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000572 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573 if (currentResult >= AKEY_STATE_DOWN) {
574 return currentResult;
575 } else if (currentResult == AKEY_STATE_UP) {
576 result = currentResult;
577 }
578 }
579 }
580 }
581 return result;
582}
583
Andrii Kulian763a3a42016-03-08 10:46:16 -0800584void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000585 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800586 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800587 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800588 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
589 return;
590 }
591
Andrii Kulian763a3a42016-03-08 10:46:16 -0800592 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000593 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800594 return;
595 }
596
597 device->updateMetaState(AKEYCODE_CAPS_LOCK);
598}
599
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700600bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
601 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000602 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800603
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700604 memset(outFlags, 0, keyCodes.size());
605 return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606}
607
608bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700609 const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700610 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 bool result = false;
612 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800613 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800614 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700615 result = device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 }
617 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000618 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000619 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700620 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700621 result |= device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 }
623 }
624 }
625 return result;
626}
627
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000628void InputReader::addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const {
629 std::scoped_lock _l(mLock);
630
631 InputDevice* device = findInputDeviceLocked(deviceId);
632 if (device != nullptr) {
633 device->addKeyRemapping(fromKeyCode, toKeyCode);
634 }
635}
636
Philip Junker4af3b3d2021-12-14 10:36:55 +0100637int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
638 std::scoped_lock _l(mLock);
639
640 InputDevice* device = findInputDeviceLocked(deviceId);
641 if (device == nullptr) {
642 ALOGW("Failed to get key code for key location: Input device with id %d not found",
643 deviceId);
644 return AKEYCODE_UNKNOWN;
645 }
646 return device->getKeyCodeForKeyLocation(locationKeyCode);
647}
648
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000649void InputReader::requestRefreshConfiguration(ConfigurationChanges changes) {
Chris Ye87143712020-11-10 05:05:58 +0000650 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000652 if (changes.any()) {
653 bool needWake = !mConfigurationChangesToRefresh.any();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800654 mConfigurationChangesToRefresh |= changes;
655
656 if (needWake) {
657 mEventHub->wake();
658 }
659 }
660}
661
Chris Ye87143712020-11-10 05:05:58 +0000662void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
663 int32_t token) {
664 std::scoped_lock _l(mLock);
665
Chris Ye1c2e0892020-11-30 21:41:44 -0800666 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800667 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700668 mPendingArgs += device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 }
670}
671
672void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000673 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674
Chris Ye1c2e0892020-11-30 21:41:44 -0800675 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800676 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700677 mPendingArgs += device->cancelVibrate(token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800678 }
679}
680
Chris Ye87143712020-11-10 05:05:58 +0000681bool InputReader::isVibrating(int32_t deviceId) {
682 std::scoped_lock _l(mLock);
683
684 InputDevice* device = findInputDeviceLocked(deviceId);
685 if (device) {
686 return device->isVibrating();
687 }
688 return false;
689}
690
691std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
692 std::scoped_lock _l(mLock);
693
694 InputDevice* device = findInputDeviceLocked(deviceId);
695 if (device) {
696 return device->getVibratorIds();
697 }
698 return {};
699}
700
Chris Yef59a2f42020-10-16 12:55:26 -0700701void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
702 std::scoped_lock _l(mLock);
703
704 InputDevice* device = findInputDeviceLocked(deviceId);
705 if (device) {
706 device->disableSensor(sensorType);
707 }
708}
709
710bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
711 std::chrono::microseconds samplingPeriod,
712 std::chrono::microseconds maxBatchReportLatency) {
713 std::scoped_lock _l(mLock);
714
715 InputDevice* device = findInputDeviceLocked(deviceId);
716 if (device) {
717 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
718 }
719 return false;
720}
721
722void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
723 std::scoped_lock _l(mLock);
724
725 InputDevice* device = findInputDeviceLocked(deviceId);
726 if (device) {
727 device->flushSensor(sensorType);
728 }
729}
730
Kim Low03ea0352020-11-06 12:45:07 -0800731std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400732 std::optional<int32_t> eventHubId;
733 {
734 // Do not query the battery state while holding the lock. For some peripheral devices,
735 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
736 // would block all other event processing during this time. For now, we assume this
737 // call never happens on the InputReader thread and get the battery state outside the
738 // lock to prevent event processing from being blocked by this call.
739 std::scoped_lock _l(mLock);
740 InputDevice* device = findInputDeviceLocked(deviceId);
741 if (!device) return {};
742 eventHubId = device->getBatteryEventHubId();
743 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800744
Andy Chenf9f1a022022-08-29 20:07:10 -0400745 if (!eventHubId) return {};
746 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000747 if (batteryIds.empty()) {
748 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
749 return {};
750 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400751 return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800752}
753
754std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400755 std::optional<int32_t> eventHubId;
756 {
757 // Do not query the battery state while holding the lock. For some peripheral devices,
758 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
759 // would block all other event processing during this time. For now, we assume this
760 // call never happens on the InputReader thread and get the battery state outside the
761 // lock to prevent event processing from being blocked by this call.
762 std::scoped_lock _l(mLock);
763 InputDevice* device = findInputDeviceLocked(deviceId);
764 if (!device) return {};
765 eventHubId = device->getBatteryEventHubId();
766 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800767
Andy Chenf9f1a022022-08-29 20:07:10 -0400768 if (!eventHubId) return {};
769 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000770 if (batteryIds.empty()) {
771 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
772 return {};
773 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400774 return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800775}
776
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000777std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) {
778 std::scoped_lock _l(mLock);
779
780 InputDevice* device = findInputDeviceLocked(deviceId);
781 if (!device) return {};
782
783 std::optional<int32_t> eventHubId = device->getBatteryEventHubId();
784 if (!eventHubId) return {};
785 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
786 if (batteryIds.empty()) {
787 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
788 return {};
789 }
790 const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front());
791 if (!batteryInfo) {
792 ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__,
793 batteryIds.front(), *eventHubId);
794 return {};
795 }
796 return batteryInfo->path;
797}
798
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000799std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800800 std::scoped_lock _l(mLock);
801
802 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000803 if (device == nullptr) {
804 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800805 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000806
807 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800808}
809
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000810std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800811 std::scoped_lock _l(mLock);
812
813 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000814 if (device == nullptr) {
815 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800816 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000817
818 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800819}
820
Omar Abdelmonem5e70e962024-08-06 09:38:42 +0000821std::optional<HardwareProperties> InputReader::getTouchpadHardwareProperties(int32_t deviceId) {
822 std::scoped_lock _l(mLock);
823
824 InputDevice* device = findInputDeviceLocked(deviceId);
825
826 if (device == nullptr) {
827 return {};
828 }
829
830 return device->getTouchpadHardwareProperties();
831}
832
Chris Ye3fdbfef2021-01-06 18:45:18 -0800833bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
834 std::scoped_lock _l(mLock);
835
836 InputDevice* device = findInputDeviceLocked(deviceId);
837 if (device) {
838 return device->setLightColor(lightId, color);
839 }
840 return false;
841}
842
843bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
844 std::scoped_lock _l(mLock);
845
846 InputDevice* device = findInputDeviceLocked(deviceId);
847 if (device) {
848 return device->setLightPlayerId(lightId, playerId);
849 }
850 return false;
851}
852
853std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
854 std::scoped_lock _l(mLock);
855
856 InputDevice* device = findInputDeviceLocked(deviceId);
857 if (device) {
858 return device->getLightColor(lightId);
859 }
860 return std::nullopt;
861}
862
863std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
864 std::scoped_lock _l(mLock);
865
866 InputDevice* device = findInputDeviceLocked(deviceId);
867 if (device) {
868 return device->getLightPlayerId(lightId);
869 }
870 return std::nullopt;
871}
872
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000873std::optional<std::string> InputReader::getBluetoothAddress(int32_t deviceId) const {
874 std::scoped_lock _l(mLock);
875
876 InputDevice* device = findInputDeviceLocked(deviceId);
877 if (device) {
878 return device->getBluetoothAddress();
879 }
880 return std::nullopt;
881}
882
Linnan Li13bf76a2024-05-05 19:18:02 +0800883bool InputReader::canDispatchToDisplay(int32_t deviceId, ui::LogicalDisplayId displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000884 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800885
Chris Ye1c2e0892020-11-30 21:41:44 -0800886 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800887 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800888 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
889 return false;
890 }
891
Arthur Hung2c9a3342019-07-23 14:18:59 +0800892 if (!device->isEnabled()) {
893 ALOGW("Ignoring disabled device %s", device->getName().c_str());
894 return false;
895 }
896
Linnan Li13bf76a2024-05-05 19:18:02 +0800897 std::optional<ui::LogicalDisplayId> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800898 // No associated display. By default, can dispatch to all displays.
Linnan Li13bf76a2024-05-05 19:18:02 +0800899 if (!associatedDisplayId || !associatedDisplayId->isValid()) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800900 return true;
901 }
902
903 return *associatedDisplayId == displayId;
904}
905
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000906void InputReader::sysfsNodeChanged(const std::string& sysfsNodePath) {
907 mEventHub->sysfsNodeChanged(sysfsNodePath);
908}
909
Prabir Pradhan018faea2024-05-08 21:52:54 +0000910DeviceId InputReader::getLastUsedInputDeviceId() {
911 std::scoped_lock _l(mLock);
912 return mLastUsedDeviceId;
913}
914
Arpit Singh849beb42024-06-06 07:14:17 +0000915void InputReader::notifyMouseCursorFadedOnTyping() {
916 std::scoped_lock _l(mLock);
917 // disable touchpad taps when cursor has faded due to typing
918 mPreventingTouchpadTaps = true;
919}
920
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800921void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000922 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923
924 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800925 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926
Chris Yee7310032020-09-22 15:36:28 -0700927 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
928 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929
Chris Yee7310032020-09-22 15:36:28 -0700930 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
931 const std::shared_ptr<InputDevice>& device = devicePair.first;
932 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
933 for (const auto& eId : devicePair.second) {
934 eventHubDevStr += StringPrintf("%d ", eId);
935 }
936 eventHubDevStr += "] \n";
937 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938 }
939
Harry Cutts8c7cb592023-08-23 17:20:13 +0000940 dump += StringPrintf(INDENT "NextTimeout: %" PRId64 "\n", mNextTimeout);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800941 dump += INDENT "Configuration:\n";
942 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
944 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800945 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100947 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800948 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800949 dump += "]\n";
950 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700951 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800953 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700954 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
955 "acceleration=%0.3f\n",
956 mConfig.pointerVelocityControlParameters.scale,
957 mConfig.pointerVelocityControlParameters.lowThreshold,
958 mConfig.pointerVelocityControlParameters.highThreshold,
959 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800961 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700962 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
963 "acceleration=%0.3f\n",
964 mConfig.wheelVelocityControlParameters.scale,
965 mConfig.wheelVelocityControlParameters.lowThreshold,
966 mConfig.wheelVelocityControlParameters.highThreshold,
967 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800969 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700970 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800971 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700972 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800973 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700974 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800975 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700976 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800977 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700978 mConfig.pointerGestureTapDragInterval * 0.000001f);
979 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800980 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700981 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800982 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700983 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800984 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700985 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800986 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700987 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800988 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700989 mConfig.pointerGestureMovementSpeedRatio);
990 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700991
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800992 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700993 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800994}
995
996void InputReader::monitor() {
997 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800998 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -08001000 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001 // Check the EventHub
1002 mEventHub->monitor();
1003}
1004
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005// --- InputReader::ContextImpl ---
1006
Garfield Tanff1f1bb2020-01-28 13:24:04 -08001007InputReader::ContextImpl::ContextImpl(InputReader* reader)
1008 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009
1010void InputReader::ContextImpl::updateGlobalMetaState() {
1011 // lock is already held by the input loop
1012 mReader->updateGlobalMetaStateLocked();
1013}
1014
1015int32_t InputReader::ContextImpl::getGlobalMetaState() {
1016 // lock is already held by the input loop
1017 return mReader->getGlobalMetaStateLocked();
1018}
1019
arthurhungc903df12020-08-11 15:08:42 +08001020void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
1021 // lock is already held by the input loop
1022 mReader->updateLedMetaStateLocked(metaState);
1023}
1024
1025int32_t InputReader::ContextImpl::getLedMetaState() {
1026 // lock is already held by the input loop
1027 return mReader->getLedMetaStateLocked();
1028}
1029
Arpit Singha5ea7c12023-07-05 15:39:25 +00001030void InputReader::ContextImpl::setPreventingTouchpadTaps(bool prevent) {
1031 // lock is already held by the input loop
1032 mReader->mPreventingTouchpadTaps = prevent;
1033}
1034
1035bool InputReader::ContextImpl::isPreventingTouchpadTaps() {
1036 // lock is already held by the input loop
1037 return mReader->mPreventingTouchpadTaps;
1038}
1039
Arpit Singh82e413e2023-10-10 19:30:58 +00001040void InputReader::ContextImpl::setLastKeyDownTimestamp(nsecs_t when) {
1041 mReader->mLastKeyDownTimestamp = when;
1042}
1043
1044nsecs_t InputReader::ContextImpl::getLastKeyDownTimestamp() {
1045 return mReader->mLastKeyDownTimestamp;
1046}
1047
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
1049 // lock is already held by the input loop
1050 mReader->disableVirtualKeysUntilLocked(time);
1051}
1052
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001053bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
1054 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001055 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001056 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001057}
1058
Michael Wrightd02c5b62014-02-10 15:10:22 -08001059void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
1060 // lock is already held by the input loop
1061 mReader->requestTimeoutAtTimeLocked(when);
1062}
1063
1064int32_t InputReader::ContextImpl::bumpGeneration() {
1065 // lock is already held by the input loop
1066 return mReader->bumpGenerationLocked();
1067}
1068
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001069void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -07001070 // lock is already held by whatever called refreshConfigurationLocked
1071 mReader->getExternalStylusDevicesLocked(outDevices);
1072}
1073
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001074std::list<NotifyArgs> InputReader::ContextImpl::dispatchExternalStylusState(
1075 const StylusState& state) {
1076 return mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -07001077}
1078
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
1080 return mReader->mPolicy.get();
1081}
1082
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083EventHubInterface* InputReader::ContextImpl::getEventHub() {
1084 return mReader->mEventHub.get();
1085}
1086
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00001087int32_t InputReader::ContextImpl::getNextId() {
1088 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -08001089}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00001091KeyboardClassifier& InputReader::ContextImpl::getKeyboardClassifier() {
1092 return *mReader->mKeyboardClassifier;
1093}
1094
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095} // namespace android