blob: 1481d0dbb193a6cfed2212a95bc41c60c1f4e493 [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 */
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000011#include "libbb.h"
12#include <linux/kd.h>
13
14int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000015int kbd_mode_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000016{
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000017 enum {
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000018 SCANCODE = (1 << 0),
Denys Vlasenkofb132e42010-10-29 11:46:52 +020019 ASCII = (1 << 1),
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000020 MEDIUMRAW = (1 << 2),
Denys Vlasenkofb132e42010-10-29 11:46:52 +020021 UNICODE = (1 << 3),
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000022 };
Denis Vlasenkodc700692008-11-08 21:39:06 +000023 int fd;
24 unsigned opt;
25 const char *tty_name = CURRENT_TTY;
26
27 opt = getopt32(argv, "sakuC:", &tty_name);
Bernhard Reutner-Fischera4830872009-10-26 23:27:08 +010028 fd = xopen_nonblocking(tty_name);
Denis Vlasenkodc700692008-11-08 21:39:06 +000029 opt &= 0xf; /* clear -C bit, see (*) */
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000030
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000031 if (!opt) { /* print current setting */
32 const char *mode = "unknown";
33 int m;
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000034
Denis Vlasenko4febd912008-11-08 21:42:14 +000035 xioctl(fd, KDGKBMODE, &m);
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000036 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 Vlasenkodc700692008-11-08 21:39:06 +000046 /* here we depend on specific bits assigned to options (*) */
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000047 opt = opt & UNICODE ? 3 : opt >> 1;
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000048 /* double cast prevents warnings about widening conversion */
49 xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt);
Denis Vlasenkoaa7a8882007-10-20 00:17:34 +000050 }
51
52 if (ENABLE_FEATURE_CLEAN_UP)
53 close(fd);
54 return EXIT_SUCCESS;
55}