2008-08-17 22:17:48 +08:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* shows keys pressed. inspired by kbd package
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
|
|
|
|
*
|
2010-08-17 02:14:46 +08:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2008-08-17 22:17:48 +08:00
|
|
|
*/
|
2016-11-23 17:39:27 +08:00
|
|
|
//config:config SHOWKEY
|
2017-07-19 04:01:24 +08:00
|
|
|
//config: bool "showkey (4.7 kb)"
|
2016-11-23 17:39:27 +08:00
|
|
|
//config: default y
|
|
|
|
//config: select PLATFORM_LINUX
|
|
|
|
//config: help
|
2017-07-21 15:50:55 +08:00
|
|
|
//config: Shows keys pressed.
|
2016-11-23 17:39:27 +08:00
|
|
|
|
|
|
|
//applet:IF_SHOWKEY(APPLET(showkey, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_SHOWKEY) += showkey.o
|
2008-08-17 22:17:48 +08:00
|
|
|
|
2011-03-28 05:42:28 +08:00
|
|
|
//usage:#define showkey_trivial_usage
|
|
|
|
//usage: "[-a | -k | -s]"
|
|
|
|
//usage:#define showkey_full_usage "\n\n"
|
|
|
|
//usage: "Show keys pressed\n"
|
|
|
|
//usage: "\n -a Display decimal/octal/hex values of the keys"
|
|
|
|
//usage: "\n -k Display interpreted keycodes (default)"
|
|
|
|
//usage: "\n -s Display raw scan-codes"
|
|
|
|
|
2008-08-17 22:17:48 +08:00
|
|
|
#include "libbb.h"
|
|
|
|
#include <linux/kd.h>
|
|
|
|
|
|
|
|
|
|
|
|
struct globals {
|
|
|
|
int kbmode;
|
|
|
|
struct termios tio, tio0;
|
|
|
|
};
|
|
|
|
#define G (*ptr_to_globals)
|
2010-10-29 17:46:52 +08:00
|
|
|
#define kbmode (G.kbmode)
|
|
|
|
#define tio (G.tio)
|
|
|
|
#define tio0 (G.tio0)
|
2008-08-17 22:17:48 +08:00
|
|
|
#define INIT_G() do { \
|
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2010-10-29 03:34:56 +08:00
|
|
|
// set raw tty mode
|
|
|
|
// also used by microcom
|
|
|
|
// libbb candidates?
|
|
|
|
static void xget1(struct termios *t, struct termios *oldt)
|
2008-08-17 22:17:48 +08:00
|
|
|
{
|
2010-10-29 03:34:56 +08:00
|
|
|
tcgetattr(STDIN_FILENO, oldt);
|
|
|
|
*t = *oldt;
|
|
|
|
cfmakeraw(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xset1(struct termios *t)
|
|
|
|
{
|
|
|
|
int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
|
|
|
|
if (ret) {
|
|
|
|
bb_perror_msg("can't tcsetattr for stdin");
|
|
|
|
}
|
2008-08-17 22:17:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int showkey_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
enum {
|
2010-10-29 17:46:52 +08:00
|
|
|
OPT_a = (1<<0), // display the decimal/octal/hex values of the keys
|
|
|
|
OPT_k = (1<<1), // display only the interpreted keycodes (default)
|
|
|
|
OPT_s = (1<<2), // display only the raw scan-codes
|
2008-08-17 22:17:48 +08:00
|
|
|
};
|
|
|
|
|
2010-10-29 03:34:56 +08:00
|
|
|
INIT_G();
|
|
|
|
|
2008-08-17 22:17:48 +08:00
|
|
|
// FIXME: aks are all mutually exclusive
|
|
|
|
getopt32(argv, "aks");
|
|
|
|
|
|
|
|
// prepare for raw mode
|
2010-08-29 11:00:40 +08:00
|
|
|
xget1(&tio, &tio0);
|
2008-08-17 22:17:48 +08:00
|
|
|
// put stdin in raw mode
|
2010-08-29 11:00:40 +08:00
|
|
|
xset1(&tio);
|
2008-08-17 22:17:48 +08:00
|
|
|
|
2011-02-27 14:16:44 +08:00
|
|
|
#define press_keys "Press any keys, program terminates %s:\r\n\n"
|
|
|
|
|
2008-08-17 22:17:48 +08:00
|
|
|
if (option_mask32 & OPT_a) {
|
2011-02-27 14:16:44 +08:00
|
|
|
// just read stdin char by char
|
2010-08-29 11:00:40 +08:00
|
|
|
unsigned char c;
|
2010-10-29 03:34:56 +08:00
|
|
|
|
2011-02-27 14:16:44 +08:00
|
|
|
printf(press_keys, "on EOF (ctrl-D)");
|
|
|
|
|
|
|
|
// read and show byte values
|
2010-10-29 03:34:56 +08:00
|
|
|
while (1 == read(STDIN_FILENO, &c, 1)) {
|
2010-08-29 11:00:40 +08:00
|
|
|
printf("%3u 0%03o 0x%02x\r\n", c, c, c);
|
2008-08-17 22:17:48 +08:00
|
|
|
if (04 /*CTRL-D*/ == c)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2011-02-27 14:16:44 +08:00
|
|
|
// we assume a PC keyboard
|
|
|
|
xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
|
|
|
|
printf("Keyboard mode was %s.\r\n\n",
|
|
|
|
kbmode == K_RAW ? "RAW" :
|
|
|
|
(kbmode == K_XLATE ? "XLATE" :
|
|
|
|
(kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
|
|
|
|
(kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN")))
|
|
|
|
);
|
|
|
|
|
2008-08-17 22:17:48 +08:00
|
|
|
// set raw keyboard mode
|
2008-09-29 00:44:28 +08:00
|
|
|
xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
|
2008-08-17 22:17:48 +08:00
|
|
|
|
2010-10-29 03:34:56 +08:00
|
|
|
// we should exit on any signal; signals should interrupt read
|
|
|
|
bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
|
|
|
|
|
2011-02-27 14:16:44 +08:00
|
|
|
// inform user that program ends after time of inactivity
|
|
|
|
printf(press_keys, "10s after last keypress");
|
|
|
|
|
2008-08-17 22:17:48 +08:00
|
|
|
// read and show scancodes
|
2010-10-29 03:34:56 +08:00
|
|
|
while (!bb_got_signal) {
|
2008-08-17 22:17:48 +08:00
|
|
|
char buf[18];
|
|
|
|
int i, n;
|
2010-10-29 03:34:56 +08:00
|
|
|
|
2008-08-17 22:17:48 +08:00
|
|
|
// setup 10s watchdog
|
|
|
|
alarm(10);
|
2011-02-27 14:16:44 +08:00
|
|
|
|
2008-08-17 22:17:48 +08:00
|
|
|
// read scancodes
|
|
|
|
n = read(STDIN_FILENO, buf, sizeof(buf));
|
|
|
|
i = 0;
|
|
|
|
while (i < n) {
|
|
|
|
if (option_mask32 & OPT_s) {
|
2010-10-29 03:34:56 +08:00
|
|
|
// show raw scancodes
|
2008-08-17 22:17:48 +08:00
|
|
|
printf("0x%02x ", buf[i++]);
|
|
|
|
} else {
|
2010-10-29 03:34:56 +08:00
|
|
|
// show interpreted scancodes (default)
|
|
|
|
char c = buf[i];
|
2008-08-17 22:17:48 +08:00
|
|
|
int kc;
|
2010-08-29 11:00:40 +08:00
|
|
|
if (i+2 < n
|
|
|
|
&& (c & 0x7f) == 0
|
|
|
|
&& (buf[i+1] & 0x80) != 0
|
|
|
|
&& (buf[i+2] & 0x80) != 0
|
|
|
|
) {
|
2008-08-17 22:17:48 +08:00
|
|
|
kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
|
|
|
|
i += 3;
|
|
|
|
} else {
|
|
|
|
kc = (c & 0x7f);
|
|
|
|
i++;
|
|
|
|
}
|
2010-08-29 11:00:40 +08:00
|
|
|
printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
|
2008-08-17 22:17:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
puts("\r");
|
|
|
|
}
|
2011-02-27 14:16:44 +08:00
|
|
|
|
|
|
|
// restore keyboard mode
|
|
|
|
xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
|
2008-08-17 22:17:48 +08:00
|
|
|
}
|
|
|
|
|
2011-02-27 14:16:44 +08:00
|
|
|
// restore console settings
|
2010-10-29 03:34:56 +08:00
|
|
|
xset1(&tio0);
|
2008-08-17 22:17:48 +08:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|