pcsx related refactoring
[libpicofe.git] / linux / in_evdev.c
... / ...
CommitLineData
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
11#include <stdio.h>
12#include <stdlib.h>
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
22#include "../common/input.h"
23#include "in_evdev.h"
24
25typedef struct {
26 int fd;
27 int *kbits;
28 int abs_min_x;
29 int abs_max_x;
30 int abs_min_y;
31 int abs_max_y;
32 int abs_lzone;
33 int abs_lastx;
34 int abs_lasty;
35 int kc_first;
36 int kc_last;
37} in_evdev_t;
38
39#ifndef KEY_CNT
40#define KEY_CNT (KEY_MAX + 1)
41#endif
42
43#define KEYBITS_BIT(x) (keybits[(x)/sizeof(keybits[0])/8] & \
44 (1 << ((x) & (sizeof(keybits[0])*8-1))))
45
46#define KEYBITS_BIT_SET(x) (keybits[(x)/sizeof(keybits[0])/8] |= \
47 (1 << ((x) & (sizeof(keybits[0])*8-1))))
48
49#define KEYBITS_BIT_CLEAR(x) (keybits[(x)/sizeof(keybits[0])/8] &= \
50 ~(1 << ((x) & (sizeof(keybits[0])*8-1))))
51
52static const char * const in_evdev_prefix = "evdev:";
53static const char * const in_evdev_keys[KEY_CNT] = {
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
140static void in_evdev_probe(void)
141{
142 long keybits[KEY_CNT / sizeof(long) / 8];
143 long absbits[(ABS_MAX+1) / sizeof(long) / 8];
144 int i;
145
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
151 for (i = 0;; i++)
152 {
153 int support = 0, count = 0;
154 int u, ret, fd, kc_first, kc_last;
155 in_evdev_t *dev;
156 char name[64];
157
158 snprintf(name, sizeof(name), "/dev/input/event%d", i);
159 fd = open(name, O_RDONLY|O_NONBLOCK);
160 if (fd == -1) {
161 if (errno == EACCES)
162 continue; /* maybe we can access next one */
163 break;
164 }
165
166 /* check supported events */
167 ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support);
168 if (ret == -1) {
169 printf("in_evdev: ioctl failed on %s\n", name);
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) {
178 printf("in_evdev: ioctl failed on %s\n", name);
179 goto skip;
180 }
181
182 /* check for interesting keys */
183 kc_first = KEY_MAX;
184 kc_last = 0;
185 for (u = 0; u < KEY_CNT; u++) {
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 }
194 }
195
196 if (count == 0)
197 goto skip;
198
199 dev = calloc(1, sizeof(*dev));
200 if (dev == NULL)
201 goto skip;
202
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
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;
225 dev->abs_lzone = dist / 4;
226 dev->abs_min_x = ainfo.minimum;
227 dev->abs_max_x = ainfo.maximum;
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;
234 dev->abs_min_y = ainfo.minimum;
235 dev->abs_max_y = ainfo.maximum;
236 }
237 }
238
239no_abs:
240 dev->fd = fd;
241 dev->kc_first = kc_first;
242 dev->kc_last = kc_last;
243 strcpy(name, in_evdev_prefix);
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);
247 in_register(name, IN_DRVID_EVDEV, fd, dev, KEY_CNT, in_evdev_keys, 0);
248 continue;
249
250skip:
251 close(fd);
252 }
253}
254
255static void in_evdev_free(void *drv_data)
256{
257 in_evdev_t *dev = drv_data;
258 if (dev == NULL)
259 return;
260 close(dev->fd);
261 free(dev);
262}
263
264static int in_evdev_get_bind_count(void)
265{
266 return KEY_CNT;
267}
268
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)
279{
280 struct input_event ev[16];
281 struct input_absinfo ainfo;
282 int keybits_[KEY_CNT / sizeof(int)];
283 int *keybits = keybits_;
284 in_evdev_t *dev = drv_data;
285 int rd, ret, u, lzone;
286
287 if (dev->kbits == NULL) {
288 ret = ioctl(dev->fd, EVIOCGKEY(sizeof(keybits_)), keybits_);
289 if (ret == -1) {
290 perror("in_evdev: ioctl failed");
291 return -1;
292 }
293 }
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 }
312 }
313
314 for (u = dev->kc_first; u <= dev->kc_last; u++) {
315 if (KEYBITS_BIT(u))
316 or_binds(binds, u, result);
317 }
318
319 /* map X and Y absolute to UDLR */
320 lzone = dev->abs_lzone;
321 if (lzone != 0) {
322 ret = ioctl(dev->fd, EVIOCGABS(ABS_X), &ainfo);
323 if (ret != -1) {
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);
326 }
327 }
328 if (lzone != 0) {
329 ret = ioctl(dev->fd, EVIOCGABS(ABS_Y), &ainfo);
330 if (ret != -1) {
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);
333 }
334 }
335
336 return 0;
337}
338
339static int in_evdev_set_blocking(in_evdev_t *dev, int y)
340{
341 long flags;
342 int ret;
343
344 flags = (long)fcntl(dev->fd, F_GETFL);
345 if ((int)flags == -1) {
346 perror("in_evdev: F_GETFL fcntl failed");
347 return -1;
348 }
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
359 if (y)
360 flags &= ~O_NONBLOCK;
361 else
362 flags |= O_NONBLOCK;
363 ret = fcntl(dev->fd, F_SETFL, flags);
364 if (ret == -1) {
365 perror("in_evdev: F_SETFL fcntl failed");
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;
396}
397
398static int in_evdev_update_keycode(void *data, int *is_down)
399{
400 int ret_kc = -1, ret_down = 0;
401 in_evdev_t *dev = data;
402 struct input_event ev;
403 int rd;
404
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);
412 }
413 goto out;
414 }
415
416 if (ev.type == EV_KEY) {
417 if (ev.value < 0 || ev.value > 1)
418 goto out;
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;
434 }
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;
453 goto out;
454 }
455 ret_kc = *last = down;
456 ret_down = 1;
457 goto out;
458 }
459
460out:
461 if (is_down != NULL)
462 *is_down = ret_down;
463
464 return ret_kc;
465}
466
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 },
476 /* XXX: maybe better set this from it's plat code somehow */
477 /* Pandora */
478 { KEY_END, PBTN_MOK },
479 { KEY_PAGEDOWN, PBTN_MBACK },
480 { KEY_HOME, PBTN_MA2 },
481 { KEY_PAGEUP, PBTN_MA3 },
482 { KEY_LEFTCTRL, PBTN_MENU },
483 { KEY_RIGHTSHIFT, PBTN_L },
484 { KEY_RIGHTCTRL, PBTN_R },
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 },
500 { KEY_RIGHTBRACE, PBTN_R },
501};
502
503#define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
504
505static int in_evdev_menu_translate(void *drv_data, int keycode)
506{
507 in_evdev_t *dev = drv_data;
508 int i;
509
510 if (keycode < 0)
511 {
512 /* menu -> kc */
513 keycode = -keycode;
514 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
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 }
521 }
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;
530}
531
532/* FIXME: move to plat */
533#if 0
534static const struct {
535 short code;
536 char btype;
537 char bit;
538} in_evdev_def_binds[] =
539{
540 /* MXYZ SACB RLDU */
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 */
546 { KEY_D, IN_BINDTYPE_PLAYER12, 5 }, /* C */
547 { KEY_A, IN_BINDTYPE_PLAYER12, 6 }, /* A */
548 { KEY_ENTER, IN_BINDTYPE_PLAYER12, 7 },
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 },
554 { KEY_LEFTALT, IN_BINDTYPE_PLAYER12, 7 },
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 },
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 },
566};
567#endif
568
569static void in_evdev_get_def_binds(int *binds)
570{
571 int i;
572
573 for (i = 0; ; i++) {
574 if (in_evdev_defbinds[i].bit == 0 && in_evdev_defbinds[i].code == 0)
575 break;
576 binds[IN_BIND_OFFS(in_evdev_defbinds[i].code, in_evdev_defbinds[i].btype)] =
577 1 << in_evdev_defbinds[i].bit;
578 }
579}
580
581/* remove binds of missing keys, count remaining ones */
582static int in_evdev_clean_binds(void *drv_data, int *binds, int *def_binds)
583{
584 int keybits[KEY_CNT / sizeof(int)];
585 in_evdev_t *dev = drv_data;
586 int i, t, ret, offs, count = 0;
587
588 ret = ioctl(dev->fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
589 if (ret == -1) {
590 perror("in_evdev: ioctl failed");
591 memset(keybits, 0xff, sizeof(keybits)); /* mark all as good */
592 }
593
594 if (dev->abs_lzone != 0) {
595 KEYBITS_BIT_SET(KEY_LEFT);
596 KEYBITS_BIT_SET(KEY_RIGHT);
597 }
598 if (dev->abs_lzone != 0) {
599 KEYBITS_BIT_SET(KEY_UP);
600 KEYBITS_BIT_SET(KEY_DOWN);
601 }
602
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 }
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;
626 drv->set_config = in_evdev_set_config;
627 drv->update_keycode = in_evdev_update_keycode;
628 drv->menu_translate = in_evdev_menu_translate;
629}
630