]> git.proxmox.com Git - grub2.git/blame - grub-core/commands/keystatus.c
Improve and unify messages.
[grub2.git] / grub-core / commands / keystatus.c
CommitLineData
4cbe67e5 1/* keystatus.c - Command to check key modifier status. */
2/*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <grub/dl.h>
21#include <grub/misc.h>
22#include <grub/extcmd.h>
23#include <grub/term.h>
77a79592 24#include <grub/i18n.h>
4cbe67e5 25
e745cf0c
VS
26GRUB_MOD_LICENSE ("GPLv3+");
27
4cbe67e5 28static const struct grub_arg_option options[] =
29 {
77a79592 30 {"shift", 's', 0, N_("Check Shift key."), 0, 0},
31 {"ctrl", 'c', 0, N_("Check Control key."), 0, 0},
32 {"alt", 'a', 0, N_("Check Alt key."), 0, 0},
4cbe67e5 33 {0, 0, 0, 0, 0, 0}
34 };
35
87fae34a
VS
36static int
37grub_getkeystatus (void)
38{
39 int status = 0;
40 grub_term_input_t term;
41
fea90138
VS
42 if (grub_term_poll_usb)
43 grub_term_poll_usb ();
44
87fae34a
VS
45 FOR_ACTIVE_TERM_INPUTS(term)
46 {
47 if (term->getkeystatus)
7a6459e1 48 status |= term->getkeystatus (term);
87fae34a
VS
49 }
50
51 return status;
52}
4cbe67e5 53
54static grub_err_t
28be0e94 55grub_cmd_keystatus (grub_extcmd_context_t ctxt,
4cbe67e5 56 int argc __attribute__ ((unused)),
57 char **args __attribute__ ((unused)))
58{
28be0e94 59 struct grub_arg_list *state = ctxt->state;
4cbe67e5 60 int expect_mods = 0;
61 int mods;
62
63 if (state[0].set)
e55e0962 64 expect_mods |= (GRUB_TERM_STATUS_LSHIFT | GRUB_TERM_STATUS_RSHIFT);
4cbe67e5 65 if (state[1].set)
e55e0962 66 expect_mods |= (GRUB_TERM_STATUS_LCTRL | GRUB_TERM_STATUS_RCTRL);
4cbe67e5 67 if (state[2].set)
e55e0962 68 expect_mods |= (GRUB_TERM_STATUS_LALT | GRUB_TERM_STATUS_RALT);
4cbe67e5 69
8eca55a6
RM
70 grub_dprintf ("keystatus", "expect_mods: %d\n", expect_mods);
71
4cbe67e5 72 /* Without arguments, just check whether getkeystatus is supported at
73 all. */
8eca55a6
RM
74 if (expect_mods == 0)
75 {
76 grub_term_input_t term;
ca04c7a5 77 int nterms = 0;
8eca55a6
RM
78
79 FOR_ACTIVE_TERM_INPUTS (term)
ca04c7a5
VS
80 if (!term->getkeystatus)
81 return grub_error (GRUB_ERR_TEST_FAILURE, "false");
82 else
83 nterms++;
84 if (!nterms)
85 return grub_error (GRUB_ERR_TEST_FAILURE, "false");
86 return 0;
8eca55a6 87 }
4cbe67e5 88
89 mods = grub_getkeystatus ();
90 grub_dprintf ("keystatus", "mods: %d\n", mods);
91 if (mods >= 0 && (mods & expect_mods) != 0)
92 return 0;
93 else
94 return grub_error (GRUB_ERR_TEST_FAILURE, "false");
95}
96
97static grub_extcmd_t cmd;
98\f
99GRUB_MOD_INIT(keystatus)
100{
ed80f7d5 101 cmd = grub_register_extcmd ("keystatus", grub_cmd_keystatus, 0,
77a79592 102 N_("[--shift] [--ctrl] [--alt]"),
103 N_("Check key modifier status."),
4cbe67e5 104 options);
105}
106
107GRUB_MOD_FINI(keystatus)
108{
109 grub_unregister_extcmd (cmd);
110}