setkeycodes.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * setkeycodes
  4. *
  5. * Copyright (C) 1994-1998 Andries E. Brouwer <aeb@cwi.nl>
  6. *
  7. * Adjusted for BusyBox by Erik Andersen <andersen@codepoet.org>
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10. */
  11. //config:config SETKEYCODES
  12. //config: bool "setkeycodes (2.1 kb)"
  13. //config: default y
  14. //config: select PLATFORM_LINUX
  15. //config: help
  16. //config: This program loads entries into the kernel's scancode-to-keycode
  17. //config: map, allowing unusual keyboards to generate usable keycodes.
  18. //applet:IF_SETKEYCODES(APPLET_NOEXEC(setkeycodes, setkeycodes, BB_DIR_USR_BIN, BB_SUID_DROP, setkeycodes))
  19. //kbuild:lib-$(CONFIG_SETKEYCODES) += setkeycodes.o
  20. //usage:#define setkeycodes_trivial_usage
  21. //usage: "{ SCANCODE KEYCODE }..."
  22. //usage:#define setkeycodes_full_usage "\n\n"
  23. //usage: "Modify kernel's scancode-to-keycode map,\n"
  24. //usage: "allowing unusual keyboards to generate usable keycodes.\n\n"
  25. //usage: "SCANCODE is either xx or e0xx (hexadecimal), KEYCODE is decimal."
  26. //usage:
  27. //usage:#define setkeycodes_example_usage
  28. //usage: "$ setkeycodes e030 127\n"
  29. #include "libbb.h"
  30. /* From <linux/kd.h> */
  31. struct kbkeycode {
  32. unsigned scancode, keycode;
  33. };
  34. enum {
  35. KDSETKEYCODE = 0x4B4D /* write kernel keycode table entry */
  36. };
  37. int setkeycodes_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  38. int setkeycodes_main(int argc, char **argv)
  39. {
  40. int fd;
  41. if (!(argc & 1) /* if even */ || argc < 2) {
  42. bb_show_usage();
  43. }
  44. fd = get_console_fd_or_die();
  45. while (argv[1]) {
  46. struct kbkeycode a;
  47. int sc;
  48. sc = xstrtoul_range(argv[1], 16, 0, 0xe07f);
  49. if (sc >= 0xe000) {
  50. sc -= 0xe000;
  51. sc += 0x0080;
  52. }
  53. a.scancode = sc;
  54. a.keycode = xatou_range(argv[2], 0, 255);
  55. ioctl_or_perror_and_die(fd, KDSETKEYCODE, &a,
  56. "can't set SCANCODE %x to KEYCODE %d",
  57. sc, a.keycode);
  58. argv += 2;
  59. }
  60. return EXIT_SUCCESS;
  61. }