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