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