3
0

kbd_mode.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. */
  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 ATTRIBUTE_UNUSED argc, 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();
  28. /* if (fd < 0)
  29. return EXIT_FAILURE;
  30. */
  31. if (!opt) { /* print current setting */
  32. const char *mode = "unknown";
  33. int m;
  34. ioctl(fd, KDGKBMODE, &m);
  35. if (m == K_RAW)
  36. mode = "raw (scancode)";
  37. else if (m == K_XLATE)
  38. mode = "default (ASCII)";
  39. else if (m == K_MEDIUMRAW)
  40. mode = "mediumraw (keycode)";
  41. else if (m == K_UNICODE)
  42. mode = "Unicode (UTF-8)";
  43. printf("The keyboard is in %s mode\n", mode);
  44. } else {
  45. opt = opt & UNICODE ? 3 : opt >> 1;
  46. /* double cast prevents warnings about widening conversion */
  47. xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt);
  48. }
  49. if (ENABLE_FEATURE_CLEAN_UP)
  50. close(fd);
  51. return EXIT_SUCCESS;
  52. }