in_evdev: don't stop on bit0
[picodrive.git] / platform / linux / in_evdev.c
CommitLineData
9c5a64a0 1/*
2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2010
3 *
4 * This work is licensed under the terms of any of these licenses
5 * (at your option):
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.
9 */
10
4f96f79c 11#include <stdio.h>
483230c7 12#include <stdlib.h>
4f96f79c 13#include <string.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <fcntl.h>
17#include <sys/ioctl.h>
18#include <unistd.h>
19#include <linux/input.h>
20#include <errno.h>
21
e1f99f2b 22#include "../common/input.h"
3b13ec65 23#include "in_evdev.h"
4f96f79c 24
483230c7 25typedef struct {
26 int fd;
2843d00e 27 int *kbits;
603c28b3 28 int abs_min_x;
29 int abs_max_x;
30 int abs_min_y;
31 int abs_max_y;
483230c7 32 int abs_lzone;
483230c7 33 int abs_lastx;
34 int abs_lasty;
603c28b3 35 int kc_first;
36 int kc_last;
483230c7 37} in_evdev_t;
38
093b8a42 39#ifndef KEY_CNT
40#define KEY_CNT (KEY_MAX + 1)
41#endif
42
819ef025 43#define KEYBITS_BIT(x) (keybits[(x)/sizeof(keybits[0])/8] & \
4f96f79c 44 (1 << ((x) & (sizeof(keybits[0])*8-1))))
45
483230c7 46#define KEYBITS_BIT_SET(x) (keybits[(x)/sizeof(keybits[0])/8] |= \
47 (1 << ((x) & (sizeof(keybits[0])*8-1))))
48
2843d00e 49#define KEYBITS_BIT_CLEAR(x) (keybits[(x)/sizeof(keybits[0])/8] &= \
50 ~(1 << ((x) & (sizeof(keybits[0])*8-1))))
51
819ef025 52static const char * const in_evdev_prefix = "evdev:";
093b8a42 53static const char * const in_evdev_keys[KEY_CNT] = {
3427e573 54 [0 ... KEY_MAX] = NULL,
55 [KEY_RESERVED] = "Reserved", [KEY_ESC] = "Esc",
56 [KEY_1] = "1", [KEY_2] = "2",
57 [KEY_3] = "3", [KEY_4] = "4",
58 [KEY_5] = "5", [KEY_6] = "6",
59 [KEY_7] = "7", [KEY_8] = "8",
60 [KEY_9] = "9", [KEY_0] = "0",
61 [KEY_MINUS] = "Minus", [KEY_EQUAL] = "Equal",
62 [KEY_BACKSPACE] = "Backspace", [KEY_TAB] = "Tab",
63 [KEY_Q] = "Q", [KEY_W] = "W",
64 [KEY_E] = "E", [KEY_R] = "R",
65 [KEY_T] = "T", [KEY_Y] = "Y",
66 [KEY_U] = "U", [KEY_I] = "I",
67 [KEY_O] = "O", [KEY_P] = "P",
68 [KEY_LEFTBRACE] = "LeftBrace", [KEY_RIGHTBRACE] = "RightBrace",
69 [KEY_ENTER] = "Enter", [KEY_LEFTCTRL] = "LeftControl",
70 [KEY_A] = "A", [KEY_S] = "S",
71 [KEY_D] = "D", [KEY_F] = "F",
72 [KEY_G] = "G", [KEY_H] = "H",
73 [KEY_J] = "J", [KEY_K] = "K",
74 [KEY_L] = "L", [KEY_SEMICOLON] = "Semicolon",
75 [KEY_APOSTROPHE] = "Apostrophe", [KEY_GRAVE] = "Grave",
76 [KEY_LEFTSHIFT] = "LeftShift", [KEY_BACKSLASH] = "BackSlash",
77 [KEY_Z] = "Z", [KEY_X] = "X",
78 [KEY_C] = "C", [KEY_V] = "V",
79 [KEY_B] = "B", [KEY_N] = "N",
80 [KEY_M] = "M", [KEY_COMMA] = "Comma",
81 [KEY_DOT] = "Dot", [KEY_SLASH] = "Slash",
82 [KEY_RIGHTSHIFT] = "RightShift", [KEY_KPASTERISK] = "KPAsterisk",
83 [KEY_LEFTALT] = "LeftAlt", [KEY_SPACE] = "Space",
84 [KEY_CAPSLOCK] = "CapsLock", [KEY_F1] = "F1",
85 [KEY_F2] = "F2", [KEY_F3] = "F3",
86 [KEY_F4] = "F4", [KEY_F5] = "F5",
87 [KEY_F6] = "F6", [KEY_F7] = "F7",
88 [KEY_F8] = "F8", [KEY_F9] = "F9",
89 [KEY_F10] = "F10", [KEY_NUMLOCK] = "NumLock",
90 [KEY_SCROLLLOCK] = "ScrollLock", [KEY_KP7] = "KP7",
91 [KEY_KP8] = "KP8", [KEY_KP9] = "KP9",
92 [KEY_KPMINUS] = "KPMinus", [KEY_KP4] = "KP4",
93 [KEY_KP5] = "KP5", [KEY_KP6] = "KP6",
94 [KEY_KPPLUS] = "KPPlus", [KEY_KP1] = "KP1",
95 [KEY_KP2] = "KP2", [KEY_KP3] = "KP3",
96 [KEY_KP0] = "KP0", [KEY_KPDOT] = "KPDot",
97 [KEY_ZENKAKUHANKAKU] = "Zenkaku/Hankaku", [KEY_102ND] = "102nd",
98 [KEY_F11] = "F11", [KEY_F12] = "F12",
99 [KEY_KPJPCOMMA] = "KPJpComma", [KEY_KPENTER] = "KPEnter",
100 [KEY_RIGHTCTRL] = "RightCtrl", [KEY_KPSLASH] = "KPSlash",
101 [KEY_SYSRQ] = "SysRq", [KEY_RIGHTALT] = "RightAlt",
102 [KEY_LINEFEED] = "LineFeed", [KEY_HOME] = "Home",
103 [KEY_UP] = "Up", [KEY_PAGEUP] = "PageUp",
104 [KEY_LEFT] = "Left", [KEY_RIGHT] = "Right",
105 [KEY_END] = "End", [KEY_DOWN] = "Down",
106 [KEY_PAGEDOWN] = "PageDown", [KEY_INSERT] = "Insert",
107 [KEY_DELETE] = "Delete", [KEY_MACRO] = "Macro",
108 [KEY_HELP] = "Help", [KEY_MENU] = "Menu",
109 [KEY_COFFEE] = "Coffee", [KEY_DIRECTION] = "Direction",
110 [BTN_0] = "Btn0", [BTN_1] = "Btn1",
111 [BTN_2] = "Btn2", [BTN_3] = "Btn3",
112 [BTN_4] = "Btn4", [BTN_5] = "Btn5",
113 [BTN_6] = "Btn6", [BTN_7] = "Btn7",
114 [BTN_8] = "Btn8", [BTN_9] = "Btn9",
115 [BTN_LEFT] = "LeftBtn", [BTN_RIGHT] = "RightBtn",
116 [BTN_MIDDLE] = "MiddleBtn", [BTN_SIDE] = "SideBtn",
117 [BTN_EXTRA] = "ExtraBtn", [BTN_FORWARD] = "ForwardBtn",
118 [BTN_BACK] = "BackBtn", [BTN_TASK] = "TaskBtn",
119 [BTN_TRIGGER] = "Trigger", [BTN_THUMB] = "ThumbBtn",
120 [BTN_THUMB2] = "ThumbBtn2", [BTN_TOP] = "TopBtn",
121 [BTN_TOP2] = "TopBtn2", [BTN_PINKIE] = "PinkieBtn",
122 [BTN_BASE] = "BaseBtn", [BTN_BASE2] = "BaseBtn2",
123 [BTN_BASE3] = "BaseBtn3", [BTN_BASE4] = "BaseBtn4",
124 [BTN_BASE5] = "BaseBtn5", [BTN_BASE6] = "BaseBtn6",
125 [BTN_DEAD] = "BtnDead", [BTN_A] = "BtnA",
126 [BTN_B] = "BtnB", [BTN_C] = "BtnC",
127 [BTN_X] = "BtnX", [BTN_Y] = "BtnY",
128 [BTN_Z] = "BtnZ", [BTN_TL] = "BtnTL",
129 [BTN_TR] = "BtnTR", [BTN_TL2] = "BtnTL2",
130 [BTN_TR2] = "BtnTR2", [BTN_SELECT] = "BtnSelect",
131 [BTN_START] = "BtnStart", [BTN_MODE] = "BtnMode",
132 [BTN_THUMBL] = "BtnThumbL", [BTN_THUMBR] = "BtnThumbR",
133 [BTN_TOUCH] = "Touch", [BTN_STYLUS] = "Stylus",
134 [BTN_STYLUS2] = "Stylus2", [BTN_TOOL_DOUBLETAP] = "Tool Doubletap",
135 [BTN_TOOL_TRIPLETAP] = "Tool Tripletap", [BTN_GEAR_DOWN] = "WheelBtn",
136 [BTN_GEAR_UP] = "Gear up", [KEY_OK] = "Ok",
137};
138
139
819ef025 140static void in_evdev_probe(void)
4f96f79c 141{
b60b8745 142 long keybits[KEY_CNT / sizeof(long) / 8];
143 long absbits[(ABS_MAX+1) / sizeof(long) / 8];
4f96f79c 144 int i;
145
b60b8745 146 // the kernel might support and return less keys then we know about,
147 // so make sure the buffers are clear.
148 memset(keybits, 0, sizeof(keybits));
149 memset(absbits, 0, sizeof(absbits));
150
e1f99f2b 151 for (i = 0;; i++)
4f96f79c 152 {
4f96f79c 153 int support = 0, count = 0;
603c28b3 154 int u, ret, fd, kc_first, kc_last;
483230c7 155 in_evdev_t *dev;
4f96f79c 156 char name[64];
157
158 snprintf(name, sizeof(name), "/dev/input/event%d", i);
683bbd55 159 fd = open(name, O_RDONLY|O_NONBLOCK);
093b8a42 160 if (fd == -1) {
161 if (errno == EACCES)
162 continue; /* maybe we can access next one */
4f96f79c 163 break;
093b8a42 164 }
4f96f79c 165
166 /* check supported events */
167 ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support);
168 if (ret == -1) {
e1f99f2b 169 printf("in_evdev: ioctl failed on %s\n", name);
4f96f79c 170 goto skip;
171 }
172
173 if (!(support & (1 << EV_KEY)))
174 goto skip;
175
176 ret = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
177 if (ret == -1) {
e1f99f2b 178 printf("in_evdev: ioctl failed on %s\n", name);
4f96f79c 179 goto skip;
180 }
181
4f96f79c 182 /* check for interesting keys */
603c28b3 183 kc_first = KEY_MAX;
184 kc_last = 0;
093b8a42 185 for (u = 0; u < KEY_CNT; u++) {
603c28b3 186 if (KEYBITS_BIT(u)) {
187 if (u < kc_first)
188 kc_first = u;
189 if (u > kc_last)
190 kc_last = u;
191 if (u != KEY_POWER && u != KEY_SLEEP && u != BTN_TOUCH)
192 count++;
193 }
4f96f79c 194 }
195
196 if (count == 0)
197 goto skip;
198
483230c7 199 dev = calloc(1, sizeof(*dev));
200 if (dev == NULL)
201 goto skip;
202
2843d00e 203 ret = ioctl(fd, EVIOCGKEY(sizeof(keybits)), keybits);
204 if (ret == -1) {
205 printf("Warning: EVIOCGKEY not supported, will have to track state\n");
206 dev->kbits = calloc(KEY_CNT, sizeof(int));
207 if (dev->kbits == NULL) {
208 free(dev);
209 goto skip;
210 }
211 }
212
483230c7 213 /* check for abs too */
214 if (support & (1 << EV_ABS)) {
215 struct input_absinfo ainfo;
216 int dist;
217 ret = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits);
218 if (ret == -1)
219 goto no_abs;
220 if (absbits[0] & (1 << ABS_X)) {
221 ret = ioctl(fd, EVIOCGABS(ABS_X), &ainfo);
222 if (ret == -1)
223 goto no_abs;
224 dist = ainfo.maximum - ainfo.minimum;
603c28b3 225 dev->abs_lzone = dist / 4;
226 dev->abs_min_x = ainfo.minimum;
227 dev->abs_max_x = ainfo.maximum;
483230c7 228 }
229 if (absbits[0] & (1 << ABS_Y)) {
230 ret = ioctl(fd, EVIOCGABS(ABS_Y), &ainfo);
231 if (ret == -1)
232 goto no_abs;
233 dist = ainfo.maximum - ainfo.minimum;
603c28b3 234 dev->abs_min_y = ainfo.minimum;
235 dev->abs_max_y = ainfo.maximum;
483230c7 236 }
237 }
238
239no_abs:
240 dev->fd = fd;
603c28b3 241 dev->kc_first = kc_first;
242 dev->kc_last = kc_last;
819ef025 243 strcpy(name, in_evdev_prefix);
e1f99f2b 244 ioctl(fd, EVIOCGNAME(sizeof(name)-6), name+6);
245 printf("in_evdev: found \"%s\" with %d events (type %08x)\n",
246 name+6, count, support);
603c28b3 247 in_register(name, IN_DRVID_EVDEV, fd, dev, KEY_CNT, in_evdev_keys, 0);
4f96f79c 248 continue;
249
250skip:
251 close(fd);
252 }
4f96f79c 253}
254
819ef025 255static void in_evdev_free(void *drv_data)
e1f99f2b 256{
483230c7 257 in_evdev_t *dev = drv_data;
258 if (dev == NULL)
259 return;
260 close(dev->fd);
261 free(dev);
e1f99f2b 262}
263
819ef025 264static int in_evdev_get_bind_count(void)
4f96f79c 265{
093b8a42 266 return KEY_CNT;
4f96f79c 267}
268
093b8a42 269static void or_binds(const int *binds, int key, int *result)
270{
271 int t;
272 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
273 result[t] |= binds[IN_BIND_OFFS(key, t)];
274}
275
276/* ORs result with binds of pressed buttons
277 * XXX: should measure performance hit of this func, might need to optimize */
278int in_evdev_update(void *drv_data, const int *binds, int *result)
4f96f79c 279{
280 struct input_event ev[16];
483230c7 281 struct input_absinfo ainfo;
2843d00e 282 int keybits_[KEY_CNT / sizeof(int)];
283 int *keybits = keybits_;
483230c7 284 in_evdev_t *dev = drv_data;
603c28b3 285 int rd, ret, u, lzone;
4f96f79c 286
2843d00e 287 if (dev->kbits == NULL) {
370d5805 288 ret = ioctl(dev->fd, EVIOCGKEY(sizeof(keybits_)), keybits_);
2843d00e 289 if (ret == -1) {
290 perror("in_evdev: ioctl failed");
291 return -1;
4f96f79c 292 }
e1f99f2b 293 }
2843d00e 294 else {
295 keybits = dev->kbits;
296 while (1) {
297 rd = read(dev->fd, ev, sizeof(ev));
298 if (rd < (int)sizeof(ev[0])) {
299 if (errno != EAGAIN)
300 perror("in_evdev: read failed");
301 break;
302 }
303 for (u = 0; u < rd / sizeof(ev[0]); u++) {
304 if (ev[u].type != EV_KEY)
305 continue;
306 else if (ev[u].value == 1)
307 KEYBITS_BIT_SET(ev[u].code);
308 else if (ev[u].value == 0)
309 KEYBITS_BIT_CLEAR(ev[u].code);
310 }
311 }
e1f99f2b 312 }
4f96f79c 313
603c28b3 314 for (u = dev->kc_first; u <= dev->kc_last; u++) {
093b8a42 315 if (KEYBITS_BIT(u))
316 or_binds(binds, u, result);
4f96f79c 317 }
318
483230c7 319 /* map X and Y absolute to UDLR */
603c28b3 320 lzone = dev->abs_lzone;
321 if (lzone != 0) {
483230c7 322 ret = ioctl(dev->fd, EVIOCGABS(ABS_X), &ainfo);
323 if (ret != -1) {
603c28b3 324 if (ainfo.value < dev->abs_min_x + lzone) or_binds(binds, KEY_LEFT, result);
325 if (ainfo.value > dev->abs_max_x - lzone) or_binds(binds, KEY_RIGHT, result);
483230c7 326 }
327 }
603c28b3 328 if (lzone != 0) {
483230c7 329 ret = ioctl(dev->fd, EVIOCGABS(ABS_Y), &ainfo);
330 if (ret != -1) {
603c28b3 331 if (ainfo.value < dev->abs_min_y + lzone) or_binds(binds, KEY_UP, result);
332 if (ainfo.value > dev->abs_max_y - lzone) or_binds(binds, KEY_DOWN, result);
483230c7 333 }
334 }
335
093b8a42 336 return 0;
4f96f79c 337}
338
603c28b3 339static int in_evdev_set_blocking(in_evdev_t *dev, int y)
3b13ec65 340{
3b13ec65 341 long flags;
3427e573 342 int ret;
3b13ec65 343
483230c7 344 flags = (long)fcntl(dev->fd, F_GETFL);
3427e573 345 if ((int)flags == -1) {
346 perror("in_evdev: F_GETFL fcntl failed");
603c28b3 347 return -1;
3b13ec65 348 }
d8afe7b8 349
350 if (flags & O_NONBLOCK) {
351 /* flush the event queue */
352 struct input_event ev;
353 do {
354 ret = read(dev->fd, &ev, sizeof(ev));
355 }
356 while (ret == sizeof(ev));
357 }
358
3427e573 359 if (y)
360 flags &= ~O_NONBLOCK;
361 else
362 flags |= O_NONBLOCK;
483230c7 363 ret = fcntl(dev->fd, F_SETFL, flags);
603c28b3 364 if (ret == -1) {
3427e573 365 perror("in_evdev: F_SETFL fcntl failed");
603c28b3 366 return -1;
367 }
368
369 return 0;
370}
371
372static int in_evdev_set_config(void *drv_data, int what, int val)
373{
374 in_evdev_t *dev = drv_data;
375 int tmp;
376
377 switch (what) {
378 case IN_CFG_BLOCKING:
379 return in_evdev_set_blocking(dev, val);
380 case IN_CFG_ABS_DEAD_ZONE:
381 if (val < 1 || val > 99 || dev->abs_lzone == 0)
382 return -1;
383 /* XXX: based on X axis only, for now.. */
384 tmp = (dev->abs_max_x - dev->abs_min_x) / 2;
385 dev->abs_lzone = tmp - tmp * val / 100;
386 if (dev->abs_lzone < 1)
387 dev->abs_lzone = 1;
388 else if (dev->abs_lzone >= tmp)
389 dev->abs_lzone = tmp - 1;
390 break;
391 default:
392 return -1;
393 }
394
395 return 0;
3427e573 396}
397
7201dc73 398static int in_evdev_update_keycode(void *data, int *is_down)
3427e573 399{
603c28b3 400 int ret_kc = -1, ret_down = 0;
7201dc73 401 in_evdev_t *dev = data;
402 struct input_event ev;
403 int rd;
f484a9fe 404
afc00cad 405 /* do single event, the caller sometimes wants
406 * to do select() in blocking mode */
407 rd = read(dev->fd, &ev, sizeof(ev));
408 if (rd < (int) sizeof(ev)) {
409 if (errno != EAGAIN) {
410 perror("in_evdev: error reading");
411 sleep(1);
25915832 412 }
afc00cad 413 goto out;
414 }
603c28b3 415
afc00cad 416 if (ev.type == EV_KEY) {
417 if (ev.value < 0 || ev.value > 1)
603c28b3 418 goto out;
afc00cad 419 ret_kc = ev.code;
420 ret_down = ev.value;
421 goto out;
422 }
423 else if (ev.type == EV_ABS)
424 {
425 int lzone = dev->abs_lzone, down = 0, *last;
426
427 // map absolute to up/down/left/right
428 if (lzone != 0 && ev.code == ABS_X) {
429 if (ev.value < dev->abs_min_x + lzone)
430 down = KEY_LEFT;
431 else if (ev.value > dev->abs_max_x - lzone)
432 down = KEY_RIGHT;
433 last = &dev->abs_lastx;
25915832 434 }
afc00cad 435 else if (lzone != 0 && ev.code == ABS_Y) {
436 if (ev.value < dev->abs_min_y + lzone)
437 down = KEY_UP;
438 else if (ev.value > dev->abs_max_y - lzone)
439 down = KEY_DOWN;
440 last = &dev->abs_lasty;
441 }
442 else
443 goto out;
444
445 if (down == *last)
446 goto out;
447
448 if (down == 0 || *last != 0) {
449 /* key up or direction change, return up event for old key */
450 ret_kc = *last;
451 ret_down = 0;
452 *last = 0;
603c28b3 453 goto out;
3b13ec65 454 }
afc00cad 455 ret_kc = *last = down;
456 ret_down = 1;
457 goto out;
3b13ec65 458 }
7201dc73 459
603c28b3 460out:
461 if (is_down != NULL)
462 *is_down = ret_down;
463
464 return ret_kc;
3427e573 465}
3b13ec65 466
991473ad 467static const struct {
468 short key;
469 short pbtn;
470} key_pbtn_map[] =
471{
472 { KEY_UP, PBTN_UP },
473 { KEY_DOWN, PBTN_DOWN },
474 { KEY_LEFT, PBTN_LEFT },
475 { KEY_RIGHT, PBTN_RIGHT },
603c28b3 476 /* XXX: maybe better set this from it's plat code somehow */
477 /* Pandora */
be672de7 478 { KEY_END, PBTN_MOK },
be672de7 479 { KEY_PAGEDOWN, PBTN_MBACK },
be672de7 480 { KEY_HOME, PBTN_MA2 },
be672de7 481 { KEY_PAGEUP, PBTN_MA3 },
e708403e 482 { KEY_LEFTCTRL, PBTN_MENU },
be672de7 483 { KEY_RIGHTSHIFT, PBTN_L },
be672de7 484 { KEY_RIGHTCTRL, PBTN_R },
603c28b3 485 /* Caanoo */
486 { BTN_THUMB2, PBTN_MOK },
487 { BTN_THUMB, PBTN_MBACK },
488 { BTN_TRIGGER, PBTN_MA2 },
489 { BTN_TOP, PBTN_MA3 },
490 { BTN_BASE, PBTN_MENU },
491 { BTN_TOP2, PBTN_L },
492 { BTN_PINKIE, PBTN_R },
493 /* "normal" keyboards */
494 { KEY_ENTER, PBTN_MOK },
495 { KEY_ESC, PBTN_MBACK },
496 { KEY_A, PBTN_MA2 },
497 { KEY_S, PBTN_MA3 },
498 { KEY_BACKSLASH, PBTN_MENU },
499 { KEY_LEFTBRACE, PBTN_L },
991473ad 500 { KEY_RIGHTBRACE, PBTN_R },
501};
502
503#define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
504
603c28b3 505static int in_evdev_menu_translate(void *drv_data, int keycode)
3427e573 506{
603c28b3 507 in_evdev_t *dev = drv_data;
991473ad 508 int i;
603c28b3 509
991473ad 510 if (keycode < 0)
511 {
512 /* menu -> kc */
513 keycode = -keycode;
514 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
603c28b3 515 if (key_pbtn_map[i].pbtn == keycode) {
516 int k = key_pbtn_map[i].key;
517 /* should really check EVIOCGBIT, but this is enough for now */
518 if (dev->kc_first <= k && k <= dev->kc_last)
519 return k;
520 }
3b13ec65 521 }
991473ad 522 else
523 {
524 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
525 if (key_pbtn_map[i].key == keycode)
526 return key_pbtn_map[i].pbtn;
527 }
528
529 return 0;
3427e573 530}
3b13ec65 531
ea5da0ba 532/* FIXME: move to plat */
533#if 0
819ef025 534static const struct {
535 short code;
093b8a42 536 char btype;
537 char bit;
819ef025 538} in_evdev_def_binds[] =
539{
540 /* MXYZ SACB RLDU */
093b8a42 541 { KEY_UP, IN_BINDTYPE_PLAYER12, 0 },
542 { KEY_DOWN, IN_BINDTYPE_PLAYER12, 1 },
543 { KEY_LEFT, IN_BINDTYPE_PLAYER12, 2 },
544 { KEY_RIGHT, IN_BINDTYPE_PLAYER12, 3 },
545 { KEY_S, IN_BINDTYPE_PLAYER12, 4 }, /* B */
093b8a42 546 { KEY_D, IN_BINDTYPE_PLAYER12, 5 }, /* C */
093b8a42 547 { KEY_A, IN_BINDTYPE_PLAYER12, 6 }, /* A */
093b8a42 548 { KEY_ENTER, IN_BINDTYPE_PLAYER12, 7 },
603c28b3 549 { KEY_BACKSLASH, IN_BINDTYPE_EMU, PEVB_MENU },
550 /* Pandora */
551 { KEY_PAGEDOWN, IN_BINDTYPE_PLAYER12, 4 },
552 { KEY_END, IN_BINDTYPE_PLAYER12, 5 },
553 { KEY_HOME, IN_BINDTYPE_PLAYER12, 6 },
e708403e 554 { KEY_LEFTALT, IN_BINDTYPE_PLAYER12, 7 },
be672de7 555 { KEY_RIGHTSHIFT,IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
556 { KEY_RIGHTCTRL, IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
557 { KEY_LEFTCTRL, IN_BINDTYPE_EMU, PEVB_MENU },
603c28b3 558 /* Caanoo */
559 { BTN_THUMB, IN_BINDTYPE_PLAYER12, 4 }, /* B */
560 { BTN_THUMB2, IN_BINDTYPE_PLAYER12, 5 }, /* C */
561 { BTN_TRIGGER, IN_BINDTYPE_PLAYER12, 6 }, /* A */
562 { BTN_BASE3, IN_BINDTYPE_PLAYER12, 7 },
563 { BTN_TOP2, IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
564 { BTN_PINKIE, IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
565 { BTN_BASE, IN_BINDTYPE_EMU, PEVB_MENU },
819ef025 566};
ea5da0ba 567#endif
819ef025 568
569static void in_evdev_get_def_binds(int *binds)
570{
571 int i;
572
2c0838c7 573 for (i = 0; ; i++) {
574 if (in_evdev_defbinds[i].bit == 0 && in_evdev_defbinds[i].code == 0)
575 break;
ea5da0ba 576 binds[IN_BIND_OFFS(in_evdev_defbinds[i].code, in_evdev_defbinds[i].btype)] =
577 1 << in_evdev_defbinds[i].bit;
578 }
819ef025 579}
580
483230c7 581/* remove binds of missing keys, count remaining ones */
093b8a42 582static int in_evdev_clean_binds(void *drv_data, int *binds, int *def_binds)
819ef025 583{
093b8a42 584 int keybits[KEY_CNT / sizeof(int)];
483230c7 585 in_evdev_t *dev = drv_data;
093b8a42 586 int i, t, ret, offs, count = 0;
819ef025 587
483230c7 588 ret = ioctl(dev->fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
819ef025 589 if (ret == -1) {
590 perror("in_evdev: ioctl failed");
591 memset(keybits, 0xff, sizeof(keybits)); /* mark all as good */
592 }
593
483230c7 594 if (dev->abs_lzone != 0) {
595 KEYBITS_BIT_SET(KEY_LEFT);
596 KEYBITS_BIT_SET(KEY_RIGHT);
597 }
603c28b3 598 if (dev->abs_lzone != 0) {
483230c7 599 KEYBITS_BIT_SET(KEY_UP);
600 KEYBITS_BIT_SET(KEY_DOWN);
601 }
602
093b8a42 603 for (i = 0; i < KEY_CNT; i++) {
604 for (t = 0; t < IN_BINDTYPE_COUNT; t++) {
605 offs = IN_BIND_OFFS(i, t);
606 if (!KEYBITS_BIT(i))
607 binds[offs] = def_binds[offs] = 0;
608 if (binds[offs])
609 count++;
610 }
819ef025 611 }
612
613 return count;
614}
615
616void in_evdev_init(void *vdrv)
617{
618 in_drv_t *drv = vdrv;
619
620 drv->prefix = in_evdev_prefix;
621 drv->probe = in_evdev_probe;
622 drv->free = in_evdev_free;
623 drv->get_bind_count = in_evdev_get_bind_count;
624 drv->get_def_binds = in_evdev_get_def_binds;
625 drv->clean_binds = in_evdev_clean_binds;
603c28b3 626 drv->set_config = in_evdev_set_config;
7201dc73 627 drv->update_keycode = in_evdev_update_keycode;
819ef025 628 drv->menu_translate = in_evdev_menu_translate;
819ef025 629}
630