keystatus.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include <grub/dl.h>
  20. #include <grub/misc.h>
  21. #include <grub/extcmd.h>
  22. #include <grub/term.h>
  23. #include <grub/i18n.h>
  24. GRUB_MOD_LICENSE ("GPLv3+");
  25. static const struct grub_arg_option options[] =
  26. {
  27. /* TRANSLATORS: "Check" in a sense that if this key is pressed then
  28. "true" is returned, otherwise "false". */
  29. {"shift", 's', 0, N_("Check Shift key."), 0, 0},
  30. {"ctrl", 'c', 0, N_("Check Control key."), 0, 0},
  31. {"alt", 'a', 0, N_("Check Alt key."), 0, 0},
  32. {0, 0, 0, 0, 0, 0}
  33. };
  34. static int
  35. grub_getkeystatus (void)
  36. {
  37. int status = 0;
  38. grub_term_input_t term;
  39. if (grub_term_poll_usb)
  40. grub_term_poll_usb (0);
  41. FOR_ACTIVE_TERM_INPUTS(term)
  42. {
  43. if (term->getkeystatus)
  44. status |= term->getkeystatus (term);
  45. }
  46. return status;
  47. }
  48. static grub_err_t
  49. grub_cmd_keystatus (grub_extcmd_context_t ctxt,
  50. int argc __attribute__ ((unused)),
  51. char **args __attribute__ ((unused)))
  52. {
  53. struct grub_arg_list *state = ctxt->state;
  54. int expect_mods = 0;
  55. int mods;
  56. if (state[0].set)
  57. expect_mods |= (GRUB_TERM_STATUS_LSHIFT | GRUB_TERM_STATUS_RSHIFT);
  58. if (state[1].set)
  59. expect_mods |= (GRUB_TERM_STATUS_LCTRL | GRUB_TERM_STATUS_RCTRL);
  60. if (state[2].set)
  61. expect_mods |= (GRUB_TERM_STATUS_LALT | GRUB_TERM_STATUS_RALT);
  62. grub_dprintf ("keystatus", "expect_mods: %d\n", expect_mods);
  63. /* Without arguments, just check whether getkeystatus is supported at
  64. all. */
  65. if (expect_mods == 0)
  66. {
  67. grub_term_input_t term;
  68. int nterms = 0;
  69. FOR_ACTIVE_TERM_INPUTS (term)
  70. if (!term->getkeystatus)
  71. return grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
  72. else
  73. nterms++;
  74. if (!nterms)
  75. return grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
  76. return 0;
  77. }
  78. mods = grub_getkeystatus ();
  79. grub_dprintf ("keystatus", "mods: %d\n", mods);
  80. if (mods >= 0 && (mods & expect_mods) != 0)
  81. return 0;
  82. else
  83. return grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
  84. }
  85. static grub_extcmd_t cmd;
  86. GRUB_MOD_INIT(keystatus)
  87. {
  88. cmd = grub_register_extcmd ("keystatus", grub_cmd_keystatus, 0,
  89. "[--shift] [--ctrl] [--alt]",
  90. /* TRANSLATORS: there are 3 modifiers. */
  91. N_("Check key modifier status."),
  92. options);
  93. }
  94. GRUB_MOD_FINI(keystatus)
  95. {
  96. grub_unregister_extcmd (cmd);
  97. }