2 * (C) Gražvydas "notaz" Ignotas, 2010
4 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
5 * See the COPYING file in the top-level directory.
12 #include <sys/ioctl.h>
16 #include <linux/input.h>
24 /* XXX: these should go to private data */
25 static int osdl_evdev_devs[32];
26 static int osdl_evdev_dev_count;
27 static int osdl_tslib_fd;
28 static struct tsdev *osdl_tslib_dev;
30 static short osdl_evdev_map[KEY_CNT] = {
32 [KEY_0] = SDLK_0, [KEY_F10] = SDLK_F10,
33 [KEY_1] = SDLK_1, [KEY_F1] = SDLK_F1,
34 [KEY_2] = SDLK_2, [KEY_F2] = SDLK_F2,
35 [KEY_3] = SDLK_3, [KEY_F3] = SDLK_F3,
36 [KEY_4] = SDLK_4, [KEY_F4] = SDLK_F4,
37 [KEY_5] = SDLK_5, [KEY_F5] = SDLK_F5,
38 [KEY_6] = SDLK_6, [KEY_F6] = SDLK_F6,
39 [KEY_7] = SDLK_7, [KEY_F7] = SDLK_F7,
40 [KEY_8] = SDLK_8, [KEY_F8] = SDLK_F8,
41 [KEY_9] = SDLK_9, [KEY_F9] = SDLK_F9,
42 [KEY_A] = SDLK_a, [KEY_APOSTROPHE]= SDLK_QUOTE, /* ' */
43 [KEY_B] = SDLK_b, [KEY_F14] = 124, /* | */
44 [KEY_C] = SDLK_c, [KEY_BACKSLASH] = SDLK_BACKSLASH, /* \ */
45 [KEY_D] = SDLK_d, [KEY_KPMINUS] = SDLK_MINUS,
46 [KEY_E] = SDLK_e, [KEY_LEFTBRACE] = SDLK_LEFTPAREN,
47 [KEY_F] = SDLK_f, [KEY_KPPLUS] = SDLK_PLUS,
48 [KEY_G] = SDLK_g, [KEY_EQUAL] = SDLK_EQUALS,
49 [KEY_H] = SDLK_h, [KEY_GRAVE] = SDLK_BACKQUOTE, /* ` */
50 [KEY_I] = SDLK_i, /* [KEY_BRIGHTNESSUP] */
51 [KEY_J] = SDLK_j, /* [KEY_F13] */ /* ’ */
52 [KEY_K] = SDLK_k, /* [KEY_F18] */ /* £ (pound) */
53 [KEY_L] = SDLK_l, /* [KEY_YEN] */
54 [KEY_M] = SDLK_m, [KEY_F23] = SDLK_EURO,
55 [KEY_N] = SDLK_n, [KEY_F22] = SDLK_DOLLAR, /* $ */
56 [KEY_O] = SDLK_o, [KEY_F11] = SDLK_F11,
57 [KEY_P] = SDLK_p, [KEY_F12] = SDLK_F12,
58 [KEY_Q] = SDLK_q, [KEY_ESC] = SDLK_ESCAPE,
59 [KEY_R] = SDLK_r, [KEY_RIGHTBRACE]= SDLK_RIGHTPAREN,
60 [KEY_S] = SDLK_s, [KEY_F19] = SDLK_QUOTEDBL, /* " */
61 [KEY_T] = SDLK_t, [KEY_F17] = SDLK_EXCLAIM, /* ! */
62 [KEY_U] = SDLK_u, /* [KEY_BRIGHTNESSDOWN] */
63 [KEY_V] = SDLK_v, [KEY_F16] = SDLK_HASH, /* # (pound/hash) */
64 [KEY_W] = SDLK_w, [KEY_F20] = SDLK_AT, /* @ */
65 [KEY_X] = SDLK_x, [KEY_QUESTION] = SDLK_QUESTION, /* ? */
66 [KEY_Y] = SDLK_y, [KEY_F15] = SDLK_UNDERSCORE,/* _ */
67 [KEY_Z] = SDLK_z, [KEY_SLASH] = SDLK_SLASH, /* / */
68 [KEY_SPACE] = SDLK_SPACE, [KEY_TAB] = SDLK_TAB,
69 [KEY_BACKSPACE] = SDLK_BACKSPACE, [KEY_INSERT] = SDLK_INSERT,
71 [KEY_DOT] = SDLK_PERIOD, [KEY_F21] = SDLK_COLON, /* : */
72 [KEY_ENTER] = SDLK_RETURN,
73 [KEY_LEFTSHIFT] = SDLK_LSHIFT, [KEY_CAPSLOCK] = SDLK_CAPSLOCK,
74 [KEY_COMMA] = SDLK_COMMA, [KEY_SEMICOLON] = SDLK_SEMICOLON, /* ; */
77 [KEY_DOWN] = SDLK_DOWN,
78 [KEY_LEFT] = SDLK_LEFT,
79 [KEY_RIGHT] = SDLK_RIGHT,
80 [KEY_HOME] = SDLK_HOME,
82 [KEY_PAGEUP] = SDLK_PAGEUP,
83 [KEY_PAGEDOWN] = SDLK_PAGEDOWN,
84 [KEY_LEFTALT] = SDLK_LALT,
85 [KEY_LEFTCTRL] = SDLK_LCTRL,
86 [KEY_MENU] = 147, /* match default SDL here */
87 [KEY_RIGHTSHIFT]= SDLK_RSHIFT,
88 [KEY_RIGHTCTRL] = SDLK_RCTRL,
91 static const char * const osdl_evdev_keys[KEY_CNT] = {
92 [KEY_RESERVED] = "Reserved", [KEY_ESC] = "Esc",
93 [KEY_1] = "1", [KEY_2] = "2",
94 [KEY_3] = "3", [KEY_4] = "4",
95 [KEY_5] = "5", [KEY_6] = "6",
96 [KEY_7] = "7", [KEY_8] = "8",
97 [KEY_9] = "9", [KEY_0] = "0",
98 [KEY_MINUS] = "Minus", [KEY_EQUAL] = "Equal",
99 [KEY_BACKSPACE] = "Backspace", [KEY_TAB] = "Tab",
100 [KEY_Q] = "Q", [KEY_W] = "W",
101 [KEY_E] = "E", [KEY_R] = "R",
102 [KEY_T] = "T", [KEY_Y] = "Y",
103 [KEY_U] = "U", [KEY_I] = "I",
104 [KEY_O] = "O", [KEY_P] = "P",
105 [KEY_LEFTBRACE] = "LeftBrace", [KEY_RIGHTBRACE] = "RightBrace",
106 [KEY_ENTER] = "Enter", [KEY_LEFTCTRL] = "LeftControl",
107 [KEY_A] = "A", [KEY_S] = "S",
108 [KEY_D] = "D", [KEY_F] = "F",
109 [KEY_G] = "G", [KEY_H] = "H",
110 [KEY_J] = "J", [KEY_K] = "K",
111 [KEY_L] = "L", [KEY_SEMICOLON] = "Semicolon",
112 [KEY_APOSTROPHE] = "Apostrophe", [KEY_GRAVE] = "Grave",
113 [KEY_LEFTSHIFT] = "LeftShift", [KEY_BACKSLASH] = "BackSlash",
114 [KEY_Z] = "Z", [KEY_X] = "X",
115 [KEY_C] = "C", [KEY_V] = "V",
116 [KEY_B] = "B", [KEY_N] = "N",
117 [KEY_M] = "M", [KEY_COMMA] = "Comma",
118 [KEY_DOT] = "Dot", [KEY_SLASH] = "Slash",
119 [KEY_RIGHTSHIFT] = "RightShift", [KEY_KPASTERISK] = "KPAsterisk",
120 [KEY_LEFTALT] = "LeftAlt", [KEY_SPACE] = "Space",
121 [KEY_CAPSLOCK] = "CapsLock", [KEY_F1] = "F1",
122 [KEY_F2] = "F2", [KEY_F3] = "F3",
123 [KEY_F4] = "F4", [KEY_F5] = "F5",
124 [KEY_F6] = "F6", [KEY_F7] = "F7",
125 [KEY_F8] = "F8", [KEY_F9] = "F9",
126 [KEY_F10] = "F10", [KEY_NUMLOCK] = "NumLock",
127 [KEY_SCROLLLOCK] = "ScrollLock", [KEY_KP7] = "KP7",
128 [KEY_KP8] = "KP8", [KEY_KP9] = "KP9",
129 [KEY_KPMINUS] = "KPMinus", [KEY_KP4] = "KP4",
130 [KEY_KP5] = "KP5", [KEY_KP6] = "KP6",
131 [KEY_KPPLUS] = "KPPlus", [KEY_KP1] = "KP1",
132 [KEY_KP2] = "KP2", [KEY_KP3] = "KP3",
133 [KEY_KP0] = "KP0", [KEY_KPDOT] = "KPDot",
134 [KEY_ZENKAKUHANKAKU] = "Zenkaku/Hankaku", [KEY_102ND] = "102nd",
135 [KEY_F11] = "F11", [KEY_F12] = "F12",
136 [KEY_KPJPCOMMA] = "KPJpComma", [KEY_KPENTER] = "KPEnter",
137 [KEY_RIGHTCTRL] = "RightCtrl", [KEY_KPSLASH] = "KPSlash",
138 [KEY_SYSRQ] = "SysRq", [KEY_RIGHTALT] = "RightAlt",
139 [KEY_LINEFEED] = "LineFeed", [KEY_HOME] = "Home",
140 [KEY_UP] = "Up", [KEY_PAGEUP] = "PageUp",
141 [KEY_LEFT] = "Left", [KEY_RIGHT] = "Right",
142 [KEY_END] = "End", [KEY_DOWN] = "Down",
143 [KEY_PAGEDOWN] = "PageDown", [KEY_INSERT] = "Insert",
144 [KEY_DELETE] = "Delete", [KEY_MACRO] = "Macro",
145 [KEY_HELP] = "Help", [KEY_MENU] = "Menu",
146 [KEY_COFFEE] = "Coffee", [KEY_DIRECTION] = "Direction",
147 [BTN_0] = "Btn0", [BTN_1] = "Btn1",
148 [BTN_2] = "Btn2", [BTN_3] = "Btn3",
149 [BTN_4] = "Btn4", [BTN_5] = "Btn5",
150 [BTN_6] = "Btn6", [BTN_7] = "Btn7",
151 [BTN_8] = "Btn8", [BTN_9] = "Btn9",
152 [BTN_LEFT] = "LeftBtn", [BTN_RIGHT] = "RightBtn",
153 [BTN_MIDDLE] = "MiddleBtn", [BTN_SIDE] = "SideBtn",
154 [BTN_EXTRA] = "ExtraBtn", [BTN_FORWARD] = "ForwardBtn",
155 [BTN_BACK] = "BackBtn", [BTN_TASK] = "TaskBtn",
156 [BTN_TRIGGER] = "Trigger", [BTN_THUMB] = "ThumbBtn",
157 [BTN_THUMB2] = "ThumbBtn2", [BTN_TOP] = "TopBtn",
158 [BTN_TOP2] = "TopBtn2", [BTN_PINKIE] = "PinkieBtn",
159 [BTN_BASE] = "BaseBtn", [BTN_BASE2] = "BaseBtn2",
160 [BTN_BASE3] = "BaseBtn3", [BTN_BASE4] = "BaseBtn4",
161 [BTN_BASE5] = "BaseBtn5", [BTN_BASE6] = "BaseBtn6",
162 [BTN_DEAD] = "BtnDead", [BTN_A] = "BtnA",
163 [BTN_B] = "BtnB", [BTN_C] = "BtnC",
164 [BTN_X] = "BtnX", [BTN_Y] = "BtnY",
165 [BTN_Z] = "BtnZ", [BTN_TL] = "BtnTL",
166 [BTN_TR] = "BtnTR", [BTN_TL2] = "BtnTL2",
167 [BTN_TR2] = "BtnTR2", [BTN_SELECT] = "BtnSelect",
168 [BTN_START] = "BtnStart", [BTN_MODE] = "BtnMode",
169 [BTN_THUMBL] = "BtnThumbL", [BTN_THUMBR] = "BtnThumbR",
170 [BTN_TOUCH] = "Touch", [BTN_STYLUS] = "Stylus",
171 [BTN_STYLUS2] = "Stylus2", [BTN_TOOL_DOUBLETAP] = "Tool Doubletap",
172 [BTN_TOOL_TRIPLETAP] = "Tool Tripletap", [BTN_GEAR_DOWN] = "WheelBtn",
173 [BTN_GEAR_UP] = "Gear up", [KEY_OK] = "Ok",
176 #define DNKEY(x) [SDLK_##x] = #x
177 static const char * const sdl_keynames[SDLK_LAST] = {
401 void omapsdl_input_bind(const char *kname, const char *sdlname)
405 if (kname == NULL || strncasecmp(kname, "ev_", 3) != 0)
408 for (i = 0; i < ARRAY_SIZE(osdl_evdev_keys); i++) {
409 if (osdl_evdev_keys[i] == NULL)
411 if (strcasecmp(osdl_evdev_keys[i], kname + 3) == 0)
415 if (i >= ARRAY_SIZE(osdl_evdev_keys))
419 if (sdlname == NULL || strncasecmp(sdlname, "SDLK_", 5) != 0)
422 for (i = 0; i < ARRAY_SIZE(sdl_keynames); i++) {
423 if (sdl_keynames[i] == NULL)
425 if (strcasecmp(sdl_keynames[i], sdlname + 5) == 0)
429 if (i >= ARRAY_SIZE(sdl_keynames))
432 osdl_evdev_map[kc] = i;
436 err("can't resolve evdev key '%s'", kname);
440 err("can't resolve SDL key '%s'", sdlname);
443 #define KEYBITS_BIT(keybits, x) (keybits[(x)/sizeof(keybits[0])/8] & \
444 (1 << ((x) & (sizeof(keybits[0])*8-1))))
446 void omapsdl_input_init(void)
448 long keybits[KEY_CNT / sizeof(long) / 8];
449 ino_t touchscreen_ino = (ino_t)-1;
450 struct stat stat_buf;
454 /* start with touchscreen so that we can skip it later */
455 osdl_tslib_dev = ts_open(SDL_getenv("TSLIB_TSDEVICE"), 1);
456 if (ts_config(osdl_tslib_dev) < 0) {
457 ts_close(osdl_tslib_dev);
458 osdl_tslib_dev = NULL;
460 if (osdl_tslib_dev != NULL) {
461 osdl_tslib_fd = ts_fd(osdl_tslib_dev);
462 osdl_evdev_devs[osdl_evdev_dev_count++] = osdl_tslib_fd;
463 if (fstat(osdl_tslib_fd, &stat_buf) == -1)
464 err_perror("fstat ts");
466 touchscreen_ino = stat_buf.st_ino;
467 log("opened tslib touchscreen");
471 /* the kernel might support and return less keys then we know about,
472 * so make sure the buffer is clear. */
473 memset(keybits, 0, sizeof(keybits));
477 int support = 0, count = 0;
481 snprintf(name, sizeof(name), "/dev/input/event%d", i);
482 fd = open(name, O_RDONLY|O_NONBLOCK);
485 continue; /* maybe we can access next one */
489 /* touchscreen check */
490 if (touchscreen_ino != (dev_t)-1) {
491 if (fstat(fd, &stat_buf) == -1)
493 else if (touchscreen_ino == stat_buf.st_ino) {
494 log("skip %s as ts", name);
499 /* check supported events */
500 ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support);
502 err_perror("in_evdev: ioctl failed on %s", name);
506 if (!(support & (1 << EV_KEY)))
509 ret = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
511 err_perror("in_evdev: ioctl failed on %s", name);
515 /* check for interesting keys */
516 for (u = 0; u < KEY_CNT; u++) {
517 if (KEYBITS_BIT(keybits, u)) {
518 if (u != KEY_POWER && u != KEY_SLEEP && u != BTN_TOUCH)
526 osdl_evdev_devs[osdl_evdev_dev_count++] = fd;
527 ioctl(fd, EVIOCGNAME(sizeof(name)), name);
528 log("in_evdev: found \"%s\" with %d events (type %08x)",
529 name, count, support);
536 log("found %d evdev device(s).", osdl_evdev_dev_count);
539 void omapsdl_input_finish(void)
544 if (osdl_tslib_dev != NULL)
545 ts_close(osdl_tslib_dev);
547 osdl_tslib_dev = NULL;
549 for (i = 0; i < osdl_evdev_dev_count; i++) {
550 if (osdl_evdev_devs[i] != osdl_tslib_fd)
551 close(osdl_evdev_devs[i]);
553 osdl_evdev_dev_count = 0;
557 int omapsdl_input_get_events(int timeout_ms,
558 int (*key_cb)(void *cb_arg, int sdl_kc, int is_pressed),
559 int (*ts_cb)(void *cb_arg, int x, int y, unsigned int pressure),
562 struct timeval tv, *timeout = NULL;
563 struct input_event ev;
567 if (timeout_ms >= 0) {
568 tv.tv_sec = timeout_ms / 1000;
569 tv.tv_usec = (timeout_ms % 1000) * 1000;
574 for (i = 0; i < osdl_evdev_dev_count; i++) {
575 if (osdl_evdev_devs[i] > fdmax)
576 fdmax = osdl_evdev_devs[i];
577 FD_SET(osdl_evdev_devs[i], &fdset);
584 ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
587 err_perror("in_evdev: select failed");
591 return -1; /* timeout */
593 for (i = 0; i < osdl_evdev_dev_count; i++) {
594 if (!FD_ISSET(osdl_evdev_devs[i], &fdset))
597 fd = osdl_evdev_devs[i];
599 if (fd == osdl_tslib_fd && ts_cb != NULL) {
601 struct ts_sample tss;
602 ret = ts_read(osdl_tslib_dev, &tss, 1);
605 ret = ts_cb(cb_arg, tss.x, tss.y, tss.pressure);
611 /* else read below will consume the event, even if it's from ts */
615 ret = read(fd, &ev, sizeof(ev));
616 if (ret < (int)sizeof(ev)) {
617 if (errno != EAGAIN) {
618 err_perror("in_evdev: read failed");
624 if (ev.type != EV_KEY)
625 continue; /* not key event */
626 if ((unsigned int)ev.value > 1)
627 continue; /* not key up/down */
628 if ((unsigned int)ev.code >= ARRAY_SIZE(osdl_evdev_map))
629 continue; /* keycode from future */
630 sdl_kc = osdl_evdev_map[ev.code];
632 continue; /* not mapped */
633 ret = key_cb(cb_arg, sdl_kc, ev.value);
644 static unsigned char g_keystate[SDLK_LAST];
651 static int do_key_cb(void *cb_arg, int sdl_kc, int is_pressed)
653 struct key_event *ev = cb_arg;
655 ev->is_pressed = is_pressed;
660 static int do_event(SDL_Event *event, int timeout)
665 ret = omapsdl_input_get_events(timeout, do_key_cb, NULL, &ev);
669 g_keystate[ev.sdl_kc] = ev.is_pressed;
672 return 1; // FIXME: ..but the event is lost
674 memset(event, 0, sizeof(event->key));
675 event->type = ev.is_pressed ? SDL_KEYDOWN : SDL_KEYUP;
676 // event->key.which =
677 event->key.state = ev.is_pressed ? SDL_PRESSED : SDL_RELEASED;
678 event->key.keysym.sym = ev.sdl_kc;
679 // event->key.keysym.mod
685 SDL_WaitEvent(SDL_Event *event)
689 return do_event(event, -1);
693 SDL_PollEvent(SDL_Event *event)
697 return do_event(event, 0);
700 DECLSPEC Uint8 * SDLCALL
701 SDL_GetKeyState(int *numkeys)
703 trace("%p", numkeys);
706 *numkeys = ARRAY_SIZE(g_keystate);
710 DECLSPEC char * SDLCALL
711 SDL_GetKeyName(SDLKey key)
715 if ((unsigned int)key < ARRAY_SIZE(sdl_keynames))
716 return (char *)sdl_keynames[key];
722 SDL_EnableUNICODE(int enable)
733 SDL_EnableKeyRepeat(int delay, int interval)
735 trace("%d, %d", delay, interval);
742 DECLSPEC void SDLCALL
743 SDL_GetKeyRepeat(int *delay, int *interval)
745 trace("%p, %p", delay, interval);
753 DECLSPEC SDLMod SDLCALL
754 SDL_GetModState(void)
756 static const short syms[] = { SDLK_LSHIFT, SDLK_RSHIFT, SDLK_LCTRL, SDLK_RCTRL, SDLK_LALT,
757 SDLK_RALT, SDLK_LMETA, SDLK_RMETA, SDLK_NUMLOCK, SDLK_CAPSLOCK, SDLK_MODE };
758 static const short mods[] = { KMOD_LSHIFT, KMOD_RSHIFT, KMOD_LCTRL, KMOD_RCTRL, KMOD_LALT,
759 KMOD_RALT, KMOD_LMETA, KMOD_RMETA, KMOD_NUM, KMOD_CAPS, KMOD_MODE };
760 SDLMod ret = KMOD_NONE;
765 for (i = 0; i < ARRAY_SIZE(syms); i++)
766 if (g_keystate[syms[i]])
774 SDL_NumJoysticks(void)
780 DECLSPEC SDL_Joystick * SDLCALL
781 SDL_JoystickOpen(int device_index)
783 trace("%d", device_index);