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