busybox/console-tools/kbd_mode.c
<<
>>
Prefs
   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#include "libbb.h"
  12#include <linux/kd.h>
  13
  14int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  15int kbd_mode_main(int argc UNUSED_PARAM, char **argv)
  16{
  17        enum {
  18                SCANCODE  = (1 << 0),
  19                ASCII     = (1 << 1),
  20                MEDIUMRAW = (1 << 2),
  21                UNICODE   = (1 << 3),
  22        };
  23        int fd;
  24        unsigned opt;
  25        const char *tty_name = CURRENT_TTY;
  26
  27        opt = getopt32(argv, "sakuC:", &tty_name);
  28        fd = xopen_nonblocking(tty_name);
  29        opt &= 0xf; /* clear -C bit, see (*) */
  30
  31        if (!opt) { /* print current setting */
  32                const char *mode = "unknown";
  33                int m;
  34
  35                xioctl(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                /* here we depend on specific bits assigned to options (*) */
  47                opt = opt & UNICODE ? 3 : opt >> 1;
  48                /* double cast prevents warnings about widening conversion */
  49                xioctl(fd, KDSKBMODE, (void*)(ptrdiff_t)opt);
  50        }
  51
  52        if (ENABLE_FEATURE_CLEAN_UP)
  53                close(fd);
  54        return EXIT_SUCCESS;
  55}
  56