blob: aa241a00e93afafa69dc20f50ed85d1e9d1571ac [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2005 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
Mark Salyzyn5aa26b22014-06-10 13:07:44 -070017#include <assert.h>
18#include <dirent.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <inttypes.h>
22#include <memory.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/epoll.h>
28#include <sys/limits.h>
29#include <sys/inotify.h>
30#include <sys/ioctl.h>
Mark Salyzyn5aa26b22014-06-10 13:07:44 -070031#include <sys/utsname.h>
32#include <unistd.h>
33
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#define LOG_TAG "EventHub"
35
36// #define LOG_NDEBUG 0
37
38#include "EventHub.h"
39
40#include <hardware_legacy/power.h>
41
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080042#include <android-base/stringprintf.h>
Dan Albert677d87e2014-06-16 17:31:28 -070043#include <openssl/sha.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080044#include <utils/Log.h>
45#include <utils/Timers.h>
46#include <utils/threads.h>
47#include <utils/Errors.h>
48
Michael Wrightd02c5b62014-02-10 15:10:22 -080049#include <input/KeyLayoutMap.h>
50#include <input/KeyCharacterMap.h>
51#include <input/VirtualKeyMap.h>
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053/* this macro is used to tell if "bit" is set in "array"
54 * it selects a byte from the array, and does a boolean AND
55 * operation with a byte that only has the relevant bit set.
56 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
57 */
Chih-Hung Hsieh4a186d42016-05-20 11:33:26 -070058#define test_bit(bit, array) ((array)[(bit)/8] & (1<<((bit)%8)))
Michael Wrightd02c5b62014-02-10 15:10:22 -080059
60/* this macro computes the number of bytes needed to represent a bit array of the specified size */
Chih-Hung Hsieh4a186d42016-05-20 11:33:26 -070061#define sizeof_bit_array(bits) (((bits) + 7) / 8)
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
63#define INDENT " "
64#define INDENT2 " "
65#define INDENT3 " "
66
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080067using android::base::StringPrintf;
68
Michael Wrightd02c5b62014-02-10 15:10:22 -080069namespace android {
70
Siarhei Vishniakou25920312018-12-12 15:24:44 -080071static constexpr bool DEBUG = false;
72
Michael Wrightd02c5b62014-02-10 15:10:22 -080073static const char *WAKE_LOCK_ID = "KeyEvents";
74static const char *DEVICE_PATH = "/dev/input";
Siarhei Vishniakou951f3622018-12-12 19:45:42 -080075// v4l2 devices go directly into /dev
76static const char *VIDEO_DEVICE_PATH = "/dev";
Michael Wrightd02c5b62014-02-10 15:10:22 -080077
Michael Wrightd02c5b62014-02-10 15:10:22 -080078static inline const char* toString(bool value) {
79 return value ? "true" : "false";
80}
81
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010082static std::string sha1(const std::string& in) {
Dan Albert677d87e2014-06-16 17:31:28 -070083 SHA_CTX ctx;
84 SHA1_Init(&ctx);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010085 SHA1_Update(&ctx, reinterpret_cast<const u_char*>(in.c_str()), in.size());
Dan Albert677d87e2014-06-16 17:31:28 -070086 u_char digest[SHA_DIGEST_LENGTH];
87 SHA1_Final(digest, &ctx);
Michael Wrightd02c5b62014-02-10 15:10:22 -080088
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010089 std::string out;
Dan Albert677d87e2014-06-16 17:31:28 -070090 for (size_t i = 0; i < SHA_DIGEST_LENGTH; i++) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010091 out += StringPrintf("%02x", digest[i]);
Michael Wrightd02c5b62014-02-10 15:10:22 -080092 }
93 return out;
94}
95
96static void getLinuxRelease(int* major, int* minor) {
97 struct utsname info;
98 if (uname(&info) || sscanf(info.release, "%d.%d", major, minor) <= 0) {
99 *major = 0, *minor = 0;
100 ALOGE("Could not get linux version: %s", strerror(errno));
101 }
102}
103
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800104/**
105 * Return true if name matches "v4l-touch*"
106 */
107static bool isV4lTouchNode(const char* name) {
108 return strstr(name, "v4l-touch") == name;
109}
110
Siarhei Vishniakou592bac22018-11-08 19:42:38 -0800111static nsecs_t processEventTimestamp(const struct input_event& event) {
112 // Use the time specified in the event instead of the current time
113 // so that downstream code can get more accurate estimates of
114 // event dispatch latency from the time the event is enqueued onto
115 // the evdev client buffer.
116 //
117 // The event's timestamp fortuitously uses the same monotonic clock
118 // time base as the rest of Android. The kernel event device driver
119 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
120 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
121 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
122 // system call that also queries ktime_get_ts().
123
124 const nsecs_t inputEventTime = seconds_to_nanoseconds(event.time.tv_sec) +
125 microseconds_to_nanoseconds(event.time.tv_usec);
126 return inputEventTime;
127}
128
Michael Wrightd02c5b62014-02-10 15:10:22 -0800129// --- Global Functions ---
130
131uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses) {
132 // Touch devices get dibs on touch-related axes.
133 if (deviceClasses & INPUT_DEVICE_CLASS_TOUCH) {
134 switch (axis) {
135 case ABS_X:
136 case ABS_Y:
137 case ABS_PRESSURE:
138 case ABS_TOOL_WIDTH:
139 case ABS_DISTANCE:
140 case ABS_TILT_X:
141 case ABS_TILT_Y:
142 case ABS_MT_SLOT:
143 case ABS_MT_TOUCH_MAJOR:
144 case ABS_MT_TOUCH_MINOR:
145 case ABS_MT_WIDTH_MAJOR:
146 case ABS_MT_WIDTH_MINOR:
147 case ABS_MT_ORIENTATION:
148 case ABS_MT_POSITION_X:
149 case ABS_MT_POSITION_Y:
150 case ABS_MT_TOOL_TYPE:
151 case ABS_MT_BLOB_ID:
152 case ABS_MT_TRACKING_ID:
153 case ABS_MT_PRESSURE:
154 case ABS_MT_DISTANCE:
155 return INPUT_DEVICE_CLASS_TOUCH;
156 }
157 }
158
Michael Wright842500e2015-03-13 17:32:02 -0700159 // External stylus gets the pressure axis
160 if (deviceClasses & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
161 if (axis == ABS_PRESSURE) {
162 return INPUT_DEVICE_CLASS_EXTERNAL_STYLUS;
163 }
164 }
165
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166 // Joystick devices get the rest.
167 return deviceClasses & INPUT_DEVICE_CLASS_JOYSTICK;
168}
169
170// --- EventHub::Device ---
171
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100172EventHub::Device::Device(int fd, int32_t id, const std::string& path,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173 const InputDeviceIdentifier& identifier) :
Yi Kong9b14ac62018-07-17 13:48:38 -0700174 next(nullptr),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175 fd(fd), id(id), path(path), identifier(identifier),
Yi Kong9b14ac62018-07-17 13:48:38 -0700176 classes(0), configuration(nullptr), virtualKeyMap(nullptr),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177 ffEffectPlaying(false), ffEffectId(-1), controllerNumber(0),
Siarhei Vishniakou88786812018-11-09 15:36:21 -0800178 enabled(true), isVirtual(fd < 0) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 memset(keyBitmask, 0, sizeof(keyBitmask));
180 memset(absBitmask, 0, sizeof(absBitmask));
181 memset(relBitmask, 0, sizeof(relBitmask));
182 memset(swBitmask, 0, sizeof(swBitmask));
183 memset(ledBitmask, 0, sizeof(ledBitmask));
184 memset(ffBitmask, 0, sizeof(ffBitmask));
185 memset(propBitmask, 0, sizeof(propBitmask));
186}
187
188EventHub::Device::~Device() {
189 close();
190 delete configuration;
191 delete virtualKeyMap;
192}
193
194void EventHub::Device::close() {
195 if (fd >= 0) {
196 ::close(fd);
197 fd = -1;
198 }
199}
200
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700201status_t EventHub::Device::enable() {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100202 fd = open(path.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700203 if(fd < 0) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100204 ALOGE("could not open %s, %s\n", path.c_str(), strerror(errno));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700205 return -errno;
206 }
207 enabled = true;
208 return OK;
209}
210
211status_t EventHub::Device::disable() {
212 close();
213 enabled = false;
214 return OK;
215}
216
217bool EventHub::Device::hasValidFd() {
218 return !isVirtual && enabled;
219}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220
221// --- EventHub ---
222
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223const int EventHub::EPOLL_MAX_EVENTS;
224
225EventHub::EventHub(void) :
226 mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), mNextDeviceId(1), mControllerNumbers(),
Yi Kong9b14ac62018-07-17 13:48:38 -0700227 mOpeningDevices(nullptr), mClosingDevices(nullptr),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228 mNeedToSendFinishedDeviceScan(false),
229 mNeedToReopenDevices(false), mNeedToScanDevices(true),
230 mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) {
231 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
232
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -0800233 mEpollFd = epoll_create1(EPOLL_CLOEXEC);
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800234 LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance: %s", strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800235
236 mINotifyFd = inotify_init();
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800237 mInputWd = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
238 LOG_ALWAYS_FATAL_IF(mInputWd < 0, "Could not register INotify for %s: %s",
239 DEVICE_PATH, strerror(errno));
240 mVideoWd = inotify_add_watch(mINotifyFd, VIDEO_DEVICE_PATH, IN_DELETE | IN_CREATE);
241 LOG_ALWAYS_FATAL_IF(mVideoWd < 0, "Could not register INotify for %s: %s",
242 VIDEO_DEVICE_PATH, strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244 struct epoll_event eventItem;
245 memset(&eventItem, 0, sizeof(eventItem));
246 eventItem.events = EPOLLIN;
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700247 eventItem.data.fd = mINotifyFd;
Siarhei Vishniakou951f3622018-12-12 19:45:42 -0800248 int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno);
250
251 int wakeFds[2];
252 result = pipe(wakeFds);
253 LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
254
255 mWakeReadPipeFd = wakeFds[0];
256 mWakeWritePipeFd = wakeFds[1];
257
258 result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
259 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
260 errno);
261
262 result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
263 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
264 errno);
265
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700266 eventItem.data.fd = mWakeReadPipeFd;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
268 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
269 errno);
270
271 int major, minor;
272 getLinuxRelease(&major, &minor);
273 // EPOLLWAKEUP was introduced in kernel 3.5
274 mUsingEpollWakeup = major > 3 || (major == 3 && minor >= 5);
275}
276
277EventHub::~EventHub(void) {
278 closeAllDevicesLocked();
279
280 while (mClosingDevices) {
281 Device* device = mClosingDevices;
282 mClosingDevices = device->next;
283 delete device;
284 }
285
286 ::close(mEpollFd);
287 ::close(mINotifyFd);
288 ::close(mWakeReadPipeFd);
289 ::close(mWakeWritePipeFd);
290
291 release_wake_lock(WAKE_LOCK_ID);
292}
293
294InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const {
295 AutoMutex _l(mLock);
296 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700297 if (device == nullptr) return InputDeviceIdentifier();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 return device->identifier;
299}
300
301uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
302 AutoMutex _l(mLock);
303 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700304 if (device == nullptr) return 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800305 return device->classes;
306}
307
308int32_t EventHub::getDeviceControllerNumber(int32_t deviceId) const {
309 AutoMutex _l(mLock);
310 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700311 if (device == nullptr) return 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800312 return device->controllerNumber;
313}
314
315void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
316 AutoMutex _l(mLock);
317 Device* device = getDeviceLocked(deviceId);
318 if (device && device->configuration) {
319 *outConfiguration = *device->configuration;
320 } else {
321 outConfiguration->clear();
322 }
323}
324
325status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
326 RawAbsoluteAxisInfo* outAxisInfo) const {
327 outAxisInfo->clear();
328
329 if (axis >= 0 && axis <= ABS_MAX) {
330 AutoMutex _l(mLock);
331
332 Device* device = getDeviceLocked(deviceId);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700333 if (device && device->hasValidFd() && test_bit(axis, device->absBitmask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 struct input_absinfo info;
335 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
336 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100337 axis, device->identifier.name.c_str(), device->fd, errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 return -errno;
339 }
340
341 if (info.minimum != info.maximum) {
342 outAxisInfo->valid = true;
343 outAxisInfo->minValue = info.minimum;
344 outAxisInfo->maxValue = info.maximum;
345 outAxisInfo->flat = info.flat;
346 outAxisInfo->fuzz = info.fuzz;
347 outAxisInfo->resolution = info.resolution;
348 }
349 return OK;
350 }
351 }
352 return -1;
353}
354
355bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
356 if (axis >= 0 && axis <= REL_MAX) {
357 AutoMutex _l(mLock);
358
359 Device* device = getDeviceLocked(deviceId);
360 if (device) {
361 return test_bit(axis, device->relBitmask);
362 }
363 }
364 return false;
365}
366
367bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
368 if (property >= 0 && property <= INPUT_PROP_MAX) {
369 AutoMutex _l(mLock);
370
371 Device* device = getDeviceLocked(deviceId);
372 if (device) {
373 return test_bit(property, device->propBitmask);
374 }
375 }
376 return false;
377}
378
379int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
380 if (scanCode >= 0 && scanCode <= KEY_MAX) {
381 AutoMutex _l(mLock);
382
383 Device* device = getDeviceLocked(deviceId);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700384 if (device && device->hasValidFd() && test_bit(scanCode, device->keyBitmask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
386 memset(keyState, 0, sizeof(keyState));
387 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
388 return test_bit(scanCode, keyState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
389 }
390 }
391 }
392 return AKEY_STATE_UNKNOWN;
393}
394
395int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
396 AutoMutex _l(mLock);
397
398 Device* device = getDeviceLocked(deviceId);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700399 if (device && device->hasValidFd() && device->keyMap.haveKeyLayout()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400 Vector<int32_t> scanCodes;
401 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
402 if (scanCodes.size() != 0) {
403 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
404 memset(keyState, 0, sizeof(keyState));
405 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
406 for (size_t i = 0; i < scanCodes.size(); i++) {
407 int32_t sc = scanCodes.itemAt(i);
408 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, keyState)) {
409 return AKEY_STATE_DOWN;
410 }
411 }
412 return AKEY_STATE_UP;
413 }
414 }
415 }
416 return AKEY_STATE_UNKNOWN;
417}
418
419int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
420 if (sw >= 0 && sw <= SW_MAX) {
421 AutoMutex _l(mLock);
422
423 Device* device = getDeviceLocked(deviceId);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700424 if (device && device->hasValidFd() && test_bit(sw, device->swBitmask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425 uint8_t swState[sizeof_bit_array(SW_MAX + 1)];
426 memset(swState, 0, sizeof(swState));
427 if (ioctl(device->fd, EVIOCGSW(sizeof(swState)), swState) >= 0) {
428 return test_bit(sw, swState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
429 }
430 }
431 }
432 return AKEY_STATE_UNKNOWN;
433}
434
435status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
436 *outValue = 0;
437
438 if (axis >= 0 && axis <= ABS_MAX) {
439 AutoMutex _l(mLock);
440
441 Device* device = getDeviceLocked(deviceId);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700442 if (device && device->hasValidFd() && test_bit(axis, device->absBitmask)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800443 struct input_absinfo info;
444 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
445 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100446 axis, device->identifier.name.c_str(), device->fd, errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800447 return -errno;
448 }
449
450 *outValue = info.value;
451 return OK;
452 }
453 }
454 return -1;
455}
456
457bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
458 const int32_t* keyCodes, uint8_t* outFlags) const {
459 AutoMutex _l(mLock);
460
461 Device* device = getDeviceLocked(deviceId);
462 if (device && device->keyMap.haveKeyLayout()) {
463 Vector<int32_t> scanCodes;
464 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
465 scanCodes.clear();
466
467 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
468 keyCodes[codeIndex], &scanCodes);
469 if (! err) {
470 // check the possible scan codes identified by the layout map against the
471 // map of codes actually emitted by the driver
472 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
473 if (test_bit(scanCodes[sc], device->keyBitmask)) {
474 outFlags[codeIndex] = 1;
475 break;
476 }
477 }
478 }
479 }
480 return true;
481 }
482 return false;
483}
484
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700485status_t EventHub::mapKey(int32_t deviceId,
486 int32_t scanCode, int32_t usageCode, int32_t metaState,
487 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488 AutoMutex _l(mLock);
489 Device* device = getDeviceLocked(deviceId);
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700490 status_t status = NAME_NOT_FOUND;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491
492 if (device) {
493 // Check the key character map first.
494 sp<KeyCharacterMap> kcm = device->getKeyCharacterMap();
Yi Kong9b14ac62018-07-17 13:48:38 -0700495 if (kcm != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496 if (!kcm->mapKey(scanCode, usageCode, outKeycode)) {
497 *outFlags = 0;
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700498 status = NO_ERROR;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 }
500 }
501
502 // Check the key layout next.
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700503 if (status != NO_ERROR && device->keyMap.haveKeyLayout()) {
Siarhei Vishniakou592bac22018-11-08 19:42:38 -0800504 if (!device->keyMap.keyLayoutMap->mapKey(scanCode, usageCode, outKeycode, outFlags)) {
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700505 status = NO_ERROR;
506 }
507 }
508
509 if (status == NO_ERROR) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700510 if (kcm != nullptr) {
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700511 kcm->tryRemapKey(*outKeycode, metaState, outKeycode, outMetaState);
512 } else {
513 *outMetaState = metaState;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 }
515 }
516 }
517
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700518 if (status != NO_ERROR) {
519 *outKeycode = 0;
520 *outFlags = 0;
521 *outMetaState = metaState;
522 }
523
524 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525}
526
527status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const {
528 AutoMutex _l(mLock);
529 Device* device = getDeviceLocked(deviceId);
530
531 if (device && device->keyMap.haveKeyLayout()) {
532 status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo);
533 if (err == NO_ERROR) {
534 return NO_ERROR;
535 }
536 }
537
538 return NAME_NOT_FOUND;
539}
540
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100541void EventHub::setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800542 AutoMutex _l(mLock);
543
544 mExcludedDevices = devices;
545}
546
547bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
548 AutoMutex _l(mLock);
549 Device* device = getDeviceLocked(deviceId);
550 if (device && scanCode >= 0 && scanCode <= KEY_MAX) {
551 if (test_bit(scanCode, device->keyBitmask)) {
552 return true;
553 }
554 }
555 return false;
556}
557
558bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
559 AutoMutex _l(mLock);
560 Device* device = getDeviceLocked(deviceId);
561 int32_t sc;
562 if (device && mapLed(device, led, &sc) == NO_ERROR) {
563 if (test_bit(sc, device->ledBitmask)) {
564 return true;
565 }
566 }
567 return false;
568}
569
570void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
571 AutoMutex _l(mLock);
572 Device* device = getDeviceLocked(deviceId);
573 setLedStateLocked(device, led, on);
574}
575
576void EventHub::setLedStateLocked(Device* device, int32_t led, bool on) {
577 int32_t sc;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700578 if (device && device->hasValidFd() && mapLed(device, led, &sc) != NAME_NOT_FOUND) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 struct input_event ev;
580 ev.time.tv_sec = 0;
581 ev.time.tv_usec = 0;
582 ev.type = EV_LED;
583 ev.code = sc;
584 ev.value = on ? 1 : 0;
585
586 ssize_t nWrite;
587 do {
588 nWrite = write(device->fd, &ev, sizeof(struct input_event));
589 } while (nWrite == -1 && errno == EINTR);
590 }
591}
592
593void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
594 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
595 outVirtualKeys.clear();
596
597 AutoMutex _l(mLock);
598 Device* device = getDeviceLocked(deviceId);
599 if (device && device->virtualKeyMap) {
600 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
601 }
602}
603
604sp<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const {
605 AutoMutex _l(mLock);
606 Device* device = getDeviceLocked(deviceId);
607 if (device) {
608 return device->getKeyCharacterMap();
609 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700610 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611}
612
613bool EventHub::setKeyboardLayoutOverlay(int32_t deviceId,
614 const sp<KeyCharacterMap>& map) {
615 AutoMutex _l(mLock);
616 Device* device = getDeviceLocked(deviceId);
617 if (device) {
618 if (map != device->overlayKeyMap) {
619 device->overlayKeyMap = map;
620 device->combinedKeyMap = KeyCharacterMap::combine(
621 device->keyMap.keyCharacterMap, map);
622 return true;
623 }
624 }
625 return false;
626}
627
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100628static std::string generateDescriptor(InputDeviceIdentifier& identifier) {
629 std::string rawDescriptor;
630 rawDescriptor += StringPrintf(":%04x:%04x:", identifier.vendor,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800631 identifier.product);
632 // TODO add handling for USB devices to not uniqueify kbs that show up twice
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100633 if (!identifier.uniqueId.empty()) {
634 rawDescriptor += "uniqueId:";
635 rawDescriptor += identifier.uniqueId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800636 } else if (identifier.nonce != 0) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100637 rawDescriptor += StringPrintf("nonce:%04x", identifier.nonce);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 }
639
640 if (identifier.vendor == 0 && identifier.product == 0) {
641 // If we don't know the vendor and product id, then the device is probably
642 // built-in so we need to rely on other information to uniquely identify
643 // the input device. Usually we try to avoid relying on the device name or
644 // location but for built-in input device, they are unlikely to ever change.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100645 if (!identifier.name.empty()) {
646 rawDescriptor += "name:";
647 rawDescriptor += identifier.name;
648 } else if (!identifier.location.empty()) {
649 rawDescriptor += "location:";
650 rawDescriptor += identifier.location;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 }
652 }
653 identifier.descriptor = sha1(rawDescriptor);
654 return rawDescriptor;
655}
656
657void EventHub::assignDescriptorLocked(InputDeviceIdentifier& identifier) {
658 // Compute a device descriptor that uniquely identifies the device.
659 // The descriptor is assumed to be a stable identifier. Its value should not
660 // change between reboots, reconnections, firmware updates or new releases
661 // of Android. In practice we sometimes get devices that cannot be uniquely
662 // identified. In this case we enforce uniqueness between connected devices.
663 // Ideally, we also want the descriptor to be short and relatively opaque.
664
665 identifier.nonce = 0;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100666 std::string rawDescriptor = generateDescriptor(identifier);
667 if (identifier.uniqueId.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 // If it didn't have a unique id check for conflicts and enforce
669 // uniqueness if necessary.
Yi Kong9b14ac62018-07-17 13:48:38 -0700670 while(getDeviceByDescriptorLocked(identifier.descriptor) != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 identifier.nonce++;
672 rawDescriptor = generateDescriptor(identifier);
673 }
674 }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100675 ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.c_str(),
676 identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677}
678
679void EventHub::vibrate(int32_t deviceId, nsecs_t duration) {
680 AutoMutex _l(mLock);
681 Device* device = getDeviceLocked(deviceId);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700682 if (device && device->hasValidFd()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 ff_effect effect;
684 memset(&effect, 0, sizeof(effect));
685 effect.type = FF_RUMBLE;
686 effect.id = device->ffEffectId;
687 effect.u.rumble.strong_magnitude = 0xc000;
688 effect.u.rumble.weak_magnitude = 0xc000;
689 effect.replay.length = (duration + 999999LL) / 1000000LL;
690 effect.replay.delay = 0;
691 if (ioctl(device->fd, EVIOCSFF, &effect)) {
692 ALOGW("Could not upload force feedback effect to device %s due to error %d.",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100693 device->identifier.name.c_str(), errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694 return;
695 }
696 device->ffEffectId = effect.id;
697
698 struct input_event ev;
699 ev.time.tv_sec = 0;
700 ev.time.tv_usec = 0;
701 ev.type = EV_FF;
702 ev.code = device->ffEffectId;
703 ev.value = 1;
704 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
705 ALOGW("Could not start force feedback effect on device %s due to error %d.",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100706 device->identifier.name.c_str(), errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 return;
708 }
709 device->ffEffectPlaying = true;
710 }
711}
712
713void EventHub::cancelVibrate(int32_t deviceId) {
714 AutoMutex _l(mLock);
715 Device* device = getDeviceLocked(deviceId);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700716 if (device && device->hasValidFd()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717 if (device->ffEffectPlaying) {
718 device->ffEffectPlaying = false;
719
720 struct input_event ev;
721 ev.time.tv_sec = 0;
722 ev.time.tv_usec = 0;
723 ev.type = EV_FF;
724 ev.code = device->ffEffectId;
725 ev.value = 0;
726 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
727 ALOGW("Could not stop force feedback effect on device %s due to error %d.",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100728 device->identifier.name.c_str(), errno);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729 return;
730 }
731 }
732 }
733}
734
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100735EventHub::Device* EventHub::getDeviceByDescriptorLocked(const std::string& descriptor) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800736 size_t size = mDevices.size();
737 for (size_t i = 0; i < size; i++) {
738 Device* device = mDevices.valueAt(i);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100739 if (descriptor == device->identifier.descriptor) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 return device;
741 }
742 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700743 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800744}
745
746EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
747 if (deviceId == BUILT_IN_KEYBOARD_ID) {
748 deviceId = mBuiltInKeyboardId;
749 }
750 ssize_t index = mDevices.indexOfKey(deviceId);
751 return index >= 0 ? mDevices.valueAt(index) : NULL;
752}
753
754EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const {
755 for (size_t i = 0; i < mDevices.size(); i++) {
756 Device* device = mDevices.valueAt(i);
757 if (device->path == devicePath) {
758 return device;
759 }
760 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700761 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762}
763
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700764/**
765 * The file descriptor could be either input device, or a video device (associated with a
766 * specific input device). Check both cases here, and return the device that this event
767 * belongs to. Caller can compare the fd's once more to determine event type.
768 * Looks through all input devices, and only attached video devices. Unattached video
769 * devices are ignored.
770 */
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700771EventHub::Device* EventHub::getDeviceByFdLocked(int fd) const {
772 for (size_t i = 0; i < mDevices.size(); i++) {
773 Device* device = mDevices.valueAt(i);
774 if (device->fd == fd) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700775 // This is an input device event
776 return device;
777 }
778 if (device->videoDevice && device->videoDevice->getFd() == fd) {
779 // This is a video device event
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700780 return device;
781 }
782 }
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700783 // We do not check mUnattachedVideoDevices here because they should not participate in epoll,
784 // and therefore should never be looked up by fd.
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700785 return nullptr;
786}
787
Michael Wrightd02c5b62014-02-10 15:10:22 -0800788size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
789 ALOG_ASSERT(bufferSize >= 1);
790
791 AutoMutex _l(mLock);
792
793 struct input_event readBuffer[bufferSize];
794
795 RawEvent* event = buffer;
796 size_t capacity = bufferSize;
797 bool awoken = false;
798 for (;;) {
799 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
800
801 // Reopen input devices if needed.
802 if (mNeedToReopenDevices) {
803 mNeedToReopenDevices = false;
804
805 ALOGI("Reopening all input devices due to a configuration change.");
806
807 closeAllDevicesLocked();
808 mNeedToScanDevices = true;
809 break; // return to the caller before we actually rescan
810 }
811
812 // Report any devices that had last been added/removed.
813 while (mClosingDevices) {
814 Device* device = mClosingDevices;
815 ALOGV("Reporting device closed: id=%d, name=%s\n",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100816 device->id, device->path.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 mClosingDevices = device->next;
818 event->when = now;
819 event->deviceId = device->id == mBuiltInKeyboardId ? BUILT_IN_KEYBOARD_ID : device->id;
820 event->type = DEVICE_REMOVED;
821 event += 1;
822 delete device;
823 mNeedToSendFinishedDeviceScan = true;
824 if (--capacity == 0) {
825 break;
826 }
827 }
828
829 if (mNeedToScanDevices) {
830 mNeedToScanDevices = false;
831 scanDevicesLocked();
832 mNeedToSendFinishedDeviceScan = true;
833 }
834
Yi Kong9b14ac62018-07-17 13:48:38 -0700835 while (mOpeningDevices != nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 Device* device = mOpeningDevices;
837 ALOGV("Reporting device opened: id=%d, name=%s\n",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100838 device->id, device->path.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839 mOpeningDevices = device->next;
840 event->when = now;
841 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
842 event->type = DEVICE_ADDED;
843 event += 1;
844 mNeedToSendFinishedDeviceScan = true;
845 if (--capacity == 0) {
846 break;
847 }
848 }
849
850 if (mNeedToSendFinishedDeviceScan) {
851 mNeedToSendFinishedDeviceScan = false;
852 event->when = now;
853 event->type = FINISHED_DEVICE_SCAN;
854 event += 1;
855 if (--capacity == 0) {
856 break;
857 }
858 }
859
860 // Grab the next input event.
861 bool deviceChanged = false;
862 while (mPendingEventIndex < mPendingEventCount) {
863 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700864 if (eventItem.data.fd == mINotifyFd) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865 if (eventItem.events & EPOLLIN) {
866 mPendingINotify = true;
867 } else {
868 ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
869 }
870 continue;
871 }
872
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700873 if (eventItem.data.fd == mWakeReadPipeFd) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874 if (eventItem.events & EPOLLIN) {
875 ALOGV("awoken after wake()");
876 awoken = true;
877 char buffer[16];
878 ssize_t nRead;
879 do {
880 nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
881 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
882 } else {
883 ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
884 eventItem.events);
885 }
886 continue;
887 }
888
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700889 Device* device = getDeviceByFdLocked(eventItem.data.fd);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700890 if (!device) {
891 ALOGE("Received unexpected epoll event 0x%08x for unknown fd %d.",
Siarhei Vishniakou4bc561c2018-11-02 17:41:58 -0700892 eventItem.events, eventItem.data.fd);
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700893 ALOG_ASSERT(!DEBUG);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800894 continue;
895 }
Siarhei Vishniakou12598682018-11-02 17:19:19 -0700896 if (device->videoDevice && eventItem.data.fd == device->videoDevice->getFd()) {
897 if (eventItem.events & EPOLLIN) {
898 size_t numFrames = device->videoDevice->readAndQueueFrames();
899 if (numFrames == 0) {
900 ALOGE("Received epoll event for video device %s, but could not read frame",
901 device->videoDevice->getName().c_str());
902 }
903 } else if (eventItem.events & EPOLLHUP) {
904 // TODO(b/121395353) - consider adding EPOLLRDHUP
905 ALOGI("Removing video device %s due to epoll hang-up event.",
906 device->videoDevice->getName().c_str());
907 unregisterVideoDeviceFromEpollLocked(*device->videoDevice);
908 device->videoDevice = nullptr;
909 } else {
910 ALOGW("Received unexpected epoll event 0x%08x for device %s.",
911 eventItem.events, device->videoDevice->getName().c_str());
912 ALOG_ASSERT(!DEBUG);
913 }
914 continue;
915 }
916 // This must be an input event
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917 if (eventItem.events & EPOLLIN) {
918 int32_t readSize = read(device->fd, readBuffer,
919 sizeof(struct input_event) * capacity);
920 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
921 // Device was removed before INotify noticed.
Mark Salyzyn5aa26b22014-06-10 13:07:44 -0700922 ALOGW("could not get event, removed? (fd: %d size: %" PRId32
923 " bufferSize: %zu capacity: %zu errno: %d)\n",
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924 device->fd, readSize, bufferSize, capacity, errno);
925 deviceChanged = true;
926 closeDeviceLocked(device);
927 } else if (readSize < 0) {
928 if (errno != EAGAIN && errno != EINTR) {
929 ALOGW("could not get event (errno=%d)", errno);
930 }
931 } else if ((readSize % sizeof(struct input_event)) != 0) {
932 ALOGE("could not get event (wrong size: %d)", readSize);
933 } else {
934 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
935
936 size_t count = size_t(readSize) / sizeof(struct input_event);
937 for (size_t i = 0; i < count; i++) {
938 struct input_event& iev = readBuffer[i];
Siarhei Vishniakou592bac22018-11-08 19:42:38 -0800939 event->when = processEventTimestamp(iev);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 event->deviceId = deviceId;
941 event->type = iev.type;
942 event->code = iev.code;
943 event->value = iev.value;
944 event += 1;
945 capacity -= 1;
946 }
947 if (capacity == 0) {
948 // The result buffer is full. Reset the pending event index
949 // so we will try to read the device again on the next iteration.
950 mPendingEventIndex -= 1;
951 break;
952 }
953 }
954 } else if (eventItem.events & EPOLLHUP) {
955 ALOGI("Removing device %s due to epoll hang-up event.",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100956 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800957 deviceChanged = true;
958 closeDeviceLocked(device);
959 } else {
960 ALOGW("Received unexpected epoll event 0x%08x for device %s.",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100961 eventItem.events, device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800962 }
963 }
964
965 // readNotify() will modify the list of devices so this must be done after
966 // processing all other events to ensure that we read all remaining events
967 // before closing the devices.
968 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
969 mPendingINotify = false;
970 readNotifyLocked();
971 deviceChanged = true;
972 }
973
974 // Report added or removed devices immediately.
975 if (deviceChanged) {
976 continue;
977 }
978
979 // Return now if we have collected any events or if we were explicitly awoken.
980 if (event != buffer || awoken) {
981 break;
982 }
983
984 // Poll for events. Mind the wake lock dance!
985 // We hold a wake lock at all times except during epoll_wait(). This works due to some
986 // subtle choreography. When a device driver has pending (unread) events, it acquires
987 // a kernel wake lock. However, once the last pending event has been read, the device
988 // driver will release the kernel wake lock. To prevent the system from going to sleep
989 // when this happens, the EventHub holds onto its own user wake lock while the client
990 // is processing events. Thus the system can only sleep if there are no events
991 // pending or currently being processed.
992 //
993 // The timeout is advisory only. If the device is asleep, it will not wake just to
994 // service the timeout.
995 mPendingEventIndex = 0;
996
997 mLock.unlock(); // release lock before poll, must be before release_wake_lock
998 release_wake_lock(WAKE_LOCK_ID);
999
1000 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
1001
1002 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
1003 mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock
1004
1005 if (pollResult == 0) {
1006 // Timed out.
1007 mPendingEventCount = 0;
1008 break;
1009 }
1010
1011 if (pollResult < 0) {
1012 // An error occurred.
1013 mPendingEventCount = 0;
1014
1015 // Sleep after errors to avoid locking up the system.
1016 // Hopefully the error is transient.
1017 if (errno != EINTR) {
1018 ALOGW("poll failed (errno=%d)\n", errno);
1019 usleep(100000);
1020 }
1021 } else {
1022 // Some events occurred.
1023 mPendingEventCount = size_t(pollResult);
1024 }
1025 }
1026
1027 // All done, return the number of events we read.
1028 return event - buffer;
1029}
1030
1031void EventHub::wake() {
1032 ALOGV("wake() called");
1033
1034 ssize_t nWrite;
1035 do {
1036 nWrite = write(mWakeWritePipeFd, "W", 1);
1037 } while (nWrite == -1 && errno == EINTR);
1038
1039 if (nWrite != 1 && errno != EAGAIN) {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001040 ALOGW("Could not write wake signal: %s", strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001041 }
1042}
1043
1044void EventHub::scanDevicesLocked() {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001045 status_t result = scanDirLocked(DEVICE_PATH);
1046 if(result < 0) {
1047 ALOGE("scan dir failed for %s", DEVICE_PATH);
1048 }
1049 result = scanVideoDirLocked(VIDEO_DEVICE_PATH);
1050 if (result != OK) {
1051 ALOGE("scan video dir failed for %s", VIDEO_DEVICE_PATH);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052 }
1053 if (mDevices.indexOfKey(VIRTUAL_KEYBOARD_ID) < 0) {
1054 createVirtualKeyboardLocked();
1055 }
1056}
1057
1058// ----------------------------------------------------------------------------
1059
1060static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
1061 const uint8_t* end = array + endIndex;
1062 array += startIndex;
1063 while (array != end) {
1064 if (*(array++) != 0) {
1065 return true;
1066 }
1067 }
1068 return false;
1069}
1070
1071static const int32_t GAMEPAD_KEYCODES[] = {
1072 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
1073 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
1074 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
1075 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
1076 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
1077 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078};
1079
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001080status_t EventHub::registerFdForEpoll(int fd) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001081 // TODO(b/121395353) - consider adding EPOLLRDHUP
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001082 struct epoll_event eventItem = {};
1083 eventItem.events = EPOLLIN | EPOLLWAKEUP;
1084 eventItem.data.fd = fd;
1085 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
1086 ALOGE("Could not add fd to epoll instance: %s", strerror(errno));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001087 return -errno;
1088 }
1089 return OK;
1090}
1091
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001092status_t EventHub::unregisterFdFromEpoll(int fd) {
1093 if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, nullptr)) {
1094 ALOGW("Could not remove fd from epoll instance: %s", strerror(errno));
1095 return -errno;
1096 }
1097 return OK;
1098}
1099
1100status_t EventHub::registerDeviceForEpollLocked(Device* device) {
1101 if (device == nullptr) {
1102 if (DEBUG) {
1103 LOG_ALWAYS_FATAL("Cannot call registerDeviceForEpollLocked with null Device");
1104 }
1105 return BAD_VALUE;
1106 }
1107 status_t result = registerFdForEpoll(device->fd);
1108 if (result != OK) {
1109 ALOGE("Could not add input device fd to epoll for device %" PRId32, device->id);
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001110 return result;
1111 }
1112 if (device->videoDevice) {
1113 registerVideoDeviceForEpollLocked(*device->videoDevice);
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001114 }
1115 return result;
1116}
1117
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001118void EventHub::registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) {
1119 status_t result = registerFdForEpoll(videoDevice.getFd());
1120 if (result != OK) {
1121 ALOGE("Could not add video device %s to epoll", videoDevice.getName().c_str());
1122 }
1123}
1124
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001125status_t EventHub::unregisterDeviceFromEpollLocked(Device* device) {
1126 if (device->hasValidFd()) {
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001127 status_t result = unregisterFdFromEpoll(device->fd);
1128 if (result != OK) {
1129 ALOGW("Could not remove input device fd from epoll for device %" PRId32, device->id);
1130 return result;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001131 }
1132 }
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001133 if (device->videoDevice) {
1134 unregisterVideoDeviceFromEpollLocked(*device->videoDevice);
1135 }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001136 return OK;
1137}
1138
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001139void EventHub::unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) {
1140 if (videoDevice.hasValidFd()) {
1141 status_t result = unregisterFdFromEpoll(videoDevice.getFd());
1142 if (result != OK) {
1143 ALOGW("Could not remove video device fd from epoll for device: %s",
1144 videoDevice.getName().c_str());
1145 }
1146 }
1147}
1148
1149status_t EventHub::openDeviceLocked(const char* devicePath) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150 char buffer[80];
1151
1152 ALOGV("Opening device: %s", devicePath);
1153
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001154 int fd = open(devicePath, O_RDWR | O_CLOEXEC | O_NONBLOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001155 if(fd < 0) {
1156 ALOGE("could not open %s, %s\n", devicePath, strerror(errno));
1157 return -1;
1158 }
1159
1160 InputDeviceIdentifier identifier;
1161
1162 // Get device name.
1163 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
Siarhei Vishniakou25920312018-12-12 15:24:44 -08001164 ALOGE("Could not get device name for %s: %s", devicePath, strerror(errno));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001165 } else {
1166 buffer[sizeof(buffer) - 1] = '\0';
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001167 identifier.name = buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 }
1169
1170 // Check to see if the device is on our excluded list
1171 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001172 const std::string& item = mExcludedDevices[i];
Michael Wrightd02c5b62014-02-10 15:10:22 -08001173 if (identifier.name == item) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001174 ALOGI("ignoring event id %s driver %s\n", devicePath, item.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175 close(fd);
1176 return -1;
1177 }
1178 }
1179
1180 // Get device driver version.
1181 int driverVersion;
1182 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
1183 ALOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
1184 close(fd);
1185 return -1;
1186 }
1187
1188 // Get device identifier.
1189 struct input_id inputId;
1190 if(ioctl(fd, EVIOCGID, &inputId)) {
1191 ALOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
1192 close(fd);
1193 return -1;
1194 }
1195 identifier.bus = inputId.bustype;
1196 identifier.product = inputId.product;
1197 identifier.vendor = inputId.vendor;
1198 identifier.version = inputId.version;
1199
1200 // Get device physical location.
1201 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
1202 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
1203 } else {
1204 buffer[sizeof(buffer) - 1] = '\0';
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001205 identifier.location = buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001206 }
1207
1208 // Get device unique id.
1209 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
1210 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
1211 } else {
1212 buffer[sizeof(buffer) - 1] = '\0';
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001213 identifier.uniqueId = buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001214 }
1215
1216 // Fill in the descriptor.
1217 assignDescriptorLocked(identifier);
1218
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219 // Allocate device. (The device object takes ownership of the fd at this point.)
1220 int32_t deviceId = mNextDeviceId++;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001221 Device* device = new Device(fd, deviceId, devicePath, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222
1223 ALOGV("add device %d: %s\n", deviceId, devicePath);
1224 ALOGV(" bus: %04x\n"
1225 " vendor %04x\n"
1226 " product %04x\n"
1227 " version %04x\n",
1228 identifier.bus, identifier.vendor, identifier.product, identifier.version);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001229 ALOGV(" name: \"%s\"\n", identifier.name.c_str());
1230 ALOGV(" location: \"%s\"\n", identifier.location.c_str());
1231 ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.c_str());
1232 ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001233 ALOGV(" driver: v%d.%d.%d\n",
1234 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
1235
1236 // Load the configuration file for the device.
1237 loadConfigurationLocked(device);
1238
1239 // Figure out the kinds of events the device reports.
1240 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask);
1241 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask);
1242 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask);
1243 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask);
1244 ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask);
1245 ioctl(fd, EVIOCGBIT(EV_FF, sizeof(device->ffBitmask)), device->ffBitmask);
1246 ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask);
1247
1248 // See if this is a keyboard. Ignore everything in the button range except for
1249 // joystick and gamepad buttons which are handled like keyboards for the most part.
1250 bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC))
1251 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK),
1252 sizeof_bit_array(KEY_MAX + 1));
1253 bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC),
1254 sizeof_bit_array(BTN_MOUSE))
1255 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK),
1256 sizeof_bit_array(BTN_DIGI));
1257 if (haveKeyboardKeys || haveGamepadButtons) {
1258 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
1259 }
1260
1261 // See if this is a cursor device such as a trackball or mouse.
1262 if (test_bit(BTN_MOUSE, device->keyBitmask)
1263 && test_bit(REL_X, device->relBitmask)
1264 && test_bit(REL_Y, device->relBitmask)) {
1265 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
1266 }
1267
Prashant Malani1941ff52015-08-11 18:29:28 -07001268 // See if this is a rotary encoder type device.
1269 String8 deviceType = String8();
1270 if (device->configuration &&
1271 device->configuration->tryGetProperty(String8("device.type"), deviceType)) {
1272 if (!deviceType.compare(String8("rotaryEncoder"))) {
1273 device->classes |= INPUT_DEVICE_CLASS_ROTARY_ENCODER;
1274 }
1275 }
1276
Michael Wrightd02c5b62014-02-10 15:10:22 -08001277 // See if this is a touch pad.
1278 // Is this a new modern multi-touch driver?
1279 if (test_bit(ABS_MT_POSITION_X, device->absBitmask)
1280 && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) {
1281 // Some joysticks such as the PS3 controller report axes that conflict
1282 // with the ABS_MT range. Try to confirm that the device really is
1283 // a touch screen.
1284 if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) {
1285 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
1286 }
1287 // Is this an old style single-touch driver?
1288 } else if (test_bit(BTN_TOUCH, device->keyBitmask)
1289 && test_bit(ABS_X, device->absBitmask)
1290 && test_bit(ABS_Y, device->absBitmask)) {
1291 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
Michael Wright842500e2015-03-13 17:32:02 -07001292 // Is this a BT stylus?
1293 } else if ((test_bit(ABS_PRESSURE, device->absBitmask) ||
1294 test_bit(BTN_TOUCH, device->keyBitmask))
1295 && !test_bit(ABS_X, device->absBitmask)
1296 && !test_bit(ABS_Y, device->absBitmask)) {
1297 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL_STYLUS;
1298 // Keyboard will try to claim some of the buttons but we really want to reserve those so we
1299 // can fuse it with the touch screen data, so just take them back. Note this means an
1300 // external stylus cannot also be a keyboard device.
1301 device->classes &= ~INPUT_DEVICE_CLASS_KEYBOARD;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001302 }
1303
1304 // See if this device is a joystick.
1305 // Assumes that joysticks always have gamepad buttons in order to distinguish them
1306 // from other devices such as accelerometers that also have absolute axes.
1307 if (haveGamepadButtons) {
1308 uint32_t assumedClasses = device->classes | INPUT_DEVICE_CLASS_JOYSTICK;
1309 for (int i = 0; i <= ABS_MAX; i++) {
1310 if (test_bit(i, device->absBitmask)
1311 && (getAbsAxisUsage(i, assumedClasses) & INPUT_DEVICE_CLASS_JOYSTICK)) {
1312 device->classes = assumedClasses;
1313 break;
1314 }
1315 }
1316 }
1317
1318 // Check whether this device has switches.
1319 for (int i = 0; i <= SW_MAX; i++) {
1320 if (test_bit(i, device->swBitmask)) {
1321 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
1322 break;
1323 }
1324 }
1325
1326 // Check whether this device supports the vibrator.
1327 if (test_bit(FF_RUMBLE, device->ffBitmask)) {
1328 device->classes |= INPUT_DEVICE_CLASS_VIBRATOR;
1329 }
1330
1331 // Configure virtual keys.
1332 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
1333 // Load the virtual keys for the touch screen, if any.
1334 // We do this now so that we can make sure to load the keymap if necessary.
1335 status_t status = loadVirtualKeyMapLocked(device);
1336 if (!status) {
1337 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
1338 }
1339 }
1340
1341 // Load the key map.
1342 // We need to do this for joysticks too because the key layout may specify axes.
1343 status_t keyMapStatus = NAME_NOT_FOUND;
1344 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
1345 // Load the keymap for the device.
1346 keyMapStatus = loadKeyMapLocked(device);
1347 }
1348
1349 // Configure the keyboard, gamepad or virtual keyboard.
1350 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
1351 // Register the keyboard as a built-in keyboard if it is eligible.
1352 if (!keyMapStatus
1353 && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD
1354 && isEligibleBuiltInKeyboard(device->identifier,
1355 device->configuration, &device->keyMap)) {
1356 mBuiltInKeyboardId = device->id;
1357 }
1358
1359 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
1360 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
1361 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
1362 }
1363
1364 // See if this device has a DPAD.
1365 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
1366 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
1367 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
1368 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
1369 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
1370 device->classes |= INPUT_DEVICE_CLASS_DPAD;
1371 }
1372
1373 // See if this device has a gamepad.
1374 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
1375 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
1376 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
1377 break;
1378 }
1379 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001380 }
1381
1382 // If the device isn't recognized as something we handle, don't monitor it.
1383 if (device->classes == 0) {
1384 ALOGV("Dropping device: id=%d, path='%s', name='%s'",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001385 deviceId, devicePath, device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 delete device;
1387 return -1;
1388 }
1389
Tim Kilbourn063ff532015-04-08 10:26:18 -07001390 // Determine whether the device has a mic.
1391 if (deviceHasMicLocked(device)) {
1392 device->classes |= INPUT_DEVICE_CLASS_MIC;
1393 }
1394
Michael Wrightd02c5b62014-02-10 15:10:22 -08001395 // Determine whether the device is external or internal.
1396 if (isExternalDeviceLocked(device)) {
1397 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1398 }
1399
Michael Wright42f2c6a2014-03-12 10:33:03 -07001400 if (device->classes & (INPUT_DEVICE_CLASS_JOYSTICK | INPUT_DEVICE_CLASS_DPAD)
1401 && device->classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001402 device->controllerNumber = getNextControllerNumberLocked(device);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001403 setLedForControllerLocked(device);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001404 }
1405
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001406 // Find a matching video device by comparing device names
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001407 // This should be done before registerDeviceForEpollLocked, so that both fds are added to epoll
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001408 for (std::unique_ptr<TouchVideoDevice>& videoDevice : mUnattachedVideoDevices) {
1409 if (device->identifier.name == videoDevice->getName()) {
1410 device->videoDevice = std::move(videoDevice);
1411 break;
1412 }
1413 }
1414 mUnattachedVideoDevices.erase(std::remove_if(mUnattachedVideoDevices.begin(),
1415 mUnattachedVideoDevices.end(),
1416 [](const std::unique_ptr<TouchVideoDevice>& videoDevice){
1417 return videoDevice == nullptr; }), mUnattachedVideoDevices.end());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001418
1419 if (registerDeviceForEpollLocked(device) != OK) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001420 delete device;
1421 return -1;
1422 }
1423
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001424 configureFd(device);
1425
1426 ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
1427 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, ",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001428 deviceId, fd, devicePath, device->identifier.name.c_str(),
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001429 device->classes,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001430 device->configurationFile.c_str(),
1431 device->keyMap.keyLayoutFile.c_str(),
1432 device->keyMap.keyCharacterMapFile.c_str(),
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001433 toString(mBuiltInKeyboardId == deviceId));
1434
1435 addDeviceLocked(device);
1436 return OK;
1437}
1438
1439void EventHub::configureFd(Device* device) {
1440 // Set fd parameters with ioctl, such as key repeat, suspend block, and clock type
1441 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
1442 // Disable kernel key repeat since we handle it ourselves
1443 unsigned int repeatRate[] = {0, 0};
1444 if (ioctl(device->fd, EVIOCSREP, repeatRate)) {
1445 ALOGW("Unable to disable kernel key repeat for %s: %s",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001446 device->path.c_str(), strerror(errno));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001447 }
1448 }
1449
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001450 std::string wakeMechanism = "EPOLLWAKEUP";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001451 if (!mUsingEpollWakeup) {
1452#ifndef EVIOCSSUSPENDBLOCK
1453 // uapi headers don't include EVIOCSSUSPENDBLOCK, and future kernels
1454 // will use an epoll flag instead, so as long as we want to support
1455 // this feature, we need to be prepared to define the ioctl ourselves.
1456#define EVIOCSSUSPENDBLOCK _IOW('E', 0x91, int)
1457#endif
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001458 if (ioctl(device->fd, EVIOCSSUSPENDBLOCK, 1)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001459 wakeMechanism = "<none>";
1460 } else {
1461 wakeMechanism = "EVIOCSSUSPENDBLOCK";
1462 }
1463 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001464 // Tell the kernel that we want to use the monotonic clock for reporting timestamps
1465 // associated with input events. This is important because the input system
1466 // uses the timestamps extensively and assumes they were recorded using the monotonic
1467 // clock.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001468 int clockId = CLOCK_MONOTONIC;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001469 bool usingClockIoctl = !ioctl(device->fd, EVIOCSCLOCKID, &clockId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001470 ALOGI("wakeMechanism=%s, usingClockIoctl=%s", wakeMechanism.c_str(),
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001471 toString(usingClockIoctl));
1472}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001473
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001474void EventHub::openVideoDeviceLocked(const std::string& devicePath) {
1475 std::unique_ptr<TouchVideoDevice> videoDevice = TouchVideoDevice::create(devicePath);
1476 if (!videoDevice) {
1477 ALOGE("Could not create touch video device for %s. Ignoring", devicePath.c_str());
1478 return;
1479 }
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001480 // Transfer ownership of this video device to a matching input device
1481 for (size_t i = 0; i < mDevices.size(); i++) {
1482 Device* device = mDevices.valueAt(i);
1483 if (videoDevice->getName() == device->identifier.name) {
1484 device->videoDevice = std::move(videoDevice);
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001485 if (device->enabled) {
1486 registerVideoDeviceForEpollLocked(*device->videoDevice);
1487 }
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001488 return;
1489 }
1490 }
1491
1492 // Couldn't find a matching input device, so just add it to a temporary holding queue.
1493 // A matching input device may appear later.
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001494 ALOGI("Adding video device %s to list of unattached video devices",
1495 videoDevice->getName().c_str());
1496 mUnattachedVideoDevices.push_back(std::move(videoDevice));
1497}
1498
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001499bool EventHub::isDeviceEnabled(int32_t deviceId) {
1500 AutoMutex _l(mLock);
1501 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -07001502 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001503 ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
1504 return false;
1505 }
1506 return device->enabled;
1507}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001508
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001509status_t EventHub::enableDevice(int32_t deviceId) {
1510 AutoMutex _l(mLock);
1511 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -07001512 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001513 ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
1514 return BAD_VALUE;
1515 }
1516 if (device->enabled) {
1517 ALOGW("Duplicate call to %s, input device %" PRId32 " already enabled", __func__, deviceId);
1518 return OK;
1519 }
1520 status_t result = device->enable();
1521 if (result != OK) {
1522 ALOGE("Failed to enable device %" PRId32, deviceId);
1523 return result;
1524 }
1525
1526 configureFd(device);
1527
1528 return registerDeviceForEpollLocked(device);
1529}
1530
1531status_t EventHub::disableDevice(int32_t deviceId) {
1532 AutoMutex _l(mLock);
1533 Device* device = getDeviceLocked(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -07001534 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001535 ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
1536 return BAD_VALUE;
1537 }
1538 if (!device->enabled) {
1539 ALOGW("Duplicate call to %s, input device already disabled", __func__);
1540 return OK;
1541 }
1542 unregisterDeviceFromEpollLocked(device);
1543 return device->disable();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001544}
1545
1546void EventHub::createVirtualKeyboardLocked() {
1547 InputDeviceIdentifier identifier;
1548 identifier.name = "Virtual";
1549 identifier.uniqueId = "<virtual>";
1550 assignDescriptorLocked(identifier);
1551
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001552 Device* device = new Device(-1, VIRTUAL_KEYBOARD_ID, "<virtual>", identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001553 device->classes = INPUT_DEVICE_CLASS_KEYBOARD
1554 | INPUT_DEVICE_CLASS_ALPHAKEY
1555 | INPUT_DEVICE_CLASS_DPAD
1556 | INPUT_DEVICE_CLASS_VIRTUAL;
1557 loadKeyMapLocked(device);
1558 addDeviceLocked(device);
1559}
1560
1561void EventHub::addDeviceLocked(Device* device) {
1562 mDevices.add(device->id, device);
1563 device->next = mOpeningDevices;
1564 mOpeningDevices = device;
1565}
1566
1567void EventHub::loadConfigurationLocked(Device* device) {
1568 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1569 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001570 if (device->configurationFile.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571 ALOGD("No input device configuration file found for device '%s'.",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001572 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001573 } else {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001574 status_t status = PropertyMap::load(String8(device->configurationFile.c_str()),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001575 &device->configuration);
1576 if (status) {
1577 ALOGE("Error loading input device configuration file for device '%s'. "
1578 "Using default configuration.",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001579 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001580 }
1581 }
1582}
1583
1584status_t EventHub::loadVirtualKeyMapLocked(Device* device) {
1585 // The virtual key map is supplied by the kernel as a system board property file.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001586 std::string path;
1587 path += "/sys/board_properties/virtualkeys.";
1588 path += device->identifier.name;
1589 if (access(path.c_str(), R_OK)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 return NAME_NOT_FOUND;
1591 }
1592 return VirtualKeyMap::load(path, &device->virtualKeyMap);
1593}
1594
1595status_t EventHub::loadKeyMapLocked(Device* device) {
1596 return device->keyMap.load(device->identifier, device->configuration);
1597}
1598
1599bool EventHub::isExternalDeviceLocked(Device* device) {
1600 if (device->configuration) {
1601 bool value;
1602 if (device->configuration->tryGetProperty(String8("device.internal"), value)) {
1603 return !value;
1604 }
1605 }
1606 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1607}
1608
Tim Kilbourn063ff532015-04-08 10:26:18 -07001609bool EventHub::deviceHasMicLocked(Device* device) {
1610 if (device->configuration) {
1611 bool value;
1612 if (device->configuration->tryGetProperty(String8("audio.mic"), value)) {
1613 return value;
1614 }
1615 }
1616 return false;
1617}
1618
Michael Wrightd02c5b62014-02-10 15:10:22 -08001619int32_t EventHub::getNextControllerNumberLocked(Device* device) {
1620 if (mControllerNumbers.isFull()) {
1621 ALOGI("Maximum number of controllers reached, assigning controller number 0 to device %s",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001622 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001623 return 0;
1624 }
1625 // Since the controller number 0 is reserved for non-controllers, translate all numbers up by
1626 // one
1627 return static_cast<int32_t>(mControllerNumbers.markFirstUnmarkedBit() + 1);
1628}
1629
1630void EventHub::releaseControllerNumberLocked(Device* device) {
1631 int32_t num = device->controllerNumber;
1632 device->controllerNumber= 0;
1633 if (num == 0) {
1634 return;
1635 }
1636 mControllerNumbers.clearBit(static_cast<uint32_t>(num - 1));
1637}
1638
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001639void EventHub::setLedForControllerLocked(Device* device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640 for (int i = 0; i < MAX_CONTROLLER_LEDS; i++) {
1641 setLedStateLocked(device, ALED_CONTROLLER_1 + i, device->controllerNumber == i + 1);
1642 }
1643}
1644
1645bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
Bernhard Rosenkränzer6183eb72014-11-17 21:09:14 +01001646 if (!device->keyMap.haveKeyLayout()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001647 return false;
1648 }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001649
Michael Wrightd02c5b62014-02-10 15:10:22 -08001650 Vector<int32_t> scanCodes;
1651 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
1652 const size_t N = scanCodes.size();
1653 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1654 int32_t sc = scanCodes.itemAt(i);
1655 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1656 return true;
1657 }
1658 }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001659
Michael Wrightd02c5b62014-02-10 15:10:22 -08001660 return false;
1661}
1662
1663status_t EventHub::mapLed(Device* device, int32_t led, int32_t* outScanCode) const {
Bernhard Rosenkränzer6183eb72014-11-17 21:09:14 +01001664 if (!device->keyMap.haveKeyLayout()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001665 return NAME_NOT_FOUND;
1666 }
1667
1668 int32_t scanCode;
1669 if(device->keyMap.keyLayoutMap->findScanCodeForLed(led, &scanCode) != NAME_NOT_FOUND) {
1670 if(scanCode >= 0 && scanCode <= LED_MAX && test_bit(scanCode, device->ledBitmask)) {
1671 *outScanCode = scanCode;
1672 return NO_ERROR;
1673 }
1674 }
1675 return NAME_NOT_FOUND;
1676}
1677
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001678void EventHub::closeDeviceByPathLocked(const char *devicePath) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679 Device* device = getDeviceByPathLocked(devicePath);
1680 if (device) {
1681 closeDeviceLocked(device);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001682 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001683 }
1684 ALOGV("Remove device: %s not found, device may already have been removed.", devicePath);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001685}
1686
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001687/**
1688 * Find the video device by filename, and close it.
1689 * The video device is closed by path during an inotify event, where we don't have the
1690 * additional context about the video device fd, or the associated input device.
1691 */
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001692void EventHub::closeVideoDeviceByPathLocked(const std::string& devicePath) {
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001693 // A video device may be owned by an existing input device, or it may be stored in
1694 // the mUnattachedVideoDevices queue. Check both locations.
1695 for (size_t i = 0; i < mDevices.size(); i++) {
1696 Device* device = mDevices.valueAt(i);
1697 if (device->videoDevice && device->videoDevice->getPath() == devicePath) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001698 unregisterVideoDeviceFromEpollLocked(*device->videoDevice);
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001699 device->videoDevice = nullptr;
1700 return;
1701 }
1702 }
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001703 mUnattachedVideoDevices.erase(std::remove_if(mUnattachedVideoDevices.begin(),
1704 mUnattachedVideoDevices.end(), [&devicePath](
1705 const std::unique_ptr<TouchVideoDevice>& videoDevice) {
1706 return videoDevice->getPath() == devicePath; }), mUnattachedVideoDevices.end());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001707}
1708
1709void EventHub::closeAllDevicesLocked() {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001710 mUnattachedVideoDevices.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001711 while (mDevices.size() > 0) {
1712 closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1));
1713 }
1714}
1715
1716void EventHub::closeDeviceLocked(Device* device) {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001717 ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001718 device->path.c_str(), device->identifier.name.c_str(), device->id,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001719 device->fd, device->classes);
1720
1721 if (device->id == mBuiltInKeyboardId) {
1722 ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001723 device->path.c_str(), mBuiltInKeyboardId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001724 mBuiltInKeyboardId = NO_BUILT_IN_KEYBOARD;
1725 }
1726
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001727 unregisterDeviceFromEpollLocked(device);
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001728 if (device->videoDevice) {
Siarhei Vishniakou12598682018-11-02 17:19:19 -07001729 // This must be done after the video device is removed from epoll
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001730 mUnattachedVideoDevices.push_back(std::move(device->videoDevice));
1731 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001732
1733 releaseControllerNumberLocked(device);
1734
1735 mDevices.removeItem(device->id);
1736 device->close();
1737
1738 // Unlink for opening devices list if it is present.
Yi Kong9b14ac62018-07-17 13:48:38 -07001739 Device* pred = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001740 bool found = false;
Yi Kong9b14ac62018-07-17 13:48:38 -07001741 for (Device* entry = mOpeningDevices; entry != nullptr; ) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001742 if (entry == device) {
1743 found = true;
1744 break;
1745 }
1746 pred = entry;
1747 entry = entry->next;
1748 }
1749 if (found) {
1750 // Unlink the device from the opening devices list then delete it.
1751 // We don't need to tell the client that the device was closed because
1752 // it does not even know it was opened in the first place.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001753 ALOGI("Device %s was immediately closed after opening.", device->path.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001754 if (pred) {
1755 pred->next = device->next;
1756 } else {
1757 mOpeningDevices = device->next;
1758 }
1759 delete device;
1760 } else {
1761 // Link into closing devices list.
1762 // The device will be deleted later after we have informed the client.
1763 device->next = mClosingDevices;
1764 mClosingDevices = device;
1765 }
1766}
1767
1768status_t EventHub::readNotifyLocked() {
1769 int res;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001770 char event_buf[512];
1771 int event_size;
1772 int event_pos = 0;
1773 struct inotify_event *event;
1774
1775 ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
1776 res = read(mINotifyFd, event_buf, sizeof(event_buf));
1777 if(res < (int)sizeof(*event)) {
1778 if(errno == EINTR)
1779 return 0;
1780 ALOGW("could not get event, %s\n", strerror(errno));
1781 return -1;
1782 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001783
1784 while(res >= (int)sizeof(*event)) {
1785 event = (struct inotify_event *)(event_buf + event_pos);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001786 if(event->len) {
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001787 if (event->wd == mInputWd) {
1788 std::string filename = StringPrintf("%s/%s", DEVICE_PATH, event->name);
1789 if(event->mask & IN_CREATE) {
1790 openDeviceLocked(filename.c_str());
1791 } else {
1792 ALOGI("Removing device '%s' due to inotify event\n", filename.c_str());
1793 closeDeviceByPathLocked(filename.c_str());
1794 }
1795 }
1796 else if (event->wd == mVideoWd) {
1797 if (isV4lTouchNode(event->name)) {
1798 std::string filename = StringPrintf("%s/%s", VIDEO_DEVICE_PATH, event->name);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001799 if (event->mask & IN_CREATE) {
1800 openVideoDeviceLocked(filename);
1801 } else {
1802 ALOGI("Removing video device '%s' due to inotify event", filename.c_str());
1803 closeVideoDeviceByPathLocked(filename);
1804 }
Siarhei Vishniakou951f3622018-12-12 19:45:42 -08001805 }
1806 }
1807 else {
1808 LOG_ALWAYS_FATAL("Unexpected inotify event, wd = %i", event->wd);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809 }
1810 }
1811 event_size = sizeof(*event) + event->len;
1812 res -= event_size;
1813 event_pos += event_size;
1814 }
1815 return 0;
1816}
1817
1818status_t EventHub::scanDirLocked(const char *dirname)
1819{
1820 char devname[PATH_MAX];
1821 char *filename;
1822 DIR *dir;
1823 struct dirent *de;
1824 dir = opendir(dirname);
Yi Kong9b14ac62018-07-17 13:48:38 -07001825 if(dir == nullptr)
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826 return -1;
1827 strcpy(devname, dirname);
1828 filename = devname + strlen(devname);
1829 *filename++ = '/';
1830 while((de = readdir(dir))) {
1831 if(de->d_name[0] == '.' &&
1832 (de->d_name[1] == '\0' ||
1833 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1834 continue;
1835 strcpy(filename, de->d_name);
1836 openDeviceLocked(devname);
1837 }
1838 closedir(dir);
1839 return 0;
1840}
1841
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001842/**
1843 * Look for all dirname/v4l-touch* devices, and open them.
1844 */
1845status_t EventHub::scanVideoDirLocked(const std::string& dirname)
1846{
1847 DIR* dir;
1848 struct dirent* de;
1849 dir = opendir(dirname.c_str());
1850 if(!dir) {
1851 ALOGE("Could not open video directory %s", dirname.c_str());
1852 return BAD_VALUE;
1853 }
1854
1855 while((de = readdir(dir))) {
1856 const char* name = de->d_name;
1857 if (isV4lTouchNode(name)) {
1858 ALOGI("Found touch video device %s", name);
1859 openVideoDeviceLocked(dirname + "/" + name);
1860 }
1861 }
1862 closedir(dir);
1863 return OK;
1864}
1865
Michael Wrightd02c5b62014-02-10 15:10:22 -08001866void EventHub::requestReopenDevices() {
1867 ALOGV("requestReopenDevices() called");
1868
1869 AutoMutex _l(mLock);
1870 mNeedToReopenDevices = true;
1871}
1872
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001873void EventHub::dump(std::string& dump) {
1874 dump += "Event Hub State:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001875
1876 { // acquire lock
1877 AutoMutex _l(mLock);
1878
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001879 dump += StringPrintf(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001880
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001881 dump += INDENT "Devices:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001882
1883 for (size_t i = 0; i < mDevices.size(); i++) {
1884 const Device* device = mDevices.valueAt(i);
1885 if (mBuiltInKeyboardId == device->id) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001886 dump += StringPrintf(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001887 device->id, device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001888 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001889 dump += StringPrintf(INDENT2 "%d: %s\n", device->id,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001890 device->identifier.name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001891 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001892 dump += StringPrintf(INDENT3 "Classes: 0x%08x\n", device->classes);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001893 dump += StringPrintf(INDENT3 "Path: %s\n", device->path.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001894 dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(device->enabled));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001895 dump += StringPrintf(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.c_str());
1896 dump += StringPrintf(INDENT3 "Location: %s\n", device->identifier.location.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001897 dump += StringPrintf(INDENT3 "ControllerNumber: %d\n", device->controllerNumber);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001898 dump += StringPrintf(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001899 dump += StringPrintf(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
Michael Wrightd02c5b62014-02-10 15:10:22 -08001900 "product=0x%04x, version=0x%04x\n",
1901 device->identifier.bus, device->identifier.vendor,
1902 device->identifier.product, device->identifier.version);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001903 dump += StringPrintf(INDENT3 "KeyLayoutFile: %s\n",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001904 device->keyMap.keyLayoutFile.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001905 dump += StringPrintf(INDENT3 "KeyCharacterMapFile: %s\n",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001906 device->keyMap.keyCharacterMapFile.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001907 dump += StringPrintf(INDENT3 "ConfigurationFile: %s\n",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001908 device->configurationFile.c_str());
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001909 dump += StringPrintf(INDENT3 "HaveKeyboardLayoutOverlay: %s\n",
Yi Kong9b14ac62018-07-17 13:48:38 -07001910 toString(device->overlayKeyMap != nullptr));
Siarhei Vishniakouec7854a2018-12-14 16:52:34 -08001911 dump += INDENT3 "VideoDevice: ";
1912 if (device->videoDevice) {
1913 dump += device->videoDevice->dump() + "\n";
1914 } else {
1915 dump += "<none>\n";
1916 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001917 }
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001918
1919 dump += INDENT "Unattached video devices:\n";
1920 for (const std::unique_ptr<TouchVideoDevice>& videoDevice : mUnattachedVideoDevices) {
1921 dump += INDENT2 + videoDevice->dump() + "\n";
1922 }
1923 if (mUnattachedVideoDevices.empty()) {
1924 dump += INDENT2 "<none>\n";
1925 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001926 } // release lock
1927}
1928
1929void EventHub::monitor() {
1930 // Acquire and release the lock to ensure that the event hub has not deadlocked.
1931 mLock.lock();
1932 mLock.unlock();
1933}
1934
1935
1936}; // namespace android