2009-11-21 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2.git] / normal / menu_text.c
blobe0d96c47f452300be7e433ddb9d0c4f54e141690
1 /* menu_text.c - Basic text menu implementation. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/normal.h>
21 #include <grub/term.h>
22 #include <grub/misc.h>
23 #include <grub/loader.h>
24 #include <grub/mm.h>
25 #include <grub/time.h>
26 #include <grub/env.h>
27 #include <grub/menu_viewer.h>
29 /* Time to delay after displaying an error message about a default/fallback
30 entry failing to boot. */
31 #define DEFAULT_ENTRY_ERROR_DELAY_MS 2500
33 static grub_uint8_t grub_color_menu_normal;
34 static grub_uint8_t grub_color_menu_highlight;
36 /* Wait until the user pushes any key so that the user
37 can see what happened. */
38 void
39 grub_wait_after_message (void)
41 grub_printf ("\nPress any key to continue...");
42 (void) grub_getkey ();
43 grub_putchar ('\n');
46 static void
47 draw_border (void)
49 unsigned i;
51 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL);
53 grub_gotoxy (GRUB_TERM_MARGIN, GRUB_TERM_TOP_BORDER_Y);
54 grub_putcode (GRUB_TERM_DISP_UL);
55 for (i = 0; i < (unsigned) GRUB_TERM_BORDER_WIDTH - 2; i++)
56 grub_putcode (GRUB_TERM_DISP_HLINE);
57 grub_putcode (GRUB_TERM_DISP_UR);
59 for (i = 0; i < (unsigned) GRUB_TERM_NUM_ENTRIES; i++)
61 grub_gotoxy (GRUB_TERM_MARGIN, GRUB_TERM_TOP_BORDER_Y + i + 1);
62 grub_putcode (GRUB_TERM_DISP_VLINE);
63 grub_gotoxy (GRUB_TERM_MARGIN + GRUB_TERM_BORDER_WIDTH - 1,
64 GRUB_TERM_TOP_BORDER_Y + i + 1);
65 grub_putcode (GRUB_TERM_DISP_VLINE);
68 grub_gotoxy (GRUB_TERM_MARGIN,
69 GRUB_TERM_TOP_BORDER_Y + GRUB_TERM_NUM_ENTRIES + 1);
70 grub_putcode (GRUB_TERM_DISP_LL);
71 for (i = 0; i < (unsigned) GRUB_TERM_BORDER_WIDTH - 2; i++)
72 grub_putcode (GRUB_TERM_DISP_HLINE);
73 grub_putcode (GRUB_TERM_DISP_LR);
75 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL);
77 grub_gotoxy (GRUB_TERM_MARGIN,
78 (GRUB_TERM_TOP_BORDER_Y + GRUB_TERM_NUM_ENTRIES
79 + GRUB_TERM_MARGIN + 1));
82 static void
83 print_message (int nested, int edit)
85 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL);
87 if (edit)
89 grub_printf ("\n\
90 Minimum Emacs-like screen editing is supported. TAB lists\n\
91 completions. Press Ctrl-x to boot, Ctrl-c for a command-line\n\
92 or ESC to return menu.");
94 else
96 grub_printf ("\n\
97 Use the %C and %C keys to select which entry is highlighted.\n",
98 (grub_uint32_t) GRUB_TERM_DISP_UP, (grub_uint32_t) GRUB_TERM_DISP_DOWN);
99 grub_printf ("\
100 Press enter to boot the selected OS, \'e\' to edit the\n\
101 commands before booting or \'c\' for a command-line.");
102 if (nested)
103 grub_printf ("\n\
104 ESC to return previous menu.");
108 static void
109 print_entry (int y, int highlight, grub_menu_entry_t entry)
111 int x;
112 const char *title;
113 grub_size_t title_len;
114 grub_ssize_t len;
115 grub_uint32_t *unicode_title;
116 grub_ssize_t i;
117 grub_uint8_t old_color_normal, old_color_highlight;
119 title = entry ? entry->title : "";
120 title_len = grub_strlen (title);
121 unicode_title = grub_malloc (title_len * sizeof (*unicode_title));
122 if (! unicode_title)
123 /* XXX How to show this error? */
124 return;
126 len = grub_utf8_to_ucs4 (unicode_title, title_len,
127 (grub_uint8_t *) title, -1, 0);
128 if (len < 0)
130 /* It is an invalid sequence. */
131 grub_free (unicode_title);
132 return;
135 grub_getcolor (&old_color_normal, &old_color_highlight);
136 grub_setcolor (grub_color_menu_normal, grub_color_menu_highlight);
137 grub_setcolorstate (highlight
138 ? GRUB_TERM_COLOR_HIGHLIGHT
139 : GRUB_TERM_COLOR_NORMAL);
141 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN, y);
143 for (x = GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN + 1, i = 0;
144 x < GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_BORDER_WIDTH - GRUB_TERM_MARGIN;
145 i++)
147 if (i < len
148 && x <= (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_BORDER_WIDTH
149 - GRUB_TERM_MARGIN - 1))
151 grub_ssize_t width;
153 width = grub_getcharwidth (unicode_title[i]);
155 if (x + width > (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_BORDER_WIDTH
156 - GRUB_TERM_MARGIN - 1))
157 grub_putcode (GRUB_TERM_DISP_RIGHT);
158 else
159 grub_putcode (unicode_title[i]);
161 x += width;
163 else
165 grub_putchar (' ');
166 x++;
169 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL);
170 grub_putchar (' ');
172 grub_gotoxy (GRUB_TERM_CURSOR_X, y);
174 grub_setcolor (old_color_normal, old_color_highlight);
175 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL);
176 grub_free (unicode_title);
179 static void
180 print_entries (grub_menu_t menu, int first, int offset)
182 grub_menu_entry_t e;
183 int i;
185 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_BORDER_WIDTH,
186 GRUB_TERM_FIRST_ENTRY_Y);
188 if (first)
189 grub_putcode (GRUB_TERM_DISP_UP);
190 else
191 grub_putchar (' ');
193 e = grub_menu_get_entry (menu, first);
195 for (i = 0; i < GRUB_TERM_NUM_ENTRIES; i++)
197 print_entry (GRUB_TERM_FIRST_ENTRY_Y + i, offset == i, e);
198 if (e)
199 e = e->next;
202 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_BORDER_WIDTH,
203 GRUB_TERM_TOP_BORDER_Y + GRUB_TERM_NUM_ENTRIES);
205 if (e)
206 grub_putcode (GRUB_TERM_DISP_DOWN);
207 else
208 grub_putchar (' ');
210 grub_gotoxy (GRUB_TERM_CURSOR_X, GRUB_TERM_FIRST_ENTRY_Y + offset);
213 /* Initialize the screen. If NESTED is non-zero, assume that this menu
214 is run from another menu or a command-line. If EDIT is non-zero, show
215 a message for the menu entry editor. */
216 void
217 grub_menu_init_page (int nested, int edit)
219 grub_uint8_t old_color_normal, old_color_highlight;
221 grub_getcolor (&old_color_normal, &old_color_highlight);
223 /* By default, use the same colors for the menu. */
224 grub_color_menu_normal = old_color_normal;
225 grub_color_menu_highlight = old_color_highlight;
227 /* Then give user a chance to replace them. */
228 grub_parse_color_name_pair (&grub_color_menu_normal, grub_env_get ("menu_color_normal"));
229 grub_parse_color_name_pair (&grub_color_menu_highlight, grub_env_get ("menu_color_highlight"));
231 grub_normal_init_page ();
232 grub_setcolor (grub_color_menu_normal, grub_color_menu_highlight);
233 draw_border ();
234 grub_setcolor (old_color_normal, old_color_highlight);
235 print_message (nested, edit);
238 /* Get the entry number from the variable NAME. */
239 static int
240 get_entry_number (const char *name)
242 char *val;
243 int entry;
245 val = grub_env_get (name);
246 if (! val)
247 return -1;
249 grub_error_push ();
251 entry = (int) grub_strtoul (val, 0, 0);
253 if (grub_errno != GRUB_ERR_NONE)
255 grub_errno = GRUB_ERR_NONE;
256 entry = -1;
259 grub_error_pop ();
261 return entry;
264 static void
265 print_timeout (int timeout, int offset, int second_stage)
267 /* NOTE: Do not remove the trailing space characters.
268 They are required to clear the line. */
269 char *msg = " The highlighted entry will be booted automatically in %ds. ";
270 char *msg_end = grub_strchr (msg, '%');
272 grub_gotoxy (second_stage ? (msg_end - msg) : 0, GRUB_TERM_HEIGHT - 3);
273 grub_printf (second_stage ? msg_end : msg, timeout);
274 grub_gotoxy (GRUB_TERM_CURSOR_X, GRUB_TERM_FIRST_ENTRY_Y + offset);
275 grub_refresh ();
278 /* Show the menu and handle menu entry selection. Returns the menu entry
279 index that should be executed or -1 if no entry should be executed (e.g.,
280 Esc pressed to exit a sub-menu or switching menu viewers).
281 If the return value is not -1, then *AUTO_BOOT is nonzero iff the menu
282 entry to be executed is a result of an automatic default selection because
283 of the timeout. */
284 static int
285 run_menu (grub_menu_t menu, int nested, int *auto_boot)
287 int first, offset;
288 grub_uint64_t saved_time;
289 int default_entry;
290 int timeout;
292 first = 0;
294 default_entry = get_entry_number ("default");
296 /* If DEFAULT_ENTRY is not within the menu entries, fall back to
297 the first entry. */
298 if (default_entry < 0 || default_entry >= menu->size)
299 default_entry = 0;
301 /* If timeout is 0, drawing is pointless (and ugly). */
302 if (grub_menu_get_timeout () == 0)
304 *auto_boot = 1;
305 return default_entry;
308 offset = default_entry;
309 if (offset > GRUB_TERM_NUM_ENTRIES - 1)
311 first = offset - (GRUB_TERM_NUM_ENTRIES - 1);
312 offset = GRUB_TERM_NUM_ENTRIES - 1;
315 /* Initialize the time. */
316 saved_time = grub_get_time_ms ();
318 refresh:
319 grub_setcursor (0);
320 grub_menu_init_page (nested, 0);
321 print_entries (menu, first, offset);
322 grub_refresh ();
324 timeout = grub_menu_get_timeout ();
326 if (timeout > 0)
327 print_timeout (timeout, offset, 0);
329 while (1)
331 int c;
332 timeout = grub_menu_get_timeout ();
334 if (timeout > 0)
336 grub_uint64_t current_time;
338 current_time = grub_get_time_ms ();
339 if (current_time - saved_time >= 1000)
341 timeout--;
342 grub_menu_set_timeout (timeout);
343 saved_time = current_time;
344 print_timeout (timeout, offset, 1);
348 if (timeout == 0)
350 grub_env_unset ("timeout");
351 *auto_boot = 1;
352 return default_entry;
355 if (grub_checkkey () >= 0 || timeout < 0)
357 c = GRUB_TERM_ASCII_CHAR (grub_getkey ());
359 if (timeout >= 0)
361 grub_gotoxy (0, GRUB_TERM_HEIGHT - 3);
362 grub_printf ("\
364 grub_env_unset ("timeout");
365 grub_env_unset ("fallback");
366 grub_gotoxy (GRUB_TERM_CURSOR_X, GRUB_TERM_FIRST_ENTRY_Y + offset);
369 switch (c)
371 case GRUB_TERM_HOME:
372 first = 0;
373 offset = 0;
374 print_entries (menu, first, offset);
375 break;
377 case GRUB_TERM_END:
378 offset = menu->size - 1;
379 if (offset > GRUB_TERM_NUM_ENTRIES - 1)
381 first = offset - (GRUB_TERM_NUM_ENTRIES - 1);
382 offset = GRUB_TERM_NUM_ENTRIES - 1;
384 print_entries (menu, first, offset);
385 break;
387 case GRUB_TERM_UP:
388 case '^':
389 if (offset > 0)
391 print_entry (GRUB_TERM_FIRST_ENTRY_Y + offset, 0,
392 grub_menu_get_entry (menu, first + offset));
393 offset--;
394 print_entry (GRUB_TERM_FIRST_ENTRY_Y + offset, 1,
395 grub_menu_get_entry (menu, first + offset));
397 else if (first > 0)
399 first--;
400 print_entries (menu, first, offset);
402 break;
404 case GRUB_TERM_DOWN:
405 case 'v':
406 if (menu->size > first + offset + 1)
408 if (offset < GRUB_TERM_NUM_ENTRIES - 1)
410 print_entry (GRUB_TERM_FIRST_ENTRY_Y + offset, 0,
411 grub_menu_get_entry (menu, first + offset));
412 offset++;
413 print_entry (GRUB_TERM_FIRST_ENTRY_Y + offset, 1,
414 grub_menu_get_entry (menu, first + offset));
416 else
418 first++;
419 print_entries (menu, first, offset);
422 break;
424 case GRUB_TERM_PPAGE:
425 if (first == 0)
427 offset = 0;
429 else
431 first -= GRUB_TERM_NUM_ENTRIES;
433 if (first < 0)
435 offset += first;
436 first = 0;
439 print_entries (menu, first, offset);
440 break;
442 case GRUB_TERM_NPAGE:
443 if (offset == 0)
445 offset += GRUB_TERM_NUM_ENTRIES - 1;
446 if (first + offset >= menu->size)
448 offset = menu->size - first - 1;
451 else
453 first += GRUB_TERM_NUM_ENTRIES;
455 if (first + offset >= menu->size)
457 first -= GRUB_TERM_NUM_ENTRIES;
458 offset += GRUB_TERM_NUM_ENTRIES;
460 if (offset > menu->size - 1 ||
461 offset > GRUB_TERM_NUM_ENTRIES - 1)
463 offset = menu->size - first - 1;
465 if (offset > GRUB_TERM_NUM_ENTRIES)
467 first += offset - GRUB_TERM_NUM_ENTRIES + 1;
468 offset = GRUB_TERM_NUM_ENTRIES - 1;
472 print_entries (menu, first, offset);
473 break;
475 case '\n':
476 case '\r':
477 case 6:
478 grub_setcursor (1);
479 *auto_boot = 0;
480 return first + offset;
482 case '\e':
483 if (nested)
485 grub_setcursor (1);
486 return -1;
488 break;
490 case 'c':
491 grub_cmdline_run (1);
492 goto refresh;
494 case 'e':
496 grub_menu_entry_t e = grub_menu_get_entry (menu, first + offset);
497 if (e)
498 grub_menu_entry_run (e);
500 goto refresh;
502 default:
503 break;
506 grub_refresh ();
510 /* Never reach here. */
511 return -1;
514 /* Callback invoked immediately before a menu entry is executed. */
515 static void
516 notify_booting (grub_menu_entry_t entry,
517 void *userdata __attribute__((unused)))
519 grub_printf (" Booting \'%s\'\n\n", entry->title);
522 /* Callback invoked when a default menu entry executed because of a timeout
523 has failed and an attempt will be made to execute the next fallback
524 entry, ENTRY. */
525 static void
526 notify_fallback (grub_menu_entry_t entry,
527 void *userdata __attribute__((unused)))
529 grub_printf ("\n Falling back to \'%s\'\n\n", entry->title);
530 grub_millisleep (DEFAULT_ENTRY_ERROR_DELAY_MS);
533 /* Callback invoked when a menu entry has failed and there is no remaining
534 fallback entry to attempt. */
535 static void
536 notify_execution_failure (void *userdata __attribute__((unused)))
538 if (grub_errno != GRUB_ERR_NONE)
540 grub_print_error ();
541 grub_errno = GRUB_ERR_NONE;
543 grub_printf ("\n Failed to boot default entries.\n");
544 grub_wait_after_message ();
547 /* Callbacks used by the text menu to provide user feedback when menu entries
548 are executed. */
549 static struct grub_menu_execute_callback execution_callback =
551 .notify_booting = notify_booting,
552 .notify_fallback = notify_fallback,
553 .notify_failure = notify_execution_failure
556 static grub_err_t
557 show_text_menu (grub_menu_t menu, int nested)
559 while (1)
561 int boot_entry;
562 grub_menu_entry_t e;
563 int auto_boot;
565 boot_entry = run_menu (menu, nested, &auto_boot);
566 if (boot_entry < 0)
567 break;
569 e = grub_menu_get_entry (menu, boot_entry);
570 if (! e)
571 continue; /* Menu is empty. */
573 grub_cls ();
574 grub_setcursor (1);
576 if (auto_boot)
578 grub_menu_execute_with_fallback (menu, e, &execution_callback, 0);
580 else
582 grub_errno = GRUB_ERR_NONE;
583 grub_menu_execute_entry (e);
584 if (grub_errno != GRUB_ERR_NONE)
586 grub_print_error ();
587 grub_errno = GRUB_ERR_NONE;
588 grub_wait_after_message ();
593 return GRUB_ERR_NONE;
596 struct grub_menu_viewer grub_normal_text_menu_viewer =
598 .name = "text",
599 .show_menu = show_text_menu