kbd_mode.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 tarball for details.
  10. */
  11. #include "libbb.h"
  12. #include <linux/kd.h>
  13. int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  14. int kbd_mode_main(int argc UNUSED_PARAM, char **argv)
  15. {
  16. enum {
  17. SCANCODE = (1 << 0),
  18. ASCII = (1 << 1),
  19. MEDIUMRAW = (1 << 2),
  20. UNICODE = (1 << 3),
  21. };
  22. int fd;
  23. unsigned opt;
  24. const char *tty_name = CURRENT_TTY;
  25. opt = getopt32(argv, "sakuC:", &tty_name);
  26. fd = xopen_nonblocking(tty_name);
  27. opt &= 0xf; /* clear -C bit, see (*) */
  28. if (!opt) { /* print current setting */
  29. const char *mode = "unknown";
  30. int m;
  31. xioctl(fd, KDGKBMODE, &m);
  32. if (m == K_RAW)
  33. mode = "raw (scancode)";
  34. else if (m == K_XLATE)
  35. mode = "default (ASCII)";
  36. else if (m == K_MEDIUMRAW)
  37. mode = "mediumraw (keycode)";
  38. else if (m == K_UNICODE)
  39. mode = "Unicode (UTF-8)";
  40. printf("The keyboard is in %s mode\n", mode);
  41. } else {
  42. /* here we depend on specific bits assigned to options (*) */
  43. opt = opt & UNICODE ? 3 : opt >> 1;
  44. /* double cast prevents warnings about widening conversion */
  45. xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt);
  46. }
  47. if (ENABLE_FEATURE_CLEAN_UP)
  48. close(fd);
  49. return EXIT_SUCCESS;
  50. }