]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/normal/menu_text.c
cb1d3d61e4ab163acfe8c4754aedfdcd5938932d
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / grub-core / normal / menu_text.c
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.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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>
28 #include <grub/i18n.h>
29 #include <grub/charset.h>
30
31 static grub_uint8_t grub_color_menu_normal;
32 static grub_uint8_t grub_color_menu_highlight;
33
34 struct menu_viewer_data
35 {
36 int first, offset;
37 struct grub_term_screen_geometry geo;
38 enum {
39 TIMEOUT_UNKNOWN,
40 TIMEOUT_NORMAL,
41 TIMEOUT_TERSE,
42 TIMEOUT_TERSE_NO_MARGIN
43 } timeout_msg;
44 grub_menu_t menu;
45 struct grub_term_output *term;
46 };
47
48 static inline int
49 grub_term_cursor_x (const struct grub_term_screen_geometry *geo)
50 {
51 return (geo->first_entry_x + geo->entry_width);
52 }
53
54 grub_size_t
55 grub_getstringwidth (grub_uint32_t * str, const grub_uint32_t * last_position,
56 struct grub_term_output *term)
57 {
58 grub_ssize_t width = 0;
59
60 while (str < last_position)
61 {
62 struct grub_unicode_glyph glyph;
63 glyph.ncomb = 0;
64 str += grub_unicode_aglomerate_comb (str, last_position - str, &glyph);
65 width += grub_term_getcharwidth (term, &glyph);
66 grub_unicode_destroy_glyph (&glyph);
67 }
68 return width;
69 }
70
71 static int
72 grub_print_message_indented_real (const char *msg, int margin_left,
73 int margin_right,
74 struct grub_term_output *term, int dry_run)
75 {
76 grub_uint32_t *unicode_msg;
77 grub_uint32_t *last_position;
78 grub_size_t msg_len = grub_strlen (msg) + 2;
79 int ret = 0;
80
81 unicode_msg = grub_malloc (msg_len * sizeof (grub_uint32_t));
82
83 if (!unicode_msg)
84 return 0;
85
86 msg_len = grub_utf8_to_ucs4 (unicode_msg, msg_len,
87 (grub_uint8_t *) msg, -1, 0);
88
89 last_position = unicode_msg + msg_len;
90 *last_position = 0;
91
92 if (dry_run)
93 ret = grub_ucs4_count_lines (unicode_msg, last_position, margin_left,
94 margin_right, term);
95 else
96 grub_print_ucs4_menu (unicode_msg, last_position, margin_left,
97 margin_right, term, 0, -1, 0, 0);
98
99 grub_free (unicode_msg);
100
101 return ret;
102 }
103
104 void
105 grub_print_message_indented (const char *msg, int margin_left, int margin_right,
106 struct grub_term_output *term)
107 {
108 grub_print_message_indented_real (msg, margin_left, margin_right, term, 0);
109 }
110
111 static void
112 draw_border (struct grub_term_output *term, const struct grub_term_screen_geometry *geo)
113 {
114 int i;
115
116 grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
117
118 grub_term_gotoxy (term, (struct grub_term_coordinate) { geo->first_entry_x - 1,
119 geo->first_entry_y - 1 });
120 grub_putcode (GRUB_UNICODE_CORNER_UL, term);
121 for (i = 0; i < geo->entry_width + 1; i++)
122 grub_putcode (GRUB_UNICODE_HLINE, term);
123 grub_putcode (GRUB_UNICODE_CORNER_UR, term);
124
125 for (i = 0; i < geo->num_entries; i++)
126 {
127 grub_term_gotoxy (term, (struct grub_term_coordinate) { geo->first_entry_x - 1,
128 geo->first_entry_y + i });
129 grub_putcode (GRUB_UNICODE_VLINE, term);
130 grub_term_gotoxy (term,
131 (struct grub_term_coordinate) { geo->first_entry_x + geo->entry_width + 1,
132 geo->first_entry_y + i });
133 grub_putcode (GRUB_UNICODE_VLINE, term);
134 }
135
136 grub_term_gotoxy (term,
137 (struct grub_term_coordinate) { geo->first_entry_x - 1,
138 geo->first_entry_y - 1 + geo->num_entries + 1 });
139 grub_putcode (GRUB_UNICODE_CORNER_LL, term);
140 for (i = 0; i < geo->entry_width + 1; i++)
141 grub_putcode (GRUB_UNICODE_HLINE, term);
142 grub_putcode (GRUB_UNICODE_CORNER_LR, term);
143
144 grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
145
146 grub_term_gotoxy (term,
147 (struct grub_term_coordinate) { geo->first_entry_x - 1,
148 (geo->first_entry_y - 1 + geo->num_entries
149 + GRUB_TERM_MARGIN + 1) });
150 }
151
152 static int
153 print_message (int nested, int edit, struct grub_term_output *term, int dry_run)
154 {
155 int ret = 0;
156 grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
157
158 if (edit)
159 {
160 ret += grub_print_message_indented_real (_("Minimum Emacs-like screen editing is \
161 supported. TAB lists completions. Press Ctrl-x or F10 to boot, Ctrl-c or F2 for a \
162 command-line or ESC to discard edits and return to the GRUB menu."),
163 STANDARD_MARGIN, STANDARD_MARGIN,
164 term, dry_run);
165 }
166 else
167 {
168 char *msg_translated;
169
170 msg_translated = grub_xasprintf (_("Use the %C and %C keys to select which "
171 "entry is highlighted."),
172 GRUB_UNICODE_UPARROW,
173 GRUB_UNICODE_DOWNARROW);
174 if (!msg_translated)
175 return 0;
176 ret += grub_print_message_indented_real (msg_translated, STANDARD_MARGIN,
177 STANDARD_MARGIN, term, dry_run);
178
179 grub_free (msg_translated);
180
181 if (nested)
182 {
183 ret += grub_print_message_indented_real
184 (_("Press enter to boot the selected OS, "
185 "`e' to edit the commands before booting "
186 "or `c' for a command-line. ESC to return previous menu."),
187 STANDARD_MARGIN, STANDARD_MARGIN, term, dry_run);
188 }
189 else
190 {
191 ret += grub_print_message_indented_real("\n", STANDARD_MARGIN, STANDARD_MARGIN, term, dry_run);
192
193 ret += grub_print_message_indented_real(grub_env_get("VTOY_TEXT_MENU_VER"),
194 STANDARD_MARGIN, STANDARD_MARGIN, term, dry_run);
195
196 ret += grub_print_message_indented_real("\n", STANDARD_MARGIN, STANDARD_MARGIN, term, dry_run);
197 ret += grub_print_message_indented_real(grub_env_get("VTOY_HOTKEY_TIP"),
198 STANDARD_MARGIN, STANDARD_MARGIN, term, dry_run);
199 }
200 }
201 return ret;
202 }
203
204 static void
205 print_entry (int y, int highlight, grub_menu_entry_t entry,
206 const struct menu_viewer_data *data)
207 {
208 const char *title;
209 grub_size_t title_len;
210 grub_ssize_t len;
211 grub_uint32_t *unicode_title;
212 grub_ssize_t i;
213 grub_uint8_t old_color_normal, old_color_highlight;
214
215 title = entry ? entry->title : "";
216 title_len = grub_strlen (title);
217 unicode_title = grub_malloc (title_len * sizeof (*unicode_title));
218 if (! unicode_title)
219 /* XXX How to show this error? */
220 return;
221
222 len = grub_utf8_to_ucs4 (unicode_title, title_len,
223 (grub_uint8_t *) title, -1, 0);
224 if (len < 0)
225 {
226 /* It is an invalid sequence. */
227 grub_free (unicode_title);
228 return;
229 }
230
231 old_color_normal = grub_term_normal_color;
232 old_color_highlight = grub_term_highlight_color;
233 grub_term_normal_color = grub_color_menu_normal;
234 grub_term_highlight_color = grub_color_menu_highlight;
235 grub_term_setcolorstate (data->term, highlight
236 ? GRUB_TERM_COLOR_HIGHLIGHT
237 : GRUB_TERM_COLOR_NORMAL);
238
239 grub_term_gotoxy (data->term, (struct grub_term_coordinate) {
240 data->geo.first_entry_x, y });
241
242 for (i = 0; i < len; i++)
243 if (unicode_title[i] == '\n' || unicode_title[i] == '\b'
244 || unicode_title[i] == '\r' || unicode_title[i] == '\e')
245 unicode_title[i] = ' ';
246
247 if (data->geo.num_entries > 1)
248 grub_putcode (highlight ? '*' : ' ', data->term);
249
250 grub_print_ucs4_menu (unicode_title,
251 unicode_title + len,
252 0,
253 data->geo.right_margin,
254 data->term, 0, 1,
255 GRUB_UNICODE_RIGHTARROW, 0);
256
257 grub_term_setcolorstate (data->term, GRUB_TERM_COLOR_NORMAL);
258 grub_term_gotoxy (data->term,
259 (struct grub_term_coordinate) {
260 grub_term_cursor_x (&data->geo), y });
261
262 grub_term_normal_color = old_color_normal;
263 grub_term_highlight_color = old_color_highlight;
264
265 grub_term_setcolorstate (data->term, GRUB_TERM_COLOR_NORMAL);
266 grub_free (unicode_title);
267 }
268
269 static void
270 print_entries (grub_menu_t menu, const struct menu_viewer_data *data)
271 {
272 grub_menu_entry_t e;
273 int i;
274
275 grub_term_gotoxy (data->term,
276 (struct grub_term_coordinate) {
277 data->geo.first_entry_x + data->geo.entry_width
278 + data->geo.border + 1,
279 data->geo.first_entry_y });
280
281 if (data->geo.num_entries != 1)
282 {
283 if (data->first)
284 grub_putcode (GRUB_UNICODE_UPARROW, data->term);
285 else
286 grub_putcode (' ', data->term);
287 }
288 e = grub_menu_get_entry (menu, data->first);
289
290 for (i = 0; i < data->geo.num_entries; i++)
291 {
292 print_entry (data->geo.first_entry_y + i, data->offset == i,
293 e, data);
294 if (e)
295 e = e->next;
296 }
297
298 grub_term_gotoxy (data->term,
299 (struct grub_term_coordinate) { data->geo.first_entry_x + data->geo.entry_width
300 + data->geo.border + 1,
301 data->geo.first_entry_y + data->geo.num_entries - 1 });
302 if (data->geo.num_entries == 1)
303 {
304 if (data->first && e)
305 grub_putcode (GRUB_UNICODE_UPDOWNARROW, data->term);
306 else if (data->first)
307 grub_putcode (GRUB_UNICODE_UPARROW, data->term);
308 else if (e)
309 grub_putcode (GRUB_UNICODE_DOWNARROW, data->term);
310 else
311 grub_putcode (' ', data->term);
312 }
313 else
314 {
315 if (e)
316 grub_putcode (GRUB_UNICODE_DOWNARROW, data->term);
317 else
318 grub_putcode (' ', data->term);
319 }
320
321 grub_term_gotoxy (data->term,
322 (struct grub_term_coordinate) { grub_term_cursor_x (&data->geo),
323 data->geo.first_entry_y + data->offset });
324 }
325
326 /* Initialize the screen. If NESTED is non-zero, assume that this menu
327 is run from another menu or a command-line. If EDIT is non-zero, show
328 a message for the menu entry editor. */
329 void
330 grub_menu_init_page (int nested, int edit,
331 struct grub_term_screen_geometry *geo,
332 struct grub_term_output *term)
333 {
334 grub_uint8_t old_color_normal, old_color_highlight;
335 int msg_num_lines;
336 int bottom_message = 1;
337 int empty_lines = 1;
338 int version_msg = 1;
339
340 geo->border = 1;
341 geo->first_entry_x = 1 /* margin */ + 1 /* border */;
342 geo->entry_width = grub_term_width (term) - 5;
343
344 geo->first_entry_y = 2 /* two empty lines*/
345 + 1 /* GNU GRUB version text */ + 1 /* top border */;
346
347 geo->timeout_lines = 2;
348
349 /* 3 lines for timeout message and bottom margin. 2 lines for the border. */
350 geo->num_entries = grub_term_height (term) - geo->first_entry_y
351 - 1 /* bottom border */
352 - 1 /* empty line before info message*/
353 - geo->timeout_lines /* timeout */
354 - 1 /* empty final line */;
355 msg_num_lines = print_message (nested, edit, term, 1);
356 if (geo->num_entries - msg_num_lines < 3
357 || geo->entry_width < 10)
358 {
359 geo->num_entries += 4;
360 geo->first_entry_y -= 2;
361 empty_lines = 0;
362 geo->first_entry_x -= 1;
363 geo->entry_width += 1;
364 }
365 if (geo->num_entries - msg_num_lines < 3
366 || geo->entry_width < 10)
367 {
368 geo->num_entries += 2;
369 geo->first_entry_y -= 1;
370 geo->first_entry_x -= 1;
371 geo->entry_width += 2;
372 geo->border = 0;
373 }
374
375 if (geo->entry_width <= 0)
376 geo->entry_width = 1;
377
378 if (geo->num_entries - msg_num_lines < 3
379 && geo->timeout_lines == 2)
380 {
381 geo->timeout_lines = 1;
382 geo->num_entries++;
383 }
384
385 if (geo->num_entries - msg_num_lines < 3)
386 {
387 geo->num_entries += 1;
388 geo->first_entry_y -= 1;
389 version_msg = 0;
390 }
391
392 if (geo->num_entries - msg_num_lines >= 2)
393 geo->num_entries -= msg_num_lines;
394 else
395 bottom_message = 0;
396
397 /* By default, use the same colors for the menu. */
398 old_color_normal = grub_term_normal_color;
399 old_color_highlight = grub_term_highlight_color;
400 grub_color_menu_normal = grub_term_normal_color;
401 grub_color_menu_highlight = grub_term_highlight_color;
402
403 /* Then give user a chance to replace them. */
404 grub_parse_color_name_pair (&grub_color_menu_normal,
405 grub_env_get ("menu_color_normal"));
406 grub_parse_color_name_pair (&grub_color_menu_highlight,
407 grub_env_get ("menu_color_highlight"));
408
409 if (version_msg)
410 grub_normal_init_page (term, empty_lines);
411 else
412 grub_term_cls (term);
413
414 grub_term_normal_color = grub_color_menu_normal;
415 grub_term_highlight_color = grub_color_menu_highlight;
416 if (geo->border)
417 draw_border (term, geo);
418 grub_term_normal_color = old_color_normal;
419 grub_term_highlight_color = old_color_highlight;
420 geo->timeout_y = geo->first_entry_y + geo->num_entries
421 + geo->border + empty_lines;
422 if (bottom_message)
423 {
424 grub_term_gotoxy (term,
425 (struct grub_term_coordinate) { GRUB_TERM_MARGIN,
426 geo->timeout_y });
427
428 print_message (nested, edit, term, 0);
429 geo->timeout_y += msg_num_lines;
430 }
431 geo->right_margin = grub_term_width (term)
432 - geo->first_entry_x
433 - geo->entry_width - 1;
434 }
435
436 static void
437 menu_text_print_timeout (int timeout, void *dataptr)
438 {
439 struct menu_viewer_data *data = dataptr;
440 char *msg_translated = 0;
441
442 grub_term_gotoxy (data->term,
443 (struct grub_term_coordinate) { 0, data->geo.timeout_y });
444
445 if (data->timeout_msg == TIMEOUT_TERSE
446 || data->timeout_msg == TIMEOUT_TERSE_NO_MARGIN)
447 msg_translated = grub_xasprintf (_("%ds"), timeout);
448 else
449 msg_translated = grub_xasprintf (_("The highlighted entry will be executed automatically in %ds."), timeout);
450 if (!msg_translated)
451 {
452 grub_print_error ();
453 grub_errno = GRUB_ERR_NONE;
454 return;
455 }
456
457 if (data->timeout_msg == TIMEOUT_UNKNOWN)
458 {
459 data->timeout_msg = grub_print_message_indented_real (msg_translated,
460 3, 1, data->term, 1)
461 <= data->geo.timeout_lines ? TIMEOUT_NORMAL : TIMEOUT_TERSE;
462 if (data->timeout_msg == TIMEOUT_TERSE)
463 {
464 grub_free (msg_translated);
465 msg_translated = grub_xasprintf (_("%ds"), timeout);
466 if (grub_term_width (data->term) < 10)
467 data->timeout_msg = TIMEOUT_TERSE_NO_MARGIN;
468 }
469 }
470
471 grub_print_message_indented (msg_translated,
472 data->timeout_msg == TIMEOUT_TERSE_NO_MARGIN ? 0 : 3,
473 data->timeout_msg == TIMEOUT_TERSE_NO_MARGIN ? 0 : 1,
474 data->term);
475 grub_free (msg_translated);
476
477 grub_term_gotoxy (data->term,
478 (struct grub_term_coordinate) {
479 grub_term_cursor_x (&data->geo),
480 data->geo.first_entry_y + data->offset });
481 grub_term_refresh (data->term);
482 }
483
484 static void
485 menu_text_set_chosen_entry (int entry, void *dataptr)
486 {
487 struct menu_viewer_data *data = dataptr;
488 int oldoffset = data->offset;
489 int complete_redraw = 0;
490
491 data->offset = entry - data->first;
492 if (data->offset > data->geo.num_entries - 1)
493 {
494 data->first = entry - (data->geo.num_entries - 1);
495 data->offset = data->geo.num_entries - 1;
496 complete_redraw = 1;
497 }
498 if (data->offset < 0)
499 {
500 data->offset = 0;
501 data->first = entry;
502 complete_redraw = 1;
503 }
504 if (complete_redraw)
505 print_entries (data->menu, data);
506 else
507 {
508 print_entry (data->geo.first_entry_y + oldoffset, 0,
509 grub_menu_get_entry (data->menu, data->first + oldoffset),
510 data);
511 print_entry (data->geo.first_entry_y + data->offset, 1,
512 grub_menu_get_entry (data->menu, data->first + data->offset),
513 data);
514 }
515 grub_term_refresh (data->term);
516 }
517
518 static void
519 menu_text_fini (void *dataptr)
520 {
521 struct menu_viewer_data *data = dataptr;
522
523 grub_term_setcursor (data->term, 1);
524 grub_term_cls (data->term);
525 grub_free (data);
526 }
527
528 static void
529 menu_text_clear_timeout (void *dataptr)
530 {
531 struct menu_viewer_data *data = dataptr;
532 int i;
533
534 for (i = 0; i < data->geo.timeout_lines;i++)
535 {
536 grub_term_gotoxy (data->term, (struct grub_term_coordinate) {
537 0, data->geo.timeout_y + i });
538 grub_print_spaces (data->term, grub_term_width (data->term) - 1);
539 }
540 if (data->geo.num_entries <= 5 && !data->geo.border)
541 {
542 grub_term_gotoxy (data->term,
543 (struct grub_term_coordinate) {
544 data->geo.first_entry_x + data->geo.entry_width
545 + data->geo.border + 1,
546 data->geo.first_entry_y + data->geo.num_entries - 1
547 });
548 grub_putcode (' ', data->term);
549
550 data->geo.timeout_lines = 0;
551 data->geo.num_entries++;
552 print_entries (data->menu, data);
553 }
554 grub_term_gotoxy (data->term,
555 (struct grub_term_coordinate) {
556 grub_term_cursor_x (&data->geo),
557 data->geo.first_entry_y + data->offset });
558 grub_term_refresh (data->term);
559 }
560
561 grub_err_t
562 grub_menu_try_text (struct grub_term_output *term,
563 int entry, grub_menu_t menu, int nested)
564 {
565 struct menu_viewer_data *data;
566 struct grub_menu_viewer *instance;
567
568 instance = grub_zalloc (sizeof (*instance));
569 if (!instance)
570 return grub_errno;
571
572 data = grub_zalloc (sizeof (*data));
573 if (!data)
574 {
575 grub_free (instance);
576 return grub_errno;
577 }
578
579 data->term = term;
580 instance->data = data;
581 instance->set_chosen_entry = menu_text_set_chosen_entry;
582 instance->print_timeout = menu_text_print_timeout;
583 instance->clear_timeout = menu_text_clear_timeout;
584 instance->fini = menu_text_fini;
585
586 data->menu = menu;
587
588 data->offset = entry;
589 data->first = 0;
590
591 grub_term_setcursor (data->term, 0);
592 grub_menu_init_page (nested, 0, &data->geo, data->term);
593
594 if (data->offset > data->geo.num_entries - 1)
595 {
596 data->first = data->offset - (data->geo.num_entries - 1);
597 data->offset = data->geo.num_entries - 1;
598 }
599
600 print_entries (menu, data);
601 grub_term_refresh (data->term);
602 grub_menu_register_viewer (instance);
603
604 return GRUB_ERR_NONE;
605 }