blob: 69b785ec6200d3f788a814962d86a268fa95c950 [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"
Pere Orga55068c42011-03-27 23:42:28 +020014//usage: "\n -a Display decimal/octal/hex values of the keys"
15//usage: "\n -k Display interpreted keycodes (default)"
16//usage: "\n -s Display raw scan-codes"
17
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000018#include "libbb.h"
19#include <linux/kd.h>
20
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000021
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000022struct globals {
23 int kbmode;
24 struct termios tio, tio0;
25};
26#define G (*ptr_to_globals)
Denys Vlasenkofb132e42010-10-29 11:46:52 +020027#define kbmode (G.kbmode)
28#define tio (G.tio)
29#define tio0 (G.tio0)
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000030#define INIT_G() do { \
31 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
32} while (0)
33
34
Marek Polacek7b181072010-10-28 21:34:56 +020035// set raw tty mode
36// also used by microcom
37// libbb candidates?
38static void xget1(struct termios *t, struct termios *oldt)
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000039{
Marek Polacek7b181072010-10-28 21:34:56 +020040 tcgetattr(STDIN_FILENO, oldt);
41 *t = *oldt;
42 cfmakeraw(t);
43}
44
45static void xset1(struct termios *t)
46{
47 int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
48 if (ret) {
49 bb_perror_msg("can't tcsetattr for stdin");
50 }
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000051}
52
53int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
54int showkey_main(int argc UNUSED_PARAM, char **argv)
55{
56 enum {
Denys Vlasenkofb132e42010-10-29 11:46:52 +020057 OPT_a = (1<<0), // display the decimal/octal/hex values of the keys
58 OPT_k = (1<<1), // display only the interpreted keycodes (default)
59 OPT_s = (1<<2), // display only the raw scan-codes
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000060 };
61
Marek Polacek7b181072010-10-28 21:34:56 +020062 INIT_G();
63
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000064 // FIXME: aks are all mutually exclusive
65 getopt32(argv, "aks");
66
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000067 // prepare for raw mode
Denys Vlasenkocc131532010-08-29 05:00:40 +020068 xget1(&tio, &tio0);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000069 // put stdin in raw mode
Denys Vlasenkocc131532010-08-29 05:00:40 +020070 xset1(&tio);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000071
Harald Becker9beb68e2011-02-27 07:16:44 +010072#define press_keys "Press any keys, program terminates %s:\r\n\n"
73
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000074 if (option_mask32 & OPT_a) {
Harald Becker9beb68e2011-02-27 07:16:44 +010075 // just read stdin char by char
Denys Vlasenkocc131532010-08-29 05:00:40 +020076 unsigned char c;
Marek Polacek7b181072010-10-28 21:34:56 +020077
Harald Becker9beb68e2011-02-27 07:16:44 +010078 printf(press_keys, "on EOF (ctrl-D)");
79
80 // read and show byte values
Marek Polacek7b181072010-10-28 21:34:56 +020081 while (1 == read(STDIN_FILENO, &c, 1)) {
Denys Vlasenkocc131532010-08-29 05:00:40 +020082 printf("%3u 0%03o 0x%02x\r\n", c, c, c);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000083 if (04 /*CTRL-D*/ == c)
84 break;
85 }
Harald Becker9beb68e2011-02-27 07:16:44 +010086
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000087 } else {
Harald Becker9beb68e2011-02-27 07:16:44 +010088 // we assume a PC keyboard
89 xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
90 printf("Keyboard mode was %s.\r\n\n",
91 kbmode == K_RAW ? "RAW" :
92 (kbmode == K_XLATE ? "XLATE" :
93 (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
94 (kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN")))
95 );
96
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000097 // set raw keyboard mode
Denis Vlasenko651d49a2008-09-28 16:44:28 +000098 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000099
Marek Polacek7b181072010-10-28 21:34:56 +0200100 // we should exit on any signal; signals should interrupt read
101 bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
102
Harald Becker9beb68e2011-02-27 07:16:44 +0100103 // inform user that program ends after time of inactivity
104 printf(press_keys, "10s after last keypress");
105
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000106 // read and show scancodes
Marek Polacek7b181072010-10-28 21:34:56 +0200107 while (!bb_got_signal) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000108 char buf[18];
109 int i, n;
Marek Polacek7b181072010-10-28 21:34:56 +0200110
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000111 // setup 10s watchdog
112 alarm(10);
Harald Becker9beb68e2011-02-27 07:16:44 +0100113
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000114 // read scancodes
115 n = read(STDIN_FILENO, buf, sizeof(buf));
116 i = 0;
117 while (i < n) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000118 if (option_mask32 & OPT_s) {
Marek Polacek7b181072010-10-28 21:34:56 +0200119 // show raw scancodes
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000120 printf("0x%02x ", buf[i++]);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000121 } else {
Marek Polacek7b181072010-10-28 21:34:56 +0200122 // show interpreted scancodes (default)
123 char c = buf[i];
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000124 int kc;
Denys Vlasenkocc131532010-08-29 05:00:40 +0200125 if (i+2 < n
126 && (c & 0x7f) == 0
127 && (buf[i+1] & 0x80) != 0
128 && (buf[i+2] & 0x80) != 0
129 ) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000130 kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
131 i += 3;
132 } else {
133 kc = (c & 0x7f);
134 i++;
135 }
Denys Vlasenkocc131532010-08-29 05:00:40 +0200136 printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000137 }
138 }
139 puts("\r");
140 }
Harald Becker9beb68e2011-02-27 07:16:44 +0100141
142 // restore keyboard mode
143 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000144 }
145
Harald Becker9beb68e2011-02-27 07:16:44 +0100146 // restore console settings
Marek Polacek7b181072010-10-28 21:34:56 +0200147 xset1(&tio0);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000148
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000149 return EXIT_SUCCESS;
150}