| 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); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 209 |     bumpGenerationLocked(); | 
| Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 210 |  | 
 | 211 |     if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) { | 
 | 212 |         notifyExternalStylusPresenceChanged(); | 
 | 213 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 214 | } | 
 | 215 |  | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 216 | void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) { | 
 | 217 |     auto deviceIt = mDevices.find(eventHubId); | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 218 |     if (deviceIt == mDevices.end()) { | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 219 |         ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 220 |         return; | 
 | 221 |     } | 
 | 222 |  | 
| Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 223 |     std::shared_ptr<InputDevice> device = std::move(deviceIt->second); | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 224 |     mDevices.erase(deviceIt); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 225 |     bumpGenerationLocked(); | 
 | 226 |  | 
 | 227 |     if (device->isIgnored()) { | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 228 |         ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' " | 
 | 229 |               "(ignored non-input device)", | 
 | 230 |               device->getId(), eventHubId, device->getName().c_str(), | 
 | 231 |               device->getDescriptor().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 232 |     } else { | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 233 |         ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=0x%08x", | 
 | 234 |               device->getId(), eventHubId, device->getName().c_str(), | 
 | 235 |               device->getDescriptor().c_str(), device->getSources()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 236 |     } | 
 | 237 |  | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 238 |     device->removeEventHubDevice(eventHubId); | 
 | 239 |  | 
| Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 240 |     if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) { | 
 | 241 |         notifyExternalStylusPresenceChanged(); | 
 | 242 |     } | 
 | 243 |  | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 244 |     if (device->hasEventHubDevices()) { | 
 | 245 |         device->configure(when, &mConfig, 0); | 
 | 246 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 247 |     device->reset(when); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 248 | } | 
 | 249 |  | 
| Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 250 | std::shared_ptr<InputDevice> InputReader::createDeviceLocked( | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 251 |         int32_t eventHubId, const InputDeviceIdentifier& identifier) { | 
 | 252 |     auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) { | 
 | 253 |         return devicePair.second->getDescriptor().size() && identifier.descriptor.size() && | 
 | 254 |                 devicePair.second->getDescriptor() == identifier.descriptor; | 
 | 255 |     }); | 
 | 256 |  | 
 | 257 |     std::shared_ptr<InputDevice> device; | 
 | 258 |     if (deviceIt != mDevices.end()) { | 
 | 259 |         device = deviceIt->second; | 
 | 260 |     } else { | 
 | 261 |         int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked(); | 
 | 262 |         device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(), | 
 | 263 |                                                identifier); | 
 | 264 |     } | 
 | 265 |     device->addEventHubDevice(eventHubId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 266 |     return device; | 
 | 267 | } | 
 | 268 |  | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 269 | void InputReader::processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents, | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 270 |                                                size_t count) { | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 271 |     auto deviceIt = mDevices.find(eventHubId); | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 272 |     if (deviceIt == mDevices.end()) { | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 273 |         ALOGW("Discarding event for unknown eventHubId %d.", eventHubId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 274 |         return; | 
 | 275 |     } | 
 | 276 |  | 
| Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 277 |     std::shared_ptr<InputDevice>& device = deviceIt->second; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 278 |     if (device->isIgnored()) { | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 279 |         // ALOGD("Discarding event for ignored deviceId %d.", deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 280 |         return; | 
 | 281 |     } | 
 | 282 |  | 
 | 283 |     device->process(rawEvents, count); | 
 | 284 | } | 
 | 285 |  | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 286 | InputDevice* InputReader::findInputDevice(int32_t deviceId) { | 
 | 287 |     auto deviceIt = | 
 | 288 |             std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) { | 
 | 289 |                 return devicePair.second->getId() == deviceId; | 
 | 290 |             }); | 
 | 291 |     if (deviceIt != mDevices.end()) { | 
 | 292 |         return deviceIt->second.get(); | 
 | 293 |     } | 
 | 294 |     return nullptr; | 
 | 295 | } | 
 | 296 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 297 | void InputReader::timeoutExpiredLocked(nsecs_t when) { | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 298 |     for (auto& devicePair : mDevices) { | 
| Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 299 |         std::shared_ptr<InputDevice>& device = devicePair.second; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 300 |         if (!device->isIgnored()) { | 
 | 301 |             device->timeoutExpired(when); | 
 | 302 |         } | 
 | 303 |     } | 
 | 304 | } | 
 | 305 |  | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 306 | int32_t InputReader::nextInputDeviceIdLocked() { | 
 | 307 |     return ++mNextInputDeviceId; | 
 | 308 | } | 
 | 309 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 310 | void InputReader::handleConfigurationChangedLocked(nsecs_t when) { | 
 | 311 |     // Reset global meta state because it depends on the list of all configured devices. | 
 | 312 |     updateGlobalMetaStateLocked(); | 
 | 313 |  | 
 | 314 |     // Enqueue configuration changed. | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 315 |     NotifyConfigurationChangedArgs args(mContext.getNextId(), when); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 316 |     mQueuedListener->notifyConfigurationChanged(&args); | 
 | 317 | } | 
 | 318 |  | 
 | 319 | void InputReader::refreshConfigurationLocked(uint32_t changes) { | 
 | 320 |     mPolicy->getReaderConfiguration(&mConfig); | 
 | 321 |     mEventHub->setExcludedDevices(mConfig.excludedDeviceNames); | 
 | 322 |  | 
 | 323 |     if (changes) { | 
| Siarhei Vishniakou | c5ae0dc | 2019-07-10 15:51:18 -0700 | [diff] [blame] | 324 |         ALOGI("Reconfiguring input devices, changes=%s", | 
 | 325 |               InputReaderConfiguration::changesToString(changes).c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 326 |         nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); | 
 | 327 |  | 
| Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 328 |         if (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO) { | 
 | 329 |             updatePointerDisplayLocked(); | 
 | 330 |         } | 
 | 331 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 332 |         if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) { | 
 | 333 |             mEventHub->requestReopenDevices(); | 
 | 334 |         } else { | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 335 |             for (auto& devicePair : mDevices) { | 
| Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 336 |                 std::shared_ptr<InputDevice>& device = devicePair.second; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 337 |                 device->configure(now, &mConfig, changes); | 
 | 338 |             } | 
 | 339 |         } | 
 | 340 |     } | 
 | 341 | } | 
 | 342 |  | 
 | 343 | void InputReader::updateGlobalMetaStateLocked() { | 
 | 344 |     mGlobalMetaState = 0; | 
 | 345 |  | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 346 |     for (auto& devicePair : mDevices) { | 
| Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 347 |         std::shared_ptr<InputDevice>& device = devicePair.second; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 348 |         mGlobalMetaState |= device->getMetaState(); | 
 | 349 |     } | 
 | 350 | } | 
 | 351 |  | 
 | 352 | int32_t InputReader::getGlobalMetaStateLocked() { | 
 | 353 |     return mGlobalMetaState; | 
 | 354 | } | 
 | 355 |  | 
| Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 356 | void InputReader::notifyExternalStylusPresenceChanged() { | 
 | 357 |     refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE); | 
 | 358 | } | 
 | 359 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 360 | void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) { | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 361 |     for (auto& devicePair : mDevices) { | 
| Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 362 |         std::shared_ptr<InputDevice>& device = devicePair.second; | 
| Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 363 |         if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS && !device->isIgnored()) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 364 |             InputDeviceInfo info; | 
 | 365 |             device->getDeviceInfo(&info); | 
 | 366 |             outDevices.push_back(info); | 
| Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 367 |         } | 
 | 368 |     } | 
 | 369 | } | 
 | 370 |  | 
 | 371 | void InputReader::dispatchExternalStylusState(const StylusState& state) { | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 372 |     for (auto& devicePair : mDevices) { | 
| Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 373 |         std::shared_ptr<InputDevice>& device = devicePair.second; | 
| Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 374 |         device->updateExternalStylusState(state); | 
 | 375 |     } | 
 | 376 | } | 
 | 377 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 378 | void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) { | 
 | 379 |     mDisableVirtualKeysTimeout = time; | 
 | 380 | } | 
 | 381 |  | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 382 | bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 383 |     if (now < mDisableVirtualKeysTimeout) { | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 384 |         ALOGI("Dropping virtual key from device because virtual keys are " | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 385 |               "temporarily disabled for the next %0.3fms.  keyCode=%d, scanCode=%d", | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 386 |               (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode, scanCode); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 387 |         return true; | 
 | 388 |     } else { | 
 | 389 |         return false; | 
 | 390 |     } | 
 | 391 | } | 
 | 392 |  | 
