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 | * |
| 5 | * Copyright (C) 2007 Loïc Grenié <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 tarball for details. |
| 10 | * |
| 11 | */ |
| 12 | |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
| 14 | #include <linux/kd.h> |
| 15 | |
| 16 | int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 17 | int kbd_mode_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 18 | { |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 19 | int fd; |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 20 | unsigned opt; |
| 21 | enum { |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame^] | 22 | SCANCODE = (1 << 0), |
| 23 | ASCII = (1 << 1), |
| 24 | MEDIUMRAW = (1 << 2), |
| 25 | UNICODE = (1 << 3) |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 26 | }; |
| 27 | static const char KD_xxx[] ALIGN1 = "saku"; |
| 28 | opt = getopt32(argv, KD_xxx); |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame^] | 29 | fd = get_console_fd_or_die(); |
| 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 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 35 | ioctl(fd, KDGKBMODE, &m); |
| 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 { |
| 46 | opt = opt & UNICODE ? 3 : opt >> 1; |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame^] | 47 | /* double cast prevents warnings about widening conversion */ |
| 48 | xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt); |
Denis Vlasenko | aa7a888 | 2007-10-20 00:17:34 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | if (ENABLE_FEATURE_CLEAN_UP) |
| 52 | close(fd); |
| 53 | return EXIT_SUCCESS; |
| 54 | } |