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