term.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2003,2005,2007,2008,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/term.h>
  19. #include <grub/err.h>
  20. #include <grub/mm.h>
  21. #include <grub/misc.h>
  22. #include <grub/env.h>
  23. #include <grub/time.h>
  24. struct grub_term_output *grub_term_outputs_disabled;
  25. struct grub_term_input *grub_term_inputs_disabled;
  26. struct grub_term_output *grub_term_outputs;
  27. struct grub_term_input *grub_term_inputs;
  28. /* Current color state. */
  29. grub_uint8_t grub_term_normal_color = GRUB_TERM_DEFAULT_NORMAL_COLOR;
  30. grub_uint8_t grub_term_highlight_color = GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR;
  31. void (*grub_term_poll_usb) (int wait_for_completion) = NULL;
  32. void (*grub_net_poll_cards_idle) (void) = NULL;
  33. /* Put a Unicode character. */
  34. static void
  35. grub_putcode_dumb (grub_uint32_t code,
  36. struct grub_term_output *term)
  37. {
  38. struct grub_unicode_glyph c =
  39. {
  40. .base = code,
  41. .variant = 0,
  42. .attributes = 0,
  43. .ncomb = 0,
  44. .estimated_width = 1
  45. };
  46. if (code == '\t' && term->getxy)
  47. {
  48. int n;
  49. n = GRUB_TERM_TAB_WIDTH - ((term->getxy (term).x)
  50. % GRUB_TERM_TAB_WIDTH);
  51. while (n--)
  52. grub_putcode_dumb (' ', term);
  53. return;
  54. }
  55. (term->putchar) (term, &c);
  56. if (code == '\n')
  57. grub_putcode_dumb ('\r', term);
  58. }
  59. static void
  60. grub_xputs_dumb (const char *str)
  61. {
  62. for (; *str; str++)
  63. {
  64. grub_term_output_t term;
  65. grub_uint32_t code = *str;
  66. if (code > 0x7f)
  67. code = '?';
  68. FOR_ACTIVE_TERM_OUTPUTS(term)
  69. grub_putcode_dumb (code, term);
  70. }
  71. }
  72. void (*grub_xputs) (const char *str) = grub_xputs_dumb;
  73. int
  74. grub_getkey_noblock (void)
  75. {
  76. grub_term_input_t term;
  77. if (grub_term_poll_usb)
  78. grub_term_poll_usb (0);
  79. if (grub_net_poll_cards_idle)
  80. grub_net_poll_cards_idle ();
  81. FOR_ACTIVE_TERM_INPUTS(term)
  82. {
  83. int key = term->getkey (term);
  84. if (key != GRUB_TERM_NO_KEY)
  85. return key;
  86. }
  87. return GRUB_TERM_NO_KEY;
  88. }
  89. int
  90. grub_getkey (void)
  91. {
  92. int ret;
  93. grub_refresh ();
  94. while (1)
  95. {
  96. ret = grub_getkey_noblock ();
  97. if (ret != GRUB_TERM_NO_KEY)
  98. return ret;
  99. grub_cpu_idle ();
  100. }
  101. }
  102. int
  103. grub_getkeystatus (void)
  104. {
  105. int status = 0;
  106. grub_term_input_t term;
  107. if (grub_term_poll_usb)
  108. grub_term_poll_usb (0);
  109. FOR_ACTIVE_TERM_INPUTS(term)
  110. {
  111. if (term->getkeystatus)
  112. status |= term->getkeystatus (term);
  113. }
  114. return status;
  115. }
  116. int
  117. grub_key_is_interrupt (int key)
  118. {
  119. /*
  120. * ESC sometimes is the BIOS setup hotkey and may be hard to discover, also
  121. * check F4, which was chosen because is not used as a hotkey to enter the
  122. * BIOS setup by any vendor.
  123. */
  124. if (key == GRUB_TERM_ESC || key == GRUB_TERM_KEY_F4)
  125. return 1;
  126. /*
  127. * Pressing keys at the right time during boot is hard to time, also allow
  128. * interrupting sleeps / the menu countdown by keeping shift pressed.
  129. */
  130. if (grub_getkeystatus() & (GRUB_TERM_STATUS_LSHIFT | GRUB_TERM_STATUS_RSHIFT))
  131. return 1;
  132. return 0;
  133. }
  134. void
  135. grub_refresh (void)
  136. {
  137. struct grub_term_output *term;
  138. FOR_ACTIVE_TERM_OUTPUTS(term)
  139. grub_term_refresh (term);
  140. }