blob: 9bcf463c36ef80ec92909050ae8def2ca4ddf8b7 [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),
Arthur Hung95f68612022-04-07 14:08:22 +080050 mGlobalMetaState(AMETA_NONE),
51 mLedMetaState(AMETA_NONE),
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 {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000199 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800200 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000201 inputEventSourceToString(device->getSources()).c_str());
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 {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000253 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800254 device->getId(), eventHubId, device->getName().c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000255 device->getDescriptor().c_str(),
256 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257 }
258
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800259 device->removeEventHubDevice(eventHubId);
260
Chris Ye1b0c7342020-07-28 21:57:03 -0700261 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800262 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700263 }
264
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800265 if (device->hasEventHubDevices()) {
266 device->configure(when, &mConfig, 0);
267 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269}
270
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000271std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800272 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
273 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
274 return devicePair.second->getDescriptor().size() && identifier.descriptor.size() &&
275 devicePair.second->getDescriptor() == identifier.descriptor;
276 });
277
278 std::shared_ptr<InputDevice> device;
279 if (deviceIt != mDevices.end()) {
280 device = deviceIt->second;
281 } else {
282 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
283 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
284 identifier);
285 }
286 device->addEventHubDevice(eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 return device;
288}
289
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800290void InputReader::processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700291 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800292 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000293 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800294 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 return;
296 }
297
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000298 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700300 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800301 return;
302 }
303
304 device->process(rawEvents, count);
305}
306
Philip Junker4af3b3d2021-12-14 10:36:55 +0100307InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800308 auto deviceIt =
309 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
310 return devicePair.second->getId() == deviceId;
311 });
312 if (deviceIt != mDevices.end()) {
313 return deviceIt->second.get();
314 }
315 return nullptr;
316}
317
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318void InputReader::timeoutExpiredLocked(nsecs_t when) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000319 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000320 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 if (!device->isIgnored()) {
322 device->timeoutExpired(when);
323 }
324 }
325}
326
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800327int32_t InputReader::nextInputDeviceIdLocked() {
328 return ++mNextInputDeviceId;
329}
330
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
332 // Reset global meta state because it depends on the list of all configured devices.
333 updateGlobalMetaStateLocked();
334
335 // Enqueue configuration changed.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000336 NotifyConfigurationChangedArgs args(mContext.getNextId(), when);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700337 mQueuedListener.notifyConfigurationChanged(&args);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338}
339
340void InputReader::refreshConfigurationLocked(uint32_t changes) {
341 mPolicy->getReaderConfiguration(&mConfig);
342 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
343
Prabir Pradhan7e186182020-11-10 13:56:45 -0800344 if (!changes) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345
Prabir Pradhan7e186182020-11-10 13:56:45 -0800346 ALOGI("Reconfiguring input devices, changes=%s",
347 InputReaderConfiguration::changesToString(changes).c_str());
348 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800349
Prabir Pradhan7e186182020-11-10 13:56:45 -0800350 if (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO) {
351 updatePointerDisplayLocked();
352 }
353
354 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
355 mEventHub->requestReopenDevices();
356 } else {
357 for (auto& devicePair : mDevices) {
358 std::shared_ptr<InputDevice>& device = devicePair.second;
359 device->configure(now, &mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800360 }
361 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800362
363 if (changes & InputReaderConfiguration::CHANGE_POINTER_CAPTURE) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000364 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
365 ALOGV("Skipping notifying pointer capture changes: "
366 "There was no change in the pointer capture state.");
367 } else {
368 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
369 const NotifyPointerCaptureChangedArgs args(mContext.getNextId(), now,
370 mCurrentPointerCaptureRequest);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700371 mQueuedListener.notifyPointerCaptureChanged(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000372 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800373 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374}
375
376void InputReader::updateGlobalMetaStateLocked() {
377 mGlobalMetaState = 0;
378
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000379 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000380 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381 mGlobalMetaState |= device->getMetaState();
382 }
383}
384
385int32_t InputReader::getGlobalMetaStateLocked() {
386 return mGlobalMetaState;
387}
388
arthurhungc903df12020-08-11 15:08:42 +0800389void InputReader::updateLedMetaStateLocked(int32_t metaState) {
390 mLedMetaState = metaState;
391 for (auto& devicePair : mDevices) {
392 std::shared_ptr<InputDevice>& device = devicePair.second;
393 device->updateLedState(false);
394 }
395}
396
397int32_t InputReader::getLedMetaStateLocked() {
398 return mLedMetaState;
399}
400
Chris Ye1c2e0892020-11-30 21:41:44 -0800401void InputReader::notifyExternalStylusPresenceChangedLocked() {
Michael Wright842500e2015-03-13 17:32:02 -0700402 refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
403}
404
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800405void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000406 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000407 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700408 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000409 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700410 }
411 }
412}
413
Arthur Hungcadce9d2021-05-07 17:24:04 +0800414void InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000415 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000416 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wright842500e2015-03-13 17:32:02 -0700417 device->updateExternalStylusState(state);
418 }
419}
420
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
422 mDisableVirtualKeysTimeout = time;
423}
424
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800425bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800426 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800427 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700428 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800429 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800430 return true;
431 } else {
432 return false;
433 }
434}
435
Michael Wright17db18e2020-06-26 20:51:44 +0100436std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked(
437 int32_t deviceId) {
438 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800439 if (controller == nullptr) {
440 controller = mPolicy->obtainPointerController(deviceId);
441 mPointerController = controller;
442 updatePointerDisplayLocked();
443 }
444 return controller;
445}
446
447void InputReader::updatePointerDisplayLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100448 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800449 if (controller == nullptr) {
450 return;
451 }
452
453 std::optional<DisplayViewport> viewport =
454 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
455 if (!viewport) {
456 ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input "
457 "mapper. Fall back to default display",
458 mConfig.defaultPointerDisplayId);
459 viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT);
460 }
461 if (!viewport) {
462 ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to"
463 " PointerController.");
464 return;
465 }
466
467 controller->setDisplayViewport(*viewport);
468}
469
Michael Wrightd02c5b62014-02-10 15:10:22 -0800470void InputReader::fadePointerLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100471 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800472 if (controller != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +0100473 controller->fade(PointerControllerInterface::Transition::GRADUAL);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474 }
475}
476
477void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
478 if (when < mNextTimeout) {
479 mNextTimeout = when;
480 mEventHub->wake();
481 }
482}
483
484int32_t InputReader::bumpGenerationLocked() {
485 return ++mGeneration;
486}
487
Chris Ye98d3f532020-10-01 21:48:59 -0700488std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000489 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700490 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491}
492
Chris Ye98d3f532020-10-01 21:48:59 -0700493std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
494 std::vector<InputDeviceInfo> outInputDevices;
495 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496
Chris Yee7310032020-09-22 15:36:28 -0700497 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000499 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800500 }
501 }
Chris Ye98d3f532020-10-01 21:48:59 -0700502 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503}
504
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700505int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000506 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507
508 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
509}
510
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700511int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
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, scanCode, &InputDevice::getScanCodeState);
515}
516
517int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
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, switchCode, &InputDevice::getSwitchState);
521}
522
523int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700524 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525 int32_t result = AKEY_STATE_UNKNOWN;
526 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800527 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800528 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
529 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800530 }
531 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000532 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000533 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700534 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
536 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000537 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538 if (currentResult >= AKEY_STATE_DOWN) {
539 return currentResult;
540 } else if (currentResult == AKEY_STATE_UP) {
541 result = currentResult;
542 }
543 }
544 }
545 }
546 return result;
547}
548
Andrii Kulian763a3a42016-03-08 10:46:16 -0800549void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000550 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800551 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800552 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800553 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
554 return;
555 }
556
Andrii Kulian763a3a42016-03-08 10:46:16 -0800557 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000558 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800559 return;
560 }
561
562 device->updateMetaState(AKEYCODE_CAPS_LOCK);
563}
564
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700565bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
566 const int32_t* keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000567 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568
569 memset(outFlags, 0, numCodes);
570 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
571}
572
573bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700574 size_t numCodes, const int32_t* keyCodes,
575 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 bool result = false;
577 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800578 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800579 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
580 result = device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 }
582 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000583 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000584 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700585 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
586 result |= device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587 }
588 }
589 }
590 return result;
591}
592
Philip Junker4af3b3d2021-12-14 10:36:55 +0100593int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
594 std::scoped_lock _l(mLock);
595
596 InputDevice* device = findInputDeviceLocked(deviceId);
597 if (device == nullptr) {
598 ALOGW("Failed to get key code for key location: Input device with id %d not found",
599 deviceId);
600 return AKEYCODE_UNKNOWN;
601 }
602 return device->getKeyCodeForKeyLocation(locationKeyCode);
603}
604
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605void InputReader::requestRefreshConfiguration(uint32_t changes) {
Chris Ye87143712020-11-10 05:05:58 +0000606 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800607
608 if (changes) {
609 bool needWake = !mConfigurationChangesToRefresh;
610 mConfigurationChangesToRefresh |= changes;
611
612 if (needWake) {
613 mEventHub->wake();
614 }
615 }
616}
617
Chris Ye87143712020-11-10 05:05:58 +0000618void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
619 int32_t token) {
620 std::scoped_lock _l(mLock);
621
Chris Ye1c2e0892020-11-30 21:41:44 -0800622 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800623 if (device) {
Chris Ye87143712020-11-10 05:05:58 +0000624 device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625 }
626}
627
628void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000629 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630
Chris Ye1c2e0892020-11-30 21:41:44 -0800631 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800632 if (device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800633 device->cancelVibrate(token);
634 }
635}
636
Chris Ye87143712020-11-10 05:05:58 +0000637bool InputReader::isVibrating(int32_t deviceId) {
638 std::scoped_lock _l(mLock);
639
640 InputDevice* device = findInputDeviceLocked(deviceId);
641 if (device) {
642 return device->isVibrating();
643 }
644 return false;
645}
646
647std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
648 std::scoped_lock _l(mLock);
649
650 InputDevice* device = findInputDeviceLocked(deviceId);
651 if (device) {
652 return device->getVibratorIds();
653 }
654 return {};
655}
656
Chris Yef59a2f42020-10-16 12:55:26 -0700657void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
658 std::scoped_lock _l(mLock);
659
660 InputDevice* device = findInputDeviceLocked(deviceId);
661 if (device) {
662 device->disableSensor(sensorType);
663 }
664}
665
666bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
667 std::chrono::microseconds samplingPeriod,
668 std::chrono::microseconds maxBatchReportLatency) {
669 std::scoped_lock _l(mLock);
670
671 InputDevice* device = findInputDeviceLocked(deviceId);
672 if (device) {
673 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
674 }
675 return false;
676}
677
678void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
679 std::scoped_lock _l(mLock);
680
681 InputDevice* device = findInputDeviceLocked(deviceId);
682 if (device) {
683 device->flushSensor(sensorType);
684 }
685}
686
Kim Low03ea0352020-11-06 12:45:07 -0800687std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
688 std::scoped_lock _l(mLock);
689
690 InputDevice* device = findInputDeviceLocked(deviceId);
691 if (device) {
692 return device->getBatteryCapacity();
693 }
694 return std::nullopt;
695}
696
697std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
698 std::scoped_lock _l(mLock);
699
700 InputDevice* device = findInputDeviceLocked(deviceId);
701 if (device) {
702 return device->getBatteryStatus();
703 }
704 return std::nullopt;
705}
706
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000707std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800708 std::scoped_lock _l(mLock);
709
710 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000711 if (device == nullptr) {
712 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800713 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000714
715 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800716}
717
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000718std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800719 std::scoped_lock _l(mLock);
720
721 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000722 if (device == nullptr) {
723 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800724 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000725
726 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800727}
728
729bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
730 std::scoped_lock _l(mLock);
731
732 InputDevice* device = findInputDeviceLocked(deviceId);
733 if (device) {
734 return device->setLightColor(lightId, color);
735 }
736 return false;
737}
738
739bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
740 std::scoped_lock _l(mLock);
741
742 InputDevice* device = findInputDeviceLocked(deviceId);
743 if (device) {
744 return device->setLightPlayerId(lightId, playerId);
745 }
746 return false;
747}
748
749std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
750 std::scoped_lock _l(mLock);
751
752 InputDevice* device = findInputDeviceLocked(deviceId);
753 if (device) {
754 return device->getLightColor(lightId);
755 }
756 return std::nullopt;
757}
758
759std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
760 std::scoped_lock _l(mLock);
761
762 InputDevice* device = findInputDeviceLocked(deviceId);
763 if (device) {
764 return device->getLightPlayerId(lightId);
765 }
766 return std::nullopt;
767}
768
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700769bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000770 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700771
Chris Ye1c2e0892020-11-30 21:41:44 -0800772 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800773 if (device) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700774 return device->isEnabled();
775 }
776 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
777 return false;
778}
779
Arthur Hungc23540e2018-11-29 20:42:11 +0800780bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000781 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800782
Chris Ye1c2e0892020-11-30 21:41:44 -0800783 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800784 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800785 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
786 return false;
787 }
788
Arthur Hung2c9a3342019-07-23 14:18:59 +0800789 if (!device->isEnabled()) {
790 ALOGW("Ignoring disabled device %s", device->getName().c_str());
791 return false;
792 }
793
794 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800795 // No associated display. By default, can dispatch to all displays.
Weilun Dud00847d2021-12-08 10:55:58 -0800796 if (!associatedDisplayId ||
797 *associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800798 return true;
799 }
800
801 return *associatedDisplayId == displayId;
802}
803
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800804void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000805 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800806
807 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800808 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809
Chris Yee7310032020-09-22 15:36:28 -0700810 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
811 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812
Chris Yee7310032020-09-22 15:36:28 -0700813 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
814 const std::shared_ptr<InputDevice>& device = devicePair.first;
815 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
816 for (const auto& eId : devicePair.second) {
817 eventHubDevStr += StringPrintf("%d ", eId);
818 }
819 eventHubDevStr += "] \n";
820 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 }
822
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800823 dump += INDENT "Configuration:\n";
824 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
826 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800827 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100829 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800830 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800831 dump += "]\n";
832 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700833 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800835 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700836 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
837 "acceleration=%0.3f\n",
838 mConfig.pointerVelocityControlParameters.scale,
839 mConfig.pointerVelocityControlParameters.lowThreshold,
840 mConfig.pointerVelocityControlParameters.highThreshold,
841 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800842
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800843 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700844 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
845 "acceleration=%0.3f\n",
846 mConfig.wheelVelocityControlParameters.scale,
847 mConfig.wheelVelocityControlParameters.lowThreshold,
848 mConfig.wheelVelocityControlParameters.highThreshold,
849 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800851 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700852 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800853 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700854 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800855 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700856 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800857 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700858 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800859 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700860 mConfig.pointerGestureTapDragInterval * 0.000001f);
861 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800862 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700863 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800864 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700865 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800866 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700867 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800868 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700869 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800870 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700871 mConfig.pointerGestureMovementSpeedRatio);
872 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700873
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800874 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700875 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876}
877
878void InputReader::monitor() {
879 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800880 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800882 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800883 // Check the EventHub
884 mEventHub->monitor();
885}
886
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887// --- InputReader::ContextImpl ---
888
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800889InputReader::ContextImpl::ContextImpl(InputReader* reader)
890 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800891
892void InputReader::ContextImpl::updateGlobalMetaState() {
893 // lock is already held by the input loop
894 mReader->updateGlobalMetaStateLocked();
895}
896
897int32_t InputReader::ContextImpl::getGlobalMetaState() {
898 // lock is already held by the input loop
899 return mReader->getGlobalMetaStateLocked();
900}
901
arthurhungc903df12020-08-11 15:08:42 +0800902void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
903 // lock is already held by the input loop
904 mReader->updateLedMetaStateLocked(metaState);
905}
906
907int32_t InputReader::ContextImpl::getLedMetaState() {
908 // lock is already held by the input loop
909 return mReader->getLedMetaStateLocked();
910}
911
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
913 // lock is already held by the input loop
914 mReader->disableVirtualKeysUntilLocked(time);
915}
916
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800917bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
918 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800919 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800920 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921}
922
923void InputReader::ContextImpl::fadePointer() {
924 // lock is already held by the input loop
925 mReader->fadePointerLocked();
926}
927
Michael Wright17db18e2020-06-26 20:51:44 +0100928std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController(
929 int32_t deviceId) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800930 // lock is already held by the input loop
931 return mReader->getPointerControllerLocked(deviceId);
932}
933
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
935 // lock is already held by the input loop
936 mReader->requestTimeoutAtTimeLocked(when);
937}
938
939int32_t InputReader::ContextImpl::bumpGeneration() {
940 // lock is already held by the input loop
941 return mReader->bumpGenerationLocked();
942}
943
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800944void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700945 // lock is already held by whatever called refreshConfigurationLocked
946 mReader->getExternalStylusDevicesLocked(outDevices);
947}
948
949void InputReader::ContextImpl::dispatchExternalStylusState(const StylusState& state) {
Arthur Hungcadce9d2021-05-07 17:24:04 +0800950 mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -0700951}
952
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
954 return mReader->mPolicy.get();
955}
956
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700957InputListenerInterface& InputReader::ContextImpl::getListener() {
958 return mReader->mQueuedListener;
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000959}
960
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961EventHubInterface* InputReader::ContextImpl::getEventHub() {
962 return mReader->mEventHub.get();
963}
964
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000965int32_t InputReader::ContextImpl::getNextId() {
966 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800967}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969} // namespace android