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