showkey: code shrink
[platform/upstream/busybox.git] / console-tools / showkey.c
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  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9
10 #include "libbb.h"
11 #include <linux/kd.h>
12
13 // set raw tty mode
14 // also used by microcom
15 // libbb candidates?
16 static void xget1(struct termios *t, struct termios *oldt)
17 {
18         tcgetattr(STDIN_FILENO, oldt);
19         *t = *oldt;
20         cfmakeraw(t);
21 }
22
23 static void xset1(struct termios *tio)
24 {
25         int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, tio);
26         if (ret) {
27                 bb_perror_msg("can't tcsetattr for stdin");
28         }
29 }
30
31 /*
32  * GLOBALS
33  */
34 struct globals {
35         int kbmode;
36         struct termios tio, tio0;
37 };
38 #define G (*ptr_to_globals)
39 #define kbmode  (G.kbmode)
40 #define tio     (G.tio)
41 #define tio0    (G.tio0)
42 #define INIT_G() do { \
43         SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
44 } while (0)
45
46
47 static void signal_handler(int signo)
48 {
49         // restore keyboard and console settings
50         xset1(&tio0);
51         xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
52         // alarmed? -> exit 0
53         exit(SIGALRM == signo);
54 }
55
56 int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
57 int showkey_main(int argc UNUSED_PARAM, char **argv)
58 {
59         enum {
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
63         };
64
65         // FIXME: aks are all mutually exclusive
66         getopt32(argv, "aks");
67
68         INIT_G();
69
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"
78         );
79         // prepare for raw mode
80         xget1(&tio, &tio0);
81         // put stdin in raw mode
82         xset1(&tio);
83
84         if (option_mask32 & OPT_a) {
85                 unsigned char c;
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)
90                                 break;
91                 }
92         } else {
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));
97
98                 // read and show scancodes
99                 while (1) {
100                         char buf[18];
101                         int i, n;
102                         // setup 10s watchdog
103                         alarm(10);
104                         // read scancodes
105                         n = read(STDIN_FILENO, buf, sizeof(buf));
106                         i = 0;
107                         while (i < n) {
108                                 char c = buf[i];
109                                 // show raw scancodes ordered? ->
110                                 if (option_mask32 & OPT_s) {
111                                         printf("0x%02x ", buf[i++]);
112                                 // show interpreted scancodes (default) ? ->
113                                 } else {
114                                         int kc;
115                                         if (i+2 < n
116                                          && (c & 0x7f) == 0
117                                          && (buf[i+1] & 0x80) != 0
118                                          && (buf[i+2] & 0x80) != 0
119                                         ) {
120                                                 kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
121                                                 i += 3;
122                                         } else {
123                                                 kc = (c & 0x7f);
124                                                 i++;
125                                         }
126                                         printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
127                                 }
128                         }
129                         puts("\r");
130                 }
131         }
132
133         // cleanup
134         signal_handler(SIGALRM);
135
136         // should never be here!
137         return EXIT_SUCCESS;
138 }