blob: 700aa3a8ad75568d8c298b59b199279f409e43c8 [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>
The Android Open Source Project8a7a6752009-01-15 16:12:10 -080019#include <hardware_legacy/power.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070020
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;
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800523 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
524 if (test_bit(KEY_Q, key_bitmask)) {
525 device->classes |= CLASS_ALPHAKEY;
526 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700527 break;
528 }
529 }
530 }
531 if (test_bit(BTN_MOUSE, key_bitmask)) {
532 uint8_t rel_bitmask[(REL_MAX+1)/8];
533 memset(rel_bitmask, 0, sizeof(rel_bitmask));
534 LOGV("Getting relative controllers...");
535 if (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask) >= 0)
536 {
537 if (test_bit(REL_X, rel_bitmask) && test_bit(REL_Y, rel_bitmask)) {
538 device->classes |= CLASS_TRACKBALL;
539 }
540 }
541 }
542 if (test_bit(BTN_TOUCH, key_bitmask)) {
543 uint8_t abs_bitmask[(ABS_MAX+1)/8];
544 memset(abs_bitmask, 0, sizeof(abs_bitmask));
545 LOGV("Getting absolute controllers...");
546 if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) >= 0)
547 {
548 if (test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) {
549 device->classes |= CLASS_TOUCHSCREEN;
550 }
551 }
552 }
553
554#ifdef EV_SW
555 // figure out the switches this device reports
556 uint8_t sw_bitmask[(SW_MAX+1)/8];
557 memset(sw_bitmask, 0, sizeof(sw_bitmask));
558 if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask) >= 0) {
559 for (int i=0; i<EV_SW; i++) {
560 //LOGI("Device 0x%x sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask));
561 if (test_bit(i, sw_bitmask)) {
562 if (mSwitches[i] == 0) {
563 mSwitches[i] = device->id;
564 }
565 }
566 }
567 }
568#endif
569
570 LOGI("New device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n",
571 deviceName, name, device->id, mNumDevicesById, mFDCount, fd, device->classes);
572
573 if ((device->classes&CLASS_KEYBOARD) != 0) {
574 char devname[101];
575 char tmpfn[101];
576 char keylayoutFilename[300];
577
578 // a more descriptive name
579 ioctl(mFDs[mFDCount].fd, EVIOCGNAME(sizeof(devname)-1), devname);
580 devname[sizeof(devname)-1] = 0;
581 device->name = devname;
582
583 // replace all the spaces with underscores
584 strcpy(tmpfn, devname);
585 for (char *p = strchr(tmpfn, ' '); p && *p; p = strchr(tmpfn, ' '))
586 *p = '_';
587
588 // find the .kl file we need for this device
589 const char* root = getenv("ANDROID_ROOT");
590 snprintf(keylayoutFilename, sizeof(keylayoutFilename),
591 "%s/usr/keylayout/%s.kl", root, tmpfn);
592 bool defaultKeymap = false;
593 if (access(keylayoutFilename, R_OK)) {
594 snprintf(keylayoutFilename, sizeof(keylayoutFilename),
595 "%s/usr/keylayout/%s", root, "qwerty.kl");
596 defaultKeymap = true;
597 }
598 device->layoutMap->load(keylayoutFilename);
599
600 // tell the world about the devname (the descriptive name)
601 int32_t publicID;
602 if (!mHaveFirstKeyboard && !defaultKeymap) {
603 publicID = 0;
604 // the built-in keyboard has a well-known device ID of 0,
605 // this device better not go away.
606 mHaveFirstKeyboard = true;
607 mFirstKeyboardId = device->id;
608 } else {
609 publicID = device->id;
610 // ensure mFirstKeyboardId is set to -something-.
611 if (mFirstKeyboardId == 0) {
612 mFirstKeyboardId = device->id;
613 }
614 }
615 char propName[100];
616 sprintf(propName, "hw.keyboards.%u.devname", publicID);
617 property_set(propName, devname);
618
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800619 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 -0700620 publicID, device->id, devname, propName, keylayoutFilename);
621 }
622
623 LOGV("Adding device %s %p at %d, id = %d, classes = 0x%x\n",
624 deviceName, device, mFDCount, devid, device->classes);
625
626 mDevicesById[devid].device = device;
627 device->next = mOpeningDevices;
628 mOpeningDevices = device;
629 mDevices[mFDCount] = device;
630
631 mFDCount++;
632 return 0;
633}
634
635int EventHub::close_device(const char *deviceName)
636{
637 AutoMutex _l(mLock);
638
639 int i;
640 for(i = 1; i < mFDCount; i++) {
641 if(strcmp(mDevices[i]->path.string(), deviceName) == 0) {
642 //LOGD("remove device %d: %s\n", i, deviceName);
643 device_t* device = mDevices[i];
644 int count = mFDCount - i - 1;
645 int index = (device->id&ID_MASK);
646 mDevicesById[index].device = NULL;
647 memmove(mDevices + i, mDevices + i + 1, sizeof(mDevices[0]) * count);
648 memmove(mFDs + i, mFDs + i + 1, sizeof(mFDs[0]) * count);
649
650#ifdef EV_SW
651 for (int j=0; j<EV_SW; j++) {
652 if (mSwitches[j] == device->id) {
653 mSwitches[j] = 0;
654 }
655 }
656#endif
657
658 device->next = mClosingDevices;
659 mClosingDevices = device;
660
661 mFDCount--;
662
663 uint32_t publicID;
664 if (device->id == mFirstKeyboardId) {
665 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
666 device->path.string(), mFirstKeyboardId);
667 mFirstKeyboardId = 0;
668 publicID = 0;
669 } else {
670 publicID = device->id;
671 }
672 // clear the property
673 char propName[100];
674 sprintf(propName, "hw.keyboards.%u.devname", publicID);
675 property_set(propName, NULL);
676 return 0;
677 }
678 }
679 LOGE("remote device: %s not found\n", deviceName);
680 return -1;
681}
682
683int EventHub::read_notify(int nfd)
684{
685#ifdef HAVE_INOTIFY
686 int res;
687 char devname[PATH_MAX];
688 char *filename;
689 char event_buf[512];
690 int event_size;
691 int event_pos = 0;
692 struct inotify_event *event;
693
694 res = read(nfd, event_buf, sizeof(event_buf));
695 if(res < (int)sizeof(*event)) {
696 if(errno == EINTR)
697 return 0;
698 LOGW("could not get event, %s\n", strerror(errno));
699 return 1;
700 }
701 //printf("got %d bytes of event information\n", res);
702
703 strcpy(devname, device_path);
704 filename = devname + strlen(devname);
705 *filename++ = '/';
706
707 while(res >= (int)sizeof(*event)) {
708 event = (struct inotify_event *)(event_buf + event_pos);
709 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
710 if(event->len) {
711 strcpy(filename, event->name);
712 if(event->mask & IN_CREATE) {
713 open_device(devname);
714 }
715 else {
716 close_device(devname);
717 }
718 }
719 event_size = sizeof(*event) + event->len;
720 res -= event_size;
721 event_pos += event_size;
722 }
723#endif
724 return 0;
725}
726
727
728int EventHub::scan_dir(const char *dirname)
729{
730 char devname[PATH_MAX];
731 char *filename;
732 DIR *dir;
733 struct dirent *de;
734 dir = opendir(dirname);
735 if(dir == NULL)
736 return -1;
737 strcpy(devname, dirname);
738 filename = devname + strlen(devname);
739 *filename++ = '/';
740 while((de = readdir(dir))) {
741 if(de->d_name[0] == '.' &&
742 (de->d_name[1] == '\0' ||
743 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
744 continue;
745 strcpy(filename, de->d_name);
746 open_device(devname);
747 }
748 closedir(dir);
749 return 0;
750}
751
752}; // namespace android