Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 3 | * Mini kbd_mode implementation for busybox |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 4 | * |
Denis Vlasenko | d4b7198 | 2008-09-03 21:54:46 +0000 | [diff] [blame] | 5 | * Copyright (C) 2007 Loic Grenie <loic.grenie@gmail.com> |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 6 | * written using Andries Brouwer <aeb@cwi.nl>'s kbd_mode from |
| 7 | * console-utils v0.2.3, licensed under GNU GPLv2 |
| 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 10 | */ |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 11 | //config:config KBD_MODE |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 12 | //config: bool "kbd_mode (4.1 kb)" |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 13 | //config: default y |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 14 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 15 | //config: This program reports and sets keyboard mode. |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 16 | |
Denys Vlasenko | ae17ba0 | 2017-08-06 12:23:04 +0200 | [diff] [blame] | 17 | //applet:IF_KBD_MODE(APPLET_NOEXEC(kbd_mode, kbd_mode, BB_DIR_BIN, BB_SUID_DROP, kbd_mode)) |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 18 | |
| 19 | //kbuild:lib-$(CONFIG_KBD_MODE) += kbd_mode.o |
Pere Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 20 | |
| 21 | //usage:#define kbd_mode_trivial_usage |
| 22 | //usage: "[-a|k|s|u] [-C TTY]" |
| 23 | //usage:#define kbd_mode_full_usage "\n\n" |
Denys Vlasenko | 32b60cc | 2017-08-06 12:17:46 +0200 | [diff] [blame] | 24 | //usage: "Report or set VT console keyboard mode\n" |
Pere Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 25 | //usage: "\n -a Default (ASCII)" |
Denys Vlasenko | 6517bbc | 2017-08-06 11:58:46 +0200 | [diff] [blame] | 26 | //usage: "\n -k Medium-raw (keycode)" |
Pere Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 27 | //usage: "\n -s Raw (scancode)" |
| 28 | //usage: "\n -u Unicode (utf-8)" |
Denys Vlasenko | ae17ba0 | 2017-08-06 12:23:04 +0200 | [diff] [blame] | 29 | //usage: "\n -C TTY Affect TTY" |
Pere Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 30 | |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 31 | #include "libbb.h" |
| 32 | #include <linux/kd.h> |
| 33 | |
| 34 | int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 35 | int kbd_mode_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 36 | { |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 37 | enum { |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 38 | SCANCODE = (1 << 0), |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 39 | ASCII = (1 << 1), |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 40 | MEDIUMRAW = (1 << 2), |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 41 | UNICODE = (1 << 3), |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 42 | }; |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 43 | int fd; |
| 44 | unsigned opt; |
Denys Vlasenko | 32b60cc | 2017-08-06 12:17:46 +0200 | [diff] [blame] | 45 | const char *tty_name; |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 46 | |
| 47 | opt = getopt32(argv, "sakuC:", &tty_name); |
Denys Vlasenko | 32b60cc | 2017-08-06 12:17:46 +0200 | [diff] [blame] | 48 | if (opt & 0x10) { |
| 49 | opt &= 0xf; /* clear -C bit, see (*) */ |
| 50 | fd = xopen_nonblocking(tty_name); |
| 51 | } else { |
| 52 | /* kbd-2.0.3 tries in sequence: |
| 53 | * fd#0, /dev/tty, /dev/tty0. |
| 54 | * get_console_fd_or_die: /dev/console, /dev/tty0, /dev/tty. |
| 55 | * kbd-2.0.3 checks KDGKBTYPE, get_console_fd_or_die checks too. |
| 56 | */ |
| 57 | fd = get_console_fd_or_die(); |
| 58 | } |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 59 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 60 | if (!opt) { /* print current setting */ |
| 61 | const char *mode = "unknown"; |
| 62 | int m; |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 4febd91 | 2008-11-08 21:42:14 +0000 | [diff] [blame] | 64 | xioctl(fd, KDGKBMODE, &m); |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 65 | if (m == K_RAW) |
| 66 | mode = "raw (scancode)"; |
| 67 | else if (m == K_XLATE) |
| 68 | mode = "default (ASCII)"; |
| 69 | else if (m == K_MEDIUMRAW) |
| 70 | mode = "mediumraw (keycode)"; |
| 71 | else if (m == K_UNICODE) |
| 72 | mode = "Unicode (UTF-8)"; |
Denys Vlasenko | 6517bbc | 2017-08-06 11:58:46 +0200 | [diff] [blame] | 73 | else if (m == 4 /*K_OFF*/) /* kbd-2.0.3 does not show this mode, says "unknown" */ |
| 74 | mode = "off"; |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 75 | printf("The keyboard is in %s mode\n", mode); |
| 76 | } else { |
Denys Vlasenko | 6517bbc | 2017-08-06 11:58:46 +0200 | [diff] [blame] | 77 | /* here we depend on specific bits assigned to options (*) |
| 78 | * KDSKBMODE constants have these values: |
| 79 | * #define K_RAW 0x00 |
| 80 | * #define K_XLATE 0x01 |
| 81 | * #define K_MEDIUMRAW 0x02 |
| 82 | * #define K_UNICODE 0x03 |
| 83 | * #define K_OFF 0x04 |
| 84 | * (looks like "-ak" together would cause the same effect as -u) |
| 85 | */ |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 86 | opt = opt & UNICODE ? 3 : opt >> 1; |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 87 | /* double cast prevents warnings about widening conversion */ |
| 88 | xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt); |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (ENABLE_FEATURE_CLEAN_UP) |
| 92 | close(fd); |
| 93 | return EXIT_SUCCESS; |
| 94 | } |