OSDN Git Service

changed device disovery so that the first input device that provides SW_TABLET_MODE...
authorStefan Seidel <android@stefanseidel.info>
Tue, 28 Feb 2012 14:38:22 +0000 (15:38 +0100)
committerStefan Seidel <android@stefanseidel.info>
Tue, 28 Feb 2012 14:38:22 +0000 (15:38 +0100)
tablet-mode/tablet-mode.c

index 592343f..1ce4bed 100644 (file)
@@ -35,6 +35,7 @@ int openfd(void)
        DIR *dir;
        if ((dir = opendir(dirname))) {
                struct dirent *de;
+               unsigned long caps[NBITS(SW_TABLET_MODE+1)];
                while ((de = readdir(dir))) {
                        if (de->d_name[0] != 'e') // eventX
                                continue;
@@ -45,12 +46,12 @@ int openfd(void)
                                LOGE("could not open %s, %s", name, strerror(errno));
                                continue;
                        }
-                       name[sizeof(name) - 1] = '\0';
-                       if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
-                               LOGE("could not get device name for %s, %s\n", name, strerror(errno));
-                               name[0] = '\0';
+                       memset(caps, 0, sizeof(caps));
+                       if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(caps)), caps) < 1) {
+                               LOGE("could not get device caps for %s, %s\n", name, strerror(errno));
+                               continue;
                        }
-                       if (!strcmp(name, "ThinkPad Extra Buttons")) {
+                       if (test_bit(SW_TABLET_MODE, caps)) {
                                LOGI("open %s(%s) ok", de->d_name, name);
                                return fd;
                        }
@@ -67,7 +68,7 @@ void send_switch(int ufd, int state) {
        memset(&nev, 0, sizeof(struct input_event));
        nev.type = EV_SW;
        nev.code = SW_LID;
-       nev.value = state;
+       nev.value = !!state;
        write(ufd, &nev, sizeof(struct input_event));
        nev.type = EV_SYN;
        nev.code = SYN_REPORT;
@@ -79,11 +80,11 @@ int main(void)
 {
        int ifd = openfd();
        if (ifd < 0) {
-               LOGE("could not find ThinkPad Extra Buttons, exiting.");
+               LOGE("could not find any tablet mode switch, exiting.");
                return -1;
        }
 
-       sleep(15); //wait some time or otherwise EventHub might not pick up our event!?
+       sleep(15); //wait some time or otherwise EventHub might not pick up our events correctly!?
 
        int ufd = open("/dev/uinput", O_WRONLY | O_NDELAY);
        if (ufd >= 0) {
@@ -100,7 +101,7 @@ int main(void)
        }
 
        // send initial switch state
-       unsigned long sw_state[NBITS(SW_MAX+1)];
+       unsigned long sw_state[NBITS(SW_TABLET_MODE+1)];
        memset(sw_state, 0, sizeof(sw_state));
        if (ioctl(ifd, EVIOCGSW(sizeof(sw_state)), sw_state) >= 0) {
                send_switch(ufd, test_bit(SW_TABLET_MODE, sw_state) ? 0 : 1);
@@ -114,7 +115,7 @@ int main(void)
                        LOGW("insufficient input data(%d)? fd=%d", res, ifd);
                        continue;
                }
-//             LOGV("type=%d scancode=%d value=%d from fd=%d", iev.type, iev.code, iev.value, fd);
+//             LOGV("type=%d scancode=%d value=%d from fd=%d", iev.type, iev.code, iev.value, ifd);
                if (iev.type == EV_SW && iev.code == SW_TABLET_MODE) {
                        send_switch(ufd, !iev.value);
                }