input: support analog read
[picodrive.git] / platform / linux / in_evdev.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2008-2010
3  *
4  * This work is licensed under the terms of any of these licenses
5  * (at your option):
6  *  - GNU GPL, version 2 or later.
7  *  - GNU LGPL, version 2.1 or later.
8  * See the COPYING file in the top-level directory.
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <sys/ioctl.h>
18 #include <unistd.h>
19 #include <linux/input.h>
20 #include <errno.h>
21
22 #include "../common/input.h"
23 #include "in_evdev.h"
24
25 #define MAX_ABS_DEVS 8
26
27 typedef struct {
28         int fd;
29         int *kbits;
30         int abs_min_x; /* abs->digital mapping */
31         int abs_max_x;
32         int abs_min_y;
33         int abs_max_y;
34         int abs_lzone;
35         int abs_lastx;
36         int abs_lasty;
37         int kc_first;
38         int kc_last;
39         unsigned int abs_count;
40         /* 16.16 multiplier to IN_ABS_RANGE */
41         unsigned int abs_mult[MAX_ABS_DEVS];
42         unsigned int abs_to_digital:1;
43 } in_evdev_t;
44
45 #ifndef KEY_CNT
46 #define KEY_CNT (KEY_MAX + 1)
47 #endif
48
49 #define KEYBITS_BIT(x) (keybits[(x)/sizeof(keybits[0])/8] & \
50         (1 << ((x) & (sizeof(keybits[0])*8-1))))
51
52 #define KEYBITS_BIT_SET(x) (keybits[(x)/sizeof(keybits[0])/8] |= \
53         (1 << ((x) & (sizeof(keybits[0])*8-1))))
54
55 #define KEYBITS_BIT_CLEAR(x) (keybits[(x)/sizeof(keybits[0])/8] &= \
56         ~(1 << ((x) & (sizeof(keybits[0])*8-1))))
57
58 int in_evdev_allow_abs_only;
59
60 #define IN_EVDEV_PREFIX "evdev:"
61
62 static const char * const in_evdev_keys[KEY_CNT] = {
63         [0 ... KEY_MAX] = NULL,
64         [KEY_RESERVED] = "Reserved",            [KEY_ESC] = "Esc",
65         [KEY_1] = "1",                          [KEY_2] = "2",
66         [KEY_3] = "3",                          [KEY_4] = "4",
67         [KEY_5] = "5",                          [KEY_6] = "6",
68         [KEY_7] = "7",                          [KEY_8] = "8",
69         [KEY_9] = "9",                          [KEY_0] = "0",
70         [KEY_MINUS] = "Minus",                  [KEY_EQUAL] = "Equal",
71         [KEY_BACKSPACE] = "Backspace",          [KEY_TAB] = "Tab",
72         [KEY_Q] = "Q",                          [KEY_W] = "W",
73         [KEY_E] = "E",                          [KEY_R] = "R",
74         [KEY_T] = "T",                          [KEY_Y] = "Y",
75         [KEY_U] = "U",                          [KEY_I] = "I",
76         [KEY_O] = "O",                          [KEY_P] = "P",
77         [KEY_LEFTBRACE] = "LeftBrace",          [KEY_RIGHTBRACE] = "RightBrace",
78         [KEY_ENTER] = "Enter",                  [KEY_LEFTCTRL] = "LeftControl",
79         [KEY_A] = "A",                          [KEY_S] = "S",
80         [KEY_D] = "D",                          [KEY_F] = "F",
81         [KEY_G] = "G",                          [KEY_H] = "H",
82         [KEY_J] = "J",                          [KEY_K] = "K",
83         [KEY_L] = "L",                          [KEY_SEMICOLON] = "Semicolon",
84         [KEY_APOSTROPHE] = "Apostrophe",        [KEY_GRAVE] = "Grave",
85         [KEY_LEFTSHIFT] = "LeftShift",          [KEY_BACKSLASH] = "BackSlash",
86         [KEY_Z] = "Z",                          [KEY_X] = "X",
87         [KEY_C] = "C",                          [KEY_V] = "V",
88         [KEY_B] = "B",                          [KEY_N] = "N",
89         [KEY_M] = "M",                          [KEY_COMMA] = "Comma",
90         [KEY_DOT] = "Dot",                      [KEY_SLASH] = "Slash",
91         [KEY_RIGHTSHIFT] = "RightShift",        [KEY_KPASTERISK] = "KPAsterisk",
92         [KEY_LEFTALT] = "LeftAlt",              [KEY_SPACE] = "Space",
93         [KEY_CAPSLOCK] = "CapsLock",            [KEY_F1] = "F1",
94         [KEY_F2] = "F2",                        [KEY_F3] = "F3",
95         [KEY_F4] = "F4",                        [KEY_F5] = "F5",
96         [KEY_F6] = "F6",                        [KEY_F7] = "F7",
97         [KEY_F8] = "F8",                        [KEY_F9] = "F9",
98         [KEY_F10] = "F10",                      [KEY_NUMLOCK] = "NumLock",
99         [KEY_SCROLLLOCK] = "ScrollLock",        [KEY_KP7] = "KP7",
100         [KEY_KP8] = "KP8",                      [KEY_KP9] = "KP9",
101         [KEY_KPMINUS] = "KPMinus",              [KEY_KP4] = "KP4",
102         [KEY_KP5] = "KP5",                      [KEY_KP6] = "KP6",
103         [KEY_KPPLUS] = "KPPlus",                [KEY_KP1] = "KP1",
104         [KEY_KP2] = "KP2",                      [KEY_KP3] = "KP3",
105         [KEY_KP0] = "KP0",                      [KEY_KPDOT] = "KPDot",
106         [KEY_ZENKAKUHANKAKU] = "Zenkaku/Hankaku", [KEY_102ND] = "102nd",
107         [KEY_F11] = "F11",                      [KEY_F12] = "F12",
108         [KEY_KPJPCOMMA] = "KPJpComma",          [KEY_KPENTER] = "KPEnter",
109         [KEY_RIGHTCTRL] = "RightCtrl",          [KEY_KPSLASH] = "KPSlash",
110         [KEY_SYSRQ] = "SysRq",                  [KEY_RIGHTALT] = "RightAlt",
111         [KEY_LINEFEED] = "LineFeed",            [KEY_HOME] = "Home",
112         [KEY_UP] = "Up",                        [KEY_PAGEUP] = "PageUp",
113         [KEY_LEFT] = "Left",                    [KEY_RIGHT] = "Right",
114         [KEY_END] = "End",                      [KEY_DOWN] = "Down",
115         [KEY_PAGEDOWN] = "PageDown",            [KEY_INSERT] = "Insert",
116         [KEY_DELETE] = "Delete",                [KEY_MACRO] = "Macro",
117         [KEY_HELP] = "Help",                    [KEY_MENU] = "Menu",
118         [KEY_COFFEE] = "Coffee",                [KEY_DIRECTION] = "Direction",
119         [BTN_0] = "Btn0",                       [BTN_1] = "Btn1",
120         [BTN_2] = "Btn2",                       [BTN_3] = "Btn3",
121         [BTN_4] = "Btn4",                       [BTN_5] = "Btn5",
122         [BTN_6] = "Btn6",                       [BTN_7] = "Btn7",
123         [BTN_8] = "Btn8",                       [BTN_9] = "Btn9",
124         [BTN_LEFT] = "LeftBtn",                 [BTN_RIGHT] = "RightBtn",
125         [BTN_MIDDLE] = "MiddleBtn",             [BTN_SIDE] = "SideBtn",
126         [BTN_EXTRA] = "ExtraBtn",               [BTN_FORWARD] = "ForwardBtn",
127         [BTN_BACK] = "BackBtn",                 [BTN_TASK] = "TaskBtn",
128         [BTN_TRIGGER] = "Trigger",              [BTN_THUMB] = "ThumbBtn",
129         [BTN_THUMB2] = "ThumbBtn2",             [BTN_TOP] = "TopBtn",
130         [BTN_TOP2] = "TopBtn2",                 [BTN_PINKIE] = "PinkieBtn",
131         [BTN_BASE] = "BaseBtn",                 [BTN_BASE2] = "BaseBtn2",
132         [BTN_BASE3] = "BaseBtn3",               [BTN_BASE4] = "BaseBtn4",
133         [BTN_BASE5] = "BaseBtn5",               [BTN_BASE6] = "BaseBtn6",
134         [BTN_DEAD] = "BtnDead",                 [BTN_A] = "BtnA",
135         [BTN_B] = "BtnB",                       [BTN_C] = "BtnC",
136         [BTN_X] = "BtnX",                       [BTN_Y] = "BtnY",
137         [BTN_Z] = "BtnZ",                       [BTN_TL] = "BtnTL",
138         [BTN_TR] = "BtnTR",                     [BTN_TL2] = "BtnTL2",
139         [BTN_TR2] = "BtnTR2",                   [BTN_SELECT] = "BtnSelect",
140         [BTN_START] = "BtnStart",               [BTN_MODE] = "BtnMode",
141         [BTN_THUMBL] = "BtnThumbL",             [BTN_THUMBR] = "BtnThumbR",
142         [BTN_TOUCH] = "Touch",                  [BTN_STYLUS] = "Stylus",
143         [BTN_STYLUS2] = "Stylus2",              [BTN_TOOL_DOUBLETAP] = "Tool Doubletap",
144         [BTN_TOOL_TRIPLETAP] = "Tool Tripletap", [BTN_GEAR_DOWN] = "WheelBtn",
145         [BTN_GEAR_UP] = "Gear up",              [KEY_OK] = "Ok",
146 };
147
148
149 static void in_evdev_probe(void)
150 {
151         long keybits[KEY_CNT / sizeof(long) / 8];
152         long absbits[(ABS_MAX+1) / sizeof(long) / 8];
153         int i;
154
155         // the kernel might support and return less keys then we know about,
156         // so make sure the buffers are clear.
157         memset(keybits, 0, sizeof(keybits));
158         memset(absbits, 0, sizeof(absbits));
159
160         for (i = 0;; i++)
161         {
162                 int support = 0, count = 0;
163                 int u, ret, fd, kc_first = KEY_MAX, kc_last = 0, have_abs = 0;
164                 in_evdev_t *dev;
165                 char name[64];
166
167                 snprintf(name, sizeof(name), "/dev/input/event%d", i);
168                 fd = open(name, O_RDONLY|O_NONBLOCK);
169                 if (fd == -1) {
170                         if (errno == EACCES)
171                                 continue;       /* maybe we can access next one */
172                         break;
173                 }
174
175                 /* check supported events */
176                 ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support);
177                 if (ret == -1) {
178                         printf("in_evdev: ioctl failed on %s\n", name);
179                         goto skip;
180                 }
181
182                 if (support & (1 << EV_KEY)) {
183                         ret = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
184                         if (ret == -1) {
185                                 printf("in_evdev: ioctl failed on %s\n", name);
186                                 goto skip;
187                         }
188
189                         /* check for interesting keys */
190                         for (u = 0; u < KEY_CNT; u++) {
191                                 if (KEYBITS_BIT(u)) {
192                                         if (u < kc_first)
193                                                 kc_first = u;
194                                         if (u > kc_last)
195                                                 kc_last = u;
196                                         if (u != KEY_POWER && u != KEY_SLEEP && u != BTN_TOUCH)
197                                                 count++;
198                                         if (u == BTN_TOUCH) /* we can't deal with ts currently */
199                                                 goto skip;
200                                 }
201                         }
202                 }
203
204                 dev = calloc(1, sizeof(*dev));
205                 if (dev == NULL)
206                         goto skip;
207
208                 ret = ioctl(fd, EVIOCGKEY(sizeof(keybits)), keybits);
209                 if (ret == -1) {
210                         printf("Warning: EVIOCGKEY not supported, will have to track state\n");
211                         dev->kbits = calloc(KEY_CNT, sizeof(int));
212                         if (dev->kbits == NULL) {
213                                 free(dev);
214                                 goto skip;
215                         }
216                 }
217
218                 /* check for abs too */
219                 if (support & (1 << EV_ABS)) {
220                         struct input_absinfo ainfo;
221                         int dist;
222                         ret = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits);
223                         if (ret == -1)
224                                 goto no_abs;
225                         if (absbits[0] & (1 << ABS_X)) {
226                                 ret = ioctl(fd, EVIOCGABS(ABS_X), &ainfo);
227                                 if (ret == -1)
228                                         goto no_abs;
229                                 dist = ainfo.maximum - ainfo.minimum;
230                                 dev->abs_lzone = dist / 4;
231                                 dev->abs_min_x = ainfo.minimum;
232                                 dev->abs_max_x = ainfo.maximum;
233                         }
234                         if (absbits[0] & (1 << ABS_Y)) {
235                                 ret = ioctl(fd, EVIOCGABS(ABS_Y), &ainfo);
236                                 if (ret == -1)
237                                         goto no_abs;
238                                 dist = ainfo.maximum - ainfo.minimum;
239                                 dev->abs_min_y = ainfo.minimum;
240                                 dev->abs_max_y = ainfo.maximum;
241                         }
242                         for (u = 0; u < MAX_ABS_DEVS; u++) {
243                                 ret = ioctl(fd, EVIOCGABS(u), &ainfo);
244                                 if (ret == -1)
245                                         break;
246                                 dist = ainfo.maximum - ainfo.minimum;
247                                 if (dist != 0)
248                                         dev->abs_mult[u] = IN_ABS_RANGE * 2 * 65536 / dist;
249                                 have_abs = 1;
250                         }
251                         dev->abs_count = u;
252                 }
253
254 no_abs:
255                 if (count == 0 && !have_abs) {
256                         free(dev);
257                         goto skip;
258                 }
259
260                 dev->fd = fd;
261                 dev->kc_first = kc_first;
262                 dev->kc_last = kc_last;
263                 if (count > 0 || in_evdev_allow_abs_only)
264                         dev->abs_to_digital = 1;
265                 strcpy(name, IN_EVDEV_PREFIX);
266                 ioctl(fd, EVIOCGNAME(sizeof(name)-6), name+6);
267                 printf("in_evdev: found \"%s\" with %d events (type %08x)\n",
268                         name+6, count, support);
269                 in_register(name, fd, dev, KEY_CNT, in_evdev_keys, 0);
270                 continue;
271
272 skip:
273                 close(fd);
274         }
275 }
276
277 static void in_evdev_free(void *drv_data)
278 {
279         in_evdev_t *dev = drv_data;
280         if (dev == NULL)
281                 return;
282         close(dev->fd);
283         free(dev);
284 }
285
286 static const char * const *
287 in_evdev_get_key_names(int *count)
288 {
289         *count = KEY_CNT;
290         return in_evdev_keys;
291 }
292
293 static void or_binds(const int *binds, int key, int *result)
294 {
295         int t;
296         for (t = 0; t < IN_BINDTYPE_COUNT; t++)
297                 result[t] |= binds[IN_BIND_OFFS(key, t)];
298 }
299
300 /* ORs result with binds of pressed buttons
301  * XXX: should measure performance hit of this func, might need to optimize */
302 static int in_evdev_update(void *drv_data, const int *binds, int *result)
303 {
304         struct input_event ev[16];
305         struct input_absinfo ainfo;
306         int keybits_[KEY_CNT / sizeof(int)];
307         int *keybits = keybits_;
308         in_evdev_t *dev = drv_data;
309         int rd, ret, u, lzone;
310
311         if (dev->kbits == NULL) {
312                 ret = ioctl(dev->fd, EVIOCGKEY(sizeof(keybits_)), keybits_);
313                 if (ret == -1) {
314                         perror("in_evdev: ioctl failed");
315                         return -1;
316                 }
317         }
318         else {
319                 keybits = dev->kbits;
320                 while (1) {
321                         rd = read(dev->fd, ev, sizeof(ev));
322                         if (rd < (int)sizeof(ev[0])) {
323                                 if (errno != EAGAIN)
324                                         perror("in_evdev: read failed");
325                                 break;
326                         }
327                         for (u = 0; u < rd / sizeof(ev[0]); u++) {
328                                 if (ev[u].type != EV_KEY)
329                                         continue;
330                                 else if (ev[u].value == 1)
331                                         KEYBITS_BIT_SET(ev[u].code);
332                                 else if (ev[u].value == 0)
333                                         KEYBITS_BIT_CLEAR(ev[u].code);
334                         }
335                 }
336         }
337
338         for (u = dev->kc_first; u <= dev->kc_last; u++) {
339                 if (KEYBITS_BIT(u))
340                         or_binds(binds, u, result);
341         }
342
343         /* map X and Y absolute to UDLR */
344         lzone = dev->abs_lzone;
345         if (dev->abs_to_digital && lzone != 0) {
346                 ret = ioctl(dev->fd, EVIOCGABS(ABS_X), &ainfo);
347                 if (ret != -1) {
348                         if (ainfo.value < dev->abs_min_x + lzone) or_binds(binds, KEY_LEFT, result);
349                         if (ainfo.value > dev->abs_max_x - lzone) or_binds(binds, KEY_RIGHT, result);
350                 }
351                 ret = ioctl(dev->fd, EVIOCGABS(ABS_Y), &ainfo);
352                 if (ret != -1) {
353                         if (ainfo.value < dev->abs_min_y + lzone) or_binds(binds, KEY_UP, result);
354                         if (ainfo.value > dev->abs_max_y - lzone) or_binds(binds, KEY_DOWN, result);
355                 }
356         }
357
358         return 0;
359 }
360
361 static int in_evdev_update_analog(void *drv_data, int axis_id, int *result)
362 {
363         struct input_absinfo ainfo;
364         in_evdev_t *dev = drv_data;
365         int ret;
366
367         if ((unsigned int)axis_id >= MAX_ABS_DEVS)
368                 return -1;
369
370         ret = ioctl(dev->fd, EVIOCGABS(axis_id), &ainfo);
371         if (ret != 0)
372                 return ret;
373
374         *result = (int)(ainfo.value * dev->abs_mult[axis_id]) >> 16;
375         return 0;
376 }
377
378 static int in_evdev_set_blocking(in_evdev_t *dev, int y)
379 {
380         long flags;
381         int ret;
382
383         flags = (long)fcntl(dev->fd, F_GETFL);
384         if ((int)flags == -1) {
385                 perror("in_evdev: F_GETFL fcntl failed");
386                 return -1;
387         }
388
389         if (flags & O_NONBLOCK) {
390                 /* flush the event queue */
391                 struct input_event ev;
392                 do {
393                         ret = read(dev->fd, &ev, sizeof(ev));
394                 }
395                 while (ret == sizeof(ev));
396         }
397
398         if (y)
399                 flags &= ~O_NONBLOCK;
400         else
401                 flags |=  O_NONBLOCK;
402         ret = fcntl(dev->fd, F_SETFL, flags);
403         if (ret == -1) {
404                 perror("in_evdev: F_SETFL fcntl failed");
405                 return -1;
406         }
407
408         return 0;
409 }
410
411 static int in_evdev_get_config(void *drv_data, int what, int *val)
412 {
413         in_evdev_t *dev = drv_data;
414
415         switch (what) {
416         case IN_CFG_ABS_AXIS_COUNT:
417                 *val = dev->abs_count;
418                 break;
419         default:
420                 return -1;
421         }
422
423         return 0;
424 }
425
426 static int in_evdev_set_config(void *drv_data, int what, int val)
427 {
428         in_evdev_t *dev = drv_data;
429         int tmp;
430
431         switch (what) {
432         case IN_CFG_BLOCKING:
433                 return in_evdev_set_blocking(dev, val);
434         case IN_CFG_ABS_DEAD_ZONE:
435                 if (val < 1 || val > 99 || dev->abs_lzone == 0)
436                         return -1;
437                 /* XXX: based on X axis only, for now.. */
438                 tmp = (dev->abs_max_x - dev->abs_min_x) / 2;
439                 dev->abs_lzone = tmp - tmp * val / 100;
440                 if (dev->abs_lzone < 1)
441                         dev->abs_lzone = 1;
442                 else if (dev->abs_lzone >= tmp)
443                         dev->abs_lzone = tmp - 1;
444                 break;
445         default:
446                 return -1;
447         }
448
449         return 0;
450 }
451
452 static int in_evdev_update_keycode(void *data, int *is_down)
453 {
454         int ret_kc = -1, ret_down = 0;
455         in_evdev_t *dev = data;
456         struct input_event ev;
457         int rd;
458
459         /* do single event, the caller sometimes wants
460          * to do select() in blocking mode */
461         rd = read(dev->fd, &ev, sizeof(ev));
462         if (rd < (int) sizeof(ev)) {
463                 if (errno != EAGAIN) {
464                         perror("in_evdev: error reading");
465                         //sleep(1);
466                         ret_kc = -2;
467                 }
468                 goto out;
469         }
470
471         if (ev.type == EV_KEY) {
472                 if (ev.value < 0 || ev.value > 1)
473                         goto out;
474                 ret_kc = ev.code;
475                 ret_down = ev.value;
476                 goto out;
477         }
478         else if (ev.type == EV_ABS && dev->abs_to_digital)
479         {
480                 int lzone = dev->abs_lzone, down = 0, *last;
481
482                 // map absolute to up/down/left/right
483                 if (lzone != 0 && ev.code == ABS_X) {
484                         if (ev.value < dev->abs_min_x + lzone)
485                                 down = KEY_LEFT;
486                         else if (ev.value > dev->abs_max_x - lzone)
487                                 down = KEY_RIGHT;
488                         last = &dev->abs_lastx;
489                 }
490                 else if (lzone != 0 && ev.code == ABS_Y) {
491                         if (ev.value < dev->abs_min_y + lzone)
492                                 down = KEY_UP;
493                         else if (ev.value > dev->abs_max_y - lzone)
494                                 down = KEY_DOWN;
495                         last = &dev->abs_lasty;
496                 }
497                 else
498                         goto out;
499
500                 if (down == *last)
501                         goto out;
502
503                 if (down == 0 || *last != 0) {
504                         /* key up or direction change, return up event for old key */
505                         ret_kc = *last;
506                         ret_down = 0;
507                         *last = 0;
508                         goto out;
509                 }
510                 ret_kc = *last = down;
511                 ret_down = 1;
512                 goto out;
513         }
514
515 out:
516         if (is_down != NULL)
517                 *is_down = ret_down;
518
519         return ret_kc;
520 }
521
522 static const struct {
523         short key;
524         short pbtn;
525 } key_pbtn_map[] =
526 {
527         { KEY_UP,       PBTN_UP },
528         { KEY_DOWN,     PBTN_DOWN },
529         { KEY_LEFT,     PBTN_LEFT },
530         { KEY_RIGHT,    PBTN_RIGHT },
531         /* XXX: maybe better set this from it's plat code somehow */
532         /* Pandora */
533         { KEY_END,      PBTN_MOK },
534         { KEY_PAGEDOWN, PBTN_MBACK },
535         { KEY_HOME,     PBTN_MA2 },
536         { KEY_PAGEUP,   PBTN_MA3 },
537         { KEY_LEFTCTRL,   PBTN_MENU },
538         { KEY_RIGHTSHIFT, PBTN_L },
539         { KEY_RIGHTCTRL,  PBTN_R },
540         /* Caanoo */
541         { BTN_THUMB2,   PBTN_MOK },
542         { BTN_THUMB,    PBTN_MBACK },
543         { BTN_TRIGGER,  PBTN_MA2 },
544         { BTN_TOP,      PBTN_MA3 },
545         { BTN_BASE,     PBTN_MENU },
546         { BTN_TOP2,     PBTN_L },
547         { BTN_PINKIE,   PBTN_R },
548         /* "normal" keyboards */
549         { KEY_ENTER,    PBTN_MOK },
550         { KEY_ESC,      PBTN_MBACK },
551         { KEY_A,        PBTN_MA2 },
552         { KEY_S,        PBTN_MA3 },
553         { KEY_BACKSLASH,  PBTN_MENU },
554         { KEY_LEFTBRACE,  PBTN_L },
555         { KEY_RIGHTBRACE, PBTN_R },
556 };
557
558 #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
559
560 static int in_evdev_menu_translate(void *drv_data, int keycode)
561 {
562         in_evdev_t *dev = drv_data;
563         int i;
564
565         if (keycode < 0)
566         {
567                 /* menu -> kc */
568                 keycode = -keycode;
569                 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
570                         if (key_pbtn_map[i].pbtn == keycode) {
571                                 int k = key_pbtn_map[i].key;
572                                 /* should really check EVIOCGBIT, but this is enough for now */
573                                 if (dev->kc_first <= k && k <= dev->kc_last)
574                                         return k;
575                         }
576         }
577         else
578         {
579                 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
580                         if (key_pbtn_map[i].key == keycode)
581                                 return key_pbtn_map[i].pbtn;
582         }
583
584         return 0;
585 }
586
587 static void in_evdev_get_def_binds(int *binds)
588 {
589         int i;
590
591         for (i = 0; ; i++) {
592                 if (in_evdev_defbinds[i].bit == 0 && in_evdev_defbinds[i].code == 0)
593                         break;
594                 binds[IN_BIND_OFFS(in_evdev_defbinds[i].code, in_evdev_defbinds[i].btype)] =
595                         1 << in_evdev_defbinds[i].bit;
596         }
597 }
598
599 /* remove binds of missing keys, count remaining ones */
600 static int in_evdev_clean_binds(void *drv_data, int *binds, int *def_binds)
601 {
602         int keybits[KEY_CNT / sizeof(int)];
603         in_evdev_t *dev = drv_data;
604         int i, t, ret, offs, count = 0;
605
606         memset(keybits, 0, sizeof(keybits));
607         ret = ioctl(dev->fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
608         if (ret == -1) {
609                 perror("in_evdev: ioctl failed");
610                 // memset(keybits, 0xff, sizeof(keybits)); /* mark all as good */
611         }
612
613         if (dev->abs_to_digital && dev->abs_lzone != 0) {
614                 KEYBITS_BIT_SET(KEY_LEFT);
615                 KEYBITS_BIT_SET(KEY_RIGHT);
616                 KEYBITS_BIT_SET(KEY_UP);
617                 KEYBITS_BIT_SET(KEY_DOWN);
618         }
619
620         for (i = 0; i < KEY_CNT; i++) {
621                 for (t = 0; t < IN_BINDTYPE_COUNT; t++) {
622                         offs = IN_BIND_OFFS(i, t);
623                         if (!KEYBITS_BIT(i))
624                                 binds[offs] = def_binds[offs] = 0;
625                         if (binds[offs])
626                                 count++;
627                 }
628         }
629
630         return count;
631 }
632
633 static const in_drv_t in_evdev_drv = {
634         .prefix         = IN_EVDEV_PREFIX,
635         .probe          = in_evdev_probe,
636         .free           = in_evdev_free,
637         .get_key_names  = in_evdev_get_key_names,
638         .get_def_binds  = in_evdev_get_def_binds,
639         .clean_binds    = in_evdev_clean_binds,
640         .get_config     = in_evdev_get_config,
641         .set_config     = in_evdev_set_config,
642         .update         = in_evdev_update,
643         .update_analog  = in_evdev_update_analog,
644         .update_keycode = in_evdev_update_keycode,
645         .menu_translate = in_evdev_menu_translate,
646 };
647
648 void in_evdev_init(void)
649 {
650         in_register_driver(&in_evdev_drv);
651 }
652