blob: 5120860503e55ea41adfd1e5d2334e6e0a3493d7 [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.
Chris Yee14523a2020-12-19 13:46:00 -0800225 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
226 // device class to disable SENSOR sub device only.
227 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700228 mEventHub->disableDevice(eventHubId);
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230}
231
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800232void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
233 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000234 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800235 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800236 return;
237 }
238
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000239 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000240 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700241 // Erase device from device to EventHub ids map.
242 auto mapIt = mDeviceToEventHubIdsMap.find(device);
243 if (mapIt != mDeviceToEventHubIdsMap.end()) {
244 std::vector<int32_t>& eventHubIds = mapIt->second;
245 eventHubIds.erase(std::remove_if(eventHubIds.begin(), eventHubIds.end(),
246 [eventHubId](int32_t eId) { return eId == eventHubId; }),
247 eventHubIds.end());
248 if (eventHubIds.size() == 0) {
249 mDeviceToEventHubIdsMap.erase(mapIt);
250 }
251 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252 bumpGenerationLocked();
253
254 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800255 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
256 "(ignored non-input device)",
257 device->getId(), eventHubId, device->getName().c_str(),
258 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800260 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=0x%08x",
261 device->getId(), eventHubId, device->getName().c_str(),
262 device->getDescriptor().c_str(), device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263 }
264
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800265 device->removeEventHubDevice(eventHubId);
266
Chris Ye1b0c7342020-07-28 21:57:03 -0700267 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800268 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700269 }
270
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800271 if (device->hasEventHubDevices()) {
272 device->configure(when, &mConfig, 0);
273 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274 device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275}
276
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000277std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800278 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
279 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
280 return devicePair.second->getDescriptor().size() && identifier.descriptor.size() &&
281 devicePair.second->getDescriptor() == identifier.descriptor;
282 });
283
284 std::shared_ptr<InputDevice> device;
285 if (deviceIt != mDevices.end()) {
286 device = deviceIt->second;
287 } else {
288 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
289 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
290 identifier);
291 }
292 device->addEventHubDevice(eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 return device;
294}
295
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800296void InputReader::processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700297 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800298 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000299 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800300 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800301 return;
302 }
303
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000304 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800305 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700306 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 return;
308 }
309
310 device->process(rawEvents, count);
311}
312
Chris Ye1c2e0892020-11-30 21:41:44 -0800313InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800314 auto deviceIt =
315 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
316 return devicePair.second->getId() == deviceId;
317 });
318 if (deviceIt != mDevices.end()) {
319 return deviceIt->second.get();
320 }
321 return nullptr;
322}
323
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324void InputReader::timeoutExpiredLocked(nsecs_t when) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000325 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000326 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 if (!device->isIgnored()) {
328 device->timeoutExpired(when);
329 }
330 }
331}
332
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800333int32_t InputReader::nextInputDeviceIdLocked() {
334 return ++mNextInputDeviceId;
335}
336
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
338 // Reset global meta state because it depends on the list of all configured devices.
339 updateGlobalMetaStateLocked();
340
341 // Enqueue configuration changed.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000342 NotifyConfigurationChangedArgs args(mContext.getNextId(), when);
343 mQueuedListener->notifyConfigurationChanged(&args);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344}
345
346void InputReader::refreshConfigurationLocked(uint32_t changes) {
347 mPolicy->getReaderConfiguration(&mConfig);
348 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
349
Prabir Pradhan7e186182020-11-10 13:56:45 -0800350 if (!changes) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351
Prabir Pradhan7e186182020-11-10 13:56:45 -0800352 ALOGI("Reconfiguring input devices, changes=%s",
353 InputReaderConfiguration::changesToString(changes).c_str());
354 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800355
Prabir Pradhan7e186182020-11-10 13:56:45 -0800356 if (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO) {
357 updatePointerDisplayLocked();
358 }
359
360 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
361 mEventHub->requestReopenDevices();
362 } else {
363 for (auto& devicePair : mDevices) {
364 std::shared_ptr<InputDevice>& device = devicePair.second;
365 device->configure(now, &mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366 }
367 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800368
369 if (changes & InputReaderConfiguration::CHANGE_POINTER_CAPTURE) {
Prabir Pradhanf192a102021-08-06 14:01:18 +0000370 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
371 ALOGV("Skipping notifying pointer capture changes: "
372 "There was no change in the pointer capture state.");
373 } else {
374 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
375 const NotifyPointerCaptureChangedArgs args(mContext.getNextId(), now,
376 mCurrentPointerCaptureRequest);
377 mQueuedListener->notifyPointerCaptureChanged(&args);
378 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800379 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800380}
381
382void InputReader::updateGlobalMetaStateLocked() {
383 mGlobalMetaState = 0;
384
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000385 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000386 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 mGlobalMetaState |= device->getMetaState();
388 }
389}
390
391int32_t InputReader::getGlobalMetaStateLocked() {
392 return mGlobalMetaState;
393}
394
arthurhungc903df12020-08-11 15:08:42 +0800395void InputReader::updateLedMetaStateLocked(int32_t metaState) {
396 mLedMetaState = metaState;
397 for (auto& devicePair : mDevices) {
398 std::shared_ptr<InputDevice>& device = devicePair.second;
399 device->updateLedState(false);
400 }
401}
402
403int32_t InputReader::getLedMetaStateLocked() {
404 return mLedMetaState;
405}
406
Chris Ye1c2e0892020-11-30 21:41:44 -0800407void InputReader::notifyExternalStylusPresenceChangedLocked() {
Michael Wright842500e2015-03-13 17:32:02 -0700408 refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
409}
410
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800411void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000412 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000413 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700414 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000415 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700416 }
417 }
418}
419
Arthur Hungcadce9d2021-05-07 17:24:04 +0800420void InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000421 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000422 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wright842500e2015-03-13 17:32:02 -0700423 device->updateExternalStylusState(state);
424 }
425}
426
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
428 mDisableVirtualKeysTimeout = time;
429}
430
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800431bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800433 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700434 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800435 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436 return true;
437 } else {
438 return false;
439 }
440}
441
Michael Wright17db18e2020-06-26 20:51:44 +0100442std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked(
443 int32_t deviceId) {
444 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800445 if (controller == nullptr) {
446 controller = mPolicy->obtainPointerController(deviceId);
447 mPointerController = controller;
448 updatePointerDisplayLocked();
449 }
450 return controller;
451}
452
453void InputReader::updatePointerDisplayLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100454 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800455 if (controller == nullptr) {
456 return;
457 }
458
459 std::optional<DisplayViewport> viewport =
460 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
461 if (!viewport) {
462 ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input "
463 "mapper. Fall back to default display",
464 mConfig.defaultPointerDisplayId);
465 viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT);
466 }
467 if (!viewport) {
468 ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to"
469 " PointerController.");
470 return;
471 }
472
473 controller->setDisplayViewport(*viewport);
474}
475
Michael Wrightd02c5b62014-02-10 15:10:22 -0800476void InputReader::fadePointerLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100477 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800478 if (controller != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +0100479 controller->fade(PointerControllerInterface::Transition::GRADUAL);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800480 }
481}
482
483void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
484 if (when < mNextTimeout) {
485 mNextTimeout = when;
486 mEventHub->wake();
487 }
488}
489
490int32_t InputReader::bumpGenerationLocked() {
491 return ++mGeneration;
492}
493
Chris Ye98d3f532020-10-01 21:48:59 -0700494std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000495 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700496 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497}
498
Chris Ye98d3f532020-10-01 21:48:59 -0700499std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
500 std::vector<InputDeviceInfo> outInputDevices;
501 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502
Chris Yee7310032020-09-22 15:36:28 -0700503 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000505 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506 }
507 }
Chris Ye98d3f532020-10-01 21:48:59 -0700508 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800509}
510
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700511int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000512 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513
514 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
515}
516
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700517int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000518 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519
520 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
521}
522
523int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000524 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525
526 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
527}
528
529int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700530 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531 int32_t result = AKEY_STATE_UNKNOWN;
532 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800533 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800534 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
535 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800536 }
537 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000538 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000539 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700540 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800541 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
542 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000543 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544 if (currentResult >= AKEY_STATE_DOWN) {
545 return currentResult;
546 } else if (currentResult == AKEY_STATE_UP) {
547 result = currentResult;
548 }
549 }
550 }
551 }
552 return result;
553}
554
Andrii Kulian763a3a42016-03-08 10:46:16 -0800555void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000556 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800557 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800558 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800559 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
560 return;
561 }
562
Andrii Kulian763a3a42016-03-08 10:46:16 -0800563 if (device->isIgnored()) {
564 return;
565 }
566
567 device->updateMetaState(AKEYCODE_CAPS_LOCK);
568}
569
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700570bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
571 const int32_t* keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000572 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573
574 memset(outFlags, 0, numCodes);
575 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
576}
577
578bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700579 size_t numCodes, const int32_t* keyCodes,
580 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 bool result = false;
582 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800583 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800584 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
585 result = device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586 }
587 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000588 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000589 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700590 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
591 result |= device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 }
593 }
594 }
595 return result;
596}
597
598void InputReader::requestRefreshConfiguration(uint32_t changes) {
Chris Ye87143712020-11-10 05:05:58 +0000599 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600
601 if (changes) {
602 bool needWake = !mConfigurationChangesToRefresh;
603 mConfigurationChangesToRefresh |= changes;
604
605 if (needWake) {
606 mEventHub->wake();
607 }
608 }
609}
610
Chris Ye87143712020-11-10 05:05:58 +0000611void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
612 int32_t token) {
613 std::scoped_lock _l(mLock);
614
Chris Ye1c2e0892020-11-30 21:41:44 -0800615 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800616 if (device) {
Chris Ye87143712020-11-10 05:05:58 +0000617 device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 }
619}
620
621void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000622 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623
Chris Ye1c2e0892020-11-30 21:41:44 -0800624 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800625 if (device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800626 device->cancelVibrate(token);
627 }
628}
629
Chris Ye87143712020-11-10 05:05:58 +0000630bool InputReader::isVibrating(int32_t deviceId) {
631 std::scoped_lock _l(mLock);
632
633 InputDevice* device = findInputDeviceLocked(deviceId);
634 if (device) {
635 return device->isVibrating();
636 }
637 return false;
638}
639
640std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
641 std::scoped_lock _l(mLock);
642
643 InputDevice* device = findInputDeviceLocked(deviceId);
644 if (device) {
645 return device->getVibratorIds();
646 }
647 return {};
648}
649
Chris Yef59a2f42020-10-16 12:55:26 -0700650void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
651 std::scoped_lock _l(mLock);
652
653 InputDevice* device = findInputDeviceLocked(deviceId);
654 if (device) {
655 device->disableSensor(sensorType);
656 }
657}
658
659bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
660 std::chrono::microseconds samplingPeriod,
661 std::chrono::microseconds maxBatchReportLatency) {
662 std::scoped_lock _l(mLock);
663
664 InputDevice* device = findInputDeviceLocked(deviceId);
665 if (device) {
666 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
667 }
668 return false;
669}
670
671void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
672 std::scoped_lock _l(mLock);
673
674 InputDevice* device = findInputDeviceLocked(deviceId);
675 if (device) {
676 device->flushSensor(sensorType);
677 }
678}
679
Kim Low03ea0352020-11-06 12:45:07 -0800680std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
681 std::scoped_lock _l(mLock);
682
683 InputDevice* device = findInputDeviceLocked(deviceId);
684 if (device) {
685 return device->getBatteryCapacity();
686 }
687 return std::nullopt;
688}
689
690std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
691 std::scoped_lock _l(mLock);
692
693 InputDevice* device = findInputDeviceLocked(deviceId);
694 if (device) {
695 return device->getBatteryStatus();
696 }
697 return std::nullopt;
698}
699
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000700std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800701 std::scoped_lock _l(mLock);
702
703 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000704 if (device == nullptr) {
705 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800706 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000707
708 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800709}
710
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000711std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800712 std::scoped_lock _l(mLock);
713
714 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000715 if (device == nullptr) {
716 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800717 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000718
719 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800720}
721
722bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
723 std::scoped_lock _l(mLock);
724
725 InputDevice* device = findInputDeviceLocked(deviceId);
726 if (device) {
727 return device->setLightColor(lightId, color);
728 }
729 return false;
730}
731
732bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
733 std::scoped_lock _l(mLock);
734
735 InputDevice* device = findInputDeviceLocked(deviceId);
736 if (device) {
737 return device->setLightPlayerId(lightId, playerId);
738 }
739 return false;
740}
741
742std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
743 std::scoped_lock _l(mLock);
744
745 InputDevice* device = findInputDeviceLocked(deviceId);
746 if (device) {
747 return device->getLightColor(lightId);
748 }
749 return std::nullopt;
750}
751
752std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
753 std::scoped_lock _l(mLock);
754
755 InputDevice* device = findInputDeviceLocked(deviceId);
756 if (device) {
757 return device->getLightPlayerId(lightId);
758 }
759 return std::nullopt;
760}
761
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700762bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000763 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700764
Chris Ye1c2e0892020-11-30 21:41:44 -0800765 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800766 if (device) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700767 return device->isEnabled();
768 }
769 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
770 return false;
771}
772
Arthur Hungc23540e2018-11-29 20:42:11 +0800773bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000774 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800775
Chris Ye1c2e0892020-11-30 21:41:44 -0800776 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800777 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800778 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
779 return false;
780 }
781
Arthur Hung2c9a3342019-07-23 14:18:59 +0800782 if (!device->isEnabled()) {
783 ALOGW("Ignoring disabled device %s", device->getName().c_str());
784 return false;
785 }
786
787 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800788 // No associated display. By default, can dispatch to all displays.
789 if (!associatedDisplayId) {
790 return true;
791 }
792
793 if (*associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hung2c9a3342019-07-23 14:18:59 +0800794 ALOGW("Device %s is associated with display ADISPLAY_ID_NONE.", device->getName().c_str());
Arthur Hungc23540e2018-11-29 20:42:11 +0800795 return true;
796 }
797
798 return *associatedDisplayId == displayId;
799}
800
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800801void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000802 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803
804 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800805 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800806
Chris Yee7310032020-09-22 15:36:28 -0700807 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
808 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809
Chris Yee7310032020-09-22 15:36:28 -0700810 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
811 const std::shared_ptr<InputDevice>& device = devicePair.first;
812 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
813 for (const auto& eId : devicePair.second) {
814 eventHubDevStr += StringPrintf("%d ", eId);
815 }
816 eventHubDevStr += "] \n";
817 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 }
819
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800820 dump += INDENT "Configuration:\n";
821 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800822 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
823 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800824 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100826 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800828 dump += "]\n";
829 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700830 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800832 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700833 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
834 "acceleration=%0.3f\n",
835 mConfig.pointerVelocityControlParameters.scale,
836 mConfig.pointerVelocityControlParameters.lowThreshold,
837 mConfig.pointerVelocityControlParameters.highThreshold,
838 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800840 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700841 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
842 "acceleration=%0.3f\n",
843 mConfig.wheelVelocityControlParameters.scale,
844 mConfig.wheelVelocityControlParameters.lowThreshold,
845 mConfig.wheelVelocityControlParameters.highThreshold,
846 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800848 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700849 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800850 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700851 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800852 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700853 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800854 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700855 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800856 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700857 mConfig.pointerGestureTapDragInterval * 0.000001f);
858 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800859 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700860 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800861 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700862 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800863 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700864 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800865 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700866 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800867 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700868 mConfig.pointerGestureMovementSpeedRatio);
869 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700870
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800871 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700872 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873}
874
875void InputReader::monitor() {
876 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800877 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800879 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800880 // Check the EventHub
881 mEventHub->monitor();
882}
883
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884// --- InputReader::ContextImpl ---
885
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800886InputReader::ContextImpl::ContextImpl(InputReader* reader)
887 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800888
889void InputReader::ContextImpl::updateGlobalMetaState() {
890 // lock is already held by the input loop
891 mReader->updateGlobalMetaStateLocked();
892}
893
894int32_t InputReader::ContextImpl::getGlobalMetaState() {
895 // lock is already held by the input loop
896 return mReader->getGlobalMetaStateLocked();
897}
898
arthurhungc903df12020-08-11 15:08:42 +0800899void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
900 // lock is already held by the input loop
901 mReader->updateLedMetaStateLocked(metaState);
902}
903
904int32_t InputReader::ContextImpl::getLedMetaState() {
905 // lock is already held by the input loop
906 return mReader->getLedMetaStateLocked();
907}
908
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
910 // lock is already held by the input loop
911 mReader->disableVirtualKeysUntilLocked(time);
912}
913
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800914bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
915 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800917 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918}
919
920void InputReader::ContextImpl::fadePointer() {
921 // lock is already held by the input loop
922 mReader->fadePointerLocked();
923}
924
Michael Wright17db18e2020-06-26 20:51:44 +0100925std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController(
926 int32_t deviceId) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800927 // lock is already held by the input loop
928 return mReader->getPointerControllerLocked(deviceId);
929}
930
Michael Wrightd02c5b62014-02-10 15:10:22 -0800931void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
932 // lock is already held by the input loop
933 mReader->requestTimeoutAtTimeLocked(when);
934}
935
936int32_t InputReader::ContextImpl::bumpGeneration() {
937 // lock is already held by the input loop
938 return mReader->bumpGenerationLocked();
939}
940
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800941void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700942 // lock is already held by whatever called refreshConfigurationLocked
943 mReader->getExternalStylusDevicesLocked(outDevices);
944}
945
946void InputReader::ContextImpl::dispatchExternalStylusState(const StylusState& state) {
Arthur Hungcadce9d2021-05-07 17:24:04 +0800947 mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -0700948}
949
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
951 return mReader->mPolicy.get();
952}
953
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000954InputListenerInterface* InputReader::ContextImpl::getListener() {
955 return mReader->mQueuedListener.get();
956}
957
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958EventHubInterface* InputReader::ContextImpl::getEventHub() {
959 return mReader->mEventHub.get();
960}
961
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000962int32_t InputReader::ContextImpl::getNextId() {
963 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800964}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966} // namespace android