fix Wiz timer drift and button name
[picodrive.git] / platform / linux / in_evdev.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <sys/ioctl.h>
8 #include <unistd.h>
9 #include <linux/input.h>
10 #include <errno.h>
11
12 #include "../common/input.h"
13 #include "in_evdev.h"
14
15 typedef struct {
16         int fd;
17         int abs_lzone;
18         int abs_rzone;
19         int abs_tzone;
20         int abs_bzone;
21         int abs_lastx;
22         int abs_lasty;
23 } in_evdev_t;
24
25 #ifndef KEY_CNT
26 #define KEY_CNT (KEY_MAX + 1)
27 #endif
28
29 #define KEYBITS_BIT(x) (keybits[(x)/sizeof(keybits[0])/8] & \
30         (1 << ((x) & (sizeof(keybits[0])*8-1))))
31
32 #define KEYBITS_BIT_SET(x) (keybits[(x)/sizeof(keybits[0])/8] |= \
33         (1 << ((x) & (sizeof(keybits[0])*8-1))))
34
35 static const char * const in_evdev_prefix = "evdev:";
36 static const char * const in_evdev_keys[KEY_CNT] = {
37         [0 ... KEY_MAX] = NULL,
38         [KEY_RESERVED] = "Reserved",            [KEY_ESC] = "Esc",
39         [KEY_1] = "1",                          [KEY_2] = "2",
40         [KEY_3] = "3",                          [KEY_4] = "4",
41         [KEY_5] = "5",                          [KEY_6] = "6",
42         [KEY_7] = "7",                          [KEY_8] = "8",
43         [KEY_9] = "9",                          [KEY_0] = "0",
44         [KEY_MINUS] = "Minus",                  [KEY_EQUAL] = "Equal",
45         [KEY_BACKSPACE] = "Backspace",          [KEY_TAB] = "Tab",
46         [KEY_Q] = "Q",                          [KEY_W] = "W",
47         [KEY_E] = "E",                          [KEY_R] = "R",
48         [KEY_T] = "T",                          [KEY_Y] = "Y",
49         [KEY_U] = "U",                          [KEY_I] = "I",
50         [KEY_O] = "O",                          [KEY_P] = "P",
51         [KEY_LEFTBRACE] = "LeftBrace",          [KEY_RIGHTBRACE] = "RightBrace",
52         [KEY_ENTER] = "Enter",                  [KEY_LEFTCTRL] = "LeftControl",
53         [KEY_A] = "A",                          [KEY_S] = "S",
54         [KEY_D] = "D",                          [KEY_F] = "F",
55         [KEY_G] = "G",                          [KEY_H] = "H",
56         [KEY_J] = "J",                          [KEY_K] = "K",
57         [KEY_L] = "L",                          [KEY_SEMICOLON] = "Semicolon",
58         [KEY_APOSTROPHE] = "Apostrophe",        [KEY_GRAVE] = "Grave",
59         [KEY_LEFTSHIFT] = "LeftShift",          [KEY_BACKSLASH] = "BackSlash",
60         [KEY_Z] = "Z",                          [KEY_X] = "X",
61         [KEY_C] = "C",                          [KEY_V] = "V",
62         [KEY_B] = "B",                          [KEY_N] = "N",
63         [KEY_M] = "M",                          [KEY_COMMA] = "Comma",
64         [KEY_DOT] = "Dot",                      [KEY_SLASH] = "Slash",
65         [KEY_RIGHTSHIFT] = "RightShift",        [KEY_KPASTERISK] = "KPAsterisk",
66         [KEY_LEFTALT] = "LeftAlt",              [KEY_SPACE] = "Space",
67         [KEY_CAPSLOCK] = "CapsLock",            [KEY_F1] = "F1",
68         [KEY_F2] = "F2",                        [KEY_F3] = "F3",
69         [KEY_F4] = "F4",                        [KEY_F5] = "F5",
70         [KEY_F6] = "F6",                        [KEY_F7] = "F7",
71         [KEY_F8] = "F8",                        [KEY_F9] = "F9",
72         [KEY_F10] = "F10",                      [KEY_NUMLOCK] = "NumLock",
73         [KEY_SCROLLLOCK] = "ScrollLock",        [KEY_KP7] = "KP7",
74         [KEY_KP8] = "KP8",                      [KEY_KP9] = "KP9",
75         [KEY_KPMINUS] = "KPMinus",              [KEY_KP4] = "KP4",
76         [KEY_KP5] = "KP5",                      [KEY_KP6] = "KP6",
77         [KEY_KPPLUS] = "KPPlus",                [KEY_KP1] = "KP1",
78         [KEY_KP2] = "KP2",                      [KEY_KP3] = "KP3",
79         [KEY_KP0] = "KP0",                      [KEY_KPDOT] = "KPDot",
80         [KEY_ZENKAKUHANKAKU] = "Zenkaku/Hankaku", [KEY_102ND] = "102nd",
81         [KEY_F11] = "F11",                      [KEY_F12] = "F12",
82         [KEY_KPJPCOMMA] = "KPJpComma",          [KEY_KPENTER] = "KPEnter",
83         [KEY_RIGHTCTRL] = "RightCtrl",          [KEY_KPSLASH] = "KPSlash",
84         [KEY_SYSRQ] = "SysRq",                  [KEY_RIGHTALT] = "RightAlt",
85         [KEY_LINEFEED] = "LineFeed",            [KEY_HOME] = "Home",
86         [KEY_UP] = "Up",                        [KEY_PAGEUP] = "PageUp",
87         [KEY_LEFT] = "Left",                    [KEY_RIGHT] = "Right",
88         [KEY_END] = "End",                      [KEY_DOWN] = "Down",
89         [KEY_PAGEDOWN] = "PageDown",            [KEY_INSERT] = "Insert",
90         [KEY_DELETE] = "Delete",                [KEY_MACRO] = "Macro",
91         [KEY_HELP] = "Help",                    [KEY_MENU] = "Menu",
92         [KEY_COFFEE] = "Coffee",                [KEY_DIRECTION] = "Direction",
93         [BTN_0] = "Btn0",                       [BTN_1] = "Btn1",
94         [BTN_2] = "Btn2",                       [BTN_3] = "Btn3",
95         [BTN_4] = "Btn4",                       [BTN_5] = "Btn5",
96         [BTN_6] = "Btn6",                       [BTN_7] = "Btn7",
97         [BTN_8] = "Btn8",                       [BTN_9] = "Btn9",
98         [BTN_LEFT] = "LeftBtn",                 [BTN_RIGHT] = "RightBtn",
99         [BTN_MIDDLE] = "MiddleBtn",             [BTN_SIDE] = "SideBtn",
100         [BTN_EXTRA] = "ExtraBtn",               [BTN_FORWARD] = "ForwardBtn",
101         [BTN_BACK] = "BackBtn",                 [BTN_TASK] = "TaskBtn",
102         [BTN_TRIGGER] = "Trigger",              [BTN_THUMB] = "ThumbBtn",
103         [BTN_THUMB2] = "ThumbBtn2",             [BTN_TOP] = "TopBtn",
104         [BTN_TOP2] = "TopBtn2",                 [BTN_PINKIE] = "PinkieBtn",
105         [BTN_BASE] = "BaseBtn",                 [BTN_BASE2] = "BaseBtn2",
106         [BTN_BASE3] = "BaseBtn3",               [BTN_BASE4] = "BaseBtn4",
107         [BTN_BASE5] = "BaseBtn5",               [BTN_BASE6] = "BaseBtn6",
108         [BTN_DEAD] = "BtnDead",                 [BTN_A] = "BtnA",
109         [BTN_B] = "BtnB",                       [BTN_C] = "BtnC",
110         [BTN_X] = "BtnX",                       [BTN_Y] = "BtnY",
111         [BTN_Z] = "BtnZ",                       [BTN_TL] = "BtnTL",
112         [BTN_TR] = "BtnTR",                     [BTN_TL2] = "BtnTL2",
113         [BTN_TR2] = "BtnTR2",                   [BTN_SELECT] = "BtnSelect",
114         [BTN_START] = "BtnStart",               [BTN_MODE] = "BtnMode",
115         [BTN_THUMBL] = "BtnThumbL",             [BTN_THUMBR] = "BtnThumbR",
116         [BTN_TOUCH] = "Touch",                  [BTN_STYLUS] = "Stylus",
117         [BTN_STYLUS2] = "Stylus2",              [BTN_TOOL_DOUBLETAP] = "Tool Doubletap",
118         [BTN_TOOL_TRIPLETAP] = "Tool Tripletap", [BTN_GEAR_DOWN] = "WheelBtn",
119         [BTN_GEAR_UP] = "Gear up",              [KEY_OK] = "Ok",
120 };
121
122
123 static void in_evdev_probe(void)
124 {
125         int i;
126
127         for (i = 0;; i++)
128         {
129                 int keybits[KEY_CNT / sizeof(int)], absbits[(ABS_MAX+1)/sizeof(int)];
130                 int support = 0, count = 0;
131                 in_evdev_t *dev;
132                 int u, ret, fd;
133                 char name[64];
134
135                 snprintf(name, sizeof(name), "/dev/input/event%d", i);
136                 fd = open(name, O_RDONLY|O_NONBLOCK);
137                 if (fd == -1) {
138                         if (errno == EACCES)
139                                 continue;       /* maybe we can access next one */
140                         break;
141                 }
142
143                 /* check supported events */
144                 ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support);
145                 if (ret == -1) {
146                         printf("in_evdev: ioctl failed on %s\n", name);
147                         goto skip;
148                 }
149
150                 if (!(support & (1 << EV_KEY)))
151                         goto skip;
152
153                 ret = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
154                 if (ret == -1) {
155                         printf("in_evdev: ioctl failed on %s\n", name);
156                         goto skip;
157                 }
158
159                 /* check for interesting keys */
160                 for (u = 0; u < KEY_CNT; u++) {
161                         if (KEYBITS_BIT(u) && u != KEY_POWER && u != KEY_SLEEP && u != BTN_TOUCH)
162                                 count++;
163                 }
164
165                 if (count == 0)
166                         goto skip;
167
168                 dev = calloc(1, sizeof(*dev));
169                 if (dev == NULL)
170                         goto skip;
171
172                 /* check for abs too */
173                 if (support & (1 << EV_ABS)) {
174                         struct input_absinfo ainfo;
175                         int dist;
176                         ret = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits);
177                         if (ret == -1)
178                                 goto no_abs;
179                         if (absbits[0] & (1 << ABS_X)) {
180                                 ret = ioctl(fd, EVIOCGABS(ABS_X), &ainfo);
181                                 if (ret == -1)
182                                         goto no_abs;
183                                 dist = ainfo.maximum - ainfo.minimum;
184                                 dev->abs_lzone = ainfo.minimum + dist / 4;
185                                 dev->abs_rzone = ainfo.maximum - dist / 4;
186                         }
187                         if (absbits[0] & (1 << ABS_Y)) {
188                                 ret = ioctl(fd, EVIOCGABS(ABS_Y), &ainfo);
189                                 if (ret == -1)
190                                         goto no_abs;
191                                 dist = ainfo.maximum - ainfo.minimum;
192                                 dev->abs_tzone = ainfo.minimum + dist / 4;
193                                 dev->abs_bzone = ainfo.maximum - dist / 4;
194                         }
195                 }
196
197 no_abs:
198                 dev->fd = fd;
199                 strcpy(name, in_evdev_prefix);
200                 ioctl(fd, EVIOCGNAME(sizeof(name)-6), name+6);
201                 printf("in_evdev: found \"%s\" with %d events (type %08x)\n",
202                         name+6, count, support);
203                 in_register(name, IN_DRVID_EVDEV, fd, dev, KEY_CNT, 0);
204                 continue;
205
206 skip:
207                 close(fd);
208         }
209 }
210
211 static void in_evdev_free(void *drv_data)
212 {
213         in_evdev_t *dev = drv_data;
214         if (dev == NULL)
215                 return;
216         close(dev->fd);
217         free(dev);
218 }
219
220 static int in_evdev_get_bind_count(void)
221 {
222         return KEY_CNT;
223 }
224
225 static void or_binds(const int *binds, int key, int *result)
226 {
227         int t;
228         for (t = 0; t < IN_BINDTYPE_COUNT; t++)
229                 result[t] |= binds[IN_BIND_OFFS(key, t)];
230 }
231
232 /* ORs result with binds of pressed buttons
233  * XXX: should measure performance hit of this func, might need to optimize */
234 int in_evdev_update(void *drv_data, const int *binds, int *result)
235 {
236         struct input_event ev[16];
237         struct input_absinfo ainfo;
238         int keybits[KEY_CNT / sizeof(int)];
239         in_evdev_t *dev = drv_data;
240         int rd, ret, u;
241
242         while (1) {
243                 rd = read(dev->fd, ev, sizeof(ev));
244                 if (rd < (int)sizeof(ev[0])) {
245                         if (errno != EAGAIN)
246                                 perror("in_evdev: read failed");
247                         break;
248                 }
249         }
250
251         ret = ioctl(dev->fd, EVIOCGKEY(sizeof(keybits)), keybits);
252         if (ret == -1) {
253                 perror("in_evdev: ioctl failed");
254                 return -1;
255         }
256
257         for (u = 0; u < KEY_CNT; u++) {
258                 if (KEYBITS_BIT(u))
259                         or_binds(binds, u, result);
260         }
261
262         /* map X and Y absolute to UDLR */
263         if (dev->abs_lzone != 0) {
264                 ret = ioctl(dev->fd, EVIOCGABS(ABS_X), &ainfo);
265                 if (ret != -1) {
266                         if (ainfo.value < dev->abs_lzone) or_binds(binds, KEY_LEFT, result);
267                         if (ainfo.value > dev->abs_rzone) or_binds(binds, KEY_RIGHT, result);
268                 }
269         }
270         if (dev->abs_tzone != 0) {
271                 ret = ioctl(dev->fd, EVIOCGABS(ABS_Y), &ainfo);
272                 if (ret != -1) {
273                         if (ainfo.value < dev->abs_tzone) or_binds(binds, KEY_UP, result);
274                         if (ainfo.value > dev->abs_bzone) or_binds(binds, KEY_DOWN, result);
275                 }
276         }
277
278         return 0;
279 }
280
281 static void in_evdev_set_blocking(void *drv_data, int y)
282 {
283         in_evdev_t *dev = drv_data;
284         long flags;
285         int ret;
286
287         flags = (long)fcntl(dev->fd, F_GETFL);
288         if ((int)flags == -1) {
289                 perror("in_evdev: F_GETFL fcntl failed");
290                 return;
291         }
292         if (y)
293                 flags &= ~O_NONBLOCK;
294         else
295                 flags |=  O_NONBLOCK;
296         ret = fcntl(dev->fd, F_SETFL, flags);
297         if (ret == -1)
298                 perror("in_evdev: F_SETFL fcntl failed");
299 }
300
301 static int in_evdev_update_keycode(void *data, int *is_down)
302 {
303         in_evdev_t *dev = data;
304         struct input_event ev;
305         int rd;
306
307         if (is_down != NULL)
308                 *is_down = 0;
309
310         rd = read(dev->fd, &ev, sizeof(ev));
311         if (rd < (int) sizeof(ev)) {
312                 if (errno != EAGAIN) {
313                         perror("in_evdev: error reading");
314                         sleep(1);
315                 }
316                 return -1;
317         }
318
319         if (ev.type == EV_KEY) {
320                 if (ev.value < 0 || ev.value > 1)
321                         return -1;
322                 if (is_down != NULL)
323                         *is_down = ev.value;
324                 return ev.code;
325         }
326         else if (ev.type == EV_ABS)
327         {
328                 int down = 0;
329                 if (dev->abs_lzone != 0 && ev.code == ABS_X) {
330                         if (ev.value < dev->abs_lzone) {
331                                 down = 1;
332                                 dev->abs_lastx = KEY_LEFT;
333                         }
334                         else if (ev.value > dev->abs_rzone) {
335                                 down = 1;
336                                 dev->abs_lastx = KEY_RIGHT;
337                         }
338                         if (is_down != NULL)
339                                 *is_down = down;
340                         return dev->abs_lastx;
341                 }
342                 if (dev->abs_tzone != 0 && ev.code == ABS_Y) {
343                         if (ev.value < dev->abs_tzone) {
344                                 down = 1;
345                                 dev->abs_lasty = KEY_UP;
346                         }
347                         else if (ev.value > dev->abs_bzone) {
348                                 down = 1;
349                                 dev->abs_lasty = KEY_DOWN;
350                         }
351                         if (is_down != NULL)
352                                 *is_down = down;
353                         return dev->abs_lasty;
354                 }
355         }
356
357         return -1;
358 }
359
360 static const struct {
361         short key;
362         short pbtn;
363 } key_pbtn_map[] =
364 {
365         { KEY_UP,       PBTN_UP },
366         { KEY_DOWN,     PBTN_DOWN },
367         { KEY_LEFT,     PBTN_LEFT },
368         { KEY_RIGHT,    PBTN_RIGHT },
369         { KEY_ENTER,    PBTN_MOK },
370         { BTN_A,        PBTN_MOK },
371         { BTN_TRIGGER,  PBTN_MOK },
372         { KEY_ESC,      PBTN_MBACK },
373         { BTN_B,        PBTN_MBACK },
374         { BTN_THUMB,    PBTN_MBACK },
375         { KEY_A,        PBTN_MA2 },
376         { KEY_S,        PBTN_MA3 },
377         { KEY_BACKSLASH,  PBTN_MENU },
378         { KEY_MENU,       PBTN_MENU },
379         { KEY_LEFTBRACE,  PBTN_L },
380         { KEY_RIGHTBRACE, PBTN_R },
381 };
382
383 #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
384
385 static int in_evdev_menu_translate(int keycode)
386 {
387         int i;
388         if (keycode < 0)
389         {
390                 /* menu -> kc */
391                 keycode = -keycode;
392                 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
393                         if (key_pbtn_map[i].pbtn == keycode)
394                                 return key_pbtn_map[i].key;
395         }
396         else
397         {
398                 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
399                         if (key_pbtn_map[i].key == keycode)
400                                 return key_pbtn_map[i].pbtn;
401         }
402
403         return 0;
404 }
405
406 static int in_evdev_get_key_code(const char *key_name)
407 {
408         int i;
409
410         for (i = 0; i < KEY_CNT; i++) {
411                 const char *k = in_evdev_keys[i];
412                 if (k != NULL && strcasecmp(k, key_name) == 0)
413                         return i;
414         }
415
416         return -1;
417 }
418
419 static const char *in_evdev_get_key_name(int keycode)
420 {
421         const char *name = NULL;
422         if (keycode >= 0 && keycode <= KEY_MAX)
423                 name = in_evdev_keys[keycode];
424         if (name == NULL)
425                 name = "Unkn";
426         
427         return name;
428 }
429
430 static const struct {
431         short code;
432         char btype;
433         char bit;
434 } in_evdev_def_binds[] =
435 {
436         /* MXYZ SACB RLDU */
437         { KEY_UP,       IN_BINDTYPE_PLAYER12, 0 },
438         { KEY_DOWN,     IN_BINDTYPE_PLAYER12, 1 },
439         { KEY_LEFT,     IN_BINDTYPE_PLAYER12, 2 },
440         { KEY_RIGHT,    IN_BINDTYPE_PLAYER12, 3 },
441         { KEY_S,        IN_BINDTYPE_PLAYER12, 4 },      /* B */
442         { BTN_B,        IN_BINDTYPE_PLAYER12, 4 },
443         { KEY_D,        IN_BINDTYPE_PLAYER12, 5 },      /* C */
444         { BTN_A,        IN_BINDTYPE_PLAYER12, 5 },
445         { KEY_A,        IN_BINDTYPE_PLAYER12, 6 },      /* A */
446         { BTN_Y,        IN_BINDTYPE_PLAYER12, 6 },
447         { KEY_ENTER,    IN_BINDTYPE_PLAYER12, 7 },
448         { BTN_START,    IN_BINDTYPE_PLAYER12, 7 },
449         { BTN_TL,       IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
450         { BTN_TR,       IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
451 };
452
453 #define DEF_BIND_COUNT (sizeof(in_evdev_def_binds) / sizeof(in_evdev_def_binds[0]))
454
455 static void in_evdev_get_def_binds(int *binds)
456 {
457         int i;
458
459         for (i = 0; i < DEF_BIND_COUNT; i++)
460                 binds[IN_BIND_OFFS(in_evdev_def_binds[i].code, in_evdev_def_binds[i].btype)] =
461                         1 << in_evdev_def_binds[i].bit;
462 }
463
464 /* remove binds of missing keys, count remaining ones */
465 static int in_evdev_clean_binds(void *drv_data, int *binds, int *def_binds)
466 {
467         int keybits[KEY_CNT / sizeof(int)];
468         in_evdev_t *dev = drv_data;
469         int i, t, ret, offs, count = 0;
470
471         ret = ioctl(dev->fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
472         if (ret == -1) {
473                 perror("in_evdev: ioctl failed");
474                 memset(keybits, 0xff, sizeof(keybits)); /* mark all as good */
475         }
476
477         if (dev->abs_lzone != 0) {
478                 KEYBITS_BIT_SET(KEY_LEFT);
479                 KEYBITS_BIT_SET(KEY_RIGHT);
480         }
481         if (dev->abs_tzone != 0) {
482                 KEYBITS_BIT_SET(KEY_UP);
483                 KEYBITS_BIT_SET(KEY_DOWN);
484         }
485
486         for (i = 0; i < KEY_CNT; i++) {
487                 for (t = 0; t < IN_BINDTYPE_COUNT; t++) {
488                         offs = IN_BIND_OFFS(i, t);
489                         if (!KEYBITS_BIT(i))
490                                 binds[offs] = def_binds[offs] = 0;
491                         if (binds[offs])
492                                 count++;
493                 }
494         }
495
496         return count;
497 }
498
499 void in_evdev_init(void *vdrv)
500 {
501         in_drv_t *drv = vdrv;
502
503         drv->prefix = in_evdev_prefix;
504         drv->probe = in_evdev_probe;
505         drv->free = in_evdev_free;
506         drv->get_bind_count = in_evdev_get_bind_count;
507         drv->get_def_binds = in_evdev_get_def_binds;
508         drv->clean_binds = in_evdev_clean_binds;
509         drv->set_blocking = in_evdev_set_blocking;
510         drv->update_keycode = in_evdev_update_keycode;
511         drv->menu_translate = in_evdev_menu_translate;
512         drv->get_key_code = in_evdev_get_key_code;
513         drv->get_key_name = in_evdev_get_key_name;
514 }
515