lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 | |
| 12 | //usage:#define kbd_mode_trivial_usage |
| 13 | //usage: "[-a|k|s|u] [-C TTY]" |
| 14 | //usage:#define kbd_mode_full_usage "\n\n" |
| 15 | //usage: "Report or set the keyboard mode\n" |
| 16 | //usage: "\n -a Default (ASCII)" |
| 17 | //usage: "\n -k Medium-raw (keyboard)" |
| 18 | //usage: "\n -s Raw (scancode)" |
| 19 | //usage: "\n -u Unicode (utf-8)" |
| 20 | //usage: "\n -C TTY Affect TTY instead of /dev/tty" |
| 21 | |
| 22 | #include "libbb.h" |
| 23 | #include <linux/kd.h> |
| 24 | |
| 25 | int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 26 | int kbd_mode_main(int argc UNUSED_PARAM, char **argv) |
| 27 | { |
| 28 | enum { |
| 29 | SCANCODE = (1 << 0), |
| 30 | ASCII = (1 << 1), |
| 31 | MEDIUMRAW = (1 << 2), |
| 32 | UNICODE = (1 << 3), |
| 33 | }; |
| 34 | int fd; |
| 35 | unsigned opt; |
| 36 | const char *tty_name = CURRENT_TTY; |
| 37 | |
| 38 | opt = getopt32(argv, "sakuC:", &tty_name); |
| 39 | fd = xopen_nonblocking(tty_name); |
| 40 | opt &= 0xf; /* clear -C bit, see (*) */ |
| 41 | |
| 42 | if (!opt) { /* print current setting */ |
| 43 | const char *mode = "unknown"; |
| 44 | int m; |
| 45 | |
| 46 | xioctl(fd, KDGKBMODE, &m); |
| 47 | if (m == K_RAW) |
| 48 | mode = "raw (scancode)"; |
| 49 | else if (m == K_XLATE) |
| 50 | mode = "default (ASCII)"; |
| 51 | else if (m == K_MEDIUMRAW) |
| 52 | mode = "mediumraw (keycode)"; |
| 53 | else if (m == K_UNICODE) |
| 54 | mode = "Unicode (UTF-8)"; |
| 55 | printf("The keyboard is in %s mode\n", mode); |
| 56 | } else { |
| 57 | /* here we depend on specific bits assigned to options (*) */ |
| 58 | opt = opt & UNICODE ? 3 : opt >> 1; |
| 59 | /* double cast prevents warnings about widening conversion */ |
| 60 | xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt); |
| 61 | } |
| 62 | |
| 63 | if (ENABLE_FEATURE_CLEAN_UP) |
| 64 | close(fd); |
| 65 | return EXIT_SUCCESS; |
| 66 | } |