kbd_mode.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini kbd_mode implementation for busybox
  4. *
  5. * Copyright (C) 2007 Loic Grenie <loic.grenie@gmail.com>
  6. * written using Andries Brouwer <aeb@cwi.nl>'s kbd_mode from
  7. * console-utils v0.2.3, licensed under GNU GPLv2
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10. */
  11. //config:config KBD_MODE
  12. //config: bool "kbd_mode (4.3 kb)"
  13. //config: default y
  14. //config: help
  15. //config: This program reports and sets keyboard mode.
  16. //applet:IF_KBD_MODE(APPLET_NOEXEC(kbd_mode, kbd_mode, BB_DIR_BIN, BB_SUID_DROP, kbd_mode))
  17. //kbuild:lib-$(CONFIG_KBD_MODE) += kbd_mode.o
  18. //usage:#define kbd_mode_trivial_usage
  19. //usage: "[-a|k|s|u] [-C TTY]"
  20. //usage:#define kbd_mode_full_usage "\n\n"
  21. //usage: "Report or set VT console keyboard mode\n"
  22. //usage: "\n -a Default (ASCII)"
  23. //usage: "\n -k Medium-raw (keycode)"
  24. //usage: "\n -s Raw (scancode)"
  25. //usage: "\n -u Unicode (utf-8)"
  26. //usage: "\n -C TTY Affect TTY"
  27. #include "libbb.h"
  28. #include <linux/kd.h>
  29. int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  30. int kbd_mode_main(int argc UNUSED_PARAM, char **argv)
  31. {
  32. enum {
  33. SCANCODE = (1 << 0),
  34. ASCII = (1 << 1),
  35. MEDIUMRAW = (1 << 2),
  36. UNICODE = (1 << 3),
  37. };
  38. int fd;
  39. unsigned opt;
  40. const char *tty_name;
  41. opt = getopt32(argv, "sakuC:", &tty_name);
  42. if (opt & 0x10) {
  43. opt &= 0xf; /* clear -C bit, see (*) */
  44. fd = xopen_nonblocking(tty_name);
  45. } else {
  46. /* kbd-2.0.3 tries in sequence:
  47. * fd#0, /dev/tty, /dev/tty0.
  48. * get_console_fd_or_die: /dev/console, /dev/tty0, /dev/tty.
  49. * kbd-2.0.3 checks KDGKBTYPE, get_console_fd_or_die checks too.
  50. */
  51. fd = get_console_fd_or_die();
  52. }
  53. if (!opt) { /* print current setting */
  54. const char *mode = "unknown";
  55. int m;
  56. xioctl(fd, KDGKBMODE, &m);
  57. if (m == K_RAW)
  58. mode = "raw (scancode)";
  59. else if (m == K_XLATE)
  60. mode = "default (ASCII)";
  61. else if (m == K_MEDIUMRAW)
  62. mode = "mediumraw (keycode)";
  63. else if (m == K_UNICODE)
  64. mode = "Unicode (UTF-8)";
  65. else if (m == 4 /*K_OFF*/) /* kbd-2.0.3 does not show this mode, says "unknown" */
  66. mode = "off";
  67. printf("The keyboard is in %s mode\n", mode);
  68. } else {
  69. /* here we depend on specific bits assigned to options (*)
  70. * KDSKBMODE constants have these values:
  71. * #define K_RAW 0x00
  72. * #define K_XLATE 0x01
  73. * #define K_MEDIUMRAW 0x02
  74. * #define K_UNICODE 0x03
  75. * #define K_OFF 0x04
  76. * (looks like "-ak" together would cause the same effect as -u)
  77. */
  78. opt = opt & UNICODE ? 3 : opt >> 1;
  79. /* double cast prevents warnings about widening conversion */
  80. xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt);
  81. }
  82. if (ENABLE_FEATURE_CLEAN_UP)
  83. close(fd);
  84. return EXIT_SUCCESS;
  85. }