src/mess/inputx.c
| r2483 | r2484 | |
| 25 | 25 | |
| 26 | 26 | #define NUM_SIMUL_KEYS (UCHAR_SHIFT_END - UCHAR_SHIFT_BEGIN + 1) |
| 27 | 27 | #define LOG_INPUTX 0 |
| 28 | | #define DUMP_CODES 0 |
| 29 | 28 | #define SPACE_COUNT 3 |
| 30 | 29 | |
| 31 | 30 | |
| r2483 | r2484 | |
| 381 | 380 | |
| 382 | 381 | ***************************************************************************/ |
| 383 | 382 | |
| 384 | | static const char *charstr(unicode_char ch) |
| 383 | /*------------------------------------------------- |
| 384 | code_point_string - obtain a string representation of a |
| 385 | given code; used for logging and debugging |
| 386 | -------------------------------------------------*/ |
| 387 | |
| 388 | static const char *code_point_string(unicode_char ch) |
| 385 | 389 | { |
| 386 | 390 | static char buf[16]; |
| 387 | 391 | const char *result = buf; |
| 388 | 392 | |
| 389 | 393 | switch(ch) |
| 390 | 394 | { |
| 395 | /* check some magic values */ |
| 391 | 396 | case '\0': strcpy(buf, "\\0"); break; |
| 392 | 397 | case '\r': strcpy(buf, "\\r"); break; |
| 393 | 398 | case '\n': strcpy(buf, "\\n"); break; |
| 394 | 399 | case '\t': strcpy(buf, "\\t"); break; |
| 395 | 400 | |
| 396 | 401 | default: |
| 397 | | if (ch < 128) |
| 402 | if ((ch >= 32) && (ch < 128)) |
| 398 | 403 | { |
| 404 | /* seven bit ASCII is easy */ |
| 399 | 405 | buf[0] = (char) ch; |
| 400 | 406 | buf[1] = '\0'; |
| 401 | 407 | } |
| 402 | | else if ((ch >= UCHAR_MAMEKEY_BEGIN) && (ch < UCHAR_MAMEKEY_BEGIN + 1024)) |
| 408 | else if (ch >= UCHAR_MAMEKEY_BEGIN) |
| 403 | 409 | { |
| 410 | /* try to obtain a codename with input_code_name(); this can result in an empty string */ |
| 404 | 411 | astring *astr = astring_alloc(); |
| 405 | 412 | input_code_name(astr, (input_code) ch - UCHAR_MAMEKEY_BEGIN); |
| 406 | 413 | snprintf(buf, ARRAY_LENGTH(buf), "%s", astring_c(astr)); |
| r2483 | r2484 | |
| 408 | 415 | } |
| 409 | 416 | else |
| 410 | 417 | { |
| 418 | /* empty string; resolve later */ |
| 419 | buf[0] = '\0'; |
| 420 | } |
| 421 | |
| 422 | /* did we fail to resolve? if so, we have a last resort */ |
| 423 | if (buf[0] == '\0') |
| 411 | 424 | snprintf(buf, ARRAY_LENGTH(buf), "U+%04X", (unsigned) ch); |
| 412 | | } |
| 413 | 425 | break; |
| 414 | 426 | } |
| 415 | 427 | return result; |
| r2483 | r2484 | |
| 467 | 479 | code_count++; |
| 468 | 480 | |
| 469 | 481 | if (LOG_INPUTX) |
| 470 | | logerror("inputx: code=%i (%s) port=%i ipt->name='%s'\n", (int) code, charstr(code), port, ipt->name); |
| 482 | logerror("inputx: code=%i (%s) port=%i ipt->name='%s'\n", (int) code, code_point_string(code), port, ipt->name); |
| 471 | 483 | } |
| 472 | 484 | } |
| 473 | 485 | break; |
| r2483 | r2484 | |
| 595 | 607 | |
| 596 | 608 | |
| 597 | 609 | /*************************************************************************** |
| 598 | | |
| 599 | | Core |
| 600 | | |
| 610 | CORE IMPLEMENTATION |
| 601 | 611 | ***************************************************************************/ |
| 602 | 612 | |
| 603 | 613 | static mess_input_code *codes; |
| r2483 | r2484 | |
| 613 | 623 | |
| 614 | 624 | |
| 615 | 625 | #ifdef ENABLE_DEBUGGER |
| 616 | | static void execute_input(int ref, int params, const char *param[]) |
| 617 | | { |
| 618 | | inputx_post_coded(param[0]); |
| 619 | | } |
| 626 | static void execute_input(int ref, int params, const char *param[]); |
| 627 | static void execute_dumpkbd(int ref, int params, const char *param[]); |
| 620 | 628 | #endif /* ENABLE_DEBUGGER */ |
| 621 | 629 | |
| 622 | 630 | |
| r2483 | r2484 | |
| 640 | 648 | |
| 641 | 649 | |
| 642 | 650 | |
| 643 | | /*------------------------------------------------- |
| 644 | | dump_codes - output all natural keyboard data |
| 645 | | to a text file |
| 646 | | -------------------------------------------------*/ |
| 647 | | |
| 648 | | static void dump_codes(void) |
| 649 | | { |
| 650 | | FILE *f; |
| 651 | | int i; |
| 652 | | |
| 653 | | f = fopen("codes.txt", "w"); |
| 654 | | if (!f) |
| 655 | | return; |
| 656 | | |
| 657 | | if (codes) |
| 658 | | { |
| 659 | | for (i = 0; codes[i].ch; i++) |
| 660 | | { |
| 661 | | fprintf(f, "%-10d (%s)\n", codes[i].ch, charstr(codes[i].ch)); |
| 662 | | } |
| 663 | | } |
| 664 | | fclose(f); |
| 665 | | } |
| 666 | | |
| 667 | | |
| 668 | | |
| 669 | 651 | void inputx_init(running_machine *machine) |
| 670 | 652 | { |
| 671 | 653 | codes = NULL; |
| r2483 | r2484 | |
| 677 | 659 | |
| 678 | 660 | #ifdef ENABLE_DEBUGGER |
| 679 | 661 | if (machine->debug_mode) |
| 662 | { |
| 680 | 663 | debug_console_register_command("input", CMDFLAG_NONE, 0, 1, 1, execute_input); |
| 664 | debug_console_register_command("dumpkbd", CMDFLAG_NONE, 0, 0, 1, execute_dumpkbd); |
| 665 | } |
| 681 | 666 | #endif /* ENABLE_DEBUGGER */ |
| 682 | 667 | |
| 683 | 668 | /* posting keys directly only makes sense for a computer */ |
| 684 | 669 | if (machine->gamedrv->flags & GAME_COMPUTER) |
| 685 | 670 | { |
| 686 | 671 | codes = build_codes(machine->input_ports); |
| 687 | | if (DUMP_CODES) |
| 688 | | dump_codes(); |
| 689 | 672 | setup_keybuffer(machine); |
| 690 | 673 | } |
| 691 | 674 | } |
| r2483 | r2484 | |
| 868 | 851 | if (LOG_INPUTX) |
| 869 | 852 | { |
| 870 | 853 | code = find_code(ch); |
| 871 | | logerror("inputx_postn(): code=%i (%s) port=%i ipt->name='%s'\n", (int) ch, charstr(ch), code ? code->port[0] : -1, (code && code->ipt[0]) ? code->ipt[0]->name : "<null>"); |
| 854 | logerror("inputx_postn(): code=%i (%s) port=%i ipt->name='%s'\n", (int) ch, code_point_string(ch), code ? code->port[0] : -1, (code && code->ipt[0]) ? code->ipt[0]->name : "<null>"); |
| 872 | 855 | } |
| 873 | 856 | |
| 874 | 857 | if (can_post_key_directly(ch)) |
| r2483 | r2484 | |
| 1475 | 1458 | } |
| 1476 | 1459 | return FALSE; |
| 1477 | 1460 | } |
| 1461 | |
| 1462 | |
| 1463 | |
| 1464 | /*************************************************************************** |
| 1465 | DEBUGGER SUPPORT |
| 1466 | ***************************************************************************/ |
| 1467 | |
| 1468 | /*------------------------------------------------- |
| 1469 | execute_input - debugger command to enter |
| 1470 | natural keyboard input |
| 1471 | -------------------------------------------------*/ |
| 1472 | |
| 1473 | #ifdef ENABLE_DEBUGGER |
| 1474 | static void execute_input(int ref, int params, const char *param[]) |
| 1475 | { |
| 1476 | inputx_post_coded(param[0]); |
| 1477 | } |
| 1478 | #endif /* ENABLE_DEBUGGER */ |
| 1479 | |
| 1480 | |
| 1481 | |
| 1482 | /*------------------------------------------------- |
| 1483 | execute_dumpkbd - debugger command to natural |
| 1484 | keyboard codes |
| 1485 | -------------------------------------------------*/ |
| 1486 | |
| 1487 | #ifdef ENABLE_DEBUGGER |
| 1488 | static void execute_dumpkbd(int ref, int params, const char *param[]) |
| 1489 | { |
| 1490 | const char *filename; |
| 1491 | FILE *file = NULL; |
| 1492 | const mess_input_code *code; |
| 1493 | char buffer[512]; |
| 1494 | size_t pos; |
| 1495 | int i, j; |
| 1496 | size_t left_column_width = 24; |
| 1497 | |
| 1498 | /* was there a file specified? */ |
| 1499 | filename = (params > 0) ? param[0] : NULL; |
| 1500 | if (filename != NULL) |
| 1501 | { |
| 1502 | /* if so, open it */ |
| 1503 | file = fopen(filename, "w"); |
| 1504 | if (file == NULL) |
| 1505 | { |
| 1506 | debug_console_printf("Cannot open \"%s\"\n", filename); |
| 1507 | return; |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | if ((codes != NULL) && (codes[0].ch != 0)) |
| 1512 | { |
| 1513 | /* loop through all codes */ |
| 1514 | for (i = 0; codes[i].ch; i++) |
| 1515 | { |
| 1516 | code = &codes[i]; |
| 1517 | pos = 0; |
| 1518 | |
| 1519 | /* describe the character code */ |
| 1520 | pos += snprintf(&buffer[pos], ARRAY_LENGTH(buffer) - pos, "%08X (%s) ", |
| 1521 | code->ch, |
| 1522 | code_point_string(code->ch)); |
| 1523 | |
| 1524 | /* pad with spaces */ |
| 1525 | while(pos < left_column_width) |
| 1526 | buffer[pos++] = ' '; |
| 1527 | buffer[pos] = '\0'; |
| 1528 | |
| 1529 | /* identify the keys used */ |
| 1530 | for (j = 0; j < ARRAY_LENGTH(code->ipt) && (code->ipt[j] != NULL); j++) |
| 1531 | { |
| 1532 | pos += snprintf(&buffer[pos], ARRAY_LENGTH(buffer) - pos, "%s'%s'", |
| 1533 | (j > 0) ? ", " : "", |
| 1534 | code->ipt[j]->name); |
| 1535 | } |
| 1536 | |
| 1537 | /* and output it as appropriate */ |
| 1538 | if (file != NULL) |
| 1539 | fprintf(file, "%s\n", buffer); |
| 1540 | else |
| 1541 | debug_console_printf("%s\n", buffer); |
| 1542 | } |
| 1543 | } |
| 1544 | else |
| 1545 | { |
| 1546 | debug_console_printf("No natural keyboard support\n"); |
| 1547 | } |
| 1548 | |
| 1549 | /* cleanup */ |
| 1550 | if (file != NULL) |
| 1551 | fclose(file); |
| 1552 | |
| 1553 | } |
| 1554 | #endif /* ENABLE_DEBUGGER */ |