diff options
| author | Harald Becker <ralda@gmx.de> | 2011-02-27 07:16:44 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-02-27 07:16:44 +0100 |
| commit | 9beb68e3e2dc1c6f457acfb307cfed73cce65cd9 (patch) | |
| tree | 6b4582a535d8a226c3ee19596deaee398ff47c08 | |
| parent | cd387f2554c62c7716970e90af54b8bde6c86c86 (diff) | |
| download | busybox-w32-9beb68e3e2dc1c6f457acfb307cfed73cce65cd9.tar.gz busybox-w32-9beb68e3e2dc1c6f457acfb307cfed73cce65cd9.tar.bz2 busybox-w32-9beb68e3e2dc1c6f457acfb307cfed73cce65cd9.zip | |
showkey: make showkey -a work on any stdin
function old new delta
showkey_main 496 513 +17
Signed-off-by: Harald Becker <ralda@gmx.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | console-tools/showkey.c | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/console-tools/showkey.c b/console-tools/showkey.c index e7834f702..06df68bfd 100644 --- a/console-tools/showkey.c +++ b/console-tools/showkey.c | |||
| @@ -56,37 +56,45 @@ int showkey_main(int argc UNUSED_PARAM, char **argv) | |||
| 56 | // FIXME: aks are all mutually exclusive | 56 | // FIXME: aks are all mutually exclusive |
| 57 | getopt32(argv, "aks"); | 57 | getopt32(argv, "aks"); |
| 58 | 58 | ||
| 59 | // get keyboard settings | ||
| 60 | xioctl(STDIN_FILENO, KDGKBMODE, &kbmode); | ||
| 61 | printf("kb mode was %s\n\nPress any keys. Program terminates %s\n\n", | ||
| 62 | kbmode == K_RAW ? "RAW" : | ||
| 63 | (kbmode == K_XLATE ? "XLATE" : | ||
| 64 | (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" : | ||
| 65 | (kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN"))) | ||
| 66 | , (option_mask32 & OPT_a) ? "on EOF (ctrl-D)" : "10s after last keypress" | ||
| 67 | ); | ||
| 68 | |||
| 69 | // prepare for raw mode | 59 | // prepare for raw mode |
| 70 | xget1(&tio, &tio0); | 60 | xget1(&tio, &tio0); |
| 71 | // put stdin in raw mode | 61 | // put stdin in raw mode |
| 72 | xset1(&tio); | 62 | xset1(&tio); |
| 73 | 63 | ||
| 64 | #define press_keys "Press any keys, program terminates %s:\r\n\n" | ||
| 65 | |||
| 74 | if (option_mask32 & OPT_a) { | 66 | if (option_mask32 & OPT_a) { |
| 67 | // just read stdin char by char | ||
| 75 | unsigned char c; | 68 | unsigned char c; |
| 76 | 69 | ||
| 77 | // just read stdin char by char | 70 | printf(press_keys, "on EOF (ctrl-D)"); |
| 71 | |||
| 72 | // read and show byte values | ||
| 78 | while (1 == read(STDIN_FILENO, &c, 1)) { | 73 | while (1 == read(STDIN_FILENO, &c, 1)) { |
| 79 | printf("%3u 0%03o 0x%02x\r\n", c, c, c); | 74 | printf("%3u 0%03o 0x%02x\r\n", c, c, c); |
| 80 | if (04 /*CTRL-D*/ == c) | 75 | if (04 /*CTRL-D*/ == c) |
| 81 | break; | 76 | break; |
| 82 | } | 77 | } |
| 78 | |||
| 83 | } else { | 79 | } else { |
| 80 | // we assume a PC keyboard | ||
| 81 | xioctl(STDIN_FILENO, KDGKBMODE, &kbmode); | ||
| 82 | printf("Keyboard mode was %s.\r\n\n", | ||
| 83 | kbmode == K_RAW ? "RAW" : | ||
| 84 | (kbmode == K_XLATE ? "XLATE" : | ||
| 85 | (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" : | ||
| 86 | (kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN"))) | ||
| 87 | ); | ||
| 88 | |||
| 84 | // set raw keyboard mode | 89 | // set raw keyboard mode |
| 85 | xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW)); | 90 | xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW)); |
| 86 | 91 | ||
| 87 | // we should exit on any signal; signals should interrupt read | 92 | // we should exit on any signal; signals should interrupt read |
| 88 | bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo); | 93 | bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo); |
| 89 | 94 | ||
| 95 | // inform user that program ends after time of inactivity | ||
| 96 | printf(press_keys, "10s after last keypress"); | ||
| 97 | |||
| 90 | // read and show scancodes | 98 | // read and show scancodes |
| 91 | while (!bb_got_signal) { | 99 | while (!bb_got_signal) { |
| 92 | char buf[18]; | 100 | char buf[18]; |
| @@ -94,6 +102,7 @@ int showkey_main(int argc UNUSED_PARAM, char **argv) | |||
| 94 | 102 | ||
| 95 | // setup 10s watchdog | 103 | // setup 10s watchdog |
| 96 | alarm(10); | 104 | alarm(10); |
| 105 | |||
| 97 | // read scancodes | 106 | // read scancodes |
| 98 | n = read(STDIN_FILENO, buf, sizeof(buf)); | 107 | n = read(STDIN_FILENO, buf, sizeof(buf)); |
| 99 | i = 0; | 108 | i = 0; |
| @@ -121,11 +130,13 @@ int showkey_main(int argc UNUSED_PARAM, char **argv) | |||
| 121 | } | 130 | } |
| 122 | puts("\r"); | 131 | puts("\r"); |
| 123 | } | 132 | } |
| 133 | |||
| 134 | // restore keyboard mode | ||
| 135 | xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode); | ||
| 124 | } | 136 | } |
| 125 | 137 | ||
| 126 | // restore keyboard and console settings | 138 | // restore console settings |
| 127 | xset1(&tio0); | 139 | xset1(&tio0); |
| 128 | xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode); | ||
| 129 | 140 | ||
| 130 | return EXIT_SUCCESS; | 141 | return EXIT_SUCCESS; |
| 131 | } | 142 | } |
