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 //config:config SHOWKEY
10 //config:	bool "showkey (4.7 kb)"
11 //config:	default y
12 //config:	help
13 //config:	Shows keys pressed.
14 
15 //applet:IF_SHOWKEY(APPLET(showkey, BB_DIR_USR_BIN, BB_SUID_DROP))
16 
17 //kbuild:lib-$(CONFIG_SHOWKEY) += showkey.o
18 
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"
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 
27 #include "libbb.h"
28 #include <linux/kd.h>
29 
30 
31 struct globals {
32 	int kbmode;
33 	struct termios tio, tio0;
34 };
35 #define G (*ptr_to_globals)
36 #define kbmode (G.kbmode)
37 #define tio    (G.tio)
38 #define tio0   (G.tio0)
39 #define INIT_G() do { \
40 	SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
41 } while (0)
42 
43 
44 // set raw tty mode
45 // also used by microcom
46 // libbb candidates?
xget1(struct termios * t,struct termios * oldt)47 static void xget1(struct termios *t, struct termios *oldt)
48 {
49 	tcgetattr(STDIN_FILENO, oldt);
50 	*t = *oldt;
51 	cfmakeraw(t);
52 }
53 
xset1(struct termios * t)54 static void xset1(struct termios *t)
55 {
56 	int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
57 	if (ret) {
58 		bb_simple_perror_msg("can't tcsetattr for stdin");
59 	}
60 }
61 
62 int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
showkey_main(int argc UNUSED_PARAM,char ** argv)63 int showkey_main(int argc UNUSED_PARAM, char **argv)
64 {
65 	enum {
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
69 	};
70 
71 	INIT_G();
72 
73 	// FIXME: aks are all mutually exclusive
74 	getopt32(argv, "aks");
75 
76 	// prepare for raw mode
77 	xget1(&tio, &tio0);
78 	// put stdin in raw mode
79 	xset1(&tio);
80 
81 #define press_keys "Press any keys, program terminates %s:\r\n\n"
82 
83 	if (option_mask32 & OPT_a) {
84 		// just read stdin char by char
85 		unsigned char c;
86 
87 		printf(press_keys, "on EOF (ctrl-D)");
88 
89 		// read and show byte values
90 		while (1 == read(STDIN_FILENO, &c, 1)) {
91 			printf("%3u 0%03o 0x%02x\r\n", c, c, c);
92 			if (04 /*CTRL-D*/ == c)
93 				break;
94 		}
95 	} else {
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 
105 		// set raw keyboard mode
106 		xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
107 
108 		// we should exit on any signal; signals should interrupt read
109 		bb_signals_norestart(BB_FATAL_SIGS, record_signo);
110 
111 		// inform user that program ends after time of inactivity
112 		printf(press_keys, "10s after last keypress");
113 
114 		// read and show scancodes
115 		while (!bb_got_signal) {
116 			char buf[18];
117 			int i, n;
118 
119 			// setup 10s watchdog
120 			alarm(10);
121 
122 			// read scancodes
123 			n = read(STDIN_FILENO, buf, sizeof(buf));
124 			i = 0;
125 			while (i < n) {
126 				if (option_mask32 & OPT_s) {
127 					// show raw scancodes
128 					printf("0x%02x ", buf[i++]);
129 				} else {
130 					// show interpreted scancodes (default)
131 					char c = buf[i];
132 					int kc;
133 					if (i+2 < n
134 					 && (c & 0x7f) == 0
135 					 && (buf[i+1] & 0x80) != 0
136 					 && (buf[i+2] & 0x80) != 0
137 					) {
138 						kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
139 						i += 3;
140 					} else {
141 						kc = (c & 0x7f);
142 						i++;
143 					}
144 					printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
145 				}
146 			}
147 			puts("\r");
148 		}
149 
150 		// restore keyboard mode
151 		xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
152 	}
153 
154 	// restore console settings
155 	xset1(&tio0);
156 
157 	return EXIT_SUCCESS;
158 }
159