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