1 /* keystatus.c - Command to check key modifier status. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/misc.h>
22 #include <grub/extcmd.h>
23 #include <grub/term.h>
24 #include <grub/i18n.h>
26 GRUB_MOD_LICENSE ("GPLv3+");
28 static const struct grub_arg_option options
[] =
30 /* TRANSLATORS: "Check" in a sense that if this key is pressed then
31 "true" is returned, otherwise "false". */
32 {"shift", 's', 0, N_("Check Shift key."), 0, 0},
33 {"ctrl", 'c', 0, N_("Check Control key."), 0, 0},
34 {"alt", 'a', 0, N_("Check Alt key."), 0, 0},
39 grub_getkeystatus (void)
42 grub_term_input_t term
;
44 if (grub_term_poll_usb
)
45 grub_term_poll_usb (0);
47 FOR_ACTIVE_TERM_INPUTS(term
)
49 if (term
->getkeystatus
)
50 status
|= term
->getkeystatus (term
);
57 grub_cmd_keystatus (grub_extcmd_context_t ctxt
,
58 int argc
__attribute__ ((unused
)),
59 char **args
__attribute__ ((unused
)))
61 struct grub_arg_list
*state
= ctxt
->state
;
66 expect_mods
|= (GRUB_TERM_STATUS_LSHIFT
| GRUB_TERM_STATUS_RSHIFT
);
68 expect_mods
|= (GRUB_TERM_STATUS_LCTRL
| GRUB_TERM_STATUS_RCTRL
);
70 expect_mods
|= (GRUB_TERM_STATUS_LALT
| GRUB_TERM_STATUS_RALT
);
72 grub_dprintf ("keystatus", "expect_mods: %d\n", expect_mods
);
74 /* Without arguments, just check whether getkeystatus is supported at
78 grub_term_input_t term
;
81 FOR_ACTIVE_TERM_INPUTS (term
)
82 if (!term
->getkeystatus
)
83 return grub_error (GRUB_ERR_TEST_FAILURE
, N_("false"));
87 return grub_error (GRUB_ERR_TEST_FAILURE
, N_("false"));
91 mods
= grub_getkeystatus ();
92 grub_dprintf ("keystatus", "mods: %d\n", mods
);
93 if (mods
>= 0 && (mods
& expect_mods
) != 0)
96 return grub_error (GRUB_ERR_TEST_FAILURE
, N_("false"));
99 static grub_extcmd_t cmd
;
101 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."),
110 GRUB_MOD_FINI(keystatus
)
112 grub_unregister_extcmd (cmd
);