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