gamepad suppord (fake d-pad digital btns) for input layer
[picodrive.git] / platform / linux / in_evdev.c
CommitLineData
4f96f79c 1#include <stdio.h>
483230c7 2#include <stdlib.h>
4f96f79c 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
3b13ec65 12#include "../common/common.h"
e1f99f2b 13#include "../common/input.h"
3b13ec65 14#include "in_evdev.h"
4f96f79c 15
483230c7 16typedef 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
819ef025 27#define KEYBITS_BIT(x) (keybits[(x)/sizeof(keybits[0])/8] & \
4f96f79c 28 (1 << ((x) & (sizeof(keybits[0])*8-1))))
29
483230c7 30#define KEYBITS_BIT_SET(x) (keybits[(x)/sizeof(keybits[0])/8] |= \
31 (1 << ((x) & (sizeof(keybits[0])*8-1))))
32
819ef025 33static const char * const in_evdev_prefix = "evdev:";
34static const char * const in_evdev_keys[KEY_MAX + 1] = {
3427e573 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
819ef025 121static void in_evdev_probe(void)
4f96f79c 122{
123 int i;
124
e1f99f2b 125 for (i = 0;; i++)
4f96f79c 126 {
483230c7 127 int keybits[(KEY_MAX+1)/sizeof(int)], absbits[(ABS_MAX+1)/sizeof(int)];
4f96f79c 128 int support = 0, count = 0;
483230c7 129 in_evdev_t *dev;
130 int u, ret, fd;
4f96f79c 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) {
e1f99f2b 141 printf("in_evdev: ioctl failed on %s\n", name);
4f96f79c 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) {
e1f99f2b 150 printf("in_evdev: ioctl failed on %s\n", name);
4f96f79c 151 goto skip;
152 }
153
4f96f79c 154 /* check for interesting keys */
819ef025 155 for (u = 0; u < KEY_MAX + 1; u++) {
156 if (KEYBITS_BIT(u) && u != KEY_POWER && u != KEY_SLEEP)
4f96f79c 157 count++;
158 }
159
160 if (count == 0)
161 goto skip;
162
483230c7 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
192no_abs:
193 dev->fd = fd;
819ef025 194 strcpy(name, in_evdev_prefix);
e1f99f2b 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);
483230c7 198 in_register(name, IN_DRVID_EVDEV, dev);
4f96f79c 199 continue;
200
201skip:
202 close(fd);
203 }
4f96f79c 204}
205
819ef025 206static void in_evdev_free(void *drv_data)
e1f99f2b 207{
483230c7 208 in_evdev_t *dev = drv_data;
209 if (dev == NULL)
210 return;
211 close(dev->fd);
212 free(dev);
e1f99f2b 213}
214
819ef025 215static int in_evdev_get_bind_count(void)
4f96f79c 216{
819ef025 217 return KEY_MAX + 1;
4f96f79c 218}
219
483230c7 220/* returns bitfield of binds of pressed buttons */
e1f99f2b 221int in_evdev_update(void *drv_data, int *binds)
4f96f79c 222{
223 struct input_event ev[16];
483230c7 224 struct input_absinfo ainfo;
819ef025 225 int keybits[(KEY_MAX+1)/sizeof(int)];
483230c7 226 in_evdev_t *dev = drv_data;
e1f99f2b 227 int result = 0, changed = 0;
228 int rd, ret, u;
4f96f79c 229
e1f99f2b 230 while (1) {
483230c7 231 rd = read(dev->fd, ev, sizeof(ev));
e1f99f2b 232 if (rd < (int)sizeof(ev[0])) {
233 if (errno != EAGAIN)
234 perror("in_evdev: read failed");
235 break;
4f96f79c 236 }
237
e1f99f2b 238 changed = 1;
239 }
4f96f79c 240
e1f99f2b 241 if (!changed)
483230c7 242 return dev->prev_update;
f484a9fe 243
483230c7 244 ret = ioctl(dev->fd, EVIOCGKEY(sizeof(keybits)), keybits);
e1f99f2b 245 if (ret == -1) {
246 perror("in_evdev: ioctl failed");
247 return 0;
248 }
4f96f79c 249
819ef025 250 for (u = 0; u < KEY_MAX + 1; u++) {
251 if (KEYBITS_BIT(u)) {
e1f99f2b 252 result |= binds[u];
4f96f79c 253 }
4f96f79c 254 }
255
483230c7 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;
4f96f79c 273 return result;
274}
275
483230c7 276static void in_evdev_set_blocking(void *drv_data, int y)
3b13ec65 277{
483230c7 278 in_evdev_t *dev = drv_data;
3b13ec65 279 long flags;
3427e573 280 int ret;
3b13ec65 281
483230c7 282 dev->prev_update = 0;
283
284 flags = (long)fcntl(dev->fd, F_GETFL);
3427e573 285 if ((int)flags == -1) {
286 perror("in_evdev: F_GETFL fcntl failed");
287 return;
3b13ec65 288 }
3427e573 289 if (y)
290 flags &= ~O_NONBLOCK;
291 else
292 flags |= O_NONBLOCK;
483230c7 293 ret = fcntl(dev->fd, F_SETFL, flags);
3427e573 294 if (ret == -1)
295 perror("in_evdev: F_SETFL fcntl failed");
296}
297
f484a9fe 298int in_evdev_update_keycode(void **data, int dcount, int *which, int *is_down, int timeout_ms)
3427e573 299{
483230c7 300 in_evdev_t **devs = (in_evdev_t **)data;
f484a9fe 301 struct timeval tv, *timeout = NULL;
3427e573 302 int i, fdmax = -1;
303
f484a9fe 304 if (timeout_ms > 0) {
305 tv.tv_sec = timeout_ms / 1000;
306 tv.tv_usec = (timeout_ms % 1000) * 1000;
307 timeout = &tv;
308 }
309
310 if (is_down != NULL)
311 *is_down = 0;
312
3427e573 313 for (i = 0; i < dcount; i++)
483230c7 314 if (devs[i]->fd > fdmax) fdmax = devs[i]->fd;
3b13ec65 315
316 while (1)
317 {
318 struct input_event ev[64];
483230c7 319 in_evdev_t *dev = NULL;
320 int ret, rd;
3b13ec65 321 fd_set fdset;
322
323 FD_ZERO(&fdset);
3427e573 324 for (i = 0; i < dcount; i++)
483230c7 325 FD_SET(devs[i]->fd, &fdset);
3b13ec65 326
f484a9fe 327 ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
3b13ec65 328 if (ret == -1)
329 {
330 perror("in_evdev: select failed");
331 sleep(1);
332 return 0;
333 }
334
f484a9fe 335 if (ret == 0)
336 return 0; /* timeout */
337
3427e573 338 for (i = 0; i < dcount; i++)
483230c7 339 if (FD_ISSET(devs[i]->fd, &fdset))
340 *which = i, dev = devs[i];
3b13ec65 341
483230c7 342 rd = read(dev->fd, ev, sizeof(ev[0]) * 64);
3b13ec65 343 if (rd < (int) sizeof(ev[0])) {
344 perror("in_evdev: error reading");
345 sleep(1);
346 return 0;
347 }
348
3b13ec65 349 for (i = 0; i < rd / sizeof(ev[0]); i++)
350 {
483230c7 351 if (ev[i].type == EV_KEY) {
352 if (ev[i].value < 0 || ev[i].value > 1)
353 continue;
354 if (is_down != NULL)
355 *is_down = ev[i].value;
356 return ev[i].code;
357 }
358 else if (ev[i].type == EV_ABS)
359 {
360 int down = 0;
361 if (dev->abs_lzone != 0 && ev[i].code == ABS_X) {
362 if (ev[i].value < dev->abs_lzone) {
363 down = 1;
364 dev->abs_lastx = KEY_LEFT;
365 }
366 else if (ev[i].value > dev->abs_rzone) {
367 down = 1;
368 dev->abs_lastx = KEY_RIGHT;
369 }
370 if (is_down != NULL)
371 *is_down = down;
372 return dev->abs_lastx;
373 }
374 if (dev->abs_tzone != 0 && ev[i].code == ABS_Y) {
375 if (ev[i].value < dev->abs_tzone) {
376 down = 1;
377 dev->abs_lasty = KEY_UP;
378 }
379 else if (ev[i].value > dev->abs_bzone) {
380 down = 1;
381 dev->abs_lasty = KEY_DOWN;
382 }
383 if (is_down != NULL)
384 *is_down = down;
385 return dev->abs_lasty;
386 }
387 }
3b13ec65 388 }
3b13ec65 389 }
3427e573 390}
3b13ec65 391
819ef025 392static int in_evdev_menu_translate(int keycode)
3427e573 393{
394 switch (keycode) {
395 /* keyboards */
396 case KEY_UP: return PBTN_UP;
397 case KEY_DOWN: return PBTN_DOWN;
398 case KEY_LEFT: return PBTN_LEFT;
399 case KEY_RIGHT: return PBTN_RIGHT;
483230c7 400 case KEY_ENTER:
401 case BTN_TRIGGER: return PBTN_EAST;
402 case KEY_ESC:
403 case BTN_THUMB: return PBTN_SOUTH;
3427e573 404 default: return 0;
3b13ec65 405 }
3427e573 406}
3b13ec65 407
819ef025 408static int in_evdev_get_key_code(const char *key_name)
409{
410 int i;
411
412 for (i = 0; i < KEY_MAX + 1; i++) {
413 const char *k = in_evdev_keys[i];
414 if (k != NULL && k[0] == key_name[0] &&
415 strcasecmp(k, key_name) == 0)
416 return i;
417 }
418
419 return -1;
420}
421
422static const char *in_evdev_get_key_name(int keycode)
3427e573 423{
424 const char *name = NULL;
819ef025 425 if (keycode >= 0 && keycode <= KEY_MAX)
3427e573 426 name = in_evdev_keys[keycode];
427 if (name == NULL)
428 name = "Unkn";
429
430 return name;
3b13ec65 431}
432
819ef025 433static const struct {
434 short code;
435 short bit;
436} in_evdev_def_binds[] =
437{
438 /* MXYZ SACB RLDU */
439 { KEY_UP, 0 },
440 { KEY_DOWN, 1 },
441 { KEY_LEFT, 2 },
442 { KEY_RIGHT, 3 },
443 { KEY_S, 4 }, /* B */
444 { KEY_D, 5 }, /* C */
445 { KEY_A, 6 }, /* A */
446 { KEY_ENTER, 7 },
447};
448
449#define DEF_BIND_COUNT (sizeof(in_evdev_def_binds) / sizeof(in_evdev_def_binds[0]))
450
451static void in_evdev_get_def_binds(int *binds)
452{
453 int i;
454
455 for (i = 0; i < DEF_BIND_COUNT; i++)
456 binds[in_evdev_def_binds[i].code] = 1 << in_evdev_def_binds[i].bit;
457}
458
483230c7 459/* remove binds of missing keys, count remaining ones */
819ef025 460static int in_evdev_clean_binds(void *drv_data, int *binds)
461{
462 int keybits[(KEY_MAX+1)/sizeof(int)];
483230c7 463 in_evdev_t *dev = drv_data;
819ef025 464 int i, ret, count = 0;
465
483230c7 466 ret = ioctl(dev->fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
819ef025 467 if (ret == -1) {
468 perror("in_evdev: ioctl failed");
469 memset(keybits, 0xff, sizeof(keybits)); /* mark all as good */
470 }
471
483230c7 472 if (dev->abs_lzone != 0) {
473 KEYBITS_BIT_SET(KEY_LEFT);
474 KEYBITS_BIT_SET(KEY_RIGHT);
475 }
476 if (dev->abs_tzone != 0) {
477 KEYBITS_BIT_SET(KEY_UP);
478 KEYBITS_BIT_SET(KEY_DOWN);
479 }
480
819ef025 481 for (i = 0; i < KEY_MAX + 1; i++) {
482 if (!KEYBITS_BIT(i))
483 binds[i] = binds[i + KEY_MAX + 1] = 0;
484 if (binds[i])
485 count++;
486 }
487
488 return count;
489}
490
491void in_evdev_init(void *vdrv)
492{
493 in_drv_t *drv = vdrv;
494
495 drv->prefix = in_evdev_prefix;
496 drv->probe = in_evdev_probe;
497 drv->free = in_evdev_free;
498 drv->get_bind_count = in_evdev_get_bind_count;
499 drv->get_def_binds = in_evdev_get_def_binds;
500 drv->clean_binds = in_evdev_clean_binds;
501 drv->set_blocking = in_evdev_set_blocking;
502 drv->menu_translate = in_evdev_menu_translate;
503 drv->get_key_code = in_evdev_get_key_code;
504 drv->get_key_name = in_evdev_get_key_name;
505}
506