blob: 1d38b4ba8ff0379594da6249849555e2ae3f7fbe [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Hackborn0dd7cb42009-08-04 05:49:43 -070019#include <ui/KeycodeLabels.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include <hardware_legacy/power.h>
21
22#include <cutils/properties.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <utils/Log.h>
24#include <utils/Timers.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070025#include <utils/threads.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070026#include <utils/Errors.h>
The Android Open Source Project9066cfe2009-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 Brownfd035822010-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 Project9066cfe2009-03-03 19:31:44 -080060#define ID_MASK 0x0000ffff
61#define SEQ_MASK 0x7fff0000
62#define SEQ_SHIFT 16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
Dianne Hackborn0dd7cb42009-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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076namespace android {
77
78static const char *WAKE_LOCK_ID = "KeyEvents";
79static const char *device_path = "/dev/input";
80
81/* return the larger integer */
82static inline int max(int v1, int v2)
83{
84 return (v1 > v2) ? v1 : v2;
85}
86
Iliyan Malchevfc2ebc42009-08-06 14:50:08 -070087EventHub::device_t::device_t(int32_t _id, const char* _path, const char* name)
88 : id(_id), path(_path), name(name), classes(0)
Jens Gulinc4554b92010-06-22 22:21:57 +020089 , keyBitmask(NULL), layoutMap(new KeyLayoutMap()), fd(-1), next(NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090}
91
92EventHub::device_t::~device_t() {
93 delete [] keyBitmask;
94 delete layoutMap;
95}
96
97EventHub::EventHub(void)
98 : mError(NO_INIT), mHaveFirstKeyboard(false), mFirstKeyboardId(0)
99 , mDevicesById(0), mNumDevicesById(0)
100 , mOpeningDevices(0), mClosingDevices(0)
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400101 , mDevices(0), mFDs(0), mFDCount(0), mOpened(false)
Jeff Browncc2e7172010-08-17 16:48:25 -0700102 , mInputBufferIndex(0), mInputBufferCount(0), mInputDeviceIndex(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103{
104 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
105#ifdef EV_SW
106 memset(mSwitches, 0, sizeof(mSwitches));
107#endif
108}
109
110/*
111 * Clean up.
112 */
113EventHub::~EventHub(void)
114{
115 release_wake_lock(WAKE_LOCK_ID);
116 // we should free stuff here...
117}
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119status_t EventHub::errorCheck() const
120{
121 return mError;
122}
123
124String8 EventHub::getDeviceName(int32_t deviceId) const
125{
126 AutoMutex _l(mLock);
127 device_t* device = getDevice(deviceId);
128 if (device == NULL) return String8();
129 return device->name;
130}
131
132uint32_t EventHub::getDeviceClasses(int32_t deviceId) const
133{
134 AutoMutex _l(mLock);
135 device_t* device = getDevice(deviceId);
136 if (device == NULL) return 0;
137 return device->classes;
138}
139
Jeff Brown6d0fec22010-07-23 21:28:06 -0700140status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
141 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700142 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 AutoMutex _l(mLock);
145 device_t* device = getDevice(deviceId);
146 if (device == NULL) return -1;
147
148 struct input_absinfo info;
149
Jens Gulinc4554b92010-06-22 22:21:57 +0200150 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700151 LOGW("Error reading absolute controller %d for device %s fd %d\n",
Jens Gulinc4554b92010-06-22 22:21:57 +0200152 axis, device->name.string(), device->fd);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700153 return -errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700155
156 if (info.minimum != info.maximum) {
157 outAxisInfo->valid = true;
158 outAxisInfo->minValue = info.minimum;
159 outAxisInfo->maxValue = info.maximum;
160 outAxisInfo->flat = info.flat;
161 outAxisInfo->fuzz = info.fuzz;
162 }
163 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164}
165
Jeff Brown6d0fec22010-07-23 21:28:06 -0700166int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700167 if (scanCode >= 0 && scanCode <= KEY_MAX) {
168 AutoMutex _l(mLock);
169
Jeff Brown6d0fec22010-07-23 21:28:06 -0700170 device_t* device = getDevice(deviceId);
171 if (device != NULL) {
172 return getScanCodeStateLocked(device, scanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 }
174 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700175 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176}
177
Jeff Brown46b9ac02010-04-22 18:58:52 -0700178int32_t EventHub::getScanCodeStateLocked(device_t* device, int32_t scanCode) const {
Jeff Brownfd035822010-06-30 16:10:35 -0700179 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
Jeff Brown46b9ac02010-04-22 18:58:52 -0700180 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200181 if (ioctl(device->fd,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700182 EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700183 return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700184 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700185 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700186}
187
Jeff Brown6d0fec22010-07-23 21:28:06 -0700188int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
189 AutoMutex _l(mLock);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700190
Jeff Brown6d0fec22010-07-23 21:28:06 -0700191 device_t* device = getDevice(deviceId);
192 if (device != NULL) {
193 return getKeyCodeStateLocked(device, keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700195 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196}
197
Jeff Brown46b9ac02010-04-22 18:58:52 -0700198int32_t EventHub::getKeyCodeStateLocked(device_t* device, int32_t keyCode) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 Vector<int32_t> scanCodes;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700200 device->layoutMap->findScancodes(keyCode, &scanCodes);
201
Jeff Brownfd035822010-06-30 16:10:35 -0700202 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200204 if (ioctl(device->fd, EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 #if 0
206 for (size_t i=0; i<=KEY_MAX; i++) {
207 LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask));
208 }
209 #endif
210 const size_t N = scanCodes.size();
211 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
212 int32_t sc = scanCodes.itemAt(i);
213 //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask));
214 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700215 return AKEY_STATE_DOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 }
217 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700218 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700220 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700221}
222
Jeff Brown6d0fec22010-07-23 21:28:06 -0700223int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700224#ifdef EV_SW
225 if (sw >= 0 && sw <= SW_MAX) {
226 AutoMutex _l(mLock);
227
Jeff Brown46b9ac02010-04-22 18:58:52 -0700228 device_t* device = getDevice(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700229 if (device != NULL) {
230 return getSwitchStateLocked(device, sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700231 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700232 }
233#endif
Jeff Brownc5ed5912010-07-14 18:48:53 -0700234 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700235}
236
237int32_t EventHub::getSwitchStateLocked(device_t* device, int32_t sw) const {
Jeff Brownfd035822010-06-30 16:10:35 -0700238 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
Jeff Brown46b9ac02010-04-22 18:58:52 -0700239 memset(sw_bitmask, 0, sizeof(sw_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200240 if (ioctl(device->fd,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700241 EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700242 return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700243 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700244 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245}
246
Jeff Brown6d0fec22010-07-23 21:28:06 -0700247bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
248 const int32_t* keyCodes, uint8_t* outFlags) const {
249 AutoMutex _l(mLock);
250
251 device_t* device = getDevice(deviceId);
252 if (device != NULL) {
253 return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags);
254 }
255 return false;
256}
257
258bool EventHub::markSupportedKeyCodesLocked(device_t* device, size_t numCodes,
259 const int32_t* keyCodes, uint8_t* outFlags) const {
260 if (device->layoutMap == NULL || device->keyBitmask == NULL) {
261 return false;
262 }
263
264 Vector<int32_t> scanCodes;
265 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
266 scanCodes.clear();
267
268 status_t err = device->layoutMap->findScancodes(keyCodes[codeIndex], &scanCodes);
269 if (! err) {
270 // check the possible scan codes identified by the layout map against the
271 // map of codes actually emitted by the driver
272 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
273 if (test_bit(scanCodes[sc], device->keyBitmask)) {
274 outFlags[codeIndex] = 1;
275 break;
276 }
277 }
278 }
279 }
280 return true;
281}
282
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700283status_t EventHub::scancodeToKeycode(int32_t deviceId, int scancode,
284 int32_t* outKeycode, uint32_t* outFlags) const
285{
286 AutoMutex _l(mLock);
287 device_t* device = getDevice(deviceId);
288
289 if (device != NULL && device->layoutMap != NULL) {
290 status_t err = device->layoutMap->map(scancode, outKeycode, outFlags);
291 if (err == NO_ERROR) {
292 return NO_ERROR;
293 }
294 }
295
296 if (mHaveFirstKeyboard) {
297 device = getDevice(mFirstKeyboardId);
298
299 if (device != NULL && device->layoutMap != NULL) {
300 status_t err = device->layoutMap->map(scancode, outKeycode, outFlags);
301 if (err == NO_ERROR) {
302 return NO_ERROR;
303 }
304 }
305 }
306
307 *outKeycode = 0;
308 *outFlags = 0;
309 return NAME_NOT_FOUND;
310}
311
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400312void EventHub::addExcludedDevice(const char* deviceName)
313{
314 String8 name(deviceName);
315 mExcludedDevices.push_back(name);
316}
317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318EventHub::device_t* EventHub::getDevice(int32_t deviceId) const
319{
320 if (deviceId == 0) deviceId = mFirstKeyboardId;
321 int32_t id = deviceId & ID_MASK;
322 if (id >= mNumDevicesById || id < 0) return NULL;
323 device_t* dev = mDevicesById[id].device;
Dianne Hackborn4e829f02009-03-25 16:21:55 -0700324 if (dev == NULL) return NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 if (dev->id == deviceId) {
326 return dev;
327 }
328 return NULL;
329}
330
Jeff Brown6d0fec22010-07-23 21:28:06 -0700331bool EventHub::getEvent(RawEvent* outEvent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332{
Jeff Brown6d0fec22010-07-23 21:28:06 -0700333 outEvent->deviceId = 0;
334 outEvent->type = 0;
335 outEvent->scanCode = 0;
336 outEvent->keyCode = 0;
337 outEvent->flags = 0;
338 outEvent->value = 0;
339 outEvent->when = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 // Note that we only allow one caller to getEvent(), so don't need
342 // to do locking here... only when adding/removing devices.
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400343
344 if (!mOpened) {
345 mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR;
346 mOpened = true;
347 }
348
Jeff Browncc2e7172010-08-17 16:48:25 -0700349 for (;;) {
350 // Report any devices that had last been added/removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 if (mClosingDevices != NULL) {
352 device_t* device = mClosingDevices;
353 LOGV("Reporting device closed: id=0x%x, name=%s\n",
354 device->id, device->path.string());
355 mClosingDevices = device->next;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700356 if (device->id == mFirstKeyboardId) {
357 outEvent->deviceId = 0;
358 } else {
359 outEvent->deviceId = device->id;
360 }
361 outEvent->type = DEVICE_REMOVED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 delete device;
363 return true;
364 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 if (mOpeningDevices != NULL) {
367 device_t* device = mOpeningDevices;
368 LOGV("Reporting device opened: id=0x%x, name=%s\n",
369 device->id, device->path.string());
370 mOpeningDevices = device->next;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700371 if (device->id == mFirstKeyboardId) {
372 outEvent->deviceId = 0;
373 } else {
374 outEvent->deviceId = device->id;
375 }
376 outEvent->type = DEVICE_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 return true;
378 }
379
Jeff Browncc2e7172010-08-17 16:48:25 -0700380 // Grab the next input event.
381 for (;;) {
382 // Consume buffered input events, if any.
383 if (mInputBufferIndex < mInputBufferCount) {
384 const struct input_event& iev = mInputBufferData[mInputBufferIndex++];
385 const device_t* device = mDevices[mInputDeviceIndex];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386
Jeff Browncc2e7172010-08-17 16:48:25 -0700387 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, v=%d", device->path.string(),
388 (int) iev.time.tv_sec, (int) iev.time.tv_usec, iev.type, iev.code, iev.value);
389 if (device->id == mFirstKeyboardId) {
390 outEvent->deviceId = 0;
391 } else {
392 outEvent->deviceId = device->id;
393 }
394 outEvent->type = iev.type;
395 outEvent->scanCode = iev.code;
396 if (iev.type == EV_KEY) {
397 status_t err = device->layoutMap->map(iev.code,
398 & outEvent->keyCode, & outEvent->flags);
399 LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
400 iev.code, outEvent->keyCode, outEvent->flags, err);
401 if (err != 0) {
402 outEvent->keyCode = AKEYCODE_UNKNOWN;
403 outEvent->flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700405 } else {
406 outEvent->keyCode = iev.code;
407 }
408 outEvent->value = iev.value;
409
410 // Use an event timestamp in the same timebase as
411 // java.lang.System.nanoTime() and android.os.SystemClock.uptimeMillis()
412 // as expected by the rest of the system.
413 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
414 return true;
415 }
416
417 // Finish reading all events from devices identified in previous poll().
418 // This code assumes that mInputDeviceIndex is initially 0 and that the
419 // revents member of pollfd is initialized to 0 when the device is first added.
420 // Since mFDs[0] is used for inotify, we process regular events starting at index 1.
421 mInputDeviceIndex += 1;
422 if (mInputDeviceIndex >= mFDCount) {
423 mInputDeviceIndex = 0;
424 break;
425 }
426
427 const struct pollfd &pfd = mFDs[mInputDeviceIndex];
428 if (pfd.revents & POLLIN) {
429 int32_t readSize = read(pfd.fd, mInputBufferData,
430 sizeof(struct input_event) * INPUT_BUFFER_SIZE);
431 if (readSize < 0) {
432 if (errno != EAGAIN && errno != EINTR) {
433 LOGW("could not get event (errno=%d)", errno);
434 }
435 } else if ((readSize % sizeof(struct input_event)) != 0) {
436 LOGE("could not get event (wrong size: %d)", readSize);
437 } else {
438 mInputBufferCount = readSize / sizeof(struct input_event);
439 mInputBufferIndex = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 }
441 }
442 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 // read_notify() will modify mFDs and mFDCount, so this must be done after
445 // processing all other events.
446 if(mFDs[0].revents & POLLIN) {
447 read_notify(mFDs[0].fd);
448 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700449
450 // Poll for events. Mind the wake lock dance!
451 // We hold a wake lock at all times except during poll(). This works due to some
452 // subtle choreography. When a device driver has pending (unread) events, it acquires
453 // a kernel wake lock. However, once the last pending event has been read, the device
454 // driver will release the kernel wake lock. To prevent the system from going to sleep
455 // when this happens, the EventHub holds onto its own user wake lock while the client
456 // is processing events. Thus the system can only sleep if there are no events
457 // pending or currently being processed.
458 release_wake_lock(WAKE_LOCK_ID);
459
460 int pollResult = poll(mFDs, mFDCount, -1);
461
462 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
463
464 if (pollResult <= 0) {
465 if (errno != EINTR) {
466 LOGW("select failed (errno=%d)\n", errno);
467 usleep(100000);
468 }
469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 }
471}
472
473/*
474 * Open the platform-specific input device.
475 */
476bool EventHub::openPlatformInput(void)
477{
478 /*
479 * Open platform-specific input device(s).
480 */
481 int res;
482
483 mFDCount = 1;
484 mFDs = (pollfd *)calloc(1, sizeof(mFDs[0]));
485 mDevices = (device_t **)calloc(1, sizeof(mDevices[0]));
486 mFDs[0].events = POLLIN;
Jeff Browncc2e7172010-08-17 16:48:25 -0700487 mFDs[0].revents = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 mDevices[0] = NULL;
489#ifdef HAVE_INOTIFY
490 mFDs[0].fd = inotify_init();
491 res = inotify_add_watch(mFDs[0].fd, device_path, IN_DELETE | IN_CREATE);
492 if(res < 0) {
493 LOGE("could not add watch for %s, %s\n", device_path, strerror(errno));
494 }
495#else
496 /*
497 * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd.
498 * We allocate space for it and set it to something invalid.
499 */
500 mFDs[0].fd = -1;
501#endif
502
503 res = scan_dir(device_path);
504 if(res < 0) {
505 LOGE("scan dir failed for %s\n", device_path);
506 //open_device("/dev/input/event0");
507 }
508
509 return true;
510}
511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512// ----------------------------------------------------------------------------
513
Jeff Brownfd035822010-06-30 16:10:35 -0700514static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
515 const uint8_t* end = array + endIndex;
516 array += startIndex;
517 while (array != end) {
518 if (*(array++) != 0) {
519 return true;
520 }
521 }
522 return false;
523}
524
525static const int32_t GAMEPAD_KEYCODES[] = {
526 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
527 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
528 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
529 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
530 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
531 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE
532};
533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534int EventHub::open_device(const char *deviceName)
535{
536 int version;
537 int fd;
538 struct pollfd *new_mFDs;
539 device_t **new_devices;
540 char **new_device_names;
541 char name[80];
542 char location[80];
543 char idstr[80];
544 struct input_id id;
545
546 LOGV("Opening device: %s", deviceName);
547
548 AutoMutex _l(mLock);
Nick Pellye6b1bbd2010-01-20 19:36:49 -0800549
Nick Pellyc8b60d12010-01-26 10:27:15 -0800550 fd = open(deviceName, O_RDWR);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 if(fd < 0) {
552 LOGE("could not open %s, %s\n", deviceName, strerror(errno));
553 return -1;
554 }
555
556 if(ioctl(fd, EVIOCGVERSION, &version)) {
557 LOGE("could not get driver version for %s, %s\n", deviceName, strerror(errno));
558 return -1;
559 }
560 if(ioctl(fd, EVIOCGID, &id)) {
561 LOGE("could not get driver id for %s, %s\n", deviceName, strerror(errno));
562 return -1;
563 }
564 name[sizeof(name) - 1] = '\0';
565 location[sizeof(location) - 1] = '\0';
566 idstr[sizeof(idstr) - 1] = '\0';
567 if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
568 //fprintf(stderr, "could not get device name for %s, %s\n", deviceName, strerror(errno));
569 name[0] = '\0';
570 }
Mike Lockwood15431a92009-07-17 00:10:10 -0400571
572 // check to see if the device is on our excluded list
573 List<String8>::iterator iter = mExcludedDevices.begin();
574 List<String8>::iterator end = mExcludedDevices.end();
575 for ( ; iter != end; iter++) {
576 const char* test = *iter;
577 if (strcmp(name, test) == 0) {
578 LOGI("ignoring event id %s driver %s\n", deviceName, test);
579 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -0400580 return -1;
581 }
582 }
583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 if(ioctl(fd, EVIOCGPHYS(sizeof(location) - 1), &location) < 1) {
585 //fprintf(stderr, "could not get location for %s, %s\n", deviceName, strerror(errno));
586 location[0] = '\0';
587 }
588 if(ioctl(fd, EVIOCGUNIQ(sizeof(idstr) - 1), &idstr) < 1) {
589 //fprintf(stderr, "could not get idstring for %s, %s\n", deviceName, strerror(errno));
590 idstr[0] = '\0';
591 }
592
Jeff Browncc2e7172010-08-17 16:48:25 -0700593 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
594 LOGE("Error %d making device file descriptor non-blocking.", errno);
595 close(fd);
596 return -1;
597 }
598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 int devid = 0;
600 while (devid < mNumDevicesById) {
601 if (mDevicesById[devid].device == NULL) {
602 break;
603 }
604 devid++;
605 }
606 if (devid >= mNumDevicesById) {
607 device_ent* new_devids = (device_ent*)realloc(mDevicesById,
608 sizeof(mDevicesById[0]) * (devid + 1));
609 if (new_devids == NULL) {
610 LOGE("out of memory");
611 return -1;
612 }
613 mDevicesById = new_devids;
614 mNumDevicesById = devid+1;
615 mDevicesById[devid].device = NULL;
616 mDevicesById[devid].seq = 0;
617 }
618
619 mDevicesById[devid].seq = (mDevicesById[devid].seq+(1<<SEQ_SHIFT))&SEQ_MASK;
620 if (mDevicesById[devid].seq == 0) {
621 mDevicesById[devid].seq = 1<<SEQ_SHIFT;
622 }
623
624 new_mFDs = (pollfd*)realloc(mFDs, sizeof(mFDs[0]) * (mFDCount + 1));
625 new_devices = (device_t**)realloc(mDevices, sizeof(mDevices[0]) * (mFDCount + 1));
626 if (new_mFDs == NULL || new_devices == NULL) {
627 LOGE("out of memory");
628 return -1;
629 }
630 mFDs = new_mFDs;
631 mDevices = new_devices;
632
633#if 0
634 LOGI("add device %d: %s\n", mFDCount, deviceName);
635 LOGI(" bus: %04x\n"
636 " vendor %04x\n"
637 " product %04x\n"
638 " version %04x\n",
639 id.bustype, id.vendor, id.product, id.version);
640 LOGI(" name: \"%s\"\n", name);
641 LOGI(" location: \"%s\"\n"
642 " id: \"%s\"\n", location, idstr);
643 LOGI(" version: %d.%d.%d\n",
644 version >> 16, (version >> 8) & 0xff, version & 0xff);
645#endif
646
Iliyan Malchevfc2ebc42009-08-06 14:50:08 -0700647 device_t* device = new device_t(devid|mDevicesById[devid].seq, deviceName, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 if (device == NULL) {
649 LOGE("out of memory");
650 return -1;
651 }
652
Jens Gulinc4554b92010-06-22 22:21:57 +0200653 device->fd = fd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 mFDs[mFDCount].fd = fd;
655 mFDs[mFDCount].events = POLLIN;
Jeff Browncc2e7172010-08-17 16:48:25 -0700656 mFDs[mFDCount].revents = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657
Jeff Brownfd035822010-06-30 16:10:35 -0700658 // Figure out the kinds of events the device reports.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700659
Jeff Brownfd035822010-06-30 16:10:35 -0700660 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 memset(key_bitmask, 0, sizeof(key_bitmask));
Jeff Brownfd035822010-06-30 16:10:35 -0700662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 LOGV("Getting keys...");
664 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) >= 0) {
665 //LOGI("MAP\n");
Jeff Brownfd035822010-06-30 16:10:35 -0700666 //for (int i = 0; i < sizeof(key_bitmask); i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 // LOGI("%d: 0x%02x\n", i, key_bitmask[i]);
668 //}
Jeff Brownfd035822010-06-30 16:10:35 -0700669
670 // See if this is a keyboard. Ignore everything in the button range except for
671 // gamepads which are also considered keyboards.
672 if (containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC))
673 || containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_GAMEPAD),
674 sizeof_bit_array(BTN_DIGI))
675 || containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK),
676 sizeof_bit_array(KEY_MAX + 1))) {
677 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
678
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700679 device->keyBitmask = new uint8_t[sizeof(key_bitmask)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 if (device->keyBitmask != NULL) {
681 memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask));
682 } else {
683 delete device;
684 LOGE("out of memory allocating key bitmask");
685 return -1;
686 }
687 }
688 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700689
Jeff Brownfd035822010-06-30 16:10:35 -0700690 // See if this is a trackball (or mouse).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 if (test_bit(BTN_MOUSE, key_bitmask)) {
Jeff Brownfd035822010-06-30 16:10:35 -0700692 uint8_t rel_bitmask[sizeof_bit_array(REL_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 memset(rel_bitmask, 0, sizeof(rel_bitmask));
694 LOGV("Getting relative controllers...");
Jeff Brownfd035822010-06-30 16:10:35 -0700695 if (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask) >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 if (test_bit(REL_X, rel_bitmask) && test_bit(REL_Y, rel_bitmask)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700697 device->classes |= INPUT_DEVICE_CLASS_TRACKBALL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
699 }
700 }
Jeff Brownfd035822010-06-30 16:10:35 -0700701
702 // See if this is a touch pad.
703 uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700704 memset(abs_bitmask, 0, sizeof(abs_bitmask));
705 LOGV("Getting absolute controllers...");
Jeff Brownfd035822010-06-30 16:10:35 -0700706 if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) >= 0) {
707 // Is this a new modern multi-touch driver?
Jeff Brown8d608662010-08-30 03:02:23 -0700708 if (test_bit(ABS_MT_POSITION_X, abs_bitmask)
Jeff Brownfd035822010-06-30 16:10:35 -0700709 && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) {
710 device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN | INPUT_DEVICE_CLASS_TOUCHSCREEN_MT;
711
712 // Is this an old style single-touch driver?
713 } else if (test_bit(BTN_TOUCH, key_bitmask)
714 && test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) {
715 device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN;
716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 }
718
719#ifdef EV_SW
720 // figure out the switches this device reports
Jeff Brownfd035822010-06-30 16:10:35 -0700721 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 memset(sw_bitmask, 0, sizeof(sw_bitmask));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700723 bool hasSwitches = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask) >= 0) {
725 for (int i=0; i<EV_SW; i++) {
726 //LOGI("Device 0x%x sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask));
727 if (test_bit(i, sw_bitmask)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700728 hasSwitches = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 if (mSwitches[i] == 0) {
730 mSwitches[i] = device->id;
731 }
732 }
733 }
734 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700735 if (hasSwitches) {
736 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738#endif
739
Jeff Brown46b9ac02010-04-22 18:58:52 -0700740 if ((device->classes & INPUT_DEVICE_CLASS_KEYBOARD) != 0) {
Iliyan Malchevfc2ebc42009-08-06 14:50:08 -0700741 char tmpfn[sizeof(name)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 char keylayoutFilename[300];
743
744 // a more descriptive name
Iliyan Malchevfc2ebc42009-08-06 14:50:08 -0700745 device->name = name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746
747 // replace all the spaces with underscores
Iliyan Malchevfc2ebc42009-08-06 14:50:08 -0700748 strcpy(tmpfn, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 for (char *p = strchr(tmpfn, ' '); p && *p; p = strchr(tmpfn, ' '))
750 *p = '_';
751
752 // find the .kl file we need for this device
753 const char* root = getenv("ANDROID_ROOT");
754 snprintf(keylayoutFilename, sizeof(keylayoutFilename),
755 "%s/usr/keylayout/%s.kl", root, tmpfn);
756 bool defaultKeymap = false;
757 if (access(keylayoutFilename, R_OK)) {
758 snprintf(keylayoutFilename, sizeof(keylayoutFilename),
759 "%s/usr/keylayout/%s", root, "qwerty.kl");
760 defaultKeymap = true;
761 }
Jeff Brownfd035822010-06-30 16:10:35 -0700762 status_t status = device->layoutMap->load(keylayoutFilename);
763 if (status) {
764 LOGE("Error %d loading key layout.", status);
765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766
767 // tell the world about the devname (the descriptive name)
Dianne Hackborna2e92262010-03-02 17:19:29 -0800768 if (!mHaveFirstKeyboard && !defaultKeymap && strstr(name, "-keypad")) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 // the built-in keyboard has a well-known device ID of 0,
770 // this device better not go away.
771 mHaveFirstKeyboard = true;
772 mFirstKeyboardId = device->id;
Dianne Hackborna2e92262010-03-02 17:19:29 -0800773 property_set("hw.keyboards.0.devname", name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 // ensure mFirstKeyboardId is set to -something-.
776 if (mFirstKeyboardId == 0) {
777 mFirstKeyboardId = device->id;
778 }
779 }
780 char propName[100];
Dianne Hackborna2e92262010-03-02 17:19:29 -0800781 sprintf(propName, "hw.keyboards.%u.devname", device->id);
Iliyan Malchevfc2ebc42009-08-06 14:50:08 -0700782 property_set(propName, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700784 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brownfd035822010-06-30 16:10:35 -0700785 if (hasKeycode(device, AKEYCODE_Q)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700786 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700787 }
788
Jeff Brownfd035822010-06-30 16:10:35 -0700789 // See if this device has a DPAD.
790 if (hasKeycode(device, AKEYCODE_DPAD_UP) &&
791 hasKeycode(device, AKEYCODE_DPAD_DOWN) &&
792 hasKeycode(device, AKEYCODE_DPAD_LEFT) &&
793 hasKeycode(device, AKEYCODE_DPAD_RIGHT) &&
794 hasKeycode(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700795 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700796 }
797
Jeff Brownfd035822010-06-30 16:10:35 -0700798 // See if this device has a gamepad.
799 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES); i++) {
800 if (hasKeycode(device, GAMEPAD_KEYCODES[i])) {
801 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
802 break;
803 }
804 }
805
Dianne Hackborna2e92262010-03-02 17:19:29 -0800806 LOGI("New keyboard: device->id=0x%x devname='%s' propName='%s' keylayout='%s'\n",
807 device->id, name, propName, keylayoutFilename);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
809
Sean McNeilaeb00c42010-06-23 16:00:37 +0700810 // If the device isn't recognized as something we handle, don't monitor it.
811 if (device->classes == 0) {
812 LOGV("Dropping device %s %p, id = %d\n", deviceName, device, devid);
813 close(fd);
814 delete device;
815 return -1;
816 }
817
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700818 LOGI("New device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n",
819 deviceName, name, device->id, mNumDevicesById, mFDCount, fd, device->classes);
820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 LOGV("Adding device %s %p at %d, id = %d, classes = 0x%x\n",
822 deviceName, device, mFDCount, devid, device->classes);
823
824 mDevicesById[devid].device = device;
825 device->next = mOpeningDevices;
826 mOpeningDevices = device;
827 mDevices[mFDCount] = device;
828
829 mFDCount++;
830 return 0;
831}
832
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700833bool EventHub::hasKeycode(device_t* device, int keycode) const
834{
835 if (device->keyBitmask == NULL || device->layoutMap == NULL) {
836 return false;
837 }
838
839 Vector<int32_t> scanCodes;
840 device->layoutMap->findScancodes(keycode, &scanCodes);
841 const size_t N = scanCodes.size();
842 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
843 int32_t sc = scanCodes.itemAt(i);
844 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
845 return true;
846 }
847 }
848
849 return false;
850}
851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852int EventHub::close_device(const char *deviceName)
853{
854 AutoMutex _l(mLock);
855
856 int i;
857 for(i = 1; i < mFDCount; i++) {
858 if(strcmp(mDevices[i]->path.string(), deviceName) == 0) {
859 //LOGD("remove device %d: %s\n", i, deviceName);
860 device_t* device = mDevices[i];
Dianne Hackborna8f60182009-09-01 19:01:50 -0700861
862 LOGI("Removed device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n",
863 device->path.string(), device->name.string(), device->id,
864 mNumDevicesById, mFDCount, mFDs[i].fd, device->classes);
865
866 // Clear this device's entry.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 int index = (device->id&ID_MASK);
868 mDevicesById[index].device = NULL;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700869
870 // Close the file descriptor and compact the fd array.
Mike Lockwood36dad722009-08-28 13:29:06 -0700871 close(mFDs[i].fd);
Dianne Hackborna8f60182009-09-01 19:01:50 -0700872 int count = mFDCount - i - 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 memmove(mDevices + i, mDevices + i + 1, sizeof(mDevices[0]) * count);
874 memmove(mFDs + i, mFDs + i + 1, sizeof(mFDs[0]) * count);
Dianne Hackborna8f60182009-09-01 19:01:50 -0700875 mFDCount--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876
877#ifdef EV_SW
878 for (int j=0; j<EV_SW; j++) {
879 if (mSwitches[j] == device->id) {
880 mSwitches[j] = 0;
881 }
882 }
883#endif
884
885 device->next = mClosingDevices;
886 mClosingDevices = device;
887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 if (device->id == mFirstKeyboardId) {
889 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
890 device->path.string(), mFirstKeyboardId);
891 mFirstKeyboardId = 0;
Dianne Hackborna2e92262010-03-02 17:19:29 -0800892 property_set("hw.keyboards.0.devname", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
894 // clear the property
895 char propName[100];
Dianne Hackborna2e92262010-03-02 17:19:29 -0800896 sprintf(propName, "hw.keyboards.%u.devname", device->id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 property_set(propName, NULL);
898 return 0;
899 }
900 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700901 LOGE("remove device: %s not found\n", deviceName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 return -1;
903}
904
905int EventHub::read_notify(int nfd)
906{
907#ifdef HAVE_INOTIFY
908 int res;
909 char devname[PATH_MAX];
910 char *filename;
911 char event_buf[512];
912 int event_size;
913 int event_pos = 0;
914 struct inotify_event *event;
915
Dianne Hackborna8f60182009-09-01 19:01:50 -0700916 LOGV("EventHub::read_notify nfd: %d\n", nfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 res = read(nfd, event_buf, sizeof(event_buf));
918 if(res < (int)sizeof(*event)) {
919 if(errno == EINTR)
920 return 0;
921 LOGW("could not get event, %s\n", strerror(errno));
922 return 1;
923 }
924 //printf("got %d bytes of event information\n", res);
925
926 strcpy(devname, device_path);
927 filename = devname + strlen(devname);
928 *filename++ = '/';
929
930 while(res >= (int)sizeof(*event)) {
931 event = (struct inotify_event *)(event_buf + event_pos);
932 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
933 if(event->len) {
934 strcpy(filename, event->name);
935 if(event->mask & IN_CREATE) {
936 open_device(devname);
937 }
938 else {
939 close_device(devname);
940 }
941 }
942 event_size = sizeof(*event) + event->len;
943 res -= event_size;
944 event_pos += event_size;
945 }
946#endif
947 return 0;
948}
949
950
951int EventHub::scan_dir(const char *dirname)
952{
953 char devname[PATH_MAX];
954 char *filename;
955 DIR *dir;
956 struct dirent *de;
957 dir = opendir(dirname);
958 if(dir == NULL)
959 return -1;
960 strcpy(devname, dirname);
961 filename = devname + strlen(devname);
962 *filename++ = '/';
963 while((de = readdir(dir))) {
964 if(de->d_name[0] == '.' &&
965 (de->d_name[1] == '\0' ||
966 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
967 continue;
968 strcpy(filename, de->d_name);
969 open_device(devname);
970 }
971 closedir(dir);
972 return 0;
973}
974
975}; // namespace android