term.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. GRUB_EXPORT(grub_term_inputs);
  25. GRUB_EXPORT(grub_term_outputs);
  26. GRUB_EXPORT(grub_term_inputs_disabled);
  27. GRUB_EXPORT(grub_term_outputs_disabled);
  28. GRUB_EXPORT(grub_putchar);
  29. GRUB_EXPORT(grub_putcode);
  30. GRUB_EXPORT(grub_putcode);
  31. GRUB_EXPORT(grub_getkey);
  32. GRUB_EXPORT(grub_getkeystatus);
  33. GRUB_EXPORT(grub_checkkey);
  34. GRUB_EXPORT(grub_cls);
  35. GRUB_EXPORT(grub_setcolorstate);
  36. GRUB_EXPORT(grub_refresh);
  37. GRUB_EXPORT(grub_newline_hook);
  38. struct grub_term_output *grub_term_outputs_disabled;
  39. struct grub_term_input *grub_term_inputs_disabled;
  40. struct grub_term_output *grub_term_outputs;
  41. struct grub_term_input *grub_term_inputs;
  42. void (*grub_newline_hook) (void) = NULL;
  43. /* Put a Unicode character. */
  44. void
  45. grub_putcode (grub_uint32_t code, struct grub_term_output *term)
  46. {
  47. if (code == '\t' && term->getxy)
  48. {
  49. int n;
  50. n = 8 - ((term->getxy () >> 8) & 7);
  51. while (n--)
  52. grub_putcode (' ', term);
  53. return;
  54. }
  55. (term->putchar) (code);
  56. if (code == '\n')
  57. (term->putchar) ('\r');
  58. }
  59. /* Put a character. C is one byte of a UTF-8 stream.
  60. This function gathers bytes until a valid Unicode character is found. */
  61. void
  62. grub_putchar (int c)
  63. {
  64. static grub_size_t size = 0;
  65. static grub_uint8_t buf[6];
  66. grub_uint8_t *rest;
  67. grub_uint32_t code;
  68. buf[size++] = c;
  69. while (grub_utf8_to_ucs4 (&code, 1, buf, size, (const grub_uint8_t **) &rest)
  70. != 0)
  71. {
  72. struct grub_term_output *term;
  73. size -= rest - buf;
  74. grub_memmove (buf, rest, size);
  75. FOR_ACTIVE_TERM_OUTPUTS(term)
  76. grub_putcode (code, term);
  77. if (code == '\n' && grub_newline_hook)
  78. grub_newline_hook ();
  79. }
  80. }
  81. int
  82. grub_getkey (void)
  83. {
  84. grub_term_input_t term;
  85. grub_refresh ();
  86. while (1)
  87. {
  88. FOR_ACTIVE_TERM_INPUTS(term)
  89. {
  90. int key = term->checkkey ();
  91. if (key != -1)
  92. return term->getkey ();
  93. }
  94. grub_cpu_idle ();
  95. }
  96. }
  97. int
  98. grub_checkkey (void)
  99. {
  100. grub_term_input_t term;
  101. FOR_ACTIVE_TERM_INPUTS(term)
  102. {
  103. int key = term->checkkey ();
  104. if (key != -1)
  105. return key;
  106. }
  107. return -1;
  108. }
  109. int
  110. grub_getkeystatus (void)
  111. {
  112. int status = 0;
  113. grub_term_input_t term;
  114. FOR_ACTIVE_TERM_INPUTS(term)
  115. {
  116. if (term->getkeystatus)
  117. status |= term->getkeystatus ();
  118. }
  119. return status;
  120. }
  121. void
  122. grub_cls (void)
  123. {
  124. struct grub_term_output *term;
  125. FOR_ACTIVE_TERM_OUTPUTS(term)
  126. {
  127. if ((term->flags & GRUB_TERM_DUMB) || (grub_env_get ("debug")))
  128. {
  129. grub_putcode ('\n', term);
  130. grub_term_refresh (term);
  131. }
  132. else
  133. (term->cls) ();
  134. }
  135. }
  136. void
  137. grub_setcolorstate (grub_term_color_state state)
  138. {
  139. struct grub_term_output *term;
  140. FOR_ACTIVE_TERM_OUTPUTS(term)
  141. grub_term_setcolorstate (term, state);
  142. }
  143. void
  144. grub_refresh (void)
  145. {
  146. struct grub_term_output *term;
  147. FOR_ACTIVE_TERM_OUTPUTS(term)
  148. grub_term_refresh (term);
  149. }