view.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /* view.c - Graphical menu interface MVC view. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008 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/types.h>
  20. #include <grub/file.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/err.h>
  24. #include <grub/dl.h>
  25. #include <grub/normal.h>
  26. #include <grub/video.h>
  27. #include <grub/gfxterm.h>
  28. #include <grub/bitmap.h>
  29. #include <grub/bitmap_scale.h>
  30. #include <grub/term.h>
  31. #include <grub/gfxwidgets.h>
  32. #include <grub/time.h>
  33. #include <grub/menu.h>
  34. #include <grub/menu_viewer.h>
  35. #include <grub/gfxmenu_view.h>
  36. #include <grub/gui_string_util.h>
  37. #include <grub/icon_manager.h>
  38. static void
  39. init_terminal (grub_gfxmenu_view_t view);
  40. static grub_video_rect_t term_rect;
  41. static grub_gfxmenu_view_t term_view;
  42. /* Create a new view object, loading the theme specified by THEME_PATH and
  43. associating MODEL with the view. */
  44. grub_gfxmenu_view_t
  45. grub_gfxmenu_view_new (const char *theme_path,
  46. int width, int height)
  47. {
  48. grub_gfxmenu_view_t view;
  49. grub_font_t default_font;
  50. grub_gui_color_t default_fg_color;
  51. grub_gui_color_t default_bg_color;
  52. view = grub_malloc (sizeof (*view));
  53. if (! view)
  54. return 0;
  55. view->screen.x = 0;
  56. view->screen.y = 0;
  57. view->screen.width = width;
  58. view->screen.height = height;
  59. default_font = grub_font_get ("Unknown Regular 16");
  60. default_fg_color = grub_gui_color_rgb (0, 0, 0);
  61. default_bg_color = grub_gui_color_rgb (255, 255, 255);
  62. view->canvas = 0;
  63. view->title_font = default_font;
  64. view->message_font = default_font;
  65. view->terminal_font_name = grub_strdup ("Fixed 10");
  66. view->title_color = default_fg_color;
  67. view->message_color = default_bg_color;
  68. view->message_bg_color = default_fg_color;
  69. view->desktop_image = 0;
  70. view->desktop_color = default_bg_color;
  71. view->terminal_box = grub_gfxmenu_create_box (0, 0);
  72. view->title_text = grub_strdup ("GRUB Boot Menu");
  73. view->progress_message_text = 0;
  74. view->theme_path = 0;
  75. /* Set the timeout bar's frame. */
  76. view->progress_message_frame.width = view->screen.width * 4 / 5;
  77. view->progress_message_frame.height = 50;
  78. view->progress_message_frame.x = view->screen.x
  79. + (view->screen.width - view->progress_message_frame.width) / 2;
  80. view->progress_message_frame.y = view->screen.y
  81. + view->screen.height - 90 - 20 - view->progress_message_frame.height;
  82. if (grub_gfxmenu_view_load_theme (view, theme_path) != 0)
  83. {
  84. grub_gfxmenu_view_destroy (view);
  85. return 0;
  86. }
  87. return view;
  88. }
  89. /* Destroy the view object. All used memory is freed. */
  90. void
  91. grub_gfxmenu_view_destroy (grub_gfxmenu_view_t view)
  92. {
  93. if (!view)
  94. return;
  95. grub_video_bitmap_destroy (view->desktop_image);
  96. if (view->terminal_box)
  97. view->terminal_box->destroy (view->terminal_box);
  98. grub_free (view->terminal_font_name);
  99. grub_free (view->title_text);
  100. grub_free (view->progress_message_text);
  101. grub_free (view->theme_path);
  102. if (view->canvas)
  103. view->canvas->component.ops->destroy (view->canvas);
  104. grub_free (view);
  105. }
  106. static void
  107. redraw_background (grub_gfxmenu_view_t view,
  108. const grub_video_rect_t *bounds)
  109. {
  110. if (view->desktop_image)
  111. {
  112. struct grub_video_bitmap *img = view->desktop_image;
  113. grub_video_blit_bitmap (img, GRUB_VIDEO_BLIT_REPLACE,
  114. bounds->x, bounds->y,
  115. bounds->x - view->screen.x,
  116. bounds->y - view->screen.y,
  117. bounds->width, bounds->height);
  118. }
  119. else
  120. {
  121. grub_video_fill_rect (grub_gui_map_color (view->desktop_color),
  122. bounds->x, bounds->y,
  123. bounds->width, bounds->height);
  124. }
  125. }
  126. static void
  127. draw_title (grub_gfxmenu_view_t view)
  128. {
  129. if (! view->title_text)
  130. return;
  131. /* Center the title. */
  132. int title_width = grub_font_get_string_width (view->title_font,
  133. view->title_text);
  134. int x = (view->screen.width - title_width) / 2;
  135. int y = 40 + grub_font_get_ascent (view->title_font);
  136. grub_font_draw_string (view->title_text,
  137. view->title_font,
  138. grub_gui_map_color (view->title_color),
  139. x, y);
  140. }
  141. struct progress_value_data
  142. {
  143. int visible;
  144. int start;
  145. int end;
  146. int value;
  147. };
  148. struct grub_gfxmenu_timeout_notify *grub_gfxmenu_timeout_notifications;
  149. static void
  150. update_timeouts (int visible, int start, int value, int end)
  151. {
  152. struct grub_gfxmenu_timeout_notify *cur;
  153. for (cur = grub_gfxmenu_timeout_notifications; cur; cur = cur->next)
  154. cur->set_state (cur->self, visible, start, value, end);
  155. }
  156. static void
  157. redraw_timeouts (struct grub_gfxmenu_view *view)
  158. {
  159. struct grub_gfxmenu_timeout_notify *cur;
  160. for (cur = grub_gfxmenu_timeout_notifications; cur; cur = cur->next)
  161. {
  162. grub_video_rect_t bounds;
  163. cur->self->ops->get_bounds (cur->self, &bounds);
  164. grub_gfxmenu_view_redraw (view, &bounds);
  165. }
  166. }
  167. void
  168. grub_gfxmenu_print_timeout (int timeout, void *data)
  169. {
  170. struct grub_gfxmenu_view *view = data;
  171. if (view->first_timeout == -1)
  172. view->first_timeout = timeout;
  173. update_timeouts (1, -(view->first_timeout + 1), -timeout, 0);
  174. redraw_timeouts (view);
  175. grub_video_swap_buffers ();
  176. if (view->double_repaint)
  177. redraw_timeouts (view);
  178. }
  179. void
  180. grub_gfxmenu_clear_timeout (void *data)
  181. {
  182. struct grub_gfxmenu_view *view = data;
  183. update_timeouts (0, 1, 0, 0);
  184. redraw_timeouts (view);
  185. grub_video_swap_buffers ();
  186. if (view->double_repaint)
  187. redraw_timeouts (view);
  188. }
  189. static void
  190. update_menu_visit (grub_gui_component_t component,
  191. void *userdata)
  192. {
  193. grub_gfxmenu_view_t view;
  194. view = userdata;
  195. if (component->ops->is_instance (component, "list"))
  196. {
  197. grub_gui_list_t list = (grub_gui_list_t) component;
  198. list->ops->set_view_info (list, view);
  199. }
  200. }
  201. /* Update any boot menu components with the current menu model and
  202. theme path. */
  203. static void
  204. update_menu_components (grub_gfxmenu_view_t view)
  205. {
  206. grub_gui_iterate_recursively ((grub_gui_component_t) view->canvas,
  207. update_menu_visit, view);
  208. }
  209. static void
  210. draw_message (grub_gfxmenu_view_t view)
  211. {
  212. char *text = view->progress_message_text;
  213. grub_video_rect_t f = view->progress_message_frame;
  214. if (! text)
  215. return;
  216. grub_font_t font = view->message_font;
  217. grub_video_color_t color = grub_gui_map_color (view->message_color);
  218. /* Border. */
  219. grub_video_fill_rect (color,
  220. f.x-1, f.y-1, f.width+2, f.height+2);
  221. /* Fill. */
  222. grub_video_fill_rect (grub_gui_map_color (view->message_bg_color),
  223. f.x, f.y, f.width, f.height);
  224. /* Center the text. */
  225. int text_width = grub_font_get_string_width (font, text);
  226. int x = f.x + (f.width - text_width) / 2;
  227. int y = (f.y + (f.height - grub_font_get_descent (font)) / 2
  228. + grub_font_get_ascent (font) / 2);
  229. grub_font_draw_string (text, font, color, x, y);
  230. }
  231. void
  232. grub_gfxmenu_view_redraw (grub_gfxmenu_view_t view,
  233. const grub_video_rect_t *region)
  234. {
  235. if (grub_video_have_common_points (&term_rect, region))
  236. grub_gfxterm_schedule_repaint ();
  237. grub_video_set_active_render_target (GRUB_VIDEO_RENDER_TARGET_DISPLAY);
  238. redraw_background (view, region);
  239. if (view->canvas)
  240. view->canvas->component.ops->paint (view->canvas, region);
  241. draw_title (view);
  242. if (grub_video_have_common_points (&view->progress_message_frame, region))
  243. draw_message (view);
  244. }
  245. void
  246. grub_gfxmenu_view_draw (grub_gfxmenu_view_t view)
  247. {
  248. init_terminal (view);
  249. /* Clear the screen; there may be garbage left over in video memory. */
  250. grub_video_fill_rect (grub_video_map_rgb (0, 0, 0),
  251. view->screen.x, view->screen.y,
  252. view->screen.width, view->screen.height);
  253. grub_video_swap_buffers ();
  254. if (view->double_repaint)
  255. grub_video_fill_rect (grub_video_map_rgb (0, 0, 0),
  256. view->screen.x, view->screen.y,
  257. view->screen.width, view->screen.height);
  258. update_menu_components (view);
  259. grub_gfxmenu_view_redraw (view, &view->screen);
  260. grub_video_swap_buffers ();
  261. if (view->double_repaint)
  262. grub_gfxmenu_view_redraw (view, &view->screen);
  263. }
  264. static void
  265. redraw_menu_visit (grub_gui_component_t component,
  266. void *userdata)
  267. {
  268. grub_gfxmenu_view_t view;
  269. view = userdata;
  270. if (component->ops->is_instance (component, "list"))
  271. {
  272. grub_gui_list_t list;
  273. grub_video_rect_t bounds;
  274. list = (grub_gui_list_t) component;
  275. component->ops->get_bounds (component, &bounds);
  276. grub_gfxmenu_view_redraw (view, &bounds);
  277. }
  278. }
  279. void
  280. grub_gfxmenu_redraw_menu (grub_gfxmenu_view_t view)
  281. {
  282. update_menu_components (view);
  283. grub_gui_iterate_recursively ((grub_gui_component_t) view->canvas,
  284. redraw_menu_visit, view);
  285. grub_video_swap_buffers ();
  286. if (view->double_repaint)
  287. {
  288. grub_gui_iterate_recursively ((grub_gui_component_t) view->canvas,
  289. redraw_menu_visit, view);
  290. }
  291. }
  292. void
  293. grub_gfxmenu_set_chosen_entry (int entry, void *data)
  294. {
  295. grub_gfxmenu_view_t view = data;
  296. view->selected = entry;
  297. grub_gfxmenu_redraw_menu (view);
  298. }
  299. static void
  300. grub_gfxmenu_draw_terminal_box (void)
  301. {
  302. grub_gfxmenu_box_t term_box;
  303. term_box = term_view->terminal_box;
  304. if (!term_box)
  305. return;
  306. term_box->set_content_size (term_box, term_rect.width,
  307. term_rect.height);
  308. term_box->draw (term_box,
  309. term_rect.x - term_box->get_left_pad (term_box),
  310. term_rect.y - term_box->get_top_pad (term_box));
  311. grub_video_swap_buffers ();
  312. if (term_view->double_repaint)
  313. term_box->draw (term_box,
  314. term_rect.x - term_box->get_left_pad (term_box),
  315. term_rect.y - term_box->get_top_pad (term_box));
  316. }
  317. static void
  318. init_terminal (grub_gfxmenu_view_t view)
  319. {
  320. term_rect.width = view->screen.width * 7 / 10;
  321. term_rect.height = view->screen.height * 7 / 10;
  322. term_rect.x = view->screen.x + view->screen.width * (10 - 7) / 10 / 2;
  323. term_rect.y = view->screen.y + view->screen.height * (10 - 7) / 10 / 2;
  324. term_view = view;
  325. /* Note: currently there is no API for changing the gfxterm font
  326. on the fly, so whatever font the initially loaded theme specifies
  327. will be permanent. */
  328. grub_gfxterm_set_window (GRUB_VIDEO_RENDER_TARGET_DISPLAY, term_rect.x,
  329. term_rect.y,
  330. term_rect.width, term_rect.height,
  331. view->double_repaint, view->terminal_font_name, 3);
  332. grub_gfxterm_decorator_hook = grub_gfxmenu_draw_terminal_box;
  333. }
  334. /* FIXME: previously notifications were displayed in special case.
  335. Is it necessary?
  336. */
  337. #if 0
  338. /* Sets MESSAGE as the progress message for the view.
  339. MESSAGE can be 0, in which case no message is displayed. */
  340. static void
  341. set_progress_message (grub_gfxmenu_view_t view, const char *message)
  342. {
  343. grub_free (view->progress_message_text);
  344. if (message)
  345. view->progress_message_text = grub_strdup (message);
  346. else
  347. view->progress_message_text = 0;
  348. }
  349. static void
  350. notify_booting (grub_menu_entry_t entry, void *userdata)
  351. {
  352. grub_gfxmenu_view_t view = (grub_gfxmenu_view_t) userdata;
  353. char *s = grub_malloc (100 + grub_strlen (entry->title));
  354. if (!s)
  355. return;
  356. grub_sprintf (s, "Booting '%s'", entry->title);
  357. set_progress_message (view, s);
  358. grub_free (s);
  359. grub_gfxmenu_view_redraw (view, &view->progress_message_frame);
  360. grub_video_swap_buffers ();
  361. if (view->double_repaint)
  362. grub_gfxmenu_view_redraw (view, &view->progress_message_frame);
  363. }
  364. static void
  365. notify_fallback (grub_menu_entry_t entry, void *userdata)
  366. {
  367. grub_gfxmenu_view_t view = (grub_gfxmenu_view_t) userdata;
  368. char *s = grub_malloc (100 + grub_strlen (entry->title));
  369. if (!s)
  370. return;
  371. grub_sprintf (s, "Falling back to '%s'", entry->title);
  372. set_progress_message (view, s);
  373. grub_free (s);
  374. grub_gfxmenu_view_redraw (view, &view->progress_message_frame);
  375. grub_video_swap_buffers ();
  376. if (view->double_repaint)
  377. grub_gfxmenu_view_redraw (view, &view->progress_message_frame);
  378. }
  379. static void
  380. notify_execution_failure (void *userdata __attribute__ ((unused)))
  381. {
  382. }
  383. static struct grub_menu_execute_callback execute_callback =
  384. {
  385. .notify_booting = notify_booting,
  386. .notify_fallback = notify_fallback,
  387. .notify_failure = notify_execution_failure
  388. };
  389. #endif