showkey.c 3.2 KB

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