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>
29
#include <grub/charset.h>
31
static grub_uint8_t grub_color_menu_normal;
32
static grub_uint8_t grub_color_menu_highlight;
34
struct menu_viewer_data
38
struct grub_term_output *term;
42
grub_getstringwidth (grub_uint32_t * str, const grub_uint32_t * last_position,
43
struct grub_term_output *term)
45
grub_ssize_t width = 0;
47
while (str < last_position)
49
struct grub_unicode_glyph glyph;
50
str += grub_unicode_aglomerate_comb (str, last_position - str, &glyph);
51
width += grub_term_getcharwidth (term, &glyph);
57
grub_print_message_indented (const char *msg, int margin_left, int margin_right,
58
struct grub_term_output *term)
60
grub_uint32_t *unicode_msg;
61
grub_uint32_t *last_position;
65
msg_len = grub_utf8_to_ucs4_alloc (msg, &unicode_msg, &last_position);
72
grub_print_ucs4 (unicode_msg, last_position, margin_left, margin_right, term);
74
grub_free (unicode_msg);
79
draw_border (struct grub_term_output *term)
83
grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
85
grub_term_gotoxy (term, GRUB_TERM_MARGIN, GRUB_TERM_TOP_BORDER_Y);
86
grub_putcode (GRUB_UNICODE_CORNER_UL, term);
87
for (i = 0; i < (unsigned) grub_term_border_width (term) - 2; i++)
88
grub_putcode (GRUB_UNICODE_HLINE, term);
89
grub_putcode (GRUB_UNICODE_CORNER_UR, term);
91
for (i = 0; i < (unsigned) grub_term_num_entries (term); i++)
93
grub_term_gotoxy (term, GRUB_TERM_MARGIN, GRUB_TERM_TOP_BORDER_Y + i + 1);
94
grub_putcode (GRUB_UNICODE_VLINE, term);
95
grub_term_gotoxy (term, GRUB_TERM_MARGIN + grub_term_border_width (term)
97
GRUB_TERM_TOP_BORDER_Y + i + 1);
98
grub_putcode (GRUB_UNICODE_VLINE, term);
101
grub_term_gotoxy (term, GRUB_TERM_MARGIN,
102
GRUB_TERM_TOP_BORDER_Y + grub_term_num_entries (term) + 1);
103
grub_putcode (GRUB_UNICODE_CORNER_LL, term);
104
for (i = 0; i < (unsigned) grub_term_border_width (term) - 2; i++)
105
grub_putcode (GRUB_UNICODE_HLINE, term);
106
grub_putcode (GRUB_UNICODE_CORNER_LR, term);
108
grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
110
grub_term_gotoxy (term, GRUB_TERM_MARGIN,
111
(GRUB_TERM_TOP_BORDER_Y + grub_term_num_entries (term)
112
+ GRUB_TERM_MARGIN + 1));
116
print_message (int nested, int edit, struct grub_term_output *term)
118
grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
122
grub_putcode ('\n', term);
123
grub_print_message_indented (_("Minimum Emacs-like screen editing is \
124
supported. TAB lists completions. Press Ctrl-x or F10 to boot, Ctrl-c or F2 for a \
125
command-line or ESC to discard edits and return to the GRUB menu."),
126
STANDARD_MARGIN, STANDARD_MARGIN, term);
130
const char *msg = _("Use the %C and %C keys to select which "
131
"entry is highlighted.\n");
132
char *msg_translated;
134
msg_translated = grub_xasprintf (msg, GRUB_UNICODE_UPARROW,
135
GRUB_UNICODE_DOWNARROW);
138
grub_putcode ('\n', term);
139
grub_print_message_indented (msg_translated, STANDARD_MARGIN,
140
STANDARD_MARGIN, term);
142
grub_free (msg_translated);
146
grub_print_message_indented
147
(_("Press enter to boot the selected OS, "
148
"\'e\' to edit the commands before booting "
149
"or \'c\' for a command-line. ESC to return previous menu.\n"),
150
STANDARD_MARGIN, STANDARD_MARGIN, term);
154
grub_print_message_indented
155
(_("Press enter to boot the selected OS, "
156
"\'e\' to edit the commands before booting "
157
"or \'c\' for a command-line.\n"),
158
STANDARD_MARGIN, STANDARD_MARGIN, term);
164
print_entry (int y, int highlight, grub_menu_entry_t entry,
165
struct grub_term_output *term)
169
grub_size_t title_len;
171
grub_uint32_t *unicode_title;
173
grub_uint8_t old_color_normal, old_color_highlight;
175
title = entry ? entry->title : "";
176
title_len = grub_strlen (title);
177
unicode_title = grub_malloc (title_len * sizeof (*unicode_title));
179
/* XXX How to show this error? */
182
len = grub_utf8_to_ucs4 (unicode_title, title_len,
183
(grub_uint8_t *) title, -1, 0);
186
/* It is an invalid sequence. */
187
grub_free (unicode_title);
191
grub_term_getcolor (term, &old_color_normal, &old_color_highlight);
192
grub_term_setcolor (term, grub_color_menu_normal, grub_color_menu_highlight);
193
grub_term_setcolorstate (term, highlight
194
? GRUB_TERM_COLOR_HIGHLIGHT
195
: GRUB_TERM_COLOR_NORMAL);
197
grub_term_gotoxy (term, GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN, y);
199
int last_printed = 0;
200
for (x = GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN + 1, i = 0;
201
x < (int) (GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (term)
202
- GRUB_TERM_MARGIN);)
205
&& x <= (int) (GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (term)
206
- GRUB_TERM_MARGIN - 1))
209
struct grub_unicode_glyph glyph;
211
i += grub_unicode_aglomerate_comb (unicode_title + i,
214
width = grub_term_getcharwidth (term, &glyph);
215
grub_free (glyph.combining);
217
if (x + width <= (int) (GRUB_TERM_LEFT_BORDER_X
218
+ grub_term_border_width (term)
219
- GRUB_TERM_MARGIN - 1))
227
grub_print_ucs4 (unicode_title,
228
unicode_title + last_printed, 0, 0, term);
230
if (last_printed != len)
232
grub_putcode (GRUB_UNICODE_RIGHTARROW, term);
233
struct grub_unicode_glyph pseudo_glyph = {
234
.base = GRUB_UNICODE_RIGHTARROW,
241
x += grub_term_getcharwidth (term, &pseudo_glyph);
244
for (; x < (int) (GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (term)
245
- GRUB_TERM_MARGIN); x++)
246
grub_putcode (' ', term);
248
grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
249
grub_putcode (' ', term);
251
grub_term_gotoxy (term, grub_term_cursor_x (term), y);
253
grub_term_setcolor (term, old_color_normal, old_color_highlight);
254
grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
255
grub_free (unicode_title);
259
print_entries (grub_menu_t menu, int first, int offset,
260
struct grub_term_output *term)
265
grub_term_gotoxy (term,
266
GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (term),
267
GRUB_TERM_FIRST_ENTRY_Y);
270
grub_putcode (GRUB_UNICODE_UPARROW, term);
272
grub_putcode (' ', term);
274
e = grub_menu_get_entry (menu, first);
276
for (i = 0; i < grub_term_num_entries (term); i++)
278
print_entry (GRUB_TERM_FIRST_ENTRY_Y + i, offset == i, e, term);
283
grub_term_gotoxy (term, GRUB_TERM_LEFT_BORDER_X
284
+ grub_term_border_width (term),
285
GRUB_TERM_TOP_BORDER_Y + grub_term_num_entries (term));
288
grub_putcode (GRUB_UNICODE_DOWNARROW, term);
290
grub_putcode (' ', term);
292
grub_term_gotoxy (term, grub_term_cursor_x (term),
293
GRUB_TERM_FIRST_ENTRY_Y + offset);
296
/* Initialize the screen. If NESTED is non-zero, assume that this menu
297
is run from another menu or a command-line. If EDIT is non-zero, show
298
a message for the menu entry editor. */
300
grub_menu_init_page (int nested, int edit,
301
struct grub_term_output *term)
303
grub_uint8_t old_color_normal, old_color_highlight;
305
grub_term_getcolor (term, &old_color_normal, &old_color_highlight);
307
/* By default, use the same colors for the menu. */
308
grub_color_menu_normal = old_color_normal;
309
grub_color_menu_highlight = old_color_highlight;
311
/* Then give user a chance to replace them. */
312
grub_parse_color_name_pair (&grub_color_menu_normal,
313
grub_env_get ("menu_color_normal"));
314
grub_parse_color_name_pair (&grub_color_menu_highlight,
315
grub_env_get ("menu_color_highlight"));
317
grub_normal_init_page (term);
318
grub_term_setcolor (term, grub_color_menu_normal, grub_color_menu_highlight);
320
grub_term_setcolor (term, old_color_normal, old_color_highlight);
321
print_message (nested, edit, term);
325
menu_text_print_timeout (int timeout, void *dataptr)
328
_("The highlighted entry will be executed automatically in %ds.");
329
struct menu_viewer_data *data = dataptr;
330
char *msg_translated;
333
grub_term_gotoxy (data->term, 0, grub_term_height (data->term) - 3);
335
msg_translated = grub_xasprintf (msg, timeout);
339
grub_errno = GRUB_ERR_NONE;
343
grub_print_message_indented (msg_translated, 3, 0, data->term);
345
posx = grub_term_getxy (data->term) >> 8;
346
grub_print_spaces (data->term, grub_term_width (data->term) - posx - 1);
348
grub_term_gotoxy (data->term,
349
grub_term_cursor_x (data->term),
350
GRUB_TERM_FIRST_ENTRY_Y + data->offset);
351
grub_term_refresh (data->term);
355
menu_text_set_chosen_entry (int entry, void *dataptr)
357
struct menu_viewer_data *data = dataptr;
358
int oldoffset = data->offset;
359
int complete_redraw = 0;
361
data->offset = entry - data->first;
362
if (data->offset > grub_term_num_entries (data->term) - 1)
364
data->first = entry - (grub_term_num_entries (data->term) - 1);
365
data->offset = grub_term_num_entries (data->term) - 1;
368
if (data->offset < 0)
375
print_entries (data->menu, data->first, data->offset, data->term);
378
print_entry (GRUB_TERM_FIRST_ENTRY_Y + oldoffset, 0,
379
grub_menu_get_entry (data->menu, data->first + oldoffset),
381
print_entry (GRUB_TERM_FIRST_ENTRY_Y + data->offset, 1,
382
grub_menu_get_entry (data->menu, data->first + data->offset),
385
grub_term_refresh (data->term);
389
menu_text_fini (void *dataptr)
391
struct menu_viewer_data *data = dataptr;
393
grub_term_setcursor (data->term, 1);
394
grub_term_cls (data->term);
399
menu_text_clear_timeout (void *dataptr)
401
struct menu_viewer_data *data = dataptr;
403
grub_term_gotoxy (data->term, 0, grub_term_height (data->term) - 3);
404
grub_print_spaces (data->term, grub_term_width (data->term) - 1);
405
grub_term_gotoxy (data->term, grub_term_cursor_x (data->term),
406
GRUB_TERM_FIRST_ENTRY_Y + data->offset);
407
grub_term_refresh (data->term);
411
grub_menu_try_text (struct grub_term_output *term,
412
int entry, grub_menu_t menu, int nested)
414
struct menu_viewer_data *data;
415
struct grub_menu_viewer *instance;
417
instance = grub_zalloc (sizeof (*instance));
421
data = grub_zalloc (sizeof (*data));
424
grub_free (instance);
429
instance->data = data;
430
instance->set_chosen_entry = menu_text_set_chosen_entry;
431
instance->print_timeout = menu_text_print_timeout;
432
instance->clear_timeout = menu_text_clear_timeout;
433
instance->fini = menu_text_fini;
437
data->offset = entry;
439
if (data->offset > grub_term_num_entries (data->term) - 1)
441
data->first = data->offset - (grub_term_num_entries (data->term) - 1);
442
data->offset = grub_term_num_entries (data->term) - 1;
445
grub_term_setcursor (data->term, 0);
446
grub_menu_init_page (nested, 0, data->term);
447
print_entries (menu, data->first, data->offset, data->term);
448
grub_term_refresh (data->term);
449
grub_menu_register_viewer (instance);
451
return GRUB_ERR_NONE;