emuconsole.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2006,2007,2008,2013 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 <config.h>
  19. #include <config-util.h>
  20. #include <grub/term.h>
  21. #include <grub/misc.h>
  22. #include <grub/types.h>
  23. #include <grub/err.h>
  24. #include <grub/emu/console.h>
  25. #include <windows.h>
  26. static HANDLE hStdin, hStdout;
  27. static DWORD orig_mode;
  28. static int saved_orig;
  29. static void
  30. grub_console_putchar (struct grub_term_output *term __attribute__ ((unused)),
  31. const struct grub_unicode_glyph *c)
  32. {
  33. TCHAR str[2 + 30];
  34. unsigned i, j;
  35. DWORD written;
  36. /* For now, do not try to use a surrogate pair. */
  37. if (c->base > 0xffff)
  38. str[0] = '?';
  39. else
  40. str[0] = (c->base & 0xffff);
  41. j = 1;
  42. for (i = 0; i < c->ncomb && j+1 < ARRAY_SIZE (str); i++)
  43. if (c->base < 0xffff)
  44. str[j++] = grub_unicode_get_comb (c)[i].code;
  45. str[j] = 0;
  46. WriteConsole (hStdout, str, j, &written, NULL);
  47. }
  48. const unsigned windows_codes[] =
  49. {
  50. /* 0x21 */ [VK_PRIOR] = GRUB_TERM_KEY_PPAGE,
  51. /* 0x22 */ [VK_NEXT] = GRUB_TERM_KEY_NPAGE,
  52. /* 0x23 */ [VK_END] = GRUB_TERM_KEY_END,
  53. /* 0x24 */ [VK_HOME] = GRUB_TERM_KEY_HOME,
  54. /* 0x25 */ [VK_LEFT] = GRUB_TERM_KEY_LEFT,
  55. /* 0x26 */ [VK_UP] = GRUB_TERM_KEY_UP,
  56. /* 0x27 */ [VK_RIGHT] = GRUB_TERM_KEY_RIGHT,
  57. /* 0x28 */ [VK_DOWN] = GRUB_TERM_KEY_DOWN,
  58. /* 0x2e */ [VK_DELETE] = GRUB_TERM_KEY_DC,
  59. /* 0x70 */ [VK_F1] = GRUB_TERM_KEY_F1,
  60. /* 0x71 */ [VK_F2] = GRUB_TERM_KEY_F2,
  61. /* 0x72 */ [VK_F3] = GRUB_TERM_KEY_F3,
  62. /* 0x73 */ [VK_F4] = GRUB_TERM_KEY_F4,
  63. /* 0x74 */ [VK_F5] = GRUB_TERM_KEY_F5,
  64. /* 0x75 */ [VK_F6] = GRUB_TERM_KEY_F6,
  65. /* 0x76 */ [VK_F7] = GRUB_TERM_KEY_F7,
  66. /* 0x77 */ [VK_F8] = GRUB_TERM_KEY_F8,
  67. /* 0x78 */ [VK_F9] = GRUB_TERM_KEY_F9,
  68. /* 0x79 */ [VK_F10] = GRUB_TERM_KEY_F10,
  69. /* 0x7a */ [VK_F11] = GRUB_TERM_KEY_F11,
  70. /* 0x7b */ [VK_F12] = GRUB_TERM_KEY_F12,
  71. };
  72. static int
  73. grub_console_getkey (struct grub_term_input *term __attribute__ ((unused)))
  74. {
  75. while (1)
  76. {
  77. DWORD nev;
  78. INPUT_RECORD ir;
  79. int ret;
  80. if (!GetNumberOfConsoleInputEvents (hStdin, &nev))
  81. return GRUB_TERM_NO_KEY;
  82. if (nev == 0)
  83. return GRUB_TERM_NO_KEY;
  84. if (!ReadConsoleInput (hStdin, &ir, 1,
  85. &nev))
  86. return GRUB_TERM_NO_KEY;
  87. if (ir.EventType != KEY_EVENT)
  88. continue;
  89. if (!ir.Event.KeyEvent.bKeyDown)
  90. continue;
  91. ret = ir.Event.KeyEvent.uChar.UnicodeChar;
  92. if (ret == 0)
  93. {
  94. unsigned kc = ir.Event.KeyEvent.wVirtualKeyCode;
  95. if (kc < ARRAY_SIZE (windows_codes) && windows_codes[kc])
  96. ret = windows_codes[kc];
  97. else
  98. continue;
  99. if (ir.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED)
  100. ret |= GRUB_TERM_SHIFT;
  101. }
  102. /* Workaround for AltGr bug. */
  103. if (ir.Event.KeyEvent.dwControlKeyState & RIGHT_ALT_PRESSED)
  104. return ret;
  105. if (ir.Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
  106. ret |= GRUB_TERM_ALT;
  107. if (ir.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
  108. ret |= GRUB_TERM_CTRL;
  109. return ret;
  110. }
  111. }
  112. static struct grub_term_coordinate
  113. grub_console_getwh (struct grub_term_output *term __attribute__ ((unused)))
  114. {
  115. CONSOLE_SCREEN_BUFFER_INFO csbi;
  116. csbi.dwSize.X = 80;
  117. csbi.dwSize.Y = 25;
  118. GetConsoleScreenBufferInfo (hStdout, &csbi);
  119. return (struct grub_term_coordinate) { csbi.dwSize.X, csbi.dwSize.Y };
  120. }
  121. static struct grub_term_coordinate
  122. grub_console_getxy (struct grub_term_output *term __attribute__ ((unused)))
  123. {
  124. CONSOLE_SCREEN_BUFFER_INFO csbi;
  125. GetConsoleScreenBufferInfo (hStdout, &csbi);
  126. return (struct grub_term_coordinate) { csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y };
  127. }
  128. static void
  129. grub_console_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
  130. struct grub_term_coordinate pos)
  131. {
  132. COORD coord = { pos.x, pos.y };
  133. SetConsoleCursorPosition (hStdout, coord);
  134. }
  135. static void
  136. grub_console_cls (struct grub_term_output *term)
  137. {
  138. int tsz;
  139. CONSOLE_SCREEN_BUFFER_INFO csbi;
  140. struct grub_unicode_glyph c =
  141. {
  142. .base = ' ',
  143. .variant = 0,
  144. .attributes = 0,
  145. .ncomb = 0,
  146. .estimated_width = 1
  147. };
  148. GetConsoleScreenBufferInfo (hStdout, &csbi);
  149. SetConsoleTextAttribute (hStdout, 0);
  150. grub_console_gotoxy (term, (struct grub_term_coordinate) { 0, 0 });
  151. tsz = csbi.dwSize.X * csbi.dwSize.Y;
  152. while (tsz--)
  153. grub_console_putchar (term, &c);
  154. grub_console_gotoxy (term, (struct grub_term_coordinate) { 0, 0 });
  155. SetConsoleTextAttribute (hStdout, csbi.wAttributes);
  156. }
  157. static void
  158. grub_console_setcolorstate (struct grub_term_output *term
  159. __attribute__ ((unused)),
  160. grub_term_color_state state)
  161. {
  162. switch (state) {
  163. case GRUB_TERM_COLOR_STANDARD:
  164. SetConsoleTextAttribute (hStdout, GRUB_TERM_DEFAULT_STANDARD_COLOR
  165. & 0x7f);
  166. break;
  167. case GRUB_TERM_COLOR_NORMAL:
  168. SetConsoleTextAttribute (hStdout, grub_term_normal_color & 0x7f);
  169. break;
  170. case GRUB_TERM_COLOR_HIGHLIGHT:
  171. SetConsoleTextAttribute (hStdout, grub_term_highlight_color & 0x7f);
  172. break;
  173. default:
  174. break;
  175. }
  176. }
  177. static void
  178. grub_console_setcursor (struct grub_term_output *term __attribute__ ((unused)),
  179. int on)
  180. {
  181. CONSOLE_CURSOR_INFO ci;
  182. ci.dwSize = 5;
  183. ci.bVisible = on;
  184. SetConsoleCursorInfo (hStdout, &ci);
  185. }
  186. static grub_err_t
  187. grub_efi_console_init (struct grub_term_output *term)
  188. {
  189. grub_console_setcursor (term, 1);
  190. return 0;
  191. }
  192. static grub_err_t
  193. grub_efi_console_fini (struct grub_term_output *term)
  194. {
  195. grub_console_setcursor (term, 1);
  196. return 0;
  197. }
  198. static grub_err_t
  199. grub_console_init_input (struct grub_term_input *term)
  200. {
  201. if (!saved_orig)
  202. {
  203. GetConsoleMode (hStdin, &orig_mode);
  204. }
  205. saved_orig = 1;
  206. SetConsoleMode (hStdin, orig_mode & ~ENABLE_ECHO_INPUT
  207. & ~ENABLE_LINE_INPUT & ~ENABLE_PROCESSED_INPUT);
  208. return GRUB_ERR_NONE;
  209. }
  210. static grub_err_t
  211. grub_console_fini_input (struct grub_term_input *term
  212. __attribute__ ((unused)))
  213. {
  214. SetConsoleMode (hStdin, orig_mode);
  215. saved_orig = 0;
  216. return GRUB_ERR_NONE;
  217. }
  218. static struct grub_term_input grub_console_term_input =
  219. {
  220. .name = "console",
  221. .getkey = grub_console_getkey,
  222. .init = grub_console_init_input,
  223. .fini = grub_console_fini_input,
  224. };
  225. static struct grub_term_output grub_console_term_output =
  226. {
  227. .name = "console",
  228. .init = grub_efi_console_init,
  229. .fini = grub_efi_console_fini,
  230. .putchar = grub_console_putchar,
  231. .getwh = grub_console_getwh,
  232. .getxy = grub_console_getxy,
  233. .gotoxy = grub_console_gotoxy,
  234. .cls = grub_console_cls,
  235. .setcolorstate = grub_console_setcolorstate,
  236. .setcursor = grub_console_setcursor,
  237. .flags = GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS,
  238. .progress_update_divisor = GRUB_PROGRESS_FAST
  239. };
  240. void
  241. grub_console_init (void)
  242. {
  243. hStdin = GetStdHandle (STD_INPUT_HANDLE);
  244. hStdout = GetStdHandle (STD_OUTPUT_HANDLE);
  245. grub_term_register_input ("console", &grub_console_term_input);
  246. grub_term_register_output ("console", &grub_console_term_output);
  247. }
  248. void
  249. grub_console_fini (void)
  250. {
  251. if (saved_orig)
  252. {
  253. SetConsoleMode (hStdin, orig_mode);
  254. saved_orig = 0;
  255. }
  256. grub_term_unregister_input (&grub_console_term_input);
  257. grub_term_unregister_output (&grub_console_term_output);
  258. }