Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 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 Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 17 | #include "Macros.h" |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 18 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 19 | #include "InputReader.h" |
| 20 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 22 | #include <errno.h> |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 23 | #include <input/Keyboard.h> |
| 24 | #include <input/VirtualKeyMap.h> |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 25 | #include <inttypes.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 26 | #include <limits.h> |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 27 | #include <log/log.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 28 | #include <math.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 29 | #include <stddef.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <unistd.h> |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 32 | #include <utils/Errors.h> |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 33 | #include <utils/Thread.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 34 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 35 | #include "InputDevice.h" |
| 36 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 37 | using android::base::StringPrintf; |
| 38 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 39 | namespace android { |
| 40 | |
Josh Bartel | 938632f | 2022-07-19 15:34:22 -0500 | [diff] [blame] | 41 | /** |
| 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 | |
| 52 | static 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 Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 61 | // --- InputReader --- |
| 62 | |
Siarhei Vishniakou | 3bc7e09 | 2019-07-24 17:43:30 -0700 | [diff] [blame] | 63 | InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub, |
| 64 | const sp<InputReaderPolicyInterface>& policy, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 65 | InputListenerInterface& listener) |
Siarhei Vishniakou | 3bc7e09 | 2019-07-24 17:43:30 -0700 | [diff] [blame] | 66 | : mContext(this), |
| 67 | mEventHub(eventHub), |
| 68 | mPolicy(policy), |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 69 | mQueuedListener(listener), |
Arthur Hung | 95f6861 | 2022-04-07 14:08:22 +0800 | [diff] [blame] | 70 | mGlobalMetaState(AMETA_NONE), |
| 71 | mLedMetaState(AMETA_NONE), |
Siarhei Vishniakou | 3bc7e09 | 2019-07-24 17:43:30 -0700 | [diff] [blame] | 72 | mGeneration(1), |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 73 | mNextInputDeviceId(END_RESERVED_ID), |
Siarhei Vishniakou | 3bc7e09 | 2019-07-24 17:43:30 -0700 | [diff] [blame] | 74 | mDisableVirtualKeysTimeout(LLONG_MIN), |
| 75 | mNextTimeout(LLONG_MAX), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 76 | mConfigurationChangesToRefresh(0) { |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 77 | refreshConfigurationLocked(0); |
| 78 | updateGlobalMetaStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 81 | InputReader::~InputReader() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 82 | |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 83 | status_t InputReader::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 84 | if (mThread) { |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 85 | return ALREADY_EXISTS; |
| 86 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 87 | mThread = std::make_unique<InputThread>( |
| 88 | "InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); }); |
| 89 | return OK; |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | status_t InputReader::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 93 | if (mThread && mThread->isCallingThread()) { |
| 94 | ALOGE("InputReader cannot be stopped from its own thread!"); |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 95 | return INVALID_OPERATION; |
| 96 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 97 | mThread.reset(); |
| 98 | return OK; |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 101 | void InputReader::loopOnce() { |
| 102 | int32_t oldGeneration; |
| 103 | int32_t timeoutMillis; |
| 104 | bool inputDevicesChanged = false; |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 105 | std::vector<InputDeviceInfo> inputDevices; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 106 | { // acquire lock |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 107 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 108 | |
| 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 Vishniakou | 7b3ea0b | 2022-09-16 14:23:20 -0700 | [diff] [blame] | 123 | std::vector<RawEvent> events = mEventHub->getEvents(timeoutMillis); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 124 | |
| 125 | { // acquire lock |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 126 | std::scoped_lock _l(mLock); |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 127 | mReaderIsAliveCondition.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 128 | |
Siarhei Vishniakou | 7b3ea0b | 2022-09-16 14:23:20 -0700 | [diff] [blame] | 129 | if (!events.empty()) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 130 | notifyAll(processEventsLocked(events.data(), events.size())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if (mNextTimeout != LLONG_MAX) { |
| 134 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 135 | if (now >= mNextTimeout) { |
Siarhei Vishniakou | 465e1c0 | 2021-12-09 10:47:29 -0800 | [diff] [blame] | 136 | if (DEBUG_RAW_EVENTS) { |
| 137 | ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f); |
| 138 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 139 | mNextTimeout = LLONG_MAX; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 140 | notifyAll(timeoutExpiredLocked(now)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | if (oldGeneration != mGeneration) { |
| 145 | inputDevicesChanged = true; |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 146 | inputDevices = getInputDevicesLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 147 | } |
| 148 | } // release lock |
| 149 | |
| 150 | // Send out a message that the describes the changed input devices. |
| 151 | if (inputDevicesChanged) { |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 152 | mPolicy->notifyInputDevicesChanged(inputDevices); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 153 | } |
| 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 Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 162 | mQueuedListener.flush(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 165 | std::list<NotifyArgs> InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) { |
| 166 | std::list<NotifyArgs> out; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 167 | 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 Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 173 | if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT || |
| 174 | rawEvent[batchSize].deviceId != deviceId) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 | break; |
| 176 | } |
| 177 | batchSize += 1; |
| 178 | } |
Siarhei Vishniakou | 465e1c0 | 2021-12-09 10:47:29 -0800 | [diff] [blame] | 179 | if (DEBUG_RAW_EVENTS) { |
| 180 | ALOGD("BatchSize: %zu Count: %zu", batchSize, count); |
| 181 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 182 | out += processEventsForDeviceLocked(deviceId, rawEvent, batchSize); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 183 | } else { |
| 184 | switch (rawEvent->type) { |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 185 | 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 Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | count -= batchSize; |
| 200 | rawEvent += batchSize; |
| 201 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 202 | return out; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 205 | void 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 Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 211 | InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId); |
| 212 | std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 213 | |
| 214 | notifyAll(device->configure(when, &mConfig, 0)); |
| 215 | notifyAll(device->reset(when)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 216 | |
| 217 | if (device->isIgnored()) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 218 | 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 Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 221 | } else { |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 222 | ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=%s", |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 223 | device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(), |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 224 | inputEventSourceToString(device->getSources()).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 227 | mDevices.emplace(eventHubId, device); |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 228 | // 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 Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 236 | bumpGenerationLocked(); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 237 | |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 238 | if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) { |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 239 | notifyExternalStylusPresenceChangedLocked(); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 240 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 241 | |
| 242 | // Sensor input device is noisy, to save power disable it by default. |
Chris Ye | e14523a | 2020-12-19 13:46:00 -0800 | [diff] [blame] | 243 | // 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 Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 246 | mEventHub->disableDevice(eventHubId); |
| 247 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 250 | void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) { |
| 251 | auto deviceIt = mDevices.find(eventHubId); |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 252 | if (deviceIt == mDevices.end()) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 253 | ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 254 | return; |
| 255 | } |
| 256 | |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 257 | std::shared_ptr<InputDevice> device = std::move(deviceIt->second); |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 258 | mDevices.erase(deviceIt); |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 259 | // 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 Vishniakou | f47c339e | 2021-12-30 11:22:26 -0800 | [diff] [blame] | 263 | std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; }); |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 264 | if (eventHubIds.size() == 0) { |
| 265 | mDeviceToEventHubIdsMap.erase(mapIt); |
| 266 | } |
| 267 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 268 | bumpGenerationLocked(); |
| 269 | |
| 270 | if (device->isIgnored()) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 271 | 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 Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 275 | } else { |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 276 | ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=%s", |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 277 | device->getId(), eventHubId, device->getName().c_str(), |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 278 | device->getDescriptor().c_str(), |
| 279 | inputEventSourceToString(device->getSources()).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 282 | device->removeEventHubDevice(eventHubId); |
| 283 | |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 284 | if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) { |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 285 | notifyExternalStylusPresenceChangedLocked(); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 288 | std::list<NotifyArgs> resetEvents; |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 289 | if (device->hasEventHubDevices()) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 290 | resetEvents += device->configure(when, &mConfig, 0); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 291 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 292 | resetEvents += device->reset(when); |
| 293 | notifyAll(std::move(resetEvents)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 296 | std::shared_ptr<InputDevice> InputReader::createDeviceLocked( |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 297 | int32_t eventHubId, const InputDeviceIdentifier& identifier) { |
| 298 | auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) { |
Josh Bartel | 938632f | 2022-07-19 15:34:22 -0500 | [diff] [blame] | 299 | const InputDeviceIdentifier identifier2 = |
| 300 | devicePair.second->getDeviceInfo().getIdentifier(); |
| 301 | return isSubDevice(identifier, identifier2); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 302 | }); |
| 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 Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 313 | return device; |
| 314 | } |
| 315 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 316 | std::list<NotifyArgs> InputReader::processEventsForDeviceLocked(int32_t eventHubId, |
| 317 | const RawEvent* rawEvents, |
| 318 | size_t count) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 319 | auto deviceIt = mDevices.find(eventHubId); |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 320 | if (deviceIt == mDevices.end()) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 321 | ALOGW("Discarding event for unknown eventHubId %d.", eventHubId); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 322 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 325 | std::shared_ptr<InputDevice>& device = deviceIt->second; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 326 | if (device->isIgnored()) { |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 327 | // ALOGD("Discarding event for ignored deviceId %d.", deviceId); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 328 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 331 | return device->process(rawEvents, count); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 334 | InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 335 | 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 Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 345 | std::list<NotifyArgs> InputReader::timeoutExpiredLocked(nsecs_t when) { |
| 346 | std::list<NotifyArgs> out; |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 347 | for (auto& devicePair : mDevices) { |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 348 | std::shared_ptr<InputDevice>& device = devicePair.second; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 349 | if (!device->isIgnored()) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 350 | out += device->timeoutExpired(when); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 351 | } |
| 352 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 353 | return out; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 356 | int32_t InputReader::nextInputDeviceIdLocked() { |
| 357 | return ++mNextInputDeviceId; |
| 358 | } |
| 359 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 360 | void 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 Vishniakou | f2f073b | 2021-02-09 21:59:56 +0000 | [diff] [blame] | 365 | NotifyConfigurationChangedArgs args(mContext.getNextId(), when); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 366 | mQueuedListener.notifyConfigurationChanged(&args); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void InputReader::refreshConfigurationLocked(uint32_t changes) { |
| 370 | mPolicy->getReaderConfiguration(&mConfig); |
| 371 | mEventHub->setExcludedDevices(mConfig.excludedDeviceNames); |
| 372 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 373 | if (!changes) return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 374 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 375 | ALOGI("Reconfiguring input devices, changes=%s", |
| 376 | InputReaderConfiguration::changesToString(changes).c_str()); |
| 377 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 378 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 379 | 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 Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 388 | notifyAll(device->configure(now, &mConfig, changes)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 389 | } |
| 390 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 391 | |
| 392 | if (changes & InputReaderConfiguration::CHANGE_POINTER_CAPTURE) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 393 | 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 Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 400 | mQueuedListener.notifyPointerCaptureChanged(&args); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 401 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 402 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 405 | void InputReader::notifyAll(std::list<NotifyArgs>&& argsList) { |
| 406 | for (const NotifyArgs& args : argsList) { |
| 407 | mQueuedListener.notify(args); |
| 408 | } |
| 409 | } |
| 410 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 411 | void InputReader::updateGlobalMetaStateLocked() { |
| 412 | mGlobalMetaState = 0; |
| 413 | |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 414 | for (auto& devicePair : mDevices) { |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 415 | std::shared_ptr<InputDevice>& device = devicePair.second; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 416 | mGlobalMetaState |= device->getMetaState(); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | int32_t InputReader::getGlobalMetaStateLocked() { |
| 421 | return mGlobalMetaState; |
| 422 | } |
| 423 | |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 424 | void 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 | |
| 432 | int32_t InputReader::getLedMetaStateLocked() { |
| 433 | return mLedMetaState; |
| 434 | } |
| 435 | |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 436 | void InputReader::notifyExternalStylusPresenceChangedLocked() { |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 437 | refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE); |
| 438 | } |
| 439 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 440 | void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) { |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 441 | for (auto& devicePair : mDevices) { |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 442 | std::shared_ptr<InputDevice>& device = devicePair.second; |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 443 | if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) { |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 444 | outDevices.push_back(device->getDeviceInfo()); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 449 | std::list<NotifyArgs> InputReader::dispatchExternalStylusStateLocked(const StylusState& state) { |
| 450 | std::list<NotifyArgs> out; |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 451 | for (auto& devicePair : mDevices) { |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 452 | std::shared_ptr<InputDevice>& device = devicePair.second; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 453 | out += device->updateExternalStylusState(state); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 454 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 455 | return out; |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 458 | void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) { |
| 459 | mDisableVirtualKeysTimeout = time; |
| 460 | } |
| 461 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 462 | bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 463 | if (now < mDisableVirtualKeysTimeout) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 464 | ALOGI("Dropping virtual key from device because virtual keys are " |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 465 | "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d", |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 466 | (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 467 | return true; |
| 468 | } else { |
| 469 | return false; |
| 470 | } |
| 471 | } |
| 472 | |
Michael Wright | 17db18e | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 473 | std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked( |
| 474 | int32_t deviceId) { |
| 475 | std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock(); |
Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 476 | if (controller == nullptr) { |
| 477 | controller = mPolicy->obtainPointerController(deviceId); |
| 478 | mPointerController = controller; |
| 479 | updatePointerDisplayLocked(); |
| 480 | } |
| 481 | return controller; |
| 482 | } |
| 483 | |
| 484 | void InputReader::updatePointerDisplayLocked() { |
Michael Wright | 17db18e | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 485 | std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock(); |
Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 486 | 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 Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 507 | void InputReader::fadePointerLocked() { |
Michael Wright | 17db18e | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 508 | std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock(); |
Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 509 | if (controller != nullptr) { |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 510 | controller->fade(PointerControllerInterface::Transition::GRADUAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) { |
| 515 | if (when < mNextTimeout) { |
| 516 | mNextTimeout = when; |
| 517 | mEventHub->wake(); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | int32_t InputReader::bumpGenerationLocked() { |
| 522 | return ++mGeneration; |
| 523 | } |
| 524 | |
Chris Ye | 98d3f53 | 2020-10-01 21:48:59 -0700 | [diff] [blame] | 525 | std::vector<InputDeviceInfo> InputReader::getInputDevices() const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 526 | std::scoped_lock _l(mLock); |
Chris Ye | 98d3f53 | 2020-10-01 21:48:59 -0700 | [diff] [blame] | 527 | return getInputDevicesLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Chris Ye | 98d3f53 | 2020-10-01 21:48:59 -0700 | [diff] [blame] | 530 | std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const { |
| 531 | std::vector<InputDeviceInfo> outInputDevices; |
| 532 | outInputDevices.reserve(mDeviceToEventHubIdsMap.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 533 | |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 534 | for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 535 | if (!device->isIgnored()) { |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 536 | outInputDevices.push_back(device->getDeviceInfo()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 537 | } |
| 538 | } |
Chris Ye | 98d3f53 | 2020-10-01 21:48:59 -0700 | [diff] [blame] | 539 | return outInputDevices; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 542 | int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 543 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 544 | |
| 545 | return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState); |
| 546 | } |
| 547 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 548 | int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 549 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 550 | |
| 551 | return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState); |
| 552 | } |
| 553 | |
| 554 | int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 555 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 556 | |
| 557 | return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState); |
| 558 | } |
| 559 | |
| 560 | int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 561 | GetStateFunc getStateFunc) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 562 | int32_t result = AKEY_STATE_UNKNOWN; |
| 563 | if (deviceId >= 0) { |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 564 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 565 | if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 566 | result = (device->*getStateFunc)(sourceMask, code); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 567 | } |
| 568 | } else { |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 569 | for (auto& devicePair : mDevices) { |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 570 | std::shared_ptr<InputDevice>& device = devicePair.second; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 571 | if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 572 | // 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. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 574 | int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 575 | 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 Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 586 | void InputReader::toggleCapsLockState(int32_t deviceId) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 587 | std::scoped_lock _l(mLock); |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 588 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 589 | if (!device) { |
Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 590 | ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId); |
| 591 | return; |
| 592 | } |
| 593 | |
Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 594 | if (device->isIgnored()) { |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 595 | ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId); |
Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 596 | return; |
| 597 | } |
| 598 | |
| 599 | device->updateMetaState(AKEYCODE_CAPS_LOCK); |
| 600 | } |
| 601 | |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 602 | bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 603 | const std::vector<int32_t>& keyCodes, uint8_t* outFlags) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 604 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 605 | |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 606 | memset(outFlags, 0, keyCodes.size()); |
| 607 | return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 611 | const std::vector<int32_t>& keyCodes, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 612 | uint8_t* outFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 613 | bool result = false; |
| 614 | if (deviceId >= 0) { |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 615 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 616 | if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 617 | result = device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 618 | } |
| 619 | } else { |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 620 | for (auto& devicePair : mDevices) { |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 621 | std::shared_ptr<InputDevice>& device = devicePair.second; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 622 | if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 623 | result |= device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | } |
| 627 | return result; |
| 628 | } |
| 629 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 630 | int32_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 Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 642 | void InputReader::requestRefreshConfiguration(uint32_t changes) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 643 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 644 | |
| 645 | if (changes) { |
| 646 | bool needWake = !mConfigurationChangesToRefresh; |
| 647 | mConfigurationChangesToRefresh |= changes; |
| 648 | |
| 649 | if (needWake) { |
| 650 | mEventHub->wake(); |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 655 | void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat, |
| 656 | int32_t token) { |
| 657 | std::scoped_lock _l(mLock); |
| 658 | |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 659 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 660 | if (device) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 661 | notifyAll(device->vibrate(sequence, repeat, token)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| 665 | void InputReader::cancelVibrate(int32_t deviceId, int32_t token) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 666 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 667 | |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 668 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 669 | if (device) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 670 | notifyAll(device->cancelVibrate(token)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 674 | bool 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 | |
| 684 | std::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 Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 694 | void 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 | |
| 703 | bool 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 | |
| 715 | void 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 Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 724 | std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) { |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 725 | 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 Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 737 | |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 738 | if (!eventHubId) return {}; |
| 739 | const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); |
Prabir Pradhan | e287ecd | 2022-09-07 21:18:05 +0000 | [diff] [blame] | 740 | if (batteryIds.empty()) { |
| 741 | ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId); |
| 742 | return {}; |
| 743 | } |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 744 | return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front()); |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) { |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 748 | 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 Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 760 | |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 761 | if (!eventHubId) return {}; |
| 762 | const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); |
Prabir Pradhan | e287ecd | 2022-09-07 21:18:05 +0000 | [diff] [blame] | 763 | if (batteryIds.empty()) { |
| 764 | ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId); |
| 765 | return {}; |
| 766 | } |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 767 | return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front()); |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Prabir Pradhan | e287ecd | 2022-09-07 21:18:05 +0000 | [diff] [blame] | 770 | std::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 Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 792 | std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) { |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 793 | std::scoped_lock _l(mLock); |
| 794 | |
| 795 | InputDevice* device = findInputDeviceLocked(deviceId); |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 796 | if (device == nullptr) { |
| 797 | return {}; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 798 | } |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 799 | |
| 800 | return device->getDeviceInfo().getLights(); |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 803 | std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) { |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 804 | std::scoped_lock _l(mLock); |
| 805 | |
| 806 | InputDevice* device = findInputDeviceLocked(deviceId); |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 807 | if (device == nullptr) { |
| 808 | return {}; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 809 | } |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 810 | |
| 811 | return device->getDeviceInfo().getSensors(); |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | bool 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 | |
| 824 | bool 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 | |
| 834 | std::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 | |
| 844 | std::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 Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 854 | bool InputReader::isInputDeviceEnabled(int32_t deviceId) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 855 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 856 | |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 857 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 858 | if (device) { |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 859 | return device->isEnabled(); |
| 860 | } |
| 861 | ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId); |
| 862 | return false; |
| 863 | } |
| 864 | |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 865 | bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 866 | std::scoped_lock _l(mLock); |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 867 | |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 868 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 869 | if (!device) { |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 870 | ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId); |
| 871 | return false; |
| 872 | } |
| 873 | |
Arthur Hung | 2c9a334 | 2019-07-23 14:18:59 +0800 | [diff] [blame] | 874 | 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 Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 880 | // No associated display. By default, can dispatch to all displays. |
Weilun Du | d00847d | 2021-12-08 10:55:58 -0800 | [diff] [blame] | 881 | if (!associatedDisplayId || |
| 882 | *associatedDisplayId == ADISPLAY_ID_NONE) { |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 883 | return true; |
| 884 | } |
| 885 | |
| 886 | return *associatedDisplayId == displayId; |
| 887 | } |
| 888 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 889 | void InputReader::dump(std::string& dump) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 890 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 891 | |
| 892 | mEventHub->dump(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 893 | dump += "\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 894 | |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 895 | dump += StringPrintf("Input Reader State (Nums of device: %zu):\n", |
| 896 | mDeviceToEventHubIdsMap.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 897 | |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 898 | 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 Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 906 | } |
| 907 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 908 | dump += INDENT "Configuration:\n"; |
| 909 | dump += INDENT2 "ExcludedDeviceNames: ["; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 910 | for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) { |
| 911 | if (i != 0) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 912 | dump += ", "; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 913 | } |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 914 | dump += mConfig.excludedDeviceNames[i]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 915 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 916 | dump += "]\n"; |
| 917 | dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 918 | mConfig.virtualKeyQuietTime * 0.000001f); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 919 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 920 | dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: " |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 921 | "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 Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 927 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 928 | dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: " |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 929 | "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 Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 935 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 936 | dump += StringPrintf(INDENT2 "PointerGesture:\n"); |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 937 | dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled)); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 938 | dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 939 | mConfig.pointerGestureQuietInterval * 0.000001f); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 940 | dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 941 | mConfig.pointerGestureDragMinSwitchSpeed); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 942 | dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 943 | mConfig.pointerGestureTapInterval * 0.000001f); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 944 | dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 945 | mConfig.pointerGestureTapDragInterval * 0.000001f); |
| 946 | dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 947 | dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 948 | mConfig.pointerGestureMultitouchSettleInterval * 0.000001f); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 949 | dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 950 | mConfig.pointerGestureMultitouchMinDistance); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 951 | dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 952 | mConfig.pointerGestureSwipeTransitionAngleCosine); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 953 | dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 954 | mConfig.pointerGestureSwipeMaxWidthRatio); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 955 | dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 956 | mConfig.pointerGestureMovementSpeedRatio); |
| 957 | dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio); |
Santos Cordon | fa5cf46 | 2017-04-05 10:37:00 -0700 | [diff] [blame] | 958 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 959 | dump += INDENT3 "Viewports:\n"; |
Santos Cordon | fa5cf46 | 2017-04-05 10:37:00 -0700 | [diff] [blame] | 960 | mConfig.dump(dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | void InputReader::monitor() { |
| 964 | // Acquire and release the lock to ensure that the reader has not deadlocked. |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 965 | std::unique_lock<std::mutex> lock(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 966 | mEventHub->wake(); |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 967 | mReaderIsAliveCondition.wait(lock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 968 | // Check the EventHub |
| 969 | mEventHub->monitor(); |
| 970 | } |
| 971 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 972 | // --- InputReader::ContextImpl --- |
| 973 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 974 | InputReader::ContextImpl::ContextImpl(InputReader* reader) |
| 975 | : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 976 | |
| 977 | void InputReader::ContextImpl::updateGlobalMetaState() { |
| 978 | // lock is already held by the input loop |
| 979 | mReader->updateGlobalMetaStateLocked(); |
| 980 | } |
| 981 | |
| 982 | int32_t InputReader::ContextImpl::getGlobalMetaState() { |
| 983 | // lock is already held by the input loop |
| 984 | return mReader->getGlobalMetaStateLocked(); |
| 985 | } |
| 986 | |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 987 | void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) { |
| 988 | // lock is already held by the input loop |
| 989 | mReader->updateLedMetaStateLocked(metaState); |
| 990 | } |
| 991 | |
| 992 | int32_t InputReader::ContextImpl::getLedMetaState() { |
| 993 | // lock is already held by the input loop |
| 994 | return mReader->getLedMetaStateLocked(); |
| 995 | } |
| 996 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 997 | void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) { |
| 998 | // lock is already held by the input loop |
| 999 | mReader->disableVirtualKeysUntilLocked(time); |
| 1000 | } |
| 1001 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 1002 | bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode, |
| 1003 | int32_t scanCode) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1004 | // lock is already held by the input loop |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 1005 | return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | void InputReader::ContextImpl::fadePointer() { |
| 1009 | // lock is already held by the input loop |
| 1010 | mReader->fadePointerLocked(); |
| 1011 | } |
| 1012 | |
Michael Wright | 17db18e | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 1013 | std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController( |
| 1014 | int32_t deviceId) { |
Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 1015 | // lock is already held by the input loop |
| 1016 | return mReader->getPointerControllerLocked(deviceId); |
| 1017 | } |
| 1018 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1019 | void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) { |
| 1020 | // lock is already held by the input loop |
| 1021 | mReader->requestTimeoutAtTimeLocked(when); |
| 1022 | } |
| 1023 | |
| 1024 | int32_t InputReader::ContextImpl::bumpGeneration() { |
| 1025 | // lock is already held by the input loop |
| 1026 | return mReader->bumpGenerationLocked(); |
| 1027 | } |
| 1028 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1029 | void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) { |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1030 | // lock is already held by whatever called refreshConfigurationLocked |
| 1031 | mReader->getExternalStylusDevicesLocked(outDevices); |
| 1032 | } |
| 1033 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame^] | 1034 | std::list<NotifyArgs> InputReader::ContextImpl::dispatchExternalStylusState( |
| 1035 | const StylusState& state) { |
| 1036 | return mReader->dispatchExternalStylusStateLocked(state); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1039 | InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() { |
| 1040 | return mReader->mPolicy.get(); |
| 1041 | } |
| 1042 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1043 | EventHubInterface* InputReader::ContextImpl::getEventHub() { |
| 1044 | return mReader->mEventHub.get(); |
| 1045 | } |
| 1046 | |
Siarhei Vishniakou | f2f073b | 2021-02-09 21:59:56 +0000 | [diff] [blame] | 1047 | int32_t InputReader::ContextImpl::getNextId() { |
| 1048 | return mIdGenerator.nextId(); |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 1049 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1051 | } // namespace android |