blob: 8a485245daf90b34f003817a3889d49a8288544f [file] [log] [blame]
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +00001/* 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 Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +00008 */
Denys Vlasenko6d932992016-11-23 10:39:27 +01009//config:config SHOWKEY
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "showkey (4.7 kb)"
Denys Vlasenko6d932992016-11-23 10:39:27 +010011//config: default y
12//config: select PLATFORM_LINUX
13//config: help
14//config: Shows keys pressed.
15
16//applet:IF_SHOWKEY(APPLET(showkey, BB_DIR_USR_BIN, BB_SUID_DROP))
17
18//kbuild:lib-$(CONFIG_SHOWKEY) += showkey.o
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000019
Pere Orga55068c42011-03-27 23:42:28 +020020//usage:#define showkey_trivial_usage
21//usage: "[-a | -k | -s]"
22//usage:#define showkey_full_usage "\n\n"
23//usage: "Show keys pressed\n"
Pere Orga55068c42011-03-27 23:42:28 +020024//usage: "\n -a Display decimal/octal/hex values of the keys"
25//usage: "\n -k Display interpreted keycodes (default)"
26//usage: "\n -s Display raw scan-codes"
27
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000028#include "libbb.h"
29#include <linux/kd.h>
30
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000031
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000032struct globals {
33 int kbmode;
34 struct termios tio, tio0;
35};
36#define G (*ptr_to_globals)
Denys Vlasenkofb132e42010-10-29 11:46:52 +020037#define kbmode (G.kbmode)
38#define tio (G.tio)
39#define tio0 (G.tio0)
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000040#define INIT_G() do { \
41 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
42} while (0)
43
44
Marek Polacek7b181072010-10-28 21:34:56 +020045// set raw tty mode
46// also used by microcom
47// libbb candidates?
48static void xget1(struct termios *t, struct termios *oldt)
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000049{
Marek Polacek7b181072010-10-28 21:34:56 +020050 tcgetattr(STDIN_FILENO, oldt);
51 *t = *oldt;
52 cfmakeraw(t);
53}
54
55static void xset1(struct termios *t)
56{
57 int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
58 if (ret) {
59 bb_perror_msg("can't tcsetattr for stdin");
60 }
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000061}
62
63int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
64int showkey_main(int argc UNUSED_PARAM, char **argv)
65{
66 enum {
Denys Vlasenkofb132e42010-10-29 11:46:52 +020067 OPT_a = (1<<0), // display the decimal/octal/hex values of the keys
68 OPT_k = (1<<1), // display only the interpreted keycodes (default)
69 OPT_s = (1<<2), // display only the raw scan-codes
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000070 };
71
Marek Polacek7b181072010-10-28 21:34:56 +020072 INIT_G();
73
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000074 // FIXME: aks are all mutually exclusive
75 getopt32(argv, "aks");
76
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000077 // prepare for raw mode
Denys Vlasenkocc131532010-08-29 05:00:40 +020078 xget1(&tio, &tio0);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000079 // put stdin in raw mode
Denys Vlasenkocc131532010-08-29 05:00:40 +020080 xset1(&tio);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000081
Harald Becker9beb68e2011-02-27 07:16:44 +010082#define press_keys "Press any keys, program terminates %s:\r\n\n"
83
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000084 if (option_mask32 & OPT_a) {
Harald Becker9beb68e2011-02-27 07:16:44 +010085 // just read stdin char by char
Denys Vlasenkocc131532010-08-29 05:00:40 +020086 unsigned char c;
Marek Polacek7b181072010-10-28 21:34:56 +020087
Harald Becker9beb68e2011-02-27 07:16:44 +010088 printf(press_keys, "on EOF (ctrl-D)");
89
90 // read and show byte values
Marek Polacek7b181072010-10-28 21:34:56 +020091 while (1 == read(STDIN_FILENO, &c, 1)) {
Denys Vlasenkocc131532010-08-29 05:00:40 +020092 printf("%3u 0%03o 0x%02x\r\n", c, c, c);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000093 if (04 /*CTRL-D*/ == c)
94 break;
95 }
96 } else {
Harald Becker9beb68e2011-02-27 07:16:44 +010097 // we assume a PC keyboard
98 xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
99 printf("Keyboard mode was %s.\r\n\n",
100 kbmode == K_RAW ? "RAW" :
101 (kbmode == K_XLATE ? "XLATE" :
102 (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
103 (kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN")))
104 );
105
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000106 // set raw keyboard mode
Denis Vlasenko651d49a2008-09-28 16:44:28 +0000107 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000108
Marek Polacek7b181072010-10-28 21:34:56 +0200109 // we should exit on any signal; signals should interrupt read
110 bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
111
Harald Becker9beb68e2011-02-27 07:16:44 +0100112 // inform user that program ends after time of inactivity
113 printf(press_keys, "10s after last keypress");
114
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000115 // read and show scancodes
Marek Polacek7b181072010-10-28 21:34:56 +0200116 while (!bb_got_signal) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000117 char buf[18];
118 int i, n;
Marek Polacek7b181072010-10-28 21:34:56 +0200119
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000120 // setup 10s watchdog
121 alarm(10);
Harald Becker9beb68e2011-02-27 07:16:44 +0100122
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000123 // read scancodes
124 n = read(STDIN_FILENO, buf, sizeof(buf));
125 i = 0;
126 while (i < n) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000127 if (option_mask32 & OPT_s) {
Marek Polacek7b181072010-10-28 21:34:56 +0200128 // show raw scancodes
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000129 printf("0x%02x ", buf[i++]);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000130 } else {
Marek Polacek7b181072010-10-28 21:34:56 +0200131 // show interpreted scancodes (default)
132 char c = buf[i];
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000133 int kc;
Denys Vlasenkocc131532010-08-29 05:00:40 +0200134 if (i+2 < n
135 && (c & 0x7f) == 0
136 && (buf[i+1] & 0x80) != 0
137 && (buf[i+2] & 0x80) != 0
138 ) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000139 kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
140 i += 3;
141 } else {
142 kc = (c & 0x7f);
143 i++;
144 }
Denys Vlasenkocc131532010-08-29 05:00:40 +0200145 printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000146 }
147 }
148 puts("\r");
149 }
Harald Becker9beb68e2011-02-27 07:16:44 +0100150
151 // restore keyboard mode
152 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000153 }
154
Harald Becker9beb68e2011-02-27 07:16:44 +0100155 // restore console settings
Marek Polacek7b181072010-10-28 21:34:56 +0200156 xset1(&tio0);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000157
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000158 return EXIT_SUCCESS;
159}