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