blob: cd313c43ecfcbfbf7787e6e519b9976a07a4234d [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017#define LOG_TAG "EventHub"
18
JP Abgrall25a465b2012-05-16 10:33:49 -070019// #define LOG_NDEBUG 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
Jeff Brownb4ff35d2011-01-02 16:37:43 -080021#include "EventHub.h"
22
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <hardware_legacy/power.h>
24
25#include <cutils/properties.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026#include <utils/Log.h>
27#include <utils/Timers.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070028#include <utils/threads.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070029#include <utils/Errors.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31#include <stdlib.h>
32#include <stdio.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <memory.h>
36#include <errno.h>
37#include <assert.h>
38
Jeff Brown9d3b1a42013-07-01 19:07:15 -070039#include <input/KeyLayoutMap.h>
40#include <input/KeyCharacterMap.h>
41#include <input/VirtualKeyMap.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43#include <string.h>
44#include <stdint.h>
45#include <dirent.h>
Jeff Brown93fa9b32011-06-14 17:09:25 -070046
47#include <sys/inotify.h>
48#include <sys/epoll.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049#include <sys/ioctl.h>
Jeff Brown93fa9b32011-06-14 17:09:25 -070050#include <sys/limits.h>
Jeff Brown4dac9012013-04-10 01:03:19 -070051#include <sys/sha1.h>
Michael Wright67774c72013-09-25 14:13:40 -070052#include <sys/utsname.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54/* this macro is used to tell if "bit" is set in "array"
55 * it selects a byte from the array, and does a boolean AND
56 * operation with a byte that only has the relevant bit set.
57 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
58 */
59#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
60
Jeff Brownfd0358292010-06-30 16:10:35 -070061/* this macro computes the number of bytes needed to represent a bit array of the specified size */
62#define sizeof_bit_array(bits) ((bits + 7) / 8)
63
Jeff Brownf2f487182010-10-01 17:46:21 -070064#define INDENT " "
65#define INDENT2 " "
66#define INDENT3 " "
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068namespace android {
69
70static const char *WAKE_LOCK_ID = "KeyEvents";
Jeff Brown90655042010-12-02 13:50:46 -080071static const char *DEVICE_PATH = "/dev/input";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
73/* return the larger integer */
74static inline int max(int v1, int v2)
75{
76 return (v1 > v2) ? v1 : v2;
77}
78
Jeff Brownf2f487182010-10-01 17:46:21 -070079static inline const char* toString(bool value) {
80 return value ? "true" : "false";
81}
82
Jeff Browne38fdfa2012-04-06 14:51:01 -070083static String8 sha1(const String8& in) {
84 SHA1_CTX ctx;
85 SHA1Init(&ctx);
86 SHA1Update(&ctx, reinterpret_cast<const u_char*>(in.string()), in.size());
87 u_char digest[SHA1_DIGEST_LENGTH];
88 SHA1Final(digest, &ctx);
89
90 String8 out;
91 for (size_t i = 0; i < SHA1_DIGEST_LENGTH; i++) {
92 out.appendFormat("%02x", digest[i]);
93 }
94 return out;
95}
96
Michael Wright67774c72013-09-25 14:13:40 -070097static void getLinuxRelease(int* major, int* minor) {
98 struct utsname info;
99 if (uname(&info) || sscanf(info.release, "%d.%d", major, minor) <= 0) {
100 *major = 0, *minor = 0;
101 ALOGE("Could not get linux version: %s", strerror(errno));
102 }
103}
104
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700105static void setDescriptor(InputDeviceIdentifier& identifier) {
106 // Compute a device descriptor that uniquely identifies the device.
107 // The descriptor is assumed to be a stable identifier. Its value should not
108 // change between reboots, reconnections, firmware updates or new releases of Android.
109 // Ideally, we also want the descriptor to be short and relatively opaque.
110 String8 rawDescriptor;
111 rawDescriptor.appendFormat(":%04x:%04x:", identifier.vendor, identifier.product);
112 if (!identifier.uniqueId.isEmpty()) {
113 rawDescriptor.append("uniqueId:");
114 rawDescriptor.append(identifier.uniqueId);
115 } if (identifier.vendor == 0 && identifier.product == 0) {
116 // If we don't know the vendor and product id, then the device is probably
117 // built-in so we need to rely on other information to uniquely identify
118 // the input device. Usually we try to avoid relying on the device name or
119 // location but for built-in input device, they are unlikely to ever change.
120 if (!identifier.name.isEmpty()) {
121 rawDescriptor.append("name:");
122 rawDescriptor.append(identifier.name);
123 } else if (!identifier.location.isEmpty()) {
124 rawDescriptor.append("location:");
125 rawDescriptor.append(identifier.location);
126 }
127 }
128 identifier.descriptor = sha1(rawDescriptor);
Jeff Brown49ccac52012-04-11 18:27:33 -0700129 ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.string(),
130 identifier.descriptor.string());
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700131}
132
Jeff Brown9ee285af2011-08-31 12:56:34 -0700133// --- Global Functions ---
134
135uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses) {
136 // Touch devices get dibs on touch-related axes.
137 if (deviceClasses & INPUT_DEVICE_CLASS_TOUCH) {
138 switch (axis) {
139 case ABS_X:
140 case ABS_Y:
141 case ABS_PRESSURE:
142 case ABS_TOOL_WIDTH:
143 case ABS_DISTANCE:
144 case ABS_TILT_X:
145 case ABS_TILT_Y:
146 case ABS_MT_SLOT:
147 case ABS_MT_TOUCH_MAJOR:
148 case ABS_MT_TOUCH_MINOR:
149 case ABS_MT_WIDTH_MAJOR:
150 case ABS_MT_WIDTH_MINOR:
151 case ABS_MT_ORIENTATION:
152 case ABS_MT_POSITION_X:
153 case ABS_MT_POSITION_Y:
154 case ABS_MT_TOOL_TYPE:
155 case ABS_MT_BLOB_ID:
156 case ABS_MT_TRACKING_ID:
157 case ABS_MT_PRESSURE:
158 case ABS_MT_DISTANCE:
159 return INPUT_DEVICE_CLASS_TOUCH;
160 }
161 }
162
163 // Joystick devices get the rest.
164 return deviceClasses & INPUT_DEVICE_CLASS_JOYSTICK;
165}
166
Jeff Brown90655042010-12-02 13:50:46 -0800167// --- EventHub::Device ---
168
169EventHub::Device::Device(int fd, int32_t id, const String8& path,
170 const InputDeviceIdentifier& identifier) :
171 next(NULL),
172 fd(fd), id(id), path(path), identifier(identifier),
Jeff Browna47425a2012-04-13 04:09:27 -0700173 classes(0), configuration(NULL), virtualKeyMap(NULL),
Michael Wrightac6c78b2013-07-17 13:21:45 -0700174 ffEffectPlaying(false), ffEffectId(-1), controllerNumber(0),
Jeff Brown4dac9012013-04-10 01:03:19 -0700175 timestampOverrideSec(0), timestampOverrideUsec(0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700176 memset(keyBitmask, 0, sizeof(keyBitmask));
177 memset(absBitmask, 0, sizeof(absBitmask));
178 memset(relBitmask, 0, sizeof(relBitmask));
179 memset(swBitmask, 0, sizeof(swBitmask));
180 memset(ledBitmask, 0, sizeof(ledBitmask));
Jeff Browna47425a2012-04-13 04:09:27 -0700181 memset(ffBitmask, 0, sizeof(ffBitmask));
Jeff Brown93fa9b32011-06-14 17:09:25 -0700182 memset(propBitmask, 0, sizeof(propBitmask));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183}
184
Jeff Brown90655042010-12-02 13:50:46 -0800185EventHub::Device::~Device() {
186 close();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800187 delete configuration;
Jeff Brown90655042010-12-02 13:50:46 -0800188 delete virtualKeyMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189}
190
Jeff Brown90655042010-12-02 13:50:46 -0800191void EventHub::Device::close() {
192 if (fd >= 0) {
193 ::close(fd);
194 fd = -1;
195 }
196}
197
198
199// --- EventHub ---
200
Jeff Brown93fa9b32011-06-14 17:09:25 -0700201const uint32_t EventHub::EPOLL_ID_INOTIFY;
202const uint32_t EventHub::EPOLL_ID_WAKE;
203const int EventHub::EPOLL_SIZE_HINT;
204const int EventHub::EPOLL_MAX_EVENTS;
205
Jeff Brown90655042010-12-02 13:50:46 -0800206EventHub::EventHub(void) :
Michael Wrightac6c78b2013-07-17 13:21:45 -0700207 mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), mNextDeviceId(1), mControllerNumbers(),
Jeff Brown90655042010-12-02 13:50:46 -0800208 mOpeningDevices(0), mClosingDevices(0),
Jeff Brown93fa9b32011-06-14 17:09:25 -0700209 mNeedToSendFinishedDeviceScan(false),
210 mNeedToReopenDevices(false), mNeedToScanDevices(true),
211 mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brownb7198742011-03-18 18:14:26 -0700213
Jeff Brown93fa9b32011-06-14 17:09:25 -0700214 mEpollFd = epoll_create(EPOLL_SIZE_HINT);
215 LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno);
216
217 mINotifyFd = inotify_init();
218 int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
219 LOG_ALWAYS_FATAL_IF(result < 0, "Could not register INotify for %s. errno=%d",
220 DEVICE_PATH, errno);
221
222 struct epoll_event eventItem;
223 memset(&eventItem, 0, sizeof(eventItem));
224 eventItem.events = EPOLLIN;
225 eventItem.data.u32 = EPOLL_ID_INOTIFY;
226 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
227 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno);
228
229 int wakeFds[2];
230 result = pipe(wakeFds);
231 LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
232
233 mWakeReadPipeFd = wakeFds[0];
234 mWakeWritePipeFd = wakeFds[1];
235
236 result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
237 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
238 errno);
239
240 result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
241 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
242 errno);
243
244 eventItem.data.u32 = EPOLL_ID_WAKE;
245 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
246 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
247 errno);
Michael Wright67774c72013-09-25 14:13:40 -0700248
249 int major, minor;
250 getLinuxRelease(&major, &minor);
251 // EPOLLWAKEUP was introduced in kernel 3.5
252 mUsingEpollWakeup = major > 3 || (major == 3 && minor >= 5);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253}
254
Jeff Brown90655042010-12-02 13:50:46 -0800255EventHub::~EventHub(void) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700256 closeAllDevicesLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
Jeff Brown93fa9b32011-06-14 17:09:25 -0700258 while (mClosingDevices) {
259 Device* device = mClosingDevices;
260 mClosingDevices = device->next;
261 delete device;
262 }
263
264 ::close(mEpollFd);
265 ::close(mINotifyFd);
266 ::close(mWakeReadPipeFd);
267 ::close(mWakeWritePipeFd);
268
269 release_wake_lock(WAKE_LOCK_ID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270}
271
Jeff Browne38fdfa2012-04-06 14:51:01 -0700272InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800274 Device* device = getDeviceLocked(deviceId);
Jeff Browne38fdfa2012-04-06 14:51:01 -0700275 if (device == NULL) return InputDeviceIdentifier();
276 return device->identifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277}
278
Jeff Brown90655042010-12-02 13:50:46 -0800279uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800281 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 if (device == NULL) return 0;
283 return device->classes;
284}
285
Michael Wrightac6c78b2013-07-17 13:21:45 -0700286int32_t EventHub::getDeviceControllerNumber(int32_t deviceId) const {
287 AutoMutex _l(mLock);
288 Device* device = getDeviceLocked(deviceId);
289 if (device == NULL) return 0;
290 return device->controllerNumber;
291}
292
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800293void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800294 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800295 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800296 if (device && device->configuration) {
297 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800298 } else {
299 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800300 }
301}
302
Jeff Brown6d0fec22010-07-23 21:28:06 -0700303status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
304 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700305 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700306
Jeff Brownba421dd2011-08-10 15:07:05 -0700307 if (axis >= 0 && axis <= ABS_MAX) {
308 AutoMutex _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309
Jeff Brownba421dd2011-08-10 15:07:05 -0700310 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700311 if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700312 struct input_absinfo info;
313 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000314 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700315 axis, device->identifier.name.string(), device->fd, errno);
316 return -errno;
317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318
Jeff Brownba421dd2011-08-10 15:07:05 -0700319 if (info.minimum != info.maximum) {
320 outAxisInfo->valid = true;
321 outAxisInfo->minValue = info.minimum;
322 outAxisInfo->maxValue = info.maximum;
323 outAxisInfo->flat = info.flat;
324 outAxisInfo->fuzz = info.fuzz;
325 outAxisInfo->resolution = info.resolution;
326 }
327 return OK;
328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700330 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331}
332
Jeff Browncc0c1592011-02-19 05:07:28 -0800333bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
334 if (axis >= 0 && axis <= REL_MAX) {
335 AutoMutex _l(mLock);
336
337 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700338 if (device) {
Jeff Browncc0c1592011-02-19 05:07:28 -0800339 return test_bit(axis, device->relBitmask);
340 }
341 }
342 return false;
343}
344
Jeff Brown80fd47c2011-05-24 01:07:44 -0700345bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
346 if (property >= 0 && property <= INPUT_PROP_MAX) {
347 AutoMutex _l(mLock);
348
349 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700350 if (device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700351 return test_bit(property, device->propBitmask);
352 }
353 }
354 return false;
355}
356
Jeff Brown6d0fec22010-07-23 21:28:06 -0700357int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700358 if (scanCode >= 0 && scanCode <= KEY_MAX) {
359 AutoMutex _l(mLock);
360
Jeff Brown90655042010-12-02 13:50:46 -0800361 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700362 if (device && !device->isVirtual() && test_bit(scanCode, device->keyBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700363 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
364 memset(keyState, 0, sizeof(keyState));
365 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
366 return test_bit(scanCode, keyState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 }
369 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700370 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371}
372
Jeff Brown6d0fec22010-07-23 21:28:06 -0700373int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
374 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700375
Jeff Brown90655042010-12-02 13:50:46 -0800376 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700377 if (device && !device->isVirtual() && device->keyMap.haveKeyLayout()) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700378 Vector<int32_t> scanCodes;
379 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
380 if (scanCodes.size() != 0) {
381 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
382 memset(keyState, 0, sizeof(keyState));
383 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
384 for (size_t i = 0; i < scanCodes.size(); i++) {
385 int32_t sc = scanCodes.itemAt(i);
386 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, keyState)) {
387 return AKEY_STATE_DOWN;
388 }
389 }
390 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 }
392 }
393 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700394 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700395}
396
Jeff Brown6d0fec22010-07-23 21:28:06 -0700397int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700398 if (sw >= 0 && sw <= SW_MAX) {
399 AutoMutex _l(mLock);
400
Jeff Brown90655042010-12-02 13:50:46 -0800401 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700402 if (device && !device->isVirtual() && test_bit(sw, device->swBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700403 uint8_t swState[sizeof_bit_array(SW_MAX + 1)];
404 memset(swState, 0, sizeof(swState));
405 if (ioctl(device->fd, EVIOCGSW(sizeof(swState)), swState) >= 0) {
406 return test_bit(sw, swState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
407 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700408 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700409 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700410 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700411}
412
Jeff Brown2717eff2011-06-30 23:53:07 -0700413status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
Jeff Brown06309752011-08-11 17:10:06 -0700414 *outValue = 0;
415
Jeff Brown2717eff2011-06-30 23:53:07 -0700416 if (axis >= 0 && axis <= ABS_MAX) {
417 AutoMutex _l(mLock);
418
419 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700420 if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700421 struct input_absinfo info;
422 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000423 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700424 axis, device->identifier.name.string(), device->fd, errno);
425 return -errno;
426 }
427
428 *outValue = info.value;
429 return OK;
Jeff Brown2717eff2011-06-30 23:53:07 -0700430 }
431 }
Jeff Brown2717eff2011-06-30 23:53:07 -0700432 return -1;
433}
434
Jeff Brown6d0fec22010-07-23 21:28:06 -0700435bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
436 const int32_t* keyCodes, uint8_t* outFlags) const {
437 AutoMutex _l(mLock);
438
Jeff Brown90655042010-12-02 13:50:46 -0800439 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700440 if (device && device->keyMap.haveKeyLayout()) {
441 Vector<int32_t> scanCodes;
442 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
443 scanCodes.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700444
Jeff Brownba421dd2011-08-10 15:07:05 -0700445 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
446 keyCodes[codeIndex], &scanCodes);
447 if (! err) {
448 // check the possible scan codes identified by the layout map against the
449 // map of codes actually emitted by the driver
450 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
451 if (test_bit(scanCodes[sc], device->keyBitmask)) {
452 outFlags[codeIndex] = 1;
453 break;
454 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700455 }
456 }
457 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700458 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700459 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700460 return false;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700461}
462
Jeff Brown49ccac52012-04-11 18:27:33 -0700463status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
464 int32_t* outKeycode, uint32_t* outFlags) const {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700465 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800466 Device* device = getDeviceLocked(deviceId);
Jeff Brown49ccac52012-04-11 18:27:33 -0700467
Jeff Brown4a3862f2012-04-17 18:50:05 -0700468 if (device) {
469 // Check the key character map first.
470 sp<KeyCharacterMap> kcm = device->getKeyCharacterMap();
471 if (kcm != NULL) {
472 if (!kcm->mapKey(scanCode, usageCode, outKeycode)) {
473 *outFlags = 0;
474 return NO_ERROR;
475 }
476 }
477
478 // Check the key layout next.
479 if (device->keyMap.haveKeyLayout()) {
480 if (!device->keyMap.keyLayoutMap->mapKey(
481 scanCode, usageCode, outKeycode, outFlags)) {
482 return NO_ERROR;
483 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700484 }
485 }
Jeff Brown49ccac52012-04-11 18:27:33 -0700486
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700487 *outKeycode = 0;
488 *outFlags = 0;
489 return NAME_NOT_FOUND;
490}
491
Jeff Brown49ccac52012-04-11 18:27:33 -0700492status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800493 AutoMutex _l(mLock);
494 Device* device = getDeviceLocked(deviceId);
495
496 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700497 status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800498 if (err == NO_ERROR) {
499 return NO_ERROR;
500 }
501 }
502
Jeff Brown6f2fba42011-02-19 01:08:02 -0800503 return NAME_NOT_FOUND;
504}
505
Jeff Brown1a84fd12011-06-02 01:26:32 -0700506void EventHub::setExcludedDevices(const Vector<String8>& devices) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700507 AutoMutex _l(mLock);
508
Jeff Brown1a84fd12011-06-02 01:26:32 -0700509 mExcludedDevices = devices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400510}
511
Jeff Brown49754db2011-07-01 17:37:58 -0700512bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
513 AutoMutex _l(mLock);
514 Device* device = getDeviceLocked(deviceId);
515 if (device && scanCode >= 0 && scanCode <= KEY_MAX) {
516 if (test_bit(scanCode, device->keyBitmask)) {
517 return true;
518 }
519 }
520 return false;
521}
522
Jeff Brown497a92c2010-09-12 17:55:08 -0700523bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
524 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800525 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700526 if (device && led >= 0 && led <= LED_MAX) {
527 if (test_bit(led, device->ledBitmask)) {
528 return true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700529 }
530 }
531 return false;
532}
533
534void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
535 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800536 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700537 if (device && !device->isVirtual() && led >= 0 && led <= LED_MAX) {
Jeff Brown497a92c2010-09-12 17:55:08 -0700538 struct input_event ev;
539 ev.time.tv_sec = 0;
540 ev.time.tv_usec = 0;
541 ev.type = EV_LED;
542 ev.code = led;
543 ev.value = on ? 1 : 0;
544
545 ssize_t nWrite;
546 do {
547 nWrite = write(device->fd, &ev, sizeof(struct input_event));
548 } while (nWrite == -1 && errno == EINTR);
549 }
550}
551
Jeff Brown90655042010-12-02 13:50:46 -0800552void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
553 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
554 outVirtualKeys.clear();
555
556 AutoMutex _l(mLock);
557 Device* device = getDeviceLocked(deviceId);
558 if (device && device->virtualKeyMap) {
559 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
560 }
561}
562
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700563sp<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const {
Jeff Brown1e08fe92011-11-15 17:48:10 -0800564 AutoMutex _l(mLock);
565 Device* device = getDeviceLocked(deviceId);
566 if (device) {
Jeff Brown4a3862f2012-04-17 18:50:05 -0700567 return device->getKeyCharacterMap();
Jeff Brown1e08fe92011-11-15 17:48:10 -0800568 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700569 return NULL;
Jeff Brown1e08fe92011-11-15 17:48:10 -0800570}
571
Jeff Brown6ec6f792012-04-17 16:52:41 -0700572bool EventHub::setKeyboardLayoutOverlay(int32_t deviceId,
573 const sp<KeyCharacterMap>& map) {
574 AutoMutex _l(mLock);
575 Device* device = getDeviceLocked(deviceId);
576 if (device) {
577 if (map != device->overlayKeyMap) {
578 device->overlayKeyMap = map;
579 device->combinedKeyMap = KeyCharacterMap::combine(
580 device->keyMap.keyCharacterMap, map);
581 return true;
582 }
583 }
584 return false;
585}
586
Jeff Browna47425a2012-04-13 04:09:27 -0700587void EventHub::vibrate(int32_t deviceId, nsecs_t duration) {
588 AutoMutex _l(mLock);
589 Device* device = getDeviceLocked(deviceId);
590 if (device && !device->isVirtual()) {
591 ff_effect effect;
592 memset(&effect, 0, sizeof(effect));
593 effect.type = FF_RUMBLE;
594 effect.id = device->ffEffectId;
595 effect.u.rumble.strong_magnitude = 0xc000;
596 effect.u.rumble.weak_magnitude = 0xc000;
597 effect.replay.length = (duration + 999999LL) / 1000000LL;
598 effect.replay.delay = 0;
599 if (ioctl(device->fd, EVIOCSFF, &effect)) {
600 ALOGW("Could not upload force feedback effect to device %s due to error %d.",
601 device->identifier.name.string(), errno);
602 return;
603 }
604 device->ffEffectId = effect.id;
605
606 struct input_event ev;
607 ev.time.tv_sec = 0;
608 ev.time.tv_usec = 0;
609 ev.type = EV_FF;
610 ev.code = device->ffEffectId;
611 ev.value = 1;
612 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
613 ALOGW("Could not start force feedback effect on device %s due to error %d.",
614 device->identifier.name.string(), errno);
615 return;
616 }
617 device->ffEffectPlaying = true;
618 }
619}
620
621void EventHub::cancelVibrate(int32_t deviceId) {
622 AutoMutex _l(mLock);
623 Device* device = getDeviceLocked(deviceId);
624 if (device && !device->isVirtual()) {
625 if (device->ffEffectPlaying) {
626 device->ffEffectPlaying = false;
627
628 struct input_event ev;
629 ev.time.tv_sec = 0;
630 ev.time.tv_usec = 0;
631 ev.type = EV_FF;
632 ev.code = device->ffEffectId;
633 ev.value = 0;
634 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
635 ALOGW("Could not stop force feedback effect on device %s due to error %d.",
636 device->identifier.name.string(), errno);
637 return;
638 }
639 }
640 }
641}
642
Jeff Brown90655042010-12-02 13:50:46 -0800643EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700644 if (deviceId == BUILT_IN_KEYBOARD_ID) {
Jeff Brown90655042010-12-02 13:50:46 -0800645 deviceId = mBuiltInKeyboardId;
646 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700647 ssize_t index = mDevices.indexOfKey(deviceId);
648 return index >= 0 ? mDevices.valueAt(index) : NULL;
649}
Jeff Brown90655042010-12-02 13:50:46 -0800650
Jeff Brown93fa9b32011-06-14 17:09:25 -0700651EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const {
652 for (size_t i = 0; i < mDevices.size(); i++) {
653 Device* device = mDevices.valueAt(i);
654 if (device->path == devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800655 return device;
656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 }
658 return NULL;
659}
660
Jeff Brownb7198742011-03-18 18:14:26 -0700661size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
Steve Blockec193de2012-01-09 18:35:44 +0000662 ALOG_ASSERT(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400663
Jeff Brown93fa9b32011-06-14 17:09:25 -0700664 AutoMutex _l(mLock);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400665
Jeff Brownb7198742011-03-18 18:14:26 -0700666 struct input_event readBuffer[bufferSize];
667
668 RawEvent* event = buffer;
669 size_t capacity = bufferSize;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700670 bool awoken = false;
Jeff Browncc2e7172010-08-17 16:48:25 -0700671 for (;;) {
Jeff Brownb7198742011-03-18 18:14:26 -0700672 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
673
Jeff Brown1a84fd12011-06-02 01:26:32 -0700674 // Reopen input devices if needed.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700675 if (mNeedToReopenDevices) {
676 mNeedToReopenDevices = false;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700677
Steve Block6215d3f2012-01-04 20:05:49 +0000678 ALOGI("Reopening all input devices due to a configuration change.");
Jeff Brown1a84fd12011-06-02 01:26:32 -0700679
Jeff Brown93fa9b32011-06-14 17:09:25 -0700680 closeAllDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700681 mNeedToScanDevices = true;
682 break; // return to the caller before we actually rescan
683 }
684
Jeff Browncc2e7172010-08-17 16:48:25 -0700685 // Report any devices that had last been added/removed.
Jeff Brownb7198742011-03-18 18:14:26 -0700686 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800687 Device* device = mClosingDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100688 ALOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 device->id, device->path.string());
690 mClosingDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700691 event->when = now;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700692 event->deviceId = device->id == mBuiltInKeyboardId ? BUILT_IN_KEYBOARD_ID : device->id;
Jeff Brownb7198742011-03-18 18:14:26 -0700693 event->type = DEVICE_REMOVED;
694 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700696 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700697 if (--capacity == 0) {
698 break;
699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700701
Jeff Brown1a84fd12011-06-02 01:26:32 -0700702 if (mNeedToScanDevices) {
703 mNeedToScanDevices = false;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700704 scanDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700705 mNeedToSendFinishedDeviceScan = true;
706 }
707
Jeff Brownb7198742011-03-18 18:14:26 -0700708 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800709 Device* device = mOpeningDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100710 ALOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 device->id, device->path.string());
712 mOpeningDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700713 event->when = now;
714 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
715 event->type = DEVICE_ADDED;
716 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700717 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700718 if (--capacity == 0) {
719 break;
720 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700721 }
722
723 if (mNeedToSendFinishedDeviceScan) {
724 mNeedToSendFinishedDeviceScan = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700725 event->when = now;
726 event->type = FINISHED_DEVICE_SCAN;
727 event += 1;
728 if (--capacity == 0) {
729 break;
730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
732
Jeff Browncc2e7172010-08-17 16:48:25 -0700733 // Grab the next input event.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700734 bool deviceChanged = false;
735 while (mPendingEventIndex < mPendingEventCount) {
736 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
737 if (eventItem.data.u32 == EPOLL_ID_INOTIFY) {
738 if (eventItem.events & EPOLLIN) {
739 mPendingINotify = true;
740 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000741 ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700742 }
743 continue;
744 }
745
746 if (eventItem.data.u32 == EPOLL_ID_WAKE) {
747 if (eventItem.events & EPOLLIN) {
Steve Block71f2cf12011-10-20 11:56:00 +0100748 ALOGV("awoken after wake()");
Jeff Brown93fa9b32011-06-14 17:09:25 -0700749 awoken = true;
750 char buffer[16];
751 ssize_t nRead;
752 do {
753 nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
754 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
755 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000756 ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700757 eventItem.events);
758 }
759 continue;
760 }
761
762 ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32);
763 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000764 ALOGW("Received unexpected epoll event 0x%08x for unknown device id %d.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700765 eventItem.events, eventItem.data.u32);
766 continue;
767 }
768
769 Device* device = mDevices.valueAt(deviceIndex);
770 if (eventItem.events & EPOLLIN) {
771 int32_t readSize = read(device->fd, readBuffer,
772 sizeof(struct input_event) * capacity);
773 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
774 // Device was removed before INotify noticed.
Jeff Brown41305542011-10-05 11:14:13 -0700775 ALOGW("could not get event, removed? (fd: %d size: %d bufferSize: %d "
776 "capacity: %d errno: %d)\n",
777 device->fd, readSize, bufferSize, capacity, errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700778 deviceChanged = true;
779 closeDeviceLocked(device);
780 } else if (readSize < 0) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700781 if (errno != EAGAIN && errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000782 ALOGW("could not get event (errno=%d)", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700783 }
784 } else if ((readSize % sizeof(struct input_event)) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000785 ALOGE("could not get event (wrong size: %d)", readSize);
Jeff Browncc2e7172010-08-17 16:48:25 -0700786 } else {
Jeff Brownb7198742011-03-18 18:14:26 -0700787 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
788
789 size_t count = size_t(readSize) / sizeof(struct input_event);
790 for (size_t i = 0; i < count; i++) {
Jeff Brown4dac9012013-04-10 01:03:19 -0700791 struct input_event& iev = readBuffer[i];
792 ALOGV("%s got: time=%d.%06d, type=%d, code=%d, value=%d",
JP Abgrall25a465b2012-05-16 10:33:49 -0700793 device->path.string(),
794 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
795 iev.type, iev.code, iev.value);
796
Jeff Brown4dac9012013-04-10 01:03:19 -0700797 // Some input devices may have a better concept of the time
798 // when an input event was actually generated than the kernel
799 // which simply timestamps all events on entry to evdev.
800 // This is a custom Android extension of the input protocol
801 // mainly intended for use with uinput based device drivers.
802 if (iev.type == EV_MSC) {
803 if (iev.code == MSC_ANDROID_TIME_SEC) {
804 device->timestampOverrideSec = iev.value;
805 continue;
806 } else if (iev.code == MSC_ANDROID_TIME_USEC) {
807 device->timestampOverrideUsec = iev.value;
808 continue;
809 }
810 }
811 if (device->timestampOverrideSec || device->timestampOverrideUsec) {
812 iev.time.tv_sec = device->timestampOverrideSec;
813 iev.time.tv_usec = device->timestampOverrideUsec;
814 if (iev.type == EV_SYN && iev.code == SYN_REPORT) {
815 device->timestampOverrideSec = 0;
816 device->timestampOverrideUsec = 0;
817 }
818 ALOGV("applied override time %d.%06d",
819 int(iev.time.tv_sec), int(iev.time.tv_usec));
820 }
821
Jeff Brown4e91a182011-04-07 11:38:09 -0700822#ifdef HAVE_POSIX_CLOCKS
823 // Use the time specified in the event instead of the current time
824 // so that downstream code can get more accurate estimates of
825 // event dispatch latency from the time the event is enqueued onto
826 // the evdev client buffer.
827 //
828 // The event's timestamp fortuitously uses the same monotonic clock
829 // time base as the rest of Android. The kernel event device driver
830 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
831 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
832 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
833 // system call that also queries ktime_get_ts().
834 event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
835 + nsecs_t(iev.time.tv_usec) * 1000LL;
JP Abgrall25a465b2012-05-16 10:33:49 -0700836 ALOGV("event time %lld, now %lld", event->when, now);
Jeff Brownf33b2b22012-10-05 17:59:56 -0700837
838 // Bug 7291243: Add a guard in case the kernel generates timestamps
839 // that appear to be far into the future because they were generated
840 // using the wrong clock source.
841 //
842 // This can happen because when the input device is initially opened
843 // it has a default clock source of CLOCK_REALTIME. Any input events
844 // enqueued right after the device is opened will have timestamps
845 // generated using CLOCK_REALTIME. We later set the clock source
846 // to CLOCK_MONOTONIC but it is already too late.
847 //
848 // Invalid input event timestamps can result in ANRs, crashes and
849 // and other issues that are hard to track down. We must not let them
850 // propagate through the system.
851 //
852 // Log a warning so that we notice the problem and recover gracefully.
853 if (event->when >= now + 10 * 1000000000LL) {
854 // Double-check. Time may have moved on.
855 nsecs_t time = systemTime(SYSTEM_TIME_MONOTONIC);
856 if (event->when > time) {
857 ALOGW("An input event from %s has a timestamp that appears to "
858 "have been generated using the wrong clock source "
859 "(expected CLOCK_MONOTONIC): "
860 "event time %lld, current time %lld, call time %lld. "
861 "Using current time instead.",
862 device->path.string(), event->when, time, now);
863 event->when = time;
864 } else {
865 ALOGV("Event time is ok but failed the fast path and required "
866 "an extra call to systemTime: "
867 "event time %lld, current time %lld, call time %lld.",
868 event->when, time, now);
869 }
870 }
Jeff Brown4e91a182011-04-07 11:38:09 -0700871#else
Jeff Brownb7198742011-03-18 18:14:26 -0700872 event->when = now;
Jeff Brown4e91a182011-04-07 11:38:09 -0700873#endif
Jeff Brownb7198742011-03-18 18:14:26 -0700874 event->deviceId = deviceId;
875 event->type = iev.type;
Jeff Brown49ccac52012-04-11 18:27:33 -0700876 event->code = iev.code;
Jeff Brownb7198742011-03-18 18:14:26 -0700877 event->value = iev.value;
Jeff Brownb7198742011-03-18 18:14:26 -0700878 event += 1;
Jeff Brown4dac9012013-04-10 01:03:19 -0700879 capacity -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700880 }
Jeff Brownb7198742011-03-18 18:14:26 -0700881 if (capacity == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700882 // The result buffer is full. Reset the pending event index
883 // so we will try to read the device again on the next iteration.
884 mPendingEventIndex -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700885 break;
886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700888 } else if (eventItem.events & EPOLLHUP) {
889 ALOGI("Removing device %s due to epoll hang-up event.",
890 device->identifier.name.string());
891 deviceChanged = true;
892 closeDeviceLocked(device);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700893 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000894 ALOGW("Received unexpected epoll event 0x%08x for device %s.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700895 eventItem.events, device->identifier.name.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 }
897 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700898
Jeff Brown93fa9b32011-06-14 17:09:25 -0700899 // readNotify() will modify the list of devices so this must be done after
900 // processing all other events to ensure that we read all remaining events
901 // before closing the devices.
902 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
903 mPendingINotify = false;
904 readNotifyLocked();
905 deviceChanged = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -0800906 }
907
Jeff Brown93fa9b32011-06-14 17:09:25 -0700908 // Report added or removed devices immediately.
909 if (deviceChanged) {
910 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 }
Jeff Browna9b84222010-10-14 02:23:43 -0700912
Jeff Brown93fa9b32011-06-14 17:09:25 -0700913 // Return now if we have collected any events or if we were explicitly awoken.
914 if (event != buffer || awoken) {
Jeff Brownb7198742011-03-18 18:14:26 -0700915 break;
916 }
917
Jeff Browncc2e7172010-08-17 16:48:25 -0700918 // Poll for events. Mind the wake lock dance!
Jeff Brown93fa9b32011-06-14 17:09:25 -0700919 // We hold a wake lock at all times except during epoll_wait(). This works due to some
Jeff Browncc2e7172010-08-17 16:48:25 -0700920 // subtle choreography. When a device driver has pending (unread) events, it acquires
921 // a kernel wake lock. However, once the last pending event has been read, the device
922 // driver will release the kernel wake lock. To prevent the system from going to sleep
923 // when this happens, the EventHub holds onto its own user wake lock while the client
924 // is processing events. Thus the system can only sleep if there are no events
925 // pending or currently being processed.
Jeff Brownaa3855d2011-03-17 01:34:19 -0700926 //
927 // The timeout is advisory only. If the device is asleep, it will not wake just to
928 // service the timeout.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700929 mPendingEventIndex = 0;
930
931 mLock.unlock(); // release lock before poll, must be before release_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700932 release_wake_lock(WAKE_LOCK_ID);
933
Jeff Brown93fa9b32011-06-14 17:09:25 -0700934 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700935
936 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700937 mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700938
Jeff Brownaa3855d2011-03-17 01:34:19 -0700939 if (pollResult == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700940 // Timed out.
941 mPendingEventCount = 0;
942 break;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700943 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700944
Jeff Brownaa3855d2011-03-17 01:34:19 -0700945 if (pollResult < 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700946 // An error occurred.
947 mPendingEventCount = 0;
948
Jeff Brownb7198742011-03-18 18:14:26 -0700949 // Sleep after errors to avoid locking up the system.
950 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -0700951 if (errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000952 ALOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700953 usleep(100000);
954 }
Jeff Brownb7198742011-03-18 18:14:26 -0700955 } else {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700956 // Some events occurred.
957 mPendingEventCount = size_t(pollResult);
Jeff Browncc2e7172010-08-17 16:48:25 -0700958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
Jeff Brownb7198742011-03-18 18:14:26 -0700960
961 // All done, return the number of events we read.
962 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963}
964
Jeff Brown93fa9b32011-06-14 17:09:25 -0700965void EventHub::wake() {
Steve Block71f2cf12011-10-20 11:56:00 +0100966 ALOGV("wake() called");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967
Jeff Brown93fa9b32011-06-14 17:09:25 -0700968 ssize_t nWrite;
969 do {
970 nWrite = write(mWakeWritePipeFd, "W", 1);
971 } while (nWrite == -1 && errno == EINTR);
972
973 if (nWrite != 1 && errno != EAGAIN) {
Steve Block8564c8d2012-01-05 23:22:43 +0000974 ALOGW("Could not write wake signal, errno=%d", errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700976}
Jeff Brown90655042010-12-02 13:50:46 -0800977
Jeff Brown93fa9b32011-06-14 17:09:25 -0700978void EventHub::scanDevicesLocked() {
979 status_t res = scanDirLocked(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 if(res < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000981 ALOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700983 if (mDevices.indexOfKey(VIRTUAL_KEYBOARD_ID) < 0) {
984 createVirtualKeyboardLocked();
985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986}
987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988// ----------------------------------------------------------------------------
989
Jeff Brownfd0358292010-06-30 16:10:35 -0700990static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
991 const uint8_t* end = array + endIndex;
992 array += startIndex;
993 while (array != end) {
994 if (*(array++) != 0) {
995 return true;
996 }
997 }
998 return false;
999}
1000
1001static const int32_t GAMEPAD_KEYCODES[] = {
1002 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
1003 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
1004 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
1005 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
1006 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -08001007 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
1008 AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
1009 AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
1010 AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
1011 AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
Jeff Brownfd0358292010-06-30 16:10:35 -07001012};
1013
Jeff Brown93fa9b32011-06-14 17:09:25 -07001014status_t EventHub::openDeviceLocked(const char *devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -08001015 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016
Steve Block71f2cf12011-10-20 11:56:00 +01001017 ALOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018
Jeff Brown874c1e92012-01-19 14:32:47 -08001019 int fd = open(devicePath, O_RDWR | O_CLOEXEC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 if(fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001021 ALOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 return -1;
1023 }
1024
Jeff Brown90655042010-12-02 13:50:46 -08001025 InputDeviceIdentifier identifier;
1026
1027 // Get device name.
1028 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
1029 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
1030 } else {
1031 buffer[sizeof(buffer) - 1] = '\0';
1032 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
Mike Lockwood15431a92009-07-17 00:10:10 -04001034
Jeff Brown90655042010-12-02 13:50:46 -08001035 // Check to see if the device is on our excluded list
Jeff Brown1a84fd12011-06-02 01:26:32 -07001036 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
1037 const String8& item = mExcludedDevices.itemAt(i);
1038 if (identifier.name == item) {
Steve Block6215d3f2012-01-04 20:05:49 +00001039 ALOGI("ignoring event id %s driver %s\n", devicePath, item.string());
Mike Lockwood15431a92009-07-17 00:10:10 -04001040 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -04001041 return -1;
1042 }
1043 }
1044
Jeff Brown90655042010-12-02 13:50:46 -08001045 // Get device driver version.
1046 int driverVersion;
1047 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
Steve Block3762c312012-01-06 19:20:56 +00001048 ALOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -08001049 close(fd);
1050 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
1052
Jeff Brown90655042010-12-02 13:50:46 -08001053 // Get device identifier.
1054 struct input_id inputId;
1055 if(ioctl(fd, EVIOCGID, &inputId)) {
Steve Block3762c312012-01-06 19:20:56 +00001056 ALOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -08001057 close(fd);
1058 return -1;
1059 }
1060 identifier.bus = inputId.bustype;
1061 identifier.product = inputId.product;
1062 identifier.vendor = inputId.vendor;
1063 identifier.version = inputId.version;
1064
1065 // Get device physical location.
1066 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
1067 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
1068 } else {
1069 buffer[sizeof(buffer) - 1] = '\0';
1070 identifier.location.setTo(buffer);
1071 }
1072
1073 // Get device unique id.
1074 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
1075 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
1076 } else {
1077 buffer[sizeof(buffer) - 1] = '\0';
1078 identifier.uniqueId.setTo(buffer);
1079 }
1080
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001081 // Fill in the descriptor.
1082 setDescriptor(identifier);
Jeff Browne38fdfa2012-04-06 14:51:01 -07001083
Jeff Brown90655042010-12-02 13:50:46 -08001084 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -07001085 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
Steve Block3762c312012-01-06 19:20:56 +00001086 ALOGE("Error %d making device file descriptor non-blocking.", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -07001087 close(fd);
1088 return -1;
1089 }
1090
Jeff Brown90655042010-12-02 13:50:46 -08001091 // Allocate device. (The device object takes ownership of the fd at this point.)
1092 int32_t deviceId = mNextDeviceId++;
1093 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094
Jeff Browne38fdfa2012-04-06 14:51:01 -07001095 ALOGV("add device %d: %s\n", deviceId, devicePath);
1096 ALOGV(" bus: %04x\n"
1097 " vendor %04x\n"
1098 " product %04x\n"
1099 " version %04x\n",
Jeff Brown90655042010-12-02 13:50:46 -08001100 identifier.bus, identifier.vendor, identifier.product, identifier.version);
Jeff Browne38fdfa2012-04-06 14:51:01 -07001101 ALOGV(" name: \"%s\"\n", identifier.name.string());
1102 ALOGV(" location: \"%s\"\n", identifier.location.string());
1103 ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.string());
Jeff Brown49ccac52012-04-11 18:27:33 -07001104 ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.string());
Jeff Browne38fdfa2012-04-06 14:51:01 -07001105 ALOGV(" driver: v%d.%d.%d\n",
Jeff Brown90655042010-12-02 13:50:46 -08001106 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001108 // Load the configuration file for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001109 loadConfigurationLocked(device);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001110
Jeff Brownfd0358292010-06-30 16:10:35 -07001111 // Figure out the kinds of events the device reports.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001112 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask);
1113 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask);
1114 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask);
1115 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask);
1116 ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask);
Jeff Browna47425a2012-04-13 04:09:27 -07001117 ioctl(fd, EVIOCGBIT(EV_FF, sizeof(device->ffBitmask)), device->ffBitmask);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001118 ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask);
Jeff Browncc0c1592011-02-19 05:07:28 -08001119
Jeff Brown6f2fba42011-02-19 01:08:02 -08001120 // See if this is a keyboard. Ignore everything in the button range except for
1121 // joystick and gamepad buttons which are handled like keyboards for the most part.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001122 bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC))
1123 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK),
Jeff Brown6f2fba42011-02-19 01:08:02 -08001124 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001125 bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC),
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001126 sizeof_bit_array(BTN_MOUSE))
Jeff Brown93fa9b32011-06-14 17:09:25 -07001127 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK),
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001128 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -08001129 if (haveKeyboardKeys || haveGamepadButtons) {
1130 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001132
Jeff Brown83c09682010-12-23 17:50:18 -08001133 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001134 if (test_bit(BTN_MOUSE, device->keyBitmask)
1135 && test_bit(REL_X, device->relBitmask)
1136 && test_bit(REL_Y, device->relBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001137 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001139
1140 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -08001141 // Is this a new modern multi-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -07001142 if (test_bit(ABS_MT_POSITION_X, device->absBitmask)
1143 && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001144 // Some joysticks such as the PS3 controller report axes that conflict
1145 // with the ABS_MT range. Try to confirm that the device really is
1146 // a touch screen.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001147 if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -08001148 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd0358292010-06-30 16:10:35 -07001149 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001150 // Is this an old style single-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -07001151 } else if (test_bit(BTN_TOUCH, device->keyBitmask)
1152 && test_bit(ABS_X, device->absBitmask)
1153 && test_bit(ABS_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001154 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 }
1156
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001157 // See if this device is a joystick.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001158 // Assumes that joysticks always have gamepad buttons in order to distinguish them
1159 // from other devices such as accelerometers that also have absolute axes.
Jeff Brown9ee285af2011-08-31 12:56:34 -07001160 if (haveGamepadButtons) {
1161 uint32_t assumedClasses = device->classes | INPUT_DEVICE_CLASS_JOYSTICK;
1162 for (int i = 0; i <= ABS_MAX; i++) {
1163 if (test_bit(i, device->absBitmask)
1164 && (getAbsAxisUsage(i, assumedClasses) & INPUT_DEVICE_CLASS_JOYSTICK)) {
1165 device->classes = assumedClasses;
1166 break;
1167 }
1168 }
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001169 }
1170
Jeff Brown93fa9b32011-06-14 17:09:25 -07001171 // Check whether this device has switches.
1172 for (int i = 0; i <= SW_MAX; i++) {
1173 if (test_bit(i, device->swBitmask)) {
1174 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
1175 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
1177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178
Jeff Browna47425a2012-04-13 04:09:27 -07001179 // Check whether this device supports the vibrator.
1180 if (test_bit(FF_RUMBLE, device->ffBitmask)) {
1181 device->classes |= INPUT_DEVICE_CLASS_VIBRATOR;
1182 }
1183
Jeff Brown93fa9b32011-06-14 17:09:25 -07001184 // Configure virtual keys.
Jeff Brown58a2da82011-01-25 16:02:22 -08001185 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -08001186 // Load the virtual keys for the touch screen, if any.
1187 // We do this now so that we can make sure to load the keymap if necessary.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001188 status_t status = loadVirtualKeyMapLocked(device);
Jeff Brown90655042010-12-02 13:50:46 -08001189 if (!status) {
1190 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
Jeff Brown90655042010-12-02 13:50:46 -08001192 }
1193
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001194 // Load the key map.
1195 // We need to do this for joysticks too because the key layout may specify axes.
1196 status_t keyMapStatus = NAME_NOT_FOUND;
1197 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
Jeff Brown90655042010-12-02 13:50:46 -08001198 // Load the keymap for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001199 keyMapStatus = loadKeyMapLocked(device);
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001200 }
Jeff Brown90655042010-12-02 13:50:46 -08001201
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001202 // Configure the keyboard, gamepad or virtual keyboard.
1203 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -08001204 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001205 if (!keyMapStatus
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001206 && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD
Jeff Brown90655042010-12-02 13:50:46 -08001207 && isEligibleBuiltInKeyboard(device->identifier,
1208 device->configuration, &device->keyMap)) {
1209 mBuiltInKeyboardId = device->id;
Jeff Brown497a92c2010-09-12 17:55:08 -07001210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211
Ken Wakasa02a44f72013-07-05 04:08:36 +00001212 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
1213 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
1214 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
1215 }
1216
Jeff Brownfd0358292010-06-30 16:10:35 -07001217 // See if this device has a DPAD.
Jeff Brownf2f487182010-10-01 17:46:21 -07001218 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
1219 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
1220 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
1221 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
1222 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001223 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001224 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001225
Jeff Brownfd0358292010-06-30 16:10:35 -07001226 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -07001227 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f487182010-10-01 17:46:21 -07001228 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd0358292010-06-30 16:10:35 -07001229 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
1230 break;
1231 }
1232 }
Michael Wrighta0a72852013-02-21 23:51:45 -08001233
1234 // Disable kernel key repeat since we handle it ourselves
1235 unsigned int repeatRate[] = {0,0};
1236 if (ioctl(fd, EVIOCSREP, repeatRate)) {
1237 ALOGW("Unable to disable kernel key repeat for %s: %s", devicePath, strerror(errno));
1238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 }
1240
Sean McNeilaeb00c42010-06-23 16:00:37 +07001241 // If the device isn't recognized as something we handle, don't monitor it.
1242 if (device->classes == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001243 ALOGV("Dropping device: id=%d, path='%s', name='%s'",
Jeff Brown90655042010-12-02 13:50:46 -08001244 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +07001245 delete device;
1246 return -1;
1247 }
1248
Jeff Brown56194eb2011-03-02 19:23:13 -08001249 // Determine whether the device is external or internal.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001250 if (isExternalDeviceLocked(device)) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001251 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1252 }
1253
Michael Wrightac6c78b2013-07-17 13:21:45 -07001254 if (device->classes & (INPUT_DEVICE_CLASS_JOYSTICK | INPUT_DEVICE_CLASS_GAMEPAD)) {
1255 device->controllerNumber = getNextControllerNumberLocked(device);
1256 }
1257
Jeff Brown93fa9b32011-06-14 17:09:25 -07001258 // Register with epoll.
1259 struct epoll_event eventItem;
1260 memset(&eventItem, 0, sizeof(eventItem));
Michael Wright67774c72013-09-25 14:13:40 -07001261 eventItem.events = mUsingEpollWakeup ? EPOLLIN : EPOLLIN | EPOLLWAKEUP;
Jeff Brown93fa9b32011-06-14 17:09:25 -07001262 eventItem.data.u32 = deviceId;
1263 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
Steve Block3762c312012-01-06 19:20:56 +00001264 ALOGE("Could not add device fd to epoll instance. errno=%d", errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001265 delete device;
1266 return -1;
1267 }
1268
Michael Wright67774c72013-09-25 14:13:40 -07001269 String8 wakeMechanism("EPOLLWAKEUP");
1270 if (!mUsingEpollWakeup) {
1271 if (ioctl(fd, EVIOCSSUSPENDBLOCK, 1)) {
1272 wakeMechanism = "<none>";
1273 } else {
1274 wakeMechanism = "EVIOCSSUSPENDBLOCK";
1275 }
1276 }
Jeff Browneca3cf52012-04-06 19:31:36 -07001277
1278 // Tell the kernel that we want to use the monotonic clock for reporting timestamps
1279 // associated with input events. This is important because the input system
1280 // uses the timestamps extensively and assumes they were recorded using the monotonic
1281 // clock.
1282 //
1283 // In older kernel, before Linux 3.4, there was no way to tell the kernel which
1284 // clock to use to input event timestamps. The standard kernel behavior was to
1285 // record a real time timestamp, which isn't what we want. Android kernels therefore
1286 // contained a patch to the evdev_event() function in drivers/input/evdev.c to
1287 // replace the call to do_gettimeofday() with ktime_get_ts() to cause the monotonic
1288 // clock to be used instead of the real time clock.
1289 //
1290 // As of Linux 3.4, there is a new EVIOCSCLOCKID ioctl to set the desired clock.
1291 // Therefore, we no longer require the Android-specific kernel patch described above
1292 // as long as we make sure to set select the monotonic clock. We do that here.
Jeff Browna75fe052012-05-01 18:41:26 -07001293 int clockId = CLOCK_MONOTONIC;
1294 bool usingClockIoctl = !ioctl(fd, EVIOCSCLOCKID, &clockId);
Jeff Browne22afbe2011-12-16 13:45:40 -08001295
Steve Block6215d3f2012-01-04 20:05:49 +00001296 ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
Jeff Browne22afbe2011-12-16 13:45:40 -08001297 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, "
Michael Wright67774c72013-09-25 14:13:40 -07001298 "wakeMechanism=%s, usingClockIoctl=%s",
Jeff Brown90655042010-12-02 13:50:46 -08001299 deviceId, fd, devicePath, device->identifier.name.string(),
1300 device->classes,
1301 device->configurationFile.string(),
1302 device->keyMap.keyLayoutFile.string(),
1303 device->keyMap.keyCharacterMapFile.string(),
Jeff Browne22afbe2011-12-16 13:45:40 -08001304 toString(mBuiltInKeyboardId == deviceId),
Michael Wright67774c72013-09-25 14:13:40 -07001305 wakeMechanism.string(), toString(usingClockIoctl));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001306
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001307 addDeviceLocked(device);
1308 return 0;
1309}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001311void EventHub::createVirtualKeyboardLocked() {
1312 InputDeviceIdentifier identifier;
1313 identifier.name = "Virtual";
1314 identifier.uniqueId = "<virtual>";
1315 setDescriptor(identifier);
1316
1317 Device* device = new Device(-1, VIRTUAL_KEYBOARD_ID, String8("<virtual>"), identifier);
1318 device->classes = INPUT_DEVICE_CLASS_KEYBOARD
1319 | INPUT_DEVICE_CLASS_ALPHAKEY
1320 | INPUT_DEVICE_CLASS_DPAD
1321 | INPUT_DEVICE_CLASS_VIRTUAL;
1322 loadKeyMapLocked(device);
1323 addDeviceLocked(device);
1324}
1325
1326void EventHub::addDeviceLocked(Device* device) {
1327 mDevices.add(device->id, device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 device->next = mOpeningDevices;
1329 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330}
1331
Jeff Brown93fa9b32011-06-14 17:09:25 -07001332void EventHub::loadConfigurationLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001333 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1334 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001335 if (device->configurationFile.isEmpty()) {
Steve Block5baa3a62011-12-20 16:23:08 +00001336 ALOGD("No input device configuration file found for device '%s'.",
Jeff Brown90655042010-12-02 13:50:46 -08001337 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001338 } else {
1339 status_t status = PropertyMap::load(device->configurationFile,
1340 &device->configuration);
1341 if (status) {
Steve Block3762c312012-01-06 19:20:56 +00001342 ALOGE("Error loading input device configuration file for device '%s'. "
Jeff Brown90655042010-12-02 13:50:46 -08001343 "Using default configuration.",
1344 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001345 }
1346 }
1347}
1348
Jeff Brown93fa9b32011-06-14 17:09:25 -07001349status_t EventHub::loadVirtualKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001350 // The virtual key map is supplied by the kernel as a system board property file.
1351 String8 path;
1352 path.append("/sys/board_properties/virtualkeys.");
1353 path.append(device->identifier.name);
1354 if (access(path.string(), R_OK)) {
1355 return NAME_NOT_FOUND;
1356 }
1357 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -07001358}
1359
Jeff Brown93fa9b32011-06-14 17:09:25 -07001360status_t EventHub::loadKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001361 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001362}
1363
Jeff Brown93fa9b32011-06-14 17:09:25 -07001364bool EventHub::isExternalDeviceLocked(Device* device) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001365 if (device->configuration) {
1366 bool value;
Max Braune81056f2011-08-30 14:35:45 -07001367 if (device->configuration->tryGetProperty(String8("device.internal"), value)) {
1368 return !value;
Jeff Brown56194eb2011-03-02 19:23:13 -08001369 }
1370 }
1371 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1372}
1373
Michael Wrightac6c78b2013-07-17 13:21:45 -07001374int32_t EventHub::getNextControllerNumberLocked(Device* device) {
1375 if (mControllerNumbers.isFull()) {
1376 ALOGI("Maximum number of controllers reached, assigning controller number 0 to device %s",
1377 device->identifier.name.string());
1378 return 0;
1379 }
1380 // Since the controller number 0 is reserved for non-controllers, translate all numbers up by
1381 // one
1382 return static_cast<int32_t>(mControllerNumbers.markFirstUnmarkedBit() + 1);
1383}
1384
1385void EventHub::releaseControllerNumberLocked(Device* device) {
1386 int32_t num = device->controllerNumber;
1387 device->controllerNumber= 0;
1388 if (num == 0) {
1389 return;
1390 }
1391 mControllerNumbers.clearBit(static_cast<uint32_t>(num - 1));
1392}
1393
1394
Jeff Brown90655042010-12-02 13:50:46 -08001395bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1396 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001397 return false;
1398 }
1399
1400 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001401 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001402 const size_t N = scanCodes.size();
1403 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1404 int32_t sc = scanCodes.itemAt(i);
1405 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1406 return true;
1407 }
1408 }
1409
1410 return false;
1411}
1412
Jeff Brown93fa9b32011-06-14 17:09:25 -07001413status_t EventHub::closeDeviceByPathLocked(const char *devicePath) {
1414 Device* device = getDeviceByPathLocked(devicePath);
1415 if (device) {
1416 closeDeviceLocked(device);
1417 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
Steve Block71f2cf12011-10-20 11:56:00 +01001419 ALOGV("Remove device: %s not found, device may already have been removed.", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 return -1;
1421}
1422
Jeff Brown93fa9b32011-06-14 17:09:25 -07001423void EventHub::closeAllDevicesLocked() {
1424 while (mDevices.size() > 0) {
1425 closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1));
1426 }
1427}
1428
1429void EventHub::closeDeviceLocked(Device* device) {
Steve Block6215d3f2012-01-04 20:05:49 +00001430 ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001431 device->path.string(), device->identifier.name.string(), device->id,
1432 device->fd, device->classes);
1433
Jeff Brown33bbfd22011-02-24 20:55:35 -08001434 if (device->id == mBuiltInKeyboardId) {
Steve Block8564c8d2012-01-05 23:22:43 +00001435 ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001436 device->path.string(), mBuiltInKeyboardId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001437 mBuiltInKeyboardId = NO_BUILT_IN_KEYBOARD;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001438 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001439
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001440 if (!device->isVirtual()) {
1441 if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) {
1442 ALOGW("Could not remove device fd from epoll instance. errno=%d", errno);
1443 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001444 }
1445
Michael Wrightac6c78b2013-07-17 13:21:45 -07001446 releaseControllerNumberLocked(device);
1447
Jeff Brown93fa9b32011-06-14 17:09:25 -07001448 mDevices.removeItem(device->id);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001449 device->close();
1450
Jeff Brown8e9d4432011-03-12 19:46:59 -08001451 // Unlink for opening devices list if it is present.
1452 Device* pred = NULL;
1453 bool found = false;
1454 for (Device* entry = mOpeningDevices; entry != NULL; ) {
1455 if (entry == device) {
1456 found = true;
1457 break;
1458 }
1459 pred = entry;
1460 entry = entry->next;
1461 }
1462 if (found) {
1463 // Unlink the device from the opening devices list then delete it.
1464 // We don't need to tell the client that the device was closed because
1465 // it does not even know it was opened in the first place.
Steve Block6215d3f2012-01-04 20:05:49 +00001466 ALOGI("Device %s was immediately closed after opening.", device->path.string());
Jeff Brown8e9d4432011-03-12 19:46:59 -08001467 if (pred) {
1468 pred->next = device->next;
1469 } else {
1470 mOpeningDevices = device->next;
1471 }
1472 delete device;
1473 } else {
1474 // Link into closing devices list.
1475 // The device will be deleted later after we have informed the client.
1476 device->next = mClosingDevices;
1477 mClosingDevices = device;
1478 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001479}
1480
Jeff Brown93fa9b32011-06-14 17:09:25 -07001481status_t EventHub::readNotifyLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 int res;
1483 char devname[PATH_MAX];
1484 char *filename;
1485 char event_buf[512];
1486 int event_size;
1487 int event_pos = 0;
1488 struct inotify_event *event;
1489
Steve Block71f2cf12011-10-20 11:56:00 +01001490 ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001491 res = read(mINotifyFd, event_buf, sizeof(event_buf));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 if(res < (int)sizeof(*event)) {
1493 if(errno == EINTR)
1494 return 0;
Steve Block8564c8d2012-01-05 23:22:43 +00001495 ALOGW("could not get event, %s\n", strerror(errno));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001496 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 }
1498 //printf("got %d bytes of event information\n", res);
1499
Jeff Brown90655042010-12-02 13:50:46 -08001500 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 filename = devname + strlen(devname);
1502 *filename++ = '/';
1503
1504 while(res >= (int)sizeof(*event)) {
1505 event = (struct inotify_event *)(event_buf + event_pos);
1506 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1507 if(event->len) {
1508 strcpy(filename, event->name);
1509 if(event->mask & IN_CREATE) {
Jeff Brown93fa9b32011-06-14 17:09:25 -07001510 openDeviceLocked(devname);
1511 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00001512 ALOGI("Removing device '%s' due to inotify event\n", devname);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001513 closeDeviceByPathLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 }
1515 }
1516 event_size = sizeof(*event) + event->len;
1517 res -= event_size;
1518 event_pos += event_size;
1519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 return 0;
1521}
1522
Jeff Brown93fa9b32011-06-14 17:09:25 -07001523status_t EventHub::scanDirLocked(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524{
1525 char devname[PATH_MAX];
1526 char *filename;
1527 DIR *dir;
1528 struct dirent *de;
1529 dir = opendir(dirname);
1530 if(dir == NULL)
1531 return -1;
1532 strcpy(devname, dirname);
1533 filename = devname + strlen(devname);
1534 *filename++ = '/';
1535 while((de = readdir(dir))) {
1536 if(de->d_name[0] == '.' &&
1537 (de->d_name[1] == '\0' ||
1538 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1539 continue;
1540 strcpy(filename, de->d_name);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001541 openDeviceLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 }
1543 closedir(dir);
1544 return 0;
1545}
1546
Jeff Brown93fa9b32011-06-14 17:09:25 -07001547void EventHub::requestReopenDevices() {
Steve Block71f2cf12011-10-20 11:56:00 +01001548 ALOGV("requestReopenDevices() called");
Jeff Brown93fa9b32011-06-14 17:09:25 -07001549
1550 AutoMutex _l(mLock);
1551 mNeedToReopenDevices = true;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001552}
1553
Jeff Brownf2f487182010-10-01 17:46:21 -07001554void EventHub::dump(String8& dump) {
1555 dump.append("Event Hub State:\n");
1556
1557 { // acquire lock
1558 AutoMutex _l(mLock);
1559
Jeff Brown90655042010-12-02 13:50:46 -08001560 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f487182010-10-01 17:46:21 -07001561
1562 dump.append(INDENT "Devices:\n");
1563
Jeff Brown93fa9b32011-06-14 17:09:25 -07001564 for (size_t i = 0; i < mDevices.size(); i++) {
1565 const Device* device = mDevices.valueAt(i);
1566 if (mBuiltInKeyboardId == device->id) {
1567 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1568 device->id, device->identifier.name.string());
1569 } else {
1570 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1571 device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001572 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001573 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1574 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
Jeff Browne38fdfa2012-04-06 14:51:01 -07001575 dump.appendFormat(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.string());
Jeff Brown93fa9b32011-06-14 17:09:25 -07001576 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
Michael Wrightac6c78b2013-07-17 13:21:45 -07001577 dump.appendFormat(INDENT3 "ControllerNumber: %d\n", device->controllerNumber);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001578 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1579 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1580 "product=0x%04x, version=0x%04x\n",
1581 device->identifier.bus, device->identifier.vendor,
1582 device->identifier.product, device->identifier.version);
1583 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
1584 device->keyMap.keyLayoutFile.string());
1585 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
1586 device->keyMap.keyCharacterMapFile.string());
1587 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1588 device->configurationFile.string());
Jeff Brown61c08242012-04-19 11:14:33 -07001589 dump.appendFormat(INDENT3 "HaveKeyboardLayoutOverlay: %s\n",
1590 toString(device->overlayKeyMap != NULL));
Jeff Brownf2f487182010-10-01 17:46:21 -07001591 }
1592 } // release lock
1593}
1594
Jeff Brown89ef0722011-08-10 16:25:21 -07001595void EventHub::monitor() {
1596 // Acquire and release the lock to ensure that the event hub has not deadlocked.
1597 mLock.lock();
1598 mLock.unlock();
1599}
1600
1601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602}; // namespace android