Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * shows keys pressed. inspired by kbd package |
| 4 | * |
| 5 | * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Pere Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 10 | //usage:#define showkey_trivial_usage |
| 11 | //usage: "[-a | -k | -s]" |
| 12 | //usage:#define showkey_full_usage "\n\n" |
| 13 | //usage: "Show keys pressed\n" |
Pere Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 14 | //usage: "\n -a Display decimal/octal/hex values of the keys" |
| 15 | //usage: "\n -k Display interpreted keycodes (default)" |
| 16 | //usage: "\n -s Display raw scan-codes" |
| 17 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
| 19 | #include <linux/kd.h> |
| 20 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 22 | struct globals { |
| 23 | int kbmode; |
| 24 | struct termios tio, tio0; |
| 25 | }; |
| 26 | #define G (*ptr_to_globals) |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 27 | #define kbmode (G.kbmode) |
| 28 | #define tio (G.tio) |
| 29 | #define tio0 (G.tio0) |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 30 | #define INIT_G() do { \ |
| 31 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 32 | } while (0) |
| 33 | |
| 34 | |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 35 | // set raw tty mode |
| 36 | // also used by microcom |
| 37 | // libbb candidates? |
| 38 | static void xget1(struct termios *t, struct termios *oldt) |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 39 | { |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 40 | tcgetattr(STDIN_FILENO, oldt); |
| 41 | *t = *oldt; |
| 42 | cfmakeraw(t); |
| 43 | } |
| 44 | |
| 45 | static void xset1(struct termios *t) |
| 46 | { |
| 47 | int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t); |
| 48 | if (ret) { |
| 49 | bb_perror_msg("can't tcsetattr for stdin"); |
| 50 | } |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 54 | int showkey_main(int argc UNUSED_PARAM, char **argv) |
| 55 | { |
| 56 | enum { |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 57 | OPT_a = (1<<0), // display the decimal/octal/hex values of the keys |
| 58 | OPT_k = (1<<1), // display only the interpreted keycodes (default) |
| 59 | OPT_s = (1<<2), // display only the raw scan-codes |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 62 | INIT_G(); |
| 63 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 64 | // FIXME: aks are all mutually exclusive |
| 65 | getopt32(argv, "aks"); |
| 66 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 67 | // prepare for raw mode |
Denys Vlasenko | cc13153 | 2010-08-29 05:00:40 +0200 | [diff] [blame] | 68 | xget1(&tio, &tio0); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 69 | // put stdin in raw mode |
Denys Vlasenko | cc13153 | 2010-08-29 05:00:40 +0200 | [diff] [blame] | 70 | xset1(&tio); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 71 | |
Harald Becker | 9beb68e | 2011-02-27 07:16:44 +0100 | [diff] [blame] | 72 | #define press_keys "Press any keys, program terminates %s:\r\n\n" |
| 73 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 74 | if (option_mask32 & OPT_a) { |
Harald Becker | 9beb68e | 2011-02-27 07:16:44 +0100 | [diff] [blame] | 75 | // just read stdin char by char |
Denys Vlasenko | cc13153 | 2010-08-29 05:00:40 +0200 | [diff] [blame] | 76 | unsigned char c; |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 77 | |
Harald Becker | 9beb68e | 2011-02-27 07:16:44 +0100 | [diff] [blame] | 78 | printf(press_keys, "on EOF (ctrl-D)"); |
| 79 | |
| 80 | // read and show byte values |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 81 | while (1 == read(STDIN_FILENO, &c, 1)) { |
Denys Vlasenko | cc13153 | 2010-08-29 05:00:40 +0200 | [diff] [blame] | 82 | printf("%3u 0%03o 0x%02x\r\n", c, c, c); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 83 | if (04 /*CTRL-D*/ == c) |
| 84 | break; |
| 85 | } |
Harald Becker | 9beb68e | 2011-02-27 07:16:44 +0100 | [diff] [blame] | 86 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 87 | } else { |
Harald Becker | 9beb68e | 2011-02-27 07:16:44 +0100 | [diff] [blame] | 88 | // we assume a PC keyboard |
| 89 | xioctl(STDIN_FILENO, KDGKBMODE, &kbmode); |
| 90 | printf("Keyboard mode was %s.\r\n\n", |
| 91 | kbmode == K_RAW ? "RAW" : |
| 92 | (kbmode == K_XLATE ? "XLATE" : |
| 93 | (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" : |
| 94 | (kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN"))) |
| 95 | ); |
| 96 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 97 | // set raw keyboard mode |
Denis Vlasenko | 651d49a | 2008-09-28 16:44:28 +0000 | [diff] [blame] | 98 | xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW)); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 99 | |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 100 | // we should exit on any signal; signals should interrupt read |
| 101 | bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo); |
| 102 | |
Harald Becker | 9beb68e | 2011-02-27 07:16:44 +0100 | [diff] [blame] | 103 | // inform user that program ends after time of inactivity |
| 104 | printf(press_keys, "10s after last keypress"); |
| 105 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 106 | // read and show scancodes |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 107 | while (!bb_got_signal) { |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 108 | char buf[18]; |
| 109 | int i, n; |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 110 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 111 | // setup 10s watchdog |
| 112 | alarm(10); |
Harald Becker | 9beb68e | 2011-02-27 07:16:44 +0100 | [diff] [blame] | 113 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 114 | // read scancodes |
| 115 | n = read(STDIN_FILENO, buf, sizeof(buf)); |
| 116 | i = 0; |
| 117 | while (i < n) { |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 118 | if (option_mask32 & OPT_s) { |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 119 | // show raw scancodes |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 120 | printf("0x%02x ", buf[i++]); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 121 | } else { |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 122 | // show interpreted scancodes (default) |
| 123 | char c = buf[i]; |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 124 | int kc; |
Denys Vlasenko | cc13153 | 2010-08-29 05:00:40 +0200 | [diff] [blame] | 125 | if (i+2 < n |
| 126 | && (c & 0x7f) == 0 |
| 127 | && (buf[i+1] & 0x80) != 0 |
| 128 | && (buf[i+2] & 0x80) != 0 |
| 129 | ) { |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 130 | kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f); |
| 131 | i += 3; |
| 132 | } else { |
| 133 | kc = (c & 0x7f); |
| 134 | i++; |
| 135 | } |
Denys Vlasenko | cc13153 | 2010-08-29 05:00:40 +0200 | [diff] [blame] | 136 | printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press"); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | puts("\r"); |
| 140 | } |
Harald Becker | 9beb68e | 2011-02-27 07:16:44 +0100 | [diff] [blame] | 141 | |
| 142 | // restore keyboard mode |
| 143 | xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Harald Becker | 9beb68e | 2011-02-27 07:16:44 +0100 | [diff] [blame] | 146 | // restore console settings |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 147 | xset1(&tio0); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 148 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 149 | return EXIT_SUCCESS; |
| 150 | } |