blob: 929e100134d11c8307f6965c8be6466e4fca83ce [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070017#include "Macros.h"
Michael Wright842500e2015-03-13 17:32:02 -070018
Michael Wrightd02c5b62014-02-10 15:10:22 -080019#include "InputReader.h"
20
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080021#include <android-base/stringprintf.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070022#include <errno.h>
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080023#include <input/Keyboard.h>
24#include <input/VirtualKeyMap.h>
Michael Wright842500e2015-03-13 17:32:02 -070025#include <inttypes.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070026#include <limits.h>
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080027#include <log/log.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070028#include <math.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <stddef.h>
30#include <stdlib.h>
31#include <unistd.h>
Prabir Pradhan28efc192019-11-05 01:10:04 +000032#include <utils/Errors.h>
Prabir Pradhan28efc192019-11-05 01:10:04 +000033#include <utils/Thread.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080035#include "InputDevice.h"
36
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080037using android::base::StringPrintf;
38
Michael Wrightd02c5b62014-02-10 15:10:22 -080039namespace android {
40
Prabir Pradhan28efc192019-11-05 01:10:04 +000041// --- InputReader ---
42
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070043InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
44 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070045 InputListenerInterface& listener)
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070046 : mContext(this),
47 mEventHub(eventHub),
48 mPolicy(policy),
Siarhei Vishniakou18050092021-09-01 13:32:49 -070049 mQueuedListener(listener),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070050 mGlobalMetaState(0),
arthurhungc903df12020-08-11 15:08:42 +080051 mLedMetaState(AMETA_NUM_LOCK_ON),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070052 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080053 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070054 mDisableVirtualKeysTimeout(LLONG_MIN),
55 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -080056 mConfigurationChangesToRefresh(0) {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070057 refreshConfigurationLocked(0);
58 updateGlobalMetaStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -080059}
60
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +000061InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
Prabir Pradhan28efc192019-11-05 01:10:04 +000063status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070064 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000065 return ALREADY_EXISTS;
66 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070067 mThread = std::make_unique<InputThread>(
68 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
69 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +000070}
71
72status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070073 if (mThread && mThread->isCallingThread()) {
74 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +000075 return INVALID_OPERATION;
76 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070077 mThread.reset();
78 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +000079}
80
Michael Wrightd02c5b62014-02-10 15:10:22 -080081void InputReader::loopOnce() {
82 int32_t oldGeneration;
83 int32_t timeoutMillis;
84 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -080085 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -080086 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +000087 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -080088
89 oldGeneration = mGeneration;
90 timeoutMillis = -1;
91
92 uint32_t changes = mConfigurationChangesToRefresh;
93 if (changes) {
94 mConfigurationChangesToRefresh = 0;
95 timeoutMillis = 0;
96 refreshConfigurationLocked(changes);
97 } else if (mNextTimeout != LLONG_MAX) {
98 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
99 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
100 }
101 } // release lock
102
103 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
104
105 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000106 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800107 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800108
109 if (count) {
110 processEventsLocked(mEventBuffer, count);
111 }
112
113 if (mNextTimeout != LLONG_MAX) {
114 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
115 if (now >= mNextTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800116 if (DEBUG_RAW_EVENTS) {
117 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
118 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119 mNextTimeout = LLONG_MAX;
120 timeoutExpiredLocked(now);
121 }
122 }
123
124 if (oldGeneration != mGeneration) {
125 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800126 inputDevices = getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127 }
128 } // release lock
129
130 // Send out a message that the describes the changed input devices.
131 if (inputDevicesChanged) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800132 mPolicy->notifyInputDevicesChanged(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800133 }
134
135 // Flush queued events out to the listener.
136 // This must happen outside of the lock because the listener could potentially call
137 // back into the InputReader's methods, such as getScanCodeState, or become blocked
138 // on another thread similarly waiting to acquire the InputReader lock thereby
139 // resulting in a deadlock. This situation is actually quite plausible because the
140 // listener is actually the input dispatcher, which calls into the window manager,
141 // which occasionally calls into the input reader.
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700142 mQueuedListener.flush();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143}
144
145void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
146 for (const RawEvent* rawEvent = rawEvents; count;) {
147 int32_t type = rawEvent->type;
148 size_t batchSize = 1;
149 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
150 int32_t deviceId = rawEvent->deviceId;
151 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700152 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
153 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 break;
155 }
156 batchSize += 1;
157 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800158 if (DEBUG_RAW_EVENTS) {
159 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
160 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
162 } else {
163 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700164 case EventHubInterface::DEVICE_ADDED:
165 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
166 break;
167 case EventHubInterface::DEVICE_REMOVED:
168 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
169 break;
170 case EventHubInterface::FINISHED_DEVICE_SCAN:
171 handleConfigurationChangedLocked(rawEvent->when);
172 break;
173 default:
174 ALOG_ASSERT(false); // can't happen
175 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176 }
177 }
178 count -= batchSize;
179 rawEvent += batchSize;
180 }
181}
182
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800183void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
184 if (mDevices.find(eventHubId) != mDevices.end()) {
185 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 return;
187 }
188
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800189 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
190 std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191 device->configure(when, &mConfig, 0);
192 device->reset(when);
193
194 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800195 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
196 "(ignored non-input device)",
197 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800199 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=0x%08x",
200 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700201 device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202 }
203
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800204 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700205 // Add device to device to EventHub ids map.
206 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
207 if (mapIt == mDeviceToEventHubIdsMap.end()) {
208 std::vector<int32_t> ids = {eventHubId};
209 mDeviceToEventHubIdsMap.emplace(device, ids);
210 } else {
211 mapIt->second.push_back(eventHubId);
212 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700214
Chris Ye1b0c7342020-07-28 21:57:03 -0700215 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800216 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700217 }
Chris Yef59a2f42020-10-16 12:55:26 -0700218
219 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800220 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
221 // device class to disable SENSOR sub device only.
222 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700223 mEventHub->disableDevice(eventHubId);
224 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225}
226
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800227void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
228 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000229 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800230 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 return;
232 }
233
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000234 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000235 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700236 // Erase device from device to EventHub ids map.
237 auto mapIt = mDeviceToEventHubIdsMap.find(device);
238 if (mapIt != mDeviceToEventHubIdsMap.end()) {
239 std::vector<int32_t>& eventHubIds = mapIt->second;
240 eventHubIds.erase(std::remove_if(eventHubIds.begin(), eventHubIds.end(),
241 [eventHubId](int32_t eId) { return eId == eventHubId; }),
242 eventHubIds.end());
243 if (eventHubIds.size() == 0) {
244 mDeviceToEventHubIdsMap.erase(mapIt);
245 }
246 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247 bumpGenerationLocked();
248
249 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800250 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
251 "(ignored non-input device)",
252 device->getId(), eventHubId, device->getName().c_str(),
253 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800255 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=0x%08x",
256 device->getId(), eventHubId, device->getName().c_str(),
257 device->getDescriptor().c_str(), device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258 }
259
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800260 device->removeEventHubDevice(eventHubId);
261
Chris Ye1b0c7342020-07-28 21:57:03 -0700262 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800263 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700264 }
265
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800266 if (device->hasEventHubDevices()) {
267 device->configure(when, &mConfig, 0);
268 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270}
271
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000272std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800273 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
274 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
275 return devicePair.second->getDescriptor().size() && identifier.descriptor.size() &&
276 devicePair.second->getDescriptor() == identifier.descriptor;
277 });
278
279 std::shared_ptr<InputDevice> device;
280 if (deviceIt != mDevices.end()) {
281 device = deviceIt->second;
282 } else {
283 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
284 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
285 identifier);
286 }
287 device->addEventHubDevice(eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800288 return device;
289}
290
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800291void InputReader::processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700292 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800293 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000294 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800295 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 return;
297 }
298
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000299 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800300 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700301 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800302 return;
303 }
304
305 device->process(rawEvents, count);
306}
307
Chris Ye1c2e0892020-11-30 21:41:44 -0800308InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800309 auto deviceIt =
310 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
311 return devicePair.second->getId() == deviceId;
312 });
313 if (deviceIt != mDevices.end()) {
314 return deviceIt->second.get();
315 }
316 return nullptr;
317}
318
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319void InputReader::timeoutExpiredLocked(nsecs_t when) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000320 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000321 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 if (!device->isIgnored()) {
323 device->timeoutExpired(when);
324 }
325 }
326}
327
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800328int32_t InputReader::nextInputDeviceIdLocked() {
329 return ++mNextInputDeviceId;
330}
331
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
333 // Reset global meta state because it depends on the list of all configured devices.
334 updateGlobalMetaStateLocked();
335
336 // Enqueue configuration changed.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000337 NotifyConfigurationChangedArgs args(mContext.getNextId(), when);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700338 mQueuedListener.notifyConfigurationChanged(&args);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339}
340
341void InputReader::refreshConfigurationLocked(uint32_t changes) {
342 mPolicy->getReaderConfiguration(&mConfig);
343 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
344
Prabir Pradhan7e186182020-11-10 13:56:45 -0800345 if (!changes) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346
Prabir Pradhan7e186182020-11-10 13:56:45 -0800347 ALOGI("Reconfiguring input devices, changes=%s",
348 InputReaderConfiguration::changesToString(changes).c_str());
349 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800350
Prabir Pradhan7e186182020-11-10 13:56:45 -0800351 if (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO) {
352 updatePointerDisplayLocked();
353 }
354
355 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
356 mEventHub->requestReopenDevices();
357 } else {
358 for (auto& devicePair : mDevices) {
359 std::shared_ptr<InputDevice>& device = devicePair.second;
360 device->configure(now, &mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361 }
362 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800363
364 if (changes & InputReaderConfiguration::CHANGE_POINTER_CAPTURE) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000365 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
366 ALOGV("Skipping notifying pointer capture changes: "
367 "There was no change in the pointer capture state.");
368 } else {
369 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
370 const NotifyPointerCaptureChangedArgs args(mContext.getNextId(), now,
371 mCurrentPointerCaptureRequest);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700372 mQueuedListener.notifyPointerCaptureChanged(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000373 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800374 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375}
376
377void InputReader::updateGlobalMetaStateLocked() {
378 mGlobalMetaState = 0;
379
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000380 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000381 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 mGlobalMetaState |= device->getMetaState();
383 }
384}
385
386int32_t InputReader::getGlobalMetaStateLocked() {
387 return mGlobalMetaState;
388}
389
arthurhungc903df12020-08-11 15:08:42 +0800390void InputReader::updateLedMetaStateLocked(int32_t metaState) {
391 mLedMetaState = metaState;
392 for (auto& devicePair : mDevices) {
393 std::shared_ptr<InputDevice>& device = devicePair.second;
394 device->updateLedState(false);
395 }
396}
397
398int32_t InputReader::getLedMetaStateLocked() {
399 return mLedMetaState;
400}
401
Chris Ye1c2e0892020-11-30 21:41:44 -0800402void InputReader::notifyExternalStylusPresenceChangedLocked() {
Michael Wright842500e2015-03-13 17:32:02 -0700403 refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
404}
405
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800406void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000407 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000408 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700409 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000410 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700411 }
412 }
413}
414
Arthur Hungcadce9d2021-05-07 17:24:04 +0800415void InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000416 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000417 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wright842500e2015-03-13 17:32:02 -0700418 device->updateExternalStylusState(state);
419 }
420}
421
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
423 mDisableVirtualKeysTimeout = time;
424}
425
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800426bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800428 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700429 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800430 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431 return true;
432 } else {
433 return false;
434 }
435}
436
Michael Wright17db18e2020-06-26 20:51:44 +0100437std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked(
438 int32_t deviceId) {
439 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800440 if (controller == nullptr) {
441 controller = mPolicy->obtainPointerController(deviceId);
442 mPointerController = controller;
443 updatePointerDisplayLocked();
444 }
445 return controller;
446}
447
448void InputReader::updatePointerDisplayLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100449 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800450 if (controller == nullptr) {
451 return;
452 }
453
454 std::optional<DisplayViewport> viewport =
455 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
456 if (!viewport) {
457 ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input "
458 "mapper. Fall back to default display",
459 mConfig.defaultPointerDisplayId);
460 viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT);
461 }
462 if (!viewport) {
463 ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to"
464 " PointerController.");
465 return;
466 }
467
468 controller->setDisplayViewport(*viewport);
469}
470
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471void InputReader::fadePointerLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100472 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800473 if (controller != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +0100474 controller->fade(PointerControllerInterface::Transition::GRADUAL);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 }
476}
477
478void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
479 if (when < mNextTimeout) {
480 mNextTimeout = when;
481 mEventHub->wake();
482 }
483}
484
485int32_t InputReader::bumpGenerationLocked() {
486 return ++mGeneration;
487}
488
Chris Ye98d3f532020-10-01 21:48:59 -0700489std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000490 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700491 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492}
493
Chris Ye98d3f532020-10-01 21:48:59 -0700494std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
495 std::vector<InputDeviceInfo> outInputDevices;
496 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497
Chris Yee7310032020-09-22 15:36:28 -0700498 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000500 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 }
502 }
Chris Ye98d3f532020-10-01 21:48:59 -0700503 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504}
505
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700506int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000507 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800508
509 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
510}
511
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700512int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000513 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514
515 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
516}
517
518int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000519 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800520
521 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
522}
523
524int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700525 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526 int32_t result = AKEY_STATE_UNKNOWN;
527 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800528 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800529 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
530 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531 }
532 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000533 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000534 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700535 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800536 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
537 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000538 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 if (currentResult >= AKEY_STATE_DOWN) {
540 return currentResult;
541 } else if (currentResult == AKEY_STATE_UP) {
542 result = currentResult;
543 }
544 }
545 }
546 }
547 return result;
548}
549
Andrii Kulian763a3a42016-03-08 10:46:16 -0800550void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000551 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800552 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800553 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800554 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
555 return;
556 }
557
Andrii Kulian763a3a42016-03-08 10:46:16 -0800558 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000559 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800560 return;
561 }
562
563 device->updateMetaState(AKEYCODE_CAPS_LOCK);
564}
565
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700566bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
567 const int32_t* keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000568 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800569
570 memset(outFlags, 0, numCodes);
571 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
572}
573
574bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700575 size_t numCodes, const int32_t* keyCodes,
576 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 bool result = false;
578 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800579 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800580 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
581 result = device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582 }
583 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000584 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000585 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700586 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
587 result |= device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589 }
590 }
591 return result;
592}
593
594void InputReader::requestRefreshConfiguration(uint32_t changes) {
Chris Ye87143712020-11-10 05:05:58 +0000595 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596
597 if (changes) {
598 bool needWake = !mConfigurationChangesToRefresh;
599 mConfigurationChangesToRefresh |= changes;
600
601 if (needWake) {
602 mEventHub->wake();
603 }
604 }
605}
606
Chris Ye87143712020-11-10 05:05:58 +0000607void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
608 int32_t token) {
609 std::scoped_lock _l(mLock);
610
Chris Ye1c2e0892020-11-30 21:41:44 -0800611 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800612 if (device) {
Chris Ye87143712020-11-10 05:05:58 +0000613 device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614 }
615}
616
617void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000618 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619
Chris Ye1c2e0892020-11-30 21:41:44 -0800620 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 if (device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 device->cancelVibrate(token);
623 }
624}
625
Chris Ye87143712020-11-10 05:05:58 +0000626bool InputReader::isVibrating(int32_t deviceId) {
627 std::scoped_lock _l(mLock);
628
629 InputDevice* device = findInputDeviceLocked(deviceId);
630 if (device) {
631 return device->isVibrating();
632 }
633 return false;
634}
635
636std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
637 std::scoped_lock _l(mLock);
638
639 InputDevice* device = findInputDeviceLocked(deviceId);
640 if (device) {
641 return device->getVibratorIds();
642 }
643 return {};
644}
645
Chris Yef59a2f42020-10-16 12:55:26 -0700646void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
647 std::scoped_lock _l(mLock);
648
649 InputDevice* device = findInputDeviceLocked(deviceId);
650 if (device) {
651 device->disableSensor(sensorType);
652 }
653}
654
655bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
656 std::chrono::microseconds samplingPeriod,
657 std::chrono::microseconds maxBatchReportLatency) {
658 std::scoped_lock _l(mLock);
659
660 InputDevice* device = findInputDeviceLocked(deviceId);
661 if (device) {
662 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
663 }
664 return false;
665}
666
667void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
668 std::scoped_lock _l(mLock);
669
670 InputDevice* device = findInputDeviceLocked(deviceId);
671 if (device) {
672 device->flushSensor(sensorType);
673 }
674}
675
Kim Low03ea0352020-11-06 12:45:07 -0800676std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
677 std::scoped_lock _l(mLock);
678
679 InputDevice* device = findInputDeviceLocked(deviceId);
680 if (device) {
681 return device->getBatteryCapacity();
682 }
683 return std::nullopt;
684}
685
686std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
687 std::scoped_lock _l(mLock);
688
689 InputDevice* device = findInputDeviceLocked(deviceId);
690 if (device) {
691 return device->getBatteryStatus();
692 }
693 return std::nullopt;
694}
695
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000696std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800697 std::scoped_lock _l(mLock);
698
699 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000700 if (device == nullptr) {
701 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800702 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000703
704 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800705}
706
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000707std::vector<InputDeviceSensorInfo> InputReader::getSensors(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().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800716}
717
718bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
719 std::scoped_lock _l(mLock);
720
721 InputDevice* device = findInputDeviceLocked(deviceId);
722 if (device) {
723 return device->setLightColor(lightId, color);
724 }
725 return false;
726}
727
728bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
729 std::scoped_lock _l(mLock);
730
731 InputDevice* device = findInputDeviceLocked(deviceId);
732 if (device) {
733 return device->setLightPlayerId(lightId, playerId);
734 }
735 return false;
736}
737
738std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
739 std::scoped_lock _l(mLock);
740
741 InputDevice* device = findInputDeviceLocked(deviceId);
742 if (device) {
743 return device->getLightColor(lightId);
744 }
745 return std::nullopt;
746}
747
748std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
749 std::scoped_lock _l(mLock);
750
751 InputDevice* device = findInputDeviceLocked(deviceId);
752 if (device) {
753 return device->getLightPlayerId(lightId);
754 }
755 return std::nullopt;
756}
757
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700758bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000759 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700760
Chris Ye1c2e0892020-11-30 21:41:44 -0800761 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800762 if (device) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700763 return device->isEnabled();
764 }
765 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
766 return false;
767}
768
Arthur Hungc23540e2018-11-29 20:42:11 +0800769bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000770 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800771
Chris Ye1c2e0892020-11-30 21:41:44 -0800772 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800773 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800774 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
775 return false;
776 }
777
Arthur Hung2c9a3342019-07-23 14:18:59 +0800778 if (!device->isEnabled()) {
779 ALOGW("Ignoring disabled device %s", device->getName().c_str());
780 return false;
781 }
782
783 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800784 // No associated display. By default, can dispatch to all displays.
785 if (!associatedDisplayId) {
786 return true;
787 }
788
789 if (*associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hung2c9a3342019-07-23 14:18:59 +0800790 ALOGW("Device %s is associated with display ADISPLAY_ID_NONE.", device->getName().c_str());
Arthur Hungc23540e2018-11-29 20:42:11 +0800791 return true;
792 }
793
794 return *associatedDisplayId == displayId;
795}
796
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800797void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000798 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800799
800 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800801 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802
Chris Yee7310032020-09-22 15:36:28 -0700803 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
804 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805
Chris Yee7310032020-09-22 15:36:28 -0700806 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
807 const std::shared_ptr<InputDevice>& device = devicePair.first;
808 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
809 for (const auto& eId : devicePair.second) {
810 eventHubDevStr += StringPrintf("%d ", eId);
811 }
812 eventHubDevStr += "] \n";
813 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814 }
815
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800816 dump += INDENT "Configuration:\n";
817 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
819 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800820 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100822 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800824 dump += "]\n";
825 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700826 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800828 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700829 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
830 "acceleration=%0.3f\n",
831 mConfig.pointerVelocityControlParameters.scale,
832 mConfig.pointerVelocityControlParameters.lowThreshold,
833 mConfig.pointerVelocityControlParameters.highThreshold,
834 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800835
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800836 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700837 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
838 "acceleration=%0.3f\n",
839 mConfig.wheelVelocityControlParameters.scale,
840 mConfig.wheelVelocityControlParameters.lowThreshold,
841 mConfig.wheelVelocityControlParameters.highThreshold,
842 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800843
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800844 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700845 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800846 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700847 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800848 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700849 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800850 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700851 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800852 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700853 mConfig.pointerGestureTapDragInterval * 0.000001f);
854 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800855 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700856 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800857 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700858 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800859 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700860 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800861 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700862 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800863 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700864 mConfig.pointerGestureMovementSpeedRatio);
865 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700866
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800867 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700868 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869}
870
871void InputReader::monitor() {
872 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800873 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800875 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876 // Check the EventHub
877 mEventHub->monitor();
878}
879
Michael Wrightd02c5b62014-02-10 15:10:22 -0800880// --- InputReader::ContextImpl ---
881
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800882InputReader::ContextImpl::ContextImpl(InputReader* reader)
883 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884
885void InputReader::ContextImpl::updateGlobalMetaState() {
886 // lock is already held by the input loop
887 mReader->updateGlobalMetaStateLocked();
888}
889
890int32_t InputReader::ContextImpl::getGlobalMetaState() {
891 // lock is already held by the input loop
892 return mReader->getGlobalMetaStateLocked();
893}
894
arthurhungc903df12020-08-11 15:08:42 +0800895void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
896 // lock is already held by the input loop
897 mReader->updateLedMetaStateLocked(metaState);
898}
899
900int32_t InputReader::ContextImpl::getLedMetaState() {
901 // lock is already held by the input loop
902 return mReader->getLedMetaStateLocked();
903}
904
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
906 // lock is already held by the input loop
907 mReader->disableVirtualKeysUntilLocked(time);
908}
909
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800910bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
911 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800913 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914}
915
916void InputReader::ContextImpl::fadePointer() {
917 // lock is already held by the input loop
918 mReader->fadePointerLocked();
919}
920
Michael Wright17db18e2020-06-26 20:51:44 +0100921std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController(
922 int32_t deviceId) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800923 // lock is already held by the input loop
924 return mReader->getPointerControllerLocked(deviceId);
925}
926
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
928 // lock is already held by the input loop
929 mReader->requestTimeoutAtTimeLocked(when);
930}
931
932int32_t InputReader::ContextImpl::bumpGeneration() {
933 // lock is already held by the input loop
934 return mReader->bumpGenerationLocked();
935}
936
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800937void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700938 // lock is already held by whatever called refreshConfigurationLocked
939 mReader->getExternalStylusDevicesLocked(outDevices);
940}
941
942void InputReader::ContextImpl::dispatchExternalStylusState(const StylusState& state) {
Arthur Hungcadce9d2021-05-07 17:24:04 +0800943 mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -0700944}
945
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
947 return mReader->mPolicy.get();
948}
949
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700950InputListenerInterface& InputReader::ContextImpl::getListener() {
951 return mReader->mQueuedListener;
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000952}
953
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954EventHubInterface* InputReader::ContextImpl::getEventHub() {
955 return mReader->mEventHub.get();
956}
957
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000958int32_t InputReader::ContextImpl::getNextId() {
959 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800960}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961
Michael Wrightd02c5b62014-02-10 15:10:22 -0800962} // namespace android