menu_text.c 17 KB

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