]>
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 | |
e745cf0c VS |
26 | GRUB_MOD_LICENSE ("GPLv3+"); |
27 | ||
4cbe67e5 | 28 | static const struct grub_arg_option options[] = |
29 | { | |
40211ab8 VS |
30 | /* TRANSLATORS: "Check" in a sense that if this key is pressed then |
31 | "true" is returned, otherwise "false". */ | |
77a79592 | 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}, | |
4cbe67e5 | 35 | {0, 0, 0, 0, 0, 0} |
36 | }; | |
37 | ||
87fae34a VS |
38 | static int |
39 | grub_getkeystatus (void) | |
40 | { | |
41 | int status = 0; | |
42 | grub_term_input_t term; | |
43 | ||
fea90138 | 44 | if (grub_term_poll_usb) |
d745dda7 | 45 | grub_term_poll_usb (0); |
fea90138 | 46 | |
87fae34a VS |
47 | FOR_ACTIVE_TERM_INPUTS(term) |
48 | { | |
49 | if (term->getkeystatus) | |
7a6459e1 | 50 | status |= term->getkeystatus (term); |
87fae34a VS |
51 | } |
52 | ||
53 | return status; | |
54 | } | |
4cbe67e5 | 55 | |
56 | static grub_err_t | |
28be0e94 | 57 | grub_cmd_keystatus (grub_extcmd_context_t ctxt, |
4cbe67e5 | 58 | int argc __attribute__ ((unused)), |
59 | char **args __attribute__ ((unused))) | |
60 | { | |
28be0e94 | 61 | struct grub_arg_list *state = ctxt->state; |
4cbe67e5 | 62 | int expect_mods = 0; |
63 | int mods; | |
64 | ||
65 | if (state[0].set) | |
e55e0962 | 66 | expect_mods |= (GRUB_TERM_STATUS_LSHIFT | GRUB_TERM_STATUS_RSHIFT); |
4cbe67e5 | 67 | if (state[1].set) |
e55e0962 | 68 | expect_mods |= (GRUB_TERM_STATUS_LCTRL | GRUB_TERM_STATUS_RCTRL); |
4cbe67e5 | 69 | if (state[2].set) |
e55e0962 | 70 | expect_mods |= (GRUB_TERM_STATUS_LALT | GRUB_TERM_STATUS_RALT); |
4cbe67e5 | 71 | |
8eca55a6 RM |
72 | grub_dprintf ("keystatus", "expect_mods: %d\n", expect_mods); |
73 | ||
4cbe67e5 | 74 | /* Without arguments, just check whether getkeystatus is supported at |
75 | all. */ | |
8eca55a6 RM |
76 | if (expect_mods == 0) |
77 | { | |
78 | grub_term_input_t term; | |
ca04c7a5 | 79 | int nterms = 0; |
8eca55a6 RM |
80 | |
81 | FOR_ACTIVE_TERM_INPUTS (term) | |
ca04c7a5 | 82 | if (!term->getkeystatus) |
d61386e2 | 83 | return grub_error (GRUB_ERR_TEST_FAILURE, N_("false")); |
ca04c7a5 VS |
84 | else |
85 | nterms++; | |
86 | if (!nterms) | |
d61386e2 | 87 | return grub_error (GRUB_ERR_TEST_FAILURE, N_("false")); |
ca04c7a5 | 88 | return 0; |
8eca55a6 | 89 | } |
4cbe67e5 | 90 | |
91 | mods = grub_getkeystatus (); | |
92 | grub_dprintf ("keystatus", "mods: %d\n", mods); | |
93 | if (mods >= 0 && (mods & expect_mods) != 0) | |
94 | return 0; | |
95 | else | |
d61386e2 | 96 | return grub_error (GRUB_ERR_TEST_FAILURE, N_("false")); |
4cbe67e5 | 97 | } |
98 | ||
99 | static grub_extcmd_t cmd; | |
100 | \f | |
101 | GRUB_MOD_INIT(keystatus) | |
102 | { | |
ed80f7d5 | 103 | cmd = grub_register_extcmd ("keystatus", grub_cmd_keystatus, 0, |
b525fd83 | 104 | "[--shift] [--ctrl] [--alt]", |
67093bc0 | 105 | /* TRANSLATORS: there are 3 modifiers. */ |
77a79592 | 106 | N_("Check key modifier status."), |
4cbe67e5 | 107 | options); |
108 | } | |
109 | ||
110 | GRUB_MOD_FINI(keystatus) | |
111 | { | |
112 | grub_unregister_extcmd (cmd); | |
113 | } |