blob: 428e999c1be2deed6520fa8a2126b1511c7edea2 [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
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700123 std::vector<RawEvent> events = mEventHub->getEvents(timeoutMillis);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800124
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
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700129 if (!events.empty()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700130 notifyAll(processEventsLocked(events.data(), events.size()));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131 }
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;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700140 notifyAll(timeoutExpiredLocked(now));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141 }
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
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700165std::list<NotifyArgs> InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
166 std::list<NotifyArgs> out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167 for (const RawEvent* rawEvent = rawEvents; count;) {
168 int32_t type = rawEvent->type;
169 size_t batchSize = 1;
170 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
171 int32_t deviceId = rawEvent->deviceId;
172 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700173 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
174 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175 break;
176 }
177 batchSize += 1;
178 }
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800179 if (DEBUG_RAW_EVENTS) {
180 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
181 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700182 out += processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183 } else {
184 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700185 case EventHubInterface::DEVICE_ADDED:
186 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
187 break;
188 case EventHubInterface::DEVICE_REMOVED:
189 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
190 break;
191 case EventHubInterface::FINISHED_DEVICE_SCAN:
192 handleConfigurationChangedLocked(rawEvent->when);
193 break;
194 default:
195 ALOG_ASSERT(false); // can't happen
196 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800197 }
198 }
199 count -= batchSize;
200 rawEvent += batchSize;
201 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700202 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203}
204
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800205void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
206 if (mDevices.find(eventHubId) != mDevices.end()) {
207 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208 return;
209 }
210
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800211 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
212 std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700213
214 notifyAll(device->configure(when, &mConfig, 0));
215 notifyAll(device->reset(when));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216
217 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800218 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
219 "(ignored non-input device)",
220 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800221 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000222 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800223 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000224 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225 }
226
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800227 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700228 // Add device to device to EventHub ids map.
229 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
230 if (mapIt == mDeviceToEventHubIdsMap.end()) {
231 std::vector<int32_t> ids = {eventHubId};
232 mDeviceToEventHubIdsMap.emplace(device, ids);
233 } else {
234 mapIt->second.push_back(eventHubId);
235 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800236 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700237
Chris Ye1b0c7342020-07-28 21:57:03 -0700238 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800239 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700240 }
Chris Yef59a2f42020-10-16 12:55:26 -0700241
242 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800243 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
244 // device class to disable SENSOR sub device only.
245 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700246 mEventHub->disableDevice(eventHubId);
247 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248}
249
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800250void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
251 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000252 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800253 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254 return;
255 }
256
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000257 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000258 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700259 // Erase device from device to EventHub ids map.
260 auto mapIt = mDeviceToEventHubIdsMap.find(device);
261 if (mapIt != mDeviceToEventHubIdsMap.end()) {
262 std::vector<int32_t>& eventHubIds = mapIt->second;
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -0800263 std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
Chris Yee7310032020-09-22 15:36:28 -0700264 if (eventHubIds.size() == 0) {
265 mDeviceToEventHubIdsMap.erase(mapIt);
266 }
267 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 bumpGenerationLocked();
269
270 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800271 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
272 "(ignored non-input device)",
273 device->getId(), eventHubId, device->getName().c_str(),
274 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000276 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800277 device->getId(), eventHubId, device->getName().c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000278 device->getDescriptor().c_str(),
279 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280 }
281
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800282 device->removeEventHubDevice(eventHubId);
283
Chris Ye1b0c7342020-07-28 21:57:03 -0700284 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800285 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700286 }
287
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700288 std::list<NotifyArgs> resetEvents;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800289 if (device->hasEventHubDevices()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700290 resetEvents += device->configure(when, &mConfig, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800291 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700292 resetEvents += device->reset(when);
293 notifyAll(std::move(resetEvents));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294}
295
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000296std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800297 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
298 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
Josh Bartel938632f2022-07-19 15:34:22 -0500299 const InputDeviceIdentifier identifier2 =
300 devicePair.second->getDeviceInfo().getIdentifier();
301 return isSubDevice(identifier, identifier2);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800302 });
303
304 std::shared_ptr<InputDevice> device;
305 if (deviceIt != mDevices.end()) {
306 device = deviceIt->second;
307 } else {
308 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
309 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
310 identifier);
311 }
312 device->addEventHubDevice(eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 return device;
314}
315
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700316std::list<NotifyArgs> InputReader::processEventsForDeviceLocked(int32_t eventHubId,
317 const RawEvent* rawEvents,
318 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800319 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000320 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800321 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700322 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323 }
324
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000325 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700327 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700328 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 }
330
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700331 return device->process(rawEvents, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332}
333
Philip Junker4af3b3d2021-12-14 10:36:55 +0100334InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800335 auto deviceIt =
336 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
337 return devicePair.second->getId() == deviceId;
338 });
339 if (deviceIt != mDevices.end()) {
340 return deviceIt->second.get();
341 }
342 return nullptr;
343}
344
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700345std::list<NotifyArgs> InputReader::timeoutExpiredLocked(nsecs_t when) {
346 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000347 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000348 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349 if (!device->isIgnored()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700350 out += device->timeoutExpired(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 }
352 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700353 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800354}
355
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800356int32_t InputReader::nextInputDeviceIdLocked() {
357 return ++mNextInputDeviceId;
358}
359
Michael Wrightd02c5b62014-02-10 15:10:22 -0800360void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
361 // Reset global meta state because it depends on the list of all configured devices.
362 updateGlobalMetaStateLocked();
363
364 // Enqueue configuration changed.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000365 NotifyConfigurationChangedArgs args(mContext.getNextId(), when);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700366 mQueuedListener.notifyConfigurationChanged(&args);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367}
368
369void InputReader::refreshConfigurationLocked(uint32_t changes) {
370 mPolicy->getReaderConfiguration(&mConfig);
371 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
372
Prabir Pradhan7e186182020-11-10 13:56:45 -0800373 if (!changes) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374
Prabir Pradhan7e186182020-11-10 13:56:45 -0800375 ALOGI("Reconfiguring input devices, changes=%s",
376 InputReaderConfiguration::changesToString(changes).c_str());
377 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800378
Prabir Pradhan7e186182020-11-10 13:56:45 -0800379 if (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO) {
380 updatePointerDisplayLocked();
381 }
382
383 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
384 mEventHub->requestReopenDevices();
385 } else {
386 for (auto& devicePair : mDevices) {
387 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700388 notifyAll(device->configure(now, &mConfig, changes));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389 }
390 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800391
392 if (changes & InputReaderConfiguration::CHANGE_POINTER_CAPTURE) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000393 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
394 ALOGV("Skipping notifying pointer capture changes: "
395 "There was no change in the pointer capture state.");
396 } else {
397 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
398 const NotifyPointerCaptureChangedArgs args(mContext.getNextId(), now,
399 mCurrentPointerCaptureRequest);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700400 mQueuedListener.notifyPointerCaptureChanged(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000401 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800402 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403}
404
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700405void InputReader::notifyAll(std::list<NotifyArgs>&& argsList) {
406 for (const NotifyArgs& args : argsList) {
407 mQueuedListener.notify(args);
408 }
409}
410
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411void InputReader::updateGlobalMetaStateLocked() {
412 mGlobalMetaState = 0;
413
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000414 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000415 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416 mGlobalMetaState |= device->getMetaState();
417 }
418}
419
420int32_t InputReader::getGlobalMetaStateLocked() {
421 return mGlobalMetaState;
422}
423
arthurhungc903df12020-08-11 15:08:42 +0800424void InputReader::updateLedMetaStateLocked(int32_t metaState) {
425 mLedMetaState = metaState;
426 for (auto& devicePair : mDevices) {
427 std::shared_ptr<InputDevice>& device = devicePair.second;
428 device->updateLedState(false);
429 }
430}
431
432int32_t InputReader::getLedMetaStateLocked() {
433 return mLedMetaState;
434}
435
Chris Ye1c2e0892020-11-30 21:41:44 -0800436void InputReader::notifyExternalStylusPresenceChangedLocked() {
Michael Wright842500e2015-03-13 17:32:02 -0700437 refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
438}
439
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800440void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000441 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000442 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700443 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000444 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700445 }
446 }
447}
448
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700449std::list<NotifyArgs> InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
450 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000451 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000452 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700453 out += device->updateExternalStylusState(state);
Michael Wright842500e2015-03-13 17:32:02 -0700454 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700455 return out;
Michael Wright842500e2015-03-13 17:32:02 -0700456}
457
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
459 mDisableVirtualKeysTimeout = time;
460}
461
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800462bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800463 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800464 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700465 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800466 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800467 return true;
468 } else {
469 return false;
470 }
471}
472
Michael Wright17db18e2020-06-26 20:51:44 +0100473std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked(
474 int32_t deviceId) {
475 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800476 if (controller == nullptr) {
477 controller = mPolicy->obtainPointerController(deviceId);
478 mPointerController = controller;
479 updatePointerDisplayLocked();
480 }
481 return controller;
482}
483
484void InputReader::updatePointerDisplayLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100485 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800486 if (controller == nullptr) {
487 return;
488 }
489
490 std::optional<DisplayViewport> viewport =
491 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
492 if (!viewport) {
493 ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input "
494 "mapper. Fall back to default display",
495 mConfig.defaultPointerDisplayId);
496 viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT);
497 }
498 if (!viewport) {
499 ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to"
500 " PointerController.");
501 return;
502 }
503
504 controller->setDisplayViewport(*viewport);
505}
506
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507void InputReader::fadePointerLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100508 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800509 if (controller != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +0100510 controller->fade(PointerControllerInterface::Transition::GRADUAL);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511 }
512}
513
514void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
515 if (when < mNextTimeout) {
516 mNextTimeout = when;
517 mEventHub->wake();
518 }
519}
520
521int32_t InputReader::bumpGenerationLocked() {
522 return ++mGeneration;
523}
524
Chris Ye98d3f532020-10-01 21:48:59 -0700525std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000526 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700527 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528}
529
Chris Ye98d3f532020-10-01 21:48:59 -0700530std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
531 std::vector<InputDeviceInfo> outInputDevices;
532 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533
Chris Yee7310032020-09-22 15:36:28 -0700534 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000536 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537 }
538 }
Chris Ye98d3f532020-10-01 21:48:59 -0700539 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540}
541
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700542int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000543 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544
545 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
546}
547
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700548int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000549 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550
551 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
552}
553
554int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000555 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556
557 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
558}
559
560int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700561 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 int32_t result = AKEY_STATE_UNKNOWN;
563 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800564 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800565 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
566 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567 }
568 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000569 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000570 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700571 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
573 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000574 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800575 if (currentResult >= AKEY_STATE_DOWN) {
576 return currentResult;
577 } else if (currentResult == AKEY_STATE_UP) {
578 result = currentResult;
579 }
580 }
581 }
582 }
583 return result;
584}
585
Andrii Kulian763a3a42016-03-08 10:46:16 -0800586void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000587 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800588 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800589 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800590 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
591 return;
592 }
593
Andrii Kulian763a3a42016-03-08 10:46:16 -0800594 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000595 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800596 return;
597 }
598
599 device->updateMetaState(AKEYCODE_CAPS_LOCK);
600}
601
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700602bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
603 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000604 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700606 memset(outFlags, 0, keyCodes.size());
607 return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608}
609
610bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700611 const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700612 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613 bool result = false;
614 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800615 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800616 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700617 result = device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 }
619 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000620 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000621 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700622 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700623 result |= device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800624 }
625 }
626 }
627 return result;
628}
629
Philip Junker4af3b3d2021-12-14 10:36:55 +0100630int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
631 std::scoped_lock _l(mLock);
632
633 InputDevice* device = findInputDeviceLocked(deviceId);
634 if (device == nullptr) {
635 ALOGW("Failed to get key code for key location: Input device with id %d not found",
636 deviceId);
637 return AKEYCODE_UNKNOWN;
638 }
639 return device->getKeyCodeForKeyLocation(locationKeyCode);
640}
641
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642void InputReader::requestRefreshConfiguration(uint32_t changes) {
Chris Ye87143712020-11-10 05:05:58 +0000643 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644
645 if (changes) {
646 bool needWake = !mConfigurationChangesToRefresh;
647 mConfigurationChangesToRefresh |= changes;
648
649 if (needWake) {
650 mEventHub->wake();
651 }
652 }
653}
654
Chris Ye87143712020-11-10 05:05:58 +0000655void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
656 int32_t token) {
657 std::scoped_lock _l(mLock);
658
Chris Ye1c2e0892020-11-30 21:41:44 -0800659 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800660 if (device) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700661 notifyAll(device->vibrate(sequence, repeat, token));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800662 }
663}
664
665void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000666 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667
Chris Ye1c2e0892020-11-30 21:41:44 -0800668 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800669 if (device) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700670 notifyAll(device->cancelVibrate(token));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 }
672}
673
Chris Ye87143712020-11-10 05:05:58 +0000674bool InputReader::isVibrating(int32_t deviceId) {
675 std::scoped_lock _l(mLock);
676
677 InputDevice* device = findInputDeviceLocked(deviceId);
678 if (device) {
679 return device->isVibrating();
680 }
681 return false;
682}
683
684std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
685 std::scoped_lock _l(mLock);
686
687 InputDevice* device = findInputDeviceLocked(deviceId);
688 if (device) {
689 return device->getVibratorIds();
690 }
691 return {};
692}
693
Chris Yef59a2f42020-10-16 12:55:26 -0700694void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
695 std::scoped_lock _l(mLock);
696
697 InputDevice* device = findInputDeviceLocked(deviceId);
698 if (device) {
699 device->disableSensor(sensorType);
700 }
701}
702
703bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
704 std::chrono::microseconds samplingPeriod,
705 std::chrono::microseconds maxBatchReportLatency) {
706 std::scoped_lock _l(mLock);
707
708 InputDevice* device = findInputDeviceLocked(deviceId);
709 if (device) {
710 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
711 }
712 return false;
713}
714
715void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
716 std::scoped_lock _l(mLock);
717
718 InputDevice* device = findInputDeviceLocked(deviceId);
719 if (device) {
720 device->flushSensor(sensorType);
721 }
722}
723
Kim Low03ea0352020-11-06 12:45:07 -0800724std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400725 std::optional<int32_t> eventHubId;
726 {
727 // Do not query the battery state while holding the lock. For some peripheral devices,
728 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
729 // would block all other event processing during this time. For now, we assume this
730 // call never happens on the InputReader thread and get the battery state outside the
731 // lock to prevent event processing from being blocked by this call.
732 std::scoped_lock _l(mLock);
733 InputDevice* device = findInputDeviceLocked(deviceId);
734 if (!device) return {};
735 eventHubId = device->getBatteryEventHubId();
736 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800737
Andy Chenf9f1a022022-08-29 20:07:10 -0400738 if (!eventHubId) return {};
739 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000740 if (batteryIds.empty()) {
741 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
742 return {};
743 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400744 return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800745}
746
747std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400748 std::optional<int32_t> eventHubId;
749 {
750 // Do not query the battery state while holding the lock. For some peripheral devices,
751 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
752 // would block all other event processing during this time. For now, we assume this
753 // call never happens on the InputReader thread and get the battery state outside the
754 // lock to prevent event processing from being blocked by this call.
755 std::scoped_lock _l(mLock);
756 InputDevice* device = findInputDeviceLocked(deviceId);
757 if (!device) return {};
758 eventHubId = device->getBatteryEventHubId();
759 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800760
Andy Chenf9f1a022022-08-29 20:07:10 -0400761 if (!eventHubId) return {};
762 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000763 if (batteryIds.empty()) {
764 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
765 return {};
766 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400767 return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800768}
769
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000770std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) {
771 std::scoped_lock _l(mLock);
772
773 InputDevice* device = findInputDeviceLocked(deviceId);
774 if (!device) return {};
775
776 std::optional<int32_t> eventHubId = device->getBatteryEventHubId();
777 if (!eventHubId) return {};
778 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
779 if (batteryIds.empty()) {
780 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
781 return {};
782 }
783 const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front());
784 if (!batteryInfo) {
785 ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__,
786 batteryIds.front(), *eventHubId);
787 return {};
788 }
789 return batteryInfo->path;
790}
791
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000792std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800793 std::scoped_lock _l(mLock);
794
795 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000796 if (device == nullptr) {
797 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800798 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000799
800 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800801}
802
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000803std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800804 std::scoped_lock _l(mLock);
805
806 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000807 if (device == nullptr) {
808 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800809 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000810
811 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800812}
813
814bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
815 std::scoped_lock _l(mLock);
816
817 InputDevice* device = findInputDeviceLocked(deviceId);
818 if (device) {
819 return device->setLightColor(lightId, color);
820 }
821 return false;
822}
823
824bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
825 std::scoped_lock _l(mLock);
826
827 InputDevice* device = findInputDeviceLocked(deviceId);
828 if (device) {
829 return device->setLightPlayerId(lightId, playerId);
830 }
831 return false;
832}
833
834std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
835 std::scoped_lock _l(mLock);
836
837 InputDevice* device = findInputDeviceLocked(deviceId);
838 if (device) {
839 return device->getLightColor(lightId);
840 }
841 return std::nullopt;
842}
843
844std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
845 std::scoped_lock _l(mLock);
846
847 InputDevice* device = findInputDeviceLocked(deviceId);
848 if (device) {
849 return device->getLightPlayerId(lightId);
850 }
851 return std::nullopt;
852}
853
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700854bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000855 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700856
Chris Ye1c2e0892020-11-30 21:41:44 -0800857 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800858 if (device) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700859 return device->isEnabled();
860 }
861 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
862 return false;
863}
864
Arthur Hungc23540e2018-11-29 20:42:11 +0800865bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000866 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800867
Chris Ye1c2e0892020-11-30 21:41:44 -0800868 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800869 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800870 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
871 return false;
872 }
873
Arthur Hung2c9a3342019-07-23 14:18:59 +0800874 if (!device->isEnabled()) {
875 ALOGW("Ignoring disabled device %s", device->getName().c_str());
876 return false;
877 }
878
879 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800880 // No associated display. By default, can dispatch to all displays.
Weilun Dud00847d2021-12-08 10:55:58 -0800881 if (!associatedDisplayId ||
882 *associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800883 return true;
884 }
885
886 return *associatedDisplayId == displayId;
887}
888
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800889void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000890 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800891
892 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800893 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800894
Chris Yee7310032020-09-22 15:36:28 -0700895 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
896 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897
Chris Yee7310032020-09-22 15:36:28 -0700898 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
899 const std::shared_ptr<InputDevice>& device = devicePair.first;
900 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
901 for (const auto& eId : devicePair.second) {
902 eventHubDevStr += StringPrintf("%d ", eId);
903 }
904 eventHubDevStr += "] \n";
905 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906 }
907
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800908 dump += INDENT "Configuration:\n";
909 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800910 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
911 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800912 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100914 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800916 dump += "]\n";
917 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700918 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800919
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800920 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700921 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
922 "acceleration=%0.3f\n",
923 mConfig.pointerVelocityControlParameters.scale,
924 mConfig.pointerVelocityControlParameters.lowThreshold,
925 mConfig.pointerVelocityControlParameters.highThreshold,
926 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800928 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700929 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
930 "acceleration=%0.3f\n",
931 mConfig.wheelVelocityControlParameters.scale,
932 mConfig.wheelVelocityControlParameters.lowThreshold,
933 mConfig.wheelVelocityControlParameters.highThreshold,
934 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800936 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700937 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800938 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700939 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800940 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700941 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800942 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700943 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800944 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700945 mConfig.pointerGestureTapDragInterval * 0.000001f);
946 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800947 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700948 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800949 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700950 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800951 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700952 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800953 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700954 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800955 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700956 mConfig.pointerGestureMovementSpeedRatio);
957 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700958
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800959 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700960 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961}
962
963void InputReader::monitor() {
964 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -0800965 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -0800967 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 // Check the EventHub
969 mEventHub->monitor();
970}
971
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972// --- InputReader::ContextImpl ---
973
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800974InputReader::ContextImpl::ContextImpl(InputReader* reader)
975 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976
977void InputReader::ContextImpl::updateGlobalMetaState() {
978 // lock is already held by the input loop
979 mReader->updateGlobalMetaStateLocked();
980}
981
982int32_t InputReader::ContextImpl::getGlobalMetaState() {
983 // lock is already held by the input loop
984 return mReader->getGlobalMetaStateLocked();
985}
986
arthurhungc903df12020-08-11 15:08:42 +0800987void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
988 // lock is already held by the input loop
989 mReader->updateLedMetaStateLocked(metaState);
990}
991
992int32_t InputReader::ContextImpl::getLedMetaState() {
993 // lock is already held by the input loop
994 return mReader->getLedMetaStateLocked();
995}
996
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
998 // lock is already held by the input loop
999 mReader->disableVirtualKeysUntilLocked(time);
1000}
1001
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001002bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
1003 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001005 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001006}
1007
1008void InputReader::ContextImpl::fadePointer() {
1009 // lock is already held by the input loop
1010 mReader->fadePointerLocked();
1011}
1012
Michael Wright17db18e2020-06-26 20:51:44 +01001013std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController(
1014 int32_t deviceId) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001015 // lock is already held by the input loop
1016 return mReader->getPointerControllerLocked(deviceId);
1017}
1018
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
1020 // lock is already held by the input loop
1021 mReader->requestTimeoutAtTimeLocked(when);
1022}
1023
1024int32_t InputReader::ContextImpl::bumpGeneration() {
1025 // lock is already held by the input loop
1026 return mReader->bumpGenerationLocked();
1027}
1028
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001029void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -07001030 // lock is already held by whatever called refreshConfigurationLocked
1031 mReader->getExternalStylusDevicesLocked(outDevices);
1032}
1033
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001034std::list<NotifyArgs> InputReader::ContextImpl::dispatchExternalStylusState(
1035 const StylusState& state) {
1036 return mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -07001037}
1038
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
1040 return mReader->mPolicy.get();
1041}
1042
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043EventHubInterface* InputReader::ContextImpl::getEventHub() {
1044 return mReader->mEventHub.get();
1045}
1046
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00001047int32_t InputReader::ContextImpl::getNextId() {
1048 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -08001049}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001050
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051} // namespace android