keystatus.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. static const struct grub_arg_option options[] =
  25. {
  26. {"shift", 's', 0, N_("Check Shift key."), 0, 0},
  27. {"ctrl", 'c', 0, N_("Check Control key."), 0, 0},
  28. {"alt", 'a', 0, N_("Check Alt key."), 0, 0},
  29. {0, 0, 0, 0, 0, 0}
  30. };
  31. #define grub_cur_term_input grub_term_get_current_input ()
  32. static grub_err_t
  33. grub_cmd_keystatus (grub_extcmd_t cmd,
  34. int argc __attribute__ ((unused)),
  35. char **args __attribute__ ((unused)))
  36. {
  37. struct grub_arg_list *state = cmd->state;
  38. int expect_mods = 0;
  39. int mods;
  40. if (state[0].set)
  41. expect_mods |= GRUB_TERM_STATUS_SHIFT;
  42. if (state[1].set)
  43. expect_mods |= GRUB_TERM_STATUS_CTRL;
  44. if (state[2].set)
  45. expect_mods |= GRUB_TERM_STATUS_ALT;
  46. grub_dprintf ("keystatus", "expect_mods: %d\n", expect_mods);
  47. /* Without arguments, just check whether getkeystatus is supported at
  48. all. */
  49. if (expect_mods == 0)
  50. {
  51. grub_term_input_t term;
  52. int nterms = 0;
  53. FOR_ACTIVE_TERM_INPUTS (term)
  54. if (!term->getkeystatus)
  55. return grub_error (GRUB_ERR_TEST_FAILURE, "false");
  56. else
  57. nterms++;
  58. if (!nterms)
  59. return grub_error (GRUB_ERR_TEST_FAILURE, "false");
  60. return 0;
  61. }
  62. mods = grub_getkeystatus ();
  63. grub_dprintf ("keystatus", "mods: %d\n", mods);
  64. if (mods >= 0 && (mods & expect_mods) != 0)
  65. return 0;
  66. else
  67. return grub_error (GRUB_ERR_TEST_FAILURE, "false");
  68. }
  69. static grub_extcmd_t cmd;
  70. GRUB_MOD_INIT(keystatus)
  71. {
  72. cmd = grub_register_extcmd ("keystatus", grub_cmd_keystatus,
  73. GRUB_COMMAND_FLAG_BOTH,
  74. N_("[--shift] [--ctrl] [--alt]"),
  75. N_("Check key modifier status."),
  76. options);
  77. }
  78. GRUB_MOD_FINI(keystatus)
  79. {
  80. grub_unregister_extcmd (cmd);
  81. }