updated s/h arm asm code. Untested.
[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);
683bbd55 134 fd = open(name, O_RDONLY|O_NONBLOCK);
4f96f79c 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
963b7f4f 304 if (timeout_ms >= 0) {
f484a9fe 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 {
963b7f4f 318 struct input_event ev;
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
963b7f4f 342 rd = read(dev->fd, &ev, sizeof(ev));
343 if (rd < (int) sizeof(ev)) {
3b13ec65 344 perror("in_evdev: error reading");
345 sleep(1);
346 return 0;
347 }
348
963b7f4f 349 if (ev.type == EV_KEY) {
350 if (ev.value < 0 || ev.value > 1)
351 continue;
352 if (is_down != NULL)
353 *is_down = ev.value;
354 return ev.code;
355 }
356 else if (ev.type == EV_ABS)
3b13ec65 357 {
963b7f4f 358 int down = 0;
359 if (dev->abs_lzone != 0 && ev.code == ABS_X) {
360 if (ev.value < dev->abs_lzone) {
361 down = 1;
362 dev->abs_lastx = KEY_LEFT;
363 }
364 else if (ev.value > dev->abs_rzone) {
365 down = 1;
366 dev->abs_lastx = KEY_RIGHT;
367 }
483230c7 368 if (is_down != NULL)
963b7f4f 369 *is_down = down;
370 return dev->abs_lastx;
483230c7 371 }
963b7f4f 372 if (dev->abs_tzone != 0 && ev.code == ABS_Y) {
373 if (ev.value < dev->abs_tzone) {
374 down = 1;
375 dev->abs_lasty = KEY_UP;
483230c7 376 }
963b7f4f 377 else if (ev.value > dev->abs_bzone) {
378 down = 1;
379 dev->abs_lasty = KEY_DOWN;
483230c7 380 }
963b7f4f 381 if (is_down != NULL)
382 *is_down = down;
383 return dev->abs_lasty;
483230c7 384 }
3b13ec65 385 }
3b13ec65 386 }
3427e573 387}
3b13ec65 388
819ef025 389static int in_evdev_menu_translate(int keycode)
3427e573 390{
391 switch (keycode) {
3427e573 392 case KEY_UP: return PBTN_UP;
393 case KEY_DOWN: return PBTN_DOWN;
394 case KEY_LEFT: return PBTN_LEFT;
395 case KEY_RIGHT: return PBTN_RIGHT;
483230c7 396 case KEY_ENTER:
683bbd55 397 case BTN_A:
963b7f4f 398 case BTN_TRIGGER: return PBTN_MOK;
483230c7 399 case KEY_ESC:
683bbd55 400 case BTN_B:
963b7f4f 401 case BTN_THUMB: return PBTN_MBACK;
402 case KEY_MENU: return PBTN_MENU;
3427e573 403 default: return 0;
3b13ec65 404 }
3427e573 405}
3b13ec65 406
819ef025 407static int in_evdev_get_key_code(const char *key_name)
408{
409 int i;
410
411 for (i = 0; i < KEY_MAX + 1; i++) {
412 const char *k = in_evdev_keys[i];
413 if (k != NULL && k[0] == key_name[0] &&
414 strcasecmp(k, key_name) == 0)
415 return i;
416 }
417
418 return -1;
419}
420
421static const char *in_evdev_get_key_name(int keycode)
3427e573 422{
423 const char *name = NULL;
819ef025 424 if (keycode >= 0 && keycode <= KEY_MAX)
3427e573 425 name = in_evdev_keys[keycode];
426 if (name == NULL)
427 name = "Unkn";
428
429 return name;
3b13ec65 430}
431
819ef025 432static const struct {
433 short code;
434 short bit;
435} in_evdev_def_binds[] =
436{
437 /* MXYZ SACB RLDU */
438 { KEY_UP, 0 },
439 { KEY_DOWN, 1 },
440 { KEY_LEFT, 2 },
441 { KEY_RIGHT, 3 },
442 { KEY_S, 4 }, /* B */
683bbd55 443 { BTN_B, 4 },
819ef025 444 { KEY_D, 5 }, /* C */
683bbd55 445 { BTN_A, 5 },
819ef025 446 { KEY_A, 6 }, /* A */
683bbd55 447 { BTN_Y, 6 },
819ef025 448 { KEY_ENTER, 7 },
683bbd55 449 { BTN_START, 7 },
819ef025 450};
451
452#define DEF_BIND_COUNT (sizeof(in_evdev_def_binds) / sizeof(in_evdev_def_binds[0]))
453
454static void in_evdev_get_def_binds(int *binds)
455{
456 int i;
457
458 for (i = 0; i < DEF_BIND_COUNT; i++)
459 binds[in_evdev_def_binds[i].code] = 1 << in_evdev_def_binds[i].bit;
460}
461
483230c7 462/* remove binds of missing keys, count remaining ones */
819ef025 463static int in_evdev_clean_binds(void *drv_data, int *binds)
464{
465 int keybits[(KEY_MAX+1)/sizeof(int)];
483230c7 466 in_evdev_t *dev = drv_data;
819ef025 467 int i, ret, count = 0;
468
483230c7 469 ret = ioctl(dev->fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
819ef025 470 if (ret == -1) {
471 perror("in_evdev: ioctl failed");
472 memset(keybits, 0xff, sizeof(keybits)); /* mark all as good */
473 }
474
483230c7 475 if (dev->abs_lzone != 0) {
476 KEYBITS_BIT_SET(KEY_LEFT);
477 KEYBITS_BIT_SET(KEY_RIGHT);
478 }
479 if (dev->abs_tzone != 0) {
480 KEYBITS_BIT_SET(KEY_UP);
481 KEYBITS_BIT_SET(KEY_DOWN);
482 }
483
819ef025 484 for (i = 0; i < KEY_MAX + 1; i++) {
485 if (!KEYBITS_BIT(i))
486 binds[i] = binds[i + KEY_MAX + 1] = 0;
487 if (binds[i])
488 count++;
489 }
490
491 return count;
492}
493
494void in_evdev_init(void *vdrv)
495{
496 in_drv_t *drv = vdrv;
497
498 drv->prefix = in_evdev_prefix;
499 drv->probe = in_evdev_probe;
500 drv->free = in_evdev_free;
501 drv->get_bind_count = in_evdev_get_bind_count;
502 drv->get_def_binds = in_evdev_get_def_binds;
503 drv->clean_binds = in_evdev_clean_binds;
504 drv->set_blocking = in_evdev_set_blocking;
505 drv->menu_translate = in_evdev_menu_translate;
506 drv->get_key_code = in_evdev_get_key_code;
507 drv->get_key_name = in_evdev_get_key_name;
508}
509