blob: 9080cc1d2693d67c075dedc1a3376d0d6b1cfd44 [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 Pradhanda20b172022-09-26 17:01:18 +000061static bool isStylusPointerGestureStart(const NotifyMotionArgs& motionArgs) {
62 const auto actionMasked = MotionEvent::getActionMasked(motionArgs.action);
63 if (actionMasked != AMOTION_EVENT_ACTION_HOVER_ENTER &&
64 actionMasked != AMOTION_EVENT_ACTION_DOWN &&
65 actionMasked != AMOTION_EVENT_ACTION_POINTER_DOWN) {
66 return false;
67 }
68 const auto actionIndex = MotionEvent::getActionIndex(motionArgs.action);
Prabir Pradhane5626962022-10-27 20:30:53 +000069 return isStylusToolType(motionArgs.pointerProperties[actionIndex].toolType);
Prabir Pradhanda20b172022-09-26 17:01:18 +000070}
71
Prabir Pradhan28efc192019-11-05 01:10:04 +000072// --- InputReader ---
73
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070074InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
75 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070076 InputListenerInterface& listener)
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070077 : mContext(this),
78 mEventHub(eventHub),
79 mPolicy(policy),
Siarhei Vishniakou18050092021-09-01 13:32:49 -070080 mQueuedListener(listener),
Arthur Hung95f68612022-04-07 14:08:22 +080081 mGlobalMetaState(AMETA_NONE),
82 mLedMetaState(AMETA_NONE),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070083 mGeneration(1),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080084 mNextInputDeviceId(END_RESERVED_ID),
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070085 mDisableVirtualKeysTimeout(LLONG_MIN),
86 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -080087 mConfigurationChangesToRefresh(0) {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070088 refreshConfigurationLocked(0);
89 updateGlobalMetaStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -080090}
91
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +000092InputReader::~InputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
Prabir Pradhan28efc192019-11-05 01:10:04 +000094status_t InputReader::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070095 if (mThread) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000096 return ALREADY_EXISTS;
97 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070098 mThread = std::make_unique<InputThread>(
99 "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
100 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000101}
102
103status_t InputReader::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700104 if (mThread && mThread->isCallingThread()) {
105 ALOGE("InputReader cannot be stopped from its own thread!");
Prabir Pradhan28efc192019-11-05 01:10:04 +0000106 return INVALID_OPERATION;
107 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700108 mThread.reset();
109 return OK;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000110}
111
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112void InputReader::loopOnce() {
113 int32_t oldGeneration;
114 int32_t timeoutMillis;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000115 // Copy some state so that we can access it outside the lock later.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116 bool inputDevicesChanged = false;
Chris Ye1c2e0892020-11-30 21:41:44 -0800117 std::vector<InputDeviceInfo> inputDevices;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000118 std::list<NotifyArgs> notifyArgs;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000120 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800121
122 oldGeneration = mGeneration;
123 timeoutMillis = -1;
124
125 uint32_t changes = mConfigurationChangesToRefresh;
126 if (changes) {
127 mConfigurationChangesToRefresh = 0;
128 timeoutMillis = 0;
129 refreshConfigurationLocked(changes);
130 } else if (mNextTimeout != LLONG_MAX) {
131 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
132 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
133 }
134 } // release lock
135
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700136 std::vector<RawEvent> events = mEventHub->getEvents(timeoutMillis);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137
138 { // acquire lock
Chris Ye87143712020-11-10 05:05:58 +0000139 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800140 mReaderIsAliveCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700142 if (!events.empty()) {
Prabir Pradhanda20b172022-09-26 17:01:18 +0000143 notifyArgs += processEventsLocked(events.data(), events.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144 }
145
146 if (mNextTimeout != LLONG_MAX) {
147 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
148 if (now >= mNextTimeout) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000149 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800150 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
151 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152 mNextTimeout = LLONG_MAX;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000153 notifyArgs += timeoutExpiredLocked(now);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 }
155 }
156
157 if (oldGeneration != mGeneration) {
158 inputDevicesChanged = true;
Chris Ye1c2e0892020-11-30 21:41:44 -0800159 inputDevices = getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160 }
161 } // release lock
162
163 // Send out a message that the describes the changed input devices.
164 if (inputDevicesChanged) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800165 mPolicy->notifyInputDevicesChanged(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166 }
167
Prabir Pradhanda20b172022-09-26 17:01:18 +0000168 // Notify the policy of the start of every new stylus gesture outside the lock.
169 for (const auto& args : notifyArgs) {
170 const auto* motionArgs = std::get_if<NotifyMotionArgs>(&args);
171 if (motionArgs != nullptr && isStylusPointerGestureStart(*motionArgs)) {
172 mPolicy->notifyStylusGestureStarted(motionArgs->deviceId, motionArgs->eventTime);
173 }
174 }
175
176 notifyAll(std::move(notifyArgs));
177
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178 // Flush queued events out to the listener.
179 // This must happen outside of the lock because the listener could potentially call
180 // back into the InputReader's methods, such as getScanCodeState, or become blocked
181 // on another thread similarly waiting to acquire the InputReader lock thereby
182 // resulting in a deadlock. This situation is actually quite plausible because the
183 // listener is actually the input dispatcher, which calls into the window manager,
184 // which occasionally calls into the input reader.
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700185 mQueuedListener.flush();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186}
187
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700188std::list<NotifyArgs> InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
189 std::list<NotifyArgs> out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190 for (const RawEvent* rawEvent = rawEvents; count;) {
191 int32_t type = rawEvent->type;
192 size_t batchSize = 1;
193 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
194 int32_t deviceId = rawEvent->deviceId;
195 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700196 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
197 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198 break;
199 }
200 batchSize += 1;
201 }
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000202 if (debugRawEvents()) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800203 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
204 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700205 out += processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206 } else {
207 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700208 case EventHubInterface::DEVICE_ADDED:
209 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
210 break;
211 case EventHubInterface::DEVICE_REMOVED:
212 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
213 break;
214 case EventHubInterface::FINISHED_DEVICE_SCAN:
215 handleConfigurationChangedLocked(rawEvent->when);
216 break;
217 default:
218 ALOG_ASSERT(false); // can't happen
219 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220 }
221 }
222 count -= batchSize;
223 rawEvent += batchSize;
224 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700225 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226}
227
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800228void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
229 if (mDevices.find(eventHubId) != mDevices.end()) {
230 ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 return;
232 }
233
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800234 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
235 std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700236
237 notifyAll(device->configure(when, &mConfig, 0));
238 notifyAll(device->reset(when));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239
240 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800241 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
242 "(ignored non-input device)",
243 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000245 ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800246 device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000247 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 }
249
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800250 mDevices.emplace(eventHubId, device);
Chris Yee7310032020-09-22 15:36:28 -0700251 // Add device to device to EventHub ids map.
252 const auto mapIt = mDeviceToEventHubIdsMap.find(device);
253 if (mapIt == mDeviceToEventHubIdsMap.end()) {
254 std::vector<int32_t> ids = {eventHubId};
255 mDeviceToEventHubIdsMap.emplace(device, ids);
256 } else {
257 mapIt->second.push_back(eventHubId);
258 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700260
Chris Ye1b0c7342020-07-28 21:57:03 -0700261 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800262 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700263 }
Chris Yef59a2f42020-10-16 12:55:26 -0700264
265 // Sensor input device is noisy, to save power disable it by default.
Chris Yee14523a2020-12-19 13:46:00 -0800266 // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub
267 // device class to disable SENSOR sub device only.
268 if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) {
Chris Yef59a2f42020-10-16 12:55:26 -0700269 mEventHub->disableDevice(eventHubId);
270 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271}
272
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800273void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
274 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000275 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800276 ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800277 return;
278 }
279
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000280 std::shared_ptr<InputDevice> device = std::move(deviceIt->second);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000281 mDevices.erase(deviceIt);
Chris Yee7310032020-09-22 15:36:28 -0700282 // Erase device from device to EventHub ids map.
283 auto mapIt = mDeviceToEventHubIdsMap.find(device);
284 if (mapIt != mDeviceToEventHubIdsMap.end()) {
285 std::vector<int32_t>& eventHubIds = mapIt->second;
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -0800286 std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
Chris Yee7310032020-09-22 15:36:28 -0700287 if (eventHubIds.size() == 0) {
288 mDeviceToEventHubIdsMap.erase(mapIt);
289 }
290 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291 bumpGenerationLocked();
292
293 if (device->isIgnored()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800294 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
295 "(ignored non-input device)",
296 device->getId(), eventHubId, device->getName().c_str(),
297 device->getDescriptor().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 } else {
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000299 ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=%s",
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800300 device->getId(), eventHubId, device->getName().c_str(),
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000301 device->getDescriptor().c_str(),
302 inputEventSourceToString(device->getSources()).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 }
304
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800305 device->removeEventHubDevice(eventHubId);
306
Chris Ye1b0c7342020-07-28 21:57:03 -0700307 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800308 notifyExternalStylusPresenceChangedLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700309 }
310
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700311 std::list<NotifyArgs> resetEvents;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800312 if (device->hasEventHubDevices()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700313 resetEvents += device->configure(when, &mConfig, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800314 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700315 resetEvents += device->reset(when);
316 notifyAll(std::move(resetEvents));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317}
318
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000319std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800320 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
321 auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
Josh Bartel938632f2022-07-19 15:34:22 -0500322 const InputDeviceIdentifier identifier2 =
323 devicePair.second->getDeviceInfo().getIdentifier();
324 return isSubDevice(identifier, identifier2);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800325 });
326
327 std::shared_ptr<InputDevice> device;
328 if (deviceIt != mDevices.end()) {
329 device = deviceIt->second;
330 } else {
331 int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
332 device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
333 identifier);
334 }
335 device->addEventHubDevice(eventHubId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 return device;
337}
338
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700339std::list<NotifyArgs> InputReader::processEventsForDeviceLocked(int32_t eventHubId,
340 const RawEvent* rawEvents,
341 size_t count) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800342 auto deviceIt = mDevices.find(eventHubId);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000343 if (deviceIt == mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800344 ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700345 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346 }
347
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000348 std::shared_ptr<InputDevice>& device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700350 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700351 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800352 }
353
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700354 return device->process(rawEvents, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355}
356
Philip Junker4af3b3d2021-12-14 10:36:55 +0100357InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800358 auto deviceIt =
359 std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
360 return devicePair.second->getId() == deviceId;
361 });
362 if (deviceIt != mDevices.end()) {
363 return deviceIt->second.get();
364 }
365 return nullptr;
366}
367
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700368std::list<NotifyArgs> InputReader::timeoutExpiredLocked(nsecs_t when) {
369 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000370 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000371 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372 if (!device->isIgnored()) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700373 out += device->timeoutExpired(when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374 }
375 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700376 return out;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377}
378
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800379int32_t InputReader::nextInputDeviceIdLocked() {
380 return ++mNextInputDeviceId;
381}
382
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
384 // Reset global meta state because it depends on the list of all configured devices.
385 updateGlobalMetaStateLocked();
386
387 // Enqueue configuration changed.
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000388 NotifyConfigurationChangedArgs args(mContext.getNextId(), when);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700389 mQueuedListener.notifyConfigurationChanged(&args);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390}
391
392void InputReader::refreshConfigurationLocked(uint32_t changes) {
393 mPolicy->getReaderConfiguration(&mConfig);
394 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
395
Prabir Pradhan7e186182020-11-10 13:56:45 -0800396 if (!changes) return;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397
Prabir Pradhan7e186182020-11-10 13:56:45 -0800398 ALOGI("Reconfiguring input devices, changes=%s",
399 InputReaderConfiguration::changesToString(changes).c_str());
400 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800401
Prabir Pradhan7e186182020-11-10 13:56:45 -0800402 if (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO) {
403 updatePointerDisplayLocked();
404 }
405
406 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
407 mEventHub->requestReopenDevices();
408 } else {
409 for (auto& devicePair : mDevices) {
410 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700411 notifyAll(device->configure(now, &mConfig, changes));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800412 }
413 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800414
415 if (changes & InputReaderConfiguration::CHANGE_POINTER_CAPTURE) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000416 if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) {
417 ALOGV("Skipping notifying pointer capture changes: "
418 "There was no change in the pointer capture state.");
419 } else {
420 mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest;
421 const NotifyPointerCaptureChangedArgs args(mContext.getNextId(), now,
422 mCurrentPointerCaptureRequest);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700423 mQueuedListener.notifyPointerCaptureChanged(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000424 }
Prabir Pradhan7e186182020-11-10 13:56:45 -0800425 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800426}
427
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700428void InputReader::notifyAll(std::list<NotifyArgs>&& argsList) {
429 for (const NotifyArgs& args : argsList) {
430 mQueuedListener.notify(args);
431 }
432}
433
Michael Wrightd02c5b62014-02-10 15:10:22 -0800434void InputReader::updateGlobalMetaStateLocked() {
435 mGlobalMetaState = 0;
436
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000437 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000438 std::shared_ptr<InputDevice>& device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439 mGlobalMetaState |= device->getMetaState();
440 }
441}
442
443int32_t InputReader::getGlobalMetaStateLocked() {
444 return mGlobalMetaState;
445}
446
arthurhungc903df12020-08-11 15:08:42 +0800447void InputReader::updateLedMetaStateLocked(int32_t metaState) {
448 mLedMetaState = metaState;
449 for (auto& devicePair : mDevices) {
450 std::shared_ptr<InputDevice>& device = devicePair.second;
451 device->updateLedState(false);
452 }
453}
454
455int32_t InputReader::getLedMetaStateLocked() {
456 return mLedMetaState;
457}
458
Chris Ye1c2e0892020-11-30 21:41:44 -0800459void InputReader::notifyExternalStylusPresenceChangedLocked() {
Michael Wright842500e2015-03-13 17:32:02 -0700460 refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
461}
462
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800463void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000464 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000465 std::shared_ptr<InputDevice>& device = devicePair.second;
Chris Ye1b0c7342020-07-28 21:57:03 -0700466 if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000467 outDevices.push_back(device->getDeviceInfo());
Michael Wright842500e2015-03-13 17:32:02 -0700468 }
469 }
470}
471
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700472std::list<NotifyArgs> InputReader::dispatchExternalStylusStateLocked(const StylusState& state) {
473 std::list<NotifyArgs> out;
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000474 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000475 std::shared_ptr<InputDevice>& device = devicePair.second;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700476 out += device->updateExternalStylusState(state);
Michael Wright842500e2015-03-13 17:32:02 -0700477 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700478 return out;
Michael Wright842500e2015-03-13 17:32:02 -0700479}
480
Michael Wrightd02c5b62014-02-10 15:10:22 -0800481void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
482 mDisableVirtualKeysTimeout = time;
483}
484
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800485bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486 if (now < mDisableVirtualKeysTimeout) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800487 ALOGI("Dropping virtual key from device because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700488 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800489 (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 return true;
491 } else {
492 return false;
493 }
494}
495
Michael Wright17db18e2020-06-26 20:51:44 +0100496std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked(
497 int32_t deviceId) {
498 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800499 if (controller == nullptr) {
500 controller = mPolicy->obtainPointerController(deviceId);
501 mPointerController = controller;
502 updatePointerDisplayLocked();
503 }
504 return controller;
505}
506
507void InputReader::updatePointerDisplayLocked() {
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) {
510 return;
511 }
512
513 std::optional<DisplayViewport> viewport =
514 mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId);
515 if (!viewport) {
516 ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input "
517 "mapper. Fall back to default display",
518 mConfig.defaultPointerDisplayId);
519 viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT);
520 }
521 if (!viewport) {
522 ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to"
523 " PointerController.");
524 return;
525 }
526
527 controller->setDisplayViewport(*viewport);
528}
529
Michael Wrightd02c5b62014-02-10 15:10:22 -0800530void InputReader::fadePointerLocked() {
Michael Wright17db18e2020-06-26 20:51:44 +0100531 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800532 if (controller != nullptr) {
Michael Wrightca5bede2020-07-02 00:00:29 +0100533 controller->fade(PointerControllerInterface::Transition::GRADUAL);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534 }
535}
536
537void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
538 if (when < mNextTimeout) {
539 mNextTimeout = when;
540 mEventHub->wake();
541 }
542}
543
544int32_t InputReader::bumpGenerationLocked() {
545 return ++mGeneration;
546}
547
Chris Ye98d3f532020-10-01 21:48:59 -0700548std::vector<InputDeviceInfo> InputReader::getInputDevices() const {
Chris Ye87143712020-11-10 05:05:58 +0000549 std::scoped_lock _l(mLock);
Chris Ye98d3f532020-10-01 21:48:59 -0700550 return getInputDevicesLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551}
552
Chris Ye98d3f532020-10-01 21:48:59 -0700553std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const {
554 std::vector<InputDeviceInfo> outInputDevices;
555 outInputDevices.reserve(mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556
Chris Yee7310032020-09-22 15:36:28 -0700557 for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558 if (!device->isIgnored()) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000559 outInputDevices.push_back(device->getDeviceInfo());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800560 }
561 }
Chris Ye98d3f532020-10-01 21:48:59 -0700562 return outInputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800563}
564
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700565int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Chris Ye87143712020-11-10 05:05:58 +0000566 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567
568 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
569}
570
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700571int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Chris Ye87143712020-11-10 05:05:58 +0000572 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573
574 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
575}
576
577int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Chris Ye87143712020-11-10 05:05:58 +0000578 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579
580 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
581}
582
583int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700584 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585 int32_t result = AKEY_STATE_UNKNOWN;
586 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800587 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800588 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
589 result = (device->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590 }
591 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000592 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000593 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700594 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
596 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000597 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598 if (currentResult >= AKEY_STATE_DOWN) {
599 return currentResult;
600 } else if (currentResult == AKEY_STATE_UP) {
601 result = currentResult;
602 }
603 }
604 }
605 }
606 return result;
607}
608
Andrii Kulian763a3a42016-03-08 10:46:16 -0800609void InputReader::toggleCapsLockState(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000610 std::scoped_lock _l(mLock);
Chris Ye1c2e0892020-11-30 21:41:44 -0800611 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800612 if (!device) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800613 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
614 return;
615 }
616
Andrii Kulian763a3a42016-03-08 10:46:16 -0800617 if (device->isIgnored()) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000618 ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
Andrii Kulian763a3a42016-03-08 10:46:16 -0800619 return;
620 }
621
622 device->updateMetaState(AKEYCODE_CAPS_LOCK);
623}
624
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700625bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
626 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) {
Chris Ye87143712020-11-10 05:05:58 +0000627 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700629 memset(outFlags, 0, keyCodes.size());
630 return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800631}
632
633bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700634 const std::vector<int32_t>& keyCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700635 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800636 bool result = false;
637 if (deviceId >= 0) {
Chris Ye1c2e0892020-11-30 21:41:44 -0800638 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800639 if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700640 result = device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641 }
642 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000643 for (auto& devicePair : mDevices) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000644 std::shared_ptr<InputDevice>& device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700645 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700646 result |= device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800647 }
648 }
649 }
650 return result;
651}
652
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000653void InputReader::addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const {
654 std::scoped_lock _l(mLock);
655
656 InputDevice* device = findInputDeviceLocked(deviceId);
657 if (device != nullptr) {
658 device->addKeyRemapping(fromKeyCode, toKeyCode);
659 }
660}
661
Philip Junker4af3b3d2021-12-14 10:36:55 +0100662int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
663 std::scoped_lock _l(mLock);
664
665 InputDevice* device = findInputDeviceLocked(deviceId);
666 if (device == nullptr) {
667 ALOGW("Failed to get key code for key location: Input device with id %d not found",
668 deviceId);
669 return AKEYCODE_UNKNOWN;
670 }
671 return device->getKeyCodeForKeyLocation(locationKeyCode);
672}
673
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674void InputReader::requestRefreshConfiguration(uint32_t changes) {
Chris Ye87143712020-11-10 05:05:58 +0000675 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676
677 if (changes) {
678 bool needWake = !mConfigurationChangesToRefresh;
679 mConfigurationChangesToRefresh |= changes;
680
681 if (needWake) {
682 mEventHub->wake();
683 }
684 }
685}
686
Chris Ye87143712020-11-10 05:05:58 +0000687void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
688 int32_t token) {
689 std::scoped_lock _l(mLock);
690
Chris Ye1c2e0892020-11-30 21:41:44 -0800691 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800692 if (device) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700693 notifyAll(device->vibrate(sequence, repeat, token));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694 }
695}
696
697void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
Chris Ye87143712020-11-10 05:05:58 +0000698 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699
Chris Ye1c2e0892020-11-30 21:41:44 -0800700 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800701 if (device) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700702 notifyAll(device->cancelVibrate(token));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 }
704}
705
Chris Ye87143712020-11-10 05:05:58 +0000706bool InputReader::isVibrating(int32_t deviceId) {
707 std::scoped_lock _l(mLock);
708
709 InputDevice* device = findInputDeviceLocked(deviceId);
710 if (device) {
711 return device->isVibrating();
712 }
713 return false;
714}
715
716std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) {
717 std::scoped_lock _l(mLock);
718
719 InputDevice* device = findInputDeviceLocked(deviceId);
720 if (device) {
721 return device->getVibratorIds();
722 }
723 return {};
724}
725
Chris Yef59a2f42020-10-16 12:55:26 -0700726void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
727 std::scoped_lock _l(mLock);
728
729 InputDevice* device = findInputDeviceLocked(deviceId);
730 if (device) {
731 device->disableSensor(sensorType);
732 }
733}
734
735bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
736 std::chrono::microseconds samplingPeriod,
737 std::chrono::microseconds maxBatchReportLatency) {
738 std::scoped_lock _l(mLock);
739
740 InputDevice* device = findInputDeviceLocked(deviceId);
741 if (device) {
742 return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
743 }
744 return false;
745}
746
747void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
748 std::scoped_lock _l(mLock);
749
750 InputDevice* device = findInputDeviceLocked(deviceId);
751 if (device) {
752 device->flushSensor(sensorType);
753 }
754}
755
Kim Low03ea0352020-11-06 12:45:07 -0800756std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400757 std::optional<int32_t> eventHubId;
758 {
759 // Do not query the battery state while holding the lock. For some peripheral devices,
760 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
761 // would block all other event processing during this time. For now, we assume this
762 // call never happens on the InputReader thread and get the battery state outside the
763 // lock to prevent event processing from being blocked by this call.
764 std::scoped_lock _l(mLock);
765 InputDevice* device = findInputDeviceLocked(deviceId);
766 if (!device) return {};
767 eventHubId = device->getBatteryEventHubId();
768 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800769
Andy Chenf9f1a022022-08-29 20:07:10 -0400770 if (!eventHubId) return {};
771 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000772 if (batteryIds.empty()) {
773 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
774 return {};
775 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400776 return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800777}
778
779std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) {
Andy Chenf9f1a022022-08-29 20:07:10 -0400780 std::optional<int32_t> eventHubId;
781 {
782 // Do not query the battery state while holding the lock. For some peripheral devices,
783 // reading battery state can be broken and take 5+ seconds. Holding the lock in this case
784 // would block all other event processing during this time. For now, we assume this
785 // call never happens on the InputReader thread and get the battery state outside the
786 // lock to prevent event processing from being blocked by this call.
787 std::scoped_lock _l(mLock);
788 InputDevice* device = findInputDeviceLocked(deviceId);
789 if (!device) return {};
790 eventHubId = device->getBatteryEventHubId();
791 } // release lock
Kim Low03ea0352020-11-06 12:45:07 -0800792
Andy Chenf9f1a022022-08-29 20:07:10 -0400793 if (!eventHubId) return {};
794 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000795 if (batteryIds.empty()) {
796 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
797 return {};
798 }
Andy Chenf9f1a022022-08-29 20:07:10 -0400799 return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front());
Kim Low03ea0352020-11-06 12:45:07 -0800800}
801
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000802std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) {
803 std::scoped_lock _l(mLock);
804
805 InputDevice* device = findInputDeviceLocked(deviceId);
806 if (!device) return {};
807
808 std::optional<int32_t> eventHubId = device->getBatteryEventHubId();
809 if (!eventHubId) return {};
810 const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId);
811 if (batteryIds.empty()) {
812 ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId);
813 return {};
814 }
815 const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front());
816 if (!batteryInfo) {
817 ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__,
818 batteryIds.front(), *eventHubId);
819 return {};
820 }
821 return batteryInfo->path;
822}
823
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000824std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800825 std::scoped_lock _l(mLock);
826
827 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000828 if (device == nullptr) {
829 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800830 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000831
832 return device->getDeviceInfo().getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800833}
834
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000835std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800836 std::scoped_lock _l(mLock);
837
838 InputDevice* device = findInputDeviceLocked(deviceId);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000839 if (device == nullptr) {
840 return {};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800841 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000842
843 return device->getDeviceInfo().getSensors();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800844}
845
846bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
847 std::scoped_lock _l(mLock);
848
849 InputDevice* device = findInputDeviceLocked(deviceId);
850 if (device) {
851 return device->setLightColor(lightId, color);
852 }
853 return false;
854}
855
856bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
857 std::scoped_lock _l(mLock);
858
859 InputDevice* device = findInputDeviceLocked(deviceId);
860 if (device) {
861 return device->setLightPlayerId(lightId, playerId);
862 }
863 return false;
864}
865
866std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) {
867 std::scoped_lock _l(mLock);
868
869 InputDevice* device = findInputDeviceLocked(deviceId);
870 if (device) {
871 return device->getLightColor(lightId);
872 }
873 return std::nullopt;
874}
875
876std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) {
877 std::scoped_lock _l(mLock);
878
879 InputDevice* device = findInputDeviceLocked(deviceId);
880 if (device) {
881 return device->getLightPlayerId(lightId);
882 }
883 return std::nullopt;
884}
885
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000886std::optional<std::string> InputReader::getBluetoothAddress(int32_t deviceId) const {
887 std::scoped_lock _l(mLock);
888
889 InputDevice* device = findInputDeviceLocked(deviceId);
890 if (device) {
891 return device->getBluetoothAddress();
892 }
893 return std::nullopt;
894}
895
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700896bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
Chris Ye87143712020-11-10 05:05:58 +0000897 std::scoped_lock _l(mLock);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700898
Chris Ye1c2e0892020-11-30 21:41:44 -0800899 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800900 if (device) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700901 return device->isEnabled();
902 }
903 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
904 return false;
905}
906
Arthur Hungc23540e2018-11-29 20:42:11 +0800907bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
Chris Ye87143712020-11-10 05:05:58 +0000908 std::scoped_lock _l(mLock);
Arthur Hungc23540e2018-11-29 20:42:11 +0800909
Chris Ye1c2e0892020-11-30 21:41:44 -0800910 InputDevice* device = findInputDeviceLocked(deviceId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800911 if (!device) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800912 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
913 return false;
914 }
915
Arthur Hung2c9a3342019-07-23 14:18:59 +0800916 if (!device->isEnabled()) {
917 ALOGW("Ignoring disabled device %s", device->getName().c_str());
918 return false;
919 }
920
921 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800922 // No associated display. By default, can dispatch to all displays.
Weilun Dud00847d2021-12-08 10:55:58 -0800923 if (!associatedDisplayId ||
924 *associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800925 return true;
926 }
927
928 return *associatedDisplayId == displayId;
929}
930
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800931void InputReader::dump(std::string& dump) {
Chris Ye87143712020-11-10 05:05:58 +0000932 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933
934 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800935 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936
Chris Yee7310032020-09-22 15:36:28 -0700937 dump += StringPrintf("Input Reader State (Nums of device: %zu):\n",
938 mDeviceToEventHubIdsMap.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939
Chris Yee7310032020-09-22 15:36:28 -0700940 for (const auto& devicePair : mDeviceToEventHubIdsMap) {
941 const std::shared_ptr<InputDevice>& device = devicePair.first;
942 std::string eventHubDevStr = INDENT "EventHub Devices: [ ";
943 for (const auto& eId : devicePair.second) {
944 eventHubDevStr += StringPrintf("%d ", eId);
945 }
946 eventHubDevStr += "] \n";
947 device->dump(dump, eventHubDevStr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800948 }
949
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800950 dump += INDENT "Configuration:\n";
951 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
953 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800954 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100956 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800957 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800958 dump += "]\n";
959 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700960 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800962 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700963 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
964 "acceleration=%0.3f\n",
965 mConfig.pointerVelocityControlParameters.scale,
966 mConfig.pointerVelocityControlParameters.lowThreshold,
967 mConfig.pointerVelocityControlParameters.highThreshold,
968 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800970 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700971 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
972 "acceleration=%0.3f\n",
973 mConfig.wheelVelocityControlParameters.scale,
974 mConfig.wheelVelocityControlParameters.lowThreshold,
975 mConfig.wheelVelocityControlParameters.highThreshold,
976 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800978 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700979 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800980 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700981 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800982 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700983 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800984 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700985 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800986 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700987 mConfig.pointerGestureTapDragInterval * 0.000001f);
988 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800989 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700990 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800991 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700992 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800993 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700994 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800995 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700996 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800997 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700998 mConfig.pointerGestureMovementSpeedRatio);
999 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001000
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001001 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -07001002 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001003}
1004
1005void InputReader::monitor() {
1006 // Acquire and release the lock to ensure that the reader has not deadlocked.
Chris Ye1c2e0892020-11-30 21:41:44 -08001007 std::unique_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001008 mEventHub->wake();
Chris Ye1c2e0892020-11-30 21:41:44 -08001009 mReaderIsAliveCondition.wait(lock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001010 // Check the EventHub
1011 mEventHub->monitor();
1012}
1013
Michael Wrightd02c5b62014-02-10 15:10:22 -08001014// --- InputReader::ContextImpl ---
1015
Garfield Tanff1f1bb2020-01-28 13:24:04 -08001016InputReader::ContextImpl::ContextImpl(InputReader* reader)
1017 : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018
1019void InputReader::ContextImpl::updateGlobalMetaState() {
1020 // lock is already held by the input loop
1021 mReader->updateGlobalMetaStateLocked();
1022}
1023
1024int32_t InputReader::ContextImpl::getGlobalMetaState() {
1025 // lock is already held by the input loop
1026 return mReader->getGlobalMetaStateLocked();
1027}
1028
arthurhungc903df12020-08-11 15:08:42 +08001029void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) {
1030 // lock is already held by the input loop
1031 mReader->updateLedMetaStateLocked(metaState);
1032}
1033
1034int32_t InputReader::ContextImpl::getLedMetaState() {
1035 // lock is already held by the input loop
1036 return mReader->getLedMetaStateLocked();
1037}
1038
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
1040 // lock is already held by the input loop
1041 mReader->disableVirtualKeysUntilLocked(time);
1042}
1043
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001044bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
1045 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046 // lock is already held by the input loop
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001047 return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048}
1049
1050void InputReader::ContextImpl::fadePointer() {
1051 // lock is already held by the input loop
1052 mReader->fadePointerLocked();
1053}
1054
Michael Wright17db18e2020-06-26 20:51:44 +01001055std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController(
1056 int32_t deviceId) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08001057 // lock is already held by the input loop
1058 return mReader->getPointerControllerLocked(deviceId);
1059}
1060
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
1062 // lock is already held by the input loop
1063 mReader->requestTimeoutAtTimeLocked(when);
1064}
1065
1066int32_t InputReader::ContextImpl::bumpGeneration() {
1067 // lock is already held by the input loop
1068 return mReader->bumpGenerationLocked();
1069}
1070
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001071void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -07001072 // lock is already held by whatever called refreshConfigurationLocked
1073 mReader->getExternalStylusDevicesLocked(outDevices);
1074}
1075
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001076std::list<NotifyArgs> InputReader::ContextImpl::dispatchExternalStylusState(
1077 const StylusState& state) {
1078 return mReader->dispatchExternalStylusStateLocked(state);
Michael Wright842500e2015-03-13 17:32:02 -07001079}
1080
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
1082 return mReader->mPolicy.get();
1083}
1084
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085EventHubInterface* InputReader::ContextImpl::getEventHub() {
1086 return mReader->mEventHub.get();
1087}
1088
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +00001089int32_t InputReader::ContextImpl::getNextId() {
1090 return mIdGenerator.nextId();
Prabir Pradhan42611e02018-11-27 14:04:02 -08001091}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092
Michael Wrightd02c5b62014-02-10 15:10:22 -08001093} // namespace android