GRUB-1.98 changes
[grub2/jjazz.git] / term / efi / console.c
blob264770cae41d52852e9a4e7bef0832bc0c331853
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <grub/term.h>
20 #include <grub/misc.h>
21 #include <grub/types.h>
22 #include <grub/err.h>
23 #include <grub/efi/efi.h>
24 #include <grub/efi/api.h>
25 #include <grub/efi/console.h>
27 static grub_uint8_t
28 grub_console_standard_color = GRUB_EFI_TEXT_ATTR (GRUB_EFI_YELLOW,
29 GRUB_EFI_BACKGROUND_BLACK);
30 static grub_uint8_t
31 grub_console_normal_color = GRUB_EFI_TEXT_ATTR (GRUB_EFI_LIGHTGRAY,
32 GRUB_EFI_BACKGROUND_BLACK);
33 static grub_uint8_t
34 grub_console_highlight_color = GRUB_EFI_TEXT_ATTR (GRUB_EFI_BLACK,
35 GRUB_EFI_BACKGROUND_LIGHTGRAY);
37 static int read_key = -1;
39 static grub_uint32_t
40 map_char (grub_uint32_t c)
42 if (c > 0x7f)
44 /* Map some unicode characters to the EFI character. */
45 switch (c)
47 case 0x2190: /* left arrow */
48 c = 0x25c4;
49 break;
50 case 0x2191: /* up arrow */
51 c = 0x25b2;
52 break;
53 case 0x2192: /* right arrow */
54 c = 0x25ba;
55 break;
56 case 0x2193: /* down arrow */
57 c = 0x25bc;
58 break;
59 case 0x2501: /* horizontal line */
60 c = 0x2500;
61 break;
62 case 0x2503: /* vertical line */
63 c = 0x2502;
64 break;
65 case 0x250F: /* upper-left corner */
66 c = 0x250c;
67 break;
68 case 0x2513: /* upper-right corner */
69 c = 0x2510;
70 break;
71 case 0x2517: /* lower-left corner */
72 c = 0x2514;
73 break;
74 case 0x251B: /* lower-right corner */
75 c = 0x2518;
76 break;
78 default:
79 c = '?';
80 break;
84 return c;
87 static void
88 grub_console_putchar (grub_uint32_t c)
90 grub_efi_char16_t str[2];
91 grub_efi_simple_text_output_interface_t *o;
93 o = grub_efi_system_table->con_out;
95 /* For now, do not try to use a surrogate pair. */
96 if (c > 0xffff)
97 c = '?';
99 str[0] = (grub_efi_char16_t) map_char (c & 0xffff);
100 str[1] = 0;
102 /* Should this test be cached? */
103 if (c > 0x7f && efi_call_2 (o->test_string, o, str) != GRUB_EFI_SUCCESS)
104 return;
106 efi_call_2 (o->output_string, o, str);
109 static grub_ssize_t
110 grub_console_getcharwidth (grub_uint32_t c __attribute__ ((unused)))
112 /* For now, every printable character has the width 1. */
113 return 1;
116 static int
117 grub_console_checkkey (void)
119 grub_efi_simple_input_interface_t *i;
120 grub_efi_input_key_t key;
121 grub_efi_status_t status;
123 if (read_key >= 0)
124 return 1;
126 i = grub_efi_system_table->con_in;
127 status = efi_call_2 (i->read_key_stroke, i, &key);
128 #if 0
129 switch (status)
131 case GRUB_EFI_SUCCESS:
133 grub_uint16_t xy;
135 xy = grub_getxy ();
136 grub_gotoxy (0, 0);
137 grub_printf ("scan_code=%x,unicode_char=%x ",
138 (unsigned) key.scan_code,
139 (unsigned) key.unicode_char);
140 grub_gotoxy (xy >> 8, xy & 0xff);
142 break;
144 case GRUB_EFI_NOT_READY:
145 //grub_printf ("not ready ");
146 break;
148 default:
149 //grub_printf ("device error ");
150 break;
152 #endif
154 if (status == GRUB_EFI_SUCCESS)
156 switch (key.scan_code)
158 case 0x00:
159 read_key = key.unicode_char;
160 break;
161 case 0x01:
162 read_key = 16;
163 break;
164 case 0x02:
165 read_key = 14;
166 break;
167 case 0x03:
168 read_key = 6;
169 break;
170 case 0x04:
171 read_key = 2;
172 break;
173 case 0x05:
174 read_key = 1;
175 break;
176 case 0x06:
177 read_key = 5;
178 break;
179 case 0x07:
180 break;
181 case 0x08:
182 read_key = 4;
183 break;
184 case 0x09:
185 break;
186 case 0x0a:
187 break;
188 case 0x0b:
189 read_key = 24;
190 break;
191 case 0x0c:
192 read_key = 1;
193 break;
194 case 0x0d:
195 read_key = 5;
196 break;
197 case 0x17:
198 read_key = '\e';
199 break;
200 default:
201 break;
205 return read_key;
208 static int
209 grub_console_getkey (void)
211 grub_efi_simple_input_interface_t *i;
212 grub_efi_boot_services_t *b;
213 grub_efi_uintn_t index;
214 grub_efi_status_t status;
215 int key;
217 if (read_key >= 0)
219 key = read_key;
220 read_key = -1;
221 return key;
224 i = grub_efi_system_table->con_in;
225 b = grub_efi_system_table->boot_services;
229 status = efi_call_3 (b->wait_for_event, 1, &(i->wait_for_key), &index);
230 if (status != GRUB_EFI_SUCCESS)
231 return -1;
233 grub_console_checkkey ();
235 while (read_key < 0);
237 key = read_key;
238 read_key = -1;
239 return key;
242 static grub_uint16_t
243 grub_console_getwh (void)
245 grub_efi_simple_text_output_interface_t *o;
246 grub_efi_uintn_t columns, rows;
248 o = grub_efi_system_table->con_out;
249 if (efi_call_4 (o->query_mode, o, o->mode->mode, &columns, &rows) != GRUB_EFI_SUCCESS)
251 /* Why does this fail? */
252 columns = 80;
253 rows = 25;
256 return ((columns << 8) | rows);
259 static grub_uint16_t
260 grub_console_getxy (void)
262 grub_efi_simple_text_output_interface_t *o;
264 o = grub_efi_system_table->con_out;
265 return ((o->mode->cursor_column << 8) | o->mode->cursor_row);
268 static void
269 grub_console_gotoxy (grub_uint8_t x, grub_uint8_t y)
271 grub_efi_simple_text_output_interface_t *o;
273 o = grub_efi_system_table->con_out;
274 efi_call_3 (o->set_cursor_position, o, x, y);
277 static void
278 grub_console_cls (void)
280 grub_efi_simple_text_output_interface_t *o;
281 grub_efi_int32_t orig_attr;
283 o = grub_efi_system_table->con_out;
284 orig_attr = o->mode->attribute;
285 efi_call_2 (o->set_attributes, o, GRUB_EFI_BACKGROUND_BLACK);
286 efi_call_1 (o->clear_screen, o);
287 efi_call_2 (o->set_attributes, o, orig_attr);
290 static void
291 grub_console_setcolorstate (grub_term_color_state state)
293 grub_efi_simple_text_output_interface_t *o;
295 o = grub_efi_system_table->con_out;
297 switch (state) {
298 case GRUB_TERM_COLOR_STANDARD:
299 efi_call_2 (o->set_attributes, o, grub_console_standard_color);
300 break;
301 case GRUB_TERM_COLOR_NORMAL:
302 efi_call_2 (o->set_attributes, o, grub_console_normal_color);
303 break;
304 case GRUB_TERM_COLOR_HIGHLIGHT:
305 efi_call_2 (o->set_attributes, o, grub_console_highlight_color);
306 break;
307 default:
308 break;
312 static void
313 grub_console_setcolor (grub_uint8_t normal_color, grub_uint8_t highlight_color)
315 grub_console_normal_color = normal_color;
316 grub_console_highlight_color = highlight_color;
319 static void
320 grub_console_getcolor (grub_uint8_t *normal_color, grub_uint8_t *highlight_color)
322 *normal_color = grub_console_normal_color;
323 *highlight_color = grub_console_highlight_color;
326 static void
327 grub_console_setcursor (int on)
329 grub_efi_simple_text_output_interface_t *o;
331 o = grub_efi_system_table->con_out;
332 efi_call_2 (o->enable_cursor, o, on);
335 static struct grub_term_input grub_console_term_input =
337 .name = "console",
338 .checkkey = grub_console_checkkey,
339 .getkey = grub_console_getkey,
342 static struct grub_term_output grub_console_term_output =
344 .name = "console",
345 .putchar = grub_console_putchar,
346 .getcharwidth = grub_console_getcharwidth,
347 .getwh = grub_console_getwh,
348 .getxy = grub_console_getxy,
349 .gotoxy = grub_console_gotoxy,
350 .cls = grub_console_cls,
351 .setcolorstate = grub_console_setcolorstate,
352 .setcolor = grub_console_setcolor,
353 .getcolor = grub_console_getcolor,
354 .setcursor = grub_console_setcursor
357 void
358 grub_console_init (void)
360 /* FIXME: it is necessary to consider the case where no console control
361 is present but the default is already in text mode. */
362 if (! grub_efi_set_text_mode (1))
364 grub_error (GRUB_ERR_BAD_DEVICE, "cannot set text mode");
365 return;
368 grub_term_register_input ("console", &grub_console_term_input);
369 grub_term_register_output ("console", &grub_console_term_output);
372 void
373 grub_console_fini (void)
375 grub_term_unregister_input (&grub_console_term_input);
376 grub_term_unregister_output (&grub_console_term_output);