| Michael Wright | 7a37667 | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 393 | std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked( | 
 | 394 |         int32_t deviceId) { | 
 | 395 |     std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock(); | 
| Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 396 |     if (controller == nullptr) { | 
 | 397 |         controller = mPolicy->obtainPointerController(deviceId); | 
 | 398 |         mPointerController = controller; | 
 | 399 |         updatePointerDisplayLocked(); | 
 | 400 |     } | 
 | 401 |     return controller; | 
 | 402 | } | 
 | 403 |  | 
 | 404 | void InputReader::updatePointerDisplayLocked() { | 
| Michael Wright | 7a37667 | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 405 |     std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock(); | 
| Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 406 |     if (controller == nullptr) { | 
 | 407 |         return; | 
 | 408 |     } | 
 | 409 |  | 
 | 410 |     std::optional<DisplayViewport> viewport = | 
 | 411 |             mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId); | 
 | 412 |     if (!viewport) { | 
 | 413 |         ALOGW("Can't find the designated viewport with ID %" PRId32 " to update cursor input " | 
 | 414 |               "mapper. Fall back to default display", | 
 | 415 |               mConfig.defaultPointerDisplayId); | 
 | 416 |         viewport = mConfig.getDisplayViewportById(ADISPLAY_ID_DEFAULT); | 
 | 417 |     } | 
 | 418 |     if (!viewport) { | 
 | 419 |         ALOGE("Still can't find a viable viewport to update cursor input mapper. Skip setting it to" | 
 | 420 |               " PointerController."); | 
 | 421 |         return; | 
 | 422 |     } | 
 | 423 |  | 
 | 424 |     controller->setDisplayViewport(*viewport); | 
 | 425 | } | 
 | 426 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 427 | void InputReader::fadePointerLocked() { | 
| Michael Wright | 7a37667 | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 428 |     std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock(); | 
| Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 429 |     if (controller != nullptr) { | 
| Michael Wright | 976db0c | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 430 |         controller->fade(PointerControllerInterface::Transition::GRADUAL); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 431 |     } | 
 | 432 | } | 
 | 433 |  | 
 | 434 | void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) { | 
 | 435 |     if (when < mNextTimeout) { | 
 | 436 |         mNextTimeout = when; | 
 | 437 |         mEventHub->wake(); | 
 | 438 |     } | 
 | 439 | } | 
 | 440 |  | 
 | 441 | int32_t InputReader::bumpGenerationLocked() { | 
 | 442 |     return ++mGeneration; | 
 | 443 | } | 
 | 444 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 445 | void InputReader::getInputDevices(std::vector<InputDeviceInfo>& outInputDevices) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 446 |     AutoMutex _l(mLock); | 
 | 447 |     getInputDevicesLocked(outInputDevices); | 
 | 448 | } | 
 | 449 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 450 | void InputReader::getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 451 |     outInputDevices.clear(); | 
 | 452 |  | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 453 |     for (auto& devicePair : mDevices) { | 
| Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 454 |         std::shared_ptr<InputDevice>& device = devicePair.second; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 455 |         if (!device->isIgnored()) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 456 |             InputDeviceInfo info; | 
 | 457 |             device->getDeviceInfo(&info); | 
 | 458 |             outInputDevices.push_back(info); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 459 |         } | 
 | 460 |     } | 
 | 461 | } | 
 | 462 |  | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 463 | 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] | 464 |     AutoMutex _l(mLock); | 
 | 465 |  | 
 | 466 |     return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState); | 
 | 467 | } | 
 | 468 |  | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 469 | 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] | 470 |     AutoMutex _l(mLock); | 
 | 471 |  | 
 | 472 |     return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState); | 
 | 473 | } | 
 | 474 |  | 
 | 475 | int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) { | 
 | 476 |     AutoMutex _l(mLock); | 
 | 477 |  | 
 | 478 |     return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState); | 
 | 479 | } | 
 | 480 |  | 
 | 481 | 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] | 482 |                                     GetStateFunc getStateFunc) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 483 |     int32_t result = AKEY_STATE_UNKNOWN; | 
 | 484 |     if (deviceId >= 0) { | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 485 |         InputDevice* device = findInputDevice(deviceId); | 
 | 486 |         if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { | 
 | 487 |             result = (device->*getStateFunc)(sourceMask, code); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 488 |         } | 
 | 489 |     } else { | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 490 |         for (auto& devicePair : mDevices) { | 
| Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 491 |             std::shared_ptr<InputDevice>& device = devicePair.second; | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 492 |             if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 493 |                 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that | 
 | 494 |                 // 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] | 495 |                 int32_t currentResult = (device.get()->*getStateFunc)(sourceMask, code); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 496 |                 if (currentResult >= AKEY_STATE_DOWN) { | 
 | 497 |                     return currentResult; | 
 | 498 |                 } else if (currentResult == AKEY_STATE_UP) { | 
 | 499 |                     result = currentResult; | 
 | 500 |                 } | 
 | 501 |             } | 
 | 502 |         } | 
 | 503 |     } | 
 | 504 |     return result; | 
 | 505 | } | 
 | 506 |  | 
| Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 507 | void InputReader::toggleCapsLockState(int32_t deviceId) { | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 508 |     InputDevice* device = findInputDevice(deviceId); | 
 | 509 |     if (!device) { | 
| Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 510 |         ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId); | 
 | 511 |         return; | 
 | 512 |     } | 
 | 513 |  | 
| Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 514 |     if (device->isIgnored()) { | 
 | 515 |         return; | 
 | 516 |     } | 
 | 517 |  | 
 | 518 |     device->updateMetaState(AKEYCODE_CAPS_LOCK); | 
 | 519 | } | 
 | 520 |  | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 521 | bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes, | 
 | 522 |                           const int32_t* keyCodes, uint8_t* outFlags) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 523 |     AutoMutex _l(mLock); | 
 | 524 |  | 
 | 525 |     memset(outFlags, 0, numCodes); | 
 | 526 |     return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags); | 
 | 527 | } | 
 | 528 |  | 
 | 529 | bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 530 |                                               size_t numCodes, const int32_t* keyCodes, | 
 | 531 |                                               uint8_t* outFlags) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 532 |     bool result = false; | 
 | 533 |     if (deviceId >= 0) { | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 534 |         InputDevice* device = findInputDevice(deviceId); | 
 | 535 |         if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { | 
 | 536 |             result = device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 537 |         } | 
 | 538 |     } else { | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 539 |         for (auto& devicePair : mDevices) { | 
| Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 540 |             std::shared_ptr<InputDevice>& device = devicePair.second; | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 541 |             if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { | 
 | 542 |                 result |= device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 543 |             } | 
 | 544 |         } | 
 | 545 |     } | 
 | 546 |     return result; | 
 | 547 | } | 
 | 548 |  | 
 | 549 | void InputReader::requestRefreshConfiguration(uint32_t changes) { | 
 | 550 |     AutoMutex _l(mLock); | 
 | 551 |  | 
 | 552 |     if (changes) { | 
 | 553 |         bool needWake = !mConfigurationChangesToRefresh; | 
 | 554 |         mConfigurationChangesToRefresh |= changes; | 
 | 555 |  | 
 | 556 |         if (needWake) { | 
 | 557 |             mEventHub->wake(); | 
 | 558 |         } | 
 | 559 |     } | 
 | 560 | } | 
 | 561 |  | 
 | 562 | 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] | 563 |                           ssize_t repeat, int32_t token) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 564 |     AutoMutex _l(mLock); | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 565 |     InputDevice* device = findInputDevice(deviceId); | 
 | 566 |     if (device) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 567 |         device->vibrate(pattern, patternSize, repeat, token); | 
 | 568 |     } | 
 | 569 | } | 
 | 570 |  | 
 | 571 | void InputReader::cancelVibrate(int32_t deviceId, int32_t token) { | 
 | 572 |     AutoMutex _l(mLock); | 
 | 573 |  | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 574 |     InputDevice* device = findInputDevice(deviceId); | 
 | 575 |     if (device) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 576 |         device->cancelVibrate(token); | 
 | 577 |     } | 
 | 578 | } | 
 | 579 |  | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 580 | bool InputReader::isInputDeviceEnabled(int32_t deviceId) { | 
 | 581 |     AutoMutex _l(mLock); | 
 | 582 |  | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 583 |     InputDevice* device = findInputDevice(deviceId); | 
 | 584 |     if (device) { | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 585 |         return device->isEnabled(); | 
 | 586 |     } | 
 | 587 |     ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId); | 
 | 588 |     return false; | 
 | 589 | } | 
 | 590 |  | 
| Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 591 | bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) { | 
 | 592 |     AutoMutex _l(mLock); | 
 | 593 |  | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 594 |     InputDevice* device = findInputDevice(deviceId); | 
 | 595 |     if (!device) { | 
| Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 596 |         ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId); | 
 | 597 |         return false; | 
 | 598 |     } | 
 | 599 |  | 
| Arthur Hung | 2c9a334 | 2019-07-23 14:18:59 +0800 | [diff] [blame] | 600 |     if (!device->isEnabled()) { | 
 | 601 |         ALOGW("Ignoring disabled device %s", device->getName().c_str()); | 
 | 602 |         return false; | 
 | 603 |     } | 
 | 604 |  | 
 | 605 |     std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId(); | 
| Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 606 |     // No associated display. By default, can dispatch to all displays. | 
 | 607 |     if (!associatedDisplayId) { | 
 | 608 |         return true; | 
 | 609 |     } | 
 | 610 |  | 
 | 611 |     if (*associatedDisplayId == ADISPLAY_ID_NONE) { | 
| Arthur Hung | 2c9a334 | 2019-07-23 14:18:59 +0800 | [diff] [blame] | 612 |         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] | 613 |         return true; | 
 | 614 |     } | 
 | 615 |  | 
 | 616 |     return *associatedDisplayId == displayId; | 
 | 617 | } | 
 | 618 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 619 | void InputReader::dump(std::string& dump) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 620 |     AutoMutex _l(mLock); | 
 | 621 |  | 
 | 622 |     mEventHub->dump(dump); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 623 |     dump += "\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 624 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 625 |     dump += "Input Reader State:\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 626 |  | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 627 |     for (const auto& devicePair : mDevices) { | 
| Nathaniel R. Lewis | 0cab12d | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 628 |         const std::shared_ptr<InputDevice>& device = devicePair.second; | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 629 |         device->dump(dump); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 630 |     } | 
 | 631 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 632 |     dump += INDENT "Configuration:\n"; | 
 | 633 |     dump += INDENT2 "ExcludedDeviceNames: ["; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 634 |     for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) { | 
 | 635 |         if (i != 0) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 636 |             dump += ", "; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 637 |         } | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 638 |         dump += mConfig.excludedDeviceNames[i]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 639 |     } | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 640 |     dump += "]\n"; | 
 | 641 |     dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 642 |                          mConfig.virtualKeyQuietTime * 0.000001f); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 643 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 644 |     dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: " | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 645 |                                  "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, " | 
 | 646 |                                  "acceleration=%0.3f\n", | 
 | 647 |                          mConfig.pointerVelocityControlParameters.scale, | 
 | 648 |                          mConfig.pointerVelocityControlParameters.lowThreshold, | 
 | 649 |                          mConfig.pointerVelocityControlParameters.highThreshold, | 
 | 650 |                          mConfig.pointerVelocityControlParameters.acceleration); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 651 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 652 |     dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: " | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 653 |                                  "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, " | 
 | 654 |                                  "acceleration=%0.3f\n", | 
 | 655 |                          mConfig.wheelVelocityControlParameters.scale, | 
 | 656 |                          mConfig.wheelVelocityControlParameters.lowThreshold, | 
 | 657 |                          mConfig.wheelVelocityControlParameters.highThreshold, | 
 | 658 |                          mConfig.wheelVelocityControlParameters.acceleration); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 659 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 660 |     dump += StringPrintf(INDENT2 "PointerGesture:\n"); | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 661 |     dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled)); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 662 |     dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 663 |                          mConfig.pointerGestureQuietInterval * 0.000001f); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 664 |     dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 665 |                          mConfig.pointerGestureDragMinSwitchSpeed); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 666 |     dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 667 |                          mConfig.pointerGestureTapInterval * 0.000001f); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 668 |     dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 669 |                          mConfig.pointerGestureTapDragInterval * 0.000001f); | 
 | 670 |     dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 671 |     dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 672 |                          mConfig.pointerGestureMultitouchSettleInterval * 0.000001f); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 673 |     dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 674 |                          mConfig.pointerGestureMultitouchMinDistance); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 675 |     dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 676 |                          mConfig.pointerGestureSwipeTransitionAngleCosine); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 677 |     dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 678 |                          mConfig.pointerGestureSwipeMaxWidthRatio); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 679 |     dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n", | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 680 |                          mConfig.pointerGestureMovementSpeedRatio); | 
 | 681 |     dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio); | 
