3
0

kbd_mode.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini loadkmap 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 <getopt.h>
  13. #include "libbb.h"
  14. #include <linux/kd.h>
  15. int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  16. int kbd_mode_main(int argc, char **argv)
  17. {
  18. static const char opts[] = "saku";
  19. const char *opt = argv[1];
  20. const char *p;
  21. int fd;
  22. fd = get_console_fd();
  23. if (fd < 0) /* get_console_fd() already complained */
  24. return EXIT_FAILURE;
  25. if (opt == NULL) {
  26. /* No arg */
  27. const char *msg = "unknown";
  28. int mode;
  29. ioctl(fd, KDGKBMODE, &mode);
  30. switch(mode) {
  31. case K_RAW:
  32. msg = "raw (scancode)";
  33. break;
  34. case K_XLATE:
  35. msg = "default (ASCII)";
  36. break;
  37. case K_MEDIUMRAW:
  38. msg = "mediumraw (keycode)";
  39. break;
  40. case K_UNICODE:
  41. msg = "Unicode (UTF-8)";
  42. break;
  43. }
  44. printf("The keyboard is in %s mode\n", msg);
  45. }
  46. else if (argc > 2 /* more than 1 arg */
  47. || *opt != '-' /* not an option */
  48. || (p = strchr(opts, opt[1])) == NULL /* not an option we expect */
  49. || opt[2] != '\0' /* more than one option char */
  50. ) {
  51. bb_show_usage();
  52. /* return EXIT_FAILURE; - not reached */
  53. }
  54. else {
  55. #if K_RAW != 0 || K_XLATE != 1 || K_MEDIUMRAW != 2 || K_UNICODE != 3
  56. #error kbd_mode must be changed
  57. #endif
  58. /* The options are in the order of the various K_xxx */
  59. ioctl(fd, KDSKBMODE, p - opts);
  60. }
  61. if (ENABLE_FEATURE_CLEAN_UP)
  62. close(fd);
  63. return EXIT_SUCCESS;
  64. }