blob: 31d331bb98174c6e4e27faa65b36ec047aabd266 [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
Prabir Pradhan28efc192019-11-05 01:10:04 +000041// --- InputReader ---
42
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070043InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
44 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070045 InputListenerInterface& listener)
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070046 : mContext(this),
47 mEventHub(eventHub),
48 mPolicy(policy),
Siarhei Vishniakou18050092021-09-01 13:32:49 -070049 mQueuedListener(listener),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070050 mGlobalMetaState(0),
arthurhungc903df12020-08-11 15:08:42 +080051 mLedMetaState(AMETA_NUM_LOCK_ON),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070052 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080053 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070054 mDisableVirtualKeysTimeout(LLONG_MIN),
55 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -080056 mConfigurationChangesToRefresh(0) {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070057 refreshConfigurationLocked(0);
58 updateGlobalMetaStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -080059}
60
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +000061InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
Prabir Pradhan28efc192019-11-05 01:10:04 +000063status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070064 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000065 return ALREADY_EXISTS;
66 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070067 mThread = std::make_unique<InputThread>(
68 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
69 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +000070}
71
72status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070073 if (mThread && mThread->isCallingThread()) {
74 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +000075 return INVALID_OPERATION;
76 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070077 mThread.reset();
78 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +000079}
80
Michael Wrightd02c5b62014-02-10 15:10:22 -080081void InputReader::loopOnce() {
82 int32_t oldGeneration;
83 int32_t timeoutMillis;
84 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -080085 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -080086 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +000087 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -080088
89 oldGeneration = mGeneration;
90 timeoutMillis = -1;
91
92 uint32_t changes = mConfigurationChangesToRefresh;
93 if (changes) {
94 mConfigurationChangesToRefresh = 0;
95 timeoutMillis = 0;
96 refreshConfigurationLocked(changes);
97 } else if (mNextTimeout != LLONG_MAX) {
98 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
99 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
100 }
101 } // release lock
102
103 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
104
105 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000106 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800107 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800108
109 if (count) {
110 processEventsLocked(mEventBuffer, count);
111 }
112
113 if (mNextTimeout != LLONG_MAX) {
114 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
115 if (now >= mNextTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800116 if (DEBUG_RAW_EVENTS) {
117 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
118 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119 mNextTimeout = LLONG_MAX;
120 timeoutExpiredLocked(now);
121 }
122 }
123
124 if (oldGeneration != mGeneration) {
125 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800126 inputDevices = getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127 }
128 } // release lock
129
130 // Send out a message that the describes the changed input devices.
131 if (inputDevicesChanged) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800132 mPolicy->notifyInputDevicesChanged(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800133 }
134
135 // Flush queued events out to the listener.
136 // This must happen outside of the lock because the listener could potentially call
137 // back into the InputReader's methods, such as getScanCodeState, or become blocked
138 // on another thread similarly waiting to acquire the InputReader lock thereby
139 // resulting in a deadlock. This situation is actually quite plausible because the
140 // listener is actually the input dispatcher, which calls into the window manager,
141 // which occasionally calls into the input reader.
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700142 mQueuedListener.flush();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143}
144
145void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
146 for (const RawEvent* rawEvent = rawEvents; count;) {
147 int32_t type = rawEvent->type;
148 size_t batchSize = 1;
149 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
150 int32_t deviceId = rawEvent->deviceId;
151 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700152 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
153 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 break;
155 }
156 batchSize += 1;
157 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800158 if (DEBUG_RAW_EVENTS) {
159 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
160 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
162 } else {
163 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700164 case EventHubInterface::DEVICE_ADDED:
165 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
166 break;
167 case EventHubInterface::DEVICE_REMOVED:
168 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
169 break;
170 case EventHubInterface::FINISHED_DEVICE_SCAN:
171 handleConfigurationChangedLocked(rawEvent->when);
172 break;
173 default:
174 ALOG_ASSERT(false); // can't happen
175 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176 }
177 }
178 count -= batchSize;
179 rawEvent += batchSize;
180 }
181}
182
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800183void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
184 if (mDevices.find(eventHubId) != mDevices.end()) {
185 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 return;
187 }
188
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800189 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
190 std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191 device->configure(when, &mConfig, 0);
192 device->reset(when);
193
194 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800195 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
196 "(ignored non-input device)",
197 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800199 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=0x%08x",
200 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700201 device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202 }
203
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800204 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700205 // Add device to device to EventHub ids map.
206 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
207 if (mapIt == mDeviceToEventHubIdsMap.end()) {
208 std::vector<int32_t> ids = {eventHubId};
209 mDeviceToEventHubIdsMap.emplace(device, ids);
210 } else {
211 mapIt->second.push_back(eventHubId);
212 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700214
Chris Ye1b0c7342020-07-28 21:57:03 -0700215 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800216 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700217 }
Chris Yef59a2f42020-10-16 12:55:26 -0700218
219 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800220 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
221 // device class to disable SENSOR sub device only.
222 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700223 mEventHub->disableDevice(eventHubId);
224 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225}
226
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800227void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
228 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000229 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800230 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 return;
232 }
233
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000234 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000235 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700236 // Erase device from device to EventHub ids map.
237 auto mapIt = mDeviceToEventHubIdsMap.find(device);
238 if (mapIt != mDeviceToEventHubIdsMap.end()) {
239 std::vector<int32_t>& eventHubIds = mapIt->second;
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -0800240 std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
Chris Yee7310032020-09-22 15:36:28 -0700241 if (eventHubIds.size() == 0) {
242 mDeviceToEventHubIdsMap.erase(mapIt);
243 }
244 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245 bumpGenerationLocked();
246
247 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800248 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
249 "(ignored non-input device)",
250 device->getId(), eventHubId, device->getName().c_str(),
251 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800253 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=0x%08x",
254 device->getId(), eventHubId, device->getName().c_str(),
255 device->getDescriptor().c_str(), device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256 }
257
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800258 device->removeEventHubDevice(eventHubId);
259
Chris Ye1b0c7342020-07-28 21:57:03 -0700260 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800261 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700262 }
263
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800264 if (device->hasEventHubDevices()) {
265 device->configure(when, &mConfig, 0);
266 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267 device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268}
269
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000270std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800271 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
272 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
273 return devicePair.second->getDescriptor().size() && identifier.descriptor.size() &&
274 devicePair.second->getDescriptor() == identifier.descriptor;
275 });
276
277 std::shared_ptr<InputDevice> device;
278 if (deviceIt != mDevices.end()) {
279 device = deviceIt->second;
280 } else {
281 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
282 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
283 identifier);
284 }
285 device->addEventHubDevice(eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800286 return device;
287}
288
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800289void InputReader::processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700290 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800291 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000292 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800293 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294 return;
295 }
296
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000297 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700299 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800300 return;
301 }
302
303 device->process(rawEvents, count);
304}
305
Philip Junker4af3b3d2021-12-14 10:36:55 +0100306InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800307 auto deviceIt =
308 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
309 return devicePair.second->getId() == deviceId;
310 });
311 if (deviceIt != mDevices.end()) {
312 return deviceIt->second.get();
313 }
314 return nullptr;
315}
316
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317void InputReader::timeoutExpiredLocked(nsecs_t when) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000318 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000319 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320 if (!device->isIgnored()) {
321 device->timeoutExpired(when);
322 }
323 }
324}
325
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800326int32_t InputReader::nextInputDeviceIdLocked() {
327 return ++mNextInputDeviceId;
328}
329
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
331 // Reset global meta state because it depends on the list of all configured devices.
332 updateGlobalMetaStateLocked();
333
334 // Enqueue configuration changed.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000335 NotifyConfigurationChangedArgs args(mContext.getNextId(), when);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700336 mQueuedListener.notifyConfigurationChanged(&args);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337}
338
339void InputReader::refreshConfigurationLocked(uint32_t changes) {
340 mPolicy->getReaderConfiguration(&mConfig);
341 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
342
Prabir Pradhan7e186182020-11-10 13:56:45 -0800343 if (!changes) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344
Prabir Pradhan7e186182020-11-10 13:56:45 -0800345 ALOGI("Reconfiguring input devices, changes=%s",
346 InputReaderConfiguration::changesToString(changes).c_str());
347 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800348
Prabir Pradhan7e186182020-11-10 13:56:45 -0800349 if (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO) {
350 updatePointerDisplayLocked();
351 }
352
353 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
354 mEventHub->requestReopenDevices();
355 } else {
356 for (auto& devicePair : mDevices) {
357 std::shared_ptr<InputDevice>& device = devicePair.second;
358 device->configure(now, &mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359 }
360 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800361
362 if (changes & InputReaderConfiguration::CHANGE_POINTER_CAPTURE) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
364 ALOGV("Skipping notifying pointer capture changes: "
365 "There was no change in the pointer capture state.");
366 } else {
367 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
368 const NotifyPointerCaptureChangedArgs args(mContext.getNextId(), now,
369 mCurrentPointerCaptureRequest);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700370 mQueuedListener.notifyPointerCaptureChanged(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000371 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800372 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373}
374
375void InputReader::updateGlobalMetaStateLocked() {
376 mGlobalMetaState = 0;
377
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000378 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000379 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800380 mGlobalMetaState |= device->getMetaState();
381 }
382}
383
384int32_t InputReader::getGlobalMetaStateLocked() {
385 return mGlobalMetaState;
386}
387
arthurhungc903df12020-08-11 15:08:42 +0800388void InputReader::updateLedMetaStateLocked(int32_t metaState) {
389 mLedMetaState = metaState;
390 for (auto& devicePair : mDevices) {
391 std::shared_ptr<InputDevice>& device = devicePair.second;
392 device->updateLedState(false);
393 }
394}
395
396int32_t InputReader::getLedMetaStateLocked() {
397 return mLedMetaState;
398}
399
Chris Ye1c2e0892020-11-30 21:41:44 -0800400void InputReader::notifyExternalStylusPresenceChangedLocked() {
Michael Wright842500e2015-03-13 17:32:02 -0700401 refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
402}
403
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800404void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000405 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000406 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700407 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000408 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700409 }
410 }
411}
412
Arthur Hungcadce9d2021-05-07 17:24:04 +0800413void InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000414 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000415 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wright842500e2015-03-13 17:32:02 -0700416 device->updateExternalStylusState(state);
417 }
418}
419
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
421 mDisableVirtualKeysTimeout = time;
422}
423
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800424bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800426 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700427 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800428 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429 return true;
430 } else {
431 return false;
432 }
433}
434
Michael Wright17db18e2020-06-26 20:51:44 +0100435std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked(
436 int32_t deviceId) {
437 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800438 if (controller == nullptr) {
439 controller = mPolicy->obtainPointerController(deviceId);
440 mPointerController = controller;
441 updatePointerDisplayLocked();
442 }
443 return controller;
444}
445
446void InputReader::updatePointerDisplayLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100447 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800448 if (controller == nullptr) {
449 return;
450 }
451
452 std::optional<DisplayViewport> viewport =
453 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
454 if (!viewport) {
455 ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input "
456 "mapper. Fall back to default display",
457 mConfig.defaultPointerDisplayId);
458 viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT);
459 }
460 if (!viewport) {
461 ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to"
462 " PointerController.");
463 return;
464 }
465
466 controller->setDisplayViewport(*viewport);
467}
468
Michael Wrightd02c5b62014-02-10 15:10:22 -0800469void InputReader::fadePointerLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100470 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800471 if (controller != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +0100472 controller->fade(PointerControllerInterface::Transition::GRADUAL);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473 }
474}
475
476void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
477 if (when < mNextTimeout) {
478 mNextTimeout = when;
479 mEventHub->wake();
480 }
481}
482
483int32_t InputReader::bumpGenerationLocked() {
484 return ++mGeneration;
485}
486
Chris Ye98d3f532020-10-01 21:48:59 -0700487std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000488 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700489 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490}
491
Chris Ye98d3f532020-10-01 21:48:59 -0700492std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
493 std::vector<InputDeviceInfo> outInputDevices;
494 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800495
Chris Yee7310032020-09-22 15:36:28 -0700496 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000498 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 }
500 }
Chris Ye98d3f532020-10-01 21:48:59 -0700501 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502}
503
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700504int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000505 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506
507 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
508}
509
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700510int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000511 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512
513 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
514}
515
516int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000517 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518
519 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
520}
521
522int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700523 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524 int32_t result = AKEY_STATE_UNKNOWN;
525 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800526 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800527 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
528 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529 }
530 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000531 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000532 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700533 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
535 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000536 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537 if (currentResult >= AKEY_STATE_DOWN) {
538 return currentResult;
539 } else if (currentResult == AKEY_STATE_UP) {
540 result = currentResult;
541 }
542 }
543 }
544 }
545 return result;
546}
547
Andrii Kulian763a3a42016-03-08 10:46:16 -0800548void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000549 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800550 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800551 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800552 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
553 return;
554 }
555
Andrii Kulian763a3a42016-03-08 10:46:16 -0800556 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000557 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800558 return;
559 }
560
561 device->updateMetaState(AKEYCODE_CAPS_LOCK);
562}
563
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700564bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
565 const int32_t* keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000566 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567
568 memset(outFlags, 0, numCodes);
569 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
570}
571
572bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700573 size_t numCodes, const int32_t* keyCodes,
574 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800575 bool result = false;
576 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800577 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800578 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
579 result = device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580 }
581 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000582 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000583 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700584 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
585 result |= device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586 }
587 }
588 }
589 return result;
590}
591
Philip Junker4af3b3d2021-12-14 10:36:55 +0100592int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
593 std::scoped_lock _l(mLock);
594
595 InputDevice* device = findInputDeviceLocked(deviceId);
596 if (device == nullptr) {
597 ALOGW("Failed to get key code for key location: Input device with id %d not found",
598 deviceId);
599 return AKEYCODE_UNKNOWN;
600 }
601 return device->getKeyCodeForKeyLocation(locationKeyCode);
602}
603
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604void InputReader::requestRefreshConfiguration(uint32_t changes) {
Chris Ye87143712020-11-10 05:05:58 +0000605 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606
607 if (changes) {
608 bool needWake = !mConfigurationChangesToRefresh;
609 mConfigurationChangesToRefresh |= changes;
610
611 if (needWake) {
612 mEventHub->wake();
613 }
614 }
615}
616
Chris Ye87143712020-11-10 05:05:58 +0000617void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
618 int32_t token) {
619 std::scoped_lock _l(mLock);
620
Chris Ye1c2e0892020-11-30 21:41:44 -0800621 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800622 if (device) {
Chris Ye87143712020-11-10 05:05:58 +0000623 device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800624 }
625}
626
627void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000628 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629
Chris Ye1c2e0892020-11-30 21:41:44 -0800630 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800631 if (device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 device->cancelVibrate(token);
633 }
634}
635
Chris Ye87143712020-11-10 05:05:58 +0000636bool InputReader::isVibrating(int32_t deviceId) {
637 std::scoped_lock _l(mLock);
638
639 InputDevice* device = findInputDeviceLocked(deviceId);
640 if (device) {
641 return device->isVibrating();
642 }
643 return false;
644}
645
646std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
647 std::scoped_lock _l(mLock);
648
649 InputDevice* device = findInputDeviceLocked(deviceId);
650 if (device) {
651 return device->getVibratorIds();
652 }
653 return {};
654}
655
Chris Yef59a2f42020-10-16 12:55:26 -0700656void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
657 std::scoped_lock _l(mLock);
658
659 InputDevice* device = findInputDeviceLocked(deviceId);
660 if (device) {
661 device->disableSensor(sensorType);
662 }
663}
664
665bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
666 std::chrono::microseconds samplingPeriod,
667 std::chrono::microseconds maxBatchReportLatency) {
668 std::scoped_lock _l(mLock);
669
670 InputDevice* device = findInputDeviceLocked(deviceId);
671 if (device) {
672 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
673 }
674 return false;
675}
676
677void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
678 std::scoped_lock _l(mLock);
679
680 InputDevice* device = findInputDeviceLocked(deviceId);
681 if (device) {
682 device->flushSensor(sensorType);
683 }
684}
685
Kim Low03ea0352020-11-06 12:45:07 -0800686std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
687 std::scoped_lock _l(mLock);
688
689 InputDevice* device = findInputDeviceLocked(deviceId);
690 if (device) {
691 return device->getBatteryCapacity();
692 }
693 return std::nullopt;
694}
695
696std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
697 std::scoped_lock _l(mLock);
698
699 InputDevice* device = findInputDeviceLocked(deviceId);
700 if (device) {
701 return device->getBatteryStatus();
702 }
703 return std::nullopt;
704}
705
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000706std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800707 std::scoped_lock _l(mLock);
708
709 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000710 if (device == nullptr) {
711 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800712 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000713
714 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800715}
716
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000717std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800718 std::scoped_lock _l(mLock);
719
720 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000721 if (device == nullptr) {
722 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800723 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000724
725 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800726}
727
728bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
729 std::scoped_lock _l(mLock);
730
731 InputDevice* device = findInputDeviceLocked(deviceId);
732 if (device) {
733 return device->setLightColor(lightId, color);
734 }
735 return false;
736}
737
738bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
739 std::scoped_lock _l(mLock);
740
741 InputDevice* device = findInputDeviceLocked(deviceId);
742 if (device) {
743 return device->setLightPlayerId(lightId, playerId);
744 }
745 return false;
746}
747
748std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
749 std::scoped_lock _l(mLock);
750
751 InputDevice* device = findInputDeviceLocked(deviceId);
752 if (device) {
753 return device->getLightColor(lightId);
754 }
755 return std::nullopt;
756}
757
758std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
759 std::scoped_lock _l(mLock);
760
761 InputDevice* device = findInputDeviceLocked(deviceId);
762 if (device) {
763 return device->getLightPlayerId(lightId);
764 }
765 return std::nullopt;
766}
767
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700768bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000769 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700770
Chris Ye1c2e0892020-11-30 21:41:44 -0800771 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800772 if (device) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700773 return device->isEnabled();
774 }
775 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
776 return false;
777}
778
Arthur Hungc23540e2018-11-29 20:42:11 +0800779bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000780 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800781
Chris Ye1c2e0892020-11-30 21:41:44 -0800782 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800783 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800784 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
785 return false;
786 }
787
Arthur Hung2c9a3342019-07-23 14:18:59 +0800788 if (!device->isEnabled()) {
789 ALOGW("Ignoring disabled device %s", device->getName().c_str());
790 return false;
791 }
792
793 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800794 // No associated display. By default, can dispatch to all displays.
Weilun Dud00847d2021-12-08 10:55:58 -0800795 if (!associatedDisplayId ||
796 *associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800797 return true;
798 }
799
800 return *associatedDisplayId == displayId;
801}
802
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800803void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000804 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805
806 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800807 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800808
Chris Yee7310032020-09-22 15:36:28 -0700809 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
810 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800811
Chris Yee7310032020-09-22 15:36:28 -0700812 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
813 const std::shared_ptr<InputDevice>& device = devicePair.first;
814 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
815 for (const auto& eId : devicePair.second) {
816 eventHubDevStr += StringPrintf("%d ", eId);
817 }
818 eventHubDevStr += "] \n";
819 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 }
821
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800822 dump += INDENT "Configuration:\n";
823 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800824 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
825 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800826 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100828 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800830 dump += "]\n";
831 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700832 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800834 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700835 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
836 "acceleration=%0.3f\n",
837 mConfig.pointerVelocityControlParameters.scale,
838 mConfig.pointerVelocityControlParameters.lowThreshold,
839 mConfig.pointerVelocityControlParameters.highThreshold,
840 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800841
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800842 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700843 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
844 "acceleration=%0.3f\n",
845 mConfig.wheelVelocityControlParameters.scale,
846 mConfig.wheelVelocityControlParameters.lowThreshold,
847 mConfig.wheelVelocityControlParameters.highThreshold,
848 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800850 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700851 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800852 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700853 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800854 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700855 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800856 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700857 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800858 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700859 mConfig.pointerGestureTapDragInterval * 0.000001f);
860 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800861 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700862 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800863 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700864 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800865 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700866 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800867 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700868 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800869 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700870 mConfig.pointerGestureMovementSpeedRatio);
871 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700872
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800873 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700874 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875}
876
877void InputReader::monitor() {
878 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800879 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800880 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800881 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882 // Check the EventHub
883 mEventHub->monitor();
884}
885
Michael Wrightd02c5b62014-02-10 15:10:22 -0800886// --- InputReader::ContextImpl ---
887
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800888InputReader::ContextImpl::ContextImpl(InputReader* reader)
889 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890
891void InputReader::ContextImpl::updateGlobalMetaState() {
892 // lock is already held by the input loop
893 mReader->updateGlobalMetaStateLocked();
894}
895
896int32_t InputReader::ContextImpl::getGlobalMetaState() {
897 // lock is already held by the input loop
898 return mReader->getGlobalMetaStateLocked();
899}
900
arthurhungc903df12020-08-11 15:08:42 +0800901void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
902 // lock is already held by the input loop
903 mReader->updateLedMetaStateLocked(metaState);
904}
905
906int32_t InputReader::ContextImpl::getLedMetaState() {
907 // lock is already held by the input loop
908 return mReader->getLedMetaStateLocked();
909}
910
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
912 // lock is already held by the input loop
913 mReader->disableVirtualKeysUntilLocked(time);
914}
915
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800916bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
917 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800919 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920}
921
922void InputReader::ContextImpl::fadePointer() {
923 // lock is already held by the input loop
924 mReader->fadePointerLocked();
925}
926
Michael Wright17db18e2020-06-26 20:51:44 +0100927std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController(
928 int32_t deviceId) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800929 // lock is already held by the input loop
930 return mReader->getPointerControllerLocked(deviceId);
931}
932
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
934 // lock is already held by the input loop
935 mReader->requestTimeoutAtTimeLocked(when);
936}
937
938int32_t InputReader::ContextImpl::bumpGeneration() {
939 // lock is already held by the input loop
940 return mReader->bumpGenerationLocked();
941}
942
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800943void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700944 // lock is already held by whatever called refreshConfigurationLocked
945 mReader->getExternalStylusDevicesLocked(outDevices);
946}
947
948void InputReader::ContextImpl::dispatchExternalStylusState(const StylusState& state) {
Arthur Hungcadce9d2021-05-07 17:24:04 +0800949 mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -0700950}
951
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
953 return mReader->mPolicy.get();
954}
955
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700956InputListenerInterface& InputReader::ContextImpl::getListener() {
957 return mReader->mQueuedListener;
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000958}
959
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960EventHubInterface* InputReader::ContextImpl::getEventHub() {
961 return mReader->mEventHub.get();
962}
963
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000964int32_t InputReader::ContextImpl::getNextId() {
965 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800966}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968} // namespace android