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