blob: 903d072c96fd34e52dc146ad4f6313a8e2a4c393 [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);
69 return motionArgs.pointerProperties[actionIndex].toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
70 motionArgs.pointerProperties[actionIndex].toolType == AMOTION_EVENT_TOOL_TYPE_ERASER;
71}
72
Prabir Pradhan28efc192019-11-05 01:10:04 +000073// --- InputReader ---
74
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070075InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
76 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070077 InputListenerInterface& listener)
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070078 : mContext(this),
79 mEventHub(eventHub),
80 mPolicy(policy),
Siarhei Vishniakou18050092021-09-01 13:32:49 -070081 mQueuedListener(listener),
Arthur Hung95f68612022-04-07 14:08:22 +080082 mGlobalMetaState(AMETA_NONE),
83 mLedMetaState(AMETA_NONE),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070084 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080085 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070086 mDisableVirtualKeysTimeout(LLONG_MIN),
87 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -080088 mConfigurationChangesToRefresh(0) {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070089 refreshConfigurationLocked(0);
90 updateGlobalMetaStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -080091}
92
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +000093InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080094
Prabir Pradhan28efc192019-11-05 01:10:04 +000095status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070096 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000097 return ALREADY_EXISTS;
98 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070099 mThread = std::make_unique<InputThread>(
100 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
101 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000102}
103
104status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700105 if (mThread && mThread->isCallingThread()) {
106 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +0000107 return INVALID_OPERATION;
108 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700109 mThread.reset();
110 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000111}
112
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113void InputReader::loopOnce() {
114 int32_t oldGeneration;
115 int32_t timeoutMillis;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000116 // Copy some state so that we can access it outside the lock later.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -0800118 std::vector<InputDeviceInfo> inputDevices;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000119 std::list<NotifyArgs> notifyArgs;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000121 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800122
123 oldGeneration = mGeneration;
124 timeoutMillis = -1;
125
126 uint32_t changes = mConfigurationChangesToRefresh;
127 if (changes) {
128 mConfigurationChangesToRefresh = 0;
129 timeoutMillis = 0;
130 refreshConfigurationLocked(changes);
131 } else if (mNextTimeout != LLONG_MAX) {
132 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
133 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
134 }
135 } // release lock
136
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700137 std::vector<RawEvent> events = mEventHub->getEvents(timeoutMillis);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138
139 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000140 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800141 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800142
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700143 if (!events.empty()) {
Prabir Pradhanda20b172022-09-26 17:01:18 +0000144 notifyArgs += processEventsLocked(events.data(), events.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 }
146
147 if (mNextTimeout != LLONG_MAX) {
148 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
149 if (now >= mNextTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800150 if (DEBUG_RAW_EVENTS) {
151 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
152 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153 mNextTimeout = LLONG_MAX;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000154 notifyArgs += timeoutExpiredLocked(now);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155 }
156 }
157
158 if (oldGeneration != mGeneration) {
159 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800160 inputDevices = getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 }
162 } // release lock
163
164 // Send out a message that the describes the changed input devices.
165 if (inputDevicesChanged) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800166 mPolicy->notifyInputDevicesChanged(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167 }
168
Prabir Pradhanda20b172022-09-26 17:01:18 +0000169 // Notify the policy of the start of every new stylus gesture outside the lock.
170 for (const auto& args : notifyArgs) {
171 const auto* motionArgs = std::get_if<NotifyMotionArgs>(&args);
172 if (motionArgs != nullptr && isStylusPointerGestureStart(*motionArgs)) {
173 mPolicy->notifyStylusGestureStarted(motionArgs->deviceId, motionArgs->eventTime);
174 }
175 }
176
177 notifyAll(std::move(notifyArgs));
178
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 // Flush queued events out to the listener.
180 // This must happen outside of the lock because the listener could potentially call
181 // back into the InputReader's methods, such as getScanCodeState, or become blocked
182 // on another thread similarly waiting to acquire the InputReader lock thereby
183 // resulting in a deadlock. This situation is actually quite plausible because the
184 // listener is actually the input dispatcher, which calls into the window manager,
185 // which occasionally calls into the input reader.
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700186 mQueuedListener.flush();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187}
188
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700189std::list<NotifyArgs> InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
190 std::list<NotifyArgs> out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191 for (const RawEvent* rawEvent = rawEvents; count;) {
192 int32_t type = rawEvent->type;
193 size_t batchSize = 1;
194 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
195 int32_t deviceId = rawEvent->deviceId;
196 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700197 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
198 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199 break;
200 }
201 batchSize += 1;
202 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800203 if (DEBUG_RAW_EVENTS) {
204 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
205 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700206 out += processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 } else {
208 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700209 case EventHubInterface::DEVICE_ADDED:
210 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
211 break;
212 case EventHubInterface::DEVICE_REMOVED:
213 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
214 break;
215 case EventHubInterface::FINISHED_DEVICE_SCAN:
216 handleConfigurationChangedLocked(rawEvent->when);
217 break;
218 default:
219 ALOG_ASSERT(false); // can't happen
220 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800221 }
222 }
223 count -= batchSize;
224 rawEvent += batchSize;
225 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700226 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227}
228
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800229void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
230 if (mDevices.find(eventHubId) != mDevices.end()) {
231 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232 return;
233 }
234
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800235 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
236 std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700237
238 notifyAll(device->configure(when, &mConfig, 0));
239 notifyAll(device->reset(when));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240
241 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800242 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
243 "(ignored non-input device)",
244 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000246 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800247 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000248 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 }
250
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800251 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700252 // Add device to device to EventHub ids map.
253 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
254 if (mapIt == mDeviceToEventHubIdsMap.end()) {
255 std::vector<int32_t> ids = {eventHubId};
256 mDeviceToEventHubIdsMap.emplace(device, ids);
257 } else {
258 mapIt->second.push_back(eventHubId);
259 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700261
Chris Ye1b0c7342020-07-28 21:57:03 -0700262 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800263 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700264 }
Chris Yef59a2f42020-10-16 12:55:26 -0700265
266 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800267 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
268 // device class to disable SENSOR sub device only.
269 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700270 mEventHub->disableDevice(eventHubId);
271 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272}
273
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800274void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
275 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000276 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800277 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800278 return;
279 }
280
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000281 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000282 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700283 // Erase device from device to EventHub ids map.
284 auto mapIt = mDeviceToEventHubIdsMap.find(device);
285 if (mapIt != mDeviceToEventHubIdsMap.end()) {
286 std::vector<int32_t>& eventHubIds = mapIt->second;
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -0800287 std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
Chris Yee7310032020-09-22 15:36:28 -0700288 if (eventHubIds.size() == 0) {
289 mDeviceToEventHubIdsMap.erase(mapIt);
290 }
291 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292 bumpGenerationLocked();
293
294 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800295 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
296 "(ignored non-input device)",
297 device->getId(), eventHubId, device->getName().c_str(),
298 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000300 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800301 device->getId(), eventHubId, device->getName().c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000302 device->getDescriptor().c_str(),
303 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800304 }
305
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800306 device->removeEventHubDevice(eventHubId);
307
Chris Ye1b0c7342020-07-28 21:57:03 -0700308 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800309 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700310 }
311
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700312 std::list<NotifyArgs> resetEvents;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800313 if (device->hasEventHubDevices()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700314 resetEvents += device->configure(when, &mConfig, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800315 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700316 resetEvents += device->reset(when);
317 notifyAll(std::move(resetEvents));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318}
319
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000320std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800321 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
322 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
Josh Bartel938632f2022-07-19 15:34:22 -0500323 const InputDeviceIdentifier identifier2 =
324 devicePair.second->getDeviceInfo().getIdentifier();
325 return isSubDevice(identifier, identifier2);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800326 });
327
328 std::shared_ptr<InputDevice> device;
329 if (deviceIt != mDevices.end()) {
330 device = deviceIt->second;
331 } else {
332 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
333 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
334 identifier);
335 }
336 device->addEventHubDevice(eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337 return device;
338}
339
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700340std::list<NotifyArgs> InputReader::processEventsForDeviceLocked(int32_t eventHubId,
341 const RawEvent* rawEvents,
342 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800343 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000344 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800345 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700346 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000349 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700351 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700352 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353 }
354
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700355 return device->process(rawEvents, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356}
357
Philip Junker4af3b3d2021-12-14 10:36:55 +0100358InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800359 auto deviceIt =
360 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
361 return devicePair.second->getId() == deviceId;
362 });
363 if (deviceIt != mDevices.end()) {
364 return deviceIt->second.get();
365 }
366 return nullptr;
367}
368
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700369std::list<NotifyArgs> InputReader::timeoutExpiredLocked(nsecs_t when) {
370 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000371 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000372 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373 if (!device->isIgnored()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700374 out += device->timeoutExpired(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375 }
376 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700377 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378}
379
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800380int32_t InputReader::nextInputDeviceIdLocked() {
381 return ++mNextInputDeviceId;
382}
383
Michael Wrightd02c5b62014-02-10 15:10:22 -0800384void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
385 // Reset global meta state because it depends on the list of all configured devices.
386 updateGlobalMetaStateLocked();
387
388 // Enqueue configuration changed.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000389 NotifyConfigurationChangedArgs args(mContext.getNextId(), when);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700390 mQueuedListener.notifyConfigurationChanged(&args);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391}
392
393void InputReader::refreshConfigurationLocked(uint32_t changes) {
394 mPolicy->getReaderConfiguration(&mConfig);
395 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
396
Prabir Pradhan7e186182020-11-10 13:56:45 -0800397 if (!changes) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800398
Prabir Pradhan7e186182020-11-10 13:56:45 -0800399 ALOGI("Reconfiguring input devices, changes=%s",
400 InputReaderConfiguration::changesToString(changes).c_str());
401 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800402
Prabir Pradhan7e186182020-11-10 13:56:45 -0800403 if (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO) {
404 updatePointerDisplayLocked();
405 }
406
407 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
408 mEventHub->requestReopenDevices();
409 } else {
410 for (auto& devicePair : mDevices) {
411 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700412 notifyAll(device->configure(now, &mConfig, changes));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413 }
414 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800415
416 if (changes & InputReaderConfiguration::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;
422 const NotifyPointerCaptureChangedArgs args(mContext.getNextId(), now,
423 mCurrentPointerCaptureRequest);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700424 mQueuedListener.notifyPointerCaptureChanged(&args);
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
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700429void InputReader::notifyAll(std::list<NotifyArgs>&& argsList) {
430 for (const NotifyArgs& args : argsList) {
431 mQueuedListener.notify(args);
432 }
433}
434
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435void InputReader::updateGlobalMetaStateLocked() {
436 mGlobalMetaState = 0;
437
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000438 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000439 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440 mGlobalMetaState |= device->getMetaState();
441 }
442}
443
444int32_t InputReader::getGlobalMetaStateLocked() {
445 return mGlobalMetaState;
446}
447
arthurhungc903df12020-08-11 15:08:42 +0800448void InputReader::updateLedMetaStateLocked(int32_t metaState) {
449 mLedMetaState = metaState;
450 for (auto& devicePair : mDevices) {
451 std::shared_ptr<InputDevice>& device = devicePair.second;
452 device->updateLedState(false);
453 }
454}
455
456int32_t InputReader::getLedMetaStateLocked() {
457 return mLedMetaState;
458}
459
Chris Ye1c2e0892020-11-30 21:41:44 -0800460void InputReader::notifyExternalStylusPresenceChangedLocked() {
Michael Wright842500e2015-03-13 17:32:02 -0700461 refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
462}
463
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800464void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000465 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000466 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700467 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000468 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700469 }
470 }
471}
472
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700473std::list<NotifyArgs> InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
474 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000475 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000476 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700477 out += device->updateExternalStylusState(state);
Michael Wright842500e2015-03-13 17:32:02 -0700478 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700479 return out;
Michael Wright842500e2015-03-13 17:32:02 -0700480}
481
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
483 mDisableVirtualKeysTimeout = time;
484}
485
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800486bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800487 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800488 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700489 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800490 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491 return true;
492 } else {
493 return false;
494 }
495}
496
Michael Wright17db18e2020-06-26 20:51:44 +0100497std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked(
498 int32_t deviceId) {
499 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800500 if (controller == nullptr) {
501 controller = mPolicy->obtainPointerController(deviceId);
502 mPointerController = controller;
503 updatePointerDisplayLocked();
504 }
505 return controller;
506}
507
508void InputReader::updatePointerDisplayLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100509 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800510 if (controller == nullptr) {
511 return;
512 }
513
514 std::optional<DisplayViewport> viewport =
515 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
516 if (!viewport) {
517 ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input "
518 "mapper. Fall back to default display",
519 mConfig.defaultPointerDisplayId);
520 viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT);
521 }
522 if (!viewport) {
523 ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to"
524 " PointerController.");
525 return;
526 }
527
528 controller->setDisplayViewport(*viewport);
529}
530
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531void InputReader::fadePointerLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100532 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800533 if (controller != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +0100534 controller->fade(PointerControllerInterface::Transition::GRADUAL);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 }
536}
537
538void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
539 if (when < mNextTimeout) {
540 mNextTimeout = when;
541 mEventHub->wake();
542 }
543}
544
545int32_t InputReader::bumpGenerationLocked() {
546 return ++mGeneration;
547}
548
Chris Ye98d3f532020-10-01 21:48:59 -0700549std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000550 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700551 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800552}
553
Chris Ye98d3f532020-10-01 21:48:59 -0700554std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
555 std::vector<InputDeviceInfo> outInputDevices;
556 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557
Chris Yee7310032020-09-22 15:36:28 -0700558 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000560 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800561 }
562 }
Chris Ye98d3f532020-10-01 21:48:59 -0700563 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800564}
565
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700566int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
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, keyCode, &InputDevice::getKeyCodeState);
570}
571
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700572int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
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, scanCode, &InputDevice::getScanCodeState);
576}
577
578int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000579 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580
581 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
582}
583
584int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700585 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586 int32_t result = AKEY_STATE_UNKNOWN;
587 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800588 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800589 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
590 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 }
592 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000593 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000594 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700595 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
597 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000598 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599 if (currentResult >= AKEY_STATE_DOWN) {
600 return currentResult;
601 } else if (currentResult == AKEY_STATE_UP) {
602 result = currentResult;
603 }
604 }
605 }
606 }
607 return result;
608}
609
Andrii Kulian763a3a42016-03-08 10:46:16 -0800610void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000611 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800612 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800613 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800614 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
615 return;
616 }
617
Andrii Kulian763a3a42016-03-08 10:46:16 -0800618 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000619 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800620 return;
621 }
622
623 device->updateMetaState(AKEYCODE_CAPS_LOCK);
624}
625
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700626bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
627 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000628 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700630 memset(outFlags, 0, keyCodes.size());
631 return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632}
633
634bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700635 const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700636 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 bool result = false;
638 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800639 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800640 if (device && !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 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000644 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000645 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700646 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700647 result |= device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 }
649 }
650 }
651 return result;
652}
653
Philip Junker4af3b3d2021-12-14 10:36:55 +0100654int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
655 std::scoped_lock _l(mLock);
656
657 InputDevice* device = findInputDeviceLocked(deviceId);
658 if (device == nullptr) {
659 ALOGW("Failed to get key code for key location: Input device with id %d not found",
660 deviceId);
661 return AKEYCODE_UNKNOWN;
662 }
663 return device->getKeyCodeForKeyLocation(locationKeyCode);
664}
665
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666void InputReader::requestRefreshConfiguration(uint32_t changes) {
Chris Ye87143712020-11-10 05:05:58 +0000667 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668
669 if (changes) {
670 bool needWake = !mConfigurationChangesToRefresh;
671 mConfigurationChangesToRefresh |= changes;
672
673 if (needWake) {
674 mEventHub->wake();
675 }
676 }
677}
678
Chris Ye87143712020-11-10 05:05:58 +0000679void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
680 int32_t token) {
681 std::scoped_lock _l(mLock);
682
Chris Ye1c2e0892020-11-30 21:41:44 -0800683 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800684 if (device) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700685 notifyAll(device->vibrate(sequence, repeat, token));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 }
687}
688
689void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000690 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691
Chris Ye1c2e0892020-11-30 21:41:44 -0800692 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800693 if (device) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700694 notifyAll(device->cancelVibrate(token));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800695 }
696}
697
Chris Ye87143712020-11-10 05:05:58 +0000698bool InputReader::isVibrating(int32_t deviceId) {
699 std::scoped_lock _l(mLock);
700
701 InputDevice* device = findInputDeviceLocked(deviceId);
702 if (device) {
703 return device->isVibrating();
704 }
705 return false;
706}
707
708std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
709 std::scoped_lock _l(mLock);
710
711 InputDevice* device = findInputDeviceLocked(deviceId);
712 if (device) {
713 return device->getVibratorIds();
714 }
715 return {};
716}
717
Chris Yef59a2f42020-10-16 12:55:26 -0700718void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
719 std::scoped_lock _l(mLock);
720
721 InputDevice* device = findInputDeviceLocked(deviceId);
722 if (device) {
723 device->disableSensor(sensorType);
724 }
725}
726
727bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
728 std::chrono::microseconds samplingPeriod,
729 std::chrono::microseconds maxBatchReportLatency) {
730 std::scoped_lock _l(mLock);
731
732 InputDevice* device = findInputDeviceLocked(deviceId);
733 if (device) {
734 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
735 }
736 return false;
737}
738
739void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
740 std::scoped_lock _l(mLock);
741
742 InputDevice* device = findInputDeviceLocked(deviceId);
743 if (device) {
744 device->flushSensor(sensorType);
745 }
746}
747
Kim Low03ea0352020-11-06 12:45:07 -0800748std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400749 std::optional<int32_t> eventHubId;
750 {
751 // Do not query the battery state while holding the lock. For some peripheral devices,
752 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
753 // would block all other event processing during this time. For now, we assume this
754 // call never happens on the InputReader thread and get the battery state outside the
755 // lock to prevent event processing from being blocked by this call.
756 std::scoped_lock _l(mLock);
757 InputDevice* device = findInputDeviceLocked(deviceId);
758 if (!device) return {};
759 eventHubId = device->getBatteryEventHubId();
760 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800761
Andy Chenf9f1a022022-08-29 20:07:10 -0400762 if (!eventHubId) return {};
763 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000764 if (batteryIds.empty()) {
765 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
766 return {};
767 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400768 return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800769}
770
771std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400772 std::optional<int32_t> eventHubId;
773 {
774 // Do not query the battery state while holding the lock. For some peripheral devices,
775 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
776 // would block all other event processing during this time. For now, we assume this
777 // call never happens on the InputReader thread and get the battery state outside the
778 // lock to prevent event processing from being blocked by this call.
779 std::scoped_lock _l(mLock);
780 InputDevice* device = findInputDeviceLocked(deviceId);
781 if (!device) return {};
782 eventHubId = device->getBatteryEventHubId();
783 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800784
Andy Chenf9f1a022022-08-29 20:07:10 -0400785 if (!eventHubId) return {};
786 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000787 if (batteryIds.empty()) {
788 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
789 return {};
790 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400791 return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800792}
793
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000794std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) {
795 std::scoped_lock _l(mLock);
796
797 InputDevice* device = findInputDeviceLocked(deviceId);
798 if (!device) return {};
799
800 std::optional<int32_t> eventHubId = device->getBatteryEventHubId();
801 if (!eventHubId) return {};
802 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
803 if (batteryIds.empty()) {
804 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
805 return {};
806 }
807 const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front());
808 if (!batteryInfo) {
809 ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__,
810 batteryIds.front(), *eventHubId);
811 return {};
812 }
813 return batteryInfo->path;
814}
815
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000816std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800817 std::scoped_lock _l(mLock);
818
819 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000820 if (device == nullptr) {
821 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800822 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000823
824 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800825}
826
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000827std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800828 std::scoped_lock _l(mLock);
829
830 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000831 if (device == nullptr) {
832 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800833 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000834
835 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800836}
837
838bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
839 std::scoped_lock _l(mLock);
840
841 InputDevice* device = findInputDeviceLocked(deviceId);
842 if (device) {
843 return device->setLightColor(lightId, color);
844 }
845 return false;
846}
847
848bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
849 std::scoped_lock _l(mLock);
850
851 InputDevice* device = findInputDeviceLocked(deviceId);
852 if (device) {
853 return device->setLightPlayerId(lightId, playerId);
854 }
855 return false;
856}
857
858std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
859 std::scoped_lock _l(mLock);
860
861 InputDevice* device = findInputDeviceLocked(deviceId);
862 if (device) {
863 return device->getLightColor(lightId);
864 }
865 return std::nullopt;
866}
867
868std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
869 std::scoped_lock _l(mLock);
870
871 InputDevice* device = findInputDeviceLocked(deviceId);
872 if (device) {
873 return device->getLightPlayerId(lightId);
874 }
875 return std::nullopt;
876}
877
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700878bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000879 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700880
Chris Ye1c2e0892020-11-30 21:41:44 -0800881 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800882 if (device) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700883 return device->isEnabled();
884 }
885 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
886 return false;
887}
888
Arthur Hungc23540e2018-11-29 20:42:11 +0800889bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000890 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800891
Chris Ye1c2e0892020-11-30 21:41:44 -0800892 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800893 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800894 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
895 return false;
896 }
897
Arthur Hung2c9a3342019-07-23 14:18:59 +0800898 if (!device->isEnabled()) {
899 ALOGW("Ignoring disabled device %s", device->getName().c_str());
900 return false;
901 }
902
903 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800904 // No associated display. By default, can dispatch to all displays.
Weilun Dud00847d2021-12-08 10:55:58 -0800905 if (!associatedDisplayId ||
906 *associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800907 return true;
908 }
909
910 return *associatedDisplayId == displayId;
911}
912
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800913void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000914 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915
916 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800917 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918
Chris Yee7310032020-09-22 15:36:28 -0700919 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
920 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921
Chris Yee7310032020-09-22 15:36:28 -0700922 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
923 const std::shared_ptr<InputDevice>& device = devicePair.first;
924 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
925 for (const auto& eId : devicePair.second) {
926 eventHubDevStr += StringPrintf("%d ", eId);
927 }
928 eventHubDevStr += "] \n";
929 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930 }
931
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800932 dump += INDENT "Configuration:\n";
933 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
935 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800936 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100938 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800940 dump += "]\n";
941 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700942 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800944 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700945 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
946 "acceleration=%0.3f\n",
947 mConfig.pointerVelocityControlParameters.scale,
948 mConfig.pointerVelocityControlParameters.lowThreshold,
949 mConfig.pointerVelocityControlParameters.highThreshold,
950 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800952 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700953 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
954 "acceleration=%0.3f\n",
955 mConfig.wheelVelocityControlParameters.scale,
956 mConfig.wheelVelocityControlParameters.lowThreshold,
957 mConfig.wheelVelocityControlParameters.highThreshold,
958 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800960 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700961 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800962 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700963 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800964 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700965 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800966 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700967 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800968 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700969 mConfig.pointerGestureTapDragInterval * 0.000001f);
970 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800971 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700972 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800973 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700974 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800975 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700976 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800977 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700978 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800979 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700980 mConfig.pointerGestureMovementSpeedRatio);
981 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700982
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800983 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700984 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985}
986
987void InputReader::monitor() {
988 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800989 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800991 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800992 // Check the EventHub
993 mEventHub->monitor();
994}
995
Michael Wrightd02c5b62014-02-10 15:10:22 -0800996// --- InputReader::ContextImpl ---
997
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800998InputReader::ContextImpl::ContextImpl(InputReader* reader)
999 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000
1001void InputReader::ContextImpl::updateGlobalMetaState() {
1002 // lock is already held by the input loop
1003 mReader->updateGlobalMetaStateLocked();
1004}
1005
1006int32_t InputReader::ContextImpl::getGlobalMetaState() {
1007 // lock is already held by the input loop
1008 return mReader->getGlobalMetaStateLocked();
1009}
1010
arthurhungc903df12020-08-11 15:08:42 +08001011void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
1012 // lock is already held by the input loop
1013 mReader->updateLedMetaStateLocked(metaState);
1014}
1015
1016int32_t InputReader::ContextImpl::getLedMetaState() {
1017 // lock is already held by the input loop
1018 return mReader->getLedMetaStateLocked();
1019}
1020
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
1022 // lock is already held by the input loop
1023 mReader->disableVirtualKeysUntilLocked(time);
1024}
1025
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001026bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
1027 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001028 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001029 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001030}
1031
1032void InputReader::ContextImpl::fadePointer() {
1033 // lock is already held by the input loop
1034 mReader->fadePointerLocked();
1035}
1036
Michael Wright17db18e2020-06-26 20:51:44 +01001037std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController(
1038 int32_t deviceId) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001039 // lock is already held by the input loop
1040 return mReader->getPointerControllerLocked(deviceId);
1041}
1042
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
1044 // lock is already held by the input loop
1045 mReader->requestTimeoutAtTimeLocked(when);
1046}
1047
1048int32_t InputReader::ContextImpl::bumpGeneration() {
1049 // lock is already held by the input loop
1050 return mReader->bumpGenerationLocked();
1051}
1052
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001053void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -07001054 // lock is already held by whatever called refreshConfigurationLocked
1055 mReader->getExternalStylusDevicesLocked(outDevices);
1056}
1057
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001058std::list<NotifyArgs> InputReader::ContextImpl::dispatchExternalStylusState(
1059 const StylusState& state) {
1060 return mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -07001061}
1062
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
1064 return mReader->mPolicy.get();
1065}
1066
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067EventHubInterface* InputReader::ContextImpl::getEventHub() {
1068 return mReader->mEventHub.get();
1069}
1070
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00001071int32_t InputReader::ContextImpl::getNextId() {
1072 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -08001073}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075} // namespace android