kbd_mode.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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"
  13. //config: default y
  14. //config: select PLATFORM_LINUX
  15. //config: help
  16. //config: This program reports and sets keyboard mode.
  17. //applet:IF_KBD_MODE(APPLET(kbd_mode, BB_DIR_BIN, BB_SUID_DROP))
  18. //kbuild:lib-$(CONFIG_KBD_MODE) += kbd_mode.o
  19. //usage:#define kbd_mode_trivial_usage
  20. //usage: "[-a|k|s|u] [-C TTY]"
  21. //usage:#define kbd_mode_full_usage "\n\n"
  22. //usage: "Report or set the keyboard mode\n"
  23. //usage: "\n -a Default (ASCII)"
  24. //usage: "\n -k Medium-raw (keyboard)"
  25. //usage: "\n -s Raw (scancode)"
  26. //usage: "\n -u Unicode (utf-8)"
  27. //usage: "\n -C TTY Affect TTY instead of /dev/tty"
  28. #include "libbb.h"
  29. #include <linux/kd.h>
  30. int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  31. int kbd_mode_main(int argc UNUSED_PARAM, char **argv)
  32. {
  33. enum {
  34. SCANCODE = (1 << 0),
  35. ASCII = (1 << 1),
  36. MEDIUMRAW = (1 << 2),
  37. UNICODE = (1 << 3),
  38. };
  39. int fd;
  40. unsigned opt;
  41. const char *tty_name = CURRENT_TTY;
  42. opt = getopt32(argv, "sakuC:", &tty_name);
  43. fd = xopen_nonblocking(tty_name);
  44. opt &= 0xf; /* clear -C bit, see (*) */
  45. if (!opt) { /* print current setting */
  46. const char *mode = "unknown";
  47. int m;
  48. xioctl(fd, KDGKBMODE, &m);
  49. if (m == K_RAW)
  50. mode = "raw (scancode)";
  51. else if (m == K_XLATE)
  52. mode = "default (ASCII)";
  53. else if (m == K_MEDIUMRAW)
  54. mode = "mediumraw (keycode)";
  55. else if (m == K_UNICODE)
  56. mode = "Unicode (UTF-8)";
  57. printf("The keyboard is in %s mode\n", mode);
  58. } else {
  59. /* here we depend on specific bits assigned to options (*) */
  60. opt = opt & UNICODE ? 3 : opt >> 1;
  61. /* double cast prevents warnings about widening conversion */
  62. xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt);
  63. }
  64. if (ENABLE_FEATURE_CLEAN_UP)
  65. close(fd);
  66. return EXIT_SUCCESS;
  67. }