blob: 2c832c92c337f3f93bdbca7bdd960d9add131d72 [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 */
9
Pere Orga55068c42011-03-27 23:42:28 +020010//usage:#define showkey_trivial_usage
11//usage: "[-a | -k | -s]"
12//usage:#define showkey_full_usage "\n\n"
13//usage: "Show keys pressed\n"
14//usage: "\nOptions:"
15//usage: "\n -a Display decimal/octal/hex values of the keys"
16//usage: "\n -k Display interpreted keycodes (default)"
17//usage: "\n -s Display raw scan-codes"
18
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000019#include "libbb.h"
20#include <linux/kd.h>
21
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000022
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000023struct globals {
24 int kbmode;
25 struct termios tio, tio0;
26};
27#define G (*ptr_to_globals)
Denys Vlasenkofb132e42010-10-29 11:46:52 +020028#define kbmode (G.kbmode)
29#define tio (G.tio)
30#define tio0 (G.tio0)
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000031#define INIT_G() do { \
32 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
33} while (0)
34
35
Marek Polacek7b181072010-10-28 21:34:56 +020036// set raw tty mode
37// also used by microcom
38// libbb candidates?
39static void xget1(struct termios *t, struct termios *oldt)
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000040{
Marek Polacek7b181072010-10-28 21:34:56 +020041 tcgetattr(STDIN_FILENO, oldt);
42 *t = *oldt;
43 cfmakeraw(t);
44}
45
46static void xset1(struct termios *t)
47{
48 int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
49 if (ret) {
50 bb_perror_msg("can't tcsetattr for stdin");
51 }
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000052}
53
54int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
55int showkey_main(int argc UNUSED_PARAM, char **argv)
56{
57 enum {
Denys Vlasenkofb132e42010-10-29 11:46:52 +020058 OPT_a = (1<<0), // display the decimal/octal/hex values of the keys
59 OPT_k = (1<<1), // display only the interpreted keycodes (default)
60 OPT_s = (1<<2), // display only the raw scan-codes
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000061 };
62
Marek Polacek7b181072010-10-28 21:34:56 +020063 INIT_G();
64
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000065 // FIXME: aks are all mutually exclusive
66 getopt32(argv, "aks");
67
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000068 // prepare for raw mode
Denys Vlasenkocc131532010-08-29 05:00:40 +020069 xget1(&tio, &tio0);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000070 // put stdin in raw mode
Denys Vlasenkocc131532010-08-29 05:00:40 +020071 xset1(&tio);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000072
Harald Becker9beb68e2011-02-27 07:16:44 +010073#define press_keys "Press any keys, program terminates %s:\r\n\n"
74
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000075 if (option_mask32 & OPT_a) {
Harald Becker9beb68e2011-02-27 07:16:44 +010076 // just read stdin char by char
Denys Vlasenkocc131532010-08-29 05:00:40 +020077 unsigned char c;
Marek Polacek7b181072010-10-28 21:34:56 +020078
Harald Becker9beb68e2011-02-27 07:16:44 +010079 printf(press_keys, "on EOF (ctrl-D)");
80
81 // read and show byte values
Marek Polacek7b181072010-10-28 21:34:56 +020082 while (1 == read(STDIN_FILENO, &c, 1)) {
Denys Vlasenkocc131532010-08-29 05:00:40 +020083 printf("%3u 0%03o 0x%02x\r\n", c, c, c);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000084 if (04 /*CTRL-D*/ == c)
85 break;
86 }
Harald Becker9beb68e2011-02-27 07:16:44 +010087
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000088 } else {
Harald Becker9beb68e2011-02-27 07:16:44 +010089 // we assume a PC keyboard
90 xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
91 printf("Keyboard mode was %s.\r\n\n",
92 kbmode == K_RAW ? "RAW" :
93 (kbmode == K_XLATE ? "XLATE" :
94 (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
95 (kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN")))
96 );
97
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000098 // set raw keyboard mode
Denis Vlasenko651d49a2008-09-28 16:44:28 +000099 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000100
Marek Polacek7b181072010-10-28 21:34:56 +0200101 // we should exit on any signal; signals should interrupt read
102 bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
103
Harald Becker9beb68e2011-02-27 07:16:44 +0100104 // inform user that program ends after time of inactivity
105 printf(press_keys, "10s after last keypress");
106
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000107 // read and show scancodes
Marek Polacek7b181072010-10-28 21:34:56 +0200108 while (!bb_got_signal) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000109 char buf[18];
110 int i, n;
Marek Polacek7b181072010-10-28 21:34:56 +0200111
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000112 // setup 10s watchdog
113 alarm(10);
Harald Becker9beb68e2011-02-27 07:16:44 +0100114
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000115 // read scancodes
116 n = read(STDIN_FILENO, buf, sizeof(buf));
117 i = 0;
118 while (i < n) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000119 if (option_mask32 & OPT_s) {
Marek Polacek7b181072010-10-28 21:34:56 +0200120 // show raw scancodes
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000121 printf("0x%02x ", buf[i++]);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000122 } else {
Marek Polacek7b181072010-10-28 21:34:56 +0200123 // show interpreted scancodes (default)
124 char c = buf[i];
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000125 int kc;
Denys Vlasenkocc131532010-08-29 05:00:40 +0200126 if (i+2 < n
127 && (c & 0x7f) == 0
128 && (buf[i+1] & 0x80) != 0
129 && (buf[i+2] & 0x80) != 0
130 ) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000131 kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
132 i += 3;
133 } else {
134 kc = (c & 0x7f);
135 i++;
136 }
Denys Vlasenkocc131532010-08-29 05:00:40 +0200137 printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000138 }
139 }
140 puts("\r");
141 }
Harald Becker9beb68e2011-02-27 07:16:44 +0100142
143 // restore keyboard mode
144 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000145 }
146
Harald Becker9beb68e2011-02-27 07:16:44 +0100147 // restore console settings
Marek Polacek7b181072010-10-28 21:34:56 +0200148 xset1(&tio0);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000149
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000150 return EXIT_SUCCESS;
151}