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