blob: 5d52bbc91f38ed4465759cfa1c740b948c0c33e8 [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 Pradhanba266f22019-09-03 17:11:27 -070041#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 Pradhanba266f22019-09-03 17:11:27 -070046#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 Pradhanba266f22019-09-03 17:11:27 -070052// --- 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 Pradhanba266f22019-09-03 17:11:27 -070086 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() {
97 for (size_t i = 0; i < mDevices.size(); i++) {
98 delete mDevices.valueAt(i);
99 }
100}
101
Prabir Pradhanba266f22019-09-03 17:11:27 -0700102status_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) {
227 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
228 if (deviceIndex >= 0) {
229 ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
230 return;
231 }
232
233 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId);
234 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
235 int32_t controllerNumber = mEventHub->getDeviceControllerNumber(deviceId);
236
237 InputDevice* device = createDeviceLocked(deviceId, controllerNumber, identifier, classes);
238 device->configure(when, &mConfig, 0);
239 device->reset(when);
240
241 if (device->isIgnored()) {
242 ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700243 identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244 } else {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700245 ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, identifier.name.c_str(),
246 device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247 }
248
249 mDevices.add(deviceId, device);
250 bumpGenerationLocked();
Michael Wright842500e2015-03-13 17:32:02 -0700251
252 if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
253 notifyExternalStylusPresenceChanged();
254 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800255}
256
257void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700258 InputDevice* device = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
260 if (deviceIndex < 0) {
261 ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
262 return;
263 }
264
265 device = mDevices.valueAt(deviceIndex);
266 mDevices.removeItemsAt(deviceIndex, 1);
267 bumpGenerationLocked();
268
269 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700270 ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)", device->getId(),
271 device->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272 } else {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700273 ALOGI("Device removed: id=%d, name='%s', sources=0x%08x", device->getId(),
274 device->getName().c_str(), device->getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275 }
276
Michael Wright842500e2015-03-13 17:32:02 -0700277 if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
278 notifyExternalStylusPresenceChanged();
279 }
280
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 device->reset(when);
282 delete device;
283}
284
285InputDevice* InputReader::createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700286 const InputDeviceIdentifier& identifier,
287 uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800288 InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700289 controllerNumber, identifier, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800290
291 // External devices.
292 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
293 device->setExternal(true);
294 }
295
Tim Kilbourn063ff532015-04-08 10:26:18 -0700296 // Devices with mics.
297 if (classes & INPUT_DEVICE_CLASS_MIC) {
298 device->setMic(true);
299 }
300
Michael Wrightd02c5b62014-02-10 15:10:22 -0800301 // Switch-like devices.
302 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
303 device->addMapper(new SwitchInputMapper(device));
304 }
305
Prashant Malani1941ff52015-08-11 18:29:28 -0700306 // Scroll wheel-like devices.
307 if (classes & INPUT_DEVICE_CLASS_ROTARY_ENCODER) {
308 device->addMapper(new RotaryEncoderInputMapper(device));
309 }
310
Michael Wrightd02c5b62014-02-10 15:10:22 -0800311 // Vibrator-like devices.
312 if (classes & INPUT_DEVICE_CLASS_VIBRATOR) {
313 device->addMapper(new VibratorInputMapper(device));
314 }
315
316 // Keyboard-like devices.
317 uint32_t keyboardSource = 0;
318 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
319 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
320 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
321 }
322 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
323 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
324 }
325 if (classes & INPUT_DEVICE_CLASS_DPAD) {
326 keyboardSource |= AINPUT_SOURCE_DPAD;
327 }
328 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
329 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
330 }
331
332 if (keyboardSource != 0) {
333 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
334 }
335
336 // Cursor-like devices.
337 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
338 device->addMapper(new CursorInputMapper(device));
339 }
340
341 // Touchscreens and touchpad devices.
342 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
343 device->addMapper(new MultiTouchInputMapper(device));
344 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
345 device->addMapper(new SingleTouchInputMapper(device));
346 }
347
348 // Joystick-like devices.
349 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
350 device->addMapper(new JoystickInputMapper(device));
351 }
352
Michael Wright842500e2015-03-13 17:32:02 -0700353 // External stylus-like devices.
354 if (classes & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
355 device->addMapper(new ExternalStylusInputMapper(device));
356 }
357
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358 return device;
359}
360
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700361void InputReader::processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents,
362 size_t count) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
364 if (deviceIndex < 0) {
365 ALOGW("Discarding event for unknown deviceId %d.", deviceId);
366 return;
367 }
368
369 InputDevice* device = mDevices.valueAt(deviceIndex);
370 if (device->isIgnored()) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700371 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372 return;
373 }
374
375 device->process(rawEvents, count);
376}
377
378void InputReader::timeoutExpiredLocked(nsecs_t when) {
379 for (size_t i = 0; i < mDevices.size(); i++) {
380 InputDevice* device = mDevices.valueAt(i);
381 if (!device->isIgnored()) {
382 device->timeoutExpired(when);
383 }
384 }
385}
386
387void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
388 // Reset global meta state because it depends on the list of all configured devices.
389 updateGlobalMetaStateLocked();
390
391 // Enqueue configuration changed.
Prabir Pradhan42611e02018-11-27 14:04:02 -0800392 NotifyConfigurationChangedArgs args(mContext.getNextSequenceNum(), when);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393 mQueuedListener->notifyConfigurationChanged(&args);
394}
395
396void InputReader::refreshConfigurationLocked(uint32_t changes) {
397 mPolicy->getReaderConfiguration(&mConfig);
398 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
399
400 if (changes) {
Siarhei Vishniakouc5ae0dc2019-07-10 15:51:18 -0700401 ALOGI("Reconfiguring input devices, changes=%s",
402 InputReaderConfiguration::changesToString(changes).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
404
405 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
406 mEventHub->requestReopenDevices();
407 } else {
408 for (size_t i = 0; i < mDevices.size(); i++) {
409 InputDevice* device = mDevices.valueAt(i);
410 device->configure(now, &mConfig, changes);
411 }
412 }
413 }
414}
415
416void InputReader::updateGlobalMetaStateLocked() {
417 mGlobalMetaState = 0;
418
419 for (size_t i = 0; i < mDevices.size(); i++) {
420 InputDevice* device = mDevices.valueAt(i);
421 mGlobalMetaState |= device->getMetaState();
422 }
423}
424
425int32_t InputReader::getGlobalMetaStateLocked() {
426 return mGlobalMetaState;
427}
428
Michael Wright842500e2015-03-13 17:32:02 -0700429void InputReader::notifyExternalStylusPresenceChanged() {
430 refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
431}
432
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800433void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700434 for (size_t i = 0; i < mDevices.size(); i++) {
435 InputDevice* device = mDevices.valueAt(i);
436 if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS && !device->isIgnored()) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800437 InputDeviceInfo info;
438 device->getDeviceInfo(&info);
439 outDevices.push_back(info);
Michael Wright842500e2015-03-13 17:32:02 -0700440 }
441 }
442}
443
444void InputReader::dispatchExternalStylusState(const StylusState& state) {
445 for (size_t i = 0; i < mDevices.size(); i++) {
446 InputDevice* device = mDevices.valueAt(i);
447 device->updateExternalStylusState(state);
448 }
449}
450
Michael Wrightd02c5b62014-02-10 15:10:22 -0800451void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
452 mDisableVirtualKeysTimeout = time;
453}
454
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700455bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, InputDevice* device, int32_t keyCode,
456 int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457 if (now < mDisableVirtualKeysTimeout) {
458 ALOGI("Dropping virtual key from device %s because virtual keys are "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700459 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
460 device->getName().c_str(), (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode,
461 scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462 return true;
463 } else {
464 return false;
465 }
466}
467
468void InputReader::fadePointerLocked() {
469 for (size_t i = 0; i < mDevices.size(); i++) {
470 InputDevice* device = mDevices.valueAt(i);
471 device->fadePointer();
472 }
473}
474
475void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
476 if (when < mNextTimeout) {
477 mNextTimeout = when;
478 mEventHub->wake();
479 }
480}
481
482int32_t InputReader::bumpGenerationLocked() {
483 return ++mGeneration;
484}
485
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800486void InputReader::getInputDevices(std::vector<InputDeviceInfo>& outInputDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800487 AutoMutex _l(mLock);
488 getInputDevicesLocked(outInputDevices);
489}
490
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800491void InputReader::getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492 outInputDevices.clear();
493
494 size_t numDevices = mDevices.size();
495 for (size_t i = 0; i < numDevices; i++) {
496 InputDevice* device = mDevices.valueAt(i);
497 if (!device->isIgnored()) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800498 InputDeviceInfo info;
499 device->getDeviceInfo(&info);
500 outInputDevices.push_back(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 }
502 }
503}
504
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700505int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506 AutoMutex _l(mLock);
507
508 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
509}
510
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700511int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512 AutoMutex _l(mLock);
513
514 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
515}
516
517int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
518 AutoMutex _l(mLock);
519
520 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
521}
522
523int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700524 GetStateFunc getStateFunc) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525 int32_t result = AKEY_STATE_UNKNOWN;
526 if (deviceId >= 0) {
527 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
528 if (deviceIndex >= 0) {
529 InputDevice* device = mDevices.valueAt(deviceIndex);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700530 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531 result = (device->*getStateFunc)(sourceMask, code);
532 }
533 }
534 } else {
535 size_t numDevices = mDevices.size();
536 for (size_t i = 0; i < numDevices; i++) {
537 InputDevice* device = mDevices.valueAt(i);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700538 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
540 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
541 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
542 if (currentResult >= AKEY_STATE_DOWN) {
543 return currentResult;
544 } else if (currentResult == AKEY_STATE_UP) {
545 result = currentResult;
546 }
547 }
548 }
549 }
550 return result;
551}
552
Andrii Kulian763a3a42016-03-08 10:46:16 -0800553void InputReader::toggleCapsLockState(int32_t deviceId) {
554 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
555 if (deviceIndex < 0) {
556 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
557 return;
558 }
559
560 InputDevice* device = mDevices.valueAt(deviceIndex);
561 if (device->isIgnored()) {
562 return;
563 }
564
565 device->updateMetaState(AKEYCODE_CAPS_LOCK);
566}
567
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700568bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
569 const int32_t* keyCodes, uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570 AutoMutex _l(mLock);
571
572 memset(outFlags, 0, numCodes);
573 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
574}
575
576bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700577 size_t numCodes, const int32_t* keyCodes,
578 uint8_t* outFlags) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 bool result = false;
580 if (deviceId >= 0) {
581 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
582 if (deviceIndex >= 0) {
583 InputDevice* device = mDevices.valueAt(deviceIndex);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700584 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
585 result = device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586 }
587 }
588 } else {
589 size_t numDevices = mDevices.size();
590 for (size_t i = 0; i < numDevices; i++) {
591 InputDevice* device = mDevices.valueAt(i);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700592 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
593 result |= device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 }
595 }
596 }
597 return result;
598}
599
600void InputReader::requestRefreshConfiguration(uint32_t changes) {
601 AutoMutex _l(mLock);
602
603 if (changes) {
604 bool needWake = !mConfigurationChangesToRefresh;
605 mConfigurationChangesToRefresh |= changes;
606
607 if (needWake) {
608 mEventHub->wake();
609 }
610 }
611}
612
613void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700614 ssize_t repeat, int32_t token) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615 AutoMutex _l(mLock);
616
617 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
618 if (deviceIndex >= 0) {
619 InputDevice* device = mDevices.valueAt(deviceIndex);
620 device->vibrate(pattern, patternSize, repeat, token);
621 }
622}
623
624void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
625 AutoMutex _l(mLock);
626
627 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
628 if (deviceIndex >= 0) {
629 InputDevice* device = mDevices.valueAt(deviceIndex);
630 device->cancelVibrate(token);
631 }
632}
633
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700634bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
635 AutoMutex _l(mLock);
636
637 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
638 if (deviceIndex >= 0) {
639 InputDevice* device = mDevices.valueAt(deviceIndex);
640 return device->isEnabled();
641 }
642 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
643 return false;
644}
645
Arthur Hungc23540e2018-11-29 20:42:11 +0800646bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
647 AutoMutex _l(mLock);
648
649 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
650 if (deviceIndex < 0) {
651 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
652 return false;
653 }
654
655 InputDevice* device = mDevices.valueAt(deviceIndex);
Arthur Hung2c9a3342019-07-23 14:18:59 +0800656 if (!device->isEnabled()) {
657 ALOGW("Ignoring disabled device %s", device->getName().c_str());
658 return false;
659 }
660
661 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplayId();
Arthur Hungc23540e2018-11-29 20:42:11 +0800662 // No associated display. By default, can dispatch to all displays.
663 if (!associatedDisplayId) {
664 return true;
665 }
666
667 if (*associatedDisplayId == ADISPLAY_ID_NONE) {
Arthur Hung2c9a3342019-07-23 14:18:59 +0800668 ALOGW("Device %s is associated with display ADISPLAY_ID_NONE.", device->getName().c_str());
Arthur Hungc23540e2018-11-29 20:42:11 +0800669 return true;
670 }
671
672 return *associatedDisplayId == displayId;
673}
674
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800675void InputReader::dump(std::string& dump) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676 AutoMutex _l(mLock);
677
678 mEventHub->dump(dump);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800679 dump += "\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800681 dump += "Input Reader State:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682
683 for (size_t i = 0; i < mDevices.size(); i++) {
684 mDevices.valueAt(i)->dump(dump);
685 }
686
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800687 dump += INDENT "Configuration:\n";
688 dump += INDENT2 "ExcludedDeviceNames: [";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
690 if (i != 0) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800691 dump += ", ";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800692 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100693 dump += mConfig.excludedDeviceNames[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800695 dump += "]\n";
696 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700697 mConfig.virtualKeyQuietTime * 0.000001f);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800698
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800699 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700700 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
701 "acceleration=%0.3f\n",
702 mConfig.pointerVelocityControlParameters.scale,
703 mConfig.pointerVelocityControlParameters.lowThreshold,
704 mConfig.pointerVelocityControlParameters.highThreshold,
705 mConfig.pointerVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800707 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700708 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
709 "acceleration=%0.3f\n",
710 mConfig.wheelVelocityControlParameters.scale,
711 mConfig.wheelVelocityControlParameters.lowThreshold,
712 mConfig.wheelVelocityControlParameters.highThreshold,
713 mConfig.wheelVelocityControlParameters.acceleration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800715 dump += StringPrintf(INDENT2 "PointerGesture:\n");
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700716 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800717 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700718 mConfig.pointerGestureQuietInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800719 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700720 mConfig.pointerGestureDragMinSwitchSpeed);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800721 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700722 mConfig.pointerGestureTapInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800723 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700724 mConfig.pointerGestureTapDragInterval * 0.000001f);
725 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800726 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700727 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800728 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700729 mConfig.pointerGestureMultitouchMinDistance);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800730 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700731 mConfig.pointerGestureSwipeTransitionAngleCosine);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800732 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700733 mConfig.pointerGestureSwipeMaxWidthRatio);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800734 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700735 mConfig.pointerGestureMovementSpeedRatio);
736 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700737
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800738 dump += INDENT3 "Viewports:\n";
Santos Cordonfa5cf462017-04-05 10:37:00 -0700739 mConfig.dump(dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740}
741
742void InputReader::monitor() {
743 // Acquire and release the lock to ensure that the reader has not deadlocked.
744 mLock.lock();
745 mEventHub->wake();
746 mReaderIsAliveCondition.wait(mLock);
747 mLock.unlock();
748
749 // Check the EventHub
750 mEventHub->monitor();
751}
752
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753// --- InputReader::ContextImpl ---
754
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700755InputReader::ContextImpl::ContextImpl(InputReader* reader) : mReader(reader) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756
757void InputReader::ContextImpl::updateGlobalMetaState() {
758 // lock is already held by the input loop
759 mReader->updateGlobalMetaStateLocked();
760}
761
762int32_t InputReader::ContextImpl::getGlobalMetaState() {
763 // lock is already held by the input loop
764 return mReader->getGlobalMetaStateLocked();
765}
766
767void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
768 // lock is already held by the input loop
769 mReader->disableVirtualKeysUntilLocked(time);
770}
771
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700772bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, InputDevice* device,
773 int32_t keyCode, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774 // lock is already held by the input loop
775 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
776}
777
778void InputReader::ContextImpl::fadePointer() {
779 // lock is already held by the input loop
780 mReader->fadePointerLocked();
781}
782
783void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
784 // lock is already held by the input loop
785 mReader->requestTimeoutAtTimeLocked(when);
786}
787
788int32_t InputReader::ContextImpl::bumpGeneration() {
789 // lock is already held by the input loop
790 return mReader->bumpGenerationLocked();
791}
792
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800793void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700794 // lock is already held by whatever called refreshConfigurationLocked
795 mReader->getExternalStylusDevicesLocked(outDevices);
796}
797
798void InputReader::ContextImpl::dispatchExternalStylusState(const StylusState& state) {
799 mReader->dispatchExternalStylusState(state);
800}
801
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
803 return mReader->mPolicy.get();
804}
805
806InputListenerInterface* InputReader::ContextImpl::getListener() {
807 return mReader->mQueuedListener.get();
808}
809
810EventHubInterface* InputReader::ContextImpl::getEventHub() {
811 return mReader->mEventHub.get();
812}
813
Prabir Pradhan42611e02018-11-27 14:04:02 -0800814uint32_t InputReader::ContextImpl::getNextSequenceNum() {
815 return (mReader->mNextSequenceNum)++;
816}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818} // namespace android