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