2N/A/* keystatus.c - Command to check key modifier status. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/dl.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/extcmd.h>
2N/A#include <grub/term.h>
2N/A#include <grub/i18n.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astatic const struct grub_arg_option options[] =
2N/A {
2N/A {"shift", 's', 0, N_("Check Shift key."), 0, 0},
2N/A {"ctrl", 'c', 0, N_("Check Control key."), 0, 0},
2N/A {"alt", 'a', 0, N_("Check Alt key."), 0, 0},
2N/A {0, 0, 0, 0, 0, 0}
2N/A };
2N/A
2N/Astatic int
2N/Agrub_getkeystatus (void)
2N/A{
2N/A int status = 0;
2N/A grub_term_input_t term;
2N/A
2N/A if (grub_term_poll_usb)
2N/A grub_term_poll_usb ();
2N/A
2N/A FOR_ACTIVE_TERM_INPUTS(term)
2N/A {
2N/A if (term->getkeystatus)
2N/A status |= term->getkeystatus (term);
2N/A }
2N/A
2N/A return status;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_cmd_keystatus (grub_extcmd_context_t ctxt,
2N/A int argc __attribute__ ((unused)),
2N/A char **args __attribute__ ((unused)))
2N/A{
2N/A struct grub_arg_list *state = ctxt->state;
2N/A int expect_mods = 0;
2N/A int mods;
2N/A
2N/A if (state[0].set)
2N/A expect_mods |= (GRUB_TERM_STATUS_LSHIFT | GRUB_TERM_STATUS_RSHIFT);
2N/A if (state[1].set)
2N/A expect_mods |= (GRUB_TERM_STATUS_LCTRL | GRUB_TERM_STATUS_RCTRL);
2N/A if (state[2].set)
2N/A expect_mods |= (GRUB_TERM_STATUS_LALT | GRUB_TERM_STATUS_RALT);
2N/A
2N/A grub_dprintf ("keystatus", "expect_mods: %d\n", expect_mods);
2N/A
2N/A /* Without arguments, just check whether getkeystatus is supported at
2N/A all. */
2N/A if (expect_mods == 0)
2N/A {
2N/A grub_term_input_t term;
2N/A int nterms = 0;
2N/A
2N/A FOR_ACTIVE_TERM_INPUTS (term)
2N/A if (!term->getkeystatus)
2N/A return grub_error (GRUB_ERR_TEST_FAILURE, "false");
2N/A else
2N/A nterms++;
2N/A if (!nterms)
2N/A return grub_error (GRUB_ERR_TEST_FAILURE, "false");
2N/A return 0;
2N/A }
2N/A
2N/A mods = grub_getkeystatus ();
2N/A grub_dprintf ("keystatus", "mods: %d\n", mods);
2N/A if (mods >= 0 && (mods & expect_mods) != 0)
2N/A return 0;
2N/A else
2N/A return grub_error (GRUB_ERR_TEST_FAILURE, "false");
2N/A}
2N/A
2N/Astatic grub_extcmd_t cmd;
2N/A
2N/AGRUB_MOD_INIT(keystatus)
2N/A{
2N/A cmd = grub_register_extcmd ("keystatus", grub_cmd_keystatus, 0,
2N/A N_("[--shift] [--ctrl] [--alt]"),
2N/A N_("Check key modifier status."),
2N/A options);
2N/A}
2N/A
2N/AGRUB_MOD_FINI(keystatus)
2N/A{
2N/A grub_unregister_extcmd (cmd);
2N/A}