blob: 13853672129d0837cdd44cc74091eb79c994f045 [file] [log] [blame]
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +00001/* vi: set sw=4 ts=4: */
2/*
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +00003 * Mini kbd_mode implementation for busybox
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +00004 *
Denis Vlasenkod4b71982008-09-03 21:54:46 +00005 * Copyright (C) 2007 Loic Grenie <loic.grenie@gmail.com>
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +00006 * written using Andries Brouwer <aeb@cwi.nl>'s kbd_mode from
7 * console-utils v0.2.3, licensed under GNU GPLv2
8 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000010 */
Pere Orga55068c42011-03-27 23:42:28 +020011
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"
Pere Orga55068c42011-03-27 23:42:28 +020016//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
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000022#include "libbb.h"
23#include <linux/kd.h>
24
25int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000026int kbd_mode_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000027{
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000028 enum {
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000029 SCANCODE = (1 << 0),
Denys Vlasenkofb132e42010-10-29 11:46:52 +020030 ASCII = (1 << 1),
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000031 MEDIUMRAW = (1 << 2),
Denys Vlasenkofb132e42010-10-29 11:46:52 +020032 UNICODE = (1 << 3),
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000033 };
Denis Vlasenkodc700692008-11-08 21:39:06 +000034 int fd;
35 unsigned opt;
36 const char *tty_name = CURRENT_TTY;
37
38 opt = getopt32(argv, "sakuC:", &tty_name);
Bernhard Reutner-Fischera4830872009-10-26 23:27:08 +010039 fd = xopen_nonblocking(tty_name);
Denis Vlasenkodc700692008-11-08 21:39:06 +000040 opt &= 0xf; /* clear -C bit, see (*) */
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000041
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000042 if (!opt) { /* print current setting */
43 const char *mode = "unknown";
44 int m;
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000045
Denis Vlasenko4febd912008-11-08 21:42:14 +000046 xioctl(fd, KDGKBMODE, &m);
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000047 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 {
Denis Vlasenkodc700692008-11-08 21:39:06 +000057 /* here we depend on specific bits assigned to options (*) */
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000058 opt = opt & UNICODE ? 3 : opt >> 1;
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000059 /* double cast prevents warnings about widening conversion */
60 xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt);
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000061 }
62
63 if (ENABLE_FEATURE_CLEAN_UP)
64 close(fd);
65 return EXIT_SUCCESS;
66}