blob: be21ace2218f86db60193a9b9dac9927211359e1 [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,
45 const sp<InputListenerInterface>& listener)
46 : mContext(this),
47 mEventHub(eventHub),
48 mPolicy(policy),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070049 mGlobalMetaState(0),
arthurhungc903df12020-08-11 15:08:42 +080050 mLedMetaState(AMETA_NUM_LOCK_ON),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070051 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080052 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070053 mDisableVirtualKeysTimeout(LLONG_MIN),
54 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -080055 mConfigurationChangesToRefresh(0) {
56 mQueuedListener = new QueuedInputListener(listener);
57
58 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +000059 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -080060
61 refreshConfigurationLocked(0);
62 updateGlobalMetaStateLocked();
63 } // release lock
64}
65
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +000066InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
Prabir Pradhan28efc192019-11-05 01:10:04 +000068status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070069 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000070 return ALREADY_EXISTS;
71 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070072 mThread = std::make_unique<InputThread>(
73 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
74 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +000075}
76
77status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070078 if (mThread && mThread->isCallingThread()) {
79 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +000080 return INVALID_OPERATION;
81 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070082 mThread.reset();
83 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +000084}
85
Michael Wrightd02c5b62014-02-10 15:10:22 -080086void InputReader::loopOnce() {
87 int32_t oldGeneration;
88 int32_t timeoutMillis;
89 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -080090 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -080091 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +000092 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
94 oldGeneration = mGeneration;
95 timeoutMillis = -1;
96
97 uint32_t changes = mConfigurationChangesToRefresh;
98 if (changes) {
99 mConfigurationChangesToRefresh = 0;
100 timeoutMillis = 0;
101 refreshConfigurationLocked(changes);
102 } else if (mNextTimeout != LLONG_MAX) {
103 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
104 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
105 }
106 } // release lock
107
108 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
109
110 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000111 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800112 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113
114 if (count) {
115 processEventsLocked(mEventBuffer, count);
116 }
117
118 if (mNextTimeout != LLONG_MAX) {
119 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
120 if (now >= mNextTimeout) {
121#if DEBUG_RAW_EVENTS
122 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
123#endif
124 mNextTimeout = LLONG_MAX;
125 timeoutExpiredLocked(now);
126 }
127 }
128
129 if (oldGeneration != mGeneration) {
130 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800131 inputDevices = getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800132 }
133 } // release lock
134
135 // Send out a message that the describes the changed input devices.
136 if (inputDevicesChanged) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800137 mPolicy->notifyInputDevicesChanged(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138 }
139
140 // Flush queued events out to the listener.
141 // This must happen outside of the lock because the listener could potentially call
142 // back into the InputReader's methods, such as getScanCodeState, or become blocked
143 // on another thread similarly waiting to acquire the InputReader lock thereby
144 // resulting in a deadlock. This situation is actually quite plausible because the
145 // listener is actually the input dispatcher, which calls into the window manager,
146 // which occasionally calls into the input reader.
147 mQueuedListener->flush();
148}
149
150void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
151 for (const RawEvent* rawEvent = rawEvents; count;) {
152 int32_t type = rawEvent->type;
153 size_t batchSize = 1;
154 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
155 int32_t deviceId = rawEvent->deviceId;
156 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700157 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
158 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159 break;
160 }
161 batchSize += 1;
162 }
163#if DEBUG_RAW_EVENTS
Siarhei Vishniakou5d83f602017-09-12 12:40:29 -0700164 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165#endif
166 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
167 } else {
168 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700169 case EventHubInterface::DEVICE_ADDED:
170 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
171 break;
172 case EventHubInterface::DEVICE_REMOVED:
173 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
174 break;
175 case EventHubInterface::FINISHED_DEVICE_SCAN:
176 handleConfigurationChangedLocked(rawEvent->when);
177 break;
178 default:
179 ALOG_ASSERT(false); // can't happen
180 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 }
182 }
183 count -= batchSize;
184 rawEvent += batchSize;
185 }
186}
187
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800188void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
189 if (mDevices.find(eventHubId) != mDevices.end()) {
190 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191 return;
192 }
193
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800194 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
195 std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 device->configure(when, &mConfig, 0);
197 device->reset(when);
198
199 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800200 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
201 "(ignored non-input device)",
202 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800204 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=0x%08x",
205 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700206 device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 }
208
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800209 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700210 // Add device to device to EventHub ids map.
211 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
212 if (mapIt == mDeviceToEventHubIdsMap.end()) {
213 std::vector<int32_t> ids = {eventHubId};
214 mDeviceToEventHubIdsMap.emplace(device, ids);
215 } else {
216 mapIt->second.push_back(eventHubId);
217 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700219
Chris Ye1b0c7342020-07-28 21:57:03 -0700220 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800221 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700222 }
Chris Yef59a2f42020-10-16 12:55:26 -0700223
224 // Sensor input device is noisy, to save power disable it by default.
225 if (device->getClasses().test(InputDeviceClass::SENSOR)) {
226 mEventHub->disableDevice(eventHubId);
227 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228}
229
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800230void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
231 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000232 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800233 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 return;
235 }
236
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000237 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000238 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700239 // Erase device from device to EventHub ids map.
240 auto mapIt = mDeviceToEventHubIdsMap.find(device);
241 if (mapIt != mDeviceToEventHubIdsMap.end()) {
242 std::vector<int32_t>& eventHubIds = mapIt->second;
243 eventHubIds.erase(std::remove_if(eventHubIds.begin(), eventHubIds.end(),
244 [eventHubId](int32_t eId) { return eId == eventHubId; }),
245 eventHubIds.end());
246 if (eventHubIds.size() == 0) {
247 mDeviceToEventHubIdsMap.erase(mapIt);
248 }
249 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250 bumpGenerationLocked();
251
252 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800253 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
254 "(ignored non-input device)",
255 device->getId(), eventHubId, device->getName().c_str(),
256 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800258 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=0x%08x",
259 device->getId(), eventHubId, device->getName().c_str(),
260 device->getDescriptor().c_str(), device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 }
262
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800263 device->removeEventHubDevice(eventHubId);
264
Chris Ye1b0c7342020-07-28 21:57:03 -0700265 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800266 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700267 }
268
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800269 if (device->hasEventHubDevices()) {
270 device->configure(when, &mConfig, 0);
271 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272 device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273}
274
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000275std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800276 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
277 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
278 return devicePair.second->getDescriptor().size() && identifier.descriptor.size() &&
279 devicePair.second->getDescriptor() == identifier.descriptor;
280 });
281
282 std::shared_ptr<InputDevice> device;
283 if (deviceIt != mDevices.end()) {
284 device = deviceIt->second;
285 } else {
286 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
287 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
288 identifier);
289 }
290 device->addEventHubDevice(eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291 return device;
292}
293
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800294void InputReader::processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700295 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800296 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000297 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800298 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299 return;
300 }
301
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000302 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700304 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800305 return;
306 }
307
308 device->process(rawEvents, count);
309}
310
Chris Ye1c2e0892020-11-30 21:41:44 -0800311InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800312 auto deviceIt =
313 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
314 return devicePair.second->getId() == deviceId;
315 });
316 if (deviceIt != mDevices.end()) {
317 return deviceIt->second.get();
318 }
319 return nullptr;
320}
321
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322void InputReader::timeoutExpiredLocked(nsecs_t when) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000323 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000324 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 if (!device->isIgnored()) {
326 device->timeoutExpired(when);
327 }
328 }
329}
330
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800331int32_t InputReader::nextInputDeviceIdLocked() {
332 return ++mNextInputDeviceId;
333}
334
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
336 // Reset global meta state because it depends on the list of all configured devices.
337 updateGlobalMetaStateLocked();
338
339 // Enqueue configuration changed.
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800340 NotifyConfigurationChangedArgs args(mContext.getNextId(), when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 mQueuedListener->notifyConfigurationChanged(&args);
342}
343
344void InputReader::refreshConfigurationLocked(uint32_t changes) {
345 mPolicy->getReaderConfiguration(&mConfig);
346 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
347
Prabir Pradhan7e186182020-11-10 13:56:45 -0800348 if (!changes) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349
Prabir Pradhan7e186182020-11-10 13:56:45 -0800350 ALOGI("Reconfiguring input devices, changes=%s",
351 InputReaderConfiguration::changesToString(changes).c_str());
352 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800353
Prabir Pradhan7e186182020-11-10 13:56:45 -0800354 if (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO) {
355 updatePointerDisplayLocked();
356 }
357
358 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
359 mEventHub->requestReopenDevices();
360 } else {
361 for (auto& devicePair : mDevices) {
362 std::shared_ptr<InputDevice>& device = devicePair.second;
363 device->configure(now, &mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 }
365 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800366
367 if (changes & InputReaderConfiguration::CHANGE_POINTER_CAPTURE) {
368 const NotifyPointerCaptureChangedArgs args(mContext.getNextId(), now,
369 mConfig.pointerCapture);
370 mQueuedListener->notifyPointerCaptureChanged(&args);
371 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372}
373
374void InputReader::updateGlobalMetaStateLocked() {
375 mGlobalMetaState = 0;
376
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000377 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000378 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800379 mGlobalMetaState |= device->getMetaState();
380 }
381}
382
383int32_t InputReader::getGlobalMetaStateLocked() {
384 return mGlobalMetaState;
385}
386
arthurhungc903df12020-08-11 15:08:42 +0800387void InputReader::updateLedMetaStateLocked(int32_t metaState) {
388 mLedMetaState = metaState;
389 for (auto& devicePair : mDevices) {
390 std::shared_ptr<InputDevice>& device = devicePair.second;
391 device->updateLedState(false);
392 }
393}
394
395int32_t InputReader::getLedMetaStateLocked() {
396 return mLedMetaState;
397}
398
Chris Ye1c2e0892020-11-30 21:41:44 -0800399void InputReader::notifyExternalStylusPresenceChangedLocked() {
Michael Wright842500e2015-03-13 17:32:02 -0700400 refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
401}
402
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800403void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000404 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000405 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700406 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800407 InputDeviceInfo info;
408 device->getDeviceInfo(&info);
409 outDevices.push_back(info);
Michael Wright842500e2015-03-13 17:32:02 -0700410 }
411 }
412}
413
414void InputReader::dispatchExternalStylusState(const StylusState& state) {
Chris Ye87143712020-11-10 05:05:58 +0000415 std::scoped_lock _l(mLock);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000416 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000417 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wright842500e2015-03-13 17:32:02 -0700418 device->updateExternalStylusState(state);
419 }
420}
421
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
423 mDisableVirtualKeysTimeout = time;
424}
425
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800426bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800428 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700429 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800430 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431 return true;
432 } else {
433 return false;
434 }
435}
436
Michael Wright17db18e2020-06-26 20:51:44 +0100437std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked(
438 int32_t deviceId) {
439 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800440 if (controller == nullptr) {
441 controller = mPolicy->obtainPointerController(deviceId);
442 mPointerController = controller;
443 updatePointerDisplayLocked();
444 }
445 return controller;
446}
447
448void InputReader::updatePointerDisplayLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100449 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800450 if (controller == nullptr) {
451 return;
452 }
453
454 std::optional<DisplayViewport> viewport =
455 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
456 if (!viewport) {
457 ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input "
458 "mapper. Fall back to default display",
459 mConfig.defaultPointerDisplayId);
460 viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT);
461 }
462 if (!viewport) {
463 ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to"
464 " PointerController.");
465 return;
466 }
467
468 controller->setDisplayViewport(*viewport);
469}
470
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471void InputReader::fadePointerLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100472 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800473 if (controller != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +0100474 controller->fade(PointerControllerInterface::Transition::GRADUAL);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 }
476}
477
478void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
479 if (when < mNextTimeout) {
480 mNextTimeout = when;
481 mEventHub->wake();
482 }
483}
484
485int32_t InputReader::bumpGenerationLocked() {
486 return ++mGeneration;
487}
488
Chris Ye98d3f532020-10-01 21:48:59 -0700489std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000490 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700491 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492}
493
Chris Ye98d3f532020-10-01 21:48:59 -0700494std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
495 std::vector<InputDeviceInfo> outInputDevices;
496 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497
Chris Yee7310032020-09-22 15:36:28 -0700498 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 if (!device->isIgnored()) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800500 InputDeviceInfo info;
501 device->getDeviceInfo(&info);
502 outInputDevices.push_back(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 }
504 }
Chris Ye98d3f532020-10-01 21:48:59 -0700505 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506}
507
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700508int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000509 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510
511 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
512}
513
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700514int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000515 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800516
517 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
518}
519
520int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000521 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522
523 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
524}
525
526int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700527 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528 int32_t result = AKEY_STATE_UNKNOWN;
529 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800530 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800531 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
532 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533 }
534 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000535 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000536 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700537 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
539 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000540 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800541 if (currentResult >= AKEY_STATE_DOWN) {
542 return currentResult;
543 } else if (currentResult == AKEY_STATE_UP) {
544 result = currentResult;
545 }
546 }
547 }
548 }
549 return result;
550}
551
Andrii Kulian763a3a42016-03-08 10:46:16 -0800552void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000553 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800554 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800555 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800556 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
557 return;
558 }
559
Andrii Kulian763a3a42016-03-08 10:46:16 -0800560 if (device->isIgnored()) {
561 return;
562 }
563
564 device->updateMetaState(AKEYCODE_CAPS_LOCK);
565}
566
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700567bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
568 const int32_t* keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000569 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570
571 memset(outFlags, 0, numCodes);
572 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
573}
574
575bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700576 size_t numCodes, const int32_t* keyCodes,
577 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 bool result = false;
579 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800580 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800581 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
582 result = device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800583 }
584 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000585 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000586 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700587 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
588 result |= device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 }
590 }
591 }
592 return result;
593}
594
595void InputReader::requestRefreshConfiguration(uint32_t changes) {
Chris Ye87143712020-11-10 05:05:58 +0000596 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597
598 if (changes) {
599 bool needWake = !mConfigurationChangesToRefresh;
600 mConfigurationChangesToRefresh |= changes;
601
602 if (needWake) {
603 mEventHub->wake();
604 }
605 }
606}
607
Chris Ye87143712020-11-10 05:05:58 +0000608void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
609 int32_t token) {
610 std::scoped_lock _l(mLock);
611
Chris Ye1c2e0892020-11-30 21:41:44 -0800612 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800613 if (device) {
Chris Ye87143712020-11-10 05:05:58 +0000614 device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615 }
616}
617
618void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000619 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800620
Chris Ye1c2e0892020-11-30 21:41:44 -0800621 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800622 if (device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623 device->cancelVibrate(token);
624 }
625}
626
Chris Ye87143712020-11-10 05:05:58 +0000627bool InputReader::isVibrating(int32_t deviceId) {
628 std::scoped_lock _l(mLock);
629
630 InputDevice* device = findInputDeviceLocked(deviceId);
631 if (device) {
632 return device->isVibrating();
633 }
634 return false;
635}
636
637std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
638 std::scoped_lock _l(mLock);
639
640 InputDevice* device = findInputDeviceLocked(deviceId);
641 if (device) {
642 return device->getVibratorIds();
643 }
644 return {};
645}
646
Chris Yef59a2f42020-10-16 12:55:26 -0700647void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
648 std::scoped_lock _l(mLock);
649
650 InputDevice* device = findInputDeviceLocked(deviceId);
651 if (device) {
652 device->disableSensor(sensorType);
653 }
654}
655
656bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
657 std::chrono::microseconds samplingPeriod,
658 std::chrono::microseconds maxBatchReportLatency) {
659 std::scoped_lock _l(mLock);
660
661 InputDevice* device = findInputDeviceLocked(deviceId);
662 if (device) {
663 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
664 }
665 return false;
666}
667
668void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
669 std::scoped_lock _l(mLock);
670
671 InputDevice* device = findInputDeviceLocked(deviceId);
672 if (device) {
673 device->flushSensor(sensorType);
674 }
675}
676
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700677bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000678 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700679
Chris Ye1c2e0892020-11-30 21:41:44 -0800680 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800681 if (device) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700682 return device->isEnabled();
683 }
684 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
685 return false;
686}
687
Arthur Hungc23540e2018-11-29 20:42:11 +0800688bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000689 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800690
Chris Ye1c2e0892020-11-30 21:41:44 -0800691 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800692 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800693 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
694 return false;
695 }
696
Arthur Hung2c9a3342019-07-23 14:18:59 +0800697 if (!device->isEnabled()) {
698 ALOGW("Ignoring disabled device %s", device->getName().c_str());
699 return false;
700 }
701
702 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800703 // No associated display. By default, can dispatch to all displays.
704 if (!associatedDisplayId) {
705 return true;
706 }
707
708 if (*associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hung2c9a3342019-07-23 14:18:59 +0800709 ALOGW("Device %s is associated with display ADISPLAY_ID_NONE.", device->getName().c_str());
Arthur Hungc23540e2018-11-29 20:42:11 +0800710 return true;
711 }
712
713 return *associatedDisplayId == displayId;
714}
715
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800716void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000717 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718
719 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800720 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721
Chris Yee7310032020-09-22 15:36:28 -0700722 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
723 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724
Chris Yee7310032020-09-22 15:36:28 -0700725 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
726 const std::shared_ptr<InputDevice>& device = devicePair.first;
727 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
728 for (const auto& eId : devicePair.second) {
729 eventHubDevStr += StringPrintf("%d ", eId);
730 }
731 eventHubDevStr += "] \n";
732 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733 }
734
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800735 dump += INDENT "Configuration:\n";
736 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
738 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800739 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100741 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800743 dump += "]\n";
744 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700745 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800746
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800747 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700748 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
749 "acceleration=%0.3f\n",
750 mConfig.pointerVelocityControlParameters.scale,
751 mConfig.pointerVelocityControlParameters.lowThreshold,
752 mConfig.pointerVelocityControlParameters.highThreshold,
753 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800755 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700756 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
757 "acceleration=%0.3f\n",
758 mConfig.wheelVelocityControlParameters.scale,
759 mConfig.wheelVelocityControlParameters.lowThreshold,
760 mConfig.wheelVelocityControlParameters.highThreshold,
761 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800763 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700764 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800765 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700766 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800767 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700768 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800769 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700770 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800771 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700772 mConfig.pointerGestureTapDragInterval * 0.000001f);
773 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800774 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700775 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800776 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700777 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800778 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700779 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800780 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700781 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800782 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700783 mConfig.pointerGestureMovementSpeedRatio);
784 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700785
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800786 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700787 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800788}
789
790void InputReader::monitor() {
791 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800792 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800794 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795 // Check the EventHub
796 mEventHub->monitor();
797}
798
Michael Wrightd02c5b62014-02-10 15:10:22 -0800799// --- InputReader::ContextImpl ---
800
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800801InputReader::ContextImpl::ContextImpl(InputReader* reader)
802 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803
804void InputReader::ContextImpl::updateGlobalMetaState() {
805 // lock is already held by the input loop
806 mReader->updateGlobalMetaStateLocked();
807}
808
809int32_t InputReader::ContextImpl::getGlobalMetaState() {
810 // lock is already held by the input loop
811 return mReader->getGlobalMetaStateLocked();
812}
813
arthurhungc903df12020-08-11 15:08:42 +0800814void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
815 // lock is already held by the input loop
816 mReader->updateLedMetaStateLocked(metaState);
817}
818
819int32_t InputReader::ContextImpl::getLedMetaState() {
820 // lock is already held by the input loop
821 return mReader->getLedMetaStateLocked();
822}
823
Michael Wrightd02c5b62014-02-10 15:10:22 -0800824void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
825 // lock is already held by the input loop
826 mReader->disableVirtualKeysUntilLocked(time);
827}
828
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800829bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
830 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800832 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833}
834
835void InputReader::ContextImpl::fadePointer() {
836 // lock is already held by the input loop
837 mReader->fadePointerLocked();
838}
839
Michael Wright17db18e2020-06-26 20:51:44 +0100840std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController(
841 int32_t deviceId) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800842 // lock is already held by the input loop
843 return mReader->getPointerControllerLocked(deviceId);
844}
845
Michael Wrightd02c5b62014-02-10 15:10:22 -0800846void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
847 // lock is already held by the input loop
848 mReader->requestTimeoutAtTimeLocked(when);
849}
850
851int32_t InputReader::ContextImpl::bumpGeneration() {
852 // lock is already held by the input loop
853 return mReader->bumpGenerationLocked();
854}
855
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800856void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700857 // lock is already held by whatever called refreshConfigurationLocked
858 mReader->getExternalStylusDevicesLocked(outDevices);
859}
860
861void InputReader::ContextImpl::dispatchExternalStylusState(const StylusState& state) {
862 mReader->dispatchExternalStylusState(state);
863}
864
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
866 return mReader->mPolicy.get();
867}
868
869InputListenerInterface* InputReader::ContextImpl::getListener() {
870 return mReader->mQueuedListener.get();
871}
872
873EventHubInterface* InputReader::ContextImpl::getEventHub() {
874 return mReader->mEventHub.get();
875}
876
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800877int32_t InputReader::ContextImpl::getNextId() {
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800878 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800879}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800880
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881} // namespace android