blob: 05f0db13299921952b4b6a2b8033cd268952703e [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
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 Pradhanbaa5c822019-08-30 15:27:05 -070017#include "Macros.h"
Michael Wright842500e2015-03-13 17:32:02 -070018
Michael Wrightd02c5b62014-02-10 15:10:22 -080019#include "InputReader.h"
20
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070021#include "CursorInputMapper.h"
22#include "ExternalStylusInputMapper.h"
23#include "InputReaderContext.h"
24#include "JoystickInputMapper.h"
25#include "KeyboardInputMapper.h"
26#include "MultiTouchInputMapper.h"
27#include "RotaryEncoderInputMapper.h"
28#include "SingleTouchInputMapper.h"
29#include "SwitchInputMapper.h"
30#include "VibratorInputMapper.h"
31
Mark Salyzyna5e161b2016-09-29 08:08:05 -070032#include <errno.h>
Michael Wright842500e2015-03-13 17:32:02 -070033#include <inttypes.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070034#include <limits.h>
35#include <math.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036#include <stddef.h>
37#include <stdlib.h>
38#include <unistd.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070039
Mark Salyzyn7823e122016-09-29 08:08:05 -070040#include <log/log.h>
Prabir Pradhan28efc192019-11-05 01:10:04 +000041#include <utils/Errors.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070042
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080043#include <android-base/stringprintf.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070044#include <input/Keyboard.h>
45#include <input/VirtualKeyMap.h>
Prabir Pradhan28efc192019-11-05 01:10:04 +000046#include <utils/Thread.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080047
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080048using android::base::StringPrintf;
49
Michael Wrightd02c5b62014-02-10 15:10:22 -080050namespace android {
51
Prabir Pradhan28efc192019-11-05 01:10:04 +000052// --- InputReader::InputReaderThread ---
53
54/* Thread that reads raw events from the event hub and processes them, endlessly. */
55class InputReader::InputReaderThread : public Thread {
56public:
57 explicit InputReaderThread(InputReader* reader)
58 : Thread(/* canCallJava */ true), mReader(reader) {}
59
60 ~InputReaderThread() {}
61
62private:
63 InputReader* mReader;
64
65 bool threadLoop() override {
66 mReader->loopOnce();
67 return true;
68 }
69};
70
71// --- InputReader ---
72
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070073InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
74 const sp<InputReaderPolicyInterface>& policy,
75 const sp<InputListenerInterface>& listener)
76 : mContext(this),
77 mEventHub(eventHub),
78 mPolicy(policy),
79 mNextSequenceNum(1),
80 mGlobalMetaState(0),
81 mGeneration(1),
82 mDisableVirtualKeysTimeout(LLONG_MIN),
83 mNextTimeout(LLONG_MAX),
Michael Wrightd02c5b62014-02-10 15:10:22 -080084 mConfigurationChangesToRefresh(0) {
85 mQueuedListener = new QueuedInputListener(listener);
Prabir Pradhan28efc192019-11-05 01:10:04 +000086 mThread = new InputReaderThread(this);
Michael Wrightd02c5b62014-02-10 15:10:22 -080087
88 { // acquire lock
89 AutoMutex _l(mLock);
90
91 refreshConfigurationLocked(0);
92 updateGlobalMetaStateLocked();
93 } // release lock
94}
95
96InputReader::~InputReader() {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +000097 for (auto& devicePair : mDevices) {
98 delete devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -080099 }
100}
101
Prabir Pradhan28efc192019-11-05 01:10:04 +0000102status_t InputReader::start() {
103 if (mThread->isRunning()) {
104 return ALREADY_EXISTS;
105 }
106 return mThread->run("InputReader", PRIORITY_URGENT_DISPLAY);
107}
108
109status_t InputReader::stop() {
110 if (!mThread->isRunning()) {
111 return OK;
112 }
113 if (gettid() == mThread->getTid()) {
114 ALOGE("InputReader can only be stopped from outside of the InputReaderThread!");
115 return INVALID_OPERATION;
116 }
117 // Directly calling requestExitAndWait() causes the thread to not exit
118 // if mEventHub is waiting for a long timeout.
119 mThread->requestExit();
120 mEventHub->wake();
121 return mThread->requestExitAndWait();
122}
123
Michael Wrightd02c5b62014-02-10 15:10:22 -0800124void InputReader::loopOnce() {
125 int32_t oldGeneration;
126 int32_t timeoutMillis;
127 bool inputDevicesChanged = false;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800128 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800129 { // acquire lock
130 AutoMutex _l(mLock);
131
132 oldGeneration = mGeneration;
133 timeoutMillis = -1;
134
135 uint32_t changes = mConfigurationChangesToRefresh;
136 if (changes) {
137 mConfigurationChangesToRefresh = 0;
138 timeoutMillis = 0;
139 refreshConfigurationLocked(changes);
140 } else if (mNextTimeout != LLONG_MAX) {
141 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
142 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
143 }
144 } // release lock
145
146 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
147
148 { // acquire lock
149 AutoMutex _l(mLock);
150 mReaderIsAliveCondition.broadcast();
151
152 if (count) {
153 processEventsLocked(mEventBuffer, count);
154 }
155
156 if (mNextTimeout != LLONG_MAX) {
157 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
158 if (now >= mNextTimeout) {
159#if DEBUG_RAW_EVENTS
160 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
161#endif
162 mNextTimeout = LLONG_MAX;
163 timeoutExpiredLocked(now);
164 }
165 }
166
167 if (oldGeneration != mGeneration) {
168 inputDevicesChanged = true;
169 getInputDevicesLocked(inputDevices);
170 }
171 } // release lock
172
173 // Send out a message that the describes the changed input devices.
174 if (inputDevicesChanged) {
175 mPolicy->notifyInputDevicesChanged(inputDevices);
176 }
177
178 // Flush queued events out to the listener.
179 // This must happen outside of the lock because the listener could potentially call
180 // back into the InputReader's methods, such as getScanCodeState, or become blocked
181 // on another thread similarly waiting to acquire the InputReader lock thereby
182 // resulting in a deadlock. This situation is actually quite plausible because the
183 // listener is actually the input dispatcher, which calls into the window manager,
184 // which occasionally calls into the input reader.
185 mQueuedListener->flush();
186}
187
188void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
189 for (const RawEvent* rawEvent = rawEvents; count;) {
190 int32_t type = rawEvent->type;
191 size_t batchSize = 1;
192 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
193 int32_t deviceId = rawEvent->deviceId;
194 while (batchSize < count) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700195 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
196 rawEvent[batchSize].deviceId != deviceId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800197 break;
198 }
199 batchSize += 1;
200 }
201#if DEBUG_RAW_EVENTS
Siarhei Vishniakou5d83f602017-09-12 12:40:29 -0700202 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203#endif
204 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
205 } else {
206 switch (rawEvent->type) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700207 case EventHubInterface::DEVICE_ADDED:
208 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
209 break;
210 case EventHubInterface::DEVICE_REMOVED:
211 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
212 break;
213 case EventHubInterface::FINISHED_DEVICE_SCAN:
214 handleConfigurationChangedLocked(rawEvent->when);
215 break;
216 default:
217 ALOG_ASSERT(false); // can't happen
218 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219 }
220 }
221 count -= batchSize;
222 rawEvent += batchSize;
223 }
224}
225
226void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000227 if (mDevices.find(deviceId) != mDevices.end()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228 ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
229 return;
230 }
231
232 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId);
233 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
234 int32_t controllerNumber = mEventHub->getDeviceControllerNumber(deviceId);
235
236 InputDevice* device = createDeviceLocked(deviceId, controllerNumber, identifier, classes);
237 device->configure(when, &mConfig, 0);
238 device->reset(when);
239
240 if (device->isIgnored()) {
241 ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700242 identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243 } else {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700244 ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, identifier.name.c_str(),
245 device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246 }
247
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000248 mDevices.insert({deviceId, device});
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700250
251 if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
252 notifyExternalStylusPresenceChanged();
253 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254}
255
256void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000257 auto deviceIt = mDevices.find(deviceId);
258 if (deviceIt == mDevices.end()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
260 return;
261 }
262
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000263 InputDevice* device = deviceIt->second;
264 mDevices.erase(deviceIt);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265 bumpGenerationLocked();
266
267 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700268 ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)", device->getId(),
269 device->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 } else {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700271 ALOGI("Device removed: id=%d, name='%s', sources=0x%08x", device->getId(),
272 device->getName().c_str(), device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273 }
274
Michael Wright842500e2015-03-13 17:32:02 -0700275 if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
276 notifyExternalStylusPresenceChanged();
277 }
278
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 device->reset(when);
280 delete device;
281}
282
283InputDevice* InputReader::createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700284 const InputDeviceIdentifier& identifier,
285 uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800286 InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700287 controllerNumber, identifier, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800288
289 // External devices.
290 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
291 device->setExternal(true);
292 }
293
Tim Kilbourn063ff532015-04-08 10:26:18 -0700294 // Devices with mics.
295 if (classes & INPUT_DEVICE_CLASS_MIC) {
296 device->setMic(true);
297 }
298
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299 // Switch-like devices.
300 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
301 device->addMapper(new SwitchInputMapper(device));
302 }
303
Prashant Malani1941ff52015-08-11 18:29:28 -0700304 // Scroll wheel-like devices.
305 if (classes & INPUT_DEVICE_CLASS_ROTARY_ENCODER) {
306 device->addMapper(new RotaryEncoderInputMapper(device));
307 }
308
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 // Vibrator-like devices.
310 if (classes & INPUT_DEVICE_CLASS_VIBRATOR) {
311 device->addMapper(new VibratorInputMapper(device));
312 }
313
314 // Keyboard-like devices.
315 uint32_t keyboardSource = 0;
316 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
317 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
318 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
319 }
320 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
321 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
322 }
323 if (classes & INPUT_DEVICE_CLASS_DPAD) {
324 keyboardSource |= AINPUT_SOURCE_DPAD;
325 }
326 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
327 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
328 }
329
330 if (keyboardSource != 0) {
331 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
332 }
333
334 // Cursor-like devices.
335 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
336 device->addMapper(new CursorInputMapper(device));
337 }
338
339 // Touchscreens and touchpad devices.
340 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
341 device->addMapper(new MultiTouchInputMapper(device));
342 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
343 device->addMapper(new SingleTouchInputMapper(device));
344 }
345
346 // Joystick-like devices.
347 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
348 device->addMapper(new JoystickInputMapper(device));
349 }
350
Michael Wright842500e2015-03-13 17:32:02 -0700351 // External stylus-like devices.
352 if (classes & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
353 device->addMapper(new ExternalStylusInputMapper(device));
354 }
355
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356 return device;
357}
358
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700359void InputReader::processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents,
360 size_t count) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000361 auto deviceIt = mDevices.find(deviceId);
362 if (deviceIt == mDevices.end()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363 ALOGW("Discarding event for unknown deviceId %d.", deviceId);
364 return;
365 }
366
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000367 InputDevice* device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800368 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700369 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800370 return;
371 }
372
373 device->process(rawEvents, count);
374}
375
376void InputReader::timeoutExpiredLocked(nsecs_t when) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000377 for (auto& devicePair : mDevices) {
378 InputDevice* device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800379 if (!device->isIgnored()) {
380 device->timeoutExpired(when);
381 }
382 }
383}
384
385void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
386 // Reset global meta state because it depends on the list of all configured devices.
387 updateGlobalMetaStateLocked();
388
389 // Enqueue configuration changed.
Prabir Pradhan42611e02018-11-27 14:04:02 -0800390 NotifyConfigurationChangedArgs args(mContext.getNextSequenceNum(), when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391 mQueuedListener->notifyConfigurationChanged(&args);
392}
393
394void InputReader::refreshConfigurationLocked(uint32_t changes) {
395 mPolicy->getReaderConfiguration(&mConfig);
396 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
397
398 if (changes) {
Siarhei Vishniakouc5ae0dc2019-07-10 15:51:18 -0700399 ALOGI("Reconfiguring input devices, changes=%s",
400 InputReaderConfiguration::changesToString(changes).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
402
403 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
404 mEventHub->requestReopenDevices();
405 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000406 for (auto& devicePair : mDevices) {
407 InputDevice* device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 device->configure(now, &mConfig, changes);
409 }
410 }
411 }
412}
413
414void InputReader::updateGlobalMetaStateLocked() {
415 mGlobalMetaState = 0;
416
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000417 for (auto& devicePair : mDevices) {
418 InputDevice* device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800419 mGlobalMetaState |= device->getMetaState();
420 }
421}
422
423int32_t InputReader::getGlobalMetaStateLocked() {
424 return mGlobalMetaState;
425}
426
Michael Wright842500e2015-03-13 17:32:02 -0700427void InputReader::notifyExternalStylusPresenceChanged() {
428 refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
429}
430
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800431void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000432 for (auto& devicePair : mDevices) {
433 InputDevice* device = devicePair.second;
Michael Wright842500e2015-03-13 17:32:02 -0700434 if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS && !device->isIgnored()) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800435 InputDeviceInfo info;
436 device->getDeviceInfo(&info);
437 outDevices.push_back(info);
Michael Wright842500e2015-03-13 17:32:02 -0700438 }
439 }
440}
441
442void InputReader::dispatchExternalStylusState(const StylusState& state) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000443 for (auto& devicePair : mDevices) {
444 InputDevice* device = devicePair.second;
Michael Wright842500e2015-03-13 17:32:02 -0700445 device->updateExternalStylusState(state);
446 }
447}
448
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
450 mDisableVirtualKeysTimeout = time;
451}
452
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700453bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, InputDevice* device, int32_t keyCode,
454 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455 if (now < mDisableVirtualKeysTimeout) {
456 ALOGI("Dropping virtual key from device %s because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700457 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
458 device->getName().c_str(), (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode,
459 scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460 return true;
461 } else {
462 return false;
463 }
464}
465
466void InputReader::fadePointerLocked() {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000467 for (auto& devicePair : mDevices) {
468 InputDevice* device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800469 device->fadePointer();
470 }
471}
472
473void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
474 if (when < mNextTimeout) {
475 mNextTimeout = when;
476 mEventHub->wake();
477 }
478}
479
480int32_t InputReader::bumpGenerationLocked() {
481 return ++mGeneration;
482}
483
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800484void InputReader::getInputDevices(std::vector<InputDeviceInfo>& outInputDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485 AutoMutex _l(mLock);
486 getInputDevicesLocked(outInputDevices);
487}
488
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800489void InputReader::getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 outInputDevices.clear();
491
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000492 for (auto& devicePair : mDevices) {
493 InputDevice* device = devicePair.second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 if (!device->isIgnored()) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800495 InputDeviceInfo info;
496 device->getDeviceInfo(&info);
497 outInputDevices.push_back(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498 }
499 }
500}
501
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700502int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 AutoMutex _l(mLock);
504
505 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
506}
507
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700508int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800509 AutoMutex _l(mLock);
510
511 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
512}
513
514int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
515 AutoMutex _l(mLock);
516
517 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
518}
519
520int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700521 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522 int32_t result = AKEY_STATE_UNKNOWN;
523 if (deviceId >= 0) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000524 auto deviceIt = mDevices.find(deviceId);
525 if (deviceIt != mDevices.end()) {
526 InputDevice* device = deviceIt->second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700527 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528 result = (device->*getStateFunc)(sourceMask, code);
529 }
530 }
531 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000532 for (auto& devicePair : mDevices) {
533 InputDevice* device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700534 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
536 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
537 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
538 if (currentResult >= AKEY_STATE_DOWN) {
539 return currentResult;
540 } else if (currentResult == AKEY_STATE_UP) {
541 result = currentResult;
542 }
543 }
544 }
545 }
546 return result;
547}
548
Andrii Kulian763a3a42016-03-08 10:46:16 -0800549void InputReader::toggleCapsLockState(int32_t deviceId) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000550 auto deviceIt = mDevices.find(deviceId);
551 if (deviceIt == mDevices.end()) {
Andrii Kulian763a3a42016-03-08 10:46:16 -0800552 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
553 return;
554 }
555
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000556 InputDevice* device = deviceIt->second;
Andrii Kulian763a3a42016-03-08 10:46:16 -0800557 if (device->isIgnored()) {
558 return;
559 }
560
561 device->updateMetaState(AKEYCODE_CAPS_LOCK);
562}
563
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700564bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
565 const int32_t* keyCodes, uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 AutoMutex _l(mLock);
567
568 memset(outFlags, 0, numCodes);
569 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
570}
571
572bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700573 size_t numCodes, const int32_t* keyCodes,
574 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800575 bool result = false;
576 if (deviceId >= 0) {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000577 auto deviceIt = mDevices.find(deviceId);
578 if (deviceIt != mDevices.end()) {
579 InputDevice* device = deviceIt->second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700580 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
581 result = device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582 }
583 }
584 } else {
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000585 for (auto& devicePair : mDevices) {
586 InputDevice* device = devicePair.second;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700587 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
588 result |= device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 }
590 }
591 }
592 return result;
593}
594
595void InputReader::requestRefreshConfiguration(uint32_t changes) {
596 AutoMutex _l(mLock);
597
598 if (changes) {
599 bool needWake = !mConfigurationChangesToRefresh;
600 mConfigurationChangesToRefresh |= changes;
601
602 if (needWake) {
603 mEventHub->wake();
604 }
605 }
606}
607
608void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700609 ssize_t repeat, int32_t token) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 AutoMutex _l(mLock);
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000611 auto deviceIt = mDevices.find(deviceId);
612 if (deviceIt != mDevices.end()) {
613 InputDevice* device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614 device->vibrate(pattern, patternSize, repeat, token);
615 }
616}
617
618void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
619 AutoMutex _l(mLock);
620
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000621 auto deviceIt = mDevices.find(deviceId);
622 if (deviceIt != mDevices.end()) {
623 InputDevice* device = deviceIt->second;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800624 device->cancelVibrate(token);
625 }
626}
627
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700628bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
629 AutoMutex _l(mLock);
630
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000631 auto deviceIt = mDevices.find(deviceId);
632 if (deviceIt != mDevices.end()) {
633 InputDevice* device = deviceIt->second;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700634 return device->isEnabled();
635 }
636 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
637 return false;
638}
639
Arthur Hungc23540e2018-11-29 20:42:11 +0800640bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
641 AutoMutex _l(mLock);
642
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000643 auto deviceIt = mDevices.find(deviceId);
644 if (deviceIt == mDevices.end()) {
Arthur Hungc23540e2018-11-29 20:42:11 +0800645 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
646 return false;
647 }
648
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000649 InputDevice* device = deviceIt->second;
Arthur Hung2c9a3342019-07-23 14:18:59 +0800650 if (!device->isEnabled()) {
651 ALOGW("Ignoring disabled device %s", device->getName().c_str());
652 return false;
653 }
654
655 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800656 // No associated display. By default, can dispatch to all displays.
657 if (!associatedDisplayId) {
658 return true;
659 }
660
661 if (*associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hung2c9a3342019-07-23 14:18:59 +0800662 ALOGW("Device %s is associated with display ADISPLAY_ID_NONE.", device->getName().c_str());
Arthur Hungc23540e2018-11-29 20:42:11 +0800663 return true;
664 }
665
666 return *associatedDisplayId == displayId;
667}
668
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800669void InputReader::dump(std::string& dump) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 AutoMutex _l(mLock);
671
672 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800673 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800675 dump += "Input Reader State:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000677 for (const auto& devicePair : mDevices) {
678 InputDevice* const device = devicePair.second;
679 device->dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 }
681
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800682 dump += INDENT "Configuration:\n";
683 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800684 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
685 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800686 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100688 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800690 dump += "]\n";
691 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700692 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800693
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800694 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700695 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
696 "acceleration=%0.3f\n",
697 mConfig.pointerVelocityControlParameters.scale,
698 mConfig.pointerVelocityControlParameters.lowThreshold,
699 mConfig.pointerVelocityControlParameters.highThreshold,
700 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800701
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800702 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700703 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
704 "acceleration=%0.3f\n",
705 mConfig.wheelVelocityControlParameters.scale,
706 mConfig.wheelVelocityControlParameters.lowThreshold,
707 mConfig.wheelVelocityControlParameters.highThreshold,
708 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800710 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700711 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800712 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700713 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800714 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700715 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800716 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700717 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800718 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700719 mConfig.pointerGestureTapDragInterval * 0.000001f);
720 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800721 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700722 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800723 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700724 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800725 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700726 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800727 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700728 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800729 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700730 mConfig.pointerGestureMovementSpeedRatio);
731 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700732
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800733 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700734 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735}
736
737void InputReader::monitor() {
738 // Acquire and release the lock to ensure that the reader has not deadlocked.
739 mLock.lock();
740 mEventHub->wake();
741 mReaderIsAliveCondition.wait(mLock);
742 mLock.unlock();
743
744 // Check the EventHub
745 mEventHub->monitor();
746}
747
Michael Wrightd02c5b62014-02-10 15:10:22 -0800748// --- InputReader::ContextImpl ---
749
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700750InputReader::ContextImpl::ContextImpl(InputReader* reader) : mReader(reader) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751
752void InputReader::ContextImpl::updateGlobalMetaState() {
753 // lock is already held by the input loop
754 mReader->updateGlobalMetaStateLocked();
755}
756
757int32_t InputReader::ContextImpl::getGlobalMetaState() {
758 // lock is already held by the input loop
759 return mReader->getGlobalMetaStateLocked();
760}
761
762void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
763 // lock is already held by the input loop
764 mReader->disableVirtualKeysUntilLocked(time);
765}
766
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700767bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, InputDevice* device,
768 int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800769 // lock is already held by the input loop
770 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
771}
772
773void InputReader::ContextImpl::fadePointer() {
774 // lock is already held by the input loop
775 mReader->fadePointerLocked();
776}
777
778void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
779 // lock is already held by the input loop
780 mReader->requestTimeoutAtTimeLocked(when);
781}
782
783int32_t InputReader::ContextImpl::bumpGeneration() {
784 // lock is already held by the input loop
785 return mReader->bumpGenerationLocked();
786}
787
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800788void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700789 // lock is already held by whatever called refreshConfigurationLocked
790 mReader->getExternalStylusDevicesLocked(outDevices);
791}
792
793void InputReader::ContextImpl::dispatchExternalStylusState(const StylusState& state) {
794 mReader->dispatchExternalStylusState(state);
795}
796
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
798 return mReader->mPolicy.get();
799}
800
801InputListenerInterface* InputReader::ContextImpl::getListener() {
802 return mReader->mQueuedListener.get();
803}
804
805EventHubInterface* InputReader::ContextImpl::getEventHub() {
806 return mReader->mEventHub.get();
807}
808
Prabir Pradhan42611e02018-11-27 14:04:02 -0800809uint32_t InputReader::ContextImpl::getNextSequenceNum() {
810 return (mReader->mNextSequenceNum)++;
811}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813} // namespace android