blob: f0c77ba42b8638a778ac4e31052a3dbc90b06576 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001//
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>
19#include <hardware/power.h>
20
21#include <cutils/properties.h>
22#include <utils/IServiceManager.h>
23#include <utils/Log.h>
24#include <utils/Timers.h>
25#include <utils.h>
26
27#include <stdlib.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <fcntl.h>
31#include <memory.h>
32#include <errno.h>
33#include <assert.h>
34
35#include "KeyLayoutMap.h"
36
37#include <string.h>
38#include <stdint.h>
39#include <dirent.h>
40#ifdef HAVE_INOTIFY
41# include <sys/inotify.h>
42#endif
43#ifdef HAVE_ANDROID_OS
44# include <sys/limits.h> /* not part of Linux */
45#endif
46#include <sys/poll.h>
47#include <sys/ioctl.h>
48
49/* this macro is used to tell if "bit" is set in "array"
50 * it selects a byte from the array, and does a boolean AND
51 * operation with a byte that only has the relevant bit set.
52 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
53 */
54#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
55
56#define ID_MASK 0x0000ffff
57#define SEQ_MASK 0x7fff0000
58#define SEQ_SHIFT 16
59#define id_to_index(id) ((id&ID_MASK)+1)
60
61namespace android {
62
63static const char *WAKE_LOCK_ID = "KeyEvents";
64static const char *device_path = "/dev/input";
65
66/* return the larger integer */
67static inline int max(int v1, int v2)
68{
69 return (v1 > v2) ? v1 : v2;
70}
71
72EventHub::device_t::device_t(int32_t _id, const char* _path)
73 : id(_id), path(_path), classes(0)
74 , layoutMap(new KeyLayoutMap()), next(NULL) {
75}
76
77EventHub::device_t::~device_t() {
78 delete layoutMap;
79}
80
81EventHub::EventHub(void)
82 : mError(NO_INIT), mHaveFirstKeyboard(false), mFirstKeyboardId(0)
83 , mDevicesById(0), mNumDevicesById(0)
84 , mOpeningDevices(0), mClosingDevices(0)
85 , mDevices(0), mFDs(0), mFDCount(0)
86{
87 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
88#ifdef EV_SW
89 memset(mSwitches, 0, sizeof(mSwitches));
90#endif
91}
92
93/*
94 * Clean up.
95 */
96EventHub::~EventHub(void)
97{
98 release_wake_lock(WAKE_LOCK_ID);
99 // we should free stuff here...
100}
101
102void EventHub::onFirstRef()
103{
104 mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR;
105}
106
107status_t EventHub::errorCheck() const
108{
109 return mError;
110}
111
112String8 EventHub::getDeviceName(int32_t deviceId) const
113{
114 AutoMutex _l(mLock);
115 device_t* device = getDevice(deviceId);
116 if (device == NULL) return String8();
117 return device->name;
118}
119
120uint32_t EventHub::getDeviceClasses(int32_t deviceId) const
121{
122 AutoMutex _l(mLock);
123 device_t* device = getDevice(deviceId);
124 if (device == NULL) return 0;
125 return device->classes;
126}
127
128int EventHub::getAbsoluteInfo(int32_t deviceId, int axis, int *outMinValue,
129 int* outMaxValue, int* outFlat, int* outFuzz) const
130{
131 AutoMutex _l(mLock);
132 device_t* device = getDevice(deviceId);
133 if (device == NULL) return -1;
134
135 struct input_absinfo info;
136
137 if(ioctl(mFDs[id_to_index(device->id)].fd, EVIOCGABS(axis), &info)) {
138 LOGE("Error reading absolute controller %d for device %s fd %d\n",
139 axis, device->name.string(), mFDs[id_to_index(device->id)].fd);
140 return -1;
141 }
142 *outMinValue = info.minimum;
143 *outMaxValue = info.maximum;
144 *outFlat = info.flat;
145 *outFuzz = info.fuzz;
146 return 0;
147}
148
149int EventHub::getSwitchState(int sw) const
150{
151#ifdef EV_SW
152 if (sw >= 0 && sw <= SW_MAX) {
153 int32_t devid = mSwitches[sw];
154 if (devid != 0) {
155 return getSwitchState(devid, sw);
156 }
157 }
158#endif
159 return -1;
160}
161
162int EventHub::getSwitchState(int32_t deviceId, int sw) const
163{
164#ifdef EV_SW
165 AutoMutex _l(mLock);
166 device_t* device = getDevice(deviceId);
167 if (device == NULL) return -1;
168
169 if (sw >= 0 && sw <= SW_MAX) {
170 uint8_t sw_bitmask[(SW_MAX+1)/8];
171 memset(sw_bitmask, 0, sizeof(sw_bitmask));
172 if (ioctl(mFDs[id_to_index(device->id)].fd,
173 EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
174 return test_bit(sw, sw_bitmask) ? 1 : 0;
175 }
176 }
177#endif
178
179 return -1;
180}
181
182int EventHub::getScancodeState(int code) const
183{
184 return getScancodeState(mFirstKeyboardId, code);
185}
186
187int EventHub::getScancodeState(int32_t deviceId, int code) const
188{
189 AutoMutex _l(mLock);
190 device_t* device = getDevice(deviceId);
191 if (device == NULL) return -1;
192
193 if (code >= 0 && code <= KEY_MAX) {
194 uint8_t key_bitmask[(KEY_MAX+1)/8];
195 memset(key_bitmask, 0, sizeof(key_bitmask));
196 if (ioctl(mFDs[id_to_index(device->id)].fd,
197 EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
198 return test_bit(code, key_bitmask) ? 1 : 0;
199 }
200 }
201
202 return -1;
203}
204
205int EventHub::getKeycodeState(int code) const
206{
207 return getKeycodeState(mFirstKeyboardId, code);
208}
209
210int EventHub::getKeycodeState(int32_t deviceId, int code) const
211{
212 AutoMutex _l(mLock);
213 device_t* device = getDevice(deviceId);
214 if (device == NULL || device->layoutMap == NULL) return -1;
215
216 Vector<int32_t> scanCodes;
217 device->layoutMap->findScancodes(code, &scanCodes);
218
219 uint8_t key_bitmask[(KEY_MAX+1)/8];
220 memset(key_bitmask, 0, sizeof(key_bitmask));
221 if (ioctl(mFDs[id_to_index(device->id)].fd,
222 EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
223 #if 0
224 for (size_t i=0; i<=KEY_MAX; i++) {
225 LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask));
226 }
227 #endif
228 const size_t N = scanCodes.size();
229 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
230 int32_t sc = scanCodes.itemAt(i);
231 //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask));
232 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) {
233 return 1;
234 }
235 }
236 }
237
238 return 0;
239}
240
241EventHub::device_t* EventHub::getDevice(int32_t deviceId) const
242{
243 if (deviceId == 0) deviceId = mFirstKeyboardId;
244 int32_t id = deviceId & ID_MASK;
245 if (id >= mNumDevicesById || id < 0) return NULL;
246 device_t* dev = mDevicesById[id].device;
247 if (dev->id == deviceId) {
248 return dev;
249 }
250 return NULL;
251}
252
253bool EventHub::getEvent(int32_t* outDeviceId, int32_t* outType,
254 int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags,
255 int32_t* outValue, nsecs_t* outWhen)
256{
257 *outDeviceId = 0;
258 *outType = 0;
259 *outScancode = 0;
260 *outKeycode = 0;
261 *outFlags = 0;
262 *outValue = 0;
263 *outWhen = 0;
264
265 status_t err;
266
267 fd_set readfds;
268 int maxFd = -1;
269 int cc;
270 int i;
271 int res;
272 int pollres;
273 struct input_event iev;
274
275 // Note that we only allow one caller to getEvent(), so don't need
276 // to do locking here... only when adding/removing devices.
277
278 while(1) {
279
280 // First, report any devices that had last been added/removed.
281 if (mClosingDevices != NULL) {
282 device_t* device = mClosingDevices;
283 LOGV("Reporting device closed: id=0x%x, name=%s\n",
284 device->id, device->path.string());
285 mClosingDevices = device->next;
286 *outDeviceId = device->id;
287 if (*outDeviceId == mFirstKeyboardId) *outDeviceId = 0;
288 *outType = DEVICE_REMOVED;
289 delete device;
290 return true;
291 }
292 if (mOpeningDevices != NULL) {
293 device_t* device = mOpeningDevices;
294 LOGV("Reporting device opened: id=0x%x, name=%s\n",
295 device->id, device->path.string());
296 mOpeningDevices = device->next;
297 *outDeviceId = device->id;
298 if (*outDeviceId == mFirstKeyboardId) *outDeviceId = 0;
299 *outType = DEVICE_ADDED;
300 return true;
301 }
302
303 release_wake_lock(WAKE_LOCK_ID);
304
305 pollres = poll(mFDs, mFDCount, -1);
306
307 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
308
309 if (pollres <= 0) {
310 if (errno != EINTR) {
311 LOGW("select failed (errno=%d)\n", errno);
312 usleep(100000);
313 }
314 continue;
315 }
316
317 //printf("poll %d, returned %d\n", mFDCount, pollres);
318 if(mFDs[0].revents & POLLIN) {
319 read_notify(mFDs[0].fd);
320 }
321 for(i = 1; i < mFDCount; i++) {
322 if(mFDs[i].revents) {
323 LOGV("revents for %d = 0x%08x", i, mFDs[i].revents);
324 if(mFDs[i].revents & POLLIN) {
325 res = read(mFDs[i].fd, &iev, sizeof(iev));
326 if (res == sizeof(iev)) {
327 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, v=%d",
328 mDevices[i]->path.string(),
329 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
330 iev.type, iev.code, iev.value);
331 *outDeviceId = mDevices[i]->id;
332 if (*outDeviceId == mFirstKeyboardId) *outDeviceId = 0;
333 *outType = iev.type;
334 *outScancode = iev.code;
335 if (iev.type == EV_KEY) {
336 err = mDevices[i]->layoutMap->map(iev.code, outKeycode, outFlags);
337 LOGV("iev.code=%d outKeycode=%d outFlags=0x%08x err=%d\n",
338 iev.code, *outKeycode, *outFlags, err);
339 if (err != 0) {
340 *outKeycode = 0;
341 *outFlags = 0;
342 }
343 } else {
344 *outKeycode = iev.code;
345 }
346 *outValue = iev.value;
347 *outWhen = s2ns(iev.time.tv_sec) + us2ns(iev.time.tv_usec);
348 return true;
349 } else {
350 if (res<0) {
351 LOGW("could not get event (errno=%d)", errno);
352 } else {
353 LOGE("could not get event (wrong size: %d)", res);
354 }
355 continue;
356 }
357 }
358 }
359 }
360 }
361}
362
363/*
364 * Open the platform-specific input device.
365 */
366bool EventHub::openPlatformInput(void)
367{
368 /*
369 * Open platform-specific input device(s).
370 */
371 int res;
372
373 mFDCount = 1;
374 mFDs = (pollfd *)calloc(1, sizeof(mFDs[0]));
375 mDevices = (device_t **)calloc(1, sizeof(mDevices[0]));
376 mFDs[0].events = POLLIN;
377 mDevices[0] = NULL;
378#ifdef HAVE_INOTIFY
379 mFDs[0].fd = inotify_init();
380 res = inotify_add_watch(mFDs[0].fd, device_path, IN_DELETE | IN_CREATE);
381 if(res < 0) {
382 LOGE("could not add watch for %s, %s\n", device_path, strerror(errno));
383 }
384#else
385 /*
386 * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd.
387 * We allocate space for it and set it to something invalid.
388 */
389 mFDs[0].fd = -1;
390#endif
391
392 res = scan_dir(device_path);
393 if(res < 0) {
394 LOGE("scan dir failed for %s\n", device_path);
395 //open_device("/dev/input/event0");
396 }
397
398 return true;
399}
400
401// ----------------------------------------------------------------------------
402
403int EventHub::open_device(const char *deviceName)
404{
405 int version;
406 int fd;
407 struct pollfd *new_mFDs;
408 device_t **new_devices;
409 char **new_device_names;
410 char name[80];
411 char location[80];
412 char idstr[80];
413 struct input_id id;
414
415 LOGV("Opening device: %s", deviceName);
416
417 AutoMutex _l(mLock);
418
419 fd = open(deviceName, O_RDWR);
420 if(fd < 0) {
421 LOGE("could not open %s, %s\n", deviceName, strerror(errno));
422 return -1;
423 }
424
425 if(ioctl(fd, EVIOCGVERSION, &version)) {
426 LOGE("could not get driver version for %s, %s\n", deviceName, strerror(errno));
427 return -1;
428 }
429 if(ioctl(fd, EVIOCGID, &id)) {
430 LOGE("could not get driver id for %s, %s\n", deviceName, strerror(errno));
431 return -1;
432 }
433 name[sizeof(name) - 1] = '\0';
434 location[sizeof(location) - 1] = '\0';
435 idstr[sizeof(idstr) - 1] = '\0';
436 if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
437 //fprintf(stderr, "could not get device name for %s, %s\n", deviceName, strerror(errno));
438 name[0] = '\0';
439 }
440 if(ioctl(fd, EVIOCGPHYS(sizeof(location) - 1), &location) < 1) {
441 //fprintf(stderr, "could not get location for %s, %s\n", deviceName, strerror(errno));
442 location[0] = '\0';
443 }
444 if(ioctl(fd, EVIOCGUNIQ(sizeof(idstr) - 1), &idstr) < 1) {
445 //fprintf(stderr, "could not get idstring for %s, %s\n", deviceName, strerror(errno));
446 idstr[0] = '\0';
447 }
448
449 int devid = 0;
450 while (devid < mNumDevicesById) {
451 if (mDevicesById[devid].device == NULL) {
452 break;
453 }
454 devid++;
455 }
456 if (devid >= mNumDevicesById) {
457 device_ent* new_devids = (device_ent*)realloc(mDevicesById,
458 sizeof(mDevicesById[0]) * (devid + 1));
459 if (new_devids == NULL) {
460 LOGE("out of memory");
461 return -1;
462 }
463 mDevicesById = new_devids;
464 mNumDevicesById = devid+1;
465 mDevicesById[devid].device = NULL;
466 mDevicesById[devid].seq = 0;
467 }
468
469 mDevicesById[devid].seq = (mDevicesById[devid].seq+(1<<SEQ_SHIFT))&SEQ_MASK;
470 if (mDevicesById[devid].seq == 0) {
471 mDevicesById[devid].seq = 1<<SEQ_SHIFT;
472 }
473
474 new_mFDs = (pollfd*)realloc(mFDs, sizeof(mFDs[0]) * (mFDCount + 1));
475 new_devices = (device_t**)realloc(mDevices, sizeof(mDevices[0]) * (mFDCount + 1));
476 if (new_mFDs == NULL || new_devices == NULL) {
477 LOGE("out of memory");
478 return -1;
479 }
480 mFDs = new_mFDs;
481 mDevices = new_devices;
482
483#if 0
484 LOGI("add device %d: %s\n", mFDCount, deviceName);
485 LOGI(" bus: %04x\n"
486 " vendor %04x\n"
487 " product %04x\n"
488 " version %04x\n",
489 id.bustype, id.vendor, id.product, id.version);
490 LOGI(" name: \"%s\"\n", name);
491 LOGI(" location: \"%s\"\n"
492 " id: \"%s\"\n", location, idstr);
493 LOGI(" version: %d.%d.%d\n",
494 version >> 16, (version >> 8) & 0xff, version & 0xff);
495#endif
496
497 device_t* device = new device_t(devid|mDevicesById[devid].seq, deviceName);
498 if (device == NULL) {
499 LOGE("out of memory");
500 return -1;
501 }
502
503 mFDs[mFDCount].fd = fd;
504 mFDs[mFDCount].events = POLLIN;
505
506 // figure out the kinds of events the device reports
507 uint8_t key_bitmask[(KEY_MAX+1)/8];
508 memset(key_bitmask, 0, sizeof(key_bitmask));
509 LOGV("Getting keys...");
510 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) >= 0) {
511 //LOGI("MAP\n");
512 //for (int i=0; i<((KEY_MAX+1)/8); i++) {
513 // LOGI("%d: 0x%02x\n", i, key_bitmask[i]);
514 //}
515 for (int i=0; i<((BTN_MISC+7)/8); i++) {
516 if (key_bitmask[i] != 0) {
517 device->classes |= CLASS_KEYBOARD;
518 break;
519 }
520 }
521 }
522 if (test_bit(BTN_MOUSE, key_bitmask)) {
523 uint8_t rel_bitmask[(REL_MAX+1)/8];
524 memset(rel_bitmask, 0, sizeof(rel_bitmask));
525 LOGV("Getting relative controllers...");
526 if (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask) >= 0)
527 {
528 if (test_bit(REL_X, rel_bitmask) && test_bit(REL_Y, rel_bitmask)) {
529 device->classes |= CLASS_TRACKBALL;
530 }
531 }
532 }
533 if (test_bit(BTN_TOUCH, key_bitmask)) {
534 uint8_t abs_bitmask[(ABS_MAX+1)/8];
535 memset(abs_bitmask, 0, sizeof(abs_bitmask));
536 LOGV("Getting absolute controllers...");
537 if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) >= 0)
538 {
539 if (test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) {
540 device->classes |= CLASS_TOUCHSCREEN;
541 }
542 }
543 }
544
545#ifdef EV_SW
546 // figure out the switches this device reports
547 uint8_t sw_bitmask[(SW_MAX+1)/8];
548 memset(sw_bitmask, 0, sizeof(sw_bitmask));
549 if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask) >= 0) {
550 for (int i=0; i<EV_SW; i++) {
551 //LOGI("Device 0x%x sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask));
552 if (test_bit(i, sw_bitmask)) {
553 if (mSwitches[i] == 0) {
554 mSwitches[i] = device->id;
555 }
556 }
557 }
558 }
559#endif
560
561 LOGI("New device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n",
562 deviceName, name, device->id, mNumDevicesById, mFDCount, fd, device->classes);
563
564 if ((device->classes&CLASS_KEYBOARD) != 0) {
565 char devname[101];
566 char tmpfn[101];
567 char keylayoutFilename[300];
568
569 // a more descriptive name
570 ioctl(mFDs[mFDCount].fd, EVIOCGNAME(sizeof(devname)-1), devname);
571 devname[sizeof(devname)-1] = 0;
572 device->name = devname;
573
574 // replace all the spaces with underscores
575 strcpy(tmpfn, devname);
576 for (char *p = strchr(tmpfn, ' '); p && *p; p = strchr(tmpfn, ' '))
577 *p = '_';
578
579 // find the .kl file we need for this device
580 const char* root = getenv("ANDROID_ROOT");
581 snprintf(keylayoutFilename, sizeof(keylayoutFilename),
582 "%s/usr/keylayout/%s.kl", root, tmpfn);
583 bool defaultKeymap = false;
584 if (access(keylayoutFilename, R_OK)) {
585 snprintf(keylayoutFilename, sizeof(keylayoutFilename),
586 "%s/usr/keylayout/%s", root, "qwerty.kl");
587 defaultKeymap = true;
588 }
589 device->layoutMap->load(keylayoutFilename);
590
591 // tell the world about the devname (the descriptive name)
592 int32_t publicID;
593 if (!mHaveFirstKeyboard && !defaultKeymap) {
594 publicID = 0;
595 // the built-in keyboard has a well-known device ID of 0,
596 // this device better not go away.
597 mHaveFirstKeyboard = true;
598 mFirstKeyboardId = device->id;
599 } else {
600 publicID = device->id;
601 // ensure mFirstKeyboardId is set to -something-.
602 if (mFirstKeyboardId == 0) {
603 mFirstKeyboardId = device->id;
604 }
605 }
606 char propName[100];
607 sprintf(propName, "hw.keyboards.%u.devname", publicID);
608 property_set(propName, devname);
609
610 LOGI("New keyboard: publicID=%d device->id=%d devname='%s propName='%s' keylayout='%s'\n",
611 publicID, device->id, devname, propName, keylayoutFilename);
612 }
613
614 LOGV("Adding device %s %p at %d, id = %d, classes = 0x%x\n",
615 deviceName, device, mFDCount, devid, device->classes);
616
617 mDevicesById[devid].device = device;
618 device->next = mOpeningDevices;
619 mOpeningDevices = device;
620 mDevices[mFDCount] = device;
621
622 mFDCount++;
623 return 0;
624}
625
626int EventHub::close_device(const char *deviceName)
627{
628 AutoMutex _l(mLock);
629
630 int i;
631 for(i = 1; i < mFDCount; i++) {
632 if(strcmp(mDevices[i]->path.string(), deviceName) == 0) {
633 //LOGD("remove device %d: %s\n", i, deviceName);
634 device_t* device = mDevices[i];
635 int count = mFDCount - i - 1;
636 int index = (device->id&ID_MASK);
637 mDevicesById[index].device = NULL;
638 memmove(mDevices + i, mDevices + i + 1, sizeof(mDevices[0]) * count);
639 memmove(mFDs + i, mFDs + i + 1, sizeof(mFDs[0]) * count);
640
641#ifdef EV_SW
642 for (int j=0; j<EV_SW; j++) {
643 if (mSwitches[j] == device->id) {
644 mSwitches[j] = 0;
645 }
646 }
647#endif
648
649 device->next = mClosingDevices;
650 mClosingDevices = device;
651
652 mFDCount--;
653
654 uint32_t publicID;
655 if (device->id == mFirstKeyboardId) {
656 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
657 device->path.string(), mFirstKeyboardId);
658 mFirstKeyboardId = 0;
659 publicID = 0;
660 } else {
661 publicID = device->id;
662 }
663 // clear the property
664 char propName[100];
665 sprintf(propName, "hw.keyboards.%u.devname", publicID);
666 property_set(propName, NULL);
667 return 0;
668 }
669 }
670 LOGE("remote device: %s not found\n", deviceName);
671 return -1;
672}
673
674int EventHub::read_notify(int nfd)
675{
676#ifdef HAVE_INOTIFY
677 int res;
678 char devname[PATH_MAX];
679 char *filename;
680 char event_buf[512];
681 int event_size;
682 int event_pos = 0;
683 struct inotify_event *event;
684
685 res = read(nfd, event_buf, sizeof(event_buf));
686 if(res < (int)sizeof(*event)) {
687 if(errno == EINTR)
688 return 0;
689 LOGW("could not get event, %s\n", strerror(errno));
690 return 1;
691 }
692 //printf("got %d bytes of event information\n", res);
693
694 strcpy(devname, device_path);
695 filename = devname + strlen(devname);
696 *filename++ = '/';
697
698 while(res >= (int)sizeof(*event)) {
699 event = (struct inotify_event *)(event_buf + event_pos);
700 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
701 if(event->len) {
702 strcpy(filename, event->name);
703 if(event->mask & IN_CREATE) {
704 open_device(devname);
705 }
706 else {
707 close_device(devname);
708 }
709 }
710 event_size = sizeof(*event) + event->len;
711 res -= event_size;
712 event_pos += event_size;
713 }
714#endif
715 return 0;
716}
717
718
719int EventHub::scan_dir(const char *dirname)
720{
721 char devname[PATH_MAX];
722 char *filename;
723 DIR *dir;
724 struct dirent *de;
725 dir = opendir(dirname);
726 if(dir == NULL)
727 return -1;
728 strcpy(devname, dirname);
729 filename = devname + strlen(devname);
730 *filename++ = '/';
731 while((de = readdir(dir))) {
732 if(de->d_name[0] == '.' &&
733 (de->d_name[1] == '\0' ||
734 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
735 continue;
736 strcpy(filename, de->d_name);
737 open_device(devname);
738 }
739 closedir(dir);
740 return 0;
741}
742
743}; // namespace android