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
17 #include "Macros.h"
18
19 #include "InputReader.h"
20
21 #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
32 #include <errno.h>
33 #include <inttypes.h>
34 #include <limits.h>
35 #include <math.h>
36 #include <stddef.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39
40 #include <log/log.h>
41
42 #include <android-base/stringprintf.h>
43 #include <input/Keyboard.h>
44 #include <input/VirtualKeyMap.h>
45
46
47 using android::base::StringPrintf;
48
49 namespace android {
50
InputReader(const sp<EventHubInterface> & eventHub,const sp<InputReaderPolicyInterface> & policy,const sp<InputListenerInterface> & listener)51 InputReader::InputReader(const sp<EventHubInterface>& eventHub,
52 const sp<InputReaderPolicyInterface>& policy,
53 const sp<InputListenerInterface>& listener)
54 : mContext(this),
55 mEventHub(eventHub),
56 mPolicy(policy),
57 mNextSequenceNum(1),
58 mGlobalMetaState(0),
59 mGeneration(1),
60 mDisableVirtualKeysTimeout(LLONG_MIN),
61 mNextTimeout(LLONG_MAX),
62 mConfigurationChangesToRefresh(0) {
63 mQueuedListener = new QueuedInputListener(listener);
64
65 { // acquire lock
66 AutoMutex _l(mLock);
67
68 refreshConfigurationLocked(0);
69 updateGlobalMetaStateLocked();
70 } // release lock
71 }
72
~InputReader()73 InputReader::~InputReader() {
74 for (size_t i = 0; i < mDevices.size(); i++) {
75 delete mDevices.valueAt(i);
76 }
77 }
78
loopOnce()79 void InputReader::loopOnce() {
80 int32_t oldGeneration;
81 int32_t timeoutMillis;
82 bool inputDevicesChanged = false;
83 std::vector<InputDeviceInfo> inputDevices;
84 { // acquire lock
85 AutoMutex _l(mLock);
86
87 oldGeneration = mGeneration;
88 timeoutMillis = -1;
89
90 uint32_t changes = mConfigurationChangesToRefresh;
91 if (changes) {
92 mConfigurationChangesToRefresh = 0;
93 timeoutMillis = 0;
94 refreshConfigurationLocked(changes);
95 } else if (mNextTimeout != LLONG_MAX) {
96 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
97 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
98 }
99 } // release lock
100
101 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
102
103 { // acquire lock
104 AutoMutex _l(mLock);
105 mReaderIsAliveCondition.broadcast();
106
107 if (count) {
108 processEventsLocked(mEventBuffer, count);
109 }
110
111 if (mNextTimeout != LLONG_MAX) {
112 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
113 if (now >= mNextTimeout) {
114 #if DEBUG_RAW_EVENTS
115 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
116 #endif
117 mNextTimeout = LLONG_MAX;
118 timeoutExpiredLocked(now);
119 }
120 }
121
122 if (oldGeneration != mGeneration) {
123 inputDevicesChanged = true;
124 getInputDevicesLocked(inputDevices);
125 }
126 } // release lock
127
128 // Send out a message that the describes the changed input devices.
129 if (inputDevicesChanged) {
130 mPolicy->notifyInputDevicesChanged(inputDevices);
131 }
132
133 // Flush queued events out to the listener.
134 // This must happen outside of the lock because the listener could potentially call
135 // back into the InputReader's methods, such as getScanCodeState, or become blocked
136 // on another thread similarly waiting to acquire the InputReader lock thereby
137 // resulting in a deadlock. This situation is actually quite plausible because the
138 // listener is actually the input dispatcher, which calls into the window manager,
139 // which occasionally calls into the input reader.
140 mQueuedListener->flush();
141 }
142
processEventsLocked(const RawEvent * rawEvents,size_t count)143 void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
144 for (const RawEvent* rawEvent = rawEvents; count;) {
145 int32_t type = rawEvent->type;
146 size_t batchSize = 1;
147 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
148 int32_t deviceId = rawEvent->deviceId;
149 while (batchSize < count) {
150 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||
151 rawEvent[batchSize].deviceId != deviceId) {
152 break;
153 }
154 batchSize += 1;
155 }
156 #if DEBUG_RAW_EVENTS
157 ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
158 #endif
159 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
160 } else {
161 switch (rawEvent->type) {
162 case EventHubInterface::DEVICE_ADDED:
163 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
164 break;
165 case EventHubInterface::DEVICE_REMOVED:
166 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
167 break;
168 case EventHubInterface::FINISHED_DEVICE_SCAN:
169 handleConfigurationChangedLocked(rawEvent->when);
170 break;
171 default:
172 ALOG_ASSERT(false); // can't happen
173 break;
174 }
175 }
176 count -= batchSize;
177 rawEvent += batchSize;
178 }
179 }
180
addDeviceLocked(nsecs_t when,int32_t deviceId)181 void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
182 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
183 if (deviceIndex >= 0) {
184 ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
185 return;
186 }
187
188 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId);
189 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
190 int32_t controllerNumber = mEventHub->getDeviceControllerNumber(deviceId);
191
192 InputDevice* device = createDeviceLocked(deviceId, controllerNumber, identifier, classes);
193 device->configure(when, &mConfig, 0);
194 device->reset(when);
195
196 if (device->isIgnored()) {
197 ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId,
198 identifier.name.c_str());
199 } else {
200 ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, identifier.name.c_str(),
201 device->getSources());
202 }
203
204 mDevices.add(deviceId, device);
205 bumpGenerationLocked();
206
207 if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
208 notifyExternalStylusPresenceChanged();
209 }
210 }
211
removeDeviceLocked(nsecs_t when,int32_t deviceId)212 void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
213 InputDevice* device = nullptr;
214 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
215 if (deviceIndex < 0) {
216 ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
217 return;
218 }
219
220 device = mDevices.valueAt(deviceIndex);
221 mDevices.removeItemsAt(deviceIndex, 1);
222 bumpGenerationLocked();
223
224 if (device->isIgnored()) {
225 ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)", device->getId(),
226 device->getName().c_str());
227 } else {
228 ALOGI("Device removed: id=%d, name='%s', sources=0x%08x", device->getId(),
229 device->getName().c_str(), device->getSources());
230 }
231
232 if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
233 notifyExternalStylusPresenceChanged();
234 }
235
236 device->reset(when);
237 delete device;
238 }
239
createDeviceLocked(int32_t deviceId,int32_t controllerNumber,const InputDeviceIdentifier & identifier,uint32_t classes)240 InputDevice* InputReader::createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
241 const InputDeviceIdentifier& identifier,
242 uint32_t classes) {
243 InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(),
244 controllerNumber, identifier, classes);
245
246 // External devices.
247 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
248 device->setExternal(true);
249 }
250
251 // Devices with mics.
252 if (classes & INPUT_DEVICE_CLASS_MIC) {
253 device->setMic(true);
254 }
255
256 // Switch-like devices.
257 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
258 device->addMapper(new SwitchInputMapper(device));
259 }
260
261 // Scroll wheel-like devices.
262 if (classes & INPUT_DEVICE_CLASS_ROTARY_ENCODER) {
263 device->addMapper(new RotaryEncoderInputMapper(device));
264 }
265
266 // Vibrator-like devices.
267 if (classes & INPUT_DEVICE_CLASS_VIBRATOR) {
268 device->addMapper(new VibratorInputMapper(device));
269 }
270
271 // Keyboard-like devices.
272 uint32_t keyboardSource = 0;
273 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
274 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
275 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
276 }
277 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
278 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
279 }
280 if (classes & INPUT_DEVICE_CLASS_DPAD) {
281 keyboardSource |= AINPUT_SOURCE_DPAD;
282 }
283 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
284 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
285 }
286
287 if (keyboardSource != 0) {
288 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
289 }
290
291 // Cursor-like devices.
292 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
293 device->addMapper(new CursorInputMapper(device));
294 }
295
296 // Touchscreens and touchpad devices.
297 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
298 device->addMapper(new MultiTouchInputMapper(device));
299 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
300 device->addMapper(new SingleTouchInputMapper(device));
301 }
302
303 // Joystick-like devices.
304 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
305 device->addMapper(new JoystickInputMapper(device));
306 }
307
308 // External stylus-like devices.
309 if (classes & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
310 device->addMapper(new ExternalStylusInputMapper(device));
311 }
312
313 return device;
314 }
315
processEventsForDeviceLocked(int32_t deviceId,const RawEvent * rawEvents,size_t count)316 void InputReader::processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents,
317 size_t count) {
318 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
319 if (deviceIndex < 0) {
320 ALOGW("Discarding event for unknown deviceId %d.", deviceId);
321 return;
322 }
323
324 InputDevice* device = mDevices.valueAt(deviceIndex);
325 if (device->isIgnored()) {
326 // ALOGD("Discarding event for ignored deviceId %d.", deviceId);
327 return;
328 }
329
330 device->process(rawEvents, count);
331 }
332
timeoutExpiredLocked(nsecs_t when)333 void InputReader::timeoutExpiredLocked(nsecs_t when) {
334 for (size_t i = 0; i < mDevices.size(); i++) {
335 InputDevice* device = mDevices.valueAt(i);
336 if (!device->isIgnored()) {
337 device->timeoutExpired(when);
338 }
339 }
340 }
341
handleConfigurationChangedLocked(nsecs_t when)342 void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
343 // Reset global meta state because it depends on the list of all configured devices.
344 updateGlobalMetaStateLocked();
345
346 // Enqueue configuration changed.
347 NotifyConfigurationChangedArgs args(mContext.getNextSequenceNum(), when);
348 mQueuedListener->notifyConfigurationChanged(&args);
349 }
350
refreshConfigurationLocked(uint32_t changes)351 void InputReader::refreshConfigurationLocked(uint32_t changes) {
352 mPolicy->getReaderConfiguration(&mConfig);
353 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
354
355 if (changes) {
356 ALOGI("Reconfiguring input devices. changes=0x%08x", changes);
357 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
358
359 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
360 mEventHub->requestReopenDevices();
361 } else {
362 for (size_t i = 0; i < mDevices.size(); i++) {
363 InputDevice* device = mDevices.valueAt(i);
364 device->configure(now, &mConfig, changes);
365 }
366 }
367 }
368 }
369
updateGlobalMetaStateLocked()370 void InputReader::updateGlobalMetaStateLocked() {
371 mGlobalMetaState = 0;
372
373 for (size_t i = 0; i < mDevices.size(); i++) {
374 InputDevice* device = mDevices.valueAt(i);
375 mGlobalMetaState |= device->getMetaState();
376 }
377 }
378
getGlobalMetaStateLocked()379 int32_t InputReader::getGlobalMetaStateLocked() {
380 return mGlobalMetaState;
381 }
382
notifyExternalStylusPresenceChanged()383 void InputReader::notifyExternalStylusPresenceChanged() {
384 refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
385 }
386
getExternalStylusDevicesLocked(std::vector<InputDeviceInfo> & outDevices)387 void InputReader::getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) {
388 for (size_t i = 0; i < mDevices.size(); i++) {
389 InputDevice* device = mDevices.valueAt(i);
390 if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS && !device->isIgnored()) {
391 InputDeviceInfo info;
392 device->getDeviceInfo(&info);
393 outDevices.push_back(info);
394 }
395 }
396 }
397
dispatchExternalStylusState(const StylusState & state)398 void InputReader::dispatchExternalStylusState(const StylusState& state) {
399 for (size_t i = 0; i < mDevices.size(); i++) {
400 InputDevice* device = mDevices.valueAt(i);
401 device->updateExternalStylusState(state);
402 }
403 }
404
disableVirtualKeysUntilLocked(nsecs_t time)405 void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
406 mDisableVirtualKeysTimeout = time;
407 }
408
shouldDropVirtualKeyLocked(nsecs_t now,InputDevice * device,int32_t keyCode,int32_t scanCode)409 bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, InputDevice* device, int32_t keyCode,
410 int32_t scanCode) {
411 if (now < mDisableVirtualKeysTimeout) {
412 ALOGI("Dropping virtual key from device %s because virtual keys are "
413 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
414 device->getName().c_str(), (mDisableVirtualKeysTimeout - now) * 0.000001, keyCode,
415 scanCode);
416 return true;
417 } else {
418 return false;
419 }
420 }
421
fadePointerLocked()422 void InputReader::fadePointerLocked() {
423 for (size_t i = 0; i < mDevices.size(); i++) {
424 InputDevice* device = mDevices.valueAt(i);
425 device->fadePointer();
426 }
427 }
428
requestTimeoutAtTimeLocked(nsecs_t when)429 void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
430 if (when < mNextTimeout) {
431 mNextTimeout = when;
432 mEventHub->wake();
433 }
434 }
435
bumpGenerationLocked()436 int32_t InputReader::bumpGenerationLocked() {
437 return ++mGeneration;
438 }
439
getInputDevices(std::vector<InputDeviceInfo> & outInputDevices)440 void InputReader::getInputDevices(std::vector<InputDeviceInfo>& outInputDevices) {
441 AutoMutex _l(mLock);
442 getInputDevicesLocked(outInputDevices);
443 }
444
getInputDevicesLocked(std::vector<InputDeviceInfo> & outInputDevices)445 void InputReader::getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices) {
446 outInputDevices.clear();
447
448 size_t numDevices = mDevices.size();
449 for (size_t i = 0; i < numDevices; i++) {
450 InputDevice* device = mDevices.valueAt(i);
451 if (!device->isIgnored()) {
452 InputDeviceInfo info;
453 device->getDeviceInfo(&info);
454 outInputDevices.push_back(info);
455 }
456 }
457 }
458
getKeyCodeState(int32_t deviceId,uint32_t sourceMask,int32_t keyCode)459 int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
460 AutoMutex _l(mLock);
461
462 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
463 }
464
getScanCodeState(int32_t deviceId,uint32_t sourceMask,int32_t scanCode)465 int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
466 AutoMutex _l(mLock);
467
468 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
469 }
470
getSwitchState(int32_t deviceId,uint32_t sourceMask,int32_t switchCode)471 int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
472 AutoMutex _l(mLock);
473
474 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
475 }
476
getStateLocked(int32_t deviceId,uint32_t sourceMask,int32_t code,GetStateFunc getStateFunc)477 int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
478 GetStateFunc getStateFunc) {
479 int32_t result = AKEY_STATE_UNKNOWN;
480 if (deviceId >= 0) {
481 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
482 if (deviceIndex >= 0) {
483 InputDevice* device = mDevices.valueAt(deviceIndex);
484 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
485 result = (device->*getStateFunc)(sourceMask, code);
486 }
487 }
488 } else {
489 size_t numDevices = mDevices.size();
490 for (size_t i = 0; i < numDevices; i++) {
491 InputDevice* device = mDevices.valueAt(i);
492 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
493 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
494 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
495 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
496 if (currentResult >= AKEY_STATE_DOWN) {
497 return currentResult;
498 } else if (currentResult == AKEY_STATE_UP) {
499 result = currentResult;
500 }
501 }
502 }
503 }
504 return result;
505 }
506
toggleCapsLockState(int32_t deviceId)507 void InputReader::toggleCapsLockState(int32_t deviceId) {
508 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
509 if (deviceIndex < 0) {
510 ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
511 return;
512 }
513
514 InputDevice* device = mDevices.valueAt(deviceIndex);
515 if (device->isIgnored()) {
516 return;
517 }
518
519 device->updateMetaState(AKEYCODE_CAPS_LOCK);
520 }
521
hasKeys(int32_t deviceId,uint32_t sourceMask,size_t numCodes,const int32_t * keyCodes,uint8_t * outFlags)522 bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
523 const int32_t* keyCodes, uint8_t* outFlags) {
524 AutoMutex _l(mLock);
525
526 memset(outFlags, 0, numCodes);
527 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
528 }
529
markSupportedKeyCodesLocked(int32_t deviceId,uint32_t sourceMask,size_t numCodes,const int32_t * keyCodes,uint8_t * outFlags)530 bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
531 size_t numCodes, const int32_t* keyCodes,
532 uint8_t* outFlags) {
533 bool result = false;
534 if (deviceId >= 0) {
535 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
536 if (deviceIndex >= 0) {
537 InputDevice* device = mDevices.valueAt(deviceIndex);
538 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
539 result = device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
540 }
541 }
542 } else {
543 size_t numDevices = mDevices.size();
544 for (size_t i = 0; i < numDevices; i++) {
545 InputDevice* device = mDevices.valueAt(i);
546 if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
547 result |= device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
548 }
549 }
550 }
551 return result;
552 }
553
requestRefreshConfiguration(uint32_t changes)554 void InputReader::requestRefreshConfiguration(uint32_t changes) {
555 AutoMutex _l(mLock);
556
557 if (changes) {
558 bool needWake = !mConfigurationChangesToRefresh;
559 mConfigurationChangesToRefresh |= changes;
560
561 if (needWake) {
562 mEventHub->wake();
563 }
564 }
565 }
566
vibrate(int32_t deviceId,const nsecs_t * pattern,size_t patternSize,ssize_t repeat,int32_t token)567 void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
568 ssize_t repeat, int32_t token) {
569 AutoMutex _l(mLock);
570
571 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
572 if (deviceIndex >= 0) {
573 InputDevice* device = mDevices.valueAt(deviceIndex);
574 device->vibrate(pattern, patternSize, repeat, token);
575 }
576 }
577
cancelVibrate(int32_t deviceId,int32_t token)578 void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
579 AutoMutex _l(mLock);
580
581 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
582 if (deviceIndex >= 0) {
583 InputDevice* device = mDevices.valueAt(deviceIndex);
584 device->cancelVibrate(token);
585 }
586 }
587
isInputDeviceEnabled(int32_t deviceId)588 bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
589 AutoMutex _l(mLock);
590
591 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
592 if (deviceIndex >= 0) {
593 InputDevice* device = mDevices.valueAt(deviceIndex);
594 return device->isEnabled();
595 }
596 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
597 return false;
598 }
599
canDispatchToDisplay(int32_t deviceId,int32_t displayId)600 bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
601 AutoMutex _l(mLock);
602
603 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
604 if (deviceIndex < 0) {
605 ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
606 return false;
607 }
608
609 InputDevice* device = mDevices.valueAt(deviceIndex);
610 std::optional<int32_t> associatedDisplayId = device->getAssociatedDisplay();
611 // No associated display. By default, can dispatch to all displays.
612 if (!associatedDisplayId) {
613 return true;
614 }
615
616 if (*associatedDisplayId == ADISPLAY_ID_NONE) {
617 ALOGW("Device has associated, but no associated display id.");
618 return true;
619 }
620
621 return *associatedDisplayId == displayId;
622 }
623
dump(std::string & dump)624 void InputReader::dump(std::string& dump) {
625 AutoMutex _l(mLock);
626
627 mEventHub->dump(dump);
628 dump += "\n";
629
630 dump += "Input Reader State:\n";
631
632 for (size_t i = 0; i < mDevices.size(); i++) {
633 mDevices.valueAt(i)->dump(dump);
634 }
635
636 dump += INDENT "Configuration:\n";
637 dump += INDENT2 "ExcludedDeviceNames: [";
638 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
639 if (i != 0) {
640 dump += ", ";
641 }
642 dump += mConfig.excludedDeviceNames[i];
643 }
644 dump += "]\n";
645 dump += StringPrintf(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
646 mConfig.virtualKeyQuietTime * 0.000001f);
647
648 dump += StringPrintf(INDENT2 "PointerVelocityControlParameters: "
649 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
650 "acceleration=%0.3f\n",
651 mConfig.pointerVelocityControlParameters.scale,
652 mConfig.pointerVelocityControlParameters.lowThreshold,
653 mConfig.pointerVelocityControlParameters.highThreshold,
654 mConfig.pointerVelocityControlParameters.acceleration);
655
656 dump += StringPrintf(INDENT2 "WheelVelocityControlParameters: "
657 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
658 "acceleration=%0.3f\n",
659 mConfig.wheelVelocityControlParameters.scale,
660 mConfig.wheelVelocityControlParameters.lowThreshold,
661 mConfig.wheelVelocityControlParameters.highThreshold,
662 mConfig.wheelVelocityControlParameters.acceleration);
663
664 dump += StringPrintf(INDENT2 "PointerGesture:\n");
665 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(mConfig.pointerGesturesEnabled));
666 dump += StringPrintf(INDENT3 "QuietInterval: %0.1fms\n",
667 mConfig.pointerGestureQuietInterval * 0.000001f);
668 dump += StringPrintf(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
669 mConfig.pointerGestureDragMinSwitchSpeed);
670 dump += StringPrintf(INDENT3 "TapInterval: %0.1fms\n",
671 mConfig.pointerGestureTapInterval * 0.000001f);
672 dump += StringPrintf(INDENT3 "TapDragInterval: %0.1fms\n",
673 mConfig.pointerGestureTapDragInterval * 0.000001f);
674 dump += StringPrintf(INDENT3 "TapSlop: %0.1fpx\n", mConfig.pointerGestureTapSlop);
675 dump += StringPrintf(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
676 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
677 dump += StringPrintf(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
678 mConfig.pointerGestureMultitouchMinDistance);
679 dump += StringPrintf(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
680 mConfig.pointerGestureSwipeTransitionAngleCosine);
681 dump += StringPrintf(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
682 mConfig.pointerGestureSwipeMaxWidthRatio);
683 dump += StringPrintf(INDENT3 "MovementSpeedRatio: %0.1f\n",
684 mConfig.pointerGestureMovementSpeedRatio);
685 dump += StringPrintf(INDENT3 "ZoomSpeedRatio: %0.1f\n", mConfig.pointerGestureZoomSpeedRatio);
686
687 dump += INDENT3 "Viewports:\n";
688 mConfig.dump(dump);
689 }
690
monitor()691 void InputReader::monitor() {
692 // Acquire and release the lock to ensure that the reader has not deadlocked.
693 mLock.lock();
694 mEventHub->wake();
695 mReaderIsAliveCondition.wait(mLock);
696 mLock.unlock();
697
698 // Check the EventHub
699 mEventHub->monitor();
700 }
701
702 // --- InputReader::ContextImpl ---
703
ContextImpl(InputReader * reader)704 InputReader::ContextImpl::ContextImpl(InputReader* reader) : mReader(reader) {}
705
updateGlobalMetaState()706 void InputReader::ContextImpl::updateGlobalMetaState() {
707 // lock is already held by the input loop
708 mReader->updateGlobalMetaStateLocked();
709 }
710
getGlobalMetaState()711 int32_t InputReader::ContextImpl::getGlobalMetaState() {
712 // lock is already held by the input loop
713 return mReader->getGlobalMetaStateLocked();
714 }
715
disableVirtualKeysUntil(nsecs_t time)716 void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
717 // lock is already held by the input loop
718 mReader->disableVirtualKeysUntilLocked(time);
719 }
720
shouldDropVirtualKey(nsecs_t now,InputDevice * device,int32_t keyCode,int32_t scanCode)721 bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, InputDevice* device,
722 int32_t keyCode, int32_t scanCode) {
723 // lock is already held by the input loop
724 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
725 }
726
fadePointer()727 void InputReader::ContextImpl::fadePointer() {
728 // lock is already held by the input loop
729 mReader->fadePointerLocked();
730 }
731
requestTimeoutAtTime(nsecs_t when)732 void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
733 // lock is already held by the input loop
734 mReader->requestTimeoutAtTimeLocked(when);
735 }
736
bumpGeneration()737 int32_t InputReader::ContextImpl::bumpGeneration() {
738 // lock is already held by the input loop
739 return mReader->bumpGenerationLocked();
740 }
741
getExternalStylusDevices(std::vector<InputDeviceInfo> & outDevices)742 void InputReader::ContextImpl::getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
743 // lock is already held by whatever called refreshConfigurationLocked
744 mReader->getExternalStylusDevicesLocked(outDevices);
745 }
746
dispatchExternalStylusState(const StylusState & state)747 void InputReader::ContextImpl::dispatchExternalStylusState(const StylusState& state) {
748 mReader->dispatchExternalStylusState(state);
749 }
750
getPolicy()751 InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
752 return mReader->mPolicy.get();
753 }
754
getListener()755 InputListenerInterface* InputReader::ContextImpl::getListener() {
756 return mReader->mQueuedListener.get();
757 }
758
getEventHub()759 EventHubInterface* InputReader::ContextImpl::getEventHub() {
760 return mReader->mEventHub.get();
761 }
762
getNextSequenceNum()763 uint32_t InputReader::ContextImpl::getNextSequenceNum() {
764 return (mReader->mNextSequenceNum)++;
765 }
766
767 } // namespace android
768