blob: 06df68bfde0adc4feb898b21a209c49d6d782677 [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
10#include "libbb.h"
11#include <linux/kd.h>
12
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000013
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000014struct globals {
15 int kbmode;
16 struct termios tio, tio0;
17};
18#define G (*ptr_to_globals)
Denys Vlasenkofb132e42010-10-29 11:46:52 +020019#define kbmode (G.kbmode)
20#define tio (G.tio)
21#define tio0 (G.tio0)
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000022#define INIT_G() do { \
23 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
24} while (0)
25
26
Marek Polacek7b181072010-10-28 21:34:56 +020027// set raw tty mode
28// also used by microcom
29// libbb candidates?
30static void xget1(struct termios *t, struct termios *oldt)
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000031{
Marek Polacek7b181072010-10-28 21:34:56 +020032 tcgetattr(STDIN_FILENO, oldt);
33 *t = *oldt;
34 cfmakeraw(t);
35}
36
37static void xset1(struct termios *t)
38{
39 int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
40 if (ret) {
41 bb_perror_msg("can't tcsetattr for stdin");
42 }
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000043}
44
45int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
46int showkey_main(int argc UNUSED_PARAM, char **argv)
47{
48 enum {
Denys Vlasenkofb132e42010-10-29 11:46:52 +020049 OPT_a = (1<<0), // display the decimal/octal/hex values of the keys
50 OPT_k = (1<<1), // display only the interpreted keycodes (default)
51 OPT_s = (1<<2), // display only the raw scan-codes
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000052 };
53
Marek Polacek7b181072010-10-28 21:34:56 +020054 INIT_G();
55
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000056 // FIXME: aks are all mutually exclusive
57 getopt32(argv, "aks");
58
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000059 // prepare for raw mode
Denys Vlasenkocc131532010-08-29 05:00:40 +020060 xget1(&tio, &tio0);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000061 // put stdin in raw mode
Denys Vlasenkocc131532010-08-29 05:00:40 +020062 xset1(&tio);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000063
Harald Becker9beb68e2011-02-27 07:16:44 +010064#define press_keys "Press any keys, program terminates %s:\r\n\n"
65
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000066 if (option_mask32 & OPT_a) {
Harald Becker9beb68e2011-02-27 07:16:44 +010067 // just read stdin char by char
Denys Vlasenkocc131532010-08-29 05:00:40 +020068 unsigned char c;
Marek Polacek7b181072010-10-28 21:34:56 +020069
Harald Becker9beb68e2011-02-27 07:16:44 +010070 printf(press_keys, "on EOF (ctrl-D)");
71
72 // read and show byte values
Marek Polacek7b181072010-10-28 21:34:56 +020073 while (1 == read(STDIN_FILENO, &c, 1)) {
Denys Vlasenkocc131532010-08-29 05:00:40 +020074 printf("%3u 0%03o 0x%02x\r\n", c, c, c);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000075 if (04 /*CTRL-D*/ == c)
76 break;
77 }
Harald Becker9beb68e2011-02-27 07:16:44 +010078
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000079 } else {
Harald Becker9beb68e2011-02-27 07:16:44 +010080 // we assume a PC keyboard
81 xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
82 printf("Keyboard mode was %s.\r\n\n",
83 kbmode == K_RAW ? "RAW" :
84 (kbmode == K_XLATE ? "XLATE" :
85 (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
86 (kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN")))
87 );
88
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000089 // set raw keyboard mode
Denis Vlasenko651d49a2008-09-28 16:44:28 +000090 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000091
Marek Polacek7b181072010-10-28 21:34:56 +020092 // we should exit on any signal; signals should interrupt read
93 bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
94
Harald Becker9beb68e2011-02-27 07:16:44 +010095 // inform user that program ends after time of inactivity
96 printf(press_keys, "10s after last keypress");
97
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +000098 // read and show scancodes
Marek Polacek7b181072010-10-28 21:34:56 +020099 while (!bb_got_signal) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000100 char buf[18];
101 int i, n;
Marek Polacek7b181072010-10-28 21:34:56 +0200102
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000103 // setup 10s watchdog
104 alarm(10);
Harald Becker9beb68e2011-02-27 07:16:44 +0100105
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000106 // read scancodes
107 n = read(STDIN_FILENO, buf, sizeof(buf));
108 i = 0;
109 while (i < n) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000110 if (option_mask32 & OPT_s) {
Marek Polacek7b181072010-10-28 21:34:56 +0200111 // show raw scancodes
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000112 printf("0x%02x ", buf[i++]);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000113 } else {
Marek Polacek7b181072010-10-28 21:34:56 +0200114 // show interpreted scancodes (default)
115 char c = buf[i];
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000116 int kc;
Denys Vlasenkocc131532010-08-29 05:00:40 +0200117 if (i+2 < n
118 && (c & 0x7f) == 0
119 && (buf[i+1] & 0x80) != 0
120 && (buf[i+2] & 0x80) != 0
121 ) {
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000122 kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
123 i += 3;
124 } else {
125 kc = (c & 0x7f);
126 i++;
127 }
Denys Vlasenkocc131532010-08-29 05:00:40 +0200128 printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000129 }
130 }
131 puts("\r");
132 }
Harald Becker9beb68e2011-02-27 07:16:44 +0100133
134 // restore keyboard mode
135 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000136 }
137
Harald Becker9beb68e2011-02-27 07:16:44 +0100138 // restore console settings
Marek Polacek7b181072010-10-28 21:34:56 +0200139 xset1(&tio0);
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000140
Denis Vlasenkod7cc2e62008-08-17 14:17:48 +0000141 return EXIT_SUCCESS;
142}