blob: fc398a2513f8619b53059f28426f63d1c63021fa [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 Pradhan28efc192019-11-05 01:10:04 +000061// --- InputReader ---
62
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070063InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
64 const sp<InputReaderPolicyInterface>& policy,
65 const sp<InputListenerInterface>& listener)
66 : mContext(this),
67 mEventHub(eventHub),
68 mPolicy(policy),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070069 mGlobalMetaState(0),
arthurhungc903df12020-08-11 15:08:42 +080070 mLedMetaState(AMETA_NUM_LOCK_ON),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070071 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080072 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070073 mDisableVirtualKeysTimeout(LLONG_MIN),
74 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -080075 mConfigurationChangesToRefresh(0) {
76 mQueuedListener = new QueuedInputListener(listener);
77
78 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +000079 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -080080
81 refreshConfigurationLocked(0);
82 updateGlobalMetaStateLocked();
83 } // release lock
84}
85
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +000086InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080087
Prabir Pradhan28efc192019-11-05 01:10:04 +000088status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070089 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000090 return ALREADY_EXISTS;
91 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070092 mThread = std::make_unique<InputThread>(
93 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
94 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +000095}
96
97status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070098 if (mThread && mThread->isCallingThread()) {
99 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +0000100 return INVALID_OPERATION;
101 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700102 mThread.reset();
103 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000104}
105
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106void InputReader::loopOnce() {
107 int32_t oldGeneration;
108 int32_t timeoutMillis;
109 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -0800110 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000112 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113
114 oldGeneration = mGeneration;
115 timeoutMillis = -1;
116
117 uint32_t changes = mConfigurationChangesToRefresh;
118 if (changes) {
119 mConfigurationChangesToRefresh = 0;
120 timeoutMillis = 0;
121 refreshConfigurationLocked(changes);
122 } else if (mNextTimeout != LLONG_MAX) {
123 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
124 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
125 }
126 } // release lock
127
128 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
129
130 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000131 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800132 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800133
134 if (count) {
135 processEventsLocked(mEventBuffer, count);
136 }
137
138 if (mNextTimeout != LLONG_MAX) {
139 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
140 if (now >= mNextTimeout) {
141#if DEBUG_RAW_EVENTS
142 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
143#endif
144 mNextTimeout = LLONG_MAX;
145 timeoutExpiredLocked(now);
146 }
147 }
148
149 if (oldGeneration != mGeneration) {
150 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800151 inputDevices = getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152 }
153 } // release lock
154
155 // Send out a message that the describes the changed input devices.
156 if (inputDevicesChanged) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800157 mPolicy->notifyInputDevicesChanged(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158 }
159
160 // Flush queued events out to the listener.
161 // This must happen outside of the lock because the listener could potentially call
162 // back into the InputReader's methods, such as getScanCodeState, or become blocked
163 // on another thread similarly waiting to acquire the InputReader lock thereby
164 // resulting in a deadlock. This situation is actually quite plausible because the
165 // listener is actually the input dispatcher, which calls into the window manager,
166 // which occasionally calls into the input reader.
167 mQueuedListener->flush();
168}
169
170void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
171 for (const RawEvent* rawEvent = rawEvents; count;) {
172 int32_t type = rawEvent->type;
173 size_t batchSize = 1;
174 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
175 int32_t deviceId = rawEvent->deviceId;
176 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700177 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
178 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 break;
180 }
181 batchSize += 1;
182 }
183#if DEBUG_RAW_EVENTS
Siarhei Vishniakou5d83f602017-09-12 12:40:29 -0700184 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185#endif
186 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
187 } else {
188 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700189 case EventHubInterface::DEVICE_ADDED:
190 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
191 break;
192 case EventHubInterface::DEVICE_REMOVED:
193 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
194 break;
195 case EventHubInterface::FINISHED_DEVICE_SCAN:
196 handleConfigurationChangedLocked(rawEvent->when);
197 break;
198 default:
199 ALOG_ASSERT(false); // can't happen
200 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800201 }
202 }
203 count -= batchSize;
204 rawEvent += batchSize;
205 }
206}
207
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800208void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
209 if (mDevices.find(eventHubId) != mDevices.end()) {
210 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211 return;
212 }
213
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800214 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
215 std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216 device->configure(when, &mConfig, 0);
217 device->reset(when);
218
219 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800220 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
221 "(ignored non-input device)",
222 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800224 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=0x%08x",
225 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700226 device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227 }
228
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800229 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700230 // Add device to device to EventHub ids map.
231 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
232 if (mapIt == mDeviceToEventHubIdsMap.end()) {
233 std::vector<int32_t> ids = {eventHubId};
234 mDeviceToEventHubIdsMap.emplace(device, ids);
235 } else {
236 mapIt->second.push_back(eventHubId);
237 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700239
Chris Ye1b0c7342020-07-28 21:57:03 -0700240 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800241 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700242 }
Chris Yef59a2f42020-10-16 12:55:26 -0700243
244 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800245 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
246 // device class to disable SENSOR sub device only.
247 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700248 mEventHub->disableDevice(eventHubId);
249 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250}
251
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800252void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
253 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000254 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800255 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256 return;
257 }
258
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000259 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000260 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700261 // Erase device from device to EventHub ids map.
262 auto mapIt = mDeviceToEventHubIdsMap.find(device);
263 if (mapIt != mDeviceToEventHubIdsMap.end()) {
264 std::vector<int32_t>& eventHubIds = mapIt->second;
265 eventHubIds.erase(std::remove_if(eventHubIds.begin(), eventHubIds.end(),
266 [eventHubId](int32_t eId) { return eId == eventHubId; }),
267 eventHubIds.end());
268 if (eventHubIds.size() == 0) {
269 mDeviceToEventHubIdsMap.erase(mapIt);
270 }
271 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272 bumpGenerationLocked();
273
274 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800275 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
276 "(ignored non-input device)",
277 device->getId(), eventHubId, device->getName().c_str(),
278 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800280 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=0x%08x",
281 device->getId(), eventHubId, device->getName().c_str(),
282 device->getDescriptor().c_str(), device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 }
284
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800285 device->removeEventHubDevice(eventHubId);
286
Chris Ye1b0c7342020-07-28 21:57:03 -0700287 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800288 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700289 }
290
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800291 if (device->hasEventHubDevices()) {
292 device->configure(when, &mConfig, 0);
293 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294 device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295}
296
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000297std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800298 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
299 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
Josh Bartel938632f2022-07-19 15:34:22 -0500300 const InputDeviceIdentifier identifier2 =
301 devicePair.second->getDeviceInfo().getIdentifier();
302 return isSubDevice(identifier, identifier2);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800303 });
304
305 std::shared_ptr<InputDevice> device;
306 if (deviceIt != mDevices.end()) {
307 device = deviceIt->second;
308 } else {
309 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
310 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
311 identifier);
312 }
313 device->addEventHubDevice(eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 return device;
315}
316
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800317void InputReader::processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700318 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800319 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000320 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800321 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 return;
323 }
324
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000325 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700327 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 return;
329 }
330
331 device->process(rawEvents, count);
332}
333
Chris Ye1c2e0892020-11-30 21:41:44 -0800334InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800335 auto deviceIt =
336 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
337 return devicePair.second->getId() == deviceId;
338 });
339 if (deviceIt != mDevices.end()) {
340 return deviceIt->second.get();
341 }
342 return nullptr;
343}
344
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345void InputReader::timeoutExpiredLocked(nsecs_t when) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000346 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000347 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348 if (!device->isIgnored()) {
349 device->timeoutExpired(when);
350 }
351 }
352}
353
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800354int32_t InputReader::nextInputDeviceIdLocked() {
355 return ++mNextInputDeviceId;
356}
357
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
359 // Reset global meta state because it depends on the list of all configured devices.
360 updateGlobalMetaStateLocked();
361
362 // Enqueue configuration changed.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000363 NotifyConfigurationChangedArgs args(mContext.getNextId(), when);
364 mQueuedListener->notifyConfigurationChanged(&args);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365}
366
367void InputReader::refreshConfigurationLocked(uint32_t changes) {
368 mPolicy->getReaderConfiguration(&mConfig);
369 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
370
Prabir Pradhan7e186182020-11-10 13:56:45 -0800371 if (!changes) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372
Prabir Pradhan7e186182020-11-10 13:56:45 -0800373 ALOGI("Reconfiguring input devices, changes=%s",
374 InputReaderConfiguration::changesToString(changes).c_str());
375 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800376
Prabir Pradhan7e186182020-11-10 13:56:45 -0800377 if (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO) {
378 updatePointerDisplayLocked();
379 }
380
381 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
382 mEventHub->requestReopenDevices();
383 } else {
384 for (auto& devicePair : mDevices) {
385 std::shared_ptr<InputDevice>& device = devicePair.second;
386 device->configure(now, &mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 }
388 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800389
390 if (changes & InputReaderConfiguration::CHANGE_POINTER_CAPTURE) {
Prabir Pradhanf192a102021-08-06 14:01:18 +0000391 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
392 ALOGV("Skipping notifying pointer capture changes: "
393 "There was no change in the pointer capture state.");
394 } else {
395 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
396 const NotifyPointerCaptureChangedArgs args(mContext.getNextId(), now,
397 mCurrentPointerCaptureRequest);
398 mQueuedListener->notifyPointerCaptureChanged(&args);
399 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800400 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401}
402
403void InputReader::updateGlobalMetaStateLocked() {
404 mGlobalMetaState = 0;
405
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000406 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000407 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 mGlobalMetaState |= device->getMetaState();
409 }
410}
411
412int32_t InputReader::getGlobalMetaStateLocked() {
413 return mGlobalMetaState;
414}
415
arthurhungc903df12020-08-11 15:08:42 +0800416void InputReader::updateLedMetaStateLocked(int32_t metaState) {
417 mLedMetaState = metaState;
418 for (auto& devicePair : mDevices) {
419 std::shared_ptr<InputDevice>& device = devicePair.second;
420 device->updateLedState(false);
421 }
422}
423
424int32_t InputReader::getLedMetaStateLocked() {
425 return mLedMetaState;
426}
427
Chris Ye1c2e0892020-11-30 21:41:44 -0800428void InputReader::notifyExternalStylusPresenceChangedLocked() {
Michael Wright842500e2015-03-13 17:32:02 -0700429 refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
430}
431
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800432void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000433 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000434 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700435 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000436 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700437 }
438 }
439}
440
Arthur Hungcadce9d2021-05-07 17:24:04 +0800441void InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000442 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000443 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wright842500e2015-03-13 17:32:02 -0700444 device->updateExternalStylusState(state);
445 }
446}
447
Michael Wrightd02c5b62014-02-10 15:10:22 -0800448void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
449 mDisableVirtualKeysTimeout = time;
450}
451
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800452bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800454 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700455 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800456 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457 return true;
458 } else {
459 return false;
460 }
461}
462
Michael Wright17db18e2020-06-26 20:51:44 +0100463std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked(
464 int32_t deviceId) {
465 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800466 if (controller == nullptr) {
467 controller = mPolicy->obtainPointerController(deviceId);
468 mPointerController = controller;
469 updatePointerDisplayLocked();
470 }
471 return controller;
472}
473
474void InputReader::updatePointerDisplayLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100475 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800476 if (controller == nullptr) {
477 return;
478 }
479
480 std::optional<DisplayViewport> viewport =
481 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
482 if (!viewport) {
483 ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input "
484 "mapper. Fall back to default display",
485 mConfig.defaultPointerDisplayId);
486 viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT);
487 }
488 if (!viewport) {
489 ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to"
490 " PointerController.");
491 return;
492 }
493
494 controller->setDisplayViewport(*viewport);
495}
496
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497void InputReader::fadePointerLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100498 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800499 if (controller != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +0100500 controller->fade(PointerControllerInterface::Transition::GRADUAL);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 }
502}
503
504void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
505 if (when < mNextTimeout) {
506 mNextTimeout = when;
507 mEventHub->wake();
508 }
509}
510
511int32_t InputReader::bumpGenerationLocked() {
512 return ++mGeneration;
513}
514
Chris Ye98d3f532020-10-01 21:48:59 -0700515std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000516 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700517 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518}
519
Chris Ye98d3f532020-10-01 21:48:59 -0700520std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
521 std::vector<InputDeviceInfo> outInputDevices;
522 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800523
Chris Yee7310032020-09-22 15:36:28 -0700524 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000526 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800527 }
528 }
Chris Ye98d3f532020-10-01 21:48:59 -0700529 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800530}
531
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700532int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000533 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534
535 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
536}
537
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700538int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000539 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540
541 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
542}
543
544int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000545 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800546
547 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
548}
549
550int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700551 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800552 int32_t result = AKEY_STATE_UNKNOWN;
553 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800554 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800555 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
556 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 }
558 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000559 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000560 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700561 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
563 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000564 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565 if (currentResult >= AKEY_STATE_DOWN) {
566 return currentResult;
567 } else if (currentResult == AKEY_STATE_UP) {
568 result = currentResult;
569 }
570 }
571 }
572 }
573 return result;
574}
575
Andrii Kulian763a3a42016-03-08 10:46:16 -0800576void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000577 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800578 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800579 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800580 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
581 return;
582 }
583
Andrii Kulian763a3a42016-03-08 10:46:16 -0800584 if (device->isIgnored()) {
585 return;
586 }
587
588 device->updateMetaState(AKEYCODE_CAPS_LOCK);
589}
590
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700591bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
592 const int32_t* keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000593 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594
595 memset(outFlags, 0, numCodes);
596 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
597}
598
599bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700600 size_t numCodes, const int32_t* keyCodes,
601 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 bool result = false;
603 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800604 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800605 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
606 result = device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800607 }
608 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000609 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000610 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700611 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
612 result |= device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613 }
614 }
615 }
616 return result;
617}
618
619void InputReader::requestRefreshConfiguration(uint32_t changes) {
Chris Ye87143712020-11-10 05:05:58 +0000620 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621
622 if (changes) {
623 bool needWake = !mConfigurationChangesToRefresh;
624 mConfigurationChangesToRefresh |= changes;
625
626 if (needWake) {
627 mEventHub->wake();
628 }
629 }
630}
631
Chris Ye87143712020-11-10 05:05:58 +0000632void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
633 int32_t token) {
634 std::scoped_lock _l(mLock);
635
Chris Ye1c2e0892020-11-30 21:41:44 -0800636 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800637 if (device) {
Chris Ye87143712020-11-10 05:05:58 +0000638 device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800639 }
640}
641
642void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000643 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644
Chris Ye1c2e0892020-11-30 21:41:44 -0800645 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800646 if (device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800647 device->cancelVibrate(token);
648 }
649}
650
Chris Ye87143712020-11-10 05:05:58 +0000651bool InputReader::isVibrating(int32_t deviceId) {
652 std::scoped_lock _l(mLock);
653
654 InputDevice* device = findInputDeviceLocked(deviceId);
655 if (device) {
656 return device->isVibrating();
657 }
658 return false;
659}
660
661std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
662 std::scoped_lock _l(mLock);
663
664 InputDevice* device = findInputDeviceLocked(deviceId);
665 if (device) {
666 return device->getVibratorIds();
667 }
668 return {};
669}
670
Chris Yef59a2f42020-10-16 12:55:26 -0700671void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
672 std::scoped_lock _l(mLock);
673
674 InputDevice* device = findInputDeviceLocked(deviceId);
675 if (device) {
676 device->disableSensor(sensorType);
677 }
678}
679
680bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
681 std::chrono::microseconds samplingPeriod,
682 std::chrono::microseconds maxBatchReportLatency) {
683 std::scoped_lock _l(mLock);
684
685 InputDevice* device = findInputDeviceLocked(deviceId);
686 if (device) {
687 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
688 }
689 return false;
690}
691
692void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
693 std::scoped_lock _l(mLock);
694
695 InputDevice* device = findInputDeviceLocked(deviceId);
696 if (device) {
697 device->flushSensor(sensorType);
698 }
699}
700
Kim Low03ea0352020-11-06 12:45:07 -0800701std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
702 std::scoped_lock _l(mLock);
703
704 InputDevice* device = findInputDeviceLocked(deviceId);
705 if (device) {
706 return device->getBatteryCapacity();
707 }
708 return std::nullopt;
709}
710
711std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
712 std::scoped_lock _l(mLock);
713
714 InputDevice* device = findInputDeviceLocked(deviceId);
715 if (device) {
716 return device->getBatteryStatus();
717 }
718 return std::nullopt;
719}
720
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000721std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800722 std::scoped_lock _l(mLock);
723
724 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000725 if (device == nullptr) {
726 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800727 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000728
729 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800730}
731
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000732std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800733 std::scoped_lock _l(mLock);
734
735 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000736 if (device == nullptr) {
737 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800738 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000739
740 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800741}
742
743bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
744 std::scoped_lock _l(mLock);
745
746 InputDevice* device = findInputDeviceLocked(deviceId);
747 if (device) {
748 return device->setLightColor(lightId, color);
749 }
750 return false;
751}
752
753bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
754 std::scoped_lock _l(mLock);
755
756 InputDevice* device = findInputDeviceLocked(deviceId);
757 if (device) {
758 return device->setLightPlayerId(lightId, playerId);
759 }
760 return false;
761}
762
763std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
764 std::scoped_lock _l(mLock);
765
766 InputDevice* device = findInputDeviceLocked(deviceId);
767 if (device) {
768 return device->getLightColor(lightId);
769 }
770 return std::nullopt;
771}
772
773std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
774 std::scoped_lock _l(mLock);
775
776 InputDevice* device = findInputDeviceLocked(deviceId);
777 if (device) {
778 return device->getLightPlayerId(lightId);
779 }
780 return std::nullopt;
781}
782
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700783bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000784 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700785
Chris Ye1c2e0892020-11-30 21:41:44 -0800786 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800787 if (device) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700788 return device->isEnabled();
789 }
790 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
791 return false;
792}
793
Arthur Hungc23540e2018-11-29 20:42:11 +0800794bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000795 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800796
Chris Ye1c2e0892020-11-30 21:41:44 -0800797 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800798 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800799 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
800 return false;
801 }
802
Arthur Hung2c9a3342019-07-23 14:18:59 +0800803 if (!device->isEnabled()) {
804 ALOGW("Ignoring disabled device %s", device->getName().c_str());
805 return false;
806 }
807
808 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800809 // No associated display. By default, can dispatch to all displays.
810 if (!associatedDisplayId) {
811 return true;
812 }
813
814 if (*associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hung2c9a3342019-07-23 14:18:59 +0800815 ALOGW("Device %s is associated with display ADISPLAY_ID_NONE.", device->getName().c_str());
Arthur Hungc23540e2018-11-29 20:42:11 +0800816 return true;
817 }
818
819 return *associatedDisplayId == displayId;
820}
821
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800822void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000823 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800824
825 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800826 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827
Chris Yee7310032020-09-22 15:36:28 -0700828 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
829 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800830
Chris Yee7310032020-09-22 15:36:28 -0700831 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
832 const std::shared_ptr<InputDevice>& device = devicePair.first;
833 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
834 for (const auto& eId : devicePair.second) {
835 eventHubDevStr += StringPrintf("%d ", eId);
836 }
837 eventHubDevStr += "] \n";
838 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839 }
840
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800841 dump += INDENT "Configuration:\n";
842 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800843 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
844 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800845 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800846 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100847 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800849 dump += "]\n";
850 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700851 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800853 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700854 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
855 "acceleration=%0.3f\n",
856 mConfig.pointerVelocityControlParameters.scale,
857 mConfig.pointerVelocityControlParameters.lowThreshold,
858 mConfig.pointerVelocityControlParameters.highThreshold,
859 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800861 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700862 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
863 "acceleration=%0.3f\n",
864 mConfig.wheelVelocityControlParameters.scale,
865 mConfig.wheelVelocityControlParameters.lowThreshold,
866 mConfig.wheelVelocityControlParameters.highThreshold,
867 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800869 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700870 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800871 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700872 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800873 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700874 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800875 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700876 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800877 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700878 mConfig.pointerGestureTapDragInterval * 0.000001f);
879 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800880 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700881 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800882 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700883 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800884 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700885 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800886 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700887 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800888 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700889 mConfig.pointerGestureMovementSpeedRatio);
890 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700891
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800892 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700893 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800894}
895
896void InputReader::monitor() {
897 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800898 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800900 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901 // Check the EventHub
902 mEventHub->monitor();
903}
904
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905// --- InputReader::ContextImpl ---
906
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800907InputReader::ContextImpl::ContextImpl(InputReader* reader)
908 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909
910void InputReader::ContextImpl::updateGlobalMetaState() {
911 // lock is already held by the input loop
912 mReader->updateGlobalMetaStateLocked();
913}
914
915int32_t InputReader::ContextImpl::getGlobalMetaState() {
916 // lock is already held by the input loop
917 return mReader->getGlobalMetaStateLocked();
918}
919
arthurhungc903df12020-08-11 15:08:42 +0800920void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
921 // lock is already held by the input loop
922 mReader->updateLedMetaStateLocked(metaState);
923}
924
925int32_t InputReader::ContextImpl::getLedMetaState() {
926 // lock is already held by the input loop
927 return mReader->getLedMetaStateLocked();
928}
929
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
931 // lock is already held by the input loop
932 mReader->disableVirtualKeysUntilLocked(time);
933}
934
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800935bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
936 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800938 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939}
940
941void InputReader::ContextImpl::fadePointer() {
942 // lock is already held by the input loop
943 mReader->fadePointerLocked();
944}
945
Michael Wright17db18e2020-06-26 20:51:44 +0100946std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController(
947 int32_t deviceId) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800948 // lock is already held by the input loop
949 return mReader->getPointerControllerLocked(deviceId);
950}
951
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
953 // lock is already held by the input loop
954 mReader->requestTimeoutAtTimeLocked(when);
955}
956
957int32_t InputReader::ContextImpl::bumpGeneration() {
958 // lock is already held by the input loop
959 return mReader->bumpGenerationLocked();
960}
961
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800962void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700963 // lock is already held by whatever called refreshConfigurationLocked
964 mReader->getExternalStylusDevicesLocked(outDevices);
965}
966
967void InputReader::ContextImpl::dispatchExternalStylusState(const StylusState& state) {
Arthur Hungcadce9d2021-05-07 17:24:04 +0800968 mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -0700969}
970
Michael Wrightd02c5b62014-02-10 15:10:22 -0800971InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
972 return mReader->mPolicy.get();
973}
974
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000975InputListenerInterface* InputReader::ContextImpl::getListener() {
976 return mReader->mQueuedListener.get();
977}
978
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979EventHubInterface* InputReader::ContextImpl::getEventHub() {
980 return mReader->mEventHub.get();
981}
982
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000983int32_t InputReader::ContextImpl::getNextId() {
984 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800985}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986
Michael Wrightd02c5b62014-02-10 15:10:22 -0800987} // namespace android