blob: a6f5df75934889f13d34efb329e0d56eb1c80a27 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070017#include "Macros.h"
Michael Wright842500e2015-03-13 17:32:02 -070018
Michael Wrightd02c5b62014-02-10 15:10:22 -080019#include "InputReader.h"
20
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080021#include <android-base/stringprintf.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070022#include <errno.h>
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080023#include <input/Keyboard.h>
24#include <input/VirtualKeyMap.h>
Michael Wright842500e2015-03-13 17:32:02 -070025#include <inttypes.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070026#include <limits.h>
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080027#include <log/log.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070028#include <math.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <stddef.h>
30#include <stdlib.h>
31#include <unistd.h>
Prabir Pradhan28efc192019-11-05 01:10:04 +000032#include <utils/Errors.h>
Prabir Pradhan28efc192019-11-05 01:10:04 +000033#include <utils/Thread.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080035#include "InputDevice.h"
Omar Abdelmonem5e70e962024-08-06 09:38:42 +000036#include "include/gestures.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080037
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080038using android::base::StringPrintf;
39
Michael Wrightd02c5b62014-02-10 15:10:22 -080040namespace android {
41
Prabir Pradhan018faea2024-05-08 21:52:54 +000042namespace {
43
Josh Bartel938632f2022-07-19 15:34:22 -050044/**
45 * Determines if the identifiers passed are a sub-devices. Sub-devices are physical devices
46 * that expose multiple input device paths such a keyboard that also has a touchpad input.
47 * These are separate devices with unique descriptors in EventHub, but InputReader should
48 * create a single InputDevice for them.
49 * Sub-devices are detected by the following criteria:
50 * 1. The vendor, product, bus, version, and unique id match
51 * 2. The location matches. The location is used to distinguish a single device with multiple
52 * inputs versus the same device plugged into multiple ports.
53 */
54
Prabir Pradhan018faea2024-05-08 21:52:54 +000055bool isSubDevice(const InputDeviceIdentifier& identifier1,
56 const InputDeviceIdentifier& identifier2) {
Josh Bartel938632f2022-07-19 15:34:22 -050057 return (identifier1.vendor == identifier2.vendor &&
58 identifier1.product == identifier2.product && identifier1.bus == identifier2.bus &&
59 identifier1.version == identifier2.version &&
60 identifier1.uniqueId == identifier2.uniqueId &&
61 identifier1.location == identifier2.location);
62}
63
Prabir Pradhan018faea2024-05-08 21:52:54 +000064bool isStylusPointerGestureStart(const NotifyMotionArgs& motionArgs) {
Prabir Pradhanda20b172022-09-26 17:01:18 +000065 const auto actionMasked = MotionEvent::getActionMasked(motionArgs.action);
66 if (actionMasked != AMOTION_EVENT_ACTION_HOVER_ENTER &&
67 actionMasked != AMOTION_EVENT_ACTION_DOWN &&
68 actionMasked != AMOTION_EVENT_ACTION_POINTER_DOWN) {
69 return false;
70 }
71 const auto actionIndex = MotionEvent::getActionIndex(motionArgs.action);
Prabir Pradhane5626962022-10-27 20:30:53 +000072 return isStylusToolType(motionArgs.pointerProperties[actionIndex].toolType);
Prabir Pradhanda20b172022-09-26 17:01:18 +000073}
74
Prabir Pradhan018faea2024-05-08 21:52:54 +000075bool isNewGestureStart(const NotifyMotionArgs& motion) {
76 return motion.action == AMOTION_EVENT_ACTION_DOWN ||
77 motion.action == AMOTION_EVENT_ACTION_HOVER_ENTER;
78}
79
80bool isNewGestureStart(const NotifyKeyArgs& key) {
81 return key.action == AKEY_EVENT_ACTION_DOWN;
82}
83
84// Return the event's device ID if it marks the start of a new gesture.
85std::optional<DeviceId> getDeviceIdOfNewGesture(const NotifyArgs& args) {
86 if (const auto* motion = std::get_if<NotifyMotionArgs>(&args); motion != nullptr) {
87 return isNewGestureStart(*motion) ? std::make_optional(motion->deviceId) : std::nullopt;
88 }
89 if (const auto* key = std::get_if<NotifyKeyArgs>(&args); key != nullptr) {
90 return isNewGestureStart(*key) ? std::make_optional(key->deviceId) : std::nullopt;
91 }
92 return std::nullopt;
93}
94
95} // namespace
96
Prabir Pradhan28efc192019-11-05 01:10:04 +000097// --- InputReader ---
98
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070099InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
100 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700101 InputListenerInterface& listener)
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700102 : mContext(this),
103 mEventHub(eventHub),
104 mPolicy(policy),
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700105 mNextListener(listener),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000106 mKeyboardClassifier(std::make_unique<KeyboardClassifier>()),
Arthur Hung95f68612022-04-07 14:08:22 +0800107 mGlobalMetaState(AMETA_NONE),
108 mLedMetaState(AMETA_NONE),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700109 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800110 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700111 mDisableVirtualKeysTimeout(LLONG_MIN),
112 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113 mConfigurationChangesToRefresh(0) {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000114 refreshConfigurationLocked(/*changes=*/{});
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700115 updateGlobalMetaStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116}
117
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000118InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119
Prabir Pradhan28efc192019-11-05 01:10:04 +0000120status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700121 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000122 return ALREADY_EXISTS;
123 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700124 mThread = std::make_unique<InputThread>(
Siarhei Vishniakouf53fa6b2024-09-19 17:42:42 -0700125 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); },
126 /*isInCriticalPath=*/true);
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700127 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000128}
129
130status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700131 if (mThread && mThread->isCallingThread()) {
132 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +0000133 return INVALID_OPERATION;
134 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700135 mThread.reset();
136 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000137}
138
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139void InputReader::loopOnce() {
140 int32_t oldGeneration;
141 int32_t timeoutMillis;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000142 // Copy some state so that we can access it outside the lock later.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -0800144 std::vector<InputDeviceInfo> inputDevices;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000145 std::list<NotifyArgs> notifyArgs;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000147 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148
149 oldGeneration = mGeneration;
150 timeoutMillis = -1;
151
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000152 auto changes = mConfigurationChangesToRefresh;
153 if (changes.any()) {
154 mConfigurationChangesToRefresh.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155 timeoutMillis = 0;
156 refreshConfigurationLocked(changes);
157 } else if (mNextTimeout != LLONG_MAX) {
158 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
159 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
160 }
161 } // release lock
162
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700163 std::vector<RawEvent> events = mEventHub->getEvents(timeoutMillis);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
165 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000166 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800167 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700169 if (!events.empty()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700170 mPendingArgs += processEventsLocked(events.data(), events.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 }
172
173 if (mNextTimeout != LLONG_MAX) {
174 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
175 if (now >= mNextTimeout) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000176 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800177 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
178 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 mNextTimeout = LLONG_MAX;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700180 mPendingArgs += timeoutExpiredLocked(now);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 }
182 }
183
184 if (oldGeneration != mGeneration) {
Liana Kazanova5b8217b2024-07-18 17:44:51 +0000185 // Reset global meta state because it depends on connected input devices.
186 updateGlobalMetaStateLocked();
187
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800189 inputDevices = getInputDevicesLocked();
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700190 mPendingArgs.emplace_back(
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000191 NotifyInputDevicesChangedArgs{mContext.getNextId(), inputDevices});
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700193
194 std::swap(notifyArgs, mPendingArgs);
Prabir Pradhan018faea2024-05-08 21:52:54 +0000195
196 // Keep track of the last used device
197 for (const NotifyArgs& args : notifyArgs) {
198 mLastUsedDeviceId = getDeviceIdOfNewGesture(args).value_or(mLastUsedDeviceId);
199 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200 } // release lock
201
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202 // Flush queued events out to the listener.
203 // This must happen outside of the lock because the listener could potentially call
204 // back into the InputReader's methods, such as getScanCodeState, or become blocked
205 // on another thread similarly waiting to acquire the InputReader lock thereby
206 // resulting in a deadlock. This situation is actually quite plausible because the
207 // listener is actually the input dispatcher, which calls into the window manager,
208 // which occasionally calls into the input reader.
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700209 for (const NotifyArgs& args : notifyArgs) {
210 mNextListener.notify(args);
211 }
Prabir Pradhanc3a92472024-02-06 20:08:05 +0000212
213 // Notify the policy that input devices have changed.
214 // This must be done after flushing events down the listener chain to ensure that the rest of
215 // the listeners are synchronized with the changes before the policy reacts to them.
216 if (inputDevicesChanged) {
217 mPolicy->notifyInputDevicesChanged(inputDevices);
218 }
219
220 // Notify the policy of the start of every new stylus gesture.
221 for (const auto& args : notifyArgs) {
222 const auto* motionArgs = std::get_if<NotifyMotionArgs>(&args);
223 if (motionArgs != nullptr && isStylusPointerGestureStart(*motionArgs)) {
224 mPolicy->notifyStylusGestureStarted(motionArgs->deviceId, motionArgs->eventTime);
225 }
226 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227}
228
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700229std::list<NotifyArgs> InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
230 std::list<NotifyArgs> out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 for (const RawEvent* rawEvent = rawEvents; count;) {
232 int32_t type = rawEvent->type;
233 size_t batchSize = 1;
234 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
235 int32_t deviceId = rawEvent->deviceId;
236 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700237 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
238 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239 break;
240 }
241 batchSize += 1;
242 }
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000243 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800244 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
245 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700246 out += processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247 } else {
248 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700249 case EventHubInterface::DEVICE_ADDED:
250 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
251 break;
252 case EventHubInterface::DEVICE_REMOVED:
253 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
254 break;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700255 default:
256 ALOG_ASSERT(false); // can't happen
257 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258 }
259 }
260 count -= batchSize;
261 rawEvent += batchSize;
262 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700263 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264}
265
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800266void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
267 if (mDevices.find(eventHubId) != mDevices.end()) {
268 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 return;
270 }
271
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800272 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
Arpit Singh82f29a12023-06-13 15:05:53 +0000273 std::shared_ptr<InputDevice> device = createDeviceLocked(when, eventHubId, identifier);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700274
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700275 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
276 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800277
278 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800279 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
280 "(ignored non-input device)",
281 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000283 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800284 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000285 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800286 }
287
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800288 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700289 // Add device to device to EventHub ids map.
290 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
291 if (mapIt == mDeviceToEventHubIdsMap.end()) {
292 std::vector<int32_t> ids = {eventHubId};
293 mDeviceToEventHubIdsMap.emplace(device, ids);
294 } else {
295 mapIt->second.push_back(eventHubId);
296 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700298
Chris Ye1b0c7342020-07-28 21:57:03 -0700299 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800300 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700301 }
Chris Yef59a2f42020-10-16 12:55:26 -0700302
303 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800304 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
305 // device class to disable SENSOR sub device only.
306 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700307 mEventHub->disableDevice(eventHubId);
308 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309}
310
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800311void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
312 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000313 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800314 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800315 return;
316 }
317
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000318 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000319 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700320 // Erase device from device to EventHub ids map.
321 auto mapIt = mDeviceToEventHubIdsMap.find(device);
322 if (mapIt != mDeviceToEventHubIdsMap.end()) {
323 std::vector<int32_t>& eventHubIds = mapIt->second;
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -0800324 std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
Chris Yee7310032020-09-22 15:36:28 -0700325 if (eventHubIds.size() == 0) {
326 mDeviceToEventHubIdsMap.erase(mapIt);
327 }
328 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 bumpGenerationLocked();
330
331 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800332 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
333 "(ignored non-input device)",
334 device->getId(), eventHubId, device->getName().c_str(),
335 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000337 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800338 device->getId(), eventHubId, device->getName().c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000339 device->getDescriptor().c_str(),
340 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 }
342
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800343 device->removeEventHubDevice(eventHubId);
344
Chris Ye1b0c7342020-07-28 21:57:03 -0700345 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800346 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700347 }
348
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800349 if (device->hasEventHubDevices()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700350 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800351 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700352 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353}
354
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000355std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Arpit Singh82f29a12023-06-13 15:05:53 +0000356 nsecs_t when, int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800357 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
Josh Bartel938632f2022-07-19 15:34:22 -0500358 const InputDeviceIdentifier identifier2 =
359 devicePair.second->getDeviceInfo().getIdentifier();
360 return isSubDevice(identifier, identifier2);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800361 });
362
363 std::shared_ptr<InputDevice> device;
364 if (deviceIt != mDevices.end()) {
365 device = deviceIt->second;
366 } else {
367 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
368 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
369 identifier);
370 }
Arpit Singh82f29a12023-06-13 15:05:53 +0000371 mPendingArgs += device->addEventHubDevice(when, eventHubId, mConfig);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372 return device;
373}
374
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700375std::list<NotifyArgs> InputReader::processEventsForDeviceLocked(int32_t eventHubId,
376 const RawEvent* rawEvents,
377 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800378 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000379 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800380 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700381 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 }
383
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000384 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700386 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700387 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388 }
389
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700390 return device->process(rawEvents, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391}
392
Philip Junker4af3b3d2021-12-14 10:36:55 +0100393InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800394 auto deviceIt =
395 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
396 return devicePair.second->getId() == deviceId;
397 });
398 if (deviceIt != mDevices.end()) {
399 return deviceIt->second.get();
400 }
401 return nullptr;
402}
403
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700404std::list<NotifyArgs> InputReader::timeoutExpiredLocked(nsecs_t when) {
405 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000406 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000407 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 if (!device->isIgnored()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700409 out += device->timeoutExpired(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410 }
411 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700412 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413}
414
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800415int32_t InputReader::nextInputDeviceIdLocked() {
416 return ++mNextInputDeviceId;
417}
418
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000419void InputReader::refreshConfigurationLocked(ConfigurationChanges changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 mPolicy->getReaderConfiguration(&mConfig);
421 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
422
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000423 using Change = InputReaderConfiguration::Change;
424 if (!changes.any()) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000426 ALOGI("Reconfiguring input devices, changes=%s", changes.string().c_str());
Prabir Pradhan7e186182020-11-10 13:56:45 -0800427 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800428
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000429 if (changes.test(Change::MUST_REOPEN)) {
Prabir Pradhan7e186182020-11-10 13:56:45 -0800430 mEventHub->requestReopenDevices();
431 } else {
432 for (auto& devicePair : mDevices) {
433 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700434 mPendingArgs += device->configure(now, mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435 }
436 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800437
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000438 if (changes.test(Change::POINTER_CAPTURE)) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000439 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
440 ALOGV("Skipping notifying pointer capture changes: "
441 "There was no change in the pointer capture state.");
442 } else {
443 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700444 mPendingArgs.emplace_back(
445 NotifyPointerCaptureChangedArgs{mContext.getNextId(), now,
446 mCurrentPointerCaptureRequest});
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000447 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800448 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449}
450
451void InputReader::updateGlobalMetaStateLocked() {
452 mGlobalMetaState = 0;
453
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000454 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000455 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800456 mGlobalMetaState |= device->getMetaState();
457 }
458}
459
460int32_t InputReader::getGlobalMetaStateLocked() {
461 return mGlobalMetaState;
462}
463
arthurhungc903df12020-08-11 15:08:42 +0800464void InputReader::updateLedMetaStateLocked(int32_t metaState) {
465 mLedMetaState = metaState;
466 for (auto& devicePair : mDevices) {
467 std::shared_ptr<InputDevice>& device = devicePair.second;
468 device->updateLedState(false);
469 }
470}
471
472int32_t InputReader::getLedMetaStateLocked() {
473 return mLedMetaState;
474}
475
Chris Ye1c2e0892020-11-30 21:41:44 -0800476void InputReader::notifyExternalStylusPresenceChangedLocked() {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000477 refreshConfigurationLocked(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Michael Wright842500e2015-03-13 17:32:02 -0700478}
479
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800480void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000481 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000482 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700483 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000484 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700485 }
486 }
487}
488
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700489std::list<NotifyArgs> InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
490 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000491 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000492 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700493 out += device->updateExternalStylusState(state);
Michael Wright842500e2015-03-13 17:32:02 -0700494 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700495 return out;
Michael Wright842500e2015-03-13 17:32:02 -0700496}
497
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
499 mDisableVirtualKeysTimeout = time;
500}
501
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800502bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800504 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700505 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800506 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 return true;
508 } else {
509 return false;
510 }
511}
512
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
514 if (when < mNextTimeout) {
515 mNextTimeout = when;
516 mEventHub->wake();
517 }
518}
519
520int32_t InputReader::bumpGenerationLocked() {
521 return ++mGeneration;
522}
523
Chris Ye98d3f532020-10-01 21:48:59 -0700524std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000525 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700526 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800527}
528
Chris Ye98d3f532020-10-01 21:48:59 -0700529std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
530 std::vector<InputDeviceInfo> outInputDevices;
531 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800532
Chris Yee7310032020-09-22 15:36:28 -0700533 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000535 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800536 }
537 }
Chris Ye98d3f532020-10-01 21:48:59 -0700538 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539}
540
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700541int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000542 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800543
544 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
545}
546
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700547int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000548 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549
550 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
551}
552
553int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000554 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555
556 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
557}
558
559int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700560 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800561 int32_t result = AKEY_STATE_UNKNOWN;
562 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800563 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800564 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
565 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 }
567 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000568 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000569 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700570 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
572 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000573 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 if (currentResult >= AKEY_STATE_DOWN) {
575 return currentResult;
576 } else if (currentResult == AKEY_STATE_UP) {
577 result = currentResult;
578 }
579 }
580 }
581 }
582 return result;
583}
584
Andrii Kulian763a3a42016-03-08 10:46:16 -0800585void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000586 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800587 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800588 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800589 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
590 return;
591 }
592
Andrii Kulian763a3a42016-03-08 10:46:16 -0800593 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000594 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800595 return;
596 }
597
598 device->updateMetaState(AKEYCODE_CAPS_LOCK);
599}
600
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700601bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
602 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000603 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700605 memset(outFlags, 0, keyCodes.size());
606 return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800607}
608
609bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700610 const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700611 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 bool result = false;
613 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800614 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800615 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700616 result = device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 }
618 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000619 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000620 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700621 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700622 result |= device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623 }
624 }
625 }
626 return result;
627}
628
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000629void InputReader::addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const {
630 std::scoped_lock _l(mLock);
631
632 InputDevice* device = findInputDeviceLocked(deviceId);
633 if (device != nullptr) {
634 device->addKeyRemapping(fromKeyCode, toKeyCode);
635 }
636}
637
Philip Junker4af3b3d2021-12-14 10:36:55 +0100638int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
639 std::scoped_lock _l(mLock);
640
641 InputDevice* device = findInputDeviceLocked(deviceId);
642 if (device == nullptr) {
643 ALOGW("Failed to get key code for key location: Input device with id %d not found",
644 deviceId);
645 return AKEYCODE_UNKNOWN;
646 }
647 return device->getKeyCodeForKeyLocation(locationKeyCode);
648}
649
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000650void InputReader::requestRefreshConfiguration(ConfigurationChanges changes) {
Chris Ye87143712020-11-10 05:05:58 +0000651 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000653 if (changes.any()) {
654 bool needWake = !mConfigurationChangesToRefresh.any();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 mConfigurationChangesToRefresh |= changes;
656
657 if (needWake) {
658 mEventHub->wake();
659 }
660 }
661}
662
Chris Ye87143712020-11-10 05:05:58 +0000663void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
664 int32_t token) {
665 std::scoped_lock _l(mLock);
666
Chris Ye1c2e0892020-11-30 21:41:44 -0800667 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800668 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700669 mPendingArgs += device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 }
671}
672
673void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000674 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675
Chris Ye1c2e0892020-11-30 21:41:44 -0800676 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800677 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700678 mPendingArgs += device->cancelVibrate(token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 }
680}
681
Chris Ye87143712020-11-10 05:05:58 +0000682bool InputReader::isVibrating(int32_t deviceId) {
683 std::scoped_lock _l(mLock);
684
685 InputDevice* device = findInputDeviceLocked(deviceId);
686 if (device) {
687 return device->isVibrating();
688 }
689 return false;
690}
691
692std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
693 std::scoped_lock _l(mLock);
694
695 InputDevice* device = findInputDeviceLocked(deviceId);
696 if (device) {
697 return device->getVibratorIds();
698 }
699 return {};
700}
701
Chris Yef59a2f42020-10-16 12:55:26 -0700702void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
703 std::scoped_lock _l(mLock);
704
705 InputDevice* device = findInputDeviceLocked(deviceId);
706 if (device) {
707 device->disableSensor(sensorType);
708 }
709}
710
711bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
712 std::chrono::microseconds samplingPeriod,
713 std::chrono::microseconds maxBatchReportLatency) {
714 std::scoped_lock _l(mLock);
715
716 InputDevice* device = findInputDeviceLocked(deviceId);
717 if (device) {
718 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
719 }
720 return false;
721}
722
723void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
724 std::scoped_lock _l(mLock);
725
726 InputDevice* device = findInputDeviceLocked(deviceId);
727 if (device) {
728 device->flushSensor(sensorType);
729 }
730}
731
Kim Low03ea0352020-11-06 12:45:07 -0800732std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400733 std::optional<int32_t> eventHubId;
734 {
735 // Do not query the battery state while holding the lock. For some peripheral devices,
736 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
737 // would block all other event processing during this time. For now, we assume this
738 // call never happens on the InputReader thread and get the battery state outside the
739 // lock to prevent event processing from being blocked by this call.
740 std::scoped_lock _l(mLock);
741 InputDevice* device = findInputDeviceLocked(deviceId);
742 if (!device) return {};
743 eventHubId = device->getBatteryEventHubId();
744 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800745
Andy Chenf9f1a022022-08-29 20:07:10 -0400746 if (!eventHubId) return {};
747 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000748 if (batteryIds.empty()) {
749 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
750 return {};
751 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400752 return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800753}
754
755std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400756 std::optional<int32_t> eventHubId;
757 {
758 // Do not query the battery state while holding the lock. For some peripheral devices,
759 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
760 // would block all other event processing during this time. For now, we assume this
761 // call never happens on the InputReader thread and get the battery state outside the
762 // lock to prevent event processing from being blocked by this call.
763 std::scoped_lock _l(mLock);
764 InputDevice* device = findInputDeviceLocked(deviceId);
765 if (!device) return {};
766 eventHubId = device->getBatteryEventHubId();
767 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800768
Andy Chenf9f1a022022-08-29 20:07:10 -0400769 if (!eventHubId) return {};
770 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000771 if (batteryIds.empty()) {
772 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
773 return {};
774 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400775 return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800776}
777
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000778std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) {
779 std::scoped_lock _l(mLock);
780
781 InputDevice* device = findInputDeviceLocked(deviceId);
782 if (!device) return {};
783
784 std::optional<int32_t> eventHubId = device->getBatteryEventHubId();
785 if (!eventHubId) return {};
786 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
787 if (batteryIds.empty()) {
788 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
789 return {};
790 }
791 const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front());
792 if (!batteryInfo) {
793 ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__,
794 batteryIds.front(), *eventHubId);
795 return {};
796 }
797 return batteryInfo->path;
798}
799
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000800std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800801 std::scoped_lock _l(mLock);
802
803 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000804 if (device == nullptr) {
805 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800806 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000807
808 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800809}
810
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000811std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800812 std::scoped_lock _l(mLock);
813
814 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000815 if (device == nullptr) {
816 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800817 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000818
819 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800820}
821
Omar Abdelmonem5e70e962024-08-06 09:38:42 +0000822std::optional<HardwareProperties> InputReader::getTouchpadHardwareProperties(int32_t deviceId) {
823 std::scoped_lock _l(mLock);
824
825 InputDevice* device = findInputDeviceLocked(deviceId);
826
827 if (device == nullptr) {
828 return {};
829 }
830
831 return device->getTouchpadHardwareProperties();
832}
833
Chris Ye3fdbfef2021-01-06 18:45:18 -0800834bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
835 std::scoped_lock _l(mLock);
836
837 InputDevice* device = findInputDeviceLocked(deviceId);
838 if (device) {
839 return device->setLightColor(lightId, color);
840 }
841 return false;
842}
843
844bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
845 std::scoped_lock _l(mLock);
846
847 InputDevice* device = findInputDeviceLocked(deviceId);
848 if (device) {
849 return device->setLightPlayerId(lightId, playerId);
850 }
851 return false;
852}
853
854std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
855 std::scoped_lock _l(mLock);
856
857 InputDevice* device = findInputDeviceLocked(deviceId);
858 if (device) {
859 return device->getLightColor(lightId);
860 }
861 return std::nullopt;
862}
863
864std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
865 std::scoped_lock _l(mLock);
866
867 InputDevice* device = findInputDeviceLocked(deviceId);
868 if (device) {
869 return device->getLightPlayerId(lightId);
870 }
871 return std::nullopt;
872}
873
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000874std::optional<std::string> InputReader::getBluetoothAddress(int32_t deviceId) const {
875 std::scoped_lock _l(mLock);
876
877 InputDevice* device = findInputDeviceLocked(deviceId);
878 if (device) {
879 return device->getBluetoothAddress();
880 }
881 return std::nullopt;
882}
883
Linnan Li13bf76a2024-05-05 19:18:02 +0800884bool InputReader::canDispatchToDisplay(int32_t deviceId, ui::LogicalDisplayId displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000885 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800886
Chris Ye1c2e0892020-11-30 21:41:44 -0800887 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800888 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800889 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
890 return false;
891 }
892
Arthur Hung2c9a3342019-07-23 14:18:59 +0800893 if (!device->isEnabled()) {
894 ALOGW("Ignoring disabled device %s", device->getName().c_str());
895 return false;
896 }
897
Linnan Li13bf76a2024-05-05 19:18:02 +0800898 std::optional<ui::LogicalDisplayId> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800899 // No associated display. By default, can dispatch to all displays.
Linnan Li13bf76a2024-05-05 19:18:02 +0800900 if (!associatedDisplayId || !associatedDisplayId->isValid()) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800901 return true;
902 }
903
904 return *associatedDisplayId == displayId;
905}
906
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000907void InputReader::sysfsNodeChanged(const std::string& sysfsNodePath) {
908 mEventHub->sysfsNodeChanged(sysfsNodePath);
909}
910
Prabir Pradhan018faea2024-05-08 21:52:54 +0000911DeviceId InputReader::getLastUsedInputDeviceId() {
912 std::scoped_lock _l(mLock);
913 return mLastUsedDeviceId;
914}
915
Arpit Singh849beb42024-06-06 07:14:17 +0000916void InputReader::notifyMouseCursorFadedOnTyping() {
917 std::scoped_lock _l(mLock);
918 // disable touchpad taps when cursor has faded due to typing
919 mPreventingTouchpadTaps = true;
920}
921
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800922void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000923 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924
925 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800926 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
Chris Yee7310032020-09-22 15:36:28 -0700928 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
929 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930
Chris Yee7310032020-09-22 15:36:28 -0700931 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
932 const std::shared_ptr<InputDevice>& device = devicePair.first;
933 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
934 for (const auto& eId : devicePair.second) {
935 eventHubDevStr += StringPrintf("%d ", eId);
936 }
937 eventHubDevStr += "] \n";
938 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 }
940
Harry Cutts8c7cb592023-08-23 17:20:13 +0000941 dump += StringPrintf(INDENT "NextTimeout: %" PRId64 "\n", mNextTimeout);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800942 dump += INDENT "Configuration:\n";
943 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
945 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800946 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100948 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800950 dump += "]\n";
951 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700952 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800954 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700955 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
956 "acceleration=%0.3f\n",
957 mConfig.pointerVelocityControlParameters.scale,
958 mConfig.pointerVelocityControlParameters.lowThreshold,
959 mConfig.pointerVelocityControlParameters.highThreshold,
960 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800962 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700963 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
964 "acceleration=%0.3f\n",
965 mConfig.wheelVelocityControlParameters.scale,
966 mConfig.wheelVelocityControlParameters.lowThreshold,
967 mConfig.wheelVelocityControlParameters.highThreshold,
968 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800970 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700971 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800972 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700973 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800974 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700975 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800976 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700977 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800978 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700979 mConfig.pointerGestureTapDragInterval * 0.000001f);
980 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800981 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700982 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800983 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700984 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800985 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700986 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800987 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700988 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800989 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700990 mConfig.pointerGestureMovementSpeedRatio);
991 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700992
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800993 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700994 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995}
996
997void InputReader::monitor() {
998 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800999 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -08001001 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002 // Check the EventHub
1003 mEventHub->monitor();
1004}
1005
Michael Wrightd02c5b62014-02-10 15:10:22 -08001006// --- InputReader::ContextImpl ---
1007
Garfield Tanff1f1bb2020-01-28 13:24:04 -08001008InputReader::ContextImpl::ContextImpl(InputReader* reader)
1009 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001010
1011void InputReader::ContextImpl::updateGlobalMetaState() {
1012 // lock is already held by the input loop
1013 mReader->updateGlobalMetaStateLocked();
1014}
1015
1016int32_t InputReader::ContextImpl::getGlobalMetaState() {
1017 // lock is already held by the input loop
1018 return mReader->getGlobalMetaStateLocked();
1019}
1020
arthurhungc903df12020-08-11 15:08:42 +08001021void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
1022 // lock is already held by the input loop
1023 mReader->updateLedMetaStateLocked(metaState);
1024}
1025
1026int32_t InputReader::ContextImpl::getLedMetaState() {
1027 // lock is already held by the input loop
1028 return mReader->getLedMetaStateLocked();
1029}
1030
Arpit Singha5ea7c12023-07-05 15:39:25 +00001031void InputReader::ContextImpl::setPreventingTouchpadTaps(bool prevent) {
1032 // lock is already held by the input loop
1033 mReader->mPreventingTouchpadTaps = prevent;
1034}
1035
1036bool InputReader::ContextImpl::isPreventingTouchpadTaps() {
1037 // lock is already held by the input loop
1038 return mReader->mPreventingTouchpadTaps;
1039}
1040
Arpit Singh82e413e2023-10-10 19:30:58 +00001041void InputReader::ContextImpl::setLastKeyDownTimestamp(nsecs_t when) {
1042 mReader->mLastKeyDownTimestamp = when;
1043}
1044
1045nsecs_t InputReader::ContextImpl::getLastKeyDownTimestamp() {
1046 return mReader->mLastKeyDownTimestamp;
1047}
1048
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
1050 // lock is already held by the input loop
1051 mReader->disableVirtualKeysUntilLocked(time);
1052}
1053
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001054bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
1055 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001056 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001057 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058}
1059
Michael Wrightd02c5b62014-02-10 15:10:22 -08001060void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
1061 // lock is already held by the input loop
1062 mReader->requestTimeoutAtTimeLocked(when);
1063}
1064
1065int32_t InputReader::ContextImpl::bumpGeneration() {
1066 // lock is already held by the input loop
1067 return mReader->bumpGenerationLocked();
1068}
1069
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001070void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -07001071 // lock is already held by whatever called refreshConfigurationLocked
1072 mReader->getExternalStylusDevicesLocked(outDevices);
1073}
1074
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001075std::list<NotifyArgs> InputReader::ContextImpl::dispatchExternalStylusState(
1076 const StylusState& state) {
1077 return mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -07001078}
1079
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
1081 return mReader->mPolicy.get();
1082}
1083
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084EventHubInterface* InputReader::ContextImpl::getEventHub() {
1085 return mReader->mEventHub.get();
1086}
1087
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00001088int32_t InputReader::ContextImpl::getNextId() {
1089 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -08001090}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00001092KeyboardClassifier& InputReader::ContextImpl::getKeyboardClassifier() {
1093 return *mReader->mKeyboardClassifier;
1094}
1095
Michael Wrightd02c5b62014-02-10 15:10:22 -08001096} // namespace android