blob: 84eb38a0a7749a77b53a30d29b7499225ae4dea8 [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
Denys Vlasenko6d932992016-11-23 10:39:27 +010012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: Shows keys pressed.
Denys Vlasenko6d932992016-11-23 10:39:27 +010014
15//applet:IF_SHOWKEY(APPLET(showkey, BB_DIR_USR_BIN, BB_SUID_DROP))
16
17//kbuild:lib-$(CONFIG_SHOWKEY) += showkey.o
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000018
Pere Orga55068c42011-03-27 23:42:28 +020019//usage:#define showkey_trivial_usage
20//usage: "[-a | -k | -s]"
21//usage:#define showkey_full_usage "\n\n"
22//usage: "Show keys pressed\n"
Pere Orga55068c42011-03-27 23:42:28 +020023//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
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000027#include "libbb.h"
28#include <linux/kd.h>
29
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000030
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000031struct globals {
32 int kbmode;
33 struct termios tio, tio0;
34};
35#define G (*ptr_to_globals)
Denys Vlasenkofb132e42010-10-29 11:46:52 +020036#define kbmode (G.kbmode)
37#define tio (G.tio)
38#define tio0 (G.tio0)
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000039#define INIT_G() do { \
40 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
41} while (0)
42
43
Marek Polacek7b181072010-10-28 21:34:56 +020044// set raw tty mode
45// also used by microcom
46// libbb candidates?
47static void xget1(struct termios *t, struct termios *oldt)
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000048{
Marek Polacek7b181072010-10-28 21:34:56 +020049 tcgetattr(STDIN_FILENO, oldt);
50 *t = *oldt;
51 cfmakeraw(t);
52}
53
54static void xset1(struct termios *t)
55{
56 int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
57 if (ret) {
James Byrne69374872019-07-02 11:35:03 +020058 bb_simple_perror_msg("can't tcsetattr for stdin");
Marek Polacek7b181072010-10-28 21:34:56 +020059 }
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000060}
61
62int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
63int showkey_main(int argc UNUSED_PARAM, char **argv)
64{
65 enum {
Denys Vlasenkofb132e42010-10-29 11:46:52 +020066 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
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000069 };
70
Marek Polacek7b181072010-10-28 21:34:56 +020071 INIT_G();
72
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000073 // FIXME: aks are all mutually exclusive
74 getopt32(argv, "aks");
75
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000076 // prepare for raw mode
Denys Vlasenkocc131532010-08-29 05:00:40 +020077 xget1(&tio, &tio0);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000078 // put stdin in raw mode
Denys Vlasenkocc131532010-08-29 05:00:40 +020079 xset1(&tio);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000080
Harald Becker9beb68e2011-02-27 07:16:44 +010081#define press_keys "Press any keys, program terminates %s:\r\n\n"
82
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000083 if (option_mask32 & OPT_a) {
Harald Becker9beb68e2011-02-27 07:16:44 +010084 // just read stdin char by char
Denys Vlasenkocc131532010-08-29 05:00:40 +020085 unsigned char c;
Marek Polacek7b181072010-10-28 21:34:56 +020086
Harald Becker9beb68e2011-02-27 07:16:44 +010087 printf(press_keys, "on EOF (ctrl-D)");
88
89 // read and show byte values
Marek Polacek7b181072010-10-28 21:34:56 +020090 while (1 == read(STDIN_FILENO, &c, 1)) {
Denys Vlasenkocc131532010-08-29 05:00:40 +020091 printf("%3u 0%03o 0x%02x\r\n", c, c, c);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000092 if (04 /*CTRL-D*/ == c)
93 break;
94 }
95 } else {
Harald Becker9beb68e2011-02-27 07:16:44 +010096 // 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
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000105 // set raw keyboard mode
Denis Vlasenko651d49a2008-09-28 16:44:28 +0000106 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000107
Marek Polacek7b181072010-10-28 21:34:56 +0200108 // we should exit on any signal; signals should interrupt read
Denys Vlasenko5dadd492021-06-05 16:20:05 +0200109 bb_signals_norestart(BB_FATAL_SIGS, record_signo);
Marek Polacek7b181072010-10-28 21:34:56 +0200110
Harald Becker9beb68e2011-02-27 07:16:44 +0100111 // inform user that program ends after time of inactivity
112 printf(press_keys, "10s after last keypress");
113
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000114 // read and show scancodes
Marek Polacek7b181072010-10-28 21:34:56 +0200115 while (!bb_got_signal) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000116 char buf[18];
117 int i, n;
Marek Polacek7b181072010-10-28 21:34:56 +0200118
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000119 // setup 10s watchdog
120 alarm(10);
Harald Becker9beb68e2011-02-27 07:16:44 +0100121
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000122 // read scancodes
123 n = read(STDIN_FILENO, buf, sizeof(buf));
124 i = 0;
125 while (i < n) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000126 if (option_mask32 & OPT_s) {
Marek Polacek7b181072010-10-28 21:34:56 +0200127 // show raw scancodes
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000128 printf("0x%02x ", buf[i++]);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000129 } else {
Marek Polacek7b181072010-10-28 21:34:56 +0200130 // show interpreted scancodes (default)
131 char c = buf[i];
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000132 int kc;
Denys Vlasenkocc131532010-08-29 05:00:40 +0200133 if (i+2 < n
134 && (c & 0x7f) == 0
135 && (buf[i+1] & 0x80) != 0
136 && (buf[i+2] & 0x80) != 0
137 ) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000138 kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
139 i += 3;
140 } else {
141 kc = (c & 0x7f);
142 i++;
143 }
Denys Vlasenkocc131532010-08-29 05:00:40 +0200144 printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000145 }
146 }
147 puts("\r");
148 }
Harald Becker9beb68e2011-02-27 07:16:44 +0100149
150 // restore keyboard mode
151 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000152 }
153
Harald Becker9beb68e2011-02-27 07:16:44 +0100154 // restore console settings
Marek Polacek7b181072010-10-28 21:34:56 +0200155 xset1(&tio0);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000156
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000157 return EXIT_SUCCESS;
158}