blob: e9a82b4d4d919ccb8f2fe16a162a27fcd3448186 [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
Josh Bartel938632f2022-07-19 15:34:22 -050041/**
42 * Determines if the identifiers passed are a sub-devices. Sub-devices are physical devices
43 * that expose multiple input device paths such a keyboard that also has a touchpad input.
44 * These are separate devices with unique descriptors in EventHub, but InputReader should
45 * create a single InputDevice for them.
46 * Sub-devices are detected by the following criteria:
47 * 1. The vendor, product, bus, version, and unique id match
48 * 2. The location matches. The location is used to distinguish a single device with multiple
49 * inputs versus the same device plugged into multiple ports.
50 */
51
52static bool isSubDevice(const InputDeviceIdentifier& identifier1,
53 const InputDeviceIdentifier& identifier2) {
54 return (identifier1.vendor == identifier2.vendor &&
55 identifier1.product == identifier2.product && identifier1.bus == identifier2.bus &&
56 identifier1.version == identifier2.version &&
57 identifier1.uniqueId == identifier2.uniqueId &&
58 identifier1.location == identifier2.location);
59}
60
Prabir Pradhan28efc192019-11-05 01:10:04 +000061// --- InputReader ---
62
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070063InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
64 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070065 InputListenerInterface& listener)
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070066 : mContext(this),
67 mEventHub(eventHub),
68 mPolicy(policy),
Siarhei Vishniakou18050092021-09-01 13:32:49 -070069 mQueuedListener(listener),
Arthur Hung95f68612022-04-07 14:08:22 +080070 mGlobalMetaState(AMETA_NONE),
71 mLedMetaState(AMETA_NONE),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070072 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080073 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070074 mDisableVirtualKeysTimeout(LLONG_MIN),
75 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -080076 mConfigurationChangesToRefresh(0) {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070077 refreshConfigurationLocked(0);
78 updateGlobalMetaStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -080079}
80
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +000081InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080082
Prabir Pradhan28efc192019-11-05 01:10:04 +000083status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070084 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000085 return ALREADY_EXISTS;
86 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070087 mThread = std::make_unique<InputThread>(
88 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
89 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +000090}
91
92status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070093 if (mThread && mThread->isCallingThread()) {
94 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +000095 return INVALID_OPERATION;
96 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070097 mThread.reset();
98 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +000099}
100
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101void InputReader::loopOnce() {
102 int32_t oldGeneration;
103 int32_t timeoutMillis;
104 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -0800105 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000107 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800108
109 oldGeneration = mGeneration;
110 timeoutMillis = -1;
111
112 uint32_t changes = mConfigurationChangesToRefresh;
113 if (changes) {
114 mConfigurationChangesToRefresh = 0;
115 timeoutMillis = 0;
116 refreshConfigurationLocked(changes);
117 } else if (mNextTimeout != LLONG_MAX) {
118 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
119 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
120 }
121 } // release lock
122
123 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
124
125 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000126 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800127 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128
129 if (count) {
130 processEventsLocked(mEventBuffer, count);
131 }
132
133 if (mNextTimeout != LLONG_MAX) {
134 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
135 if (now >= mNextTimeout) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800136 if (DEBUG_RAW_EVENTS) {
137 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
138 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139 mNextTimeout = LLONG_MAX;
140 timeoutExpiredLocked(now);
141 }
142 }
143
144 if (oldGeneration != mGeneration) {
145 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800146 inputDevices = getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147 }
148 } // release lock
149
150 // Send out a message that the describes the changed input devices.
151 if (inputDevicesChanged) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800152 mPolicy->notifyInputDevicesChanged(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153 }
154
155 // Flush queued events out to the listener.
156 // This must happen outside of the lock because the listener could potentially call
157 // back into the InputReader's methods, such as getScanCodeState, or become blocked
158 // on another thread similarly waiting to acquire the InputReader lock thereby
159 // resulting in a deadlock. This situation is actually quite plausible because the
160 // listener is actually the input dispatcher, which calls into the window manager,
161 // which occasionally calls into the input reader.
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700162 mQueuedListener.flush();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163}
164
165void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
166 for (const RawEvent* rawEvent = rawEvents; count;) {
167 int32_t type = rawEvent->type;
168 size_t batchSize = 1;
169 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
170 int32_t deviceId = rawEvent->deviceId;
171 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700172 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
173 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174 break;
175 }
176 batchSize += 1;
177 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800178 if (DEBUG_RAW_EVENTS) {
179 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
180 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
182 } else {
183 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700184 case EventHubInterface::DEVICE_ADDED:
185 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
186 break;
187 case EventHubInterface::DEVICE_REMOVED:
188 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
189 break;
190 case EventHubInterface::FINISHED_DEVICE_SCAN:
191 handleConfigurationChangedLocked(rawEvent->when);
192 break;
193 default:
194 ALOG_ASSERT(false); // can't happen
195 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 }
197 }
198 count -= batchSize;
199 rawEvent += batchSize;
200 }
201}
202
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800203void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
204 if (mDevices.find(eventHubId) != mDevices.end()) {
205 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206 return;
207 }
208
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800209 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
210 std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211 device->configure(when, &mConfig, 0);
212 device->reset(when);
213
214 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800215 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
216 "(ignored non-input device)",
217 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800219 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=0x%08x",
220 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700221 device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800222 }
223
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800224 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700225 // Add device to device to EventHub ids map.
226 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
227 if (mapIt == mDeviceToEventHubIdsMap.end()) {
228 std::vector<int32_t> ids = {eventHubId};
229 mDeviceToEventHubIdsMap.emplace(device, ids);
230 } else {
231 mapIt->second.push_back(eventHubId);
232 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800233 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700234
Chris Ye1b0c7342020-07-28 21:57:03 -0700235 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800236 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700237 }
Chris Yef59a2f42020-10-16 12:55:26 -0700238
239 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800240 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
241 // device class to disable SENSOR sub device only.
242 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700243 mEventHub->disableDevice(eventHubId);
244 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245}
246
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800247void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
248 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000249 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800250 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800251 return;
252 }
253
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000254 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000255 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700256 // Erase device from device to EventHub ids map.
257 auto mapIt = mDeviceToEventHubIdsMap.find(device);
258 if (mapIt != mDeviceToEventHubIdsMap.end()) {
259 std::vector<int32_t>& eventHubIds = mapIt->second;
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -0800260 std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
Chris Yee7310032020-09-22 15:36:28 -0700261 if (eventHubIds.size() == 0) {
262 mDeviceToEventHubIdsMap.erase(mapIt);
263 }
264 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265 bumpGenerationLocked();
266
267 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800268 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
269 "(ignored non-input device)",
270 device->getId(), eventHubId, device->getName().c_str(),
271 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800273 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=0x%08x",
274 device->getId(), eventHubId, device->getName().c_str(),
275 device->getDescriptor().c_str(), device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276 }
277
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800278 device->removeEventHubDevice(eventHubId);
279
Chris Ye1b0c7342020-07-28 21:57:03 -0700280 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800281 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700282 }
283
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800284 if (device->hasEventHubDevices()) {
285 device->configure(when, &mConfig, 0);
286 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 device->reset(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800288}
289
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000290std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800291 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
292 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
Josh Bartel938632f2022-07-19 15:34:22 -0500293 const InputDeviceIdentifier identifier2 =
294 devicePair.second->getDeviceInfo().getIdentifier();
295 return isSubDevice(identifier, identifier2);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800296 });
297
298 std::shared_ptr<InputDevice> device;
299 if (deviceIt != mDevices.end()) {
300 device = deviceIt->second;
301 } else {
302 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
303 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
304 identifier);
305 }
306 device->addEventHubDevice(eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 return device;
308}
309
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800310void InputReader::processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700311 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800312 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000313 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800314 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800315 return;
316 }
317
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000318 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700320 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 return;
322 }
323
324 device->process(rawEvents, count);
325}
326
Philip Junker4af3b3d2021-12-14 10:36:55 +0100327InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800328 auto deviceIt =
329 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
330 return devicePair.second->getId() == deviceId;
331 });
332 if (deviceIt != mDevices.end()) {
333 return deviceIt->second.get();
334 }
335 return nullptr;
336}
337
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338void InputReader::timeoutExpiredLocked(nsecs_t when) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000339 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000340 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 if (!device->isIgnored()) {
342 device->timeoutExpired(when);
343 }
344 }
345}
346
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800347int32_t InputReader::nextInputDeviceIdLocked() {
348 return ++mNextInputDeviceId;
349}
350
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
352 // Reset global meta state because it depends on the list of all configured devices.
353 updateGlobalMetaStateLocked();
354
355 // Enqueue configuration changed.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000356 NotifyConfigurationChangedArgs args(mContext.getNextId(), when);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700357 mQueuedListener.notifyConfigurationChanged(&args);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358}
359
360void InputReader::refreshConfigurationLocked(uint32_t changes) {
361 mPolicy->getReaderConfiguration(&mConfig);
362 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
363
Prabir Pradhan7e186182020-11-10 13:56:45 -0800364 if (!changes) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365
Prabir Pradhan7e186182020-11-10 13:56:45 -0800366 ALOGI("Reconfiguring input devices, changes=%s",
367 InputReaderConfiguration::changesToString(changes).c_str());
368 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800369
Prabir Pradhan7e186182020-11-10 13:56:45 -0800370 if (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO) {
371 updatePointerDisplayLocked();
372 }
373
374 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
375 mEventHub->requestReopenDevices();
376 } else {
377 for (auto& devicePair : mDevices) {
378 std::shared_ptr<InputDevice>& device = devicePair.second;
379 device->configure(now, &mConfig, changes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800380 }
381 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800382
383 if (changes & InputReaderConfiguration::CHANGE_POINTER_CAPTURE) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000384 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
385 ALOGV("Skipping notifying pointer capture changes: "
386 "There was no change in the pointer capture state.");
387 } else {
388 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
389 const NotifyPointerCaptureChangedArgs args(mContext.getNextId(), now,
390 mCurrentPointerCaptureRequest);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700391 mQueuedListener.notifyPointerCaptureChanged(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000392 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800393 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394}
395
396void InputReader::updateGlobalMetaStateLocked() {
397 mGlobalMetaState = 0;
398
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000399 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000400 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 mGlobalMetaState |= device->getMetaState();
402 }
403}
404
405int32_t InputReader::getGlobalMetaStateLocked() {
406 return mGlobalMetaState;
407}
408
arthurhungc903df12020-08-11 15:08:42 +0800409void InputReader::updateLedMetaStateLocked(int32_t metaState) {
410 mLedMetaState = metaState;
411 for (auto& devicePair : mDevices) {
412 std::shared_ptr<InputDevice>& device = devicePair.second;
413 device->updateLedState(false);
414 }
415}
416
417int32_t InputReader::getLedMetaStateLocked() {
418 return mLedMetaState;
419}
420
Chris Ye1c2e0892020-11-30 21:41:44 -0800421void InputReader::notifyExternalStylusPresenceChangedLocked() {
Michael Wright842500e2015-03-13 17:32:02 -0700422 refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
423}
424
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800425void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000426 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000427 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700428 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000429 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700430 }
431 }
432}
433
Arthur Hungcadce9d2021-05-07 17:24:04 +0800434void InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000435 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000436 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wright842500e2015-03-13 17:32:02 -0700437 device->updateExternalStylusState(state);
438 }
439}
440
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
442 mDisableVirtualKeysTimeout = time;
443}
444
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800445bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800446 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800447 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700448 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800449 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800450 return true;
451 } else {
452 return false;
453 }
454}
455
Michael Wright17db18e2020-06-26 20:51:44 +0100456std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked(
457 int32_t deviceId) {
458 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800459 if (controller == nullptr) {
460 controller = mPolicy->obtainPointerController(deviceId);
461 mPointerController = controller;
462 updatePointerDisplayLocked();
463 }
464 return controller;
465}
466
467void InputReader::updatePointerDisplayLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100468 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800469 if (controller == nullptr) {
470 return;
471 }
472
473 std::optional<DisplayViewport> viewport =
474 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
475 if (!viewport) {
476 ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input "
477 "mapper. Fall back to default display",
478 mConfig.defaultPointerDisplayId);
479 viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT);
480 }
481 if (!viewport) {
482 ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to"
483 " PointerController.");
484 return;
485 }
486
487 controller->setDisplayViewport(*viewport);
488}
489
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490void InputReader::fadePointerLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100491 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800492 if (controller != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +0100493 controller->fade(PointerControllerInterface::Transition::GRADUAL);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 }
495}
496
497void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
498 if (when < mNextTimeout) {
499 mNextTimeout = when;
500 mEventHub->wake();
501 }
502}
503
504int32_t InputReader::bumpGenerationLocked() {
505 return ++mGeneration;
506}
507
Chris Ye98d3f532020-10-01 21:48:59 -0700508std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000509 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700510 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511}
512
Chris Ye98d3f532020-10-01 21:48:59 -0700513std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
514 std::vector<InputDeviceInfo> outInputDevices;
515 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800516
Chris Yee7310032020-09-22 15:36:28 -0700517 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000519 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800520 }
521 }
Chris Ye98d3f532020-10-01 21:48:59 -0700522 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800523}
524
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700525int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000526 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800527
528 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
529}
530
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700531int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000532 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533
534 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
535}
536
537int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000538 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539
540 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
541}
542
543int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700544 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 int32_t result = AKEY_STATE_UNKNOWN;
546 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800547 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800548 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
549 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 }
551 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000552 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000553 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700554 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
556 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000557 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558 if (currentResult >= AKEY_STATE_DOWN) {
559 return currentResult;
560 } else if (currentResult == AKEY_STATE_UP) {
561 result = currentResult;
562 }
563 }
564 }
565 }
566 return result;
567}
568
Andrii Kulian763a3a42016-03-08 10:46:16 -0800569void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000570 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800571 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800572 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800573 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
574 return;
575 }
576
Andrii Kulian763a3a42016-03-08 10:46:16 -0800577 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000578 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800579 return;
580 }
581
582 device->updateMetaState(AKEYCODE_CAPS_LOCK);
583}
584
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700585bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
586 const int32_t* keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000587 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588
589 memset(outFlags, 0, numCodes);
590 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
591}
592
593bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700594 size_t numCodes, const int32_t* keyCodes,
595 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 bool result = false;
597 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800598 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800599 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
600 result = device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 }
602 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000603 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000604 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700605 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
606 result |= device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800607 }
608 }
609 }
610 return result;
611}
612
Philip Junker4af3b3d2021-12-14 10:36:55 +0100613int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
614 std::scoped_lock _l(mLock);
615
616 InputDevice* device = findInputDeviceLocked(deviceId);
617 if (device == nullptr) {
618 ALOGW("Failed to get key code for key location: Input device with id %d not found",
619 deviceId);
620 return AKEYCODE_UNKNOWN;
621 }
622 return device->getKeyCodeForKeyLocation(locationKeyCode);
623}
624
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625void InputReader::requestRefreshConfiguration(uint32_t changes) {
Chris Ye87143712020-11-10 05:05:58 +0000626 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627
628 if (changes) {
629 bool needWake = !mConfigurationChangesToRefresh;
630 mConfigurationChangesToRefresh |= changes;
631
632 if (needWake) {
633 mEventHub->wake();
634 }
635 }
636}
637
Chris Ye87143712020-11-10 05:05:58 +0000638void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
639 int32_t token) {
640 std::scoped_lock _l(mLock);
641
Chris Ye1c2e0892020-11-30 21:41:44 -0800642 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800643 if (device) {
Chris Ye87143712020-11-10 05:05:58 +0000644 device->vibrate(sequence, repeat, token);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645 }
646}
647
648void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000649 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800650
Chris Ye1c2e0892020-11-30 21:41:44 -0800651 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800652 if (device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800653 device->cancelVibrate(token);
654 }
655}
656
Chris Ye87143712020-11-10 05:05:58 +0000657bool InputReader::isVibrating(int32_t deviceId) {
658 std::scoped_lock _l(mLock);
659
660 InputDevice* device = findInputDeviceLocked(deviceId);
661 if (device) {
662 return device->isVibrating();
663 }
664 return false;
665}
666
667std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
668 std::scoped_lock _l(mLock);
669
670 InputDevice* device = findInputDeviceLocked(deviceId);
671 if (device) {
672 return device->getVibratorIds();
673 }
674 return {};
675}
676
Chris Yef59a2f42020-10-16 12:55:26 -0700677void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
678 std::scoped_lock _l(mLock);
679
680 InputDevice* device = findInputDeviceLocked(deviceId);
681 if (device) {
682 device->disableSensor(sensorType);
683 }
684}
685
686bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
687 std::chrono::microseconds samplingPeriod,
688 std::chrono::microseconds maxBatchReportLatency) {
689 std::scoped_lock _l(mLock);
690
691 InputDevice* device = findInputDeviceLocked(deviceId);
692 if (device) {
693 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
694 }
695 return false;
696}
697
698void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
699 std::scoped_lock _l(mLock);
700
701 InputDevice* device = findInputDeviceLocked(deviceId);
702 if (device) {
703 device->flushSensor(sensorType);
704 }
705}
706
Kim Low03ea0352020-11-06 12:45:07 -0800707std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
Andy Chen22c330c2022-08-29 20:07:10 -0400708 std::optional<int32_t> eventHubId;
709 {
710 // Do not query the battery state while holding the lock. For some peripheral devices,
711 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
712 // would block all other event processing during this time. For now, we assume this
713 // call never happens on the InputReader thread and get the battery state outside the
714 // lock to prevent event processing from being blocked by this call.
715 std::scoped_lock _l(mLock);
716 InputDevice* device = findInputDeviceLocked(deviceId);
717 if (!device) return {};
718 eventHubId = device->getBatteryEventHubId();
719 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800720
Andy Chen22c330c2022-08-29 20:07:10 -0400721 if (!eventHubId) return {};
722 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
723 if (batteryIds.empty()) return {};
724 return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800725}
726
727std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
Andy Chen22c330c2022-08-29 20:07:10 -0400728 std::optional<int32_t> eventHubId;
729 {
730 // Do not query the battery state while holding the lock. For some peripheral devices,
731 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
732 // would block all other event processing during this time. For now, we assume this
733 // call never happens on the InputReader thread and get the battery state outside the
734 // lock to prevent event processing from being blocked by this call.
735 std::scoped_lock _l(mLock);
736 InputDevice* device = findInputDeviceLocked(deviceId);
737 if (!device) return {};
738 eventHubId = device->getBatteryEventHubId();
739 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800740
Andy Chen22c330c2022-08-29 20:07:10 -0400741 if (!eventHubId) return {};
742 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
743 if (batteryIds.empty()) return {};
744 return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800745}
746
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000747std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800748 std::scoped_lock _l(mLock);
749
750 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000751 if (device == nullptr) {
752 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800753 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000754
755 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800756}
757
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000758std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800759 std::scoped_lock _l(mLock);
760
761 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000762 if (device == nullptr) {
763 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800764 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000765
766 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800767}
768
769bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
770 std::scoped_lock _l(mLock);
771
772 InputDevice* device = findInputDeviceLocked(deviceId);
773 if (device) {
774 return device->setLightColor(lightId, color);
775 }
776 return false;
777}
778
779bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
780 std::scoped_lock _l(mLock);
781
782 InputDevice* device = findInputDeviceLocked(deviceId);
783 if (device) {
784 return device->setLightPlayerId(lightId, playerId);
785 }
786 return false;
787}
788
789std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
790 std::scoped_lock _l(mLock);
791
792 InputDevice* device = findInputDeviceLocked(deviceId);
793 if (device) {
794 return device->getLightColor(lightId);
795 }
796 return std::nullopt;
797}
798
799std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
800 std::scoped_lock _l(mLock);
801
802 InputDevice* device = findInputDeviceLocked(deviceId);
803 if (device) {
804 return device->getLightPlayerId(lightId);
805 }
806 return std::nullopt;
807}
808
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700809bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000810 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700811
Chris Ye1c2e0892020-11-30 21:41:44 -0800812 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800813 if (device) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700814 return device->isEnabled();
815 }
816 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
817 return false;
818}
819
Arthur Hungc23540e2018-11-29 20:42:11 +0800820bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000821 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800822
Chris Ye1c2e0892020-11-30 21:41:44 -0800823 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800824 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800825 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
826 return false;
827 }
828
Arthur Hung2c9a3342019-07-23 14:18:59 +0800829 if (!device->isEnabled()) {
830 ALOGW("Ignoring disabled device %s", device->getName().c_str());
831 return false;
832 }
833
834 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800835 // No associated display. By default, can dispatch to all displays.
Weilun Dud00847d2021-12-08 10:55:58 -0800836 if (!associatedDisplayId ||
837 *associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800838 return true;
839 }
840
841 return *associatedDisplayId == displayId;
842}
843
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800844void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000845 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800846
847 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800848 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849
Chris Yee7310032020-09-22 15:36:28 -0700850 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
851 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852
Chris Yee7310032020-09-22 15:36:28 -0700853 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
854 const std::shared_ptr<InputDevice>& device = devicePair.first;
855 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
856 for (const auto& eId : devicePair.second) {
857 eventHubDevStr += StringPrintf("%d ", eId);
858 }
859 eventHubDevStr += "] \n";
860 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861 }
862
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800863 dump += INDENT "Configuration:\n";
864 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
866 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800867 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100869 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800870 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800871 dump += "]\n";
872 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700873 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800875 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700876 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
877 "acceleration=%0.3f\n",
878 mConfig.pointerVelocityControlParameters.scale,
879 mConfig.pointerVelocityControlParameters.lowThreshold,
880 mConfig.pointerVelocityControlParameters.highThreshold,
881 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800883 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700884 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
885 "acceleration=%0.3f\n",
886 mConfig.wheelVelocityControlParameters.scale,
887 mConfig.wheelVelocityControlParameters.lowThreshold,
888 mConfig.wheelVelocityControlParameters.highThreshold,
889 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800891 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700892 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800893 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700894 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800895 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700896 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800897 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700898 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800899 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700900 mConfig.pointerGestureTapDragInterval * 0.000001f);
901 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800902 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700903 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800904 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700905 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800906 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700907 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800908 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700909 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800910 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700911 mConfig.pointerGestureMovementSpeedRatio);
912 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700913
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800914 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700915 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916}
917
918void InputReader::monitor() {
919 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800920 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800922 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 // Check the EventHub
924 mEventHub->monitor();
925}
926
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927// --- InputReader::ContextImpl ---
928
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800929InputReader::ContextImpl::ContextImpl(InputReader* reader)
930 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800931
932void InputReader::ContextImpl::updateGlobalMetaState() {
933 // lock is already held by the input loop
934 mReader->updateGlobalMetaStateLocked();
935}
936
937int32_t InputReader::ContextImpl::getGlobalMetaState() {
938 // lock is already held by the input loop
939 return mReader->getGlobalMetaStateLocked();
940}
941
arthurhungc903df12020-08-11 15:08:42 +0800942void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
943 // lock is already held by the input loop
944 mReader->updateLedMetaStateLocked(metaState);
945}
946
947int32_t InputReader::ContextImpl::getLedMetaState() {
948 // lock is already held by the input loop
949 return mReader->getLedMetaStateLocked();
950}
951
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
953 // lock is already held by the input loop
954 mReader->disableVirtualKeysUntilLocked(time);
955}
956
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800957bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
958 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800960 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961}
962
963void InputReader::ContextImpl::fadePointer() {
964 // lock is already held by the input loop
965 mReader->fadePointerLocked();
966}
967
Michael Wright17db18e2020-06-26 20:51:44 +0100968std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController(
969 int32_t deviceId) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800970 // lock is already held by the input loop
971 return mReader->getPointerControllerLocked(deviceId);
972}
973
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
975 // lock is already held by the input loop
976 mReader->requestTimeoutAtTimeLocked(when);
977}
978
979int32_t InputReader::ContextImpl::bumpGeneration() {
980 // lock is already held by the input loop
981 return mReader->bumpGenerationLocked();
982}
983
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800984void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700985 // lock is already held by whatever called refreshConfigurationLocked
986 mReader->getExternalStylusDevicesLocked(outDevices);
987}
988
989void InputReader::ContextImpl::dispatchExternalStylusState(const StylusState& state) {
Arthur Hungcadce9d2021-05-07 17:24:04 +0800990 mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -0700991}
992
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
994 return mReader->mPolicy.get();
995}
996
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700997InputListenerInterface& InputReader::ContextImpl::getListener() {
998 return mReader->mQueuedListener;
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000999}
1000
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001EventHubInterface* InputReader::ContextImpl::getEventHub() {
1002 return mReader->mEventHub.get();
1003}
1004
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00001005int32_t InputReader::ContextImpl::getNextId() {
1006 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -08001007}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001008
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009} // namespace android