1 /* vi: set sw=4 ts=4: */
3 * shows keys pressed. inspired by kbd package
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
14 // also used by microcom
16 static void xget1(struct termios *t, struct termios *oldt)
18 tcgetattr(STDIN_FILENO, oldt);
23 static void xset1(struct termios *tio)
25 int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, tio);
27 bb_perror_msg("can't tcsetattr for stdin");
36 struct termios tio, tio0;
38 #define G (*ptr_to_globals)
39 #define kbmode (G.kbmode)
42 #define INIT_G() do { \
43 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
47 static void signal_handler(int signo)
49 // restore keyboard and console settings
51 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
53 exit(SIGALRM == signo);
56 int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
57 int showkey_main(int argc UNUSED_PARAM, char **argv)
60 OPT_a = (1<<0), // display the decimal/octal/hex values of the keys
61 OPT_k = (1<<1), // display only the interpreted keycodes (default)
62 OPT_s = (1<<2), // display only the raw scan-codes
65 // FIXME: aks are all mutually exclusive
66 getopt32(argv, "aks");
70 // get keyboard settings
71 xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
72 printf("kb mode was %s\n\nPress any keys. Program terminates %s\n\n",
73 kbmode == K_RAW ? "RAW" :
74 (kbmode == K_XLATE ? "XLATE" :
75 (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
76 (kbmode == K_UNICODE ? "UNICODE" : "?UNKNOWN?")))
77 , (option_mask32 & OPT_a) ? "when CTRL+D pressed" : "10s after last keypress"
79 // prepare for raw mode
81 // put stdin in raw mode
84 if (option_mask32 & OPT_a) {
86 // just read stdin char by char
87 while (1 == safe_read(STDIN_FILENO, &c, 1)) {
88 printf("%3u 0%03o 0x%02x\r\n", c, c, c);
89 if (04 /*CTRL-D*/ == c)
93 // we should exit on any signal
94 bb_signals(BB_FATAL_SIGS, signal_handler);
95 // set raw keyboard mode
96 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
98 // read and show scancodes
102 // setup 10s watchdog
105 n = read(STDIN_FILENO, buf, sizeof(buf));
109 // show raw scancodes ordered? ->
110 if (option_mask32 & OPT_s) {
111 printf("0x%02x ", buf[i++]);
112 // show interpreted scancodes (default) ? ->
117 && (buf[i+1] & 0x80) != 0
118 && (buf[i+2] & 0x80) != 0
120 kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
126 printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
134 signal_handler(SIGALRM);
136 // should never be here!