gfxterm.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/term.h>
  19. #include <grub/types.h>
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/font.h>
  23. #include <grub/mm.h>
  24. #include <grub/env.h>
  25. #include <grub/video.h>
  26. #include <grub/gfxterm.h>
  27. #include <grub/bitmap.h>
  28. #include <grub/command.h>
  29. #include <grub/extcmd.h>
  30. #include <grub/i18n.h>
  31. GRUB_MOD_LICENSE ("GPLv3+");
  32. #define DEFAULT_VIDEO_MODE "auto"
  33. #define DEFAULT_BORDER_WIDTH 10
  34. #define DEFAULT_STANDARD_COLOR 0x07
  35. struct grub_dirty_region
  36. {
  37. int top_left_x;
  38. int top_left_y;
  39. int bottom_right_x;
  40. int bottom_right_y;
  41. };
  42. struct grub_colored_char
  43. {
  44. /* An Unicode codepoint. */
  45. struct grub_unicode_glyph code;
  46. /* Color values. */
  47. grub_video_color_t fg_color;
  48. grub_video_color_t bg_color;
  49. };
  50. struct grub_virtual_screen
  51. {
  52. /* Dimensions of the virtual screen in pixels. */
  53. unsigned int width;
  54. unsigned int height;
  55. /* Offset in the display in pixels. */
  56. unsigned int offset_x;
  57. unsigned int offset_y;
  58. /* TTY Character sizes in pixes. */
  59. unsigned int normal_char_width;
  60. unsigned int normal_char_height;
  61. /* Virtual screen TTY size in characters. */
  62. unsigned int columns;
  63. unsigned int rows;
  64. /* Current cursor location in characters. */
  65. unsigned int cursor_x;
  66. unsigned int cursor_y;
  67. /* Current cursor state. */
  68. int cursor_state;
  69. /* Font settings. */
  70. grub_font_t font;
  71. /* Terminal color settings. */
  72. grub_uint8_t standard_color_setting;
  73. grub_uint8_t term_color;
  74. /* Color settings. */
  75. grub_video_color_t fg_color;
  76. grub_video_color_t bg_color;
  77. grub_video_color_t bg_color_display;
  78. /* Text buffer for virtual screen. Contains (columns * rows) number
  79. of entries. */
  80. struct grub_colored_char *text_buffer;
  81. int total_scroll;
  82. int functional;
  83. };
  84. struct grub_gfxterm_window
  85. {
  86. unsigned x;
  87. unsigned y;
  88. unsigned width;
  89. unsigned height;
  90. int double_repaint;
  91. };
  92. static struct grub_video_render_target *render_target;
  93. void (*grub_gfxterm_decorator_hook) (void) = NULL;
  94. static struct grub_gfxterm_window window;
  95. static struct grub_virtual_screen virtual_screen;
  96. static int repaint_scheduled = 0;
  97. static int repaint_was_scheduled = 0;
  98. static void destroy_window (void);
  99. static struct grub_video_render_target *text_layer;
  100. struct grub_gfxterm_background grub_gfxterm_background;
  101. static struct grub_dirty_region dirty_region;
  102. static void dirty_region_reset (void);
  103. static int dirty_region_is_empty (void);
  104. static void dirty_region_add (int x, int y,
  105. unsigned int width, unsigned int height);
  106. static unsigned int calculate_normal_character_width (grub_font_t font);
  107. static unsigned char calculate_character_width (struct grub_font_glyph *glyph);
  108. static void grub_gfxterm_refresh (struct grub_term_output *term __attribute__ ((unused)));
  109. static grub_size_t
  110. grub_gfxterm_getcharwidth (struct grub_term_output *term __attribute__ ((unused)),
  111. const struct grub_unicode_glyph *c);
  112. static void
  113. set_term_color (grub_uint8_t term_color)
  114. {
  115. struct grub_video_render_target *old_target;
  116. /* Save previous target and switch to text layer. */
  117. grub_video_get_active_render_target (&old_target);
  118. grub_video_set_active_render_target (text_layer);
  119. /* Map terminal color to text layer compatible video colors. */
  120. virtual_screen.fg_color = grub_video_map_color(term_color & 0x0f);
  121. /* Special case: use black as transparent color. */
  122. if (((term_color >> 4) & 0x0f) == 0)
  123. {
  124. virtual_screen.bg_color = grub_video_map_rgba(0, 0, 0, 0);
  125. }
  126. else
  127. {
  128. virtual_screen.bg_color = grub_video_map_color((term_color >> 4) & 0x0f);
  129. }
  130. /* Restore previous target. */
  131. grub_video_set_active_render_target (old_target);
  132. }
  133. static void
  134. clear_char (struct grub_colored_char *c)
  135. {
  136. grub_unicode_destroy_glyph (&c->code);
  137. grub_unicode_set_glyph_from_code (&c->code, ' ');
  138. c->fg_color = virtual_screen.fg_color;
  139. c->bg_color = virtual_screen.bg_color;
  140. }
  141. static void
  142. grub_virtual_screen_free (void)
  143. {
  144. virtual_screen.functional = 0;
  145. /* If virtual screen has been allocated, free it. */
  146. if (virtual_screen.text_buffer != 0)
  147. {
  148. unsigned i;
  149. for (i = 0;
  150. i < virtual_screen.columns * virtual_screen.rows;
  151. i++)
  152. grub_unicode_destroy_glyph (&virtual_screen.text_buffer[i].code);
  153. grub_free (virtual_screen.text_buffer);
  154. }
  155. /* Reset virtual screen data. */
  156. grub_memset (&virtual_screen, 0, sizeof (virtual_screen));
  157. /* Free render targets. */
  158. grub_video_delete_render_target (text_layer);
  159. text_layer = 0;
  160. }
  161. static grub_err_t
  162. grub_virtual_screen_setup (unsigned int x, unsigned int y,
  163. unsigned int width, unsigned int height,
  164. grub_font_t font)
  165. {
  166. unsigned int i;
  167. /* Free old virtual screen. */
  168. grub_virtual_screen_free ();
  169. /* Initialize with default data. */
  170. virtual_screen.font = font;
  171. virtual_screen.width = width;
  172. virtual_screen.height = height;
  173. virtual_screen.offset_x = x;
  174. virtual_screen.offset_y = y;
  175. virtual_screen.normal_char_width =
  176. calculate_normal_character_width (virtual_screen.font);
  177. virtual_screen.normal_char_height =
  178. grub_font_get_max_char_height (virtual_screen.font);
  179. if (virtual_screen.normal_char_height == 0)
  180. virtual_screen.normal_char_height = 16;
  181. virtual_screen.cursor_x = 0;
  182. virtual_screen.cursor_y = 0;
  183. virtual_screen.cursor_state = 1;
  184. virtual_screen.total_scroll = 0;
  185. /* Calculate size of text buffer. */
  186. virtual_screen.columns = virtual_screen.width / virtual_screen.normal_char_width;
  187. virtual_screen.rows = virtual_screen.height / virtual_screen.normal_char_height;
  188. /* Allocate memory for text buffer. */
  189. virtual_screen.text_buffer =
  190. (struct grub_colored_char *) grub_malloc (virtual_screen.columns
  191. * virtual_screen.rows
  192. * sizeof (*virtual_screen.text_buffer));
  193. if (grub_errno != GRUB_ERR_NONE)
  194. return grub_errno;
  195. /* Create new render target for text layer. */
  196. grub_video_create_render_target (&text_layer,
  197. virtual_screen.width,
  198. virtual_screen.height,
  199. GRUB_VIDEO_MODE_TYPE_INDEX_COLOR
  200. | GRUB_VIDEO_MODE_TYPE_ALPHA);
  201. if (grub_errno != GRUB_ERR_NONE)
  202. return grub_errno;
  203. /* As we want to have colors compatible with rendering target,
  204. we can only have those after mode is initialized. */
  205. grub_video_set_active_render_target (text_layer);
  206. virtual_screen.standard_color_setting = DEFAULT_STANDARD_COLOR;
  207. virtual_screen.term_color = virtual_screen.standard_color_setting;
  208. set_term_color (virtual_screen.term_color);
  209. grub_video_set_active_render_target (render_target);
  210. virtual_screen.bg_color_display =
  211. grub_video_map_rgba_color (grub_gfxterm_background.default_bg_color);
  212. /* Clear out text buffer. */
  213. for (i = 0; i < virtual_screen.columns * virtual_screen.rows; i++)
  214. {
  215. virtual_screen.text_buffer[i].code.ncomb = 0;
  216. clear_char (&(virtual_screen.text_buffer[i]));
  217. }
  218. if (grub_errno)
  219. return grub_errno;
  220. virtual_screen.functional = 1;
  221. return GRUB_ERR_NONE;
  222. }
  223. void
  224. grub_gfxterm_schedule_repaint (void)
  225. {
  226. repaint_scheduled = 1;
  227. }
  228. grub_err_t
  229. grub_gfxterm_set_window (struct grub_video_render_target *target,
  230. int x, int y, int width, int height,
  231. int double_repaint,
  232. grub_font_t font, int border_width)
  233. {
  234. /* Clean up any prior instance. */
  235. destroy_window ();
  236. /* Set the render target. */
  237. render_target = target;
  238. /* Create virtual screen. */
  239. if (grub_virtual_screen_setup (border_width, border_width,
  240. width - 2 * border_width,
  241. height - 2 * border_width,
  242. font)
  243. != GRUB_ERR_NONE)
  244. {
  245. return grub_errno;
  246. }
  247. /* Set window bounds. */
  248. window.x = x;
  249. window.y = y;
  250. window.width = width;
  251. window.height = height;
  252. window.double_repaint = double_repaint;
  253. dirty_region_reset ();
  254. grub_gfxterm_schedule_repaint ();
  255. return grub_errno;
  256. }
  257. static grub_err_t
  258. grub_gfxterm_fullscreen (void)
  259. {
  260. const char *font_name;
  261. struct grub_video_mode_info mode_info;
  262. grub_video_color_t color;
  263. grub_err_t err;
  264. int double_redraw;
  265. grub_font_t font;
  266. err = grub_video_get_info (&mode_info);
  267. /* Figure out what mode we ended up. */
  268. if (err)
  269. return err;
  270. grub_video_set_active_render_target (GRUB_VIDEO_RENDER_TARGET_DISPLAY);
  271. double_redraw = mode_info.mode_type & GRUB_VIDEO_MODE_TYPE_DOUBLE_BUFFERED
  272. && !(mode_info.mode_type & GRUB_VIDEO_MODE_TYPE_UPDATING_SWAP);
  273. /* Make sure screen is set to the default background color. */
  274. color = grub_video_map_rgba_color (grub_gfxterm_background.default_bg_color);
  275. grub_video_fill_rect (color, 0, 0, mode_info.width, mode_info.height);
  276. if (double_redraw)
  277. {
  278. grub_video_swap_buffers ();
  279. grub_video_fill_rect (color, 0, 0, mode_info.width, mode_info.height);
  280. }
  281. /* Select the font to use. */
  282. font_name = grub_env_get ("gfxterm_font");
  283. if (! font_name)
  284. font_name = ""; /* Allow fallback to any font. */
  285. font = grub_font_get (font_name);
  286. if (!font)
  287. return grub_error (GRUB_ERR_BAD_FONT, "no font loaded");
  288. grub_gfxterm_decorator_hook = NULL;
  289. return grub_gfxterm_set_window (GRUB_VIDEO_RENDER_TARGET_DISPLAY,
  290. 0, 0, mode_info.width, mode_info.height,
  291. double_redraw,
  292. font, DEFAULT_BORDER_WIDTH);
  293. }
  294. static grub_err_t
  295. grub_gfxterm_term_init (struct grub_term_output *term __attribute__ ((unused)))
  296. {
  297. char *tmp;
  298. grub_err_t err;
  299. const char *modevar;
  300. /* Parse gfxmode environment variable if set. */
  301. modevar = grub_env_get ("gfxmode");
  302. if (! modevar || *modevar == 0)
  303. err = grub_video_set_mode (DEFAULT_VIDEO_MODE,
  304. GRUB_VIDEO_MODE_TYPE_PURE_TEXT, 0);
  305. else
  306. {
  307. tmp = grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
  308. if (!tmp)
  309. return grub_errno;
  310. err = grub_video_set_mode (tmp, GRUB_VIDEO_MODE_TYPE_PURE_TEXT, 0);
  311. grub_free (tmp);
  312. }
  313. if (err)
  314. return err;
  315. err = grub_gfxterm_fullscreen ();
  316. if (err)
  317. grub_video_restore ();
  318. return err;
  319. }
  320. static void
  321. destroy_window (void)
  322. {
  323. grub_virtual_screen_free ();
  324. }
  325. static grub_err_t
  326. grub_gfxterm_term_fini (struct grub_term_output *term __attribute__ ((unused)))
  327. {
  328. unsigned i;
  329. destroy_window ();
  330. grub_video_restore ();
  331. for (i = 0; i < virtual_screen.columns * virtual_screen.rows; i++)
  332. {
  333. grub_unicode_destroy_glyph (&virtual_screen.text_buffer[i].code);
  334. virtual_screen.text_buffer[i].code.ncomb = 0;
  335. virtual_screen.text_buffer[i].code.base = 0;
  336. }
  337. /* Clear error state. */
  338. grub_errno = GRUB_ERR_NONE;
  339. return GRUB_ERR_NONE;
  340. }
  341. static void
  342. redraw_screen_rect (unsigned int x, unsigned int y,
  343. unsigned int width, unsigned int height)
  344. {
  345. grub_video_color_t color;
  346. grub_video_rect_t saved_view;
  347. grub_video_set_active_render_target (render_target);
  348. /* Save viewport and set it to our window. */
  349. grub_video_get_viewport ((unsigned *) &saved_view.x,
  350. (unsigned *) &saved_view.y,
  351. (unsigned *) &saved_view.width,
  352. (unsigned *) &saved_view.height);
  353. grub_video_set_viewport (window.x, window.y, window.width, window.height);
  354. if (grub_gfxterm_background.bitmap)
  355. {
  356. /* Render bitmap as background. */
  357. grub_video_blit_bitmap (grub_gfxterm_background.bitmap,
  358. GRUB_VIDEO_BLIT_REPLACE, x, y,
  359. x, y,
  360. width, height);
  361. /* If bitmap is smaller than requested blit area, use background
  362. color. */
  363. color = virtual_screen.bg_color_display;
  364. /* Fill right side of the bitmap if needed. */
  365. if ((x + width >= grub_gfxterm_background.bitmap->mode_info.width)
  366. && (y < grub_gfxterm_background.bitmap->mode_info.height))
  367. {
  368. int w = (x + width) - grub_gfxterm_background.bitmap->mode_info.width;
  369. int h = height;
  370. unsigned int tx = x;
  371. if (y + height >= grub_gfxterm_background.bitmap->mode_info.height)
  372. {
  373. h = grub_gfxterm_background.bitmap->mode_info.height - y;
  374. }
  375. if (grub_gfxterm_background.bitmap->mode_info.width > tx)
  376. {
  377. tx = grub_gfxterm_background.bitmap->mode_info.width;
  378. }
  379. /* Render background layer. */
  380. grub_video_fill_rect (color, tx, y, w, h);
  381. }
  382. /* Fill bottom side of the bitmap if needed. */
  383. if (y + height >= grub_gfxterm_background.bitmap->mode_info.height)
  384. {
  385. int h = (y + height) - grub_gfxterm_background.bitmap->mode_info.height;
  386. unsigned int ty = y;
  387. if (grub_gfxterm_background.bitmap->mode_info.height > ty)
  388. {
  389. ty = grub_gfxterm_background.bitmap->mode_info.height;
  390. }
  391. /* Render background layer. */
  392. grub_video_fill_rect (color, x, ty, width, h);
  393. }
  394. }
  395. else
  396. {
  397. /* Render background layer. */
  398. color = virtual_screen.bg_color_display;
  399. grub_video_fill_rect (color, x, y, width, height);
  400. }
  401. if (grub_gfxterm_background.blend_text_bg)
  402. /* Render text layer as blended. */
  403. grub_video_blit_render_target (text_layer, GRUB_VIDEO_BLIT_BLEND, x, y,
  404. x - virtual_screen.offset_x,
  405. y - virtual_screen.offset_y,
  406. width, height);
  407. else
  408. /* Render text layer as replaced (to get texts background color). */
  409. grub_video_blit_render_target (text_layer, GRUB_VIDEO_BLIT_REPLACE, x, y,
  410. x - virtual_screen.offset_x,
  411. y - virtual_screen.offset_y,
  412. width, height);
  413. /* Restore saved viewport. */
  414. grub_video_set_viewport (saved_view.x, saved_view.y,
  415. saved_view.width, saved_view.height);
  416. grub_video_set_active_render_target (render_target);
  417. }
  418. static void
  419. dirty_region_reset (void)
  420. {
  421. dirty_region.top_left_x = -1;
  422. dirty_region.top_left_y = -1;
  423. dirty_region.bottom_right_x = -1;
  424. dirty_region.bottom_right_y = -1;
  425. repaint_was_scheduled = 0;
  426. }
  427. static int
  428. dirty_region_is_empty (void)
  429. {
  430. if ((dirty_region.top_left_x == -1)
  431. || (dirty_region.top_left_y == -1)
  432. || (dirty_region.bottom_right_x == -1)
  433. || (dirty_region.bottom_right_y == -1))
  434. return 1;
  435. return 0;
  436. }
  437. static void
  438. dirty_region_add_real (int x, int y, unsigned int width, unsigned int height)
  439. {
  440. if (dirty_region_is_empty ())
  441. {
  442. dirty_region.top_left_x = x;
  443. dirty_region.top_left_y = y;
  444. dirty_region.bottom_right_x = x + width - 1;
  445. dirty_region.bottom_right_y = y + height - 1;
  446. }
  447. else
  448. {
  449. if (x < dirty_region.top_left_x)
  450. dirty_region.top_left_x = x;
  451. if (y < dirty_region.top_left_y)
  452. dirty_region.top_left_y = y;
  453. if ((x + (int)width - 1) > dirty_region.bottom_right_x)
  454. dirty_region.bottom_right_x = x + width - 1;
  455. if ((y + (int)height - 1) > dirty_region.bottom_right_y)
  456. dirty_region.bottom_right_y = y + height - 1;
  457. }
  458. }
  459. static void
  460. dirty_region_add (int x, int y, unsigned int width, unsigned int height)
  461. {
  462. if ((width == 0) || (height == 0))
  463. return;
  464. if (repaint_scheduled)
  465. {
  466. dirty_region_add_real (0, 0,
  467. window.width, window.height);
  468. repaint_scheduled = 0;
  469. repaint_was_scheduled = 1;
  470. }
  471. dirty_region_add_real (x, y, width, height);
  472. }
  473. static void
  474. dirty_region_add_virtualscreen (void)
  475. {
  476. /* Mark virtual screen as dirty. */
  477. dirty_region_add (virtual_screen.offset_x, virtual_screen.offset_y,
  478. virtual_screen.width, virtual_screen.height);
  479. }
  480. static void
  481. dirty_region_redraw (void)
  482. {
  483. int x;
  484. int y;
  485. int width;
  486. int height;
  487. if (dirty_region_is_empty ())
  488. return;
  489. x = dirty_region.top_left_x;
  490. y = dirty_region.top_left_y;
  491. width = dirty_region.bottom_right_x - x + 1;
  492. height = dirty_region.bottom_right_y - y + 1;
  493. if (repaint_was_scheduled && grub_gfxterm_decorator_hook)
  494. grub_gfxterm_decorator_hook ();
  495. redraw_screen_rect (x, y, width, height);
  496. }
  497. static inline void
  498. paint_char (unsigned cx, unsigned cy)
  499. {
  500. struct grub_colored_char *p;
  501. struct grub_font_glyph *glyph;
  502. grub_video_color_t color;
  503. grub_video_color_t bgcolor;
  504. unsigned int x;
  505. unsigned int y;
  506. int ascent;
  507. unsigned int height;
  508. unsigned int width;
  509. if (cy + virtual_screen.total_scroll >= virtual_screen.rows)
  510. return;
  511. /* Find out active character. */
  512. p = (virtual_screen.text_buffer
  513. + cx + (cy * virtual_screen.columns));
  514. if (!p->code.base)
  515. return;
  516. /* Get glyph for character. */
  517. glyph = grub_font_construct_glyph (virtual_screen.font, &p->code);
  518. if (!glyph)
  519. {
  520. grub_errno = GRUB_ERR_NONE;
  521. return;
  522. }
  523. ascent = grub_font_get_ascent (virtual_screen.font);
  524. width = virtual_screen.normal_char_width * calculate_character_width(glyph);
  525. height = virtual_screen.normal_char_height;
  526. color = p->fg_color;
  527. bgcolor = p->bg_color;
  528. x = cx * virtual_screen.normal_char_width;
  529. y = (cy + virtual_screen.total_scroll) * virtual_screen.normal_char_height;
  530. /* Render glyph to text layer. */
  531. grub_video_set_active_render_target (text_layer);
  532. grub_video_fill_rect (bgcolor, x, y, width, height);
  533. grub_font_draw_glyph (glyph, color, x, y + ascent);
  534. grub_video_set_active_render_target (render_target);
  535. /* Mark character to be drawn. */
  536. dirty_region_add (virtual_screen.offset_x + x, virtual_screen.offset_y + y,
  537. width, height);
  538. }
  539. static inline void
  540. write_char (void)
  541. {
  542. paint_char (virtual_screen.cursor_x, virtual_screen.cursor_y);
  543. }
  544. static inline void
  545. draw_cursor (int show)
  546. {
  547. unsigned int x;
  548. unsigned int y;
  549. unsigned int width;
  550. unsigned int height;
  551. grub_video_color_t color;
  552. write_char ();
  553. if (!show)
  554. return;
  555. if (virtual_screen.cursor_y + virtual_screen.total_scroll
  556. >= virtual_screen.rows)
  557. return;
  558. /* Determine cursor properties and position on text layer. */
  559. x = virtual_screen.cursor_x * virtual_screen.normal_char_width;
  560. width = virtual_screen.normal_char_width;
  561. color = virtual_screen.fg_color;
  562. y = ((virtual_screen.cursor_y + virtual_screen.total_scroll)
  563. * virtual_screen.normal_char_height
  564. + grub_font_get_ascent (virtual_screen.font));
  565. height = 2;
  566. /* Render cursor to text layer. */
  567. grub_video_set_active_render_target (text_layer);
  568. grub_video_fill_rect (color, x, y, width, height);
  569. grub_video_set_active_render_target (render_target);
  570. /* Mark cursor to be redrawn. */
  571. dirty_region_add (virtual_screen.offset_x + x,
  572. virtual_screen.offset_y + y,
  573. width, height);
  574. }
  575. static void
  576. real_scroll (void)
  577. {
  578. unsigned int i, j, was_scroll;
  579. grub_video_color_t color;
  580. if (!virtual_screen.total_scroll)
  581. return;
  582. /* If we have bitmap, re-draw screen, otherwise scroll physical screen too. */
  583. if (grub_gfxterm_background.bitmap)
  584. {
  585. /* Scroll physical screen. */
  586. grub_video_set_active_render_target (text_layer);
  587. color = virtual_screen.bg_color;
  588. grub_video_scroll (color, 0, -virtual_screen.normal_char_height
  589. * virtual_screen.total_scroll);
  590. /* Mark virtual screen to be redrawn. */
  591. dirty_region_add_virtualscreen ();
  592. }
  593. else
  594. {
  595. grub_video_rect_t saved_view;
  596. /* Remove cursor. */
  597. draw_cursor (0);
  598. grub_video_set_active_render_target (render_target);
  599. i = window.double_repaint ? 2 : 1;
  600. color = virtual_screen.bg_color_display;
  601. while (i--)
  602. {
  603. /* Save viewport and set it to our window. */
  604. grub_video_get_viewport ((unsigned *) &saved_view.x,
  605. (unsigned *) &saved_view.y,
  606. (unsigned *) &saved_view.width,
  607. (unsigned *) &saved_view.height);
  608. grub_video_set_viewport (window.x, window.y, window.width,
  609. window.height);
  610. /* Clear new border area. */
  611. grub_video_fill_rect (color,
  612. virtual_screen.offset_x,
  613. virtual_screen.offset_y,
  614. virtual_screen.width,
  615. virtual_screen.normal_char_height
  616. * virtual_screen.total_scroll);
  617. grub_video_set_active_render_target (render_target);
  618. dirty_region_redraw ();
  619. /* Scroll physical screen. */
  620. grub_video_scroll (color, 0, -virtual_screen.normal_char_height
  621. * virtual_screen.total_scroll);
  622. /* Restore saved viewport. */
  623. grub_video_set_viewport (saved_view.x, saved_view.y,
  624. saved_view.width, saved_view.height);
  625. if (i)
  626. grub_video_swap_buffers ();
  627. }
  628. dirty_region_reset ();
  629. /* Scroll physical screen. */
  630. grub_video_set_active_render_target (text_layer);
  631. color = virtual_screen.bg_color;
  632. grub_video_scroll (color, 0, -virtual_screen.normal_char_height
  633. * virtual_screen.total_scroll);
  634. grub_video_set_active_render_target (render_target);
  635. }
  636. was_scroll = virtual_screen.total_scroll;
  637. virtual_screen.total_scroll = 0;
  638. if (was_scroll > virtual_screen.rows)
  639. was_scroll = virtual_screen.rows;
  640. /* Draw shadow part. */
  641. for (i = virtual_screen.rows - was_scroll;
  642. i < virtual_screen.rows; i++)
  643. for (j = 0; j < virtual_screen.columns; j++)
  644. paint_char (j, i);
  645. /* Draw cursor if visible. */
  646. if (virtual_screen.cursor_state)
  647. draw_cursor (1);
  648. }
  649. static void
  650. scroll_up (void)
  651. {
  652. unsigned int i;
  653. /* Clear first line in text buffer. */
  654. for (i = 0; i < virtual_screen.columns; i++)
  655. grub_unicode_destroy_glyph (&virtual_screen.text_buffer[i].code);
  656. /* Scroll text buffer with one line to up. */
  657. grub_memmove (virtual_screen.text_buffer,
  658. virtual_screen.text_buffer + virtual_screen.columns,
  659. sizeof (*virtual_screen.text_buffer)
  660. * virtual_screen.columns
  661. * (virtual_screen.rows - 1));
  662. /* Clear last line in text buffer. */
  663. for (i = virtual_screen.columns * (virtual_screen.rows - 1);
  664. i < virtual_screen.columns * virtual_screen.rows;
  665. i++)
  666. clear_char (&(virtual_screen.text_buffer[i]));
  667. virtual_screen.total_scroll++;
  668. }
  669. static void
  670. grub_gfxterm_putchar (struct grub_term_output *term,
  671. const struct grub_unicode_glyph *c)
  672. {
  673. if (!virtual_screen.functional)
  674. return;
  675. if (c->base == '\a')
  676. /* FIXME */
  677. return;
  678. /* Erase current cursor, if any. */
  679. if (virtual_screen.cursor_state)
  680. draw_cursor (0);
  681. if (c->base == '\b' || c->base == '\n' || c->base == '\r')
  682. {
  683. switch (c->base)
  684. {
  685. case '\b':
  686. if (virtual_screen.cursor_x > 0)
  687. virtual_screen.cursor_x--;
  688. break;
  689. case '\n':
  690. if (virtual_screen.cursor_y >= virtual_screen.rows - 1)
  691. scroll_up ();
  692. else
  693. virtual_screen.cursor_y++;
  694. break;
  695. case '\r':
  696. virtual_screen.cursor_x = 0;
  697. break;
  698. }
  699. }
  700. else
  701. {
  702. struct grub_colored_char *p;
  703. unsigned char char_width;
  704. /* Calculate actual character width for glyph. This is number of
  705. times of normal_font_width. */
  706. char_width = grub_gfxterm_getcharwidth (term, c);
  707. /* If we are about to exceed line length, wrap to next line. */
  708. if (virtual_screen.cursor_x + char_width > virtual_screen.columns)
  709. {
  710. if (virtual_screen.cursor_y >= virtual_screen.rows - 1)
  711. scroll_up ();
  712. else
  713. virtual_screen.cursor_y++;
  714. }
  715. /* Find position on virtual screen, and fill information. */
  716. p = (virtual_screen.text_buffer +
  717. virtual_screen.cursor_x +
  718. virtual_screen.cursor_y * virtual_screen.columns);
  719. grub_unicode_destroy_glyph (&p->code);
  720. grub_unicode_set_glyph (&p->code, c);
  721. grub_errno = GRUB_ERR_NONE;
  722. p->fg_color = virtual_screen.fg_color;
  723. p->bg_color = virtual_screen.bg_color;
  724. /* If we have large glyph, add fixup info. */
  725. if (char_width > 1)
  726. {
  727. unsigned i;
  728. for (i = 1; i < char_width && p + i <
  729. virtual_screen.text_buffer + virtual_screen.columns
  730. * virtual_screen.rows; i++)
  731. {
  732. grub_unicode_destroy_glyph (&p[i].code);
  733. p[i].code.base = 0;
  734. }
  735. }
  736. /* Draw glyph. */
  737. write_char ();
  738. /* Make sure we scroll screen when needed and wrap line correctly. */
  739. virtual_screen.cursor_x += char_width;
  740. if (virtual_screen.cursor_x >= virtual_screen.columns)
  741. {
  742. virtual_screen.cursor_x = 0;
  743. if (virtual_screen.cursor_y >= virtual_screen.rows - 1)
  744. scroll_up ();
  745. else
  746. virtual_screen.cursor_y++;
  747. }
  748. }
  749. /* Redraw cursor if it should be visible. */
  750. /* Note: This will redraw the character as well, which means that the
  751. above call to write_char is redundant when the cursor is showing. */
  752. if (virtual_screen.cursor_state)
  753. draw_cursor (1);
  754. }
  755. /* Use ASCII characters to determine normal character width. */
  756. static unsigned int
  757. calculate_normal_character_width (grub_font_t font)
  758. {
  759. struct grub_font_glyph *glyph;
  760. unsigned int width = 0;
  761. unsigned int i;
  762. /* Get properties of every printable ASCII character. */
  763. for (i = 32; i < 127; i++)
  764. {
  765. glyph = grub_font_get_glyph (font, i);
  766. /* Skip unknown characters. Should never happen on normal conditions. */
  767. if (! glyph)
  768. continue;
  769. if (glyph->device_width > width)
  770. width = glyph->device_width;
  771. }
  772. if (!width)
  773. return 8;
  774. return width;
  775. }
  776. static unsigned char
  777. calculate_character_width (struct grub_font_glyph *glyph)
  778. {
  779. if (! glyph || glyph->device_width == 0)
  780. return 1;
  781. return (glyph->device_width
  782. + (virtual_screen.normal_char_width - 1))
  783. / virtual_screen.normal_char_width;
  784. }
  785. static grub_size_t
  786. grub_gfxterm_getcharwidth (struct grub_term_output *term __attribute__ ((unused)),
  787. const struct grub_unicode_glyph *c)
  788. {
  789. int dev_width;
  790. dev_width = grub_font_get_constructed_device_width (virtual_screen.font, c);
  791. if (dev_width == 0)
  792. return 1;
  793. return (dev_width + (virtual_screen.normal_char_width - 1))
  794. / virtual_screen.normal_char_width;
  795. }
  796. static struct grub_term_coordinate
  797. grub_virtual_screen_getwh (struct grub_term_output *term __attribute__ ((unused)))
  798. {
  799. return (struct grub_term_coordinate) { virtual_screen.columns, virtual_screen.rows };
  800. }
  801. static struct grub_term_coordinate
  802. grub_virtual_screen_getxy (struct grub_term_output *term __attribute__ ((unused)))
  803. {
  804. return (struct grub_term_coordinate) { virtual_screen.cursor_x, virtual_screen.cursor_y };
  805. }
  806. static void
  807. grub_gfxterm_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
  808. struct grub_term_coordinate pos)
  809. {
  810. if (pos.x >= virtual_screen.columns)
  811. pos.x = virtual_screen.columns - 1;
  812. if (pos.y >= virtual_screen.rows)
  813. pos.y = virtual_screen.rows - 1;
  814. /* Erase current cursor, if any. */
  815. if (virtual_screen.cursor_state)
  816. draw_cursor (0);
  817. virtual_screen.cursor_x = pos.x;
  818. virtual_screen.cursor_y = pos.y;
  819. /* Draw cursor if visible. */
  820. if (virtual_screen.cursor_state)
  821. draw_cursor (1);
  822. }
  823. static void
  824. grub_virtual_screen_cls (struct grub_term_output *term __attribute__ ((unused)))
  825. {
  826. grub_uint32_t i;
  827. for (i = 0; i < virtual_screen.columns * virtual_screen.rows; i++)
  828. clear_char (&(virtual_screen.text_buffer[i]));
  829. virtual_screen.cursor_x = virtual_screen.cursor_y = 0;
  830. }
  831. static void
  832. grub_gfxterm_cls (struct grub_term_output *term)
  833. {
  834. grub_video_color_t color;
  835. /* Clear virtual screen. */
  836. grub_virtual_screen_cls (term);
  837. /* Clear text layer. */
  838. grub_video_set_active_render_target (text_layer);
  839. color = virtual_screen.bg_color;
  840. grub_video_fill_rect (color, 0, 0,
  841. virtual_screen.width, virtual_screen.height);
  842. grub_video_set_active_render_target (render_target);
  843. /* Mark virtual screen to be redrawn. */
  844. dirty_region_add_virtualscreen ();
  845. grub_gfxterm_refresh (term);
  846. }
  847. static void
  848. grub_virtual_screen_setcolorstate (struct grub_term_output *term __attribute__ ((unused)),
  849. grub_term_color_state state)
  850. {
  851. switch (state)
  852. {
  853. case GRUB_TERM_COLOR_STANDARD:
  854. virtual_screen.term_color = virtual_screen.standard_color_setting;
  855. break;
  856. case GRUB_TERM_COLOR_NORMAL:
  857. virtual_screen.term_color = grub_term_normal_color;
  858. break;
  859. case GRUB_TERM_COLOR_HIGHLIGHT:
  860. virtual_screen.term_color = grub_term_highlight_color;
  861. break;
  862. default:
  863. break;
  864. }
  865. /* Change color to virtual terminal. */
  866. set_term_color (virtual_screen.term_color);
  867. }
  868. static void
  869. grub_gfxterm_setcursor (struct grub_term_output *term __attribute__ ((unused)),
  870. int on)
  871. {
  872. if (virtual_screen.cursor_state != on)
  873. {
  874. if (virtual_screen.cursor_state)
  875. draw_cursor (0);
  876. else
  877. draw_cursor (1);
  878. virtual_screen.cursor_state = on;
  879. }
  880. }
  881. static void
  882. grub_gfxterm_refresh (struct grub_term_output *term __attribute__ ((unused)))
  883. {
  884. real_scroll ();
  885. /* Redraw only changed regions. */
  886. dirty_region_redraw ();
  887. grub_video_swap_buffers ();
  888. if (window.double_repaint)
  889. dirty_region_redraw ();
  890. dirty_region_reset ();
  891. }
  892. static struct grub_term_output grub_video_term =
  893. {
  894. .name = "gfxterm",
  895. .init = grub_gfxterm_term_init,
  896. .fini = grub_gfxterm_term_fini,
  897. .putchar = grub_gfxterm_putchar,
  898. .getcharwidth = grub_gfxterm_getcharwidth,
  899. .getwh = grub_virtual_screen_getwh,
  900. .getxy = grub_virtual_screen_getxy,
  901. .gotoxy = grub_gfxterm_gotoxy,
  902. .cls = grub_gfxterm_cls,
  903. .setcolorstate = grub_virtual_screen_setcolorstate,
  904. .setcursor = grub_gfxterm_setcursor,
  905. .refresh = grub_gfxterm_refresh,
  906. .fullscreen = grub_gfxterm_fullscreen,
  907. .flags = GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS,
  908. .progress_update_divisor = GRUB_PROGRESS_SLOW,
  909. .next = 0
  910. };
  911. void
  912. grub_gfxterm_video_update_color (void)
  913. {
  914. struct grub_video_render_target *old_target;
  915. grub_video_get_active_render_target (&old_target);
  916. grub_video_set_active_render_target (text_layer);
  917. virtual_screen.bg_color = grub_video_map_rgba_color (grub_gfxterm_background.default_bg_color);
  918. grub_video_set_active_render_target (old_target);
  919. virtual_screen.bg_color_display =
  920. grub_video_map_rgba_color (grub_gfxterm_background.default_bg_color);
  921. }
  922. void
  923. grub_gfxterm_get_dimensions (unsigned *width, unsigned *height)
  924. {
  925. *width = window.width;
  926. *height = window.height;
  927. }
  928. GRUB_MOD_INIT(gfxterm)
  929. {
  930. grub_term_register_output ("gfxterm", &grub_video_term);
  931. }
  932. GRUB_MOD_FINI(gfxterm)
  933. {
  934. grub_term_unregister_output (&grub_video_term);
  935. }