busybox/console-tools/showkey.c

160 lines
3.8 KiB
C
Raw Normal View History

2008-08-17 19:47:48 +05:30
/* vi: set sw=4 ts=4: */
/*
* shows keys pressed. inspired by kbd package
*
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
*
* Licensed under GPLv2, see file LICENSE in this source tree.
2008-08-17 19:47:48 +05:30
*/
//config:config SHOWKEY
//config: bool "showkey (4.7 kb)"
//config: default y
//config: select PLATFORM_LINUX
//config: help
//config: Shows keys pressed.
//applet:IF_SHOWKEY(APPLET(showkey, BB_DIR_USR_BIN, BB_SUID_DROP))
//kbuild:lib-$(CONFIG_SHOWKEY) += showkey.o
2008-08-17 19:47:48 +05:30
//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 19:47:48 +05:30
#include "libbb.h"
#include <linux/kd.h>
struct globals {
int kbmode;
struct termios tio, tio0;
};
#define G (*ptr_to_globals)
#define kbmode (G.kbmode)
#define tio (G.tio)
#define tio0 (G.tio0)
2008-08-17 19:47:48 +05:30
#define INIT_G() do { \
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
} while (0)
// set raw tty mode
// also used by microcom
// libbb candidates?
static void xget1(struct termios *t, struct termios *oldt)
2008-08-17 19:47:48 +05:30
{
tcgetattr(STDIN_FILENO, oldt);
*t = *oldt;
cfmakeraw(t);
}
static void xset1(struct termios *t)
{
int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
if (ret) {
libbb: reduce the overhead of single parameter bb_error_msg() calls Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower overhead call to bb_perror_msg() when only a string was being printed with no parameters. This saves space for some CPU architectures because it avoids the overhead of a call to a variadic function. However there has never been a simple version of bb_error_msg(), and since 2007 many new calls to bb_perror_msg() have been added that only take a single parameter and so could have been using bb_simple_perror_message(). This changeset introduces 'simple' versions of bb_info_msg(), bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and bb_herror_msg_and_die(), and replaces all calls that only take a single parameter, or use something like ("%s", arg), with calls to the corresponding 'simple' version. Since it is likely that single parameter calls to the variadic functions may be accidentally reintroduced in the future a new debugging config option WARN_SIMPLE_MSG has been introduced. This uses some macro magic which will cause any such calls to generate a warning, but this is turned off by default to avoid use of the unpleasant macros in normal circumstances. This is a large changeset due to the number of calls that have been replaced. The only files that contain changes other than simple substitution of function calls are libbb.h, libbb/herror_msg.c, libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c, networking/udhcp/common.h and util-linux/mdev.c additonal macros have been added for logging so that single parameter and multiple parameter logging variants exist. The amount of space saved varies considerably by architecture, and was found to be as follows (for 'defconfig' using GCC 7.4): Arm: -92 bytes MIPS: -52 bytes PPC: -1836 bytes x86_64: -938 bytes Note that for the MIPS architecture only an exception had to be made disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h) because it made these files larger on MIPS. Signed-off-by: James Byrne <james.byrne@origamienergy.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
bb_simple_perror_msg("can't tcsetattr for stdin");
}
2008-08-17 19:47:48 +05:30
}
int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int showkey_main(int argc UNUSED_PARAM, char **argv)
{
enum {
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 19:47:48 +05:30
};
INIT_G();
2008-08-17 19:47:48 +05:30
// FIXME: aks are all mutually exclusive
getopt32(argv, "aks");
// prepare for raw mode
xget1(&tio, &tio0);
2008-08-17 19:47:48 +05:30
// put stdin in raw mode
xset1(&tio);
2008-08-17 19:47:48 +05:30
#define press_keys "Press any keys, program terminates %s:\r\n\n"
2008-08-17 19:47:48 +05:30
if (option_mask32 & OPT_a) {
// just read stdin char by char
unsigned char c;
printf(press_keys, "on EOF (ctrl-D)");
// read and show byte values
while (1 == read(STDIN_FILENO, &c, 1)) {
printf("%3u 0%03o 0x%02x\r\n", c, c, c);
2008-08-17 19:47:48 +05:30
if (04 /*CTRL-D*/ == c)
break;
}
} else {
// 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 19:47:48 +05:30
// set raw keyboard mode
2008-09-28 22:14:28 +05:30
xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
2008-08-17 19:47:48 +05:30
// we should exit on any signal; signals should interrupt read
bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
// inform user that program ends after time of inactivity
printf(press_keys, "10s after last keypress");
2008-08-17 19:47:48 +05:30
// read and show scancodes
while (!bb_got_signal) {
2008-08-17 19:47:48 +05:30
char buf[18];
int i, n;
2008-08-17 19:47:48 +05:30
// setup 10s watchdog
alarm(10);
2008-08-17 19:47:48 +05:30
// read scancodes
n = read(STDIN_FILENO, buf, sizeof(buf));
i = 0;
while (i < n) {
if (option_mask32 & OPT_s) {
// show raw scancodes
2008-08-17 19:47:48 +05:30
printf("0x%02x ", buf[i++]);
} else {
// show interpreted scancodes (default)
char c = buf[i];
2008-08-17 19:47:48 +05:30
int kc;
if (i+2 < n
&& (c & 0x7f) == 0
&& (buf[i+1] & 0x80) != 0
&& (buf[i+2] & 0x80) != 0
) {
2008-08-17 19:47:48 +05:30
kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
i += 3;
} else {
kc = (c & 0x7f);
i++;
}
printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
2008-08-17 19:47:48 +05:30
}
}
puts("\r");
}
// restore keyboard mode
xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
2008-08-17 19:47:48 +05:30
}
// restore console settings
xset1(&tio0);
2008-08-17 19:47:48 +05:30
return EXIT_SUCCESS;
}