improve linux makefile
[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);
7201dc73 198 in_register(name, IN_DRVID_EVDEV, fd, 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
7201dc73 298static int in_evdev_update_keycode(void *data, int *is_down)
3427e573 299{
7201dc73 300 in_evdev_t *dev = data;
301 struct input_event ev;
302 int rd;
f484a9fe 303
304 if (is_down != NULL)
305 *is_down = 0;
306
7201dc73 307 rd = read(dev->fd, &ev, sizeof(ev));
308 if (rd < (int) sizeof(ev)) {
217d08bc 309 if (errno != EAGAIN) {
310 perror("in_evdev: error reading");
311 sleep(1);
312 }
313 return -1;
7201dc73 314 }
3b13ec65 315
7201dc73 316 if (ev.type == EV_KEY) {
317 if (ev.value < 0 || ev.value > 1)
217d08bc 318 return -1;
7201dc73 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 }
963b7f4f 335 if (is_down != NULL)
7201dc73 336 *is_down = down;
337 return dev->abs_lastx;
963b7f4f 338 }
7201dc73 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;
483230c7 343 }
7201dc73 344 else if (ev.value > dev->abs_bzone) {
345 down = 1;
346 dev->abs_lasty = KEY_DOWN;
483230c7 347 }
7201dc73 348 if (is_down != NULL)
349 *is_down = down;
350 return dev->abs_lasty;
3b13ec65 351 }
3b13ec65 352 }
7201dc73 353
217d08bc 354 return -1;
3427e573 355}
3b13ec65 356
819ef025 357static int in_evdev_menu_translate(int keycode)
3427e573 358{
359 switch (keycode) {
3427e573 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;
483230c7 364 case KEY_ENTER:
683bbd55 365 case BTN_A:
963b7f4f 366 case BTN_TRIGGER: return PBTN_MOK;
483230c7 367 case KEY_ESC:
683bbd55 368 case BTN_B:
963b7f4f 369 case BTN_THUMB: return PBTN_MBACK;
370 case KEY_MENU: return PBTN_MENU;
3427e573 371 default: return 0;
3b13ec65 372 }
3427e573 373}
3b13ec65 374
819ef025 375static 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];
217d08bc 381 if (k != NULL && strcasecmp(k, key_name) == 0)
819ef025 382 return i;
383 }
384
385 return -1;
386}
387
388static const char *in_evdev_get_key_name(int keycode)
3427e573 389{
390 const char *name = NULL;
819ef025 391 if (keycode >= 0 && keycode <= KEY_MAX)
3427e573 392 name = in_evdev_keys[keycode];
393 if (name == NULL)
394 name = "Unkn";
395
396 return name;
3b13ec65 397}
398
819ef025 399static 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 */
683bbd55 410 { BTN_B, 4 },
819ef025 411 { KEY_D, 5 }, /* C */
683bbd55 412 { BTN_A, 5 },
819ef025 413 { KEY_A, 6 }, /* A */
683bbd55 414 { BTN_Y, 6 },
819ef025 415 { KEY_ENTER, 7 },
683bbd55 416 { BTN_START, 7 },
819ef025 417};
418
419#define DEF_BIND_COUNT (sizeof(in_evdev_def_binds) / sizeof(in_evdev_def_binds[0]))
420
421static 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
483230c7 429/* remove binds of missing keys, count remaining ones */
819ef025 430static int in_evdev_clean_binds(void *drv_data, int *binds)
431{
432 int keybits[(KEY_MAX+1)/sizeof(int)];
483230c7 433 in_evdev_t *dev = drv_data;
819ef025 434 int i, ret, count = 0;
435
483230c7 436 ret = ioctl(dev->fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
819ef025 437 if (ret == -1) {
438 perror("in_evdev: ioctl failed");
439 memset(keybits, 0xff, sizeof(keybits)); /* mark all as good */
440 }
441
483230c7 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
819ef025 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
461void 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;
7201dc73 472 drv->update_keycode = in_evdev_update_keycode;
819ef025 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