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