2007-10-20 05:47:34 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2008-05-19 13:48:50 +05:30
|
|
|
* Mini kbd_mode implementation for busybox
|
2007-10-20 05:47:34 +05:30
|
|
|
*
|
2008-09-04 03:24:46 +05:30
|
|
|
* Copyright (C) 2007 Loic Grenie <loic.grenie@gmail.com>
|
2007-10-20 05:47:34 +05:30
|
|
|
* written using Andries Brouwer <aeb@cwi.nl>'s kbd_mode from
|
|
|
|
* console-utils v0.2.3, licensed under GNU GPLv2
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2007-10-20 05:47:34 +05:30
|
|
|
*/
|
2016-11-23 15:09:27 +05:30
|
|
|
//config:config KBD_MODE
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "kbd_mode (4.1 kb)"
|
2016-11-23 15:09:27 +05:30
|
|
|
//config: default y
|
|
|
|
//config: select PLATFORM_LINUX
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: This program reports and sets keyboard mode.
|
2016-11-23 15:09:27 +05:30
|
|
|
|
2017-08-06 15:53:04 +05:30
|
|
|
//applet:IF_KBD_MODE(APPLET_NOEXEC(kbd_mode, kbd_mode, BB_DIR_BIN, BB_SUID_DROP, kbd_mode))
|
2016-11-23 15:09:27 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_KBD_MODE) += kbd_mode.o
|
2011-03-28 03:12:28 +05:30
|
|
|
|
|
|
|
//usage:#define kbd_mode_trivial_usage
|
|
|
|
//usage: "[-a|k|s|u] [-C TTY]"
|
|
|
|
//usage:#define kbd_mode_full_usage "\n\n"
|
2017-08-06 15:47:46 +05:30
|
|
|
//usage: "Report or set VT console keyboard mode\n"
|
2011-03-28 03:12:28 +05:30
|
|
|
//usage: "\n -a Default (ASCII)"
|
2017-08-06 15:28:46 +05:30
|
|
|
//usage: "\n -k Medium-raw (keycode)"
|
2011-03-28 03:12:28 +05:30
|
|
|
//usage: "\n -s Raw (scancode)"
|
|
|
|
//usage: "\n -u Unicode (utf-8)"
|
2017-08-06 15:53:04 +05:30
|
|
|
//usage: "\n -C TTY Affect TTY"
|
2011-03-28 03:12:28 +05:30
|
|
|
|
2007-10-20 05:47:34 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
#include <linux/kd.h>
|
|
|
|
|
|
|
|
int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int kbd_mode_main(int argc UNUSED_PARAM, char **argv)
|
2007-10-20 05:47:34 +05:30
|
|
|
{
|
2008-05-19 13:48:50 +05:30
|
|
|
enum {
|
2008-08-06 05:02:27 +05:30
|
|
|
SCANCODE = (1 << 0),
|
2010-10-29 15:16:52 +05:30
|
|
|
ASCII = (1 << 1),
|
2008-08-06 05:02:27 +05:30
|
|
|
MEDIUMRAW = (1 << 2),
|
2010-10-29 15:16:52 +05:30
|
|
|
UNICODE = (1 << 3),
|
2008-05-19 13:48:50 +05:30
|
|
|
};
|
2008-11-09 03:09:06 +05:30
|
|
|
int fd;
|
|
|
|
unsigned opt;
|
2017-08-06 15:47:46 +05:30
|
|
|
const char *tty_name;
|
2008-11-09 03:09:06 +05:30
|
|
|
|
|
|
|
opt = getopt32(argv, "sakuC:", &tty_name);
|
2017-08-06 15:47:46 +05:30
|
|
|
if (opt & 0x10) {
|
|
|
|
opt &= 0xf; /* clear -C bit, see (*) */
|
|
|
|
fd = xopen_nonblocking(tty_name);
|
|
|
|
} else {
|
|
|
|
/* kbd-2.0.3 tries in sequence:
|
|
|
|
* fd#0, /dev/tty, /dev/tty0.
|
|
|
|
* get_console_fd_or_die: /dev/console, /dev/tty0, /dev/tty.
|
|
|
|
* kbd-2.0.3 checks KDGKBTYPE, get_console_fd_or_die checks too.
|
|
|
|
*/
|
|
|
|
fd = get_console_fd_or_die();
|
|
|
|
}
|
2008-08-06 05:02:27 +05:30
|
|
|
|
2008-05-19 13:48:50 +05:30
|
|
|
if (!opt) { /* print current setting */
|
|
|
|
const char *mode = "unknown";
|
|
|
|
int m;
|
2007-10-20 05:47:34 +05:30
|
|
|
|
2008-11-09 03:12:14 +05:30
|
|
|
xioctl(fd, KDGKBMODE, &m);
|
2008-05-19 13:48:50 +05:30
|
|
|
if (m == K_RAW)
|
|
|
|
mode = "raw (scancode)";
|
|
|
|
else if (m == K_XLATE)
|
|
|
|
mode = "default (ASCII)";
|
|
|
|
else if (m == K_MEDIUMRAW)
|
|
|
|
mode = "mediumraw (keycode)";
|
|
|
|
else if (m == K_UNICODE)
|
|
|
|
mode = "Unicode (UTF-8)";
|
2017-08-06 15:28:46 +05:30
|
|
|
else if (m == 4 /*K_OFF*/) /* kbd-2.0.3 does not show this mode, says "unknown" */
|
|
|
|
mode = "off";
|
2008-05-19 13:48:50 +05:30
|
|
|
printf("The keyboard is in %s mode\n", mode);
|
|
|
|
} else {
|
2017-08-06 15:28:46 +05:30
|
|
|
/* here we depend on specific bits assigned to options (*)
|
|
|
|
* KDSKBMODE constants have these values:
|
|
|
|
* #define K_RAW 0x00
|
|
|
|
* #define K_XLATE 0x01
|
|
|
|
* #define K_MEDIUMRAW 0x02
|
|
|
|
* #define K_UNICODE 0x03
|
|
|
|
* #define K_OFF 0x04
|
|
|
|
* (looks like "-ak" together would cause the same effect as -u)
|
|
|
|
*/
|
2008-05-19 13:48:50 +05:30
|
|
|
opt = opt & UNICODE ? 3 : opt >> 1;
|
2008-08-06 05:02:27 +05:30
|
|
|
/* double cast prevents warnings about widening conversion */
|
|
|
|
xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt);
|
2007-10-20 05:47:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(fd);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|