| Santos Cordon | fa5cf46 | 2017-04-05 10:37:00 -0700 | [diff] [blame] | 682 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 683 |     dump += INDENT3 "Viewports:\n"; | 
| Santos Cordon | fa5cf46 | 2017-04-05 10:37:00 -0700 | [diff] [blame] | 684 |     mConfig.dump(dump); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 685 | } | 
 | 686 |  | 
 | 687 | void InputReader::monitor() { | 
 | 688 |     // Acquire and release the lock to ensure that the reader has not deadlocked. | 
 | 689 |     mLock.lock(); | 
 | 690 |     mEventHub->wake(); | 
 | 691 |     mReaderIsAliveCondition.wait(mLock); | 
 | 692 |     mLock.unlock(); | 
 | 693 |  | 
 | 694 |     // Check the EventHub | 
 | 695 |     mEventHub->monitor(); | 
 | 696 | } | 
 | 697 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 698 | // --- InputReader::ContextImpl --- | 
 | 699 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 700 | InputReader::ContextImpl::ContextImpl(InputReader* reader) | 
 | 701 |       : mReader(reader), mIdGenerator(IdGenerator::Source::INPUT_READER) {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 702 |  | 
 | 703 | void InputReader::ContextImpl::updateGlobalMetaState() { | 
 | 704 |     // lock is already held by the input loop | 
 | 705 |     mReader->updateGlobalMetaStateLocked(); | 
 | 706 | } | 
 | 707 |  | 
 | 708 | int32_t InputReader::ContextImpl::getGlobalMetaState() { | 
 | 709 |     // lock is already held by the input loop | 
 | 710 |     return mReader->getGlobalMetaStateLocked(); | 
 | 711 | } | 
 | 712 |  | 
 | 713 | void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) { | 
 | 714 |     // lock is already held by the input loop | 
 | 715 |     mReader->disableVirtualKeysUntilLocked(time); | 
 | 716 | } | 
 | 717 |  | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 718 | bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, int32_t keyCode, | 
 | 719 |                                                     int32_t scanCode) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 720 |     // lock is already held by the input loop | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 721 |     return mReader->shouldDropVirtualKeyLocked(now, keyCode, scanCode); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 722 | } | 
 | 723 |  | 
 | 724 | void InputReader::ContextImpl::fadePointer() { | 
 | 725 |     // lock is already held by the input loop | 
 | 726 |     mReader->fadePointerLocked(); | 
 | 727 | } | 
 | 728 |  | 
| Michael Wright | 7a37667 | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 729 | std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController( | 
 | 730 |         int32_t deviceId) { | 
| Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 731 |     // lock is already held by the input loop | 
 | 732 |     return mReader->getPointerControllerLocked(deviceId); | 
 | 733 | } | 
 | 734 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 735 | void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) { | 
 | 736 |     // lock is already held by the input loop | 
 | 737 |     mReader->requestTimeoutAtTimeLocked(when); | 
 | 738 | } | 
 | 739 |  | 
 | 740 | int32_t InputReader::ContextImpl::bumpGeneration() { | 
 | 741 |     // lock is already held by the input loop | 
 | 742 |     return mReader->bumpGenerationLocked(); | 
 | 743 | } | 
 | 744 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 745 | void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) { | 
| Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 746 |     // lock is already held by whatever called refreshConfigurationLocked | 
 | 747 |     mReader->getExternalStylusDevicesLocked(outDevices); | 
 | 748 | } | 
 | 749 |  | 
 | 750 | void InputReader::ContextImpl::dispatchExternalStylusState(const StylusState& state) { | 
 | 751 |     mReader->dispatchExternalStylusState(state); | 
 | 752 | } | 
 | 753 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 754 | InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() { | 
 | 755 |     return mReader->mPolicy.get(); | 
 | 756 | } | 
 | 757 |  | 
 | 758 | InputListenerInterface* InputReader::ContextImpl::getListener() { | 
 | 759 |     return mReader->mQueuedListener.get(); | 
 | 760 | } | 
 | 761 |  | 
 | 762 | EventHubInterface* InputReader::ContextImpl::getEventHub() { | 
 | 763 |     return mReader->mEventHub.get(); | 
 | 764 | } | 
 | 765 |  | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 766 | int32_t InputReader::ContextImpl::getNextId() { | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 767 |     return mIdGenerator.nextId(); | 
| Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 768 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 769 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 770 | } // namespace android |