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