9 #include <linux/input.h>
12 #include "../common/input.h"
26 #define KEY_CNT (KEY_MAX + 1)
29 #define KEYBITS_BIT(x) (keybits[(x)/sizeof(keybits[0])/8] & \
30 (1 << ((x) & (sizeof(keybits[0])*8-1))))
32 #define KEYBITS_BIT_SET(x) (keybits[(x)/sizeof(keybits[0])/8] |= \
33 (1 << ((x) & (sizeof(keybits[0])*8-1))))
35 static const char * const in_evdev_prefix = "evdev:";
36 static const char * const in_evdev_keys[KEY_CNT] = {
37 [0 ... KEY_MAX] = NULL,
38 [KEY_RESERVED] = "Reserved", [KEY_ESC] = "Esc",
39 [KEY_1] = "1", [KEY_2] = "2",
40 [KEY_3] = "3", [KEY_4] = "4",
41 [KEY_5] = "5", [KEY_6] = "6",
42 [KEY_7] = "7", [KEY_8] = "8",
43 [KEY_9] = "9", [KEY_0] = "0",
44 [KEY_MINUS] = "Minus", [KEY_EQUAL] = "Equal",
45 [KEY_BACKSPACE] = "Backspace", [KEY_TAB] = "Tab",
46 [KEY_Q] = "Q", [KEY_W] = "W",
47 [KEY_E] = "E", [KEY_R] = "R",
48 [KEY_T] = "T", [KEY_Y] = "Y",
49 [KEY_U] = "U", [KEY_I] = "I",
50 [KEY_O] = "O", [KEY_P] = "P",
51 [KEY_LEFTBRACE] = "LeftBrace", [KEY_RIGHTBRACE] = "RightBrace",
52 [KEY_ENTER] = "Enter", [KEY_LEFTCTRL] = "LeftControl",
53 [KEY_A] = "A", [KEY_S] = "S",
54 [KEY_D] = "D", [KEY_F] = "F",
55 [KEY_G] = "G", [KEY_H] = "H",
56 [KEY_J] = "J", [KEY_K] = "K",
57 [KEY_L] = "L", [KEY_SEMICOLON] = "Semicolon",
58 [KEY_APOSTROPHE] = "Apostrophe", [KEY_GRAVE] = "Grave",
59 [KEY_LEFTSHIFT] = "LeftShift", [KEY_BACKSLASH] = "BackSlash",
60 [KEY_Z] = "Z", [KEY_X] = "X",
61 [KEY_C] = "C", [KEY_V] = "V",
62 [KEY_B] = "B", [KEY_N] = "N",
63 [KEY_M] = "M", [KEY_COMMA] = "Comma",
64 [KEY_DOT] = "Dot", [KEY_SLASH] = "Slash",
65 [KEY_RIGHTSHIFT] = "RightShift", [KEY_KPASTERISK] = "KPAsterisk",
66 [KEY_LEFTALT] = "LeftAlt", [KEY_SPACE] = "Space",
67 [KEY_CAPSLOCK] = "CapsLock", [KEY_F1] = "F1",
68 [KEY_F2] = "F2", [KEY_F3] = "F3",
69 [KEY_F4] = "F4", [KEY_F5] = "F5",
70 [KEY_F6] = "F6", [KEY_F7] = "F7",
71 [KEY_F8] = "F8", [KEY_F9] = "F9",
72 [KEY_F10] = "F10", [KEY_NUMLOCK] = "NumLock",
73 [KEY_SCROLLLOCK] = "ScrollLock", [KEY_KP7] = "KP7",
74 [KEY_KP8] = "KP8", [KEY_KP9] = "KP9",
75 [KEY_KPMINUS] = "KPMinus", [KEY_KP4] = "KP4",
76 [KEY_KP5] = "KP5", [KEY_KP6] = "KP6",
77 [KEY_KPPLUS] = "KPPlus", [KEY_KP1] = "KP1",
78 [KEY_KP2] = "KP2", [KEY_KP3] = "KP3",
79 [KEY_KP0] = "KP0", [KEY_KPDOT] = "KPDot",
80 [KEY_ZENKAKUHANKAKU] = "Zenkaku/Hankaku", [KEY_102ND] = "102nd",
81 [KEY_F11] = "F11", [KEY_F12] = "F12",
82 [KEY_KPJPCOMMA] = "KPJpComma", [KEY_KPENTER] = "KPEnter",
83 [KEY_RIGHTCTRL] = "RightCtrl", [KEY_KPSLASH] = "KPSlash",
84 [KEY_SYSRQ] = "SysRq", [KEY_RIGHTALT] = "RightAlt",
85 [KEY_LINEFEED] = "LineFeed", [KEY_HOME] = "Home",
86 [KEY_UP] = "Up", [KEY_PAGEUP] = "PageUp",
87 [KEY_LEFT] = "Left", [KEY_RIGHT] = "Right",
88 [KEY_END] = "End", [KEY_DOWN] = "Down",
89 [KEY_PAGEDOWN] = "PageDown", [KEY_INSERT] = "Insert",
90 [KEY_DELETE] = "Delete", [KEY_MACRO] = "Macro",
91 [KEY_HELP] = "Help", [KEY_MENU] = "Menu",
92 [KEY_COFFEE] = "Coffee", [KEY_DIRECTION] = "Direction",
93 [BTN_0] = "Btn0", [BTN_1] = "Btn1",
94 [BTN_2] = "Btn2", [BTN_3] = "Btn3",
95 [BTN_4] = "Btn4", [BTN_5] = "Btn5",
96 [BTN_6] = "Btn6", [BTN_7] = "Btn7",
97 [BTN_8] = "Btn8", [BTN_9] = "Btn9",
98 [BTN_LEFT] = "LeftBtn", [BTN_RIGHT] = "RightBtn",
99 [BTN_MIDDLE] = "MiddleBtn", [BTN_SIDE] = "SideBtn",
100 [BTN_EXTRA] = "ExtraBtn", [BTN_FORWARD] = "ForwardBtn",
101 [BTN_BACK] = "BackBtn", [BTN_TASK] = "TaskBtn",
102 [BTN_TRIGGER] = "Trigger", [BTN_THUMB] = "ThumbBtn",
103 [BTN_THUMB2] = "ThumbBtn2", [BTN_TOP] = "TopBtn",
104 [BTN_TOP2] = "TopBtn2", [BTN_PINKIE] = "PinkieBtn",
105 [BTN_BASE] = "BaseBtn", [BTN_BASE2] = "BaseBtn2",
106 [BTN_BASE3] = "BaseBtn3", [BTN_BASE4] = "BaseBtn4",
107 [BTN_BASE5] = "BaseBtn5", [BTN_BASE6] = "BaseBtn6",
108 [BTN_DEAD] = "BtnDead", [BTN_A] = "BtnA",
109 [BTN_B] = "BtnB", [BTN_C] = "BtnC",
110 [BTN_X] = "BtnX", [BTN_Y] = "BtnY",
111 [BTN_Z] = "BtnZ", [BTN_TL] = "BtnTL",
112 [BTN_TR] = "BtnTR", [BTN_TL2] = "BtnTL2",
113 [BTN_TR2] = "BtnTR2", [BTN_SELECT] = "BtnSelect",
114 [BTN_START] = "BtnStart", [BTN_MODE] = "BtnMode",
115 [BTN_THUMBL] = "BtnThumbL", [BTN_THUMBR] = "BtnThumbR",
116 [BTN_TOUCH] = "Touch", [BTN_STYLUS] = "Stylus",
117 [BTN_STYLUS2] = "Stylus2", [BTN_TOOL_DOUBLETAP] = "Tool Doubletap",
118 [BTN_TOOL_TRIPLETAP] = "Tool Tripletap", [BTN_GEAR_DOWN] = "WheelBtn",
119 [BTN_GEAR_UP] = "Gear up", [KEY_OK] = "Ok",
123 static void in_evdev_probe(void)
129 int keybits[KEY_CNT / sizeof(int)], absbits[(ABS_MAX+1)/sizeof(int)];
130 int support = 0, count = 0;
135 snprintf(name, sizeof(name), "/dev/input/event%d", i);
136 fd = open(name, O_RDONLY|O_NONBLOCK);
139 continue; /* maybe we can access next one */
143 /* check supported events */
144 ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support);
146 printf("in_evdev: ioctl failed on %s\n", name);
150 if (!(support & (1 << EV_KEY)))
153 ret = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
155 printf("in_evdev: ioctl failed on %s\n", name);
159 /* check for interesting keys */
160 for (u = 0; u < KEY_CNT; u++) {
161 if (KEYBITS_BIT(u) && u != KEY_POWER && u != KEY_SLEEP && u != BTN_TOUCH)
168 dev = calloc(1, sizeof(*dev));
172 /* check for abs too */
173 if (support & (1 << EV_ABS)) {
174 struct input_absinfo ainfo;
176 ret = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits);
179 if (absbits[0] & (1 << ABS_X)) {
180 ret = ioctl(fd, EVIOCGABS(ABS_X), &ainfo);
183 dist = ainfo.maximum - ainfo.minimum;
184 dev->abs_lzone = ainfo.minimum + dist / 4;
185 dev->abs_rzone = ainfo.maximum - dist / 4;
187 if (absbits[0] & (1 << ABS_Y)) {
188 ret = ioctl(fd, EVIOCGABS(ABS_Y), &ainfo);
191 dist = ainfo.maximum - ainfo.minimum;
192 dev->abs_tzone = ainfo.minimum + dist / 4;
193 dev->abs_bzone = ainfo.maximum - dist / 4;
199 strcpy(name, in_evdev_prefix);
200 ioctl(fd, EVIOCGNAME(sizeof(name)-6), name+6);
201 printf("in_evdev: found \"%s\" with %d events (type %08x)\n",
202 name+6, count, support);
203 in_register(name, IN_DRVID_EVDEV, fd, dev, KEY_CNT, 0);
211 static void in_evdev_free(void *drv_data)
213 in_evdev_t *dev = drv_data;
220 static int in_evdev_get_bind_count(void)
225 static void or_binds(const int *binds, int key, int *result)
228 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
229 result[t] |= binds[IN_BIND_OFFS(key, t)];
232 /* ORs result with binds of pressed buttons
233 * XXX: should measure performance hit of this func, might need to optimize */
234 int in_evdev_update(void *drv_data, const int *binds, int *result)
236 struct input_event ev[16];
237 struct input_absinfo ainfo;
238 int keybits[KEY_CNT / sizeof(int)];
239 in_evdev_t *dev = drv_data;
243 rd = read(dev->fd, ev, sizeof(ev));
244 if (rd < (int)sizeof(ev[0])) {
246 perror("in_evdev: read failed");
251 ret = ioctl(dev->fd, EVIOCGKEY(sizeof(keybits)), keybits);
253 perror("in_evdev: ioctl failed");
257 for (u = 0; u < KEY_CNT; u++) {
259 or_binds(binds, u, result);
262 /* map X and Y absolute to UDLR */
263 if (dev->abs_lzone != 0) {
264 ret = ioctl(dev->fd, EVIOCGABS(ABS_X), &ainfo);
266 if (ainfo.value < dev->abs_lzone) or_binds(binds, KEY_LEFT, result);
267 if (ainfo.value > dev->abs_rzone) or_binds(binds, KEY_RIGHT, result);
270 if (dev->abs_tzone != 0) {
271 ret = ioctl(dev->fd, EVIOCGABS(ABS_Y), &ainfo);
273 if (ainfo.value < dev->abs_tzone) or_binds(binds, KEY_UP, result);
274 if (ainfo.value > dev->abs_bzone) or_binds(binds, KEY_DOWN, result);
281 static void in_evdev_set_blocking(void *drv_data, int y)
283 in_evdev_t *dev = drv_data;
287 flags = (long)fcntl(dev->fd, F_GETFL);
288 if ((int)flags == -1) {
289 perror("in_evdev: F_GETFL fcntl failed");
293 flags &= ~O_NONBLOCK;
296 ret = fcntl(dev->fd, F_SETFL, flags);
298 perror("in_evdev: F_SETFL fcntl failed");
301 static int in_evdev_update_keycode(void *data, int *is_down)
303 in_evdev_t *dev = data;
304 struct input_event ev;
310 rd = read(dev->fd, &ev, sizeof(ev));
311 if (rd < (int) sizeof(ev)) {
312 if (errno != EAGAIN) {
313 perror("in_evdev: error reading");
319 if (ev.type == EV_KEY) {
320 if (ev.value < 0 || ev.value > 1)
326 else if (ev.type == EV_ABS)
329 if (dev->abs_lzone != 0 && ev.code == ABS_X) {
330 if (ev.value < dev->abs_lzone) {
332 dev->abs_lastx = KEY_LEFT;
334 else if (ev.value > dev->abs_rzone) {
336 dev->abs_lastx = KEY_RIGHT;
340 return dev->abs_lastx;
342 if (dev->abs_tzone != 0 && ev.code == ABS_Y) {
343 if (ev.value < dev->abs_tzone) {
345 dev->abs_lasty = KEY_UP;
347 else if (ev.value > dev->abs_bzone) {
349 dev->abs_lasty = KEY_DOWN;
353 return dev->abs_lasty;
360 static const struct {
366 { KEY_DOWN, PBTN_DOWN },
367 { KEY_LEFT, PBTN_LEFT },
368 { KEY_RIGHT, PBTN_RIGHT },
369 { KEY_ENTER, PBTN_MOK },
371 { BTN_TRIGGER, PBTN_MOK },
372 { KEY_ESC, PBTN_MBACK },
373 { BTN_B, PBTN_MBACK },
374 { BTN_THUMB, PBTN_MBACK },
377 { KEY_BACKSLASH, PBTN_MENU },
378 { KEY_MENU, PBTN_MENU },
379 { KEY_LEFTBRACE, PBTN_L },
380 { KEY_RIGHTBRACE, PBTN_R },
383 #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
385 static int in_evdev_menu_translate(int keycode)
392 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
393 if (key_pbtn_map[i].pbtn == keycode)
394 return key_pbtn_map[i].key;
398 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
399 if (key_pbtn_map[i].key == keycode)
400 return key_pbtn_map[i].pbtn;
406 static int in_evdev_get_key_code(const char *key_name)
410 for (i = 0; i < KEY_CNT; i++) {
411 const char *k = in_evdev_keys[i];
412 if (k != NULL && strcasecmp(k, key_name) == 0)
419 static const char *in_evdev_get_key_name(int keycode)
421 const char *name = NULL;
422 if (keycode >= 0 && keycode <= KEY_MAX)
423 name = in_evdev_keys[keycode];
430 static const struct {
434 } in_evdev_def_binds[] =
437 { KEY_UP, IN_BINDTYPE_PLAYER12, 0 },
438 { KEY_DOWN, IN_BINDTYPE_PLAYER12, 1 },
439 { KEY_LEFT, IN_BINDTYPE_PLAYER12, 2 },
440 { KEY_RIGHT, IN_BINDTYPE_PLAYER12, 3 },
441 { KEY_S, IN_BINDTYPE_PLAYER12, 4 }, /* B */
442 { BTN_B, IN_BINDTYPE_PLAYER12, 4 },
443 { KEY_D, IN_BINDTYPE_PLAYER12, 5 }, /* C */
444 { BTN_A, IN_BINDTYPE_PLAYER12, 5 },
445 { KEY_A, IN_BINDTYPE_PLAYER12, 6 }, /* A */
446 { BTN_Y, IN_BINDTYPE_PLAYER12, 6 },
447 { KEY_ENTER, IN_BINDTYPE_PLAYER12, 7 },
448 { BTN_START, IN_BINDTYPE_PLAYER12, 7 },
449 { BTN_TL, IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
450 { BTN_TR, IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
453 #define DEF_BIND_COUNT (sizeof(in_evdev_def_binds) / sizeof(in_evdev_def_binds[0]))
455 static void in_evdev_get_def_binds(int *binds)
459 for (i = 0; i < DEF_BIND_COUNT; i++)
460 binds[IN_BIND_OFFS(in_evdev_def_binds[i].code, in_evdev_def_binds[i].btype)] =
461 1 << in_evdev_def_binds[i].bit;
464 /* remove binds of missing keys, count remaining ones */
465 static int in_evdev_clean_binds(void *drv_data, int *binds, int *def_binds)
467 int keybits[KEY_CNT / sizeof(int)];
468 in_evdev_t *dev = drv_data;
469 int i, t, ret, offs, count = 0;
471 ret = ioctl(dev->fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
473 perror("in_evdev: ioctl failed");
474 memset(keybits, 0xff, sizeof(keybits)); /* mark all as good */
477 if (dev->abs_lzone != 0) {
478 KEYBITS_BIT_SET(KEY_LEFT);
479 KEYBITS_BIT_SET(KEY_RIGHT);
481 if (dev->abs_tzone != 0) {
482 KEYBITS_BIT_SET(KEY_UP);
483 KEYBITS_BIT_SET(KEY_DOWN);
486 for (i = 0; i < KEY_CNT; i++) {
487 for (t = 0; t < IN_BINDTYPE_COUNT; t++) {
488 offs = IN_BIND_OFFS(i, t);
490 binds[offs] = def_binds[offs] = 0;
499 void in_evdev_init(void *vdrv)
501 in_drv_t *drv = vdrv;
503 drv->prefix = in_evdev_prefix;
504 drv->probe = in_evdev_probe;
505 drv->free = in_evdev_free;
506 drv->get_bind_count = in_evdev_get_bind_count;
507 drv->get_def_binds = in_evdev_get_def_binds;
508 drv->clean_binds = in_evdev_clean_binds;
509 drv->set_blocking = in_evdev_set_blocking;
510 drv->update_keycode = in_evdev_update_keycode;
511 drv->menu_translate = in_evdev_menu_translate;
512 drv->get_key_code = in_evdev_get_key_code;
513 drv->get_key_name = in_evdev_get_key_name;