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 | |
| 123 | size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE); |
| 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 | |
| 129 | if (count) { |
| 130 | processEventsLocked(mEventBuffer, count); |
| 131 | } |
| 132 | |
| 133 | if (mNextTimeout != LLONG_MAX) { |
| 134 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 135 | if (now >= mNextTimeout) { |
Siarhei 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; |
| 140 | timeoutExpiredLocked(now); |
| 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 | |
| 165 | void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) { |
| 166 | for (const RawEvent* rawEvent = rawEvents; count;) { |
| 167 | int32_t type = rawEvent->type; |
| 168 | size_t batchSize = 1; |
| 169 | if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) { |
| 170 | int32_t deviceId = rawEvent->deviceId; |
| 171 | while (batchSize < count) { |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 172 | if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT || |
| 173 | rawEvent[batchSize].deviceId != deviceId) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 174 | break; |
| 175 | } |
| 176 | batchSize += 1; |
| 177 | } |
Siarhei Vishniakou | 465e1c0 | 2021-12-09 10:47:29 -0800 | [diff] [blame] | 178 | if (DEBUG_RAW_EVENTS) { |
| 179 | ALOGD("BatchSize: %zu Count: %zu", batchSize, count); |
| 180 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 181 | processEventsForDeviceLocked(deviceId, rawEvent, batchSize); |
| 182 | } else { |
| 183 | switch (rawEvent->type) { |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 184 | case EventHubInterface::DEVICE_ADDED: |
| 185 | addDeviceLocked(rawEvent->when, rawEvent->deviceId); |
| 186 | break; |
| 187 | case EventHubInterface::DEVICE_REMOVED: |
| 188 | removeDeviceLocked(rawEvent->when, rawEvent->deviceId); |
| 189 | break; |
| 190 | case EventHubInterface::FINISHED_DEVICE_SCAN: |
| 191 | handleConfigurationChangedLocked(rawEvent->when); |
| 192 | break; |
| 193 | default: |
| 194 | ALOG_ASSERT(false); // can't happen |
| 195 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | count -= batchSize; |
| 199 | rawEvent += batchSize; |
| 200 | } |
| 201 | } |
| 202 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 203 | void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) { |
| 204 | if (mDevices.find(eventHubId) != mDevices.end()) { |
| 205 | ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 206 | return; |
| 207 | } |
| 208 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 209 | InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId); |
| 210 | std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 211 | device->configure(when, &mConfig, 0); |
| 212 | device->reset(when); |
| 213 | |
| 214 | if (device->isIgnored()) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 215 | ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' " |
| 216 | "(ignored non-input device)", |
| 217 | device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 218 | } else { |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 219 | 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] | 220 | device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(), |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 221 | inputEventSourceToString(device->getSources()).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 224 | mDevices.emplace(eventHubId, device); |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 225 | // Add device to device to EventHub ids map. |
| 226 | const auto mapIt = mDeviceToEventHubIdsMap.find(device); |
| 227 | if (mapIt == mDeviceToEventHubIdsMap.end()) { |
| 228 | std::vector<int32_t> ids = {eventHubId}; |
| 229 | mDeviceToEventHubIdsMap.emplace(device, ids); |
| 230 | } else { |
| 231 | mapIt->second.push_back(eventHubId); |
| 232 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 233 | bumpGenerationLocked(); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 234 | |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 235 | if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) { |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 236 | notifyExternalStylusPresenceChangedLocked(); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 237 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 238 | |
| 239 | // Sensor input device is noisy, to save power disable it by default. |
Chris Ye | e14523a | 2020-12-19 13:46:00 -0800 | [diff] [blame] | 240 | // Input device is classified as SENSOR when any sub device is a SENSOR device, check Eventhub |
| 241 | // device class to disable SENSOR sub device only. |
| 242 | if (mEventHub->getDeviceClasses(eventHubId).test(InputDeviceClass::SENSOR)) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 243 | mEventHub->disableDevice(eventHubId); |
| 244 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 247 | void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) { |
| 248 | auto deviceIt = mDevices.find(eventHubId); |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 249 | if (deviceIt == mDevices.end()) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 250 | ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 251 | return; |
| 252 | } |
| 253 | |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 254 | std::shared_ptr<InputDevice> device = std::move(deviceIt->second); |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 255 | mDevices.erase(deviceIt); |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 256 | // Erase device from device to EventHub ids map. |
| 257 | auto mapIt = mDeviceToEventHubIdsMap.find(device); |
| 258 | if (mapIt != mDeviceToEventHubIdsMap.end()) { |
| 259 | std::vector<int32_t>& eventHubIds = mapIt->second; |
Siarhei Vishniakou | f47c339e | 2021-12-30 11:22:26 -0800 | [diff] [blame] | 260 | std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; }); |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 261 | if (eventHubIds.size() == 0) { |
| 262 | mDeviceToEventHubIdsMap.erase(mapIt); |
| 263 | } |
| 264 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 265 | bumpGenerationLocked(); |
| 266 | |
| 267 | if (device->isIgnored()) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 268 | ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' " |
| 269 | "(ignored non-input device)", |
| 270 | device->getId(), eventHubId, device->getName().c_str(), |
| 271 | device->getDescriptor().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 272 | } else { |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 273 | 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] | 274 | device->getId(), eventHubId, device->getName().c_str(), |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 275 | device->getDescriptor().c_str(), |
| 276 | inputEventSourceToString(device->getSources()).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 279 | device->removeEventHubDevice(eventHubId); |
| 280 | |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 281 | if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS)) { |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 282 | notifyExternalStylusPresenceChangedLocked(); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 285 | if (device->hasEventHubDevices()) { |
| 286 | device->configure(when, &mConfig, 0); |
| 287 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 288 | device->reset(when); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 291 | std::shared_ptr<InputDevice> InputReader::createDeviceLocked( |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 292 | int32_t eventHubId, const InputDeviceIdentifier& identifier) { |
| 293 | auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) { |
Josh Bartel | 938632f | 2022-07-19 15:34:22 -0500 | [diff] [blame] | 294 | const InputDeviceIdentifier identifier2 = |
| 295 | devicePair.second->getDeviceInfo().getIdentifier(); |
| 296 | return isSubDevice(identifier, identifier2); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 297 | }); |
| 298 | |
| 299 | std::shared_ptr<InputDevice> device; |
| 300 | if (deviceIt != mDevices.end()) { |
| 301 | device = deviceIt->second; |
| 302 | } else { |
| 303 | int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked(); |
| 304 | device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(), |
| 305 | identifier); |
| 306 | } |
| 307 | device->addEventHubDevice(eventHubId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 308 | return device; |
| 309 | } |
| 310 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 311 | void InputReader::processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 312 | size_t count) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 313 | auto deviceIt = mDevices.find(eventHubId); |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 314 | if (deviceIt == mDevices.end()) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 315 | ALOGW("Discarding event for unknown eventHubId %d.", eventHubId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 319 | std::shared_ptr<InputDevice>& device = deviceIt->second; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 320 | if (device->isIgnored()) { |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 321 | // ALOGD("Discarding event for ignored deviceId %d.", deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 322 | return; |
| 323 | } |
| 324 | |
| 325 | device->process(rawEvents, count); |
| 326 | } |
| 327 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 328 | InputDevice* InputReader::findInputDeviceLocked(int32_t deviceId) const { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 329 | auto deviceIt = |
| 330 | std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) { |
| 331 | return devicePair.second->getId() == deviceId; |
| 332 | }); |
| 333 | if (deviceIt != mDevices.end()) { |
| 334 | return deviceIt->second.get(); |
| 335 | } |
| 336 | return nullptr; |
| 337 | } |
| 338 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 339 | void InputReader::timeoutExpiredLocked(nsecs_t when) { |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 340 | for (auto& devicePair : mDevices) { |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 341 | std::shared_ptr<InputDevice>& device = devicePair.second; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 342 | if (!device->isIgnored()) { |
| 343 | device->timeoutExpired(when); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 348 | int32_t InputReader::nextInputDeviceIdLocked() { |
| 349 | return ++mNextInputDeviceId; |
| 350 | } |
| 351 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 352 | void InputReader::handleConfigurationChangedLocked(nsecs_t when) { |
| 353 | // Reset global meta state because it depends on the list of all configured devices. |
| 354 | updateGlobalMetaStateLocked(); |
| 355 | |
| 356 | // Enqueue configuration changed. |
Siarhei Vishniakou | f2f073b | 2021-02-09 21:59:56 +0000 | [diff] [blame] | 357 | NotifyConfigurationChangedArgs args(mContext.getNextId(), when); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 358 | mQueuedListener.notifyConfigurationChanged(&args); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void InputReader::refreshConfigurationLocked(uint32_t changes) { |
| 362 | mPolicy->getReaderConfiguration(&mConfig); |
| 363 | mEventHub->setExcludedDevices(mConfig.excludedDeviceNames); |
| 364 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 365 | if (!changes) return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 366 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 367 | ALOGI("Reconfiguring input devices, changes=%s", |
| 368 | InputReaderConfiguration::changesToString(changes).c_str()); |
| 369 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 370 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 371 | if (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO) { |
| 372 | updatePointerDisplayLocked(); |
| 373 | } |
| 374 | |
| 375 | if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) { |
| 376 | mEventHub->requestReopenDevices(); |
| 377 | } else { |
| 378 | for (auto& devicePair : mDevices) { |
| 379 | std::shared_ptr<InputDevice>& device = devicePair.second; |
| 380 | device->configure(now, &mConfig, changes); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 381 | } |
| 382 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 383 | |
| 384 | if (changes & InputReaderConfiguration::CHANGE_POINTER_CAPTURE) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 385 | if (mCurrentPointerCaptureRequest == mConfig.pointerCaptureRequest) { |
| 386 | ALOGV("Skipping notifying pointer capture changes: " |
| 387 | "There was no change in the pointer capture state."); |
| 388 | } else { |
| 389 | mCurrentPointerCaptureRequest = mConfig.pointerCaptureRequest; |
| 390 | const NotifyPointerCaptureChangedArgs args(mContext.getNextId(), now, |
| 391 | mCurrentPointerCaptureRequest); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 392 | mQueuedListener.notifyPointerCaptureChanged(&args); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 393 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 394 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void InputReader::updateGlobalMetaStateLocked() { |
| 398 | mGlobalMetaState = 0; |
| 399 | |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 400 | for (auto& devicePair : mDevices) { |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 401 | std::shared_ptr<InputDevice>& device = devicePair.second; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 402 | mGlobalMetaState |= device->getMetaState(); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | int32_t InputReader::getGlobalMetaStateLocked() { |
| 407 | return mGlobalMetaState; |
| 408 | } |
| 409 | |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 410 | void InputReader::updateLedMetaStateLocked(int32_t metaState) { |
| 411 | mLedMetaState = metaState; |
| 412 | for (auto& devicePair : mDevices) { |
| 413 | std::shared_ptr<InputDevice>& device = devicePair.second; |
| 414 | device->updateLedState(false); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | int32_t InputReader::getLedMetaStateLocked() { |
| 419 | return mLedMetaState; |
| 420 | } |
| 421 | |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 422 | void InputReader::notifyExternalStylusPresenceChangedLocked() { |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 423 | refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE); |
| 424 | } |
| 425 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 426 | void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) { |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 427 | for (auto& devicePair : mDevices) { |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 428 | std::shared_ptr<InputDevice>& device = devicePair.second; |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 429 | if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) { |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 430 | outDevices.push_back(device->getDeviceInfo()); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
Arthur Hung | cadce9d | 2021-05-07 17:24:04 +0800 | [diff] [blame] | 435 | void InputReader::dispatchExternalStylusStateLocked(const StylusState& state) { |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 436 | for (auto& devicePair : mDevices) { |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 437 | std::shared_ptr<InputDevice>& device = devicePair.second; |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 438 | device->updateExternalStylusState(state); |
| 439 | } |
| 440 | } |
| 441 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 442 | void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) { |
| 443 | mDisableVirtualKeysTimeout = time; |
| 444 | } |
| 445 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 446 | bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 447 | if (now < mDisableVirtualKeysTimeout) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 448 | ALOGI("Dropping virtual key from device because virtual keys are " |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 449 | "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d", |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 450 | (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 451 | return true; |
| 452 | } else { |
| 453 | return false; |
| 454 | } |
| 455 | } |
| 456 | |
Michael Wright | 17db18e | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 457 | std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked( |
| 458 | int32_t deviceId) { |
| 459 | std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock(); |
Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 460 | if (controller == nullptr) { |
| 461 | controller = mPolicy->obtainPointerController(deviceId); |
| 462 | mPointerController = controller; |
| 463 | updatePointerDisplayLocked(); |
| 464 | } |
| 465 | return controller; |
| 466 | } |
| 467 | |
| 468 | void InputReader::updatePointerDisplayLocked() { |
Michael Wright | 17db18e | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 469 | std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock(); |
Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 470 | if (controller == nullptr) { |
| 471 | return; |
| 472 | } |
| 473 | |
| 474 | std::optional<DisplayViewport> viewport = |
| 475 | mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId); |
| 476 | if (!viewport) { |
| 477 | ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input " |
| 478 | "mapper. Fall back to default display", |
| 479 | mConfig.defaultPointerDisplayId); |
| 480 | viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT); |
| 481 | } |
| 482 | if (!viewport) { |
| 483 | ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to" |
| 484 | " PointerController."); |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | controller->setDisplayViewport(*viewport); |
| 489 | } |
| 490 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 491 | void InputReader::fadePointerLocked() { |
Michael Wright | 17db18e | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 492 | std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock(); |
Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 493 | if (controller != nullptr) { |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 494 | controller->fade(PointerControllerInterface::Transition::GRADUAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) { |
| 499 | if (when < mNextTimeout) { |
| 500 | mNextTimeout = when; |
| 501 | mEventHub->wake(); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | int32_t InputReader::bumpGenerationLocked() { |
| 506 | return ++mGeneration; |
| 507 | } |
| 508 | |
Chris Ye | 98d3f53 | 2020-10-01 21:48:59 -0700 | [diff] [blame] | 509 | std::vector<InputDeviceInfo> InputReader::getInputDevices() const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 510 | std::scoped_lock _l(mLock); |
Chris Ye | 98d3f53 | 2020-10-01 21:48:59 -0700 | [diff] [blame] | 511 | return getInputDevicesLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Chris Ye | 98d3f53 | 2020-10-01 21:48:59 -0700 | [diff] [blame] | 514 | std::vector<InputDeviceInfo> InputReader::getInputDevicesLocked() const { |
| 515 | std::vector<InputDeviceInfo> outInputDevices; |
| 516 | outInputDevices.reserve(mDeviceToEventHubIdsMap.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 517 | |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 518 | for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 519 | if (!device->isIgnored()) { |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 520 | outInputDevices.push_back(device->getDeviceInfo()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 521 | } |
| 522 | } |
Chris Ye | 98d3f53 | 2020-10-01 21:48:59 -0700 | [diff] [blame] | 523 | return outInputDevices; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 526 | 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] | 527 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 528 | |
| 529 | return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState); |
| 530 | } |
| 531 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 532 | 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] | 533 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 534 | |
| 535 | return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState); |
| 536 | } |
| 537 | |
| 538 | 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] | 539 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 540 | |
| 541 | return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState); |
| 542 | } |
| 543 | |
| 544 | 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] | 545 | GetStateFunc getStateFunc) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 546 | int32_t result = AKEY_STATE_UNKNOWN; |
| 547 | if (deviceId >= 0) { |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 548 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 549 | if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 550 | result = (device->*getStateFunc)(sourceMask, code); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 551 | } |
| 552 | } else { |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 553 | for (auto& devicePair : mDevices) { |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 554 | std::shared_ptr<InputDevice>& device = devicePair.second; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 555 | if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 556 | // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that |
| 557 | // 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] | 558 | int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 559 | if (currentResult >= AKEY_STATE_DOWN) { |
| 560 | return currentResult; |
| 561 | } else if (currentResult == AKEY_STATE_UP) { |
| 562 | result = currentResult; |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | return result; |
| 568 | } |
| 569 | |
Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 570 | void InputReader::toggleCapsLockState(int32_t deviceId) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 571 | std::scoped_lock _l(mLock); |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 572 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 573 | if (!device) { |
Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 574 | ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId); |
| 575 | return; |
| 576 | } |
| 577 | |
Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 578 | if (device->isIgnored()) { |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 579 | ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId); |
Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 580 | return; |
| 581 | } |
| 582 | |
| 583 | device->updateMetaState(AKEYCODE_CAPS_LOCK); |
| 584 | } |
| 585 | |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 586 | bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 587 | const std::vector<int32_t>& keyCodes, uint8_t* outFlags) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 588 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 589 | |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 590 | memset(outFlags, 0, keyCodes.size()); |
| 591 | return markSupportedKeyCodesLocked(deviceId, sourceMask, keyCodes, outFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 595 | const std::vector<int32_t>& keyCodes, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 596 | uint8_t* outFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 597 | bool result = false; |
| 598 | if (deviceId >= 0) { |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 599 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 600 | if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 601 | result = device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | } |
| 603 | } else { |
Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 604 | for (auto& devicePair : mDevices) { |
Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 605 | std::shared_ptr<InputDevice>& device = devicePair.second; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 606 | if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 607 | result |= device->markSupportedKeyCodes(sourceMask, keyCodes, outFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | } |
| 611 | return result; |
| 612 | } |
| 613 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 614 | int32_t InputReader::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const { |
| 615 | std::scoped_lock _l(mLock); |
| 616 | |
| 617 | InputDevice* device = findInputDeviceLocked(deviceId); |
| 618 | if (device == nullptr) { |
| 619 | ALOGW("Failed to get key code for key location: Input device with id %d not found", |
| 620 | deviceId); |
| 621 | return AKEYCODE_UNKNOWN; |
| 622 | } |
| 623 | return device->getKeyCodeForKeyLocation(locationKeyCode); |
| 624 | } |
| 625 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 626 | void InputReader::requestRefreshConfiguration(uint32_t changes) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 627 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 628 | |
| 629 | if (changes) { |
| 630 | bool needWake = !mConfigurationChangesToRefresh; |
| 631 | mConfigurationChangesToRefresh |= changes; |
| 632 | |
| 633 | if (needWake) { |
| 634 | mEventHub->wake(); |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 639 | void InputReader::vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat, |
| 640 | int32_t token) { |
| 641 | std::scoped_lock _l(mLock); |
| 642 | |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 643 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 644 | if (device) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 645 | device->vibrate(sequence, repeat, token); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
| 649 | void InputReader::cancelVibrate(int32_t deviceId, int32_t token) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 650 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 651 | |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 652 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 653 | if (device) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 654 | device->cancelVibrate(token); |
| 655 | } |
| 656 | } |
| 657 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 658 | bool InputReader::isVibrating(int32_t deviceId) { |
| 659 | std::scoped_lock _l(mLock); |
| 660 | |
| 661 | InputDevice* device = findInputDeviceLocked(deviceId); |
| 662 | if (device) { |
| 663 | return device->isVibrating(); |
| 664 | } |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | std::vector<int32_t> InputReader::getVibratorIds(int32_t deviceId) { |
| 669 | std::scoped_lock _l(mLock); |
| 670 | |
| 671 | InputDevice* device = findInputDeviceLocked(deviceId); |
| 672 | if (device) { |
| 673 | return device->getVibratorIds(); |
| 674 | } |
| 675 | return {}; |
| 676 | } |
| 677 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 678 | void InputReader::disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) { |
| 679 | std::scoped_lock _l(mLock); |
| 680 | |
| 681 | InputDevice* device = findInputDeviceLocked(deviceId); |
| 682 | if (device) { |
| 683 | device->disableSensor(sensorType); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | bool InputReader::enableSensor(int32_t deviceId, InputDeviceSensorType sensorType, |
| 688 | std::chrono::microseconds samplingPeriod, |
| 689 | std::chrono::microseconds maxBatchReportLatency) { |
| 690 | std::scoped_lock _l(mLock); |
| 691 | |
| 692 | InputDevice* device = findInputDeviceLocked(deviceId); |
| 693 | if (device) { |
| 694 | return device->enableSensor(sensorType, samplingPeriod, maxBatchReportLatency); |
| 695 | } |
| 696 | return false; |
| 697 | } |
| 698 | |
| 699 | void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) { |
| 700 | std::scoped_lock _l(mLock); |
| 701 | |
| 702 | InputDevice* device = findInputDeviceLocked(deviceId); |
| 703 | if (device) { |
| 704 | device->flushSensor(sensorType); |
| 705 | } |
| 706 | } |
| 707 | |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 708 | std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) { |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 709 | std::optional<int32_t> eventHubId; |
| 710 | { |
| 711 | // Do not query the battery state while holding the lock. For some peripheral devices, |
| 712 | // reading battery state can be broken and take 5+ seconds. Holding the lock in this case |
| 713 | // would block all other event processing during this time. For now, we assume this |
| 714 | // call never happens on the InputReader thread and get the battery state outside the |
| 715 | // lock to prevent event processing from being blocked by this call. |
| 716 | std::scoped_lock _l(mLock); |
| 717 | InputDevice* device = findInputDeviceLocked(deviceId); |
| 718 | if (!device) return {}; |
| 719 | eventHubId = device->getBatteryEventHubId(); |
| 720 | } // release lock |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 721 | |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 722 | if (!eventHubId) return {}; |
| 723 | const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); |
Prabir Pradhan | e287ecd | 2022-09-07 21:18:05 +0000 | [diff] [blame] | 724 | if (batteryIds.empty()) { |
| 725 | ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId); |
| 726 | return {}; |
| 727 | } |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 728 | return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front()); |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) { |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 732 | std::optional<int32_t> eventHubId; |
| 733 | { |
| 734 | // Do not query the battery state while holding the lock. For some peripheral devices, |
| 735 | // reading battery state can be broken and take 5+ seconds. Holding the lock in this case |
| 736 | // would block all other event processing during this time. For now, we assume this |
| 737 | // call never happens on the InputReader thread and get the battery state outside the |
| 738 | // lock to prevent event processing from being blocked by this call. |
| 739 | std::scoped_lock _l(mLock); |
| 740 | InputDevice* device = findInputDeviceLocked(deviceId); |
| 741 | if (!device) return {}; |
| 742 | eventHubId = device->getBatteryEventHubId(); |
| 743 | } // release lock |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 744 | |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 745 | if (!eventHubId) return {}; |
| 746 | const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); |
Prabir Pradhan | e287ecd | 2022-09-07 21:18:05 +0000 | [diff] [blame] | 747 | if (batteryIds.empty()) { |
| 748 | ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId); |
| 749 | return {}; |
| 750 | } |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 751 | return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front()); |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 752 | } |
| 753 | |
Prabir Pradhan | e287ecd | 2022-09-07 21:18:05 +0000 | [diff] [blame] | 754 | std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) { |
| 755 | std::scoped_lock _l(mLock); |
| 756 | |
| 757 | InputDevice* device = findInputDeviceLocked(deviceId); |
| 758 | if (!device) return {}; |
| 759 | |
| 760 | std::optional<int32_t> eventHubId = device->getBatteryEventHubId(); |
| 761 | if (!eventHubId) return {}; |
| 762 | const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); |
| 763 | if (batteryIds.empty()) { |
| 764 | ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId); |
| 765 | return {}; |
| 766 | } |
| 767 | const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front()); |
| 768 | if (!batteryInfo) { |
| 769 | ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__, |
| 770 | batteryIds.front(), *eventHubId); |
| 771 | return {}; |
| 772 | } |
| 773 | return batteryInfo->path; |
| 774 | } |
| 775 | |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 776 | std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) { |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 777 | std::scoped_lock _l(mLock); |
| 778 | |
| 779 | InputDevice* device = findInputDeviceLocked(deviceId); |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 780 | if (device == nullptr) { |
| 781 | return {}; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 782 | } |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 783 | |
| 784 | return device->getDeviceInfo().getLights(); |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 785 | } |
| 786 | |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 787 | std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) { |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 788 | std::scoped_lock _l(mLock); |
| 789 | |
| 790 | InputDevice* device = findInputDeviceLocked(deviceId); |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 791 | if (device == nullptr) { |
| 792 | return {}; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 793 | } |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 794 | |
| 795 | return device->getDeviceInfo().getSensors(); |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) { |
| 799 | std::scoped_lock _l(mLock); |
| 800 | |
| 801 | InputDevice* device = findInputDeviceLocked(deviceId); |
| 802 | if (device) { |
| 803 | return device->setLightColor(lightId, color); |
| 804 | } |
| 805 | return false; |
| 806 | } |
| 807 | |
| 808 | bool InputReader::setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) { |
| 809 | std::scoped_lock _l(mLock); |
| 810 | |
| 811 | InputDevice* device = findInputDeviceLocked(deviceId); |
| 812 | if (device) { |
| 813 | return device->setLightPlayerId(lightId, playerId); |
| 814 | } |
| 815 | return false; |
| 816 | } |
| 817 | |
| 818 | std::optional<int32_t> InputReader::getLightColor(int32_t deviceId, int32_t lightId) { |
| 819 | std::scoped_lock _l(mLock); |
| 820 | |
| 821 | InputDevice* device = findInputDeviceLocked(deviceId); |
| 822 | if (device) { |
| 823 | return device->getLightColor(lightId); |
| 824 | } |
| 825 | return std::nullopt; |
| 826 | } |
| 827 | |
| 828 | std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t lightId) { |
| 829 | std::scoped_lock _l(mLock); |
| 830 | |
| 831 | InputDevice* device = findInputDeviceLocked(deviceId); |
| 832 | if (device) { |
| 833 | return device->getLightPlayerId(lightId); |
| 834 | } |
| 835 | return std::nullopt; |
| 836 | } |
| 837 | |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 838 | bool InputReader::isInputDeviceEnabled(int32_t deviceId) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 839 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 840 | |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 841 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 842 | if (device) { |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 843 | return device->isEnabled(); |
| 844 | } |
| 845 | ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId); |
| 846 | return false; |
| 847 | } |
| 848 | |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 849 | bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 850 | std::scoped_lock _l(mLock); |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 851 | |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 852 | InputDevice* device = findInputDeviceLocked(deviceId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 853 | if (!device) { |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 854 | ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId); |
| 855 | return false; |
| 856 | } |
| 857 | |
Arthur Hung | 2c9a334 | 2019-07-23 14:18:59 +0800 | [diff] [blame] | 858 | if (!device->isEnabled()) { |
| 859 | ALOGW("Ignoring disabled device %s", device->getName().c_str()); |
| 860 | return false; |
| 861 | } |
| 862 | |
| 863 | std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId(); |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 864 | // No associated display. By default, can dispatch to all displays. |
Weilun Du | d00847d | 2021-12-08 10:55:58 -0800 | [diff] [blame] | 865 | if (!associatedDisplayId || |
| 866 | *associatedDisplayId == ADISPLAY_ID_NONE) { |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 867 | return true; |
| 868 | } |
| 869 | |
| 870 | return *associatedDisplayId == displayId; |
| 871 | } |
| 872 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 873 | void InputReader::dump(std::string& dump) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 874 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 875 | |
| 876 | mEventHub->dump(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 877 | dump += "\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 878 | |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 879 | dump += StringPrintf("Input Reader State (Nums of device: %zu):\n", |
| 880 | mDeviceToEventHubIdsMap.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 881 | |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 882 | for (const auto& devicePair : mDeviceToEventHubIdsMap) { |
| 883 | const std::shared_ptr<InputDevice>& device = devicePair.first; |
| 884 | std::string eventHubDevStr = INDENT "EventHub Devices: [ "; |
| 885 | for (const auto& eId : devicePair.second) { |
| 886 | eventHubDevStr += StringPrintf("%d ", eId); |
| 887 | } |
| 888 | eventHubDevStr += "] \n"; |
| 889 | device->dump(dump, eventHubDevStr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 890 | } |
| 891 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 892 | dump += INDENT "Configuration:\n"; |
| 893 | dump += INDENT2 "ExcludedDeviceNames: ["; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 894 | for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) { |
| 895 | if (i != 0) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 896 | dump += ", "; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 897 | } |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 898 | dump += mConfig.excludedDeviceNames[i]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 899 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 900 | dump += "]\n"; |
| 901 | dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 902 | mConfig.virtualKeyQuietTime * 0.000001f); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 903 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 904 | dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: " |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 905 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, " |
| 906 | "acceleration=%0.3f\n", |
| 907 | mConfig.pointerVelocityControlParameters.scale, |
| 908 | mConfig.pointerVelocityControlParameters.lowThreshold, |
| 909 | mConfig.pointerVelocityControlParameters.highThreshold, |
| 910 | mConfig.pointerVelocityControlParameters.acceleration); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 911 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 912 | dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: " |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 913 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, " |
| 914 | "acceleration=%0.3f\n", |
| 915 | mConfig.wheelVelocityControlParameters.scale, |
| 916 | mConfig.wheelVelocityControlParameters.lowThreshold, |
| 917 | mConfig.wheelVelocityControlParameters.highThreshold, |
| 918 | mConfig.wheelVelocityControlParameters.acceleration); |
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 "PointerGesture:\n"); |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 921 | dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled)); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 922 | dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 923 | mConfig.pointerGestureQuietInterval * 0.000001f); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 924 | dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 925 | mConfig.pointerGestureDragMinSwitchSpeed); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 926 | dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 927 | mConfig.pointerGestureTapInterval * 0.000001f); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 928 | dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 929 | mConfig.pointerGestureTapDragInterval * 0.000001f); |
| 930 | dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 931 | dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 932 | mConfig.pointerGestureMultitouchSettleInterval * 0.000001f); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 933 | dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 934 | mConfig.pointerGestureMultitouchMinDistance); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 935 | dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 936 | mConfig.pointerGestureSwipeTransitionAngleCosine); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 937 | dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 938 | mConfig.pointerGestureSwipeMaxWidthRatio); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 939 | dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 940 | mConfig.pointerGestureMovementSpeedRatio); |
| 941 | dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio); |
Santos Cordon | fa5cf46 | 2017-04-05 10:37:00 -0700 | [diff] [blame] | 942 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 943 | dump += INDENT3 "Viewports:\n"; |
Santos Cordon | fa5cf46 | 2017-04-05 10:37:00 -0700 | [diff] [blame] | 944 | mConfig.dump(dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | void InputReader::monitor() { |
| 948 | // 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] | 949 | std::unique_lock<std::mutex> lock(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 950 | mEventHub->wake(); |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 951 | mReaderIsAliveCondition.wait(lock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 952 | // Check the EventHub |
| 953 | mEventHub->monitor(); |
| 954 | } |
| 955 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 956 | // --- InputReader::ContextImpl --- |
| 957 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 958 | InputReader::ContextImpl::ContextImpl(InputReader* reader) |
| 959 | : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 960 | |
| 961 | void InputReader::ContextImpl::updateGlobalMetaState() { |
| 962 | // lock is already held by the input loop |
| 963 | mReader->updateGlobalMetaStateLocked(); |
| 964 | } |
| 965 | |
| 966 | int32_t InputReader::ContextImpl::getGlobalMetaState() { |
| 967 | // lock is already held by the input loop |
| 968 | return mReader->getGlobalMetaStateLocked(); |
| 969 | } |
| 970 | |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 971 | void InputReader::ContextImpl::updateLedMetaState(int32_t metaState) { |
| 972 | // lock is already held by the input loop |
| 973 | mReader->updateLedMetaStateLocked(metaState); |
| 974 | } |
| 975 | |
| 976 | int32_t InputReader::ContextImpl::getLedMetaState() { |
| 977 | // lock is already held by the input loop |
| 978 | return mReader->getLedMetaStateLocked(); |
| 979 | } |
| 980 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 981 | void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) { |
| 982 | // lock is already held by the input loop |
| 983 | mReader->disableVirtualKeysUntilLocked(time); |
| 984 | } |
| 985 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 986 | bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode, |
| 987 | int32_t scanCode) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 988 | // lock is already held by the input loop |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 989 | return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | void InputReader::ContextImpl::fadePointer() { |
| 993 | // lock is already held by the input loop |
| 994 | mReader->fadePointerLocked(); |
| 995 | } |
| 996 | |
Michael Wright | 17db18e | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 997 | std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController( |
| 998 | int32_t deviceId) { |
Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 999 | // lock is already held by the input loop |
| 1000 | return mReader->getPointerControllerLocked(deviceId); |
| 1001 | } |
| 1002 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1003 | void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) { |
| 1004 | // lock is already held by the input loop |
| 1005 | mReader->requestTimeoutAtTimeLocked(when); |
| 1006 | } |
| 1007 | |
| 1008 | int32_t InputReader::ContextImpl::bumpGeneration() { |
| 1009 | // lock is already held by the input loop |
| 1010 | return mReader->bumpGenerationLocked(); |
| 1011 | } |
| 1012 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1013 | void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) { |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1014 | // lock is already held by whatever called refreshConfigurationLocked |
| 1015 | mReader->getExternalStylusDevicesLocked(outDevices); |
| 1016 | } |
| 1017 | |
| 1018 | void InputReader::ContextImpl::dispatchExternalStylusState(const StylusState& state) { |
Arthur Hung | cadce9d | 2021-05-07 17:24:04 +0800 | [diff] [blame] | 1019 | mReader->dispatchExternalStylusStateLocked(state); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1022 | InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() { |
| 1023 | return mReader->mPolicy.get(); |
| 1024 | } |
| 1025 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1026 | InputListenerInterface& InputReader::ContextImpl::getListener() { |
| 1027 | return mReader->mQueuedListener; |
Siarhei Vishniakou | f2f073b | 2021-02-09 21:59:56 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1030 | EventHubInterface* InputReader::ContextImpl::getEventHub() { |
| 1031 | return mReader->mEventHub.get(); |
| 1032 | } |
| 1033 | |
Siarhei Vishniakou | f2f073b | 2021-02-09 21:59:56 +0000 | [diff] [blame] | 1034 | int32_t InputReader::ContextImpl::getNextId() { |
| 1035 | return mIdGenerator.nextId(); |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 1036 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1037 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1038 | } // namespace android |