123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #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)
- #define INIT_G() do { \
- SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
- } while (0)
- static void xget1(struct termios *t, struct termios *oldt)
- {
- tcgetattr(STDIN_FILENO, oldt);
- *t = *oldt;
- cfmakeraw(t);
- }
- static void xset1(struct termios *t)
- {
- int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
- if (ret) {
- bb_simple_perror_msg("can't tcsetattr for stdin");
- }
- }
- int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int showkey_main(int argc UNUSED_PARAM, char **argv)
- {
- enum {
- OPT_a = (1<<0),
- OPT_k = (1<<1),
- OPT_s = (1<<2),
- };
- INIT_G();
-
- getopt32(argv, "aks");
-
- xget1(&tio, &tio0);
-
- xset1(&tio);
- #define press_keys "Press any keys, program terminates %s:\r\n\n"
- if (option_mask32 & OPT_a) {
-
- unsigned char c;
- printf(press_keys, "on EOF (ctrl-D)");
-
- while (1 == read(STDIN_FILENO, &c, 1)) {
- printf("%3u 0%03o 0x%02x\r\n", c, c, c);
- if (04 == c)
- break;
- }
- } else {
-
- 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")))
- );
-
- xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
-
- bb_signals_norestart(BB_FATAL_SIGS, record_signo);
-
- printf(press_keys, "10s after last keypress");
-
- while (!bb_got_signal) {
- char buf[18];
- int i, n;
-
- alarm(10);
-
- n = read(STDIN_FILENO, buf, sizeof(buf));
- i = 0;
- while (i < n) {
- if (option_mask32 & OPT_s) {
-
- printf("0x%02x ", buf[i++]);
- } else {
-
- char c = buf[i];
- int kc;
- if (i+2 < n
- && (c & 0x7f) == 0
- && (buf[i+1] & 0x80) != 0
- && (buf[i+2] & 0x80) != 0
- ) {
- 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");
- }
- }
- puts("\r");
- }
-
- xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
- }
-
- xset1(&tio0);
- return EXIT_SUCCESS;
- }
|