EXEC_PREFER_APPLETS support by Gabriel L. Somlo <somlo@cmu.edu>
[platform/upstream/busybox.git] / console-tools / setkeycodes.c
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 tarball for details.
10  */
11
12 #include <sys/ioctl.h>
13 #include "busybox.h"
14
15
16 /* From <linux/kd.h> */
17 struct kbkeycode {
18         unsigned int scancode, keycode;
19 };
20 enum {
21         KDSETKEYCODE = 0x4B4D  /* write kernel keycode table entry */
22 };
23
24 int setkeycodes_main(int argc, char** argv);
25 int setkeycodes_main(int argc, char** argv)
26 {
27         int fd, sc;
28         struct kbkeycode a;
29
30         if (argc % 2 != 1 || argc < 2) {
31                 bb_show_usage();
32         }
33
34         fd = get_console_fd();
35
36         while (argc > 2) {
37                 a.keycode = xatoul_range(argv[2], 0, 127);
38                 a.scancode = sc = xstrtoul_range(argv[1], 16, 0, 255);
39                 if (a.scancode > 127) {
40                         a.scancode -= 0xe000;
41                         a.scancode += 128;
42                 }
43                 if (ioctl(fd, KDSETKEYCODE, &a)) {
44                         bb_perror_msg_and_die("failed to set SCANCODE %x to KEYCODE %d", sc, a.keycode);
45                 }
46                 argc -= 2;
47                 argv += 2;
48         }
49         return EXIT_SUCCESS;
50 }