2009-11-21 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2.git] / commands / keystatus.c
blob28ceb2d0bde22caa47e4204b95e7a87f4bc146c4
1 /* keystatus.c - Command to check key modifier status. */
2 /*
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/>.
20 #include <grub/dl.h>
21 #include <grub/misc.h>
22 #include <grub/extcmd.h>
23 #include <grub/term.h>
25 static const struct grub_arg_option options[] =
27 {"shift", 's', 0, "check Shift key", 0, 0},
28 {"ctrl", 'c', 0, "check Control key", 0, 0},
29 {"alt", 'a', 0, "check Alt key", 0, 0},
30 {0, 0, 0, 0, 0, 0}
33 #define grub_cur_term_input grub_term_get_current_input ()
35 static grub_err_t
36 grub_cmd_keystatus (grub_extcmd_t cmd,
37 int argc __attribute__ ((unused)),
38 char **args __attribute__ ((unused)))
40 struct grub_arg_list *state = cmd->state;
41 int expect_mods = 0;
42 int mods;
44 if (state[0].set)
45 expect_mods |= GRUB_TERM_STATUS_SHIFT;
46 if (state[1].set)
47 expect_mods |= GRUB_TERM_STATUS_CTRL;
48 if (state[2].set)
49 expect_mods |= GRUB_TERM_STATUS_ALT;
51 /* Without arguments, just check whether getkeystatus is supported at
52 all. */
53 if (!grub_cur_term_input->getkeystatus)
54 return grub_error (GRUB_ERR_TEST_FAILURE, "false");
55 grub_dprintf ("keystatus", "expect_mods: %d\n", expect_mods);
56 if (!expect_mods)
57 return 0;
59 mods = grub_getkeystatus ();
60 grub_dprintf ("keystatus", "mods: %d\n", mods);
61 if (mods >= 0 && (mods & expect_mods) != 0)
62 return 0;
63 else
64 return grub_error (GRUB_ERR_TEST_FAILURE, "false");
67 static grub_extcmd_t cmd;
69 GRUB_MOD_INIT(keystatus)
71 cmd = grub_register_extcmd ("keystatus", grub_cmd_keystatus,
72 GRUB_COMMAND_FLAG_BOTH,
73 "keystatus [--shift] [--ctrl] [--alt]",
74 "Check key modifier status",
75 options);
78 GRUB_MOD_FINI(keystatus)
80 grub_unregister_extcmd (cmd);