3
0

kbd_mode.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini kbd_mode implementation for busybox
  4. *
  5. * Copyright (C) 2007 Loïc Grenié <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. */
  12. #include "libbb.h"
  13. #include <linux/kd.h>
  14. int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  15. int kbd_mode_main(int argc UNUSED_PARAM, char **argv)
  16. {
  17. int fd;
  18. unsigned opt;
  19. enum {
  20. SCANCODE = (1 << 0),
  21. ASCII = (1 << 1),
  22. MEDIUMRAW = (1 << 2),
  23. UNICODE = (1 << 3)
  24. };
  25. static const char KD_xxx[] ALIGN1 = "saku";
  26. opt = getopt32(argv, KD_xxx);
  27. fd = get_console_fd_or_die();
  28. if (!opt) { /* print current setting */
  29. const char *mode = "unknown";
  30. int m;
  31. ioctl(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. opt = opt & UNICODE ? 3 : opt >> 1;
  43. /* double cast prevents warnings about widening conversion */
  44. xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt);
  45. }
  46. if (ENABLE_FEATURE_CLEAN_UP)
  47. close(fd);
  48. return EXIT_SUCCESS;
  49. }