1 /* menu_text.c - Basic text menu implementation. */
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>
25 #include <grub/time.h>
27 #include <grub/menu_viewer.h>
28 #include <grub/i18n.h>
30 /* Time to delay after displaying an error message about a default/fallback
31 entry failing to boot. */
32 #define DEFAULT_ENTRY_ERROR_DELAY_MS 2500
34 static grub_uint8_t grub_color_menu_normal
;
35 static grub_uint8_t grub_color_menu_highlight
;
37 /* Wait until the user pushes any key so that the user
38 can see what happened. */
40 grub_wait_after_message (void)
43 grub_printf (_("Press any key to continue..."));
44 (void) grub_getkey ();
49 print_spaces (int number_spaces
)
52 for (i
= 0; i
< number_spaces
; i
++)
57 grub_print_ucs4 (const grub_uint32_t
* str
,
58 const grub_uint32_t
* last_position
)
60 while (str
< last_position
)
68 getstringwidth (grub_uint32_t
* str
, const grub_uint32_t
* last_position
)
70 grub_ssize_t width
= 0;
72 while (str
< last_position
)
74 width
+= grub_getcharwidth (*str
);
81 print_message_indented (const char *msg
)
83 const int line_len
= GRUB_TERM_WIDTH
- grub_getcharwidth ('m') * 15;
85 grub_uint32_t
*unicode_msg
;
87 grub_ssize_t msg_len
= grub_strlen (msg
);
89 unicode_msg
= grub_malloc (msg_len
* sizeof (*unicode_msg
));
91 msg_len
= grub_utf8_to_ucs4 (unicode_msg
, msg_len
,
92 (grub_uint8_t
*) msg
, -1, 0);
96 grub_printf ("print_message_indented ERROR1: %s", msg
);
102 grub_printf ("print_message_indented ERROR2: %s", msg
);
103 grub_free (unicode_msg
);
107 const grub_uint32_t
*last_position
= unicode_msg
+ msg_len
;
109 grub_uint32_t
*current_position
= unicode_msg
;
111 grub_uint32_t
*next_new_line
= unicode_msg
;
113 while (current_position
< last_position
)
115 next_new_line
= (grub_uint32_t
*) last_position
;
117 while (getstringwidth (current_position
, next_new_line
) > line_len
118 || (*next_new_line
!= ' ' && next_new_line
> current_position
&&
119 next_new_line
!= last_position
))
124 if (next_new_line
== current_position
)
126 next_new_line
= (next_new_line
+ line_len
> last_position
) ?
127 (grub_uint32_t
*) last_position
: next_new_line
+ line_len
;
131 grub_print_ucs4 (current_position
, next_new_line
);
135 current_position
= next_new_line
;
137 grub_free (unicode_msg
);
146 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL
);
148 grub_gotoxy (GRUB_TERM_MARGIN
, GRUB_TERM_TOP_BORDER_Y
);
149 grub_putcode (GRUB_TERM_DISP_UL
);
150 for (i
= 0; i
< (unsigned) GRUB_TERM_BORDER_WIDTH
- 2; i
++)
151 grub_putcode (GRUB_TERM_DISP_HLINE
);
152 grub_putcode (GRUB_TERM_DISP_UR
);
154 for (i
= 0; i
< (unsigned) GRUB_TERM_NUM_ENTRIES
; i
++)
156 grub_gotoxy (GRUB_TERM_MARGIN
, GRUB_TERM_TOP_BORDER_Y
+ i
+ 1);
157 grub_putcode (GRUB_TERM_DISP_VLINE
);
158 grub_gotoxy (GRUB_TERM_MARGIN
+ GRUB_TERM_BORDER_WIDTH
- 1,
159 GRUB_TERM_TOP_BORDER_Y
+ i
+ 1);
160 grub_putcode (GRUB_TERM_DISP_VLINE
);
163 grub_gotoxy (GRUB_TERM_MARGIN
,
164 GRUB_TERM_TOP_BORDER_Y
+ GRUB_TERM_NUM_ENTRIES
+ 1);
165 grub_putcode (GRUB_TERM_DISP_LL
);
166 for (i
= 0; i
< (unsigned) GRUB_TERM_BORDER_WIDTH
- 2; i
++)
167 grub_putcode (GRUB_TERM_DISP_HLINE
);
168 grub_putcode (GRUB_TERM_DISP_LR
);
170 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL
);
172 grub_gotoxy (GRUB_TERM_MARGIN
,
173 (GRUB_TERM_TOP_BORDER_Y
+ GRUB_TERM_NUM_ENTRIES
174 + GRUB_TERM_MARGIN
+ 1));
178 print_message (int nested
, int edit
)
180 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL
);
185 print_message_indented (_("Minimum Emacs-like screen editing is \
186 supported. TAB lists completions. Press Ctrl-x to boot, Ctrl-c for a \
187 command-line or ESC to return menu."));
191 const char *msg
= _("Use the %C and %C keys to select which \
192 entry is highlighted.");
193 char *msg_translated
=
194 grub_malloc (sizeof (char) * grub_strlen (msg
) + 1);
196 grub_sprintf (msg_translated
, msg
, (grub_uint32_t
) GRUB_TERM_DISP_UP
,
197 (grub_uint32_t
) GRUB_TERM_DISP_DOWN
);
199 print_message_indented (msg_translated
);
201 grub_free (msg_translated
);
203 print_message_indented (_("Press enter to boot the selected OS, \
204 \'e\' to edit the commands before booting or \'c\' for a command-line."));
209 grub_printf (_("ESC to return previous menu."));
215 print_entry (int y
, int highlight
, grub_menu_entry_t entry
)
219 grub_size_t title_len
;
221 grub_uint32_t
*unicode_title
;
223 grub_uint8_t old_color_normal
, old_color_highlight
;
225 title
= entry
? entry
->title
: "";
226 title_len
= grub_strlen (title
);
227 unicode_title
= grub_malloc (title_len
* sizeof (*unicode_title
));
229 /* XXX How to show this error? */
232 len
= grub_utf8_to_ucs4 (unicode_title
, title_len
,
233 (grub_uint8_t
*) title
, -1, 0);
236 /* It is an invalid sequence. */
237 grub_free (unicode_title
);
241 grub_getcolor (&old_color_normal
, &old_color_highlight
);
242 grub_setcolor (grub_color_menu_normal
, grub_color_menu_highlight
);
243 grub_setcolorstate (highlight
244 ? GRUB_TERM_COLOR_HIGHLIGHT
245 : GRUB_TERM_COLOR_NORMAL
);
247 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X
+ GRUB_TERM_MARGIN
, y
);
249 for (x
= GRUB_TERM_LEFT_BORDER_X
+ GRUB_TERM_MARGIN
+ 1, i
= 0;
250 x
< GRUB_TERM_LEFT_BORDER_X
+ GRUB_TERM_BORDER_WIDTH
- GRUB_TERM_MARGIN
;
254 && x
<= (GRUB_TERM_LEFT_BORDER_X
+ GRUB_TERM_BORDER_WIDTH
255 - GRUB_TERM_MARGIN
- 1))
259 width
= grub_getcharwidth (unicode_title
[i
]);
261 if (x
+ width
> (GRUB_TERM_LEFT_BORDER_X
+ GRUB_TERM_BORDER_WIDTH
262 - GRUB_TERM_MARGIN
- 1))
263 grub_putcode (GRUB_TERM_DISP_RIGHT
);
265 grub_putcode (unicode_title
[i
]);
275 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL
);
278 grub_gotoxy (GRUB_TERM_CURSOR_X
, y
);
280 grub_setcolor (old_color_normal
, old_color_highlight
);
281 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL
);
282 grub_free (unicode_title
);
286 print_entries (grub_menu_t menu
, int first
, int offset
)
291 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X
+ GRUB_TERM_BORDER_WIDTH
,
292 GRUB_TERM_FIRST_ENTRY_Y
);
295 grub_putcode (GRUB_TERM_DISP_UP
);
299 e
= grub_menu_get_entry (menu
, first
);
301 for (i
= 0; i
< GRUB_TERM_NUM_ENTRIES
; i
++)
303 print_entry (GRUB_TERM_FIRST_ENTRY_Y
+ i
, offset
== i
, e
);
308 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X
+ GRUB_TERM_BORDER_WIDTH
,
309 GRUB_TERM_TOP_BORDER_Y
+ GRUB_TERM_NUM_ENTRIES
);
312 grub_putcode (GRUB_TERM_DISP_DOWN
);
316 grub_gotoxy (GRUB_TERM_CURSOR_X
, GRUB_TERM_FIRST_ENTRY_Y
+ offset
);
319 /* Initialize the screen. If NESTED is non-zero, assume that this menu
320 is run from another menu or a command-line. If EDIT is non-zero, show
321 a message for the menu entry editor. */
323 grub_menu_init_page (int nested
, int edit
)
325 grub_uint8_t old_color_normal
, old_color_highlight
;
327 grub_getcolor (&old_color_normal
, &old_color_highlight
);
329 /* By default, use the same colors for the menu. */
330 grub_color_menu_normal
= old_color_normal
;
331 grub_color_menu_highlight
= old_color_highlight
;
333 /* Then give user a chance to replace them. */
334 grub_parse_color_name_pair (&grub_color_menu_normal
, grub_env_get ("menu_color_normal"));
335 grub_parse_color_name_pair (&grub_color_menu_highlight
, grub_env_get ("menu_color_highlight"));
337 grub_normal_init_page ();
338 grub_setcolor (grub_color_menu_normal
, grub_color_menu_highlight
);
340 grub_setcolor (old_color_normal
, old_color_highlight
);
341 print_message (nested
, edit
);
344 /* Get the entry number from the variable NAME. */
346 get_entry_number (const char *name
)
351 val
= grub_env_get (name
);
357 entry
= (int) grub_strtoul (val
, 0, 0);
359 if (grub_errno
!= GRUB_ERR_NONE
)
361 grub_errno
= GRUB_ERR_NONE
;
371 print_timeout (int timeout
, int offset
, int second_stage
)
374 _("The highlighted entry will be booted automatically in %ds.");
375 const int msg_localized_len
= grub_strlen (msg
);
376 const int number_spaces
= GRUB_TERM_WIDTH
- msg_localized_len
- 3;
378 char *msg_end
= grub_strchr (msg
, '%');
380 grub_gotoxy (second_stage
? (msg_end
- msg
+ 3) : 3, GRUB_TERM_HEIGHT
- 3);
381 grub_printf (second_stage
? msg_end
: msg
, timeout
);
382 print_spaces (second_stage
? number_spaces
: 0);
384 grub_gotoxy (GRUB_TERM_CURSOR_X
, GRUB_TERM_FIRST_ENTRY_Y
+ offset
);
388 /* Show the menu and handle menu entry selection. Returns the menu entry
389 index that should be executed or -1 if no entry should be executed (e.g.,
390 Esc pressed to exit a sub-menu or switching menu viewers).
391 If the return value is not -1, then *AUTO_BOOT is nonzero iff the menu
392 entry to be executed is a result of an automatic default selection because
395 run_menu (grub_menu_t menu
, int nested
, int *auto_boot
)
398 grub_uint64_t saved_time
;
404 default_entry
= get_entry_number ("default");
406 /* If DEFAULT_ENTRY is not within the menu entries, fall back to
408 if (default_entry
< 0 || default_entry
>= menu
->size
)
411 /* If timeout is 0, drawing is pointless (and ugly). */
412 if (grub_menu_get_timeout () == 0)
415 return default_entry
;
418 offset
= default_entry
;
419 if (offset
> GRUB_TERM_NUM_ENTRIES
- 1)
421 first
= offset
- (GRUB_TERM_NUM_ENTRIES
- 1);
422 offset
= GRUB_TERM_NUM_ENTRIES
- 1;
425 /* Initialize the time. */
426 saved_time
= grub_get_time_ms ();
430 grub_menu_init_page (nested
, 0);
431 print_entries (menu
, first
, offset
);
434 timeout
= grub_menu_get_timeout ();
437 print_timeout (timeout
, offset
, 0);
442 timeout
= grub_menu_get_timeout ();
446 grub_uint64_t current_time
;
448 current_time
= grub_get_time_ms ();
449 if (current_time
- saved_time
>= 1000)
452 grub_menu_set_timeout (timeout
);
453 saved_time
= current_time
;
454 print_timeout (timeout
, offset
, 1);
460 grub_env_unset ("timeout");
462 return default_entry
;
465 if (grub_checkkey () >= 0 || timeout
< 0)
467 c
= GRUB_TERM_ASCII_CHAR (grub_getkey ());
471 grub_gotoxy (0, GRUB_TERM_HEIGHT
- 3);
472 print_spaces (GRUB_TERM_WIDTH
- 1);
474 grub_env_unset ("timeout");
475 grub_env_unset ("fallback");
476 grub_gotoxy (GRUB_TERM_CURSOR_X
, GRUB_TERM_FIRST_ENTRY_Y
+ offset
);
484 print_entries (menu
, first
, offset
);
488 offset
= menu
->size
- 1;
489 if (offset
> GRUB_TERM_NUM_ENTRIES
- 1)
491 first
= offset
- (GRUB_TERM_NUM_ENTRIES
- 1);
492 offset
= GRUB_TERM_NUM_ENTRIES
- 1;
494 print_entries (menu
, first
, offset
);
501 print_entry (GRUB_TERM_FIRST_ENTRY_Y
+ offset
, 0,
502 grub_menu_get_entry (menu
, first
+ offset
));
504 print_entry (GRUB_TERM_FIRST_ENTRY_Y
+ offset
, 1,
505 grub_menu_get_entry (menu
, first
+ offset
));
510 print_entries (menu
, first
, offset
);
516 if (menu
->size
> first
+ offset
+ 1)
518 if (offset
< GRUB_TERM_NUM_ENTRIES
- 1)
520 print_entry (GRUB_TERM_FIRST_ENTRY_Y
+ offset
, 0,
521 grub_menu_get_entry (menu
, first
+ offset
));
523 print_entry (GRUB_TERM_FIRST_ENTRY_Y
+ offset
, 1,
524 grub_menu_get_entry (menu
, first
+ offset
));
529 print_entries (menu
, first
, offset
);
534 case GRUB_TERM_PPAGE
:
541 first
-= GRUB_TERM_NUM_ENTRIES
;
549 print_entries (menu
, first
, offset
);
552 case GRUB_TERM_NPAGE
:
555 offset
+= GRUB_TERM_NUM_ENTRIES
- 1;
556 if (first
+ offset
>= menu
->size
)
558 offset
= menu
->size
- first
- 1;
563 first
+= GRUB_TERM_NUM_ENTRIES
;
565 if (first
+ offset
>= menu
->size
)
567 first
-= GRUB_TERM_NUM_ENTRIES
;
568 offset
+= GRUB_TERM_NUM_ENTRIES
;
570 if (offset
> menu
->size
- 1 ||
571 offset
> GRUB_TERM_NUM_ENTRIES
- 1)
573 offset
= menu
->size
- first
- 1;
575 if (offset
> GRUB_TERM_NUM_ENTRIES
)
577 first
+= offset
- GRUB_TERM_NUM_ENTRIES
+ 1;
578 offset
= GRUB_TERM_NUM_ENTRIES
- 1;
582 print_entries (menu
, first
, offset
);
590 return first
+ offset
;
601 grub_cmdline_run (1);
606 grub_menu_entry_t e
= grub_menu_get_entry (menu
, first
+ offset
);
608 grub_menu_entry_run (e
);
620 /* Never reach here. */
624 /* Callback invoked immediately before a menu entry is executed. */
626 notify_booting (grub_menu_entry_t entry
,
627 void *userdata
__attribute__((unused
)))
630 grub_printf (_("Booting \'%s\'"), entry
->title
);
631 grub_printf ("\n\n");
634 /* Callback invoked when a default menu entry executed because of a timeout
635 has failed and an attempt will be made to execute the next fallback
638 notify_fallback (grub_menu_entry_t entry
,
639 void *userdata
__attribute__((unused
)))
642 grub_printf (_("Falling back to \'%s\'"), entry
->title
);
643 grub_printf ("\n\n");
644 grub_millisleep (DEFAULT_ENTRY_ERROR_DELAY_MS
);
647 /* Callback invoked when a menu entry has failed and there is no remaining
648 fallback entry to attempt. */
650 notify_execution_failure (void *userdata
__attribute__((unused
)))
652 if (grub_errno
!= GRUB_ERR_NONE
)
655 grub_errno
= GRUB_ERR_NONE
;
658 grub_printf (_("Failed to boot default entries.\n"));
659 grub_wait_after_message ();
662 /* Callbacks used by the text menu to provide user feedback when menu entries
664 static struct grub_menu_execute_callback execution_callback
=
666 .notify_booting
= notify_booting
,
667 .notify_fallback
= notify_fallback
,
668 .notify_failure
= notify_execution_failure
672 show_text_menu (grub_menu_t menu
, int nested
)
680 boot_entry
= run_menu (menu
, nested
, &auto_boot
);
684 e
= grub_menu_get_entry (menu
, boot_entry
);
686 continue; /* Menu is empty. */
693 grub_menu_execute_with_fallback (menu
, e
, &execution_callback
, 0);
697 grub_errno
= GRUB_ERR_NONE
;
698 grub_menu_execute_entry (e
);
699 if (grub_errno
!= GRUB_ERR_NONE
)
702 grub_errno
= GRUB_ERR_NONE
;
703 grub_wait_after_message ();
708 return GRUB_ERR_NONE
;
711 struct grub_menu_viewer grub_normal_text_menu_viewer
=
714 .show_menu
= show_text_menu