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 | |
| 10 | #include "libbb.h" |
| 11 | #include <linux/kd.h> |
| 12 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 14 | struct globals { |
| 15 | int kbmode; |
| 16 | struct termios tio, tio0; |
| 17 | }; |
| 18 | #define G (*ptr_to_globals) |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 19 | #define kbmode (G.kbmode) |
| 20 | #define tio (G.tio) |
| 21 | #define tio0 (G.tio0) |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 22 | #define INIT_G() do { \ |
| 23 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 24 | } while (0) |
| 25 | |
| 26 | |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 27 | // set raw tty mode |
| 28 | // also used by microcom |
| 29 | // libbb candidates? |
| 30 | static void xget1(struct termios *t, struct termios *oldt) |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 31 | { |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 32 | tcgetattr(STDIN_FILENO, oldt); |
| 33 | *t = *oldt; |
| 34 | cfmakeraw(t); |
| 35 | } |
| 36 | |
| 37 | static void xset1(struct termios *t) |
| 38 | { |
| 39 | int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t); |
| 40 | if (ret) { |
| 41 | bb_perror_msg("can't tcsetattr for stdin"); |
| 42 | } |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 46 | int showkey_main(int argc UNUSED_PARAM, char **argv) |
| 47 | { |
| 48 | enum { |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 49 | OPT_a = (1<<0), // display the decimal/octal/hex values of the keys |
| 50 | OPT_k = (1<<1), // display only the interpreted keycodes (default) |
| 51 | OPT_s = (1<<2), // display only the raw scan-codes |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 54 | INIT_G(); |
| 55 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 56 | // FIXME: aks are all mutually exclusive |
| 57 | getopt32(argv, "aks"); |
| 58 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 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", |
Denis Vlasenko | ea7c9b3 | 2008-09-25 10:39:10 +0000 | [diff] [blame] | 62 | kbmode == K_RAW ? "RAW" : |
| 63 | (kbmode == K_XLATE ? "XLATE" : |
| 64 | (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" : |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 65 | (kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN"))) |
| 66 | , (option_mask32 & OPT_a) ? "on EOF (ctrl-D)" : "10s after last keypress" |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 67 | ); |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 68 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 69 | // prepare for raw mode |
Denys Vlasenko | cc13153 | 2010-08-29 05:00:40 +0200 | [diff] [blame] | 70 | xget1(&tio, &tio0); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 71 | // put stdin in raw mode |
Denys Vlasenko | cc13153 | 2010-08-29 05:00:40 +0200 | [diff] [blame] | 72 | xset1(&tio); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 73 | |
| 74 | if (option_mask32 & OPT_a) { |
Denys Vlasenko | cc13153 | 2010-08-29 05:00:40 +0200 | [diff] [blame] | 75 | unsigned char c; |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 76 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 77 | // just read stdin char by char |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 78 | while (1 == read(STDIN_FILENO, &c, 1)) { |
Denys Vlasenko | cc13153 | 2010-08-29 05:00:40 +0200 | [diff] [blame] | 79 | printf("%3u 0%03o 0x%02x\r\n", c, c, c); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 80 | if (04 /*CTRL-D*/ == c) |
| 81 | break; |
| 82 | } |
| 83 | } else { |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 84 | // set raw keyboard mode |
Denis Vlasenko | 651d49a | 2008-09-28 16:44:28 +0000 | [diff] [blame] | 85 | 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] | 86 | |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 87 | // we should exit on any signal; signals should interrupt read |
| 88 | bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo); |
| 89 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 90 | // read and show scancodes |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 91 | while (!bb_got_signal) { |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 92 | char buf[18]; |
| 93 | int i, n; |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 94 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 95 | // setup 10s watchdog |
| 96 | alarm(10); |
| 97 | // read scancodes |
| 98 | n = read(STDIN_FILENO, buf, sizeof(buf)); |
| 99 | i = 0; |
| 100 | while (i < n) { |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 101 | if (option_mask32 & OPT_s) { |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 102 | // show raw scancodes |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 103 | printf("0x%02x ", buf[i++]); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 104 | } else { |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 105 | // show interpreted scancodes (default) |
| 106 | char c = buf[i]; |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 107 | int kc; |
Denys Vlasenko | cc13153 | 2010-08-29 05:00:40 +0200 | [diff] [blame] | 108 | if (i+2 < n |
| 109 | && (c & 0x7f) == 0 |
| 110 | && (buf[i+1] & 0x80) != 0 |
| 111 | && (buf[i+2] & 0x80) != 0 |
| 112 | ) { |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 113 | kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f); |
| 114 | i += 3; |
| 115 | } else { |
| 116 | kc = (c & 0x7f); |
| 117 | i++; |
| 118 | } |
Denys Vlasenko | cc13153 | 2010-08-29 05:00:40 +0200 | [diff] [blame] | 119 | printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press"); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | puts("\r"); |
| 123 | } |
| 124 | } |
| 125 | |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 126 | // restore keyboard and console settings |
| 127 | xset1(&tio0); |
| 128 | xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode); |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 129 | |
Denis Vlasenko | d7cc2e6 | 2008-08-17 14:17:48 +0000 | [diff] [blame] | 130 | return EXIT_SUCCESS; |
| 131 | } |