2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2010
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
14 #include <sys/types.h>
17 #include <sys/ioctl.h>
19 #include <linux/input.h>
22 #include "../common/input.h"
25 #define MAX_ABS_DEVS 8
30 int abs_min_x; /* abs->digital mapping */
39 unsigned int abs_count;
40 /* 16.16 multiplier to IN_ABS_RANGE */
41 unsigned int abs_mult[MAX_ABS_DEVS];
42 unsigned int abs_to_digital:1;
46 #define KEY_CNT (KEY_MAX + 1)
49 #define KEYBITS_BIT(x) (keybits[(x)/sizeof(keybits[0])/8] & \
50 (1 << ((x) & (sizeof(keybits[0])*8-1))))
52 #define KEYBITS_BIT_SET(x) (keybits[(x)/sizeof(keybits[0])/8] |= \
53 (1 << ((x) & (sizeof(keybits[0])*8-1))))
55 #define KEYBITS_BIT_CLEAR(x) (keybits[(x)/sizeof(keybits[0])/8] &= \
56 ~(1 << ((x) & (sizeof(keybits[0])*8-1))))
58 int in_evdev_allow_abs_only;
60 #define IN_EVDEV_PREFIX "evdev:"
62 static const char * const in_evdev_keys[KEY_CNT] = {
63 [0 ... KEY_MAX] = NULL,
64 [KEY_RESERVED] = "Reserved", [KEY_ESC] = "Esc",
65 [KEY_1] = "1", [KEY_2] = "2",
66 [KEY_3] = "3", [KEY_4] = "4",
67 [KEY_5] = "5", [KEY_6] = "6",
68 [KEY_7] = "7", [KEY_8] = "8",
69 [KEY_9] = "9", [KEY_0] = "0",
70 [KEY_MINUS] = "Minus", [KEY_EQUAL] = "Equal",
71 [KEY_BACKSPACE] = "Backspace", [KEY_TAB] = "Tab",
72 [KEY_Q] = "Q", [KEY_W] = "W",
73 [KEY_E] = "E", [KEY_R] = "R",
74 [KEY_T] = "T", [KEY_Y] = "Y",
75 [KEY_U] = "U", [KEY_I] = "I",
76 [KEY_O] = "O", [KEY_P] = "P",
77 [KEY_LEFTBRACE] = "LeftBrace", [KEY_RIGHTBRACE] = "RightBrace",
78 [KEY_ENTER] = "Enter", [KEY_LEFTCTRL] = "LeftControl",
79 [KEY_A] = "A", [KEY_S] = "S",
80 [KEY_D] = "D", [KEY_F] = "F",
81 [KEY_G] = "G", [KEY_H] = "H",
82 [KEY_J] = "J", [KEY_K] = "K",
83 [KEY_L] = "L", [KEY_SEMICOLON] = "Semicolon",
84 [KEY_APOSTROPHE] = "Apostrophe", [KEY_GRAVE] = "Grave",
85 [KEY_LEFTSHIFT] = "LeftShift", [KEY_BACKSLASH] = "BackSlash",
86 [KEY_Z] = "Z", [KEY_X] = "X",
87 [KEY_C] = "C", [KEY_V] = "V",
88 [KEY_B] = "B", [KEY_N] = "N",
89 [KEY_M] = "M", [KEY_COMMA] = "Comma",
90 [KEY_DOT] = "Dot", [KEY_SLASH] = "Slash",
91 [KEY_RIGHTSHIFT] = "RightShift", [KEY_KPASTERISK] = "KPAsterisk",
92 [KEY_LEFTALT] = "LeftAlt", [KEY_SPACE] = "Space",
93 [KEY_CAPSLOCK] = "CapsLock", [KEY_F1] = "F1",
94 [KEY_F2] = "F2", [KEY_F3] = "F3",
95 [KEY_F4] = "F4", [KEY_F5] = "F5",
96 [KEY_F6] = "F6", [KEY_F7] = "F7",
97 [KEY_F8] = "F8", [KEY_F9] = "F9",
98 [KEY_F10] = "F10", [KEY_NUMLOCK] = "NumLock",
99 [KEY_SCROLLLOCK] = "ScrollLock", [KEY_KP7] = "KP7",
100 [KEY_KP8] = "KP8", [KEY_KP9] = "KP9",
101 [KEY_KPMINUS] = "KPMinus", [KEY_KP4] = "KP4",
102 [KEY_KP5] = "KP5", [KEY_KP6] = "KP6",
103 [KEY_KPPLUS] = "KPPlus", [KEY_KP1] = "KP1",
104 [KEY_KP2] = "KP2", [KEY_KP3] = "KP3",
105 [KEY_KP0] = "KP0", [KEY_KPDOT] = "KPDot",
106 [KEY_ZENKAKUHANKAKU] = "Zenkaku/Hankaku", [KEY_102ND] = "102nd",
107 [KEY_F11] = "F11", [KEY_F12] = "F12",
108 [KEY_KPJPCOMMA] = "KPJpComma", [KEY_KPENTER] = "KPEnter",
109 [KEY_RIGHTCTRL] = "RightCtrl", [KEY_KPSLASH] = "KPSlash",
110 [KEY_SYSRQ] = "SysRq", [KEY_RIGHTALT] = "RightAlt",
111 [KEY_LINEFEED] = "LineFeed", [KEY_HOME] = "Home",
112 [KEY_UP] = "Up", [KEY_PAGEUP] = "PageUp",
113 [KEY_LEFT] = "Left", [KEY_RIGHT] = "Right",
114 [KEY_END] = "End", [KEY_DOWN] = "Down",
115 [KEY_PAGEDOWN] = "PageDown", [KEY_INSERT] = "Insert",
116 [KEY_DELETE] = "Delete", [KEY_MACRO] = "Macro",
117 [KEY_HELP] = "Help", [KEY_MENU] = "Menu",
118 [KEY_COFFEE] = "Coffee", [KEY_DIRECTION] = "Direction",
119 [BTN_0] = "Btn0", [BTN_1] = "Btn1",
120 [BTN_2] = "Btn2", [BTN_3] = "Btn3",
121 [BTN_4] = "Btn4", [BTN_5] = "Btn5",
122 [BTN_6] = "Btn6", [BTN_7] = "Btn7",
123 [BTN_8] = "Btn8", [BTN_9] = "Btn9",
124 [BTN_LEFT] = "LeftBtn", [BTN_RIGHT] = "RightBtn",
125 [BTN_MIDDLE] = "MiddleBtn", [BTN_SIDE] = "SideBtn",
126 [BTN_EXTRA] = "ExtraBtn", [BTN_FORWARD] = "ForwardBtn",
127 [BTN_BACK] = "BackBtn", [BTN_TASK] = "TaskBtn",
128 [BTN_TRIGGER] = "Trigger", [BTN_THUMB] = "ThumbBtn",
129 [BTN_THUMB2] = "ThumbBtn2", [BTN_TOP] = "TopBtn",
130 [BTN_TOP2] = "TopBtn2", [BTN_PINKIE] = "PinkieBtn",
131 [BTN_BASE] = "BaseBtn", [BTN_BASE2] = "BaseBtn2",
132 [BTN_BASE3] = "BaseBtn3", [BTN_BASE4] = "BaseBtn4",
133 [BTN_BASE5] = "BaseBtn5", [BTN_BASE6] = "BaseBtn6",
134 [BTN_DEAD] = "BtnDead", [BTN_A] = "BtnA",
135 [BTN_B] = "BtnB", [BTN_C] = "BtnC",
136 [BTN_X] = "BtnX", [BTN_Y] = "BtnY",
137 [BTN_Z] = "BtnZ", [BTN_TL] = "BtnTL",
138 [BTN_TR] = "BtnTR", [BTN_TL2] = "BtnTL2",
139 [BTN_TR2] = "BtnTR2", [BTN_SELECT] = "BtnSelect",
140 [BTN_START] = "BtnStart", [BTN_MODE] = "BtnMode",
141 [BTN_THUMBL] = "BtnThumbL", [BTN_THUMBR] = "BtnThumbR",
142 [BTN_TOUCH] = "Touch", [BTN_STYLUS] = "Stylus",
143 [BTN_STYLUS2] = "Stylus2", [BTN_TOOL_DOUBLETAP] = "Tool Doubletap",
144 [BTN_TOOL_TRIPLETAP] = "Tool Tripletap", [BTN_GEAR_DOWN] = "WheelBtn",
145 [BTN_GEAR_UP] = "Gear up", [KEY_OK] = "Ok",
149 static void in_evdev_probe(void)
151 long keybits[KEY_CNT / sizeof(long) / 8];
152 long absbits[(ABS_MAX+1) / sizeof(long) / 8];
155 // the kernel might support and return less keys then we know about,
156 // so make sure the buffers are clear.
157 memset(keybits, 0, sizeof(keybits));
158 memset(absbits, 0, sizeof(absbits));
162 int support = 0, count = 0;
163 int u, ret, fd, kc_first = KEY_MAX, kc_last = 0, have_abs = 0;
167 snprintf(name, sizeof(name), "/dev/input/event%d", i);
168 fd = open(name, O_RDONLY|O_NONBLOCK);
171 continue; /* maybe we can access next one */
175 /* check supported events */
176 ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support);
178 printf("in_evdev: ioctl failed on %s\n", name);
182 if (support & (1 << EV_KEY)) {
183 ret = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
185 printf("in_evdev: ioctl failed on %s\n", name);
189 /* check for interesting keys */
190 for (u = 0; u < KEY_CNT; u++) {
191 if (KEYBITS_BIT(u)) {
196 if (u != KEY_POWER && u != KEY_SLEEP && u != BTN_TOUCH)
198 if (u == BTN_TOUCH) /* we can't deal with ts currently */
204 dev = calloc(1, sizeof(*dev));
208 ret = ioctl(fd, EVIOCGKEY(sizeof(keybits)), keybits);
210 printf("Warning: EVIOCGKEY not supported, will have to track state\n");
211 dev->kbits = calloc(KEY_CNT, sizeof(int));
212 if (dev->kbits == NULL) {
218 /* check for abs too */
219 if (support & (1 << EV_ABS)) {
220 struct input_absinfo ainfo;
222 ret = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits);
225 if (absbits[0] & (1 << ABS_X)) {
226 ret = ioctl(fd, EVIOCGABS(ABS_X), &ainfo);
229 dist = ainfo.maximum - ainfo.minimum;
230 dev->abs_lzone = dist / 4;
231 dev->abs_min_x = ainfo.minimum;
232 dev->abs_max_x = ainfo.maximum;
234 if (absbits[0] & (1 << ABS_Y)) {
235 ret = ioctl(fd, EVIOCGABS(ABS_Y), &ainfo);
238 dist = ainfo.maximum - ainfo.minimum;
239 dev->abs_min_y = ainfo.minimum;
240 dev->abs_max_y = ainfo.maximum;
242 for (u = 0; u < MAX_ABS_DEVS; u++) {
243 ret = ioctl(fd, EVIOCGABS(u), &ainfo);
246 dist = ainfo.maximum - ainfo.minimum;
248 dev->abs_mult[u] = IN_ABS_RANGE * 2 * 65536 / dist;
255 if (count == 0 && !have_abs) {
261 dev->kc_first = kc_first;
262 dev->kc_last = kc_last;
263 if (count > 0 || in_evdev_allow_abs_only)
264 dev->abs_to_digital = 1;
265 strcpy(name, IN_EVDEV_PREFIX);
266 ioctl(fd, EVIOCGNAME(sizeof(name)-6), name+6);
267 printf("in_evdev: found \"%s\" with %d events (type %08x)\n",
268 name+6, count, support);
269 in_register(name, fd, dev, KEY_CNT, in_evdev_keys, 0);
277 static void in_evdev_free(void *drv_data)
279 in_evdev_t *dev = drv_data;
286 static const char * const *
287 in_evdev_get_key_names(int *count)
290 return in_evdev_keys;
293 static void or_binds(const int *binds, int key, int *result)
296 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
297 result[t] |= binds[IN_BIND_OFFS(key, t)];
300 /* ORs result with binds of pressed buttons
301 * XXX: should measure performance hit of this func, might need to optimize */
302 static int in_evdev_update(void *drv_data, const int *binds, int *result)
304 struct input_event ev[16];
305 struct input_absinfo ainfo;
306 int keybits_[KEY_CNT / sizeof(int)];
307 int *keybits = keybits_;
308 in_evdev_t *dev = drv_data;
309 int rd, ret, u, lzone;
311 if (dev->kbits == NULL) {
312 ret = ioctl(dev->fd, EVIOCGKEY(sizeof(keybits_)), keybits_);
314 perror("in_evdev: ioctl failed");
319 keybits = dev->kbits;
321 rd = read(dev->fd, ev, sizeof(ev));
322 if (rd < (int)sizeof(ev[0])) {
324 perror("in_evdev: read failed");
327 for (u = 0; u < rd / sizeof(ev[0]); u++) {
328 if (ev[u].type != EV_KEY)
330 else if (ev[u].value == 1)
331 KEYBITS_BIT_SET(ev[u].code);
332 else if (ev[u].value == 0)
333 KEYBITS_BIT_CLEAR(ev[u].code);
338 for (u = dev->kc_first; u <= dev->kc_last; u++) {
340 or_binds(binds, u, result);
343 /* map X and Y absolute to UDLR */
344 lzone = dev->abs_lzone;
345 if (dev->abs_to_digital && lzone != 0) {
346 ret = ioctl(dev->fd, EVIOCGABS(ABS_X), &ainfo);
348 if (ainfo.value < dev->abs_min_x + lzone) or_binds(binds, KEY_LEFT, result);
349 if (ainfo.value > dev->abs_max_x - lzone) or_binds(binds, KEY_RIGHT, result);
351 ret = ioctl(dev->fd, EVIOCGABS(ABS_Y), &ainfo);
353 if (ainfo.value < dev->abs_min_y + lzone) or_binds(binds, KEY_UP, result);
354 if (ainfo.value > dev->abs_max_y - lzone) or_binds(binds, KEY_DOWN, result);
361 static int in_evdev_update_analog(void *drv_data, int axis_id, int *result)
363 struct input_absinfo ainfo;
364 in_evdev_t *dev = drv_data;
367 if ((unsigned int)axis_id >= MAX_ABS_DEVS)
370 ret = ioctl(dev->fd, EVIOCGABS(axis_id), &ainfo);
374 *result = (int)(ainfo.value * dev->abs_mult[axis_id]) >> 16;
378 static int in_evdev_set_blocking(in_evdev_t *dev, int y)
383 flags = (long)fcntl(dev->fd, F_GETFL);
384 if ((int)flags == -1) {
385 perror("in_evdev: F_GETFL fcntl failed");
389 if (flags & O_NONBLOCK) {
390 /* flush the event queue */
391 struct input_event ev;
393 ret = read(dev->fd, &ev, sizeof(ev));
395 while (ret == sizeof(ev));
399 flags &= ~O_NONBLOCK;
402 ret = fcntl(dev->fd, F_SETFL, flags);
404 perror("in_evdev: F_SETFL fcntl failed");
411 static int in_evdev_get_config(void *drv_data, int what, int *val)
413 in_evdev_t *dev = drv_data;
416 case IN_CFG_ABS_AXIS_COUNT:
417 *val = dev->abs_count;
426 static int in_evdev_set_config(void *drv_data, int what, int val)
428 in_evdev_t *dev = drv_data;
432 case IN_CFG_BLOCKING:
433 return in_evdev_set_blocking(dev, val);
434 case IN_CFG_ABS_DEAD_ZONE:
435 if (val < 1 || val > 99 || dev->abs_lzone == 0)
437 /* XXX: based on X axis only, for now.. */
438 tmp = (dev->abs_max_x - dev->abs_min_x) / 2;
439 dev->abs_lzone = tmp - tmp * val / 100;
440 if (dev->abs_lzone < 1)
442 else if (dev->abs_lzone >= tmp)
443 dev->abs_lzone = tmp - 1;
452 static int in_evdev_update_keycode(void *data, int *is_down)
454 int ret_kc = -1, ret_down = 0;
455 in_evdev_t *dev = data;
456 struct input_event ev;
459 /* do single event, the caller sometimes wants
460 * to do select() in blocking mode */
461 rd = read(dev->fd, &ev, sizeof(ev));
462 if (rd < (int) sizeof(ev)) {
463 if (errno != EAGAIN) {
464 perror("in_evdev: error reading");
471 if (ev.type == EV_KEY) {
472 if (ev.value < 0 || ev.value > 1)
478 else if (ev.type == EV_ABS && dev->abs_to_digital)
480 int lzone = dev->abs_lzone, down = 0, *last;
482 // map absolute to up/down/left/right
483 if (lzone != 0 && ev.code == ABS_X) {
484 if (ev.value < dev->abs_min_x + lzone)
486 else if (ev.value > dev->abs_max_x - lzone)
488 last = &dev->abs_lastx;
490 else if (lzone != 0 && ev.code == ABS_Y) {
491 if (ev.value < dev->abs_min_y + lzone)
493 else if (ev.value > dev->abs_max_y - lzone)
495 last = &dev->abs_lasty;
503 if (down == 0 || *last != 0) {
504 /* key up or direction change, return up event for old key */
510 ret_kc = *last = down;
522 static const struct {
528 { KEY_DOWN, PBTN_DOWN },
529 { KEY_LEFT, PBTN_LEFT },
530 { KEY_RIGHT, PBTN_RIGHT },
531 /* XXX: maybe better set this from it's plat code somehow */
533 { KEY_END, PBTN_MOK },
534 { KEY_PAGEDOWN, PBTN_MBACK },
535 { KEY_HOME, PBTN_MA2 },
536 { KEY_PAGEUP, PBTN_MA3 },
537 { KEY_LEFTCTRL, PBTN_MENU },
538 { KEY_RIGHTSHIFT, PBTN_L },
539 { KEY_RIGHTCTRL, PBTN_R },
541 { BTN_THUMB2, PBTN_MOK },
542 { BTN_THUMB, PBTN_MBACK },
543 { BTN_TRIGGER, PBTN_MA2 },
544 { BTN_TOP, PBTN_MA3 },
545 { BTN_BASE, PBTN_MENU },
546 { BTN_TOP2, PBTN_L },
547 { BTN_PINKIE, PBTN_R },
548 /* "normal" keyboards */
549 { KEY_ENTER, PBTN_MOK },
550 { KEY_ESC, PBTN_MBACK },
553 { KEY_BACKSLASH, PBTN_MENU },
554 { KEY_LEFTBRACE, PBTN_L },
555 { KEY_RIGHTBRACE, PBTN_R },
558 #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
560 static int in_evdev_menu_translate(void *drv_data, int keycode)
562 in_evdev_t *dev = drv_data;
569 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
570 if (key_pbtn_map[i].pbtn == keycode) {
571 int k = key_pbtn_map[i].key;
572 /* should really check EVIOCGBIT, but this is enough for now */
573 if (dev->kc_first <= k && k <= dev->kc_last)
579 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
580 if (key_pbtn_map[i].key == keycode)
581 return key_pbtn_map[i].pbtn;
587 static void in_evdev_get_def_binds(int *binds)
592 if (in_evdev_defbinds[i].bit == 0 && in_evdev_defbinds[i].code == 0)
594 binds[IN_BIND_OFFS(in_evdev_defbinds[i].code, in_evdev_defbinds[i].btype)] =
595 1 << in_evdev_defbinds[i].bit;
599 /* remove binds of missing keys, count remaining ones */
600 static int in_evdev_clean_binds(void *drv_data, int *binds, int *def_binds)
602 int keybits[KEY_CNT / sizeof(int)];
603 in_evdev_t *dev = drv_data;
604 int i, t, ret, offs, count = 0;
606 memset(keybits, 0, sizeof(keybits));
607 ret = ioctl(dev->fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
609 perror("in_evdev: ioctl failed");
610 // memset(keybits, 0xff, sizeof(keybits)); /* mark all as good */
613 if (dev->abs_to_digital && dev->abs_lzone != 0) {
614 KEYBITS_BIT_SET(KEY_LEFT);
615 KEYBITS_BIT_SET(KEY_RIGHT);
616 KEYBITS_BIT_SET(KEY_UP);
617 KEYBITS_BIT_SET(KEY_DOWN);
620 for (i = 0; i < KEY_CNT; i++) {
621 for (t = 0; t < IN_BINDTYPE_COUNT; t++) {
622 offs = IN_BIND_OFFS(i, t);
624 binds[offs] = def_binds[offs] = 0;
633 static const in_drv_t in_evdev_drv = {
634 .prefix = IN_EVDEV_PREFIX,
635 .probe = in_evdev_probe,
636 .free = in_evdev_free,
637 .get_key_names = in_evdev_get_key_names,
638 .get_def_binds = in_evdev_get_def_binds,
639 .clean_binds = in_evdev_clean_binds,
640 .get_config = in_evdev_get_config,
641 .set_config = in_evdev_set_config,
642 .update = in_evdev_update,
643 .update_analog = in_evdev_update_analog,
644 .update_keycode = in_evdev_update_keycode,
645 .menu_translate = in_evdev_menu_translate,
648 void in_evdev_init(void)
650 in_register_driver(&in_evdev_drv);