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 | */ |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
| 12 | #include <linux/kd.h> |
| 13 | |
| 14 | int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 15 | int kbd_mode_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 16 | { |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 17 | enum { |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 18 | SCANCODE = (1 << 0), |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame^] | 19 | ASCII = (1 << 1), |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 20 | MEDIUMRAW = (1 << 2), |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame^] | 21 | UNICODE = (1 << 3), |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 22 | }; |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 23 | int fd; |
| 24 | unsigned opt; |
| 25 | const char *tty_name = CURRENT_TTY; |
| 26 | |
| 27 | opt = getopt32(argv, "sakuC:", &tty_name); |
Bernhard Reutner-Fischer | a483087 | 2009-10-26 23:27:08 +0100 | [diff] [blame] | 28 | fd = xopen_nonblocking(tty_name); |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 29 | opt &= 0xf; /* clear -C bit, see (*) */ |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 30 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 31 | if (!opt) { /* print current setting */ |
| 32 | const char *mode = "unknown"; |
| 33 | int m; |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 34 | |
Denis Vlasenko | 4febd91 | 2008-11-08 21:42:14 +0000 | [diff] [blame] | 35 | xioctl(fd, KDGKBMODE, &m); |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 36 | if (m == K_RAW) |
| 37 | mode = "raw (scancode)"; |
| 38 | else if (m == K_XLATE) |
| 39 | mode = "default (ASCII)"; |
| 40 | else if (m == K_MEDIUMRAW) |
| 41 | mode = "mediumraw (keycode)"; |
| 42 | else if (m == K_UNICODE) |
| 43 | mode = "Unicode (UTF-8)"; |
| 44 | printf("The keyboard is in %s mode\n", mode); |
| 45 | } else { |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 46 | /* here we depend on specific bits assigned to options (*) */ |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 47 | opt = opt & UNICODE ? 3 : opt >> 1; |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 48 | /* double cast prevents warnings about widening conversion */ |
| 49 | xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt); |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | if (ENABLE_FEATURE_CLEAN_UP) |
| 53 | close(fd); |
| 54 | return EXIT_SUCCESS; |
| 55 | } |