1 /* vi: set sw=4 ts=4: */
3 * Mini kbd_mode implementation for busybox
5 * Copyright (C) 2007 Loic Grenie <loic.grenie@gmail.com>
6 * written using Andries Brouwer <aeb@cwi.nl>'s kbd_mode from
7 * console-utils v0.2.3, licensed under GNU GPLv2
9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 //config:config KBD_MODE
12 //config: bool "kbd_mode (4 kb)"
14 //config: select PLATFORM_LINUX
16 //config: This program reports and sets keyboard mode.
18 //applet:IF_KBD_MODE(APPLET(kbd_mode, BB_DIR_BIN, BB_SUID_DROP))
20 //kbuild:lib-$(CONFIG_KBD_MODE) += kbd_mode.o
22 //usage:#define kbd_mode_trivial_usage
23 //usage: "[-a|k|s|u] [-C TTY]"
24 //usage:#define kbd_mode_full_usage "\n\n"
25 //usage: "Report or set the keyboard mode\n"
26 //usage: "\n -a Default (ASCII)"
27 //usage: "\n -k Medium-raw (keycode)"
28 //usage: "\n -s Raw (scancode)"
29 //usage: "\n -u Unicode (utf-8)"
30 //usage: "\n -C TTY Affect TTY instead of /dev/tty"
35 int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
36 int kbd_mode_main(int argc UNUSED_PARAM, char **argv)
46 //TODO? kbd-2.0.3 without -C tries in sequence:
47 //fd#0, /dev/tty, /dev/tty0.
48 //Also, it checks KDGKBTYPE before doing KDGKBMODE
49 //maybe we can use get_console_fd_or_die()?
50 const char *tty_name = CURRENT_TTY;
52 opt = getopt32(argv, "sakuC:", &tty_name);
53 fd = xopen_nonblocking(tty_name);
54 opt &= 0xf; /* clear -C bit, see (*) */
56 if (!opt) { /* print current setting */
57 const char *mode = "unknown";
60 xioctl(fd, KDGKBMODE, &m);
62 mode = "raw (scancode)";
63 else if (m == K_XLATE)
64 mode = "default (ASCII)";
65 else if (m == K_MEDIUMRAW)
66 mode = "mediumraw (keycode)";
67 else if (m == K_UNICODE)
68 mode = "Unicode (UTF-8)";
69 else if (m == 4 /*K_OFF*/) /* kbd-2.0.3 does not show this mode, says "unknown" */
71 printf("The keyboard is in %s mode\n", mode);
73 /* here we depend on specific bits assigned to options (*)
74 * KDSKBMODE constants have these values:
76 * #define K_XLATE 0x01
77 * #define K_MEDIUMRAW 0x02
78 * #define K_UNICODE 0x03
80 * (looks like "-ak" together would cause the same effect as -u)
82 opt = opt & UNICODE ? 3 : opt >> 1;
83 /* double cast prevents warnings about widening conversion */
84 xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt);
87 if (ENABLE_FEATURE_CLEAN_UP)