blob: da31af28d33cde71bf6b49eadc104ad1224ed3ce [file] [log] [blame]
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +00001/* vi: set sw=4 ts=4: */
2/*
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +00003 * Mini kbd_mode implementation for busybox
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +00004 *
Denis Vlasenkod4b71982008-09-03 21:54:46 +00005 * Copyright (C) 2007 Loic Grenie <loic.grenie@gmail.com>
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +00006 * written using Andries Brouwer <aeb@cwi.nl>'s kbd_mode from
7 * console-utils v0.2.3, licensed under GNU GPLv2
8 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000010 */
Denys Vlasenko6d932992016-11-23 10:39:27 +010011//config:config KBD_MODE
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020012//config: bool "kbd_mode (4 kb)"
Denys Vlasenko6d932992016-11-23 10:39:27 +010013//config: default y
14//config: select PLATFORM_LINUX
15//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: This program reports and sets keyboard mode.
Denys Vlasenko6d932992016-11-23 10:39:27 +010017
18//applet:IF_KBD_MODE(APPLET(kbd_mode, BB_DIR_BIN, BB_SUID_DROP))
19
20//kbuild:lib-$(CONFIG_KBD_MODE) += kbd_mode.o
Pere Orga55068c42011-03-27 23:42:28 +020021
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"
Pere Orga55068c42011-03-27 23:42:28 +020026//usage: "\n -a Default (ASCII)"
Denys Vlasenko6517bbc2017-08-06 11:58:46 +020027//usage: "\n -k Medium-raw (keycode)"
Pere Orga55068c42011-03-27 23:42:28 +020028//usage: "\n -s Raw (scancode)"
29//usage: "\n -u Unicode (utf-8)"
30//usage: "\n -C TTY Affect TTY instead of /dev/tty"
31
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000032#include "libbb.h"
33#include <linux/kd.h>
34
35int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000036int kbd_mode_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000037{
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000038 enum {
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000039 SCANCODE = (1 << 0),
Denys Vlasenkofb132e42010-10-29 11:46:52 +020040 ASCII = (1 << 1),
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000041 MEDIUMRAW = (1 << 2),
Denys Vlasenkofb132e42010-10-29 11:46:52 +020042 UNICODE = (1 << 3),
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000043 };
Denis Vlasenkodc700692008-11-08 21:39:06 +000044 int fd;
45 unsigned opt;
Denys Vlasenko6517bbc2017-08-06 11:58:46 +020046//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()?
Denis Vlasenkodc700692008-11-08 21:39:06 +000050 const char *tty_name = CURRENT_TTY;
51
52 opt = getopt32(argv, "sakuC:", &tty_name);
Bernhard Reutner-Fischera4830872009-10-26 23:27:08 +010053 fd = xopen_nonblocking(tty_name);
Denis Vlasenkodc700692008-11-08 21:39:06 +000054 opt &= 0xf; /* clear -C bit, see (*) */
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000055
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000056 if (!opt) { /* print current setting */
57 const char *mode = "unknown";
58 int m;
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000059
Denis Vlasenko4febd912008-11-08 21:42:14 +000060 xioctl(fd, KDGKBMODE, &m);
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000061 if (m == K_RAW)
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)";
Denys Vlasenko6517bbc2017-08-06 11:58:46 +020069 else if (m == 4 /*K_OFF*/) /* kbd-2.0.3 does not show this mode, says "unknown" */
70 mode = "off";
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000071 printf("The keyboard is in %s mode\n", mode);
72 } else {
Denys Vlasenko6517bbc2017-08-06 11:58:46 +020073 /* here we depend on specific bits assigned to options (*)
74 * KDSKBMODE constants have these values:
75 * #define K_RAW 0x00
76 * #define K_XLATE 0x01
77 * #define K_MEDIUMRAW 0x02
78 * #define K_UNICODE 0x03
79 * #define K_OFF 0x04
80 * (looks like "-ak" together would cause the same effect as -u)
81 */
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000082 opt = opt & UNICODE ? 3 : opt >> 1;
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000083 /* double cast prevents warnings about widening conversion */
84 xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt);
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000085 }
86
87 if (ENABLE_FEATURE_CLEAN_UP)
88 close(fd);
89 return EXIT_SUCCESS;
90}