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 |
| 12 | //config: bool "kbd_mode" |
| 13 | //config: default y |
| 14 | //config: select PLATFORM_LINUX |
| 15 | //config: help |
| 16 | //config: This program reports and sets keyboard mode. |
| 17 | |
| 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 Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 21 | |
| 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 Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 26 | //usage: "\n -a Default (ASCII)" |
| 27 | //usage: "\n -k Medium-raw (keyboard)" |
| 28 | //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 Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 32 | #include "libbb.h" |
| 33 | #include <linux/kd.h> |
| 34 | |
| 35 | int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 36 | int kbd_mode_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 37 | { |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 38 | enum { |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 39 | SCANCODE = (1 << 0), |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 40 | ASCII = (1 << 1), |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 41 | MEDIUMRAW = (1 << 2), |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 42 | UNICODE = (1 << 3), |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 43 | }; |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 44 | int fd; |
| 45 | unsigned opt; |
| 46 | const char *tty_name = CURRENT_TTY; |
| 47 | |
| 48 | opt = getopt32(argv, "sakuC:", &tty_name); |
Bernhard Reutner-Fischer | a483087 | 2009-10-26 23:27:08 +0100 | [diff] [blame] | 49 | fd = xopen_nonblocking(tty_name); |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 50 | opt &= 0xf; /* clear -C bit, see (*) */ |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 51 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 52 | if (!opt) { /* print current setting */ |
| 53 | const char *mode = "unknown"; |
| 54 | int m; |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | 4febd91 | 2008-11-08 21:42:14 +0000 | [diff] [blame] | 56 | xioctl(fd, KDGKBMODE, &m); |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 57 | if (m == K_RAW) |
| 58 | mode = "raw (scancode)"; |
| 59 | else if (m == K_XLATE) |
| 60 | mode = "default (ASCII)"; |
| 61 | else if (m == K_MEDIUMRAW) |
| 62 | mode = "mediumraw (keycode)"; |
| 63 | else if (m == K_UNICODE) |
| 64 | mode = "Unicode (UTF-8)"; |
| 65 | printf("The keyboard is in %s mode\n", mode); |
| 66 | } else { |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 67 | /* here we depend on specific bits assigned to options (*) */ |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 68 | opt = opt & UNICODE ? 3 : opt >> 1; |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 69 | /* double cast prevents warnings about widening conversion */ |
| 70 | xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt); |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | if (ENABLE_FEATURE_CLEAN_UP) |
| 74 | close(fd); |
| 75 | return EXIT_SUCCESS; |
| 76 | } |