keystatus.c 2.8 KB

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