3
0

showkey.c 3.8 KB

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