setkeycodes.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <fcntl.h>
  27. #include <sys/ioctl.h>
  28. #include "busybox.h"
  29. /* From <linux/kd.h> */
  30. struct kbkeycode {
  31. unsigned int scancode, keycode;
  32. };
  33. enum {
  34. KDSETKEYCODE = 0x4B4D /* write kernel keycode table entry */
  35. };
  36. extern int
  37. setkeycodes_main(int argc, char** argv)
  38. {
  39. char *ep;
  40. int fd, sc;
  41. struct kbkeycode a;
  42. if (argc % 2 != 1 || argc < 2) {
  43. bb_show_usage();
  44. }
  45. fd = get_console_fd();
  46. while (argc > 2) {
  47. a.keycode = atoi(argv[2]);
  48. a.scancode = sc = strtol(argv[1], &ep, 16);
  49. if (*ep) {
  50. bb_error_msg_and_die("error reading SCANCODE: '%s'", argv[1]);
  51. }
  52. if (a.scancode > 127) {
  53. a.scancode -= 0xe000;
  54. a.scancode += 128;
  55. }
  56. if (a.scancode > 255 || a.keycode > 127) {
  57. bb_error_msg_and_die("SCANCODE or KEYCODE outside bounds");
  58. }
  59. if (ioctl(fd,KDSETKEYCODE,&a)) {
  60. perror("KDSETKEYCODE");
  61. bb_error_msg_and_die("failed to set SCANCODE %x to KEYCODE %d", sc, a.keycode);
  62. }
  63. argc -= 2;
  64. argv += 2;
  65. }
  66. return EXIT_SUCCESS;
  67. }