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