keystatus.c 3.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* keystatus.c - Command to check key modifier status.  */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2009  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/extcmd.h>
#include <grub/term.h>
24
#include <grub/i18n.h>
25

26 27
GRUB_MOD_LICENSE ("GPLv3+");

28 29
static const struct grub_arg_option options[] =
  {
30 31
    /* TRANSLATORS: "Check" in a sense that if this key is pressed then
       "true" is returned, otherwise "false".  */
32 33 34
    {"shift", 's', 0, N_("Check Shift key."), 0, 0},
    {"ctrl", 'c', 0, N_("Check Control key."), 0, 0},
    {"alt", 'a', 0, N_("Check Alt key."), 0, 0},
35 36 37
    {0, 0, 0, 0, 0, 0}
  };

38 39 40 41 42 43
static int
grub_getkeystatus (void)
{
  int status = 0;
  grub_term_input_t term;

44
  if (grub_term_poll_usb)
45
    grub_term_poll_usb (0);
46

47 48 49
  FOR_ACTIVE_TERM_INPUTS(term)
  {
    if (term->getkeystatus)
50
      status |= term->getkeystatus (term);
51 52 53 54
  }

  return status;
}
55 56

static grub_err_t
57
grub_cmd_keystatus (grub_extcmd_context_t ctxt,
58 59 60
		    int argc __attribute__ ((unused)),
		    char **args __attribute__ ((unused)))
{
61
  struct grub_arg_list *state = ctxt->state;
62 63 64 65
  int expect_mods = 0;
  int mods;

  if (state[0].set)
66
    expect_mods |= (GRUB_TERM_STATUS_LSHIFT | GRUB_TERM_STATUS_RSHIFT);
67
  if (state[1].set)
68
    expect_mods |= (GRUB_TERM_STATUS_LCTRL | GRUB_TERM_STATUS_RCTRL);
69
  if (state[2].set)
70
    expect_mods |= (GRUB_TERM_STATUS_LALT | GRUB_TERM_STATUS_RALT);
71

72 73
  grub_dprintf ("keystatus", "expect_mods: %d\n", expect_mods);

74 75
  /* Without arguments, just check whether getkeystatus is supported at
     all.  */
76 77 78
  if (expect_mods == 0)
    {
      grub_term_input_t term;
79
      int nterms = 0;
80 81

      FOR_ACTIVE_TERM_INPUTS (term)
82
	if (!term->getkeystatus)
83
	  return grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
84 85 86
	else
	  nterms++;
      if (!nterms)
87
	return grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
88
      return 0;
89
    }
90 91 92 93 94 95

  mods = grub_getkeystatus ();
  grub_dprintf ("keystatus", "mods: %d\n", mods);
  if (mods >= 0 && (mods & expect_mods) != 0)
    return 0;
  else
96
    return grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
97 98 99 100 101 102
}

static grub_extcmd_t cmd;

GRUB_MOD_INIT(keystatus)
{
103
  cmd = grub_register_extcmd ("keystatus", grub_cmd_keystatus, 0,
104
			      "[--shift] [--ctrl] [--alt]",
105
			      /* TRANSLATORS: there are 3 modifiers.  */
106
			      N_("Check key modifier status."),
107 108 109 110 111 112 113
			      options);
}

GRUB_MOD_FINI(keystatus)
{
  grub_unregister_extcmd (cmd);
}