]>
Commit | Line | Data |
---|---|---|
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 | |
26 | static const struct grub_arg_option options[] = | |
27 | { | |
77a79592 | 28 | {"shift", 's', 0, N_("Check Shift key."), 0, 0}, |
29 | {"ctrl", 'c', 0, N_("Check Control key."), 0, 0}, | |
30 | {"alt", 'a', 0, N_("Check Alt key."), 0, 0}, | |
4cbe67e5 | 31 | {0, 0, 0, 0, 0, 0} |
32 | }; | |
33 | ||
87fae34a VS |
34 | static int |
35 | grub_getkeystatus (void) | |
36 | { | |
37 | int status = 0; | |
38 | grub_term_input_t term; | |
39 | ||
40 | FOR_ACTIVE_TERM_INPUTS(term) | |
41 | { | |
42 | if (term->getkeystatus) | |
7a6459e1 | 43 | status |= term->getkeystatus (term); |
87fae34a VS |
44 | } |
45 | ||
46 | return status; | |
47 | } | |
4cbe67e5 | 48 | |
49 | static grub_err_t | |
50 | grub_cmd_keystatus (grub_extcmd_t cmd, | |
51 | int argc __attribute__ ((unused)), | |
52 | char **args __attribute__ ((unused))) | |
53 | { | |
54 | struct grub_arg_list *state = cmd->state; | |
55 | int expect_mods = 0; | |
56 | int mods; | |
57 | ||
58 | if (state[0].set) | |
59 | expect_mods |= GRUB_TERM_STATUS_SHIFT; | |
60 | if (state[1].set) | |
61 | expect_mods |= GRUB_TERM_STATUS_CTRL; | |
62 | if (state[2].set) | |
63 | expect_mods |= GRUB_TERM_STATUS_ALT; | |
64 | ||
8eca55a6 RM |
65 | grub_dprintf ("keystatus", "expect_mods: %d\n", expect_mods); |
66 | ||
4cbe67e5 | 67 | /* Without arguments, just check whether getkeystatus is supported at |
68 | all. */ | |
8eca55a6 RM |
69 | if (expect_mods == 0) |
70 | { | |
71 | grub_term_input_t term; | |
ca04c7a5 | 72 | int nterms = 0; |
8eca55a6 RM |
73 | |
74 | FOR_ACTIVE_TERM_INPUTS (term) | |
ca04c7a5 VS |
75 | if (!term->getkeystatus) |
76 | return grub_error (GRUB_ERR_TEST_FAILURE, "false"); | |
77 | else | |
78 | nterms++; | |
79 | if (!nterms) | |
80 | return grub_error (GRUB_ERR_TEST_FAILURE, "false"); | |
81 | return 0; | |
8eca55a6 | 82 | } |
4cbe67e5 | 83 | |
84 | mods = grub_getkeystatus (); | |
85 | grub_dprintf ("keystatus", "mods: %d\n", mods); | |
86 | if (mods >= 0 && (mods & expect_mods) != 0) | |
87 | return 0; | |
88 | else | |
89 | return grub_error (GRUB_ERR_TEST_FAILURE, "false"); | |
90 | } | |
91 | ||
92 | static grub_extcmd_t cmd; | |
93 | \f | |
94 | GRUB_MOD_INIT(keystatus) | |
95 | { | |
96 | cmd = grub_register_extcmd ("keystatus", grub_cmd_keystatus, | |
97 | GRUB_COMMAND_FLAG_BOTH, | |
77a79592 | 98 | N_("[--shift] [--ctrl] [--alt]"), |
99 | N_("Check key modifier status."), | |
4cbe67e5 | 100 | options); |
101 | } | |
102 | ||
103 | GRUB_MOD_FINI(keystatus) | |
104 | { | |
105 | grub_unregister_extcmd (cmd); | |
106 | } |