blob: 7f63355387ac9338ae0fa254521c69a48dff61cc [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"
36
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080037using android::base::StringPrintf;
38
Michael Wrightd02c5b62014-02-10 15:10:22 -080039namespace android {
40
Josh Bartel938632f2022-07-19 15:34:22 -050041/**
42 * Determines if the identifiers passed are a sub-devices. Sub-devices are physical devices
43 * that expose multiple input device paths such a keyboard that also has a touchpad input.
44 * These are separate devices with unique descriptors in EventHub, but InputReader should
45 * create a single InputDevice for them.
46 * Sub-devices are detected by the following criteria:
47 * 1. The vendor, product, bus, version, and unique id match
48 * 2. The location matches. The location is used to distinguish a single device with multiple
49 * inputs versus the same device plugged into multiple ports.
50 */
51
52static bool isSubDevice(const InputDeviceIdentifier& identifier1,
53 const InputDeviceIdentifier& identifier2) {
54 return (identifier1.vendor == identifier2.vendor &&
55 identifier1.product == identifier2.product && identifier1.bus == identifier2.bus &&
56 identifier1.version == identifier2.version &&
57 identifier1.uniqueId == identifier2.uniqueId &&
58 identifier1.location == identifier2.location);
59}
60
Prabir Pradhanda20b172022-09-26 17:01:18 +000061static bool isStylusPointerGestureStart(const NotifyMotionArgs& motionArgs) {
62 const auto actionMasked = MotionEvent::getActionMasked(motionArgs.action);
63 if (actionMasked != AMOTION_EVENT_ACTION_HOVER_ENTER &&
64 actionMasked != AMOTION_EVENT_ACTION_DOWN &&
65 actionMasked != AMOTION_EVENT_ACTION_POINTER_DOWN) {
66 return false;
67 }
68 const auto actionIndex = MotionEvent::getActionIndex(motionArgs.action);
Prabir Pradhane5626962022-10-27 20:30:53 +000069 return isStylusToolType(motionArgs.pointerProperties[actionIndex].toolType);
Prabir Pradhanda20b172022-09-26 17:01:18 +000070}
71
Prabir Pradhan28efc192019-11-05 01:10:04 +000072// --- InputReader ---
73
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070074InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
75 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070076 InputListenerInterface& listener)
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070077 : mContext(this),
78 mEventHub(eventHub),
79 mPolicy(policy),
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -070080 mNextListener(listener),
Arthur Hung95f68612022-04-07 14:08:22 +080081 mGlobalMetaState(AMETA_NONE),
82 mLedMetaState(AMETA_NONE),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070083 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080084 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070085 mDisableVirtualKeysTimeout(LLONG_MIN),
86 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -080087 mConfigurationChangesToRefresh(0) {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000088 refreshConfigurationLocked(/*changes=*/{});
Siarhei Vishniakou18050092021-09-01 13:32:49 -070089 updateGlobalMetaStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -080090}
91
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +000092InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
Prabir Pradhan28efc192019-11-05 01:10:04 +000094status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070095 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000096 return ALREADY_EXISTS;
97 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070098 mThread = std::make_unique<InputThread>(
99 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
100 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000101}
102
103status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700104 if (mThread && mThread->isCallingThread()) {
105 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +0000106 return INVALID_OPERATION;
107 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700108 mThread.reset();
109 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000110}
111
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112void InputReader::loopOnce() {
113 int32_t oldGeneration;
114 int32_t timeoutMillis;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000115 // Copy some state so that we can access it outside the lock later.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -0800117 std::vector<InputDeviceInfo> inputDevices;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000118 std::list<NotifyArgs> notifyArgs;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000120 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800121
122 oldGeneration = mGeneration;
123 timeoutMillis = -1;
124
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000125 auto changes = mConfigurationChangesToRefresh;
126 if (changes.any()) {
127 mConfigurationChangesToRefresh.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128 timeoutMillis = 0;
129 refreshConfigurationLocked(changes);
130 } else if (mNextTimeout != LLONG_MAX) {
131 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
132 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
133 }
134 } // release lock
135
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700136 std::vector<RawEvent> events = mEventHub->getEvents(timeoutMillis);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137
138 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000139 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800140 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700142 if (!events.empty()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700143 mPendingArgs += processEventsLocked(events.data(), events.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144 }
145
146 if (mNextTimeout != LLONG_MAX) {
147 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
148 if (now >= mNextTimeout) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000149 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800150 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
151 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152 mNextTimeout = LLONG_MAX;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700153 mPendingArgs += timeoutExpiredLocked(now);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 }
155 }
156
157 if (oldGeneration != mGeneration) {
158 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800159 inputDevices = getInputDevicesLocked();
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700160 mPendingArgs.emplace_back(
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000161 NotifyInputDevicesChangedArgs{mContext.getNextId(), inputDevices});
Michael Wrightd02c5b62014-02-10 15:10:22 -0800162 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700163
164 std::swap(notifyArgs, mPendingArgs);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165 } // release lock
166
167 // Send out a message that the describes the changed input devices.
168 if (inputDevicesChanged) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800169 mPolicy->notifyInputDevicesChanged(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 }
171
Prabir Pradhanda20b172022-09-26 17:01:18 +0000172 // Notify the policy of the start of every new stylus gesture outside the lock.
173 for (const auto& args : notifyArgs) {
174 const auto* motionArgs = std::get_if<NotifyMotionArgs>(&args);
175 if (motionArgs != nullptr && isStylusPointerGestureStart(*motionArgs)) {
176 mPolicy->notifyStylusGestureStarted(motionArgs->deviceId, motionArgs->eventTime);
177 }
178 }
179
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 // Flush queued events out to the listener.
181 // This must happen outside of the lock because the listener could potentially call
182 // back into the InputReader's methods, such as getScanCodeState, or become blocked
183 // on another thread similarly waiting to acquire the InputReader lock thereby
184 // resulting in a deadlock. This situation is actually quite plausible because the
185 // listener is actually the input dispatcher, which calls into the window manager,
186 // which occasionally calls into the input reader.
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700187 for (const NotifyArgs& args : notifyArgs) {
188 mNextListener.notify(args);
189 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190}
191
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700192std::list<NotifyArgs> InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
193 std::list<NotifyArgs> out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800194 for (const RawEvent* rawEvent = rawEvents; count;) {
195 int32_t type = rawEvent->type;
196 size_t batchSize = 1;
197 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
198 int32_t deviceId = rawEvent->deviceId;
199 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700200 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
201 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202 break;
203 }
204 batchSize += 1;
205 }
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000206 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800207 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
208 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700209 out += processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210 } else {
211 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700212 case EventHubInterface::DEVICE_ADDED:
213 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
214 break;
215 case EventHubInterface::DEVICE_REMOVED:
216 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
217 break;
218 case EventHubInterface::FINISHED_DEVICE_SCAN:
219 handleConfigurationChangedLocked(rawEvent->when);
220 break;
221 default:
222 ALOG_ASSERT(false); // can't happen
223 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800224 }
225 }
226 count -= batchSize;
227 rawEvent += batchSize;
228 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700229 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230}
231
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800232void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
233 if (mDevices.find(eventHubId) != mDevices.end()) {
234 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800235 return;
236 }
237
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800238 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
Arpit Singh7f1765e2023-07-07 13:12:37 +0000239 std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700240
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700241 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
242 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800245 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
246 "(ignored non-input device)",
247 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000249 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800250 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000251 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252 }
253
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800254 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700255 // Add device to device to EventHub ids map.
256 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
257 if (mapIt == mDeviceToEventHubIdsMap.end()) {
258 std::vector<int32_t> ids = {eventHubId};
259 mDeviceToEventHubIdsMap.emplace(device, ids);
260 } else {
261 mapIt->second.push_back(eventHubId);
262 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700264
Chris Ye1b0c7342020-07-28 21:57:03 -0700265 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800266 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700267 }
Chris Yef59a2f42020-10-16 12:55:26 -0700268
269 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800270 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
271 // device class to disable SENSOR sub device only.
272 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700273 mEventHub->disableDevice(eventHubId);
274 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275}
276
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800277void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
278 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000279 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800280 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 return;
282 }
283
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000284 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000285 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700286 // Erase device from device to EventHub ids map.
287 auto mapIt = mDeviceToEventHubIdsMap.find(device);
288 if (mapIt != mDeviceToEventHubIdsMap.end()) {
289 std::vector<int32_t>& eventHubIds = mapIt->second;
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -0800290 std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
Chris Yee7310032020-09-22 15:36:28 -0700291 if (eventHubIds.size() == 0) {
292 mDeviceToEventHubIdsMap.erase(mapIt);
293 }
294 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 bumpGenerationLocked();
296
297 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800298 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
299 "(ignored non-input device)",
300 device->getId(), eventHubId, device->getName().c_str(),
301 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800302 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000303 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800304 device->getId(), eventHubId, device->getName().c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000305 device->getDescriptor().c_str(),
306 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 }
308
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800309 device->removeEventHubDevice(eventHubId);
310
Chris Ye1b0c7342020-07-28 21:57:03 -0700311 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800312 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700313 }
314
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800315 if (device->hasEventHubDevices()) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700316 mPendingArgs += device->configure(when, mConfig, /*changes=*/{});
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800317 }
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700318 mPendingArgs += device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319}
320
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000321std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Arpit Singh7f1765e2023-07-07 13:12:37 +0000322 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800323 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
Josh Bartel938632f2022-07-19 15:34:22 -0500324 const InputDeviceIdentifier identifier2 =
325 devicePair.second->getDeviceInfo().getIdentifier();
326 return isSubDevice(identifier, identifier2);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800327 });
328
329 std::shared_ptr<InputDevice> device;
330 if (deviceIt != mDevices.end()) {
331 device = deviceIt->second;
332 } else {
333 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
334 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
335 identifier);
336 }
Arpit Singh7f1765e2023-07-07 13:12:37 +0000337 device->addEventHubDevice(eventHubId, mConfig);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 return device;
339}
340
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700341std::list<NotifyArgs> InputReader::processEventsForDeviceLocked(int32_t eventHubId,
342 const RawEvent* rawEvents,
343 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800344 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000345 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800346 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700347 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348 }
349
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000350 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700352 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700353 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800354 }
355
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700356 return device->process(rawEvents, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800357}
358
Philip Junker4af3b3d2021-12-14 10:36:55 +0100359InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800360 auto deviceIt =
361 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
362 return devicePair.second->getId() == deviceId;
363 });
364 if (deviceIt != mDevices.end()) {
365 return deviceIt->second.get();
366 }
367 return nullptr;
368}
369
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700370std::list<NotifyArgs> InputReader::timeoutExpiredLocked(nsecs_t when) {
371 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000372 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000373 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374 if (!device->isIgnored()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700375 out += device->timeoutExpired(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376 }
377 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700378 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800379}
380
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800381int32_t InputReader::nextInputDeviceIdLocked() {
382 return ++mNextInputDeviceId;
383}
384
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
386 // Reset global meta state because it depends on the list of all configured devices.
387 updateGlobalMetaStateLocked();
388
389 // Enqueue configuration changed.
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700390 mPendingArgs.emplace_back(NotifyConfigurationChangedArgs{mContext.getNextId(), when});
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391}
392
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000393void InputReader::refreshConfigurationLocked(ConfigurationChanges changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394 mPolicy->getReaderConfiguration(&mConfig);
395 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
396
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000397 using Change = InputReaderConfiguration::Change;
398 if (!changes.any()) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000400 ALOGI("Reconfiguring input devices, changes=%s", changes.string().c_str());
Prabir Pradhan7e186182020-11-10 13:56:45 -0800401 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800402
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000403 if (changes.test(Change::DISPLAY_INFO)) {
Prabir Pradhan7e186182020-11-10 13:56:45 -0800404 updatePointerDisplayLocked();
405 }
406
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000407 if (changes.test(Change::MUST_REOPEN)) {
Prabir Pradhan7e186182020-11-10 13:56:45 -0800408 mEventHub->requestReopenDevices();
409 } else {
410 for (auto& devicePair : mDevices) {
411 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700412 mPendingArgs += device->configure(now, mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413 }
414 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800415
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000416 if (changes.test(Change::POINTER_CAPTURE)) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000417 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
418 ALOGV("Skipping notifying pointer capture changes: "
419 "There was no change in the pointer capture state.");
420 } else {
421 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700422 mPendingArgs.emplace_back(
423 NotifyPointerCaptureChangedArgs{mContext.getNextId(), now,
424 mCurrentPointerCaptureRequest});
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000425 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800426 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427}
428
429void InputReader::updateGlobalMetaStateLocked() {
430 mGlobalMetaState = 0;
431
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000432 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000433 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800434 mGlobalMetaState |= device->getMetaState();
435 }
436}
437
438int32_t InputReader::getGlobalMetaStateLocked() {
439 return mGlobalMetaState;
440}
441
arthurhungc903df12020-08-11 15:08:42 +0800442void InputReader::updateLedMetaStateLocked(int32_t metaState) {
443 mLedMetaState = metaState;
444 for (auto& devicePair : mDevices) {
445 std::shared_ptr<InputDevice>& device = devicePair.second;
446 device->updateLedState(false);
447 }
448}
449
450int32_t InputReader::getLedMetaStateLocked() {
451 return mLedMetaState;
452}
453
Chris Ye1c2e0892020-11-30 21:41:44 -0800454void InputReader::notifyExternalStylusPresenceChangedLocked() {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000455 refreshConfigurationLocked(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Michael Wright842500e2015-03-13 17:32:02 -0700456}
457
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800458void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000459 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000460 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700461 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000462 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700463 }
464 }
465}
466
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700467std::list<NotifyArgs> InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
468 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000469 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000470 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700471 out += device->updateExternalStylusState(state);
Michael Wright842500e2015-03-13 17:32:02 -0700472 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700473 return out;
Michael Wright842500e2015-03-13 17:32:02 -0700474}
475
Michael Wrightd02c5b62014-02-10 15:10:22 -0800476void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
477 mDisableVirtualKeysTimeout = time;
478}
479
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800480bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800481 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800482 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700483 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800484 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485 return true;
486 } else {
487 return false;
488 }
489}
490
Michael Wright17db18e2020-06-26 20:51:44 +0100491std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked(
492 int32_t deviceId) {
493 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800494 if (controller == nullptr) {
495 controller = mPolicy->obtainPointerController(deviceId);
496 mPointerController = controller;
497 updatePointerDisplayLocked();
498 }
499 return controller;
500}
501
502void InputReader::updatePointerDisplayLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100503 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800504 if (controller == nullptr) {
505 return;
506 }
507
508 std::optional<DisplayViewport> viewport =
509 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
510 if (!viewport) {
511 ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input "
512 "mapper. Fall back to default display",
513 mConfig.defaultPointerDisplayId);
514 viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT);
515 }
516 if (!viewport) {
517 ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to"
518 " PointerController.");
519 return;
520 }
521
522 controller->setDisplayViewport(*viewport);
523}
524
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525void InputReader::fadePointerLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100526 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800527 if (controller != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +0100528 controller->fade(PointerControllerInterface::Transition::GRADUAL);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529 }
530}
531
532void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
533 if (when < mNextTimeout) {
534 mNextTimeout = when;
535 mEventHub->wake();
536 }
537}
538
539int32_t InputReader::bumpGenerationLocked() {
540 return ++mGeneration;
541}
542
Chris Ye98d3f532020-10-01 21:48:59 -0700543std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000544 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700545 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800546}
547
Chris Ye98d3f532020-10-01 21:48:59 -0700548std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
549 std::vector<InputDeviceInfo> outInputDevices;
550 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551
Chris Yee7310032020-09-22 15:36:28 -0700552 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000554 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555 }
556 }
Chris Ye98d3f532020-10-01 21:48:59 -0700557 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558}
559
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700560int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000561 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562
563 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
564}
565
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700566int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000567 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568
569 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
570}
571
572int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000573 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574
575 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
576}
577
578int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700579 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580 int32_t result = AKEY_STATE_UNKNOWN;
581 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800582 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800583 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
584 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585 }
586 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000587 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000588 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700589 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
591 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000592 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 if (currentResult >= AKEY_STATE_DOWN) {
594 return currentResult;
595 } else if (currentResult == AKEY_STATE_UP) {
596 result = currentResult;
597 }
598 }
599 }
600 }
601 return result;
602}
603
Andrii Kulian763a3a42016-03-08 10:46:16 -0800604void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000605 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800606 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800607 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800608 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
609 return;
610 }
611
Andrii Kulian763a3a42016-03-08 10:46:16 -0800612 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000613 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800614 return;
615 }
616
617 device->updateMetaState(AKEYCODE_CAPS_LOCK);
618}
619
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700620bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
621 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000622 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700624 memset(outFlags, 0, keyCodes.size());
625 return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800626}
627
628bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700629 const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700630 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800631 bool result = false;
632 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800633 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800634 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700635 result = device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800636 }
637 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000638 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000639 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700640 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700641 result |= device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642 }
643 }
644 }
645 return result;
646}
647
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000648void InputReader::addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const {
649 std::scoped_lock _l(mLock);
650
651 InputDevice* device = findInputDeviceLocked(deviceId);
652 if (device != nullptr) {
653 device->addKeyRemapping(fromKeyCode, toKeyCode);
654 }
655}
656
Philip Junker4af3b3d2021-12-14 10:36:55 +0100657int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
658 std::scoped_lock _l(mLock);
659
660 InputDevice* device = findInputDeviceLocked(deviceId);
661 if (device == nullptr) {
662 ALOGW("Failed to get key code for key location: Input device with id %d not found",
663 deviceId);
664 return AKEYCODE_UNKNOWN;
665 }
666 return device->getKeyCodeForKeyLocation(locationKeyCode);
667}
668
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000669void InputReader::requestRefreshConfiguration(ConfigurationChanges changes) {
Chris Ye87143712020-11-10 05:05:58 +0000670 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000672 if (changes.any()) {
673 bool needWake = !mConfigurationChangesToRefresh.any();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674 mConfigurationChangesToRefresh |= changes;
675
676 if (needWake) {
677 mEventHub->wake();
678 }
679 }
680}
681
Chris Ye87143712020-11-10 05:05:58 +0000682void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
683 int32_t token) {
684 std::scoped_lock _l(mLock);
685
Chris Ye1c2e0892020-11-30 21:41:44 -0800686 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800687 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700688 mPendingArgs += device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689 }
690}
691
692void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000693 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694
Chris Ye1c2e0892020-11-30 21:41:44 -0800695 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800696 if (device) {
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700697 mPendingArgs += device->cancelVibrate(token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800698 }
699}
700
Chris Ye87143712020-11-10 05:05:58 +0000701bool InputReader::isVibrating(int32_t deviceId) {
702 std::scoped_lock _l(mLock);
703
704 InputDevice* device = findInputDeviceLocked(deviceId);
705 if (device) {
706 return device->isVibrating();
707 }
708 return false;
709}
710
711std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
712 std::scoped_lock _l(mLock);
713
714 InputDevice* device = findInputDeviceLocked(deviceId);
715 if (device) {
716 return device->getVibratorIds();
717 }
718 return {};
719}
720
Chris Yef59a2f42020-10-16 12:55:26 -0700721void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
722 std::scoped_lock _l(mLock);
723
724 InputDevice* device = findInputDeviceLocked(deviceId);
725 if (device) {
726 device->disableSensor(sensorType);
727 }
728}
729
730bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
731 std::chrono::microseconds samplingPeriod,
732 std::chrono::microseconds maxBatchReportLatency) {
733 std::scoped_lock _l(mLock);
734
735 InputDevice* device = findInputDeviceLocked(deviceId);
736 if (device) {
737 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
738 }
739 return false;
740}
741
742void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
743 std::scoped_lock _l(mLock);
744
745 InputDevice* device = findInputDeviceLocked(deviceId);
746 if (device) {
747 device->flushSensor(sensorType);
748 }
749}
750
Kim Low03ea0352020-11-06 12:45:07 -0800751std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400752 std::optional<int32_t> eventHubId;
753 {
754 // Do not query the battery state while holding the lock. For some peripheral devices,
755 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
756 // would block all other event processing during this time. For now, we assume this
757 // call never happens on the InputReader thread and get the battery state outside the
758 // lock to prevent event processing from being blocked by this call.
759 std::scoped_lock _l(mLock);
760 InputDevice* device = findInputDeviceLocked(deviceId);
761 if (!device) return {};
762 eventHubId = device->getBatteryEventHubId();
763 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800764
Andy Chenf9f1a022022-08-29 20:07:10 -0400765 if (!eventHubId) return {};
766 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000767 if (batteryIds.empty()) {
768 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
769 return {};
770 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400771 return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800772}
773
774std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400775 std::optional<int32_t> eventHubId;
776 {
777 // Do not query the battery state while holding the lock. For some peripheral devices,
778 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
779 // would block all other event processing during this time. For now, we assume this
780 // call never happens on the InputReader thread and get the battery state outside the
781 // lock to prevent event processing from being blocked by this call.
782 std::scoped_lock _l(mLock);
783 InputDevice* device = findInputDeviceLocked(deviceId);
784 if (!device) return {};
785 eventHubId = device->getBatteryEventHubId();
786 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800787
Andy Chenf9f1a022022-08-29 20:07:10 -0400788 if (!eventHubId) return {};
789 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000790 if (batteryIds.empty()) {
791 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
792 return {};
793 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400794 return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800795}
796
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000797std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) {
798 std::scoped_lock _l(mLock);
799
800 InputDevice* device = findInputDeviceLocked(deviceId);
801 if (!device) return {};
802
803 std::optional<int32_t> eventHubId = device->getBatteryEventHubId();
804 if (!eventHubId) return {};
805 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
806 if (batteryIds.empty()) {
807 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
808 return {};
809 }
810 const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front());
811 if (!batteryInfo) {
812 ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__,
813 batteryIds.front(), *eventHubId);
814 return {};
815 }
816 return batteryInfo->path;
817}
818
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000819std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800820 std::scoped_lock _l(mLock);
821
822 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000823 if (device == nullptr) {
824 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800825 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000826
827 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800828}
829
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000830std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800831 std::scoped_lock _l(mLock);
832
833 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000834 if (device == nullptr) {
835 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800836 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000837
838 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800839}
840
841bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
842 std::scoped_lock _l(mLock);
843
844 InputDevice* device = findInputDeviceLocked(deviceId);
845 if (device) {
846 return device->setLightColor(lightId, color);
847 }
848 return false;
849}
850
851bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
852 std::scoped_lock _l(mLock);
853
854 InputDevice* device = findInputDeviceLocked(deviceId);
855 if (device) {
856 return device->setLightPlayerId(lightId, playerId);
857 }
858 return false;
859}
860
861std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
862 std::scoped_lock _l(mLock);
863
864 InputDevice* device = findInputDeviceLocked(deviceId);
865 if (device) {
866 return device->getLightColor(lightId);
867 }
868 return std::nullopt;
869}
870
871std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
872 std::scoped_lock _l(mLock);
873
874 InputDevice* device = findInputDeviceLocked(deviceId);
875 if (device) {
876 return device->getLightPlayerId(lightId);
877 }
878 return std::nullopt;
879}
880
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000881std::optional<std::string> InputReader::getBluetoothAddress(int32_t deviceId) const {
882 std::scoped_lock _l(mLock);
883
884 InputDevice* device = findInputDeviceLocked(deviceId);
885 if (device) {
886 return device->getBluetoothAddress();
887 }
888 return std::nullopt;
889}
890
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700891bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000892 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700893
Chris Ye1c2e0892020-11-30 21:41:44 -0800894 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800895 if (device) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700896 return device->isEnabled();
897 }
898 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
899 return false;
900}
901
Arthur Hungc23540e2018-11-29 20:42:11 +0800902bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000903 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800904
Chris Ye1c2e0892020-11-30 21:41:44 -0800905 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800906 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800907 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
908 return false;
909 }
910
Arthur Hung2c9a3342019-07-23 14:18:59 +0800911 if (!device->isEnabled()) {
912 ALOGW("Ignoring disabled device %s", device->getName().c_str());
913 return false;
914 }
915
916 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800917 // No associated display. By default, can dispatch to all displays.
Weilun Dud00847d2021-12-08 10:55:58 -0800918 if (!associatedDisplayId ||
919 *associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800920 return true;
921 }
922
923 return *associatedDisplayId == displayId;
924}
925
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000926void InputReader::sysfsNodeChanged(const std::string& sysfsNodePath) {
927 mEventHub->sysfsNodeChanged(sysfsNodePath);
928}
929
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800930void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000931 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932
933 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800934 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935
Chris Yee7310032020-09-22 15:36:28 -0700936 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
937 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938
Chris Yee7310032020-09-22 15:36:28 -0700939 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
940 const std::shared_ptr<InputDevice>& device = devicePair.first;
941 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
942 for (const auto& eId : devicePair.second) {
943 eventHubDevStr += StringPrintf("%d ", eId);
944 }
945 eventHubDevStr += "] \n";
946 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 }
948
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800949 dump += INDENT "Configuration:\n";
950 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
952 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800953 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100955 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800956 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800957 dump += "]\n";
958 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700959 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800961 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700962 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
963 "acceleration=%0.3f\n",
964 mConfig.pointerVelocityControlParameters.scale,
965 mConfig.pointerVelocityControlParameters.lowThreshold,
966 mConfig.pointerVelocityControlParameters.highThreshold,
967 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800969 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700970 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
971 "acceleration=%0.3f\n",
972 mConfig.wheelVelocityControlParameters.scale,
973 mConfig.wheelVelocityControlParameters.lowThreshold,
974 mConfig.wheelVelocityControlParameters.highThreshold,
975 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800977 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700978 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800979 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700980 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800981 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700982 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800983 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700984 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800985 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700986 mConfig.pointerGestureTapDragInterval * 0.000001f);
987 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800988 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700989 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800990 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700991 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800992 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700993 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800994 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700995 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800996 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700997 mConfig.pointerGestureMovementSpeedRatio);
998 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700999
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001000 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -07001001 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002}
1003
1004void InputReader::monitor() {
1005 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -08001006 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -08001008 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009 // Check the EventHub
1010 mEventHub->monitor();
1011}
1012
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013// --- InputReader::ContextImpl ---
1014
Garfield Tanff1f1bb2020-01-28 13:24:04 -08001015InputReader::ContextImpl::ContextImpl(InputReader* reader)
1016 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001017
1018void InputReader::ContextImpl::updateGlobalMetaState() {
1019 // lock is already held by the input loop
1020 mReader->updateGlobalMetaStateLocked();
1021}
1022
1023int32_t InputReader::ContextImpl::getGlobalMetaState() {
1024 // lock is already held by the input loop
1025 return mReader->getGlobalMetaStateLocked();
1026}
1027
arthurhungc903df12020-08-11 15:08:42 +08001028void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
1029 // lock is already held by the input loop
1030 mReader->updateLedMetaStateLocked(metaState);
1031}
1032
1033int32_t InputReader::ContextImpl::getLedMetaState() {
1034 // lock is already held by the input loop
1035 return mReader->getLedMetaStateLocked();
1036}
1037
Arpit Singha5ea7c12023-07-05 15:39:25 +00001038void InputReader::ContextImpl::setPreventingTouchpadTaps(bool prevent) {
1039 // lock is already held by the input loop
1040 mReader->mPreventingTouchpadTaps = prevent;
1041}
1042
1043bool InputReader::ContextImpl::isPreventingTouchpadTaps() {
1044 // lock is already held by the input loop
1045 return mReader->mPreventingTouchpadTaps;
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
1059void InputReader::ContextImpl::fadePointer() {
1060 // lock is already held by the input loop
1061 mReader->fadePointerLocked();
1062}
1063
Michael Wright17db18e2020-06-26 20:51:44 +01001064std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController(
1065 int32_t deviceId) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001066 // lock is already held by the input loop
1067 return mReader->getPointerControllerLocked(deviceId);
1068}
1069
Michael Wrightd02c5b62014-02-10 15:10:22 -08001070void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
1071 // lock is already held by the input loop
1072 mReader->requestTimeoutAtTimeLocked(when);
1073}
1074
1075int32_t InputReader::ContextImpl::bumpGeneration() {
1076 // lock is already held by the input loop
1077 return mReader->bumpGenerationLocked();
1078}
1079
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001080void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -07001081 // lock is already held by whatever called refreshConfigurationLocked
1082 mReader->getExternalStylusDevicesLocked(outDevices);
1083}
1084
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001085std::list<NotifyArgs> InputReader::ContextImpl::dispatchExternalStylusState(
1086 const StylusState& state) {
1087 return mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -07001088}
1089
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
1091 return mReader->mPolicy.get();
1092}
1093
Michael Wrightd02c5b62014-02-10 15:10:22 -08001094EventHubInterface* InputReader::ContextImpl::getEventHub() {
1095 return mReader->mEventHub.get();
1096}
1097
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00001098int32_t InputReader::ContextImpl::getNextId() {
1099 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -08001100}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102} // namespace android