showkey.c 3.5 KB

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