blob: 5de18b8eab9c6b9dae38e1454de085ba35c8e437 [file] [log] [blame]
Erik Andersenabc199e2000-04-28 01:26:31 +00001/* vi: set sw=4 ts=4: */
2/*
3 * setkeycodes
4 *
5 * Copyright (C) 1994-1998 Andries E. Brouwer <aeb@cwi.nl>
6 *
Eric Andersencb81e642003-07-14 21:21:08 +00007 * Adjusted for BusyBox by Erik Andersen <andersen@codepoet.org>
Erik Andersenabc199e2000-04-28 01:26:31 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersenabc199e2000-04-28 01:26:31 +000010 */
Denys Vlasenko6d932992016-11-23 10:39:27 +010011//config:config SETKEYCODES
Denys Vlasenkob097a842018-12-28 03:20:17 +010012//config: bool "setkeycodes (2.1 kb)"
Denys Vlasenko6d932992016-11-23 10:39:27 +010013//config: default y
Denys Vlasenko6d932992016-11-23 10:39:27 +010014//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: This program loads entries into the kernel's scancode-to-keycode
16//config: map, allowing unusual keyboards to generate usable keycodes.
Denys Vlasenko6d932992016-11-23 10:39:27 +010017
Denys Vlasenkob83db4d2017-08-06 18:29:25 +020018//applet:IF_SETKEYCODES(APPLET_NOEXEC(setkeycodes, setkeycodes, BB_DIR_USR_BIN, BB_SUID_DROP, setkeycodes))
Denys Vlasenko6d932992016-11-23 10:39:27 +010019
20//kbuild:lib-$(CONFIG_SETKEYCODES) += setkeycodes.o
Pere Orga55068c42011-03-27 23:42:28 +020021
22//usage:#define setkeycodes_trivial_usage
Denys Vlasenkob83db4d2017-08-06 18:29:25 +020023//usage: "{ SCANCODE KEYCODE }..."
Pere Orga55068c42011-03-27 23:42:28 +020024//usage:#define setkeycodes_full_usage "\n\n"
Denys Vlasenkob83db4d2017-08-06 18:29:25 +020025//usage: "Modify kernel's scancode-to-keycode map,\n"
Pere Orga55068c42011-03-27 23:42:28 +020026//usage: "allowing unusual keyboards to generate usable keycodes.\n\n"
Denys Vlasenkob83db4d2017-08-06 18:29:25 +020027//usage: "SCANCODE is either xx or e0xx (hexadecimal), KEYCODE is decimal."
Pere Orga55068c42011-03-27 23:42:28 +020028//usage:
29//usage:#define setkeycodes_example_usage
30//usage: "$ setkeycodes e030 127\n"
31
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000032#include "libbb.h"
Eric Andersenbd22ed82000-07-08 18:55:24 +000033
Eric Andersenbd22ed82000-07-08 18:55:24 +000034/* From <linux/kd.h> */
35struct kbkeycode {
Denis Vlasenko1d426652008-03-17 09:09:09 +000036 unsigned scancode, keycode;
Eric Andersenbd22ed82000-07-08 18:55:24 +000037};
Rob Landleybc68cd12006-03-10 19:22:06 +000038enum {
39 KDSETKEYCODE = 0x4B4D /* write kernel keycode table entry */
40};
Eric Andersenbd22ed82000-07-08 18:55:24 +000041
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000042int setkeycodes_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
43int setkeycodes_main(int argc, char **argv)
Erik Andersenabc199e2000-04-28 01:26:31 +000044{
Denys Vlasenko2e068e72010-05-19 17:15:54 +020045 int fd;
Erik Andersenabc199e2000-04-28 01:26:31 +000046
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000047 if (!(argc & 1) /* if even */ || argc < 2) {
Denis Vlasenko8fafacd2006-09-09 13:23:04 +000048 bb_show_usage();
Erik Andersenabc199e2000-04-28 01:26:31 +000049 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000050
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000051 fd = get_console_fd_or_die();
Erik Andersenabc199e2000-04-28 01:26:31 +000052
Denys Vlasenko2e068e72010-05-19 17:15:54 +020053 while (argv[1]) {
Denys Vlasenkob83db4d2017-08-06 18:29:25 +020054 struct kbkeycode a;
55 int sc;
56
57 sc = xstrtoul_range(argv[1], 16, 0, 0xe07f);
Denys Vlasenko2e068e72010-05-19 17:15:54 +020058 if (sc >= 0xe000) {
59 sc -= 0xe000;
60 sc += 0x0080;
Denis Vlasenko8fafacd2006-09-09 13:23:04 +000061 }
Denys Vlasenko2e068e72010-05-19 17:15:54 +020062 a.scancode = sc;
63 a.keycode = xatou_range(argv[2], 0, 255);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000064 ioctl_or_perror_and_die(fd, KDSETKEYCODE, &a,
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000065 "can't set SCANCODE %x to KEYCODE %d",
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000066 sc, a.keycode);
Denis Vlasenko8fafacd2006-09-09 13:23:04 +000067 argv += 2;
Erik Andersenabc199e2000-04-28 01:26:31 +000068 }
Matt Kraai3e856ce2000-12-01 02:55:13 +000069 return EXIT_SUCCESS;
Erik Andersenabc199e2000-04-28 01:26:31 +000070}