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 | |
15 | #include "input.h" |
16 | #include "plat.h" |
17 | #include "lprintf.h" |
18 | |
19 | #ifdef IN_EVDEV |
20 | #include "../linux/in_evdev.h" |
21 | #endif |
22 | #ifdef IN_GP2X |
23 | #include "../gp2x/in_gp2x.h" |
24 | #endif |
25 | #ifdef IN_VK |
26 | #include "../win32/in_vk.h" |
27 | #endif |
28 | |
29 | typedef struct |
30 | { |
31 | int drv_id; |
32 | int drv_fd_hnd; |
33 | void *drv_data; |
34 | char *name; |
35 | int key_count; |
36 | int *binds; /* total = key_count * bindtypes * 2 */ |
37 | const char * const *key_names; |
38 | unsigned int probed:1; |
39 | unsigned int does_combos:1; |
40 | } in_dev_t; |
41 | |
42 | static in_drv_t in_drivers[IN_DRVID_COUNT]; |
43 | static in_dev_t in_devices[IN_MAX_DEVS]; |
605e6809 |
44 | static int in_dev_count = 0; /* probed + bind devices */ |
698517be |
45 | static int in_have_async_devs = 0; |
46 | static int menu_key_state = 0; |
47 | static int menu_last_used_dev = 0; |
48 | |
49 | #define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0] |
50 | |
51 | |
52 | static int *in_alloc_binds(int drv_id, int key_count) |
53 | { |
54 | int *binds; |
55 | |
56 | binds = calloc(key_count * IN_BINDTYPE_COUNT * 2, sizeof(binds[0])); |
57 | if (binds == NULL) |
58 | return NULL; |
59 | |
60 | DRV(drv_id).get_def_binds(binds + key_count * IN_BINDTYPE_COUNT); |
61 | memcpy(binds, binds + key_count * IN_BINDTYPE_COUNT, |
62 | sizeof(binds[0]) * key_count * IN_BINDTYPE_COUNT); |
63 | |
64 | return binds; |
65 | } |
66 | |
605e6809 |
67 | static void in_unprobe(in_dev_t *dev) |
698517be |
68 | { |
69 | if (dev->probed) |
70 | DRV(dev->drv_id).free(dev->drv_data); |
71 | dev->probed = 0; |
72 | dev->drv_data = NULL; |
605e6809 |
73 | } |
74 | |
75 | static void in_free(in_dev_t *dev) |
76 | { |
77 | in_unprobe(dev); |
698517be |
78 | free(dev->name); |
79 | dev->name = NULL; |
80 | free(dev->binds); |
81 | dev->binds = NULL; |
82 | } |
83 | |
84 | /* to be called by drivers |
85 | * async devices must set drv_fd_hnd to -1 */ |
86 | void in_register(const char *nname, int drv_id, int drv_fd_hnd, void *drv_data, |
87 | int key_count, const char * const *key_names, int combos) |
88 | { |
89 | int i, ret, dupe_count = 0, *binds; |
90 | char name[256], *name_end, *tmp; |
91 | |
92 | strncpy(name, nname, sizeof(name)); |
93 | name[sizeof(name)-12] = 0; |
94 | name_end = name + strlen(name); |
95 | |
96 | for (i = 0; i < in_dev_count; i++) |
97 | { |
98 | if (in_devices[i].name == NULL) |
99 | continue; |
100 | if (strcmp(in_devices[i].name, name) == 0) |
101 | { |
102 | if (in_devices[i].probed) { |
103 | dupe_count++; |
104 | sprintf(name_end, " [%d]", dupe_count); |
105 | continue; |
106 | } |
107 | goto update; |
108 | } |
109 | } |
110 | |
111 | if (i >= IN_MAX_DEVS) |
112 | { |
113 | /* try to find unused device */ |
114 | for (i = 0; i < IN_MAX_DEVS; i++) |
115 | if (!in_devices[i].probed) break; |
116 | if (i >= IN_MAX_DEVS) { |
117 | lprintf("input: too many devices, can't add %s\n", name); |
118 | return; |
119 | } |
120 | in_free(&in_devices[i]); |
121 | } |
122 | |
123 | tmp = strdup(name); |
124 | if (tmp == NULL) |
125 | return; |
126 | |
127 | binds = in_alloc_binds(drv_id, key_count); |
128 | if (binds == NULL) { |
129 | free(tmp); |
130 | return; |
131 | } |
132 | |
133 | in_devices[i].name = tmp; |
134 | in_devices[i].binds = binds; |
135 | in_devices[i].key_count = key_count; |
136 | if (i + 1 > in_dev_count) |
137 | in_dev_count = i + 1; |
138 | |
139 | lprintf("input: new device #%d \"%s\"\n", i, name); |
140 | update: |
141 | in_devices[i].probed = 1; |
142 | in_devices[i].does_combos = combos; |
143 | in_devices[i].drv_id = drv_id; |
144 | in_devices[i].drv_fd_hnd = drv_fd_hnd; |
145 | in_devices[i].key_names = key_names; |
146 | in_devices[i].drv_data = drv_data; |
147 | |
148 | if (in_devices[i].binds != NULL) { |
149 | ret = DRV(drv_id).clean_binds(drv_data, in_devices[i].binds, |
150 | in_devices[i].binds + key_count * IN_BINDTYPE_COUNT); |
151 | if (ret == 0) { |
152 | /* no useable binds */ |
153 | free(in_devices[i].binds); |
154 | in_devices[i].binds = NULL; |
155 | } |
156 | } |
157 | } |
158 | |
159 | /* key combo handling, to be called by drivers that support it. |
160 | * Only care about IN_BINDTYPE_EMU */ |
161 | void in_combos_find(const int *binds, int last_key, int *combo_keys, int *combo_acts) |
162 | { |
163 | int act, u; |
164 | |
165 | *combo_keys = *combo_acts = 0; |
166 | for (act = 0; act < sizeof(binds[0]) * 8; act++) |
167 | { |
168 | int keyc = 0; |
169 | for (u = 0; u <= last_key; u++) |
170 | if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) |
171 | keyc++; |
172 | |
173 | if (keyc > 1) |
174 | { |
175 | // loop again and mark those keys and actions as combo |
176 | for (u = 0; u <= last_key; u++) |
177 | { |
178 | if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) { |
179 | *combo_keys |= 1 << u; |
180 | *combo_acts |= 1 << act; |
181 | } |
182 | } |
183 | } |
184 | } |
185 | } |
186 | |
187 | int in_combos_do(int keys, const int *binds, int last_key, int combo_keys, int combo_acts) |
188 | { |
189 | int i, ret = 0; |
190 | |
191 | for (i = 0; i <= last_key; i++) |
192 | { |
193 | int acts, acts_c, u; |
194 | |
195 | if (!(keys & (1 << i))) |
196 | continue; |
197 | |
198 | acts = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)]; |
199 | if (!acts) |
200 | continue; |
201 | |
202 | if (!(combo_keys & (1 << i))) { |
203 | ret |= acts; |
204 | continue; |
205 | } |
206 | |
207 | acts_c = acts & combo_acts; |
208 | u = last_key; |
209 | if (acts_c) { |
210 | // let's try to find the other one |
211 | for (u = i + 1; u <= last_key; u++) |
212 | if ( (keys & (1 << u)) && (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & acts_c) ) { |
213 | ret |= acts_c & binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)]; |
214 | keys &= ~((1 << i) | (1 << u)); |
215 | break; |
216 | } |
217 | } |
218 | // add non-combo actions if combo ones were not found |
219 | if (u >= last_key) |
220 | ret |= acts & ~combo_acts; |
221 | } |
222 | |
223 | return ret; |
224 | } |
225 | |
226 | void in_probe(void) |
227 | { |
228 | int i; |
229 | |
230 | in_have_async_devs = 0; |
605e6809 |
231 | menu_key_state = 0; |
232 | menu_last_used_dev = 0; |
233 | |
698517be |
234 | for (i = 0; i < in_dev_count; i++) |
605e6809 |
235 | in_unprobe(&in_devices[i]); |
698517be |
236 | |
237 | for (i = 1; i < IN_DRVID_COUNT; i++) |
238 | in_drivers[i].probe(); |
239 | |
240 | /* get rid of devs without binds and probes */ |
241 | for (i = 0; i < in_dev_count; i++) { |
242 | if (!in_devices[i].probed && in_devices[i].binds == NULL) { |
243 | in_dev_count--; |
244 | if (i < in_dev_count) { |
245 | free(in_devices[i].name); |
246 | memmove(&in_devices[i], &in_devices[i+1], |
247 | (in_dev_count - i) * sizeof(in_devices[0])); |
248 | } |
249 | |
250 | continue; |
251 | } |
252 | |
253 | if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1) |
254 | in_have_async_devs = 1; |
255 | } |
256 | |
257 | if (in_have_async_devs) |
258 | lprintf("input: async-only devices detected..\n"); |
259 | |
260 | in_debug_dump(); |
261 | } |
262 | |
263 | /* async update */ |
264 | int in_update(int *result) |
265 | { |
266 | int i, ret = 0; |
267 | |
268 | for (i = 0; i < in_dev_count; i++) { |
269 | in_dev_t *dev = &in_devices[i]; |
270 | if (dev->probed && dev->binds != NULL) { |
271 | // FIXME: this is stupid, make it indirect |
272 | switch (dev->drv_id) { |
273 | #ifdef IN_EVDEV |
274 | case IN_DRVID_EVDEV: |
275 | ret |= in_evdev_update(dev->drv_data, dev->binds, result); |
276 | break; |
277 | #endif |
278 | #ifdef IN_GP2X |
279 | case IN_DRVID_GP2X: |
280 | ret |= in_gp2x_update(dev->drv_data, dev->binds, result); |
281 | break; |
282 | #endif |
283 | #ifdef IN_VK |
284 | case IN_DRVID_VK: |
285 | ret |= in_vk_update(dev->drv_data, dev->binds, result); |
286 | break; |
287 | #endif |
288 | } |
289 | } |
290 | } |
291 | |
292 | return ret; |
293 | } |
294 | |
295 | static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms) |
296 | { |
297 | int i, is_down, result; |
298 | unsigned int ticks; |
299 | |
300 | ticks = plat_get_ticks_ms(); |
301 | |
302 | while (1) |
303 | { |
304 | for (i = 0; i < in_dev_count; i++) { |
305 | in_dev_t *d = &in_devices[i]; |
306 | if (!d->probed) |
307 | continue; |
308 | |
309 | result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down); |
310 | if (result == -1) |
311 | continue; |
312 | |
313 | if (dev_id_out) |
314 | *dev_id_out = i; |
315 | if (is_down_out) |
316 | *is_down_out = is_down; |
317 | return result; |
318 | } |
319 | |
320 | if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms) |
321 | break; |
322 | |
323 | plat_sleep_ms(10); |
324 | } |
325 | |
326 | return -1; |
327 | } |
328 | |
329 | /* |
330 | * wait for a press, always return some keycode or -1 on timeout or error |
331 | */ |
332 | int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms) |
333 | { |
334 | int result = -1, dev_id = 0, is_down, result_menu; |
335 | int fds_hnds[IN_MAX_DEVS]; |
336 | int i, ret, count = 0; |
337 | in_drv_t *drv = NULL; |
338 | unsigned int ticks; |
339 | |
340 | if (in_have_async_devs) { |
341 | result = in_update_kc_async(&dev_id, &is_down, timeout_ms); |
342 | if (result == -1) |
343 | return -1; |
344 | drv = &DRV(in_devices[dev_id].drv_id); |
345 | goto finish; |
346 | } |
347 | |
348 | ticks = plat_get_ticks_ms(); |
349 | |
350 | for (i = 0; i < in_dev_count; i++) { |
351 | if (in_devices[i].probed) |
352 | fds_hnds[count++] = in_devices[i].drv_fd_hnd; |
353 | } |
354 | |
355 | if (count == 0) { |
356 | /* don't deadlock, fail */ |
357 | lprintf("input: failed to find devices to read\n"); |
358 | exit(1); |
359 | } |
360 | |
361 | while (1) |
362 | { |
363 | ret = plat_wait_event(fds_hnds, count, timeout_ms); |
364 | if (ret < 0) |
365 | break; |
366 | |
367 | for (i = 0; i < in_dev_count; i++) { |
368 | if (in_devices[i].drv_fd_hnd == ret) { |
369 | dev_id = i; |
370 | break; |
371 | } |
372 | } |
373 | |
374 | drv = &DRV(in_devices[dev_id].drv_id); |
375 | result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down); |
376 | if (result >= 0) |
377 | break; |
378 | |
605e6809 |
379 | if (result == -2) { |
380 | lprintf("input: \"%s\" errored out, removing.\n", in_devices[dev_id].name); |
381 | in_unprobe(&in_devices[dev_id]); |
382 | break; |
383 | } |
384 | |
698517be |
385 | if (timeout_ms >= 0) { |
386 | unsigned int ticks2 = plat_get_ticks_ms(); |
387 | timeout_ms -= ticks2 - ticks; |
388 | ticks = ticks2; |
389 | if (timeout_ms <= 0) |
390 | break; |
391 | } |
392 | } |
393 | |
394 | if (result < 0) |
395 | return -1; |
396 | finish: |
397 | /* keep track of menu key state, to allow mixing |
398 | * in_update_keycode() and in_menu_wait_any() calls */ |
399 | result_menu = drv->menu_translate(in_devices[dev_id].drv_data, result); |
400 | if (result_menu != 0) { |
401 | if (is_down) |
402 | menu_key_state |= result_menu; |
403 | else |
404 | menu_key_state &= ~result_menu; |
405 | } |
406 | |
407 | if (dev_id_out != NULL) |
408 | *dev_id_out = dev_id; |
409 | if (is_down_out != NULL) |
410 | *is_down_out = is_down; |
411 | return result; |
412 | } |
413 | |
414 | /* same as above, only return bitfield of PBTN_* */ |
415 | int in_menu_wait_any(int timeout_ms) |
416 | { |
417 | int keys_old = menu_key_state; |
418 | |
419 | while (1) |
420 | { |
421 | int code, is_down = 0, dev_id = 0; |
422 | |
423 | code = in_update_keycode(&dev_id, &is_down, timeout_ms); |
424 | if (code < 0) |
425 | break; |
426 | |
427 | if (keys_old != menu_key_state) { |
428 | menu_last_used_dev = dev_id; |
429 | break; |
430 | } |
431 | } |
432 | |
433 | return menu_key_state; |
434 | } |
435 | |
436 | /* wait for menu input, do autorepeat */ |
437 | int in_menu_wait(int interesting, int autorep_delay_ms) |
438 | { |
439 | static int inp_prev = 0; |
440 | static int repeats = 0; |
441 | int ret, release = 0, wait = 450; |
442 | |
443 | if (repeats) |
444 | wait = autorep_delay_ms; |
445 | |
446 | ret = in_menu_wait_any(wait); |
447 | if (ret == inp_prev) |
448 | repeats++; |
449 | |
450 | while (!(ret & interesting)) { |
451 | ret = in_menu_wait_any(-1); |
452 | release = 1; |
453 | } |
454 | |
455 | if (release || ret != inp_prev) |
456 | repeats = 0; |
457 | |
458 | inp_prev = ret; |
459 | |
460 | /* we don't need diagonals in menus */ |
461 | if ((ret & PBTN_UP) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT; |
462 | if ((ret & PBTN_UP) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT; |
463 | if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT; |
464 | if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT; |
465 | |
466 | return ret; |
467 | } |
468 | |
469 | const int *in_get_dev_binds(int dev_id) |
470 | { |
471 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS) |
472 | return NULL; |
473 | |
474 | return in_devices[dev_id].binds; |
475 | } |
476 | |
477 | const int *in_get_dev_def_binds(int dev_id) |
478 | { |
479 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS) |
480 | return NULL; |
481 | |
482 | return in_devices[dev_id].binds + in_devices[dev_id].key_count * IN_BINDTYPE_COUNT; |
483 | } |
484 | |
485 | int in_get_config(int dev_id, int what, void *val) |
486 | { |
487 | int *ival = val; |
488 | in_dev_t *dev; |
489 | |
490 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS || val == NULL) |
491 | return -1; |
492 | |
493 | dev = &in_devices[dev_id]; |
494 | switch (what) { |
495 | case IN_CFG_BIND_COUNT: |
496 | *ival = dev->key_count; |
497 | break; |
498 | case IN_CFG_DOES_COMBOS: |
499 | *ival = dev->does_combos; |
500 | break; |
501 | case IN_CFG_BLOCKING: |
502 | case IN_CFG_KEY_NAMES: |
503 | return -1; /* not implemented */ |
504 | default: |
505 | return DRV(dev->drv_id).get_config(dev->drv_data, what, ival); |
506 | } |
507 | |
508 | return 0; |
509 | } |
510 | |
511 | static int in_set_blocking(int is_blocking) |
512 | { |
513 | int i, ret; |
514 | |
515 | /* have_async_devs means we will have to do all reads async anyway.. */ |
516 | if (!in_have_async_devs) { |
517 | for (i = 0; i < in_dev_count; i++) { |
518 | if (in_devices[i].probed) |
519 | DRV(in_devices[i].drv_id).set_config(in_devices[i].drv_data, |
520 | IN_CFG_BLOCKING, is_blocking); |
521 | } |
522 | } |
523 | |
524 | menu_key_state = 0; |
525 | |
526 | /* flush events */ |
527 | do { |
528 | ret = in_update_keycode(NULL, NULL, 0); |
529 | } while (ret >= 0); |
530 | |
531 | return 0; |
532 | } |
533 | |
534 | int in_set_config(int dev_id, int what, const void *val, int size) |
535 | { |
536 | const int *ival = val; |
537 | in_dev_t *dev; |
538 | |
539 | if (what == IN_CFG_BLOCKING) |
540 | return in_set_blocking(*ival); |
541 | |
542 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS) |
543 | return -1; |
544 | |
545 | dev = &in_devices[dev_id]; |
546 | if (what == IN_CFG_KEY_NAMES) { |
547 | const char * const *names = val; |
548 | int count = size / sizeof(names[0]); |
549 | |
550 | if (count < dev->key_count) { |
551 | lprintf("input: set_key_names: not enough keys\n"); |
552 | return -1; |
553 | } |
554 | |
555 | dev->key_names = names; |
556 | return 0; |
557 | } |
558 | |
559 | return DRV(dev->drv_id).set_config(dev->drv_data, what, *ival); |
560 | } |
561 | |
562 | const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix) |
563 | { |
564 | const char *name, *tmp; |
565 | |
566 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS) |
567 | return NULL; |
568 | |
569 | if (must_be_active && !in_devices[dev_id].probed) |
570 | return NULL; |
571 | |
572 | name = in_devices[dev_id].name; |
573 | if (name == NULL || !skip_pfix) |
574 | return name; |
575 | |
576 | /* skip prefix */ |
577 | tmp = strchr(name, ':'); |
578 | if (tmp != NULL) |
579 | name = tmp + 1; |
580 | |
581 | return name; |
582 | } |
583 | |
584 | int in_name_to_id(const char *dev_name) |
585 | { |
586 | int i; |
587 | |
588 | for (i = 0; i < in_dev_count; i++) |
589 | if (strcmp(dev_name, in_devices[i].name) == 0) |
590 | break; |
591 | |
592 | if (i >= in_dev_count) { |
593 | lprintf("input: in_name_to_id: no such device: %s\n", dev_name); |
594 | return -1; |
595 | } |
596 | |
597 | return i; |
598 | } |
599 | |
600 | /* never returns NULL */ |
601 | const char *in_get_key_name(int dev_id, int keycode) |
602 | { |
603 | const char *name = NULL; |
604 | static char xname[16]; |
605 | in_dev_t *dev; |
606 | |
607 | if (dev_id < 0) /* want last used dev? */ |
608 | dev_id = menu_last_used_dev; |
609 | |
610 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS) |
611 | return "Unkn0"; |
612 | |
613 | dev = &in_devices[dev_id]; |
614 | if (keycode < 0) /* want name for menu key? */ |
615 | keycode = DRV(dev->drv_id).menu_translate(dev->drv_data, keycode); |
616 | |
617 | if (dev->key_names != NULL && 0 <= keycode && keycode < dev->key_count) |
618 | name = dev->key_names[keycode]; |
619 | if (name != NULL) |
620 | return name; |
621 | |
622 | name = DRV(dev->drv_id).get_key_name(keycode); |
623 | if (name != NULL) |
624 | return name; |
625 | |
626 | /* assume scancode */ |
627 | if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z') |
628 | || (keycode >= 'A' && keycode <= 'Z')) |
629 | sprintf(xname, "%c", keycode); |
630 | else |
631 | sprintf(xname, "\\x%02X", keycode); |
632 | return xname; |
633 | } |
634 | |
635 | int in_get_key_code(int dev_id, const char *key_name) |
636 | { |
637 | in_dev_t *dev; |
638 | int i; |
639 | |
640 | if (dev_id < 0) /* want last used dev? */ |
641 | dev_id = menu_last_used_dev; |
642 | |
643 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS) |
644 | return -1; |
645 | |
646 | dev = &in_devices[dev_id]; |
647 | if (dev->key_names == NULL) |
648 | return -1; |
649 | |
650 | for (i = 0; i < dev->key_count; i++) |
651 | if (dev->key_names[i] && strcasecmp(dev->key_names[i], key_name) == 0) |
652 | return i; |
653 | |
654 | return -1; |
655 | } |
656 | |
657 | int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbind) |
658 | { |
659 | int ret, count; |
660 | in_dev_t *dev; |
661 | |
662 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT) |
663 | return -1; |
664 | |
665 | dev = &in_devices[dev_id]; |
666 | count = dev->key_count; |
667 | |
668 | if (dev->binds == NULL) { |
669 | if (force_unbind) |
670 | return 0; |
671 | dev->binds = in_alloc_binds(dev->drv_id, count); |
672 | if (dev->binds == NULL) |
673 | return -1; |
674 | } |
675 | |
676 | if (keycode < 0 || keycode >= count) |
677 | return -1; |
678 | |
679 | if (force_unbind) |
680 | dev->binds[IN_BIND_OFFS(keycode, bind_type)] &= ~mask; |
681 | else |
682 | dev->binds[IN_BIND_OFFS(keycode, bind_type)] ^= mask; |
683 | |
684 | ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds, |
685 | dev->binds + count * IN_BINDTYPE_COUNT); |
686 | if (ret == 0) { |
687 | free(dev->binds); |
688 | dev->binds = NULL; |
689 | } |
690 | |
691 | return 0; |
692 | } |
693 | |
43bca6fb |
694 | /* |
695 | * Unbind act_mask on binds with type bind_type |
696 | * - if dev_id_ < 0, affects all devices |
697 | * else only affects dev_id_ |
698 | * - if act_mask == -1, unbind all keys |
699 | * else only actions in mask |
700 | */ |
701 | void in_unbind_all(int dev_id_, int act_mask, int bind_type) |
698517be |
702 | { |
43bca6fb |
703 | int dev_id = 0, dev_last = IN_MAX_DEVS - 1; |
698517be |
704 | int i, count; |
705 | in_dev_t *dev; |
706 | |
43bca6fb |
707 | if (dev_id_ >= 0) |
708 | dev_id = dev_last = dev_id_; |
709 | |
710 | if (bind_type >= IN_BINDTYPE_COUNT) |
698517be |
711 | return; |
712 | |
43bca6fb |
713 | for (; dev_id <= dev_last; dev_id++) { |
714 | dev = &in_devices[dev_id]; |
715 | count = dev->key_count; |
698517be |
716 | |
43bca6fb |
717 | if (dev->binds == NULL) |
718 | continue; |
698517be |
719 | |
43bca6fb |
720 | if (act_mask != -1) { |
721 | for (i = 0; i < count; i++) |
722 | dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask; |
723 | } |
724 | else |
725 | memset(dev->binds, 0, sizeof(dev->binds[0]) * count * IN_BINDTYPE_COUNT); |
726 | } |
698517be |
727 | } |
728 | |
729 | /* returns device id, or -1 on error */ |
730 | int in_config_parse_dev(const char *name) |
731 | { |
732 | int drv_id = -1, i; |
733 | |
734 | for (i = 0; i < IN_DRVID_COUNT; i++) { |
735 | int len = strlen(in_drivers[i].prefix); |
736 | if (strncmp(name, in_drivers[i].prefix, len) == 0) { |
737 | drv_id = i; |
738 | break; |
739 | } |
740 | } |
741 | |
742 | if (drv_id < 0) { |
743 | lprintf("input: missing driver for %s\n", name); |
744 | return -1; |
745 | } |
746 | |
747 | for (i = 0; i < in_dev_count; i++) |
748 | { |
749 | if (in_devices[i].name == NULL) |
750 | continue; |
751 | if (strcmp(in_devices[i].name, name) == 0) |
752 | return i; |
753 | } |
754 | |
755 | if (i >= IN_MAX_DEVS) |
756 | { |
757 | /* try to find unused device */ |
758 | for (i = 0; i < IN_MAX_DEVS; i++) |
759 | if (in_devices[i].name == NULL) break; |
760 | if (i >= IN_MAX_DEVS) { |
761 | lprintf("input: too many devices, can't add %s\n", name); |
762 | return -1; |
763 | } |
764 | } |
765 | |
766 | memset(&in_devices[i], 0, sizeof(in_devices[i])); |
767 | |
768 | in_devices[i].name = strdup(name); |
769 | if (in_devices[i].name == NULL) |
770 | return -1; |
771 | |
605e6809 |
772 | in_devices[i].key_names = DRV(drv_id).get_key_names(&in_devices[i].key_count); |
698517be |
773 | in_devices[i].drv_id = drv_id; |
774 | |
775 | if (i + 1 > in_dev_count) |
776 | in_dev_count = i + 1; |
777 | |
778 | return i; |
779 | } |
780 | |
698517be |
781 | int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type) |
782 | { |
783 | in_dev_t *dev; |
784 | int i, offs, kc; |
785 | |
786 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT) |
787 | return -1; |
788 | dev = &in_devices[dev_id]; |
789 | |
790 | /* maybe a raw code? */ |
791 | if (key[0] == '\\' && key[1] == 'x') { |
792 | char *p = NULL; |
793 | kc = (int)strtoul(key + 2, &p, 16); |
794 | if (p == NULL || *p != 0) |
795 | kc = -1; |
796 | } |
797 | else { |
798 | /* device specific key name */ |
799 | if (dev->binds == NULL) { |
800 | dev->binds = in_alloc_binds(dev->drv_id, dev->key_count); |
801 | if (dev->binds == NULL) |
802 | return -1; |
698517be |
803 | } |
804 | |
805 | kc = -1; |
806 | if (dev->key_names != NULL) { |
807 | for (i = 0; i < dev->key_count; i++) { |
808 | const char *k = dev->key_names[i]; |
809 | if (k != NULL && strcasecmp(k, key) == 0) { |
810 | kc = i; |
811 | break; |
812 | } |
813 | } |
814 | } |
815 | |
816 | if (kc < 0) |
817 | kc = DRV(dev->drv_id).get_key_code(key); |
818 | if (kc < 0 && strlen(key) == 1) { |
819 | /* assume scancode */ |
820 | kc = key[0]; |
821 | } |
822 | } |
823 | |
824 | if (kc < 0 || kc >= dev->key_count) { |
825 | lprintf("input: bad key: %s\n", key); |
826 | return -1; |
827 | } |
828 | |
829 | if (bind_type == IN_BINDTYPE_NONE) { |
830 | for (i = 0; i < IN_BINDTYPE_COUNT; i++) |
831 | dev->binds[IN_BIND_OFFS(kc, i)] = 0; |
832 | return 0; |
833 | } |
834 | |
835 | offs = IN_BIND_OFFS(kc, bind_type); |
836 | if (dev->binds[offs] == -1) |
837 | dev->binds[offs] = 0; |
838 | dev->binds[offs] |= acts; |
839 | return 0; |
840 | } |
841 | |
0079b7b4 |
842 | void in_clean_binds(void) |
698517be |
843 | { |
844 | int i; |
845 | |
846 | for (i = 0; i < IN_MAX_DEVS; i++) { |
0079b7b4 |
847 | int ret, count, *binds, *def_binds; |
698517be |
848 | in_dev_t *dev = &in_devices[i]; |
849 | |
0079b7b4 |
850 | if (dev->binds == NULL || dev->drv_data == NULL) |
698517be |
851 | continue; |
852 | |
853 | count = dev->key_count; |
854 | binds = dev->binds; |
855 | def_binds = binds + count * IN_BINDTYPE_COUNT; |
856 | |
698517be |
857 | ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds); |
858 | if (ret == 0) { |
859 | /* no useable binds */ |
860 | free(dev->binds); |
861 | dev->binds = NULL; |
862 | } |
863 | } |
864 | } |
865 | |
866 | void in_debug_dump(void) |
867 | { |
868 | int i; |
869 | |
870 | lprintf("# drv probed binds name\n"); |
871 | for (i = 0; i < IN_MAX_DEVS; i++) { |
872 | in_dev_t *d = &in_devices[i]; |
873 | if (!d->probed && d->name == NULL && d->binds == NULL) |
874 | continue; |
875 | lprintf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n', |
876 | d->binds ? 'y' : 'n', d->name); |
877 | } |
878 | } |
879 | |
880 | /* handlers for unknown/not_preset drivers */ |
881 | |
882 | static void in_def_probe(void) {} |
883 | static void in_def_free(void *drv_data) {} |
605e6809 |
884 | static const char * const * |
885 | in_def_get_key_names(int *count) { return NULL; } |
698517be |
886 | static void in_def_get_def_binds(int *binds) {} |
887 | static int in_def_clean_binds(void *drv_data, int *b, int *db) { return 0; } |
888 | static int in_def_get_config(void *drv_data, int what, int *val) { return -1; } |
889 | static int in_def_set_config(void *drv_data, int what, int val) { return -1; } |
890 | static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; } |
891 | static int in_def_menu_translate(void *drv_data, int keycode) { return keycode; } |
892 | static int in_def_get_key_code(const char *key_name) { return -1; } |
893 | static const char *in_def_get_key_name(int keycode) { return NULL; } |
894 | |
895 | void in_init(void) |
896 | { |
897 | int i; |
898 | |
899 | memset(in_drivers, 0, sizeof(in_drivers)); |
900 | memset(in_devices, 0, sizeof(in_devices)); |
901 | in_dev_count = 0; |
902 | |
903 | for (i = 0; i < IN_DRVID_COUNT; i++) { |
904 | in_drivers[i].prefix = "none:"; |
905 | in_drivers[i].probe = in_def_probe; |
906 | in_drivers[i].free = in_def_free; |
605e6809 |
907 | in_drivers[i].get_key_names = in_def_get_key_names; |
698517be |
908 | in_drivers[i].get_def_binds = in_def_get_def_binds; |
909 | in_drivers[i].clean_binds = in_def_clean_binds; |
910 | in_drivers[i].get_config = in_def_get_config; |
911 | in_drivers[i].set_config = in_def_set_config; |
912 | in_drivers[i].update_keycode = in_def_update_keycode; |
913 | in_drivers[i].menu_translate = in_def_menu_translate; |
914 | in_drivers[i].get_key_code = in_def_get_key_code; |
915 | in_drivers[i].get_key_name = in_def_get_key_name; |
916 | } |
917 | |
918 | #ifdef IN_GP2X |
919 | in_gp2x_init(&in_drivers[IN_DRVID_GP2X]); |
920 | #endif |
921 | #ifdef IN_EVDEV |
922 | in_evdev_init(&in_drivers[IN_DRVID_EVDEV]); |
923 | #endif |
924 | #ifdef IN_VK |
925 | in_vk_init(&in_drivers[IN_DRVID_VK]); |
926 | #endif |
927 | } |
928 | |
929 | #if 0 |
930 | int main(void) |
931 | { |
932 | int ret; |
933 | |
934 | in_init(); |
935 | in_probe(); |
936 | |
937 | in_set_blocking(1); |
938 | |
939 | #if 1 |
940 | while (1) { |
941 | int dev = 0, down; |
942 | ret = in_update_keycode(&dev, &down); |
943 | lprintf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret)); |
944 | } |
945 | #else |
946 | while (1) { |
947 | ret = in_menu_wait_any(); |
948 | lprintf("%08x\n", ret); |
949 | } |
950 | #endif |
951 | |
952 | return 0; |
953 | } |
954 | #endif |