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