emuconsole.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* console.c -- console for GRUB. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2013 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 <config.h>
  20. #include <config-util.h>
  21. #include <grub/term.h>
  22. #include <grub/types.h>
  23. #include <grub/misc.h>
  24. #include <grub/mm.h>
  25. #include <grub/time.h>
  26. #include <grub/terminfo.h>
  27. #include <grub/dl.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <termios.h>
  33. #include <stdlib.h>
  34. #include <sys/ioctl.h>
  35. #include <langinfo.h>
  36. #include <grub/emu/console.h>
  37. extern struct grub_terminfo_output_state grub_console_terminfo_output;
  38. static int original_fl;
  39. static int saved_orig;
  40. static struct termios orig_tty;
  41. static struct termios new_tty;
  42. static void
  43. put (struct grub_term_output *term __attribute__ ((unused)), const int c)
  44. {
  45. char chr = c;
  46. ssize_t actual;
  47. actual = write (STDOUT_FILENO, &chr, 1);
  48. if (actual < 1)
  49. {
  50. /* We cannot do anything about this, but some systems require us to at
  51. least pretend to check the result. */
  52. }
  53. }
  54. static int
  55. readkey (struct grub_term_input *term __attribute__ ((unused)))
  56. {
  57. grub_uint8_t c;
  58. ssize_t actual;
  59. actual = read (STDIN_FILENO, &c, 1);
  60. if (actual > 0)
  61. return c;
  62. return -1;
  63. }
  64. static grub_err_t
  65. grub_console_init_input (struct grub_term_input *term)
  66. {
  67. if (!saved_orig)
  68. {
  69. original_fl = fcntl (STDIN_FILENO, F_GETFL);
  70. fcntl (STDIN_FILENO, F_SETFL, original_fl | O_NONBLOCK);
  71. }
  72. saved_orig = 1;
  73. tcgetattr(STDIN_FILENO, &orig_tty);
  74. new_tty = orig_tty;
  75. new_tty.c_lflag &= ~(ICANON | ECHO);
  76. new_tty.c_cc[VMIN] = 1;
  77. tcsetattr(STDIN_FILENO, TCSANOW, &new_tty);
  78. return grub_terminfo_input_init (term);
  79. }
  80. static grub_err_t
  81. grub_console_fini_input (struct grub_term_input *term
  82. __attribute__ ((unused)))
  83. {
  84. fcntl (STDIN_FILENO, F_SETFL, original_fl);
  85. tcsetattr(STDIN_FILENO, TCSANOW, &orig_tty);
  86. saved_orig = 0;
  87. return GRUB_ERR_NONE;
  88. }
  89. static grub_err_t
  90. grub_console_init_output (struct grub_term_output *term)
  91. {
  92. struct winsize size;
  93. if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &size) >= 0)
  94. {
  95. grub_console_terminfo_output.size.x = size.ws_col;
  96. grub_console_terminfo_output.size.y = size.ws_row;
  97. }
  98. else
  99. {
  100. grub_console_terminfo_output.size.x = 80;
  101. grub_console_terminfo_output.size.y = 24;
  102. }
  103. grub_terminfo_output_init (term);
  104. return 0;
  105. }
  106. struct grub_terminfo_input_state grub_console_terminfo_input =
  107. {
  108. .readkey = readkey
  109. };
  110. struct grub_terminfo_output_state grub_console_terminfo_output =
  111. {
  112. .put = put,
  113. .size = { 80, 24 }
  114. };
  115. static struct grub_term_input grub_console_term_input =
  116. {
  117. .name = "console",
  118. .init = grub_console_init_input,
  119. .fini = grub_console_fini_input,
  120. .getkey = grub_terminfo_getkey,
  121. .data = &grub_console_terminfo_input
  122. };
  123. static struct grub_term_output grub_console_term_output =
  124. {
  125. .name = "console",
  126. .init = grub_console_init_output,
  127. .putchar = grub_terminfo_putchar,
  128. .getxy = grub_terminfo_getxy,
  129. .getwh = grub_terminfo_getwh,
  130. .gotoxy = grub_terminfo_gotoxy,
  131. .cls = grub_terminfo_cls,
  132. .setcolorstate = grub_terminfo_setcolorstate,
  133. .setcursor = grub_terminfo_setcursor,
  134. .data = &grub_console_terminfo_output,
  135. .progress_update_divisor = GRUB_PROGRESS_FAST
  136. };
  137. void
  138. grub_console_init (void)
  139. {
  140. const char *cs = nl_langinfo (CODESET);
  141. if (cs && grub_strcasecmp (cs, "UTF-8"))
  142. grub_console_term_output.flags = GRUB_TERM_CODE_TYPE_UTF8_LOGICAL;
  143. else
  144. grub_console_term_output.flags = GRUB_TERM_CODE_TYPE_ASCII;
  145. grub_term_register_input ("console", &grub_console_term_input);
  146. grub_term_register_output ("console", &grub_console_term_output);
  147. grub_terminfo_init ();
  148. grub_terminfo_output_register (&grub_console_term_output, "vt100-color");
  149. }
  150. void
  151. grub_console_fini (void)
  152. {
  153. if (saved_orig)
  154. {
  155. fcntl (STDIN_FILENO, F_SETFL, original_fl);
  156. tcsetattr(STDIN_FILENO, TCSANOW, &orig_tty);
  157. }
  158. saved_orig = 0;
  159. }