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