blob: 094096e3ea39a349be20575753ac0445b95c46d6 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001//
2// Copyright 2005 The Android Open Source Project
3//
4// Handle events, like key input and vsync.
5//
6// The goal is to provide an optimized solution for Linux, not an
7// implementation that works well across all platforms. We expect
8// events to arrive on file descriptors, so that we can use a select()
9// select() call to sleep.
10//
11// We can't select() on anything but network sockets in Windows, so we
12// provide an alternative implementation of waitEvent for that platform.
13//
14#define LOG_TAG "EventHub"
15
16//#define LOG_NDEBUG 0
17
18#include <ui/EventHub.h>
Dianne Hackbornc5917362009-08-04 05:49:43 -070019#include <ui/KeycodeLabels.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <hardware_legacy/power.h>
21
22#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/Log.h>
24#include <utils/Timers.h>
Mathias Agopiane0c32202009-05-31 19:13:00 -070025#include <utils/threads.h>
Mathias Agopiane0c32202009-05-31 19:13:00 -070026#include <utils/Errors.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <stdlib.h>
29#include <stdio.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <memory.h>
33#include <errno.h>
34#include <assert.h>
35
36#include "KeyLayoutMap.h"
37
38#include <string.h>
39#include <stdint.h>
40#include <dirent.h>
41#ifdef HAVE_INOTIFY
42# include <sys/inotify.h>
43#endif
44#ifdef HAVE_ANDROID_OS
45# include <sys/limits.h> /* not part of Linux */
46#endif
47#include <sys/poll.h>
48#include <sys/ioctl.h>
49
50/* this macro is used to tell if "bit" is set in "array"
51 * it selects a byte from the array, and does a boolean AND
52 * operation with a byte that only has the relevant bit set.
53 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
54 */
55#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
56
Jeff Brown8575a872010-06-30 16:10:35 -070057/* this macro computes the number of bytes needed to represent a bit array of the specified size */
58#define sizeof_bit_array(bits) ((bits + 7) / 8)
59
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#define ID_MASK 0x0000ffff
61#define SEQ_MASK 0x7fff0000
62#define SEQ_SHIFT 16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Dianne Hackbornc5917362009-08-04 05:49:43 -070064#ifndef ABS_MT_TOUCH_MAJOR
65#define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */
66#endif
67
68#ifndef ABS_MT_POSITION_X
69#define ABS_MT_POSITION_X 0x35 /* Center X ellipse position */
70#endif
71
72#ifndef ABS_MT_POSITION_Y
73#define ABS_MT_POSITION_Y 0x36 /* Center Y ellipse position */
74#endif
75
Jeff Brown2806e382010-10-01 17:46:21 -070076#define INDENT " "
77#define INDENT2 " "
78#define INDENT3 " "
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080namespace android {
81
82static const char *WAKE_LOCK_ID = "KeyEvents";
83static const char *device_path = "/dev/input";
84
85/* return the larger integer */
86static inline int max(int v1, int v2)
87{
88 return (v1 > v2) ? v1 : v2;
89}
90
Jeff Brown2806e382010-10-01 17:46:21 -070091static inline const char* toString(bool value) {
92 return value ? "true" : "false";
93}
94
Iliyan Malchev34193b32009-08-06 14:50:08 -070095EventHub::device_t::device_t(int32_t _id, const char* _path, const char* name)
96 : id(_id), path(_path), name(name), classes(0)
Jens Gulin7dcaa582010-06-22 22:21:57 +020097 , keyBitmask(NULL), layoutMap(new KeyLayoutMap()), fd(-1), next(NULL) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098}
99
100EventHub::device_t::~device_t() {
101 delete [] keyBitmask;
102 delete layoutMap;
103}
104
105EventHub::EventHub(void)
106 : mError(NO_INIT), mHaveFirstKeyboard(false), mFirstKeyboardId(0)
107 , mDevicesById(0), mNumDevicesById(0)
108 , mOpeningDevices(0), mClosingDevices(0)
Mike Lockwoodb4411062009-07-16 11:11:18 -0400109 , mDevices(0), mFDs(0), mFDCount(0), mOpened(false)
Jeff Brown82763072010-08-17 16:48:25 -0700110 , mInputBufferIndex(0), mInputBufferCount(0), mInputDeviceIndex(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111{
112 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
113#ifdef EV_SW
114 memset(mSwitches, 0, sizeof(mSwitches));
115#endif
116}
117
118/*
119 * Clean up.
120 */
121EventHub::~EventHub(void)
122{
123 release_wake_lock(WAKE_LOCK_ID);
124 // we should free stuff here...
125}
126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127status_t EventHub::errorCheck() const
128{
129 return mError;
130}
131
132String8 EventHub::getDeviceName(int32_t deviceId) const
133{
134 AutoMutex _l(mLock);
Jeff Brown2806e382010-10-01 17:46:21 -0700135 device_t* device = getDeviceLocked(deviceId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 if (device == NULL) return String8();
137 return device->name;
138}
139
140uint32_t EventHub::getDeviceClasses(int32_t deviceId) const
141{
142 AutoMutex _l(mLock);
Jeff Brown2806e382010-10-01 17:46:21 -0700143 device_t* device = getDeviceLocked(deviceId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 if (device == NULL) return 0;
145 return device->classes;
146}
147
Jeff Browne57e8952010-07-23 21:28:06 -0700148status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
149 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown38a7fab2010-08-30 03:02:23 -0700150 outAxisInfo->clear();
Jeff Browne57e8952010-07-23 21:28:06 -0700151
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 AutoMutex _l(mLock);
Jeff Brown2806e382010-10-01 17:46:21 -0700153 device_t* device = getDeviceLocked(deviceId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 if (device == NULL) return -1;
155
156 struct input_absinfo info;
157
Jens Gulin7dcaa582010-06-22 22:21:57 +0200158 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Jeff Browne57e8952010-07-23 21:28:06 -0700159 LOGW("Error reading absolute controller %d for device %s fd %d\n",
Jens Gulin7dcaa582010-06-22 22:21:57 +0200160 axis, device->name.string(), device->fd);
Jeff Browne57e8952010-07-23 21:28:06 -0700161 return -errno;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 }
Jeff Browne57e8952010-07-23 21:28:06 -0700163
164 if (info.minimum != info.maximum) {
165 outAxisInfo->valid = true;
166 outAxisInfo->minValue = info.minimum;
167 outAxisInfo->maxValue = info.maximum;
168 outAxisInfo->flat = info.flat;
169 outAxisInfo->fuzz = info.fuzz;
170 }
171 return OK;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172}
173
Jeff Browne57e8952010-07-23 21:28:06 -0700174int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Browne839a582010-04-22 18:58:52 -0700175 if (scanCode >= 0 && scanCode <= KEY_MAX) {
176 AutoMutex _l(mLock);
177
Jeff Brown2806e382010-10-01 17:46:21 -0700178 device_t* device = getDeviceLocked(deviceId);
Jeff Browne57e8952010-07-23 21:28:06 -0700179 if (device != NULL) {
180 return getScanCodeStateLocked(device, scanCode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 }
182 }
Jeff Brown5c1ed842010-07-14 18:48:53 -0700183 return AKEY_STATE_UNKNOWN;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184}
185
Jeff Browne839a582010-04-22 18:58:52 -0700186int32_t EventHub::getScanCodeStateLocked(device_t* device, int32_t scanCode) const {
Jeff Brown8575a872010-06-30 16:10:35 -0700187 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
Jeff Browne839a582010-04-22 18:58:52 -0700188 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulin7dcaa582010-06-22 22:21:57 +0200189 if (ioctl(device->fd,
Jeff Browne839a582010-04-22 18:58:52 -0700190 EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
Jeff Brown5c1ed842010-07-14 18:48:53 -0700191 return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Browne839a582010-04-22 18:58:52 -0700192 }
Jeff Brown5c1ed842010-07-14 18:48:53 -0700193 return AKEY_STATE_UNKNOWN;
Jeff Browne839a582010-04-22 18:58:52 -0700194}
195
Jeff Browne57e8952010-07-23 21:28:06 -0700196int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
197 AutoMutex _l(mLock);
Jeff Browne839a582010-04-22 18:58:52 -0700198
Jeff Brown2806e382010-10-01 17:46:21 -0700199 device_t* device = getDeviceLocked(deviceId);
Jeff Browne57e8952010-07-23 21:28:06 -0700200 if (device != NULL) {
201 return getKeyCodeStateLocked(device, keyCode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 }
Jeff Brown5c1ed842010-07-14 18:48:53 -0700203 return AKEY_STATE_UNKNOWN;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204}
205
Jeff Browne839a582010-04-22 18:58:52 -0700206int32_t EventHub::getKeyCodeStateLocked(device_t* device, int32_t keyCode) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 Vector<int32_t> scanCodes;
Jeff Browne839a582010-04-22 18:58:52 -0700208 device->layoutMap->findScancodes(keyCode, &scanCodes);
209
Jeff Brown8575a872010-06-30 16:10:35 -0700210 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulin7dcaa582010-06-22 22:21:57 +0200212 if (ioctl(device->fd, EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 #if 0
214 for (size_t i=0; i<=KEY_MAX; i++) {
215 LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask));
216 }
217 #endif
218 const size_t N = scanCodes.size();
219 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
220 int32_t sc = scanCodes.itemAt(i);
221 //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask));
222 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) {
Jeff Brown5c1ed842010-07-14 18:48:53 -0700223 return AKEY_STATE_DOWN;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 }
225 }
Jeff Brown5c1ed842010-07-14 18:48:53 -0700226 return AKEY_STATE_UP;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 }
Jeff Brown5c1ed842010-07-14 18:48:53 -0700228 return AKEY_STATE_UNKNOWN;
Jeff Browne839a582010-04-22 18:58:52 -0700229}
230
Jeff Browne57e8952010-07-23 21:28:06 -0700231int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Browne839a582010-04-22 18:58:52 -0700232#ifdef EV_SW
233 if (sw >= 0 && sw <= SW_MAX) {
234 AutoMutex _l(mLock);
235
Jeff Brown2806e382010-10-01 17:46:21 -0700236 device_t* device = getDeviceLocked(deviceId);
Jeff Browne57e8952010-07-23 21:28:06 -0700237 if (device != NULL) {
238 return getSwitchStateLocked(device, sw);
Jeff Browne839a582010-04-22 18:58:52 -0700239 }
Jeff Browne839a582010-04-22 18:58:52 -0700240 }
241#endif
Jeff Brown5c1ed842010-07-14 18:48:53 -0700242 return AKEY_STATE_UNKNOWN;
Jeff Browne839a582010-04-22 18:58:52 -0700243}
244
245int32_t EventHub::getSwitchStateLocked(device_t* device, int32_t sw) const {
Jeff Brown8575a872010-06-30 16:10:35 -0700246 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
Jeff Browne839a582010-04-22 18:58:52 -0700247 memset(sw_bitmask, 0, sizeof(sw_bitmask));
Jens Gulin7dcaa582010-06-22 22:21:57 +0200248 if (ioctl(device->fd,
Jeff Browne839a582010-04-22 18:58:52 -0700249 EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
Jeff Brown5c1ed842010-07-14 18:48:53 -0700250 return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Browne839a582010-04-22 18:58:52 -0700251 }
Jeff Brown5c1ed842010-07-14 18:48:53 -0700252 return AKEY_STATE_UNKNOWN;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253}
254
Jeff Browne57e8952010-07-23 21:28:06 -0700255bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
256 const int32_t* keyCodes, uint8_t* outFlags) const {
257 AutoMutex _l(mLock);
258
Jeff Brown2806e382010-10-01 17:46:21 -0700259 device_t* device = getDeviceLocked(deviceId);
Jeff Browne57e8952010-07-23 21:28:06 -0700260 if (device != NULL) {
261 return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags);
262 }
263 return false;
264}
265
266bool EventHub::markSupportedKeyCodesLocked(device_t* device, size_t numCodes,
267 const int32_t* keyCodes, uint8_t* outFlags) const {
268 if (device->layoutMap == NULL || device->keyBitmask == NULL) {
269 return false;
270 }
271
272 Vector<int32_t> scanCodes;
273 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
274 scanCodes.clear();
275
276 status_t err = device->layoutMap->findScancodes(keyCodes[codeIndex], &scanCodes);
277 if (! err) {
278 // check the possible scan codes identified by the layout map against the
279 // map of codes actually emitted by the driver
280 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
281 if (test_bit(scanCodes[sc], device->keyBitmask)) {
282 outFlags[codeIndex] = 1;
283 break;
284 }
285 }
286 }
287 }
288 return true;
289}
290
Dianne Hackbornc968c3a2009-07-14 12:06:54 -0700291status_t EventHub::scancodeToKeycode(int32_t deviceId, int scancode,
292 int32_t* outKeycode, uint32_t* outFlags) const
293{
294 AutoMutex _l(mLock);
Jeff Brown2806e382010-10-01 17:46:21 -0700295 device_t* device = getDeviceLocked(deviceId);
Dianne Hackbornc968c3a2009-07-14 12:06:54 -0700296
297 if (device != NULL && device->layoutMap != NULL) {
298 status_t err = device->layoutMap->map(scancode, outKeycode, outFlags);
299 if (err == NO_ERROR) {
300 return NO_ERROR;
301 }
302 }
303
304 if (mHaveFirstKeyboard) {
Jeff Brown2806e382010-10-01 17:46:21 -0700305 device = getDeviceLocked(mFirstKeyboardId);
Dianne Hackbornc968c3a2009-07-14 12:06:54 -0700306
307 if (device != NULL && device->layoutMap != NULL) {
308 status_t err = device->layoutMap->map(scancode, outKeycode, outFlags);
309 if (err == NO_ERROR) {
310 return NO_ERROR;
311 }
312 }
313 }
314
315 *outKeycode = 0;
316 *outFlags = 0;
317 return NAME_NOT_FOUND;
318}
319
Mike Lockwoodb4411062009-07-16 11:11:18 -0400320void EventHub::addExcludedDevice(const char* deviceName)
321{
Jeff Brown2806e382010-10-01 17:46:21 -0700322 AutoMutex _l(mLock);
323
Mike Lockwoodb4411062009-07-16 11:11:18 -0400324 String8 name(deviceName);
325 mExcludedDevices.push_back(name);
326}
327
Jeff Brown2806e382010-10-01 17:46:21 -0700328EventHub::device_t* EventHub::getDeviceLocked(int32_t deviceId) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329{
330 if (deviceId == 0) deviceId = mFirstKeyboardId;
331 int32_t id = deviceId & ID_MASK;
332 if (id >= mNumDevicesById || id < 0) return NULL;
333 device_t* dev = mDevicesById[id].device;
Dianne Hackbornc3aa00b2009-03-25 16:21:55 -0700334 if (dev == NULL) return NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 if (dev->id == deviceId) {
336 return dev;
337 }
338 return NULL;
339}
340
Jeff Browne57e8952010-07-23 21:28:06 -0700341bool EventHub::getEvent(RawEvent* outEvent)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342{
Jeff Browne57e8952010-07-23 21:28:06 -0700343 outEvent->deviceId = 0;
344 outEvent->type = 0;
345 outEvent->scanCode = 0;
346 outEvent->keyCode = 0;
347 outEvent->flags = 0;
348 outEvent->value = 0;
349 outEvent->when = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 // Note that we only allow one caller to getEvent(), so don't need
352 // to do locking here... only when adding/removing devices.
Mike Lockwoodb4411062009-07-16 11:11:18 -0400353
354 if (!mOpened) {
355 mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR;
356 mOpened = true;
357 }
358
Jeff Brown82763072010-08-17 16:48:25 -0700359 for (;;) {
360 // Report any devices that had last been added/removed.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361 if (mClosingDevices != NULL) {
362 device_t* device = mClosingDevices;
363 LOGV("Reporting device closed: id=0x%x, name=%s\n",
364 device->id, device->path.string());
365 mClosingDevices = device->next;
Jeff Browne57e8952010-07-23 21:28:06 -0700366 if (device->id == mFirstKeyboardId) {
367 outEvent->deviceId = 0;
368 } else {
369 outEvent->deviceId = device->id;
370 }
371 outEvent->type = DEVICE_REMOVED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372 delete device;
373 return true;
374 }
Jeff Browne57e8952010-07-23 21:28:06 -0700375
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 if (mOpeningDevices != NULL) {
377 device_t* device = mOpeningDevices;
378 LOGV("Reporting device opened: id=0x%x, name=%s\n",
379 device->id, device->path.string());
380 mOpeningDevices = device->next;
Jeff Browne57e8952010-07-23 21:28:06 -0700381 if (device->id == mFirstKeyboardId) {
382 outEvent->deviceId = 0;
383 } else {
384 outEvent->deviceId = device->id;
385 }
386 outEvent->type = DEVICE_ADDED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 return true;
388 }
389
Jeff Brown82763072010-08-17 16:48:25 -0700390 // Grab the next input event.
391 for (;;) {
392 // Consume buffered input events, if any.
393 if (mInputBufferIndex < mInputBufferCount) {
394 const struct input_event& iev = mInputBufferData[mInputBufferIndex++];
395 const device_t* device = mDevices[mInputDeviceIndex];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396
Jeff Brown82763072010-08-17 16:48:25 -0700397 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, v=%d", device->path.string(),
398 (int) iev.time.tv_sec, (int) iev.time.tv_usec, iev.type, iev.code, iev.value);
399 if (device->id == mFirstKeyboardId) {
400 outEvent->deviceId = 0;
401 } else {
402 outEvent->deviceId = device->id;
403 }
404 outEvent->type = iev.type;
405 outEvent->scanCode = iev.code;
406 if (iev.type == EV_KEY) {
407 status_t err = device->layoutMap->map(iev.code,
408 & outEvent->keyCode, & outEvent->flags);
409 LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
410 iev.code, outEvent->keyCode, outEvent->flags, err);
411 if (err != 0) {
412 outEvent->keyCode = AKEYCODE_UNKNOWN;
413 outEvent->flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 }
Jeff Brown82763072010-08-17 16:48:25 -0700415 } else {
416 outEvent->keyCode = iev.code;
417 }
418 outEvent->value = iev.value;
419
420 // Use an event timestamp in the same timebase as
421 // java.lang.System.nanoTime() and android.os.SystemClock.uptimeMillis()
422 // as expected by the rest of the system.
423 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
424 return true;
425 }
426
427 // Finish reading all events from devices identified in previous poll().
428 // This code assumes that mInputDeviceIndex is initially 0 and that the
429 // revents member of pollfd is initialized to 0 when the device is first added.
430 // Since mFDs[0] is used for inotify, we process regular events starting at index 1.
431 mInputDeviceIndex += 1;
432 if (mInputDeviceIndex >= mFDCount) {
433 mInputDeviceIndex = 0;
434 break;
435 }
436
437 const struct pollfd &pfd = mFDs[mInputDeviceIndex];
438 if (pfd.revents & POLLIN) {
439 int32_t readSize = read(pfd.fd, mInputBufferData,
440 sizeof(struct input_event) * INPUT_BUFFER_SIZE);
441 if (readSize < 0) {
442 if (errno != EAGAIN && errno != EINTR) {
443 LOGW("could not get event (errno=%d)", errno);
444 }
445 } else if ((readSize % sizeof(struct input_event)) != 0) {
446 LOGE("could not get event (wrong size: %d)", readSize);
447 } else {
448 mInputBufferCount = readSize / sizeof(struct input_event);
449 mInputBufferIndex = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450 }
451 }
452 }
Jeff Brown82763072010-08-17 16:48:25 -0700453
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454 // read_notify() will modify mFDs and mFDCount, so this must be done after
455 // processing all other events.
456 if(mFDs[0].revents & POLLIN) {
457 read_notify(mFDs[0].fd);
458 }
Jeff Brown82763072010-08-17 16:48:25 -0700459
460 // Poll for events. Mind the wake lock dance!
461 // We hold a wake lock at all times except during poll(). This works due to some
462 // subtle choreography. When a device driver has pending (unread) events, it acquires
463 // a kernel wake lock. However, once the last pending event has been read, the device
464 // driver will release the kernel wake lock. To prevent the system from going to sleep
465 // when this happens, the EventHub holds onto its own user wake lock while the client
466 // is processing events. Thus the system can only sleep if there are no events
467 // pending or currently being processed.
468 release_wake_lock(WAKE_LOCK_ID);
469
470 int pollResult = poll(mFDs, mFDCount, -1);
471
472 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
473
474 if (pollResult <= 0) {
475 if (errno != EINTR) {
476 LOGW("select failed (errno=%d)\n", errno);
477 usleep(100000);
478 }
479 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480 }
481}
482
483/*
484 * Open the platform-specific input device.
485 */
486bool EventHub::openPlatformInput(void)
487{
488 /*
489 * Open platform-specific input device(s).
490 */
491 int res;
492
493 mFDCount = 1;
494 mFDs = (pollfd *)calloc(1, sizeof(mFDs[0]));
495 mDevices = (device_t **)calloc(1, sizeof(mDevices[0]));
496 mFDs[0].events = POLLIN;
Jeff Brown82763072010-08-17 16:48:25 -0700497 mFDs[0].revents = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498 mDevices[0] = NULL;
499#ifdef HAVE_INOTIFY
500 mFDs[0].fd = inotify_init();
501 res = inotify_add_watch(mFDs[0].fd, device_path, IN_DELETE | IN_CREATE);
502 if(res < 0) {
503 LOGE("could not add watch for %s, %s\n", device_path, strerror(errno));
504 }
505#else
506 /*
507 * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd.
508 * We allocate space for it and set it to something invalid.
509 */
510 mFDs[0].fd = -1;
511#endif
512
513 res = scan_dir(device_path);
514 if(res < 0) {
515 LOGE("scan dir failed for %s\n", device_path);
516 //open_device("/dev/input/event0");
517 }
518
519 return true;
520}
521
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522// ----------------------------------------------------------------------------
523
Jeff Brown8575a872010-06-30 16:10:35 -0700524static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
525 const uint8_t* end = array + endIndex;
526 array += startIndex;
527 while (array != end) {
528 if (*(array++) != 0) {
529 return true;
530 }
531 }
532 return false;
533}
534
535static const int32_t GAMEPAD_KEYCODES[] = {
536 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
537 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
538 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
539 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
540 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
541 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE
542};
543
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544int EventHub::open_device(const char *deviceName)
545{
546 int version;
547 int fd;
548 struct pollfd *new_mFDs;
549 device_t **new_devices;
550 char **new_device_names;
551 char name[80];
552 char location[80];
553 char idstr[80];
554 struct input_id id;
555
556 LOGV("Opening device: %s", deviceName);
557
558 AutoMutex _l(mLock);
Nick Pellyc81bb202010-01-20 19:36:49 -0800559
Nick Pelly1b5cf322010-01-26 10:27:15 -0800560 fd = open(deviceName, O_RDWR);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561 if(fd < 0) {
562 LOGE("could not open %s, %s\n", deviceName, strerror(errno));
563 return -1;
564 }
565
566 if(ioctl(fd, EVIOCGVERSION, &version)) {
567 LOGE("could not get driver version for %s, %s\n", deviceName, strerror(errno));
568 return -1;
569 }
570 if(ioctl(fd, EVIOCGID, &id)) {
571 LOGE("could not get driver id for %s, %s\n", deviceName, strerror(errno));
572 return -1;
573 }
574 name[sizeof(name) - 1] = '\0';
575 location[sizeof(location) - 1] = '\0';
576 idstr[sizeof(idstr) - 1] = '\0';
577 if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
578 //fprintf(stderr, "could not get device name for %s, %s\n", deviceName, strerror(errno));
579 name[0] = '\0';
580 }
Mike Lockwood24a7e042009-07-17 00:10:10 -0400581
582 // check to see if the device is on our excluded list
583 List<String8>::iterator iter = mExcludedDevices.begin();
584 List<String8>::iterator end = mExcludedDevices.end();
585 for ( ; iter != end; iter++) {
586 const char* test = *iter;
587 if (strcmp(name, test) == 0) {
588 LOGI("ignoring event id %s driver %s\n", deviceName, test);
589 close(fd);
Mike Lockwood24a7e042009-07-17 00:10:10 -0400590 return -1;
591 }
592 }
593
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 if(ioctl(fd, EVIOCGPHYS(sizeof(location) - 1), &location) < 1) {
595 //fprintf(stderr, "could not get location for %s, %s\n", deviceName, strerror(errno));
596 location[0] = '\0';
597 }
598 if(ioctl(fd, EVIOCGUNIQ(sizeof(idstr) - 1), &idstr) < 1) {
599 //fprintf(stderr, "could not get idstring for %s, %s\n", deviceName, strerror(errno));
600 idstr[0] = '\0';
601 }
602
Jeff Brown82763072010-08-17 16:48:25 -0700603 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
604 LOGE("Error %d making device file descriptor non-blocking.", errno);
605 close(fd);
606 return -1;
607 }
608
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609 int devid = 0;
610 while (devid < mNumDevicesById) {
611 if (mDevicesById[devid].device == NULL) {
612 break;
613 }
614 devid++;
615 }
616 if (devid >= mNumDevicesById) {
617 device_ent* new_devids = (device_ent*)realloc(mDevicesById,
618 sizeof(mDevicesById[0]) * (devid + 1));
619 if (new_devids == NULL) {
620 LOGE("out of memory");
621 return -1;
622 }
623 mDevicesById = new_devids;
624 mNumDevicesById = devid+1;
625 mDevicesById[devid].device = NULL;
626 mDevicesById[devid].seq = 0;
627 }
628
629 mDevicesById[devid].seq = (mDevicesById[devid].seq+(1<<SEQ_SHIFT))&SEQ_MASK;
630 if (mDevicesById[devid].seq == 0) {
631 mDevicesById[devid].seq = 1<<SEQ_SHIFT;
632 }
633
634 new_mFDs = (pollfd*)realloc(mFDs, sizeof(mFDs[0]) * (mFDCount + 1));
635 new_devices = (device_t**)realloc(mDevices, sizeof(mDevices[0]) * (mFDCount + 1));
636 if (new_mFDs == NULL || new_devices == NULL) {
637 LOGE("out of memory");
638 return -1;
639 }
640 mFDs = new_mFDs;
641 mDevices = new_devices;
642
643#if 0
644 LOGI("add device %d: %s\n", mFDCount, deviceName);
645 LOGI(" bus: %04x\n"
646 " vendor %04x\n"
647 " product %04x\n"
648 " version %04x\n",
649 id.bustype, id.vendor, id.product, id.version);
650 LOGI(" name: \"%s\"\n", name);
651 LOGI(" location: \"%s\"\n"
652 " id: \"%s\"\n", location, idstr);
653 LOGI(" version: %d.%d.%d\n",
654 version >> 16, (version >> 8) & 0xff, version & 0xff);
655#endif
656
Iliyan Malchev34193b32009-08-06 14:50:08 -0700657 device_t* device = new device_t(devid|mDevicesById[devid].seq, deviceName, name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658 if (device == NULL) {
659 LOGE("out of memory");
660 return -1;
661 }
662
Jens Gulin7dcaa582010-06-22 22:21:57 +0200663 device->fd = fd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664 mFDs[mFDCount].fd = fd;
665 mFDs[mFDCount].events = POLLIN;
Jeff Brown82763072010-08-17 16:48:25 -0700666 mFDs[mFDCount].revents = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667
Jeff Brown8575a872010-06-30 16:10:35 -0700668 // Figure out the kinds of events the device reports.
Dianne Hackbornc5917362009-08-04 05:49:43 -0700669
Jeff Brown8575a872010-06-30 16:10:35 -0700670 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671 memset(key_bitmask, 0, sizeof(key_bitmask));
Jeff Brown8575a872010-06-30 16:10:35 -0700672
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673 LOGV("Getting keys...");
674 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) >= 0) {
675 //LOGI("MAP\n");
Jeff Brown8575a872010-06-30 16:10:35 -0700676 //for (int i = 0; i < sizeof(key_bitmask); i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677 // LOGI("%d: 0x%02x\n", i, key_bitmask[i]);
678 //}
Jeff Brown8575a872010-06-30 16:10:35 -0700679
680 // See if this is a keyboard. Ignore everything in the button range except for
681 // gamepads which are also considered keyboards.
682 if (containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC))
683 || containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_GAMEPAD),
684 sizeof_bit_array(BTN_DIGI))
685 || containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK),
686 sizeof_bit_array(KEY_MAX + 1))) {
687 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
688
Dianne Hackbornc5917362009-08-04 05:49:43 -0700689 device->keyBitmask = new uint8_t[sizeof(key_bitmask)];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 if (device->keyBitmask != NULL) {
691 memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask));
692 } else {
693 delete device;
694 LOGE("out of memory allocating key bitmask");
695 return -1;
696 }
697 }
698 }
Dianne Hackbornc5917362009-08-04 05:49:43 -0700699
Jeff Brown8575a872010-06-30 16:10:35 -0700700 // See if this is a trackball (or mouse).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701 if (test_bit(BTN_MOUSE, key_bitmask)) {
Jeff Brown8575a872010-06-30 16:10:35 -0700702 uint8_t rel_bitmask[sizeof_bit_array(REL_MAX + 1)];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703 memset(rel_bitmask, 0, sizeof(rel_bitmask));
704 LOGV("Getting relative controllers...");
Jeff Brown8575a872010-06-30 16:10:35 -0700705 if (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask) >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706 if (test_bit(REL_X, rel_bitmask) && test_bit(REL_Y, rel_bitmask)) {
Jeff Browne839a582010-04-22 18:58:52 -0700707 device->classes |= INPUT_DEVICE_CLASS_TRACKBALL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800708 }
709 }
710 }
Jeff Brown8575a872010-06-30 16:10:35 -0700711
712 // See if this is a touch pad.
713 uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)];
Dianne Hackbornc5917362009-08-04 05:49:43 -0700714 memset(abs_bitmask, 0, sizeof(abs_bitmask));
715 LOGV("Getting absolute controllers...");
Jeff Brown8575a872010-06-30 16:10:35 -0700716 if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) >= 0) {
717 // Is this a new modern multi-touch driver?
Jeff Brown38a7fab2010-08-30 03:02:23 -0700718 if (test_bit(ABS_MT_POSITION_X, abs_bitmask)
Jeff Brown8575a872010-06-30 16:10:35 -0700719 && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) {
720 device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN | INPUT_DEVICE_CLASS_TOUCHSCREEN_MT;
721
722 // Is this an old style single-touch driver?
723 } else if (test_bit(BTN_TOUCH, key_bitmask)
724 && test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) {
725 device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN;
726 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727 }
728
729#ifdef EV_SW
730 // figure out the switches this device reports
Jeff Brown8575a872010-06-30 16:10:35 -0700731 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732 memset(sw_bitmask, 0, sizeof(sw_bitmask));
Jeff Browne57e8952010-07-23 21:28:06 -0700733 bool hasSwitches = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734 if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask) >= 0) {
735 for (int i=0; i<EV_SW; i++) {
736 //LOGI("Device 0x%x sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask));
737 if (test_bit(i, sw_bitmask)) {
Jeff Browne57e8952010-07-23 21:28:06 -0700738 hasSwitches = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739 if (mSwitches[i] == 0) {
740 mSwitches[i] = device->id;
741 }
742 }
743 }
744 }
Jeff Browne57e8952010-07-23 21:28:06 -0700745 if (hasSwitches) {
746 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
747 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800748#endif
749
Jeff Browne839a582010-04-22 18:58:52 -0700750 if ((device->classes & INPUT_DEVICE_CLASS_KEYBOARD) != 0) {
Iliyan Malchev34193b32009-08-06 14:50:08 -0700751 char tmpfn[sizeof(name)];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752 char keylayoutFilename[300];
753
754 // a more descriptive name
Iliyan Malchev34193b32009-08-06 14:50:08 -0700755 device->name = name;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756
757 // replace all the spaces with underscores
Iliyan Malchev34193b32009-08-06 14:50:08 -0700758 strcpy(tmpfn, name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759 for (char *p = strchr(tmpfn, ' '); p && *p; p = strchr(tmpfn, ' '))
760 *p = '_';
761
762 // find the .kl file we need for this device
763 const char* root = getenv("ANDROID_ROOT");
764 snprintf(keylayoutFilename, sizeof(keylayoutFilename),
765 "%s/usr/keylayout/%s.kl", root, tmpfn);
766 bool defaultKeymap = false;
767 if (access(keylayoutFilename, R_OK)) {
768 snprintf(keylayoutFilename, sizeof(keylayoutFilename),
769 "%s/usr/keylayout/%s", root, "qwerty.kl");
770 defaultKeymap = true;
771 }
Jeff Brown8575a872010-06-30 16:10:35 -0700772 status_t status = device->layoutMap->load(keylayoutFilename);
773 if (status) {
774 LOGE("Error %d loading key layout.", status);
775 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776
777 // tell the world about the devname (the descriptive name)
Dianne Hackborn71c3eb22010-03-02 17:19:29 -0800778 if (!mHaveFirstKeyboard && !defaultKeymap && strstr(name, "-keypad")) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 // the built-in keyboard has a well-known device ID of 0,
780 // this device better not go away.
781 mHaveFirstKeyboard = true;
782 mFirstKeyboardId = device->id;
Dianne Hackborn71c3eb22010-03-02 17:19:29 -0800783 property_set("hw.keyboards.0.devname", name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784 } else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785 // ensure mFirstKeyboardId is set to -something-.
786 if (mFirstKeyboardId == 0) {
787 mFirstKeyboardId = device->id;
788 }
789 }
790 char propName[100];
Dianne Hackborn71c3eb22010-03-02 17:19:29 -0800791 sprintf(propName, "hw.keyboards.%u.devname", device->id);
Iliyan Malchev34193b32009-08-06 14:50:08 -0700792 property_set(propName, name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793
Dianne Hackbornc5917362009-08-04 05:49:43 -0700794 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brown2806e382010-10-01 17:46:21 -0700795 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Browne839a582010-04-22 18:58:52 -0700796 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackbornc5917362009-08-04 05:49:43 -0700797 }
798
Jeff Brown8575a872010-06-30 16:10:35 -0700799 // See if this device has a DPAD.
Jeff Brown2806e382010-10-01 17:46:21 -0700800 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
801 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
802 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
803 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
804 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Browne839a582010-04-22 18:58:52 -0700805 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackbornc5917362009-08-04 05:49:43 -0700806 }
807
Jeff Brown8575a872010-06-30 16:10:35 -0700808 // See if this device has a gamepad.
809 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES); i++) {
Jeff Brown2806e382010-10-01 17:46:21 -0700810 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brown8575a872010-06-30 16:10:35 -0700811 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
812 break;
813 }
814 }
815
Dianne Hackborn71c3eb22010-03-02 17:19:29 -0800816 LOGI("New keyboard: device->id=0x%x devname='%s' propName='%s' keylayout='%s'\n",
817 device->id, name, propName, keylayoutFilename);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818 }
819
Sean McNeil2e7a5302010-06-23 16:00:37 +0700820 // If the device isn't recognized as something we handle, don't monitor it.
821 if (device->classes == 0) {
822 LOGV("Dropping device %s %p, id = %d\n", deviceName, device, devid);
823 close(fd);
824 delete device;
825 return -1;
826 }
827
Dianne Hackbornc5917362009-08-04 05:49:43 -0700828 LOGI("New device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n",
829 deviceName, name, device->id, mNumDevicesById, mFDCount, fd, device->classes);
830
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831 LOGV("Adding device %s %p at %d, id = %d, classes = 0x%x\n",
832 deviceName, device, mFDCount, devid, device->classes);
833
834 mDevicesById[devid].device = device;
835 device->next = mOpeningDevices;
836 mOpeningDevices = device;
837 mDevices[mFDCount] = device;
838
839 mFDCount++;
840 return 0;
841}
842
Jeff Brown2806e382010-10-01 17:46:21 -0700843bool EventHub::hasKeycodeLocked(device_t* device, int keycode) const
Dianne Hackbornc5917362009-08-04 05:49:43 -0700844{
845 if (device->keyBitmask == NULL || device->layoutMap == NULL) {
846 return false;
847 }
848
849 Vector<int32_t> scanCodes;
850 device->layoutMap->findScancodes(keycode, &scanCodes);
851 const size_t N = scanCodes.size();
852 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
853 int32_t sc = scanCodes.itemAt(i);
854 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
855 return true;
856 }
857 }
858
859 return false;
860}
861
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862int EventHub::close_device(const char *deviceName)
863{
864 AutoMutex _l(mLock);
865
866 int i;
867 for(i = 1; i < mFDCount; i++) {
868 if(strcmp(mDevices[i]->path.string(), deviceName) == 0) {
869 //LOGD("remove device %d: %s\n", i, deviceName);
870 device_t* device = mDevices[i];
Dianne Hackborn39bf9182009-09-01 19:01:50 -0700871
872 LOGI("Removed device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n",
873 device->path.string(), device->name.string(), device->id,
874 mNumDevicesById, mFDCount, mFDs[i].fd, device->classes);
875
876 // Clear this device's entry.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877 int index = (device->id&ID_MASK);
878 mDevicesById[index].device = NULL;
Dianne Hackborn39bf9182009-09-01 19:01:50 -0700879
880 // Close the file descriptor and compact the fd array.
Mike Lockwood07549f92009-08-28 13:29:06 -0700881 close(mFDs[i].fd);
Dianne Hackborn39bf9182009-09-01 19:01:50 -0700882 int count = mFDCount - i - 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883 memmove(mDevices + i, mDevices + i + 1, sizeof(mDevices[0]) * count);
884 memmove(mFDs + i, mFDs + i + 1, sizeof(mFDs[0]) * count);
Dianne Hackborn39bf9182009-09-01 19:01:50 -0700885 mFDCount--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886
887#ifdef EV_SW
888 for (int j=0; j<EV_SW; j++) {
889 if (mSwitches[j] == device->id) {
890 mSwitches[j] = 0;
891 }
892 }
893#endif
894
895 device->next = mClosingDevices;
896 mClosingDevices = device;
897
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898 if (device->id == mFirstKeyboardId) {
899 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
900 device->path.string(), mFirstKeyboardId);
901 mFirstKeyboardId = 0;
Dianne Hackborn71c3eb22010-03-02 17:19:29 -0800902 property_set("hw.keyboards.0.devname", NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903 }
904 // clear the property
905 char propName[100];
Dianne Hackborn71c3eb22010-03-02 17:19:29 -0800906 sprintf(propName, "hw.keyboards.%u.devname", device->id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907 property_set(propName, NULL);
908 return 0;
909 }
910 }
Dianne Hackborn39bf9182009-09-01 19:01:50 -0700911 LOGE("remove device: %s not found\n", deviceName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800912 return -1;
913}
914
915int EventHub::read_notify(int nfd)
916{
917#ifdef HAVE_INOTIFY
918 int res;
919 char devname[PATH_MAX];
920 char *filename;
921 char event_buf[512];
922 int event_size;
923 int event_pos = 0;
924 struct inotify_event *event;
925
Dianne Hackborn39bf9182009-09-01 19:01:50 -0700926 LOGV("EventHub::read_notify nfd: %d\n", nfd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927 res = read(nfd, event_buf, sizeof(event_buf));
928 if(res < (int)sizeof(*event)) {
929 if(errno == EINTR)
930 return 0;
931 LOGW("could not get event, %s\n", strerror(errno));
932 return 1;
933 }
934 //printf("got %d bytes of event information\n", res);
935
936 strcpy(devname, device_path);
937 filename = devname + strlen(devname);
938 *filename++ = '/';
939
940 while(res >= (int)sizeof(*event)) {
941 event = (struct inotify_event *)(event_buf + event_pos);
942 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
943 if(event->len) {
944 strcpy(filename, event->name);
945 if(event->mask & IN_CREATE) {
946 open_device(devname);
947 }
948 else {
949 close_device(devname);
950 }
951 }
952 event_size = sizeof(*event) + event->len;
953 res -= event_size;
954 event_pos += event_size;
955 }
956#endif
957 return 0;
958}
959
960
961int EventHub::scan_dir(const char *dirname)
962{
963 char devname[PATH_MAX];
964 char *filename;
965 DIR *dir;
966 struct dirent *de;
967 dir = opendir(dirname);
968 if(dir == NULL)
969 return -1;
970 strcpy(devname, dirname);
971 filename = devname + strlen(devname);
972 *filename++ = '/';
973 while((de = readdir(dir))) {
974 if(de->d_name[0] == '.' &&
975 (de->d_name[1] == '\0' ||
976 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
977 continue;
978 strcpy(filename, de->d_name);
979 open_device(devname);
980 }
981 closedir(dir);
982 return 0;
983}
984
Jeff Brown2806e382010-10-01 17:46:21 -0700985void EventHub::dump(String8& dump) {
986 dump.append("Event Hub State:\n");
987
988 { // acquire lock
989 AutoMutex _l(mLock);
990
991 dump.appendFormat(INDENT "HaveFirstKeyboard: %s\n", toString(mHaveFirstKeyboard));
992 dump.appendFormat(INDENT "FirstKeyboardId: 0x%x\n", mFirstKeyboardId);
993
994 dump.append(INDENT "Devices:\n");
995
996 for (int i = 0; i < mNumDevicesById; i++) {
997 const device_t* device = mDevicesById[i].device;
998 if (device) {
999 if (mFirstKeyboardId == device->id) {
1000 dump.appendFormat(INDENT2 "0x%x: %s (aka device 0 - first keyboard)\n",
1001 device->id, device->name.string());
1002 } else {
1003 dump.appendFormat(INDENT2 "0x%x: %s\n", device->id, device->name.string());
1004 }
1005 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1006 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
1007 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n", device->keylayoutFilename.string());
1008 }
1009 }
1010 } // release lock
1011}
1012
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013}; // namespace android