698517be |
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 | |
1b0c5139 |
25 | #define MAX_ABS_DEVS 8 |
26 | |
698517be |
27 | typedef struct { |
28 | int fd; |
29 | int *kbits; |
1b0c5139 |
30 | int abs_min_x; /* abs->digital mapping */ |
698517be |
31 | int abs_max_x; |
32 | int abs_min_y; |
33 | int abs_max_y; |
34 | int abs_lzone; |
35 | int abs_lastx; |
36 | int abs_lasty; |
37 | int kc_first; |
38 | int kc_last; |
1b0c5139 |
39 | unsigned int abs_count; |
40 | int abs_mult[MAX_ABS_DEVS]; /* 16.16 multiplier to IN_ABS_RANGE */ |
41 | int abs_adj[MAX_ABS_DEVS]; /* adjust for centering */ |
42 | unsigned int abs_to_digital:1; |
698517be |
43 | } in_evdev_t; |
44 | |
45 | #ifndef KEY_CNT |
46 | #define KEY_CNT (KEY_MAX + 1) |
47 | #endif |
48 | |
49 | #define KEYBITS_BIT(x) (keybits[(x)/sizeof(keybits[0])/8] & \ |
50 | (1 << ((x) & (sizeof(keybits[0])*8-1)))) |
51 | |
52 | #define KEYBITS_BIT_SET(x) (keybits[(x)/sizeof(keybits[0])/8] |= \ |
53 | (1 << ((x) & (sizeof(keybits[0])*8-1)))) |
54 | |
55 | #define KEYBITS_BIT_CLEAR(x) (keybits[(x)/sizeof(keybits[0])/8] &= \ |
56 | ~(1 << ((x) & (sizeof(keybits[0])*8-1)))) |
57 | |
1bba42a5 |
58 | int in_evdev_allow_abs_only; |
59 | |
f2de172b |
60 | #define IN_EVDEV_PREFIX "evdev:" |
61 | |
698517be |
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", |
146 | }; |
147 | |
148 | |
149 | static void in_evdev_probe(void) |
150 | { |
151 | long keybits[KEY_CNT / sizeof(long) / 8]; |
152 | long absbits[(ABS_MAX+1) / sizeof(long) / 8]; |
153 | int i; |
154 | |
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)); |
159 | |
160 | for (i = 0;; i++) |
161 | { |
162 | int support = 0, count = 0; |
1b0c5139 |
163 | int u, ret, fd, kc_first = KEY_MAX, kc_last = 0, have_abs = 0; |
698517be |
164 | in_evdev_t *dev; |
165 | char name[64]; |
166 | |
167 | snprintf(name, sizeof(name), "/dev/input/event%d", i); |
168 | fd = open(name, O_RDONLY|O_NONBLOCK); |
169 | if (fd == -1) { |
170 | if (errno == EACCES) |
171 | continue; /* maybe we can access next one */ |
172 | break; |
173 | } |
174 | |
175 | /* check supported events */ |
176 | ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support); |
177 | if (ret == -1) { |
178 | printf("in_evdev: ioctl failed on %s\n", name); |
179 | goto skip; |
180 | } |
181 | |
1bba42a5 |
182 | if (support & (1 << EV_KEY)) { |
183 | ret = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits); |
184 | if (ret == -1) { |
185 | printf("in_evdev: ioctl failed on %s\n", name); |
186 | goto skip; |
187 | } |
698517be |
188 | |
1bba42a5 |
189 | /* check for interesting keys */ |
190 | for (u = 0; u < KEY_CNT; u++) { |
191 | if (KEYBITS_BIT(u)) { |
192 | if (u < kc_first) |
193 | kc_first = u; |
194 | if (u > kc_last) |
195 | kc_last = u; |
196 | if (u != KEY_POWER && u != KEY_SLEEP && u != BTN_TOUCH) |
197 | count++; |
198 | if (u == BTN_TOUCH) /* we can't deal with ts currently */ |
199 | goto skip; |
200 | } |
698517be |
201 | } |
202 | } |
203 | |
698517be |
204 | dev = calloc(1, sizeof(*dev)); |
205 | if (dev == NULL) |
206 | goto skip; |
207 | |
208 | ret = ioctl(fd, EVIOCGKEY(sizeof(keybits)), keybits); |
209 | if (ret == -1) { |
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) { |
213 | free(dev); |
214 | goto skip; |
215 | } |
216 | } |
217 | |
218 | /* check for abs too */ |
219 | if (support & (1 << EV_ABS)) { |
220 | struct input_absinfo ainfo; |
221 | int dist; |
222 | ret = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits); |
223 | if (ret == -1) |
224 | goto no_abs; |
225 | if (absbits[0] & (1 << ABS_X)) { |
226 | ret = ioctl(fd, EVIOCGABS(ABS_X), &ainfo); |
227 | if (ret == -1) |
228 | goto no_abs; |
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; |
233 | } |
234 | if (absbits[0] & (1 << ABS_Y)) { |
235 | ret = ioctl(fd, EVIOCGABS(ABS_Y), &ainfo); |
236 | if (ret == -1) |
237 | goto no_abs; |
238 | dist = ainfo.maximum - ainfo.minimum; |
239 | dev->abs_min_y = ainfo.minimum; |
240 | dev->abs_max_y = ainfo.maximum; |
241 | } |
1b0c5139 |
242 | for (u = 0; u < MAX_ABS_DEVS; u++) { |
243 | ret = ioctl(fd, EVIOCGABS(u), &ainfo); |
244 | if (ret == -1) |
245 | break; |
246 | dist = ainfo.maximum - ainfo.minimum; |
247 | if (dist != 0) |
248 | dev->abs_mult[u] = IN_ABS_RANGE * 2 * 65536 / dist; |
249 | dev->abs_adj[u] = -(ainfo.maximum + ainfo.minimum + 1) / 2; |
250 | have_abs = 1; |
251 | } |
252 | dev->abs_count = u; |
698517be |
253 | } |
254 | |
255 | no_abs: |
1b0c5139 |
256 | if (count == 0 && !have_abs) { |
1bba42a5 |
257 | free(dev); |
258 | goto skip; |
259 | } |
260 | |
698517be |
261 | dev->fd = fd; |
262 | dev->kc_first = kc_first; |
263 | dev->kc_last = kc_last; |
1b0c5139 |
264 | if (count > 0 || in_evdev_allow_abs_only) |
265 | dev->abs_to_digital = 1; |
f2de172b |
266 | strcpy(name, IN_EVDEV_PREFIX); |
698517be |
267 | ioctl(fd, EVIOCGNAME(sizeof(name)-6), name+6); |
268 | printf("in_evdev: found \"%s\" with %d events (type %08x)\n", |
269 | name+6, count, support); |
f2de172b |
270 | in_register(name, fd, dev, KEY_CNT, in_evdev_keys, 0); |
698517be |
271 | continue; |
272 | |
273 | skip: |
274 | close(fd); |
275 | } |
276 | } |
277 | |
278 | static void in_evdev_free(void *drv_data) |
279 | { |
280 | in_evdev_t *dev = drv_data; |
281 | if (dev == NULL) |
282 | return; |
283 | close(dev->fd); |
284 | free(dev); |
285 | } |
286 | |
605e6809 |
287 | static const char * const * |
288 | in_evdev_get_key_names(int *count) |
698517be |
289 | { |
605e6809 |
290 | *count = KEY_CNT; |
291 | return in_evdev_keys; |
698517be |
292 | } |
293 | |
294 | static void or_binds(const int *binds, int key, int *result) |
295 | { |
296 | int t; |
297 | for (t = 0; t < IN_BINDTYPE_COUNT; t++) |
298 | result[t] |= binds[IN_BIND_OFFS(key, t)]; |
299 | } |
300 | |
301 | /* ORs result with binds of pressed buttons |
302 | * XXX: should measure performance hit of this func, might need to optimize */ |
f2de172b |
303 | static int in_evdev_update(void *drv_data, const int *binds, int *result) |
698517be |
304 | { |
305 | struct input_event ev[16]; |
306 | struct input_absinfo ainfo; |
307 | int keybits_[KEY_CNT / sizeof(int)]; |
308 | int *keybits = keybits_; |
309 | in_evdev_t *dev = drv_data; |
310 | int rd, ret, u, lzone; |
311 | |
312 | if (dev->kbits == NULL) { |
313 | ret = ioctl(dev->fd, EVIOCGKEY(sizeof(keybits_)), keybits_); |
314 | if (ret == -1) { |
315 | perror("in_evdev: ioctl failed"); |
316 | return -1; |
317 | } |
318 | } |
319 | else { |
320 | keybits = dev->kbits; |
321 | while (1) { |
322 | rd = read(dev->fd, ev, sizeof(ev)); |
323 | if (rd < (int)sizeof(ev[0])) { |
324 | if (errno != EAGAIN) |
325 | perror("in_evdev: read failed"); |
326 | break; |
327 | } |
328 | for (u = 0; u < rd / sizeof(ev[0]); u++) { |
329 | if (ev[u].type != EV_KEY) |
330 | continue; |
331 | else if (ev[u].value == 1) |
332 | KEYBITS_BIT_SET(ev[u].code); |
333 | else if (ev[u].value == 0) |
334 | KEYBITS_BIT_CLEAR(ev[u].code); |
335 | } |
336 | } |
337 | } |
338 | |
339 | for (u = dev->kc_first; u <= dev->kc_last; u++) { |
340 | if (KEYBITS_BIT(u)) |
341 | or_binds(binds, u, result); |
342 | } |
343 | |
344 | /* map X and Y absolute to UDLR */ |
345 | lzone = dev->abs_lzone; |
1b0c5139 |
346 | if (dev->abs_to_digital && lzone != 0) { |
698517be |
347 | ret = ioctl(dev->fd, EVIOCGABS(ABS_X), &ainfo); |
348 | if (ret != -1) { |
349 | if (ainfo.value < dev->abs_min_x + lzone) or_binds(binds, KEY_LEFT, result); |
350 | if (ainfo.value > dev->abs_max_x - lzone) or_binds(binds, KEY_RIGHT, result); |
351 | } |
698517be |
352 | ret = ioctl(dev->fd, EVIOCGABS(ABS_Y), &ainfo); |
353 | if (ret != -1) { |
354 | if (ainfo.value < dev->abs_min_y + lzone) or_binds(binds, KEY_UP, result); |
355 | if (ainfo.value > dev->abs_max_y - lzone) or_binds(binds, KEY_DOWN, result); |
356 | } |
357 | } |
358 | |
359 | return 0; |
360 | } |
361 | |
1b0c5139 |
362 | static int in_evdev_update_analog(void *drv_data, int axis_id, int *result) |
363 | { |
364 | struct input_absinfo ainfo; |
365 | in_evdev_t *dev = drv_data; |
366 | int ret; |
367 | |
368 | if ((unsigned int)axis_id >= MAX_ABS_DEVS) |
369 | return -1; |
370 | |
371 | ret = ioctl(dev->fd, EVIOCGABS(axis_id), &ainfo); |
372 | if (ret != 0) |
373 | return ret; |
374 | |
375 | *result = (ainfo.value + dev->abs_adj[axis_id]) * dev->abs_mult[axis_id]; |
376 | *result >>= 16; |
377 | return 0; |
378 | } |
379 | |
698517be |
380 | static int in_evdev_set_blocking(in_evdev_t *dev, int y) |
381 | { |
382 | long flags; |
383 | int ret; |
384 | |
385 | flags = (long)fcntl(dev->fd, F_GETFL); |
386 | if ((int)flags == -1) { |
387 | perror("in_evdev: F_GETFL fcntl failed"); |
388 | return -1; |
389 | } |
390 | |
391 | if (flags & O_NONBLOCK) { |
392 | /* flush the event queue */ |
393 | struct input_event ev; |
394 | do { |
395 | ret = read(dev->fd, &ev, sizeof(ev)); |
396 | } |
397 | while (ret == sizeof(ev)); |
398 | } |
399 | |
400 | if (y) |
401 | flags &= ~O_NONBLOCK; |
402 | else |
403 | flags |= O_NONBLOCK; |
404 | ret = fcntl(dev->fd, F_SETFL, flags); |
405 | if (ret == -1) { |
406 | perror("in_evdev: F_SETFL fcntl failed"); |
407 | return -1; |
408 | } |
409 | |
410 | return 0; |
411 | } |
412 | |
1b0c5139 |
413 | static int in_evdev_get_config(void *drv_data, int what, int *val) |
414 | { |
415 | in_evdev_t *dev = drv_data; |
416 | |
417 | switch (what) { |
418 | case IN_CFG_ABS_AXIS_COUNT: |
419 | *val = dev->abs_count; |
420 | break; |
421 | default: |
422 | return -1; |
423 | } |
424 | |
425 | return 0; |
426 | } |
427 | |
698517be |
428 | static int in_evdev_set_config(void *drv_data, int what, int val) |
429 | { |
430 | in_evdev_t *dev = drv_data; |
431 | int tmp; |
432 | |
433 | switch (what) { |
434 | case IN_CFG_BLOCKING: |
435 | return in_evdev_set_blocking(dev, val); |
436 | case IN_CFG_ABS_DEAD_ZONE: |
437 | if (val < 1 || val > 99 || dev->abs_lzone == 0) |
438 | return -1; |
439 | /* XXX: based on X axis only, for now.. */ |
440 | tmp = (dev->abs_max_x - dev->abs_min_x) / 2; |
441 | dev->abs_lzone = tmp - tmp * val / 100; |
442 | if (dev->abs_lzone < 1) |
443 | dev->abs_lzone = 1; |
444 | else if (dev->abs_lzone >= tmp) |
445 | dev->abs_lzone = tmp - 1; |
446 | break; |
447 | default: |
448 | return -1; |
449 | } |
450 | |
451 | return 0; |
452 | } |
453 | |
454 | static int in_evdev_update_keycode(void *data, int *is_down) |
455 | { |
456 | int ret_kc = -1, ret_down = 0; |
457 | in_evdev_t *dev = data; |
458 | struct input_event ev; |
459 | int rd; |
460 | |
7eda34c1 |
461 | /* do single event, the caller sometimes wants |
462 | * to do select() in blocking mode */ |
463 | rd = read(dev->fd, &ev, sizeof(ev)); |
464 | if (rd < (int) sizeof(ev)) { |
465 | if (errno != EAGAIN) { |
466 | perror("in_evdev: error reading"); |
605e6809 |
467 | //sleep(1); |
468 | ret_kc = -2; |
698517be |
469 | } |
7eda34c1 |
470 | goto out; |
471 | } |
698517be |
472 | |
7eda34c1 |
473 | if (ev.type == EV_KEY) { |
474 | if (ev.value < 0 || ev.value > 1) |
698517be |
475 | goto out; |
7eda34c1 |
476 | ret_kc = ev.code; |
477 | ret_down = ev.value; |
478 | goto out; |
479 | } |
1b0c5139 |
480 | else if (ev.type == EV_ABS && dev->abs_to_digital) |
7eda34c1 |
481 | { |
482 | int lzone = dev->abs_lzone, down = 0, *last; |
483 | |
484 | // map absolute to up/down/left/right |
485 | if (lzone != 0 && ev.code == ABS_X) { |
486 | if (ev.value < dev->abs_min_x + lzone) |
487 | down = KEY_LEFT; |
488 | else if (ev.value > dev->abs_max_x - lzone) |
489 | down = KEY_RIGHT; |
490 | last = &dev->abs_lastx; |
698517be |
491 | } |
7eda34c1 |
492 | else if (lzone != 0 && ev.code == ABS_Y) { |
493 | if (ev.value < dev->abs_min_y + lzone) |
494 | down = KEY_UP; |
495 | else if (ev.value > dev->abs_max_y - lzone) |
496 | down = KEY_DOWN; |
497 | last = &dev->abs_lasty; |
498 | } |
499 | else |
500 | goto out; |
501 | |
502 | if (down == *last) |
503 | goto out; |
504 | |
505 | if (down == 0 || *last != 0) { |
506 | /* key up or direction change, return up event for old key */ |
507 | ret_kc = *last; |
508 | ret_down = 0; |
509 | *last = 0; |
698517be |
510 | goto out; |
511 | } |
7eda34c1 |
512 | ret_kc = *last = down; |
513 | ret_down = 1; |
514 | goto out; |
698517be |
515 | } |
516 | |
517 | out: |
518 | if (is_down != NULL) |
519 | *is_down = ret_down; |
520 | |
521 | return ret_kc; |
522 | } |
523 | |
524 | static const struct { |
525 | short key; |
526 | short pbtn; |
527 | } key_pbtn_map[] = |
528 | { |
529 | { KEY_UP, PBTN_UP }, |
530 | { KEY_DOWN, PBTN_DOWN }, |
531 | { KEY_LEFT, PBTN_LEFT }, |
532 | { KEY_RIGHT, PBTN_RIGHT }, |
533 | /* XXX: maybe better set this from it's plat code somehow */ |
534 | /* Pandora */ |
535 | { KEY_END, PBTN_MOK }, |
536 | { KEY_PAGEDOWN, PBTN_MBACK }, |
537 | { KEY_HOME, PBTN_MA2 }, |
538 | { KEY_PAGEUP, PBTN_MA3 }, |
539 | { KEY_LEFTCTRL, PBTN_MENU }, |
540 | { KEY_RIGHTSHIFT, PBTN_L }, |
541 | { KEY_RIGHTCTRL, PBTN_R }, |
542 | /* Caanoo */ |
543 | { BTN_THUMB2, PBTN_MOK }, |
544 | { BTN_THUMB, PBTN_MBACK }, |
545 | { BTN_TRIGGER, PBTN_MA2 }, |
546 | { BTN_TOP, PBTN_MA3 }, |
547 | { BTN_BASE, PBTN_MENU }, |
548 | { BTN_TOP2, PBTN_L }, |
549 | { BTN_PINKIE, PBTN_R }, |
550 | /* "normal" keyboards */ |
551 | { KEY_ENTER, PBTN_MOK }, |
552 | { KEY_ESC, PBTN_MBACK }, |
553 | { KEY_A, PBTN_MA2 }, |
554 | { KEY_S, PBTN_MA3 }, |
555 | { KEY_BACKSLASH, PBTN_MENU }, |
556 | { KEY_LEFTBRACE, PBTN_L }, |
557 | { KEY_RIGHTBRACE, PBTN_R }, |
558 | }; |
559 | |
560 | #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0])) |
561 | |
61f97bb0 |
562 | static int in_evdev_menu_translate(void *drv_data, int keycode, char *charcode) |
698517be |
563 | { |
564 | in_evdev_t *dev = drv_data; |
61f97bb0 |
565 | int ret = 0; |
698517be |
566 | int i; |
567 | |
568 | if (keycode < 0) |
569 | { |
570 | /* menu -> kc */ |
571 | keycode = -keycode; |
572 | for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) |
573 | if (key_pbtn_map[i].pbtn == keycode) { |
574 | int k = key_pbtn_map[i].key; |
575 | /* should really check EVIOCGBIT, but this is enough for now */ |
576 | if (dev->kc_first <= k && k <= dev->kc_last) |
577 | return k; |
578 | } |
579 | } |
580 | else |
581 | { |
61f97bb0 |
582 | for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) { |
583 | if (key_pbtn_map[i].key == keycode) { |
584 | ret = key_pbtn_map[i].pbtn; |
585 | break; |
586 | } |
587 | } |
588 | |
589 | if (charcode != NULL && (unsigned int)keycode < KEY_CNT && |
590 | in_evdev_keys[keycode] != NULL && in_evdev_keys[keycode][1] == 0) |
591 | { |
592 | char c = in_evdev_keys[keycode][0]; |
593 | if ('A' <= c && c <= 'Z') |
594 | c = 'a' + c - 'A'; |
595 | ret |= PBTN_CHAR; |
596 | *charcode = c; |
597 | } |
698517be |
598 | } |
599 | |
61f97bb0 |
600 | return ret; |
698517be |
601 | } |
602 | |
698517be |
603 | /* remove binds of missing keys, count remaining ones */ |
604 | static int in_evdev_clean_binds(void *drv_data, int *binds, int *def_binds) |
605 | { |
606 | int keybits[KEY_CNT / sizeof(int)]; |
607 | in_evdev_t *dev = drv_data; |
608 | int i, t, ret, offs, count = 0; |
609 | |
2c886904 |
610 | memset(keybits, 0, sizeof(keybits)); |
698517be |
611 | ret = ioctl(dev->fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits); |
612 | if (ret == -1) { |
613 | perror("in_evdev: ioctl failed"); |
2c886904 |
614 | // memset(keybits, 0xff, sizeof(keybits)); /* mark all as good */ |
698517be |
615 | } |
616 | |
1b0c5139 |
617 | if (dev->abs_to_digital && dev->abs_lzone != 0) { |
698517be |
618 | KEYBITS_BIT_SET(KEY_LEFT); |
619 | KEYBITS_BIT_SET(KEY_RIGHT); |
698517be |
620 | KEYBITS_BIT_SET(KEY_UP); |
621 | KEYBITS_BIT_SET(KEY_DOWN); |
622 | } |
623 | |
624 | for (i = 0; i < KEY_CNT; i++) { |
625 | for (t = 0; t < IN_BINDTYPE_COUNT; t++) { |
626 | offs = IN_BIND_OFFS(i, t); |
627 | if (!KEYBITS_BIT(i)) |
628 | binds[offs] = def_binds[offs] = 0; |
629 | if (binds[offs]) |
630 | count++; |
631 | } |
632 | } |
633 | |
634 | return count; |
635 | } |
636 | |
f2de172b |
637 | static const in_drv_t in_evdev_drv = { |
638 | .prefix = IN_EVDEV_PREFIX, |
639 | .probe = in_evdev_probe, |
640 | .free = in_evdev_free, |
641 | .get_key_names = in_evdev_get_key_names, |
f2de172b |
642 | .clean_binds = in_evdev_clean_binds, |
1b0c5139 |
643 | .get_config = in_evdev_get_config, |
f2de172b |
644 | .set_config = in_evdev_set_config, |
645 | .update = in_evdev_update, |
1b0c5139 |
646 | .update_analog = in_evdev_update_analog, |
f2de172b |
647 | .update_keycode = in_evdev_update_keycode, |
648 | .menu_translate = in_evdev_menu_translate, |
649 | }; |
650 | |
b07c18e8 |
651 | void in_evdev_init(const struct in_default_bind *defbinds) |
698517be |
652 | { |
b07c18e8 |
653 | in_register_driver(&in_evdev_drv, defbinds); |
698517be |
654 | } |
655 | |