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