kbd_mode.c 1.8 KB

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