blob: 1bdb909ea7de5af68204fd1c920237f65d950280 [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 *
7 * Adjusted for BusyBox by Erik Andersen <andersee@debian.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
25#include "internal.h"
26#include <stdio.h>
27#include <stdlib.h>
28#include <fcntl.h>
29#include <sys/ioctl.h>
Eric Andersenbd22ed82000-07-08 18:55:24 +000030
31
32/* From <linux/kd.h> */
33struct kbkeycode {
34 unsigned int scancode, keycode;
35};
36#define KDSETKEYCODE 0x4B4D /* write kernel keycode table entry */
37
Erik Andersenabc199e2000-04-28 01:26:31 +000038
39static const char setkeycodes_usage[] =
40 "setkeycodes SCANCODE KEYCODE ...\n"
41#ifndef BB_FEATURE_TRIVIAL_HELP
42 "\nSet entries into the kernel's scancode-to-keycode map,\n"
43 "allowing unusual keyboards to generate usable keycodes.\n\n"
44 "SCANCODE may be either xx or e0xx (hexadecimal),\n"
45 "and KEYCODE is given in decimal\n"
46#endif
47 ;
48
49extern int
50setkeycodes_main(int argc, char** argv)
51{
52 char *ep;
53 int fd, sc;
54 struct kbkeycode a;
55
56 if (argc % 2 != 1 || argc < 2) {
57 usage(setkeycodes_usage);
58 }
59
60 fd = get_console_fd("/dev/console");
61
62 while (argc > 2) {
63 a.keycode = atoi(argv[2]);
64 a.scancode = sc = strtol(argv[1], &ep, 16);
65 if (*ep) {
66 fatalError("error reading SCANCODE: '%s'\n", argv[1]);
67 }
68 if (a.scancode > 127) {
69 a.scancode -= 0xe000;
70 a.scancode += 128;
71 }
72 if (a.scancode > 255 || a.keycode > 127) {
73 fatalError("SCANCODE or KEYCODE outside bounds\n");
74 }
75 if (ioctl(fd,KDSETKEYCODE,&a)) {
76 perror("KDSETKEYCODE");
77 fatalError("failed to set SCANCODE %x to KEYCODE %d\n", sc, a.keycode);
78 }
79 argc -= 2;
80 argv += 2;
81 }
Eric Andersenb6106152000-06-19 17:25:40 +000082 return( TRUE);
Erik Andersenabc199e2000-04-28 01:26:31 +000083}