busybox/console-tools/showkey.c
<<
>>
Prefs
   1/* 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 *
   7 * Licensed under GPLv2, see file LICENSE in this source tree.
   8 */
   9//config:config SHOWKEY
  10//config:       bool "showkey (4.7 kb)"
  11//config:       default y
  12//config:       select PLATFORM_LINUX
  13//config:       help
  14//config:       Shows keys pressed.
  15
  16//applet:IF_SHOWKEY(APPLET(showkey, BB_DIR_USR_BIN, BB_SUID_DROP))
  17
  18//kbuild:lib-$(CONFIG_SHOWKEY) += showkey.o
  19
  20//usage:#define showkey_trivial_usage
  21//usage:       "[-a | -k | -s]"
  22//usage:#define showkey_full_usage "\n\n"
  23//usage:       "Show keys pressed\n"
  24//usage:     "\n        -a      Display decimal/octal/hex values of the keys"
  25//usage:     "\n        -k      Display interpreted keycodes (default)"
  26//usage:     "\n        -s      Display raw scan-codes"
  27
  28#include "libbb.h"
  29#include <linux/kd.h>
  30
  31
  32struct globals {
  33        int kbmode;
  34        struct termios tio, tio0;
  35};
  36#define G (*ptr_to_globals)
  37#define kbmode (G.kbmode)
  38#define tio    (G.tio)
  39#define tio0   (G.tio0)
  40#define INIT_G() do { \
  41        SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  42} while (0)
  43
  44
  45// set raw tty mode
  46// also used by microcom
  47// libbb candidates?
  48static void xget1(struct termios *t, struct termios *oldt)
  49{
  50        tcgetattr(STDIN_FILENO, oldt);
  51        *t = *oldt;
  52        cfmakeraw(t);
  53}
  54
  55static void xset1(struct termios *t)
  56{
  57        int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
  58        if (ret) {
  59                bb_perror_msg("can't tcsetattr for stdin");
  60        }
  61}
  62
  63int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  64int showkey_main(int argc UNUSED_PARAM, char **argv)
  65{
  66        enum {
  67                OPT_a = (1<<0), // display the decimal/octal/hex values of the keys
  68                OPT_k = (1<<1), // display only the interpreted keycodes (default)
  69                OPT_s = (1<<2), // display only the raw scan-codes
  70        };
  71
  72        INIT_G();
  73
  74        // FIXME: aks are all mutually exclusive
  75        getopt32(argv, "aks");
  76
  77        // prepare for raw mode
  78        xget1(&tio, &tio0);
  79        // put stdin in raw mode
  80        xset1(&tio);
  81
  82#define press_keys "Press any keys, program terminates %s:\r\n\n"
  83
  84        if (option_mask32 & OPT_a) {
  85                // just read stdin char by char
  86                unsigned char c;
  87
  88                printf(press_keys, "on EOF (ctrl-D)");
  89
  90                // read and show byte values
  91                while (1 == read(STDIN_FILENO, &c, 1)) {
  92                        printf("%3u 0%03o 0x%02x\r\n", c, c, c);
  93                        if (04 /*CTRL-D*/ == c)
  94                                break;
  95                }
  96        } else {
  97                // we assume a PC keyboard
  98                xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
  99                printf("Keyboard mode was %s.\r\n\n",
 100                        kbmode == K_RAW ? "RAW" :
 101                                (kbmode == K_XLATE ? "XLATE" :
 102                                        (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
 103                                                (kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN")))
 104                );
 105
 106                // set raw keyboard mode
 107                xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
 108
 109                // we should exit on any signal; signals should interrupt read
 110                bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
 111
 112                // inform user that program ends after time of inactivity
 113                printf(press_keys, "10s after last keypress");
 114
 115                // read and show scancodes
 116                while (!bb_got_signal) {
 117                        char buf[18];
 118                        int i, n;
 119
 120                        // setup 10s watchdog
 121                        alarm(10);
 122
 123                        // read scancodes
 124                        n = read(STDIN_FILENO, buf, sizeof(buf));
 125                        i = 0;
 126                        while (i < n) {
 127                                if (option_mask32 & OPT_s) {
 128                                        // show raw scancodes
 129                                        printf("0x%02x ", buf[i++]);
 130                                } else {
 131                                        // show interpreted scancodes (default)
 132                                        char c = buf[i];
 133                                        int kc;
 134                                        if (i+2 < n
 135                                         && (c & 0x7f) == 0
 136                                         && (buf[i+1] & 0x80) != 0
 137                                         && (buf[i+2] & 0x80) != 0
 138                                        ) {
 139                                                kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
 140                                                i += 3;
 141                                        } else {
 142                                                kc = (c & 0x7f);
 143                                                i++;
 144                                        }
 145                                        printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
 146                                }
 147                        }
 148                        puts("\r");
 149                }
 150
 151                // restore keyboard mode
 152                xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
 153        }
 154
 155        // restore console settings
 156        xset1(&tio0);
 157
 158        return EXIT_SUCCESS;
 159}
 160