1/* vi: set sw=4 ts=4: */ 2/* 3 * Mini kbd_mode implementation for busybox 4 * 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 8 * 9 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 10 */ 11//config:config KBD_MODE 12//config: bool "kbd_mode (4.1 kb)" 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_NOEXEC(kbd_mode, kbd_mode, BB_DIR_BIN, BB_SUID_DROP, kbd_mode)) 19 20//kbuild:lib-$(CONFIG_KBD_MODE) += kbd_mode.o 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 VT console 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" 31 32#include "libbb.h" 33#include <linux/kd.h> 34 35int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 36int kbd_mode_main(int argc UNUSED_PARAM, char **argv) 37{ 38 enum { 39 SCANCODE = (1 << 0), 40 ASCII = (1 << 1), 41 MEDIUMRAW = (1 << 2), 42 UNICODE = (1 << 3), 43 }; 44 int fd; 45 unsigned opt; 46 const char *tty_name; 47 48 opt = getopt32(argv, "sakuC:", &tty_name); 49 if (opt & 0x10) { 50 opt &= 0xf; /* clear -C bit, see (*) */ 51 fd = xopen_nonblocking(tty_name); 52 } else { 53 /* kbd-2.0.3 tries in sequence: 54 * fd#0, /dev/tty, /dev/tty0. 55 * get_console_fd_or_die: /dev/console, /dev/tty0, /dev/tty. 56 * kbd-2.0.3 checks KDGKBTYPE, get_console_fd_or_die checks too. 57 */ 58 fd = get_console_fd_or_die(); 59 } 60 61 if (!opt) { /* print current setting */ 62 const char *mode = "unknown"; 63 int m; 64 65 xioctl(fd, KDGKBMODE, &m); 66 if (m == K_RAW) 67 mode = "raw (scancode)"; 68 else if (m == K_XLATE) 69 mode = "default (ASCII)"; 70 else if (m == K_MEDIUMRAW) 71 mode = "mediumraw (keycode)"; 72 else if (m == K_UNICODE) 73 mode = "Unicode (UTF-8)"; 74 else if (m == 4 /*K_OFF*/) /* kbd-2.0.3 does not show this mode, says "unknown" */ 75 mode = "off"; 76 printf("The keyboard is in %s mode\n", mode); 77 } else { 78 /* here we depend on specific bits assigned to options (*) 79 * KDSKBMODE constants have these values: 80 * #define K_RAW 0x00 81 * #define K_XLATE 0x01 82 * #define K_MEDIUMRAW 0x02 83 * #define K_UNICODE 0x03 84 * #define K_OFF 0x04 85 * (looks like "-ak" together would cause the same effect as -u) 86 */ 87 opt = opt & UNICODE ? 3 : opt >> 1; 88 /* double cast prevents warnings about widening conversion */ 89 xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt); 90 } 91 92 if (ENABLE_FEATURE_CLEAN_UP) 93 close(fd); 94 return EXIT_SUCCESS; 95} 96