blob: abe74077f9581b40409b69961bfb7e7defc9ba8f [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);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800318
319 // mFDs[0] is used for inotify, so process regular events starting at mFDs[1]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700320 for(i = 1; i < mFDCount; i++) {
321 if(mFDs[i].revents) {
322 LOGV("revents for %d = 0x%08x", i, mFDs[i].revents);
323 if(mFDs[i].revents & POLLIN) {
324 res = read(mFDs[i].fd, &iev, sizeof(iev));
325 if (res == sizeof(iev)) {
326 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, v=%d",
327 mDevices[i]->path.string(),
328 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
329 iev.type, iev.code, iev.value);
330 *outDeviceId = mDevices[i]->id;
331 if (*outDeviceId == mFirstKeyboardId) *outDeviceId = 0;
332 *outType = iev.type;
333 *outScancode = iev.code;
334 if (iev.type == EV_KEY) {
335 err = mDevices[i]->layoutMap->map(iev.code, outKeycode, outFlags);
336 LOGV("iev.code=%d outKeycode=%d outFlags=0x%08x err=%d\n",
337 iev.code, *outKeycode, *outFlags, err);
338 if (err != 0) {
339 *outKeycode = 0;
340 *outFlags = 0;
341 }
342 } else {
343 *outKeycode = iev.code;
344 }
345 *outValue = iev.value;
346 *outWhen = s2ns(iev.time.tv_sec) + us2ns(iev.time.tv_usec);
347 return true;
348 } else {
349 if (res<0) {
350 LOGW("could not get event (errno=%d)", errno);
351 } else {
352 LOGE("could not get event (wrong size: %d)", res);
353 }
354 continue;
355 }
356 }
357 }
358 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800359
360 // read_notify() will modify mFDs and mFDCount, so this must be done after
361 // processing all other events.
362 if(mFDs[0].revents & POLLIN) {
363 read_notify(mFDs[0].fd);
364 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700365 }
366}
367
368/*
369 * Open the platform-specific input device.
370 */
371bool EventHub::openPlatformInput(void)
372{
373 /*
374 * Open platform-specific input device(s).
375 */
376 int res;
377
378 mFDCount = 1;
379 mFDs = (pollfd *)calloc(1, sizeof(mFDs[0]));
380 mDevices = (device_t **)calloc(1, sizeof(mDevices[0]));
381 mFDs[0].events = POLLIN;
382 mDevices[0] = NULL;
383#ifdef HAVE_INOTIFY
384 mFDs[0].fd = inotify_init();
385 res = inotify_add_watch(mFDs[0].fd, device_path, IN_DELETE | IN_CREATE);
386 if(res < 0) {
387 LOGE("could not add watch for %s, %s\n", device_path, strerror(errno));
388 }
389#else
390 /*
391 * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd.
392 * We allocate space for it and set it to something invalid.
393 */
394 mFDs[0].fd = -1;
395#endif
396
397 res = scan_dir(device_path);
398 if(res < 0) {
399 LOGE("scan dir failed for %s\n", device_path);
400 //open_device("/dev/input/event0");
401 }
402
403 return true;
404}
405
406// ----------------------------------------------------------------------------
407
408int EventHub::open_device(const char *deviceName)
409{
410 int version;
411 int fd;
412 struct pollfd *new_mFDs;
413 device_t **new_devices;
414 char **new_device_names;
415 char name[80];
416 char location[80];
417 char idstr[80];
418 struct input_id id;
419
420 LOGV("Opening device: %s", deviceName);
421
422 AutoMutex _l(mLock);
423
424 fd = open(deviceName, O_RDWR);
425 if(fd < 0) {
426 LOGE("could not open %s, %s\n", deviceName, strerror(errno));
427 return -1;
428 }
429
430 if(ioctl(fd, EVIOCGVERSION, &version)) {
431 LOGE("could not get driver version for %s, %s\n", deviceName, strerror(errno));
432 return -1;
433 }
434 if(ioctl(fd, EVIOCGID, &id)) {
435 LOGE("could not get driver id for %s, %s\n", deviceName, strerror(errno));
436 return -1;
437 }
438 name[sizeof(name) - 1] = '\0';
439 location[sizeof(location) - 1] = '\0';
440 idstr[sizeof(idstr) - 1] = '\0';
441 if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
442 //fprintf(stderr, "could not get device name for %s, %s\n", deviceName, strerror(errno));
443 name[0] = '\0';
444 }
445 if(ioctl(fd, EVIOCGPHYS(sizeof(location) - 1), &location) < 1) {
446 //fprintf(stderr, "could not get location for %s, %s\n", deviceName, strerror(errno));
447 location[0] = '\0';
448 }
449 if(ioctl(fd, EVIOCGUNIQ(sizeof(idstr) - 1), &idstr) < 1) {
450 //fprintf(stderr, "could not get idstring for %s, %s\n", deviceName, strerror(errno));
451 idstr[0] = '\0';
452 }
453
454 int devid = 0;
455 while (devid < mNumDevicesById) {
456 if (mDevicesById[devid].device == NULL) {
457 break;
458 }
459 devid++;
460 }
461 if (devid >= mNumDevicesById) {
462 device_ent* new_devids = (device_ent*)realloc(mDevicesById,
463 sizeof(mDevicesById[0]) * (devid + 1));
464 if (new_devids == NULL) {
465 LOGE("out of memory");
466 return -1;
467 }
468 mDevicesById = new_devids;
469 mNumDevicesById = devid+1;
470 mDevicesById[devid].device = NULL;
471 mDevicesById[devid].seq = 0;
472 }
473
474 mDevicesById[devid].seq = (mDevicesById[devid].seq+(1<<SEQ_SHIFT))&SEQ_MASK;
475 if (mDevicesById[devid].seq == 0) {
476 mDevicesById[devid].seq = 1<<SEQ_SHIFT;
477 }
478
479 new_mFDs = (pollfd*)realloc(mFDs, sizeof(mFDs[0]) * (mFDCount + 1));
480 new_devices = (device_t**)realloc(mDevices, sizeof(mDevices[0]) * (mFDCount + 1));
481 if (new_mFDs == NULL || new_devices == NULL) {
482 LOGE("out of memory");
483 return -1;
484 }
485 mFDs = new_mFDs;
486 mDevices = new_devices;
487
488#if 0
489 LOGI("add device %d: %s\n", mFDCount, deviceName);
490 LOGI(" bus: %04x\n"
491 " vendor %04x\n"
492 " product %04x\n"
493 " version %04x\n",
494 id.bustype, id.vendor, id.product, id.version);
495 LOGI(" name: \"%s\"\n", name);
496 LOGI(" location: \"%s\"\n"
497 " id: \"%s\"\n", location, idstr);
498 LOGI(" version: %d.%d.%d\n",
499 version >> 16, (version >> 8) & 0xff, version & 0xff);
500#endif
501
502 device_t* device = new device_t(devid|mDevicesById[devid].seq, deviceName);
503 if (device == NULL) {
504 LOGE("out of memory");
505 return -1;
506 }
507
508 mFDs[mFDCount].fd = fd;
509 mFDs[mFDCount].events = POLLIN;
510
511 // figure out the kinds of events the device reports
512 uint8_t key_bitmask[(KEY_MAX+1)/8];
513 memset(key_bitmask, 0, sizeof(key_bitmask));
514 LOGV("Getting keys...");
515 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) >= 0) {
516 //LOGI("MAP\n");
517 //for (int i=0; i<((KEY_MAX+1)/8); i++) {
518 // LOGI("%d: 0x%02x\n", i, key_bitmask[i]);
519 //}
520 for (int i=0; i<((BTN_MISC+7)/8); i++) {
521 if (key_bitmask[i] != 0) {
522 device->classes |= CLASS_KEYBOARD;
523 break;
524 }
525 }
526 }
527 if (test_bit(BTN_MOUSE, key_bitmask)) {
528 uint8_t rel_bitmask[(REL_MAX+1)/8];
529 memset(rel_bitmask, 0, sizeof(rel_bitmask));
530 LOGV("Getting relative controllers...");
531 if (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask) >= 0)
532 {
533 if (test_bit(REL_X, rel_bitmask) && test_bit(REL_Y, rel_bitmask)) {
534 device->classes |= CLASS_TRACKBALL;
535 }
536 }
537 }
538 if (test_bit(BTN_TOUCH, key_bitmask)) {
539 uint8_t abs_bitmask[(ABS_MAX+1)/8];
540 memset(abs_bitmask, 0, sizeof(abs_bitmask));
541 LOGV("Getting absolute controllers...");
542 if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) >= 0)
543 {
544 if (test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) {
545 device->classes |= CLASS_TOUCHSCREEN;
546 }
547 }
548 }
549
550#ifdef EV_SW
551 // figure out the switches this device reports
552 uint8_t sw_bitmask[(SW_MAX+1)/8];
553 memset(sw_bitmask, 0, sizeof(sw_bitmask));
554 if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask) >= 0) {
555 for (int i=0; i<EV_SW; i++) {
556 //LOGI("Device 0x%x sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask));
557 if (test_bit(i, sw_bitmask)) {
558 if (mSwitches[i] == 0) {
559 mSwitches[i] = device->id;
560 }
561 }
562 }
563 }
564#endif
565
566 LOGI("New device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n",
567 deviceName, name, device->id, mNumDevicesById, mFDCount, fd, device->classes);
568
569 if ((device->classes&CLASS_KEYBOARD) != 0) {
570 char devname[101];
571 char tmpfn[101];
572 char keylayoutFilename[300];
573
574 // a more descriptive name
575 ioctl(mFDs[mFDCount].fd, EVIOCGNAME(sizeof(devname)-1), devname);
576 devname[sizeof(devname)-1] = 0;
577 device->name = devname;
578
579 // replace all the spaces with underscores
580 strcpy(tmpfn, devname);
581 for (char *p = strchr(tmpfn, ' '); p && *p; p = strchr(tmpfn, ' '))
582 *p = '_';
583
584 // find the .kl file we need for this device
585 const char* root = getenv("ANDROID_ROOT");
586 snprintf(keylayoutFilename, sizeof(keylayoutFilename),
587 "%s/usr/keylayout/%s.kl", root, tmpfn);
588 bool defaultKeymap = false;
589 if (access(keylayoutFilename, R_OK)) {
590 snprintf(keylayoutFilename, sizeof(keylayoutFilename),
591 "%s/usr/keylayout/%s", root, "qwerty.kl");
592 defaultKeymap = true;
593 }
594 device->layoutMap->load(keylayoutFilename);
595
596 // tell the world about the devname (the descriptive name)
597 int32_t publicID;
598 if (!mHaveFirstKeyboard && !defaultKeymap) {
599 publicID = 0;
600 // the built-in keyboard has a well-known device ID of 0,
601 // this device better not go away.
602 mHaveFirstKeyboard = true;
603 mFirstKeyboardId = device->id;
604 } else {
605 publicID = device->id;
606 // ensure mFirstKeyboardId is set to -something-.
607 if (mFirstKeyboardId == 0) {
608 mFirstKeyboardId = device->id;
609 }
610 }
611 char propName[100];
612 sprintf(propName, "hw.keyboards.%u.devname", publicID);
613 property_set(propName, devname);
614
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800615 LOGI("New keyboard: publicID=%d device->id=%d devname='%s' propName='%s' keylayout='%s'\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700616 publicID, device->id, devname, propName, keylayoutFilename);
617 }
618
619 LOGV("Adding device %s %p at %d, id = %d, classes = 0x%x\n",
620 deviceName, device, mFDCount, devid, device->classes);
621
622 mDevicesById[devid].device = device;
623 device->next = mOpeningDevices;
624 mOpeningDevices = device;
625 mDevices[mFDCount] = device;
626
627 mFDCount++;
628 return 0;
629}
630
631int EventHub::close_device(const char *deviceName)
632{
633 AutoMutex _l(mLock);
634
635 int i;
636 for(i = 1; i < mFDCount; i++) {
637 if(strcmp(mDevices[i]->path.string(), deviceName) == 0) {
638 //LOGD("remove device %d: %s\n", i, deviceName);
639 device_t* device = mDevices[i];
640 int count = mFDCount - i - 1;
641 int index = (device->id&ID_MASK);
642 mDevicesById[index].device = NULL;
643 memmove(mDevices + i, mDevices + i + 1, sizeof(mDevices[0]) * count);
644 memmove(mFDs + i, mFDs + i + 1, sizeof(mFDs[0]) * count);
645
646#ifdef EV_SW
647 for (int j=0; j<EV_SW; j++) {
648 if (mSwitches[j] == device->id) {
649 mSwitches[j] = 0;
650 }
651 }
652#endif
653
654 device->next = mClosingDevices;
655 mClosingDevices = device;
656
657 mFDCount--;
658
659 uint32_t publicID;
660 if (device->id == mFirstKeyboardId) {
661 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
662 device->path.string(), mFirstKeyboardId);
663 mFirstKeyboardId = 0;
664 publicID = 0;
665 } else {
666 publicID = device->id;
667 }
668 // clear the property
669 char propName[100];
670 sprintf(propName, "hw.keyboards.%u.devname", publicID);
671 property_set(propName, NULL);
672 return 0;
673 }
674 }
675 LOGE("remote device: %s not found\n", deviceName);
676 return -1;
677}
678
679int EventHub::read_notify(int nfd)
680{
681#ifdef HAVE_INOTIFY
682 int res;
683 char devname[PATH_MAX];
684 char *filename;
685 char event_buf[512];
686 int event_size;
687 int event_pos = 0;
688 struct inotify_event *event;
689
690 res = read(nfd, event_buf, sizeof(event_buf));
691 if(res < (int)sizeof(*event)) {
692 if(errno == EINTR)
693 return 0;
694 LOGW("could not get event, %s\n", strerror(errno));
695 return 1;
696 }
697 //printf("got %d bytes of event information\n", res);
698
699 strcpy(devname, device_path);
700 filename = devname + strlen(devname);
701 *filename++ = '/';
702
703 while(res >= (int)sizeof(*event)) {
704 event = (struct inotify_event *)(event_buf + event_pos);
705 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
706 if(event->len) {
707 strcpy(filename, event->name);
708 if(event->mask & IN_CREATE) {
709 open_device(devname);
710 }
711 else {
712 close_device(devname);
713 }
714 }
715 event_size = sizeof(*event) + event->len;
716 res -= event_size;
717 event_pos += event_size;
718 }
719#endif
720 return 0;
721}
722
723
724int EventHub::scan_dir(const char *dirname)
725{
726 char devname[PATH_MAX];
727 char *filename;
728 DIR *dir;
729 struct dirent *de;
730 dir = opendir(dirname);
731 if(dir == NULL)
732 return -1;
733 strcpy(devname, dirname);
734 filename = devname + strlen(devname);
735 *filename++ = '/';
736 while((de = readdir(dir))) {
737 if(de->d_name[0] == '.' &&
738 (de->d_name[1] == '\0' ||
739 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
740 continue;
741 strcpy(filename, de->d_name);
742 open_device(devname);
743 }
744 closedir(dir);
745 return 0;
746}
747
748}; // namespace android