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 tarball for details.
14
// also used by microcom
16
static void xget1(int fd, struct termios *t, struct termios *oldt)
23
static int xset1(int fd, struct termios *tio, const char *device)
25
int ret = tcsetattr(fd, TCSAFLUSH, tio);
28
bb_perror_msg("can't tcsetattr for %s", device);
38
struct termios tio, tio0;
40
#define G (*ptr_to_globals)
41
#define kbmode (G.kbmode)
44
#define INIT_G() do { \
45
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
49
static void signal_handler(int signo)
51
// restore keyboard and console settings
52
xset1(STDIN_FILENO, &tio0, "stdin");
53
xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
55
exit(SIGALRM == signo);
58
int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
59
int showkey_main(int argc UNUSED_PARAM, char **argv)
62
OPT_a = (1<<0), // display the decimal/octal/hex values of the keys
63
OPT_k = (1<<1), // display only the interpreted keycodes (default)
64
OPT_s = (1<<2), // display only the raw scan-codes
67
// FIXME: aks are all mutually exclusive
68
getopt32(argv, "aks");
72
// get keyboard settings
73
xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
74
printf("kb mode was %s\n\nPress any keys. Program terminates %s\n\n",
75
kbmode == K_RAW ? "RAW" :
76
(kbmode == K_XLATE ? "XLATE" :
77
(kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
78
(kbmode == K_UNICODE ? "UNICODE" : "?UNKNOWN?")))
79
, (option_mask32 & OPT_a) ? "when CTRL+D pressed" : "10s after last keypress"
81
// prepare for raw mode
82
xget1(STDIN_FILENO, &tio, &tio0);
83
// put stdin in raw mode
84
xset1(STDIN_FILENO, &tio, "stdin");
86
if (option_mask32 & OPT_a) {
88
// just read stdin char by char
89
while (1 == safe_read(STDIN_FILENO, &c, 1)) {
90
printf("%3d 0%03o 0x%02x\r\n", c, c, c);
91
if (04 /*CTRL-D*/ == c)
95
// we should exit on any signal
96
bb_signals(BB_FATAL_SIGS, signal_handler);
97
// set raw keyboard mode
98
xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
100
// read and show scancodes
104
// setup 10s watchdog
107
n = read(STDIN_FILENO, buf, sizeof(buf));
111
// show raw scancodes ordered? ->
112
if (option_mask32 & OPT_s) {
113
printf("0x%02x ", buf[i++]);
114
// show interpreted scancodes (default) ? ->
117
if (i+2 < n && (c & 0x7f) == 0
118
&& (buf[i+1] & 0x80) != 0
119
&& (buf[i+2] & 0x80) != 0) {
120
kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
126
printf("keycode %3d %s", kc, (c & 0x80) ? "release" : "press");
134
signal_handler(SIGALRM);
136
// should never be here!