showkey.c 3.7 KB

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