Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / term / gfxterm.c
blob70f10f19c8968783c52e9c9269c91bdd75fc7daa
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <grub/term.h>
20 #include <grub/types.h>
21 #include <grub/dl.h>
22 #include <grub/misc.h>
23 #include <grub/font.h>
24 #include <grub/mm.h>
25 #include <grub/env.h>
26 #include <grub/video.h>
27 #include <grub/gfxterm.h>
28 #include <grub/bitmap.h>
29 #include <grub/command.h>
30 #include <grub/extcmd.h>
31 #include <grub/i18n.h>
33 GRUB_MOD_LICENSE ("GPLv3+");
35 #define DEFAULT_VIDEO_MODE "auto"
36 #define DEFAULT_BORDER_WIDTH 10
38 #define DEFAULT_STANDARD_COLOR 0x07
40 struct grub_dirty_region
42 int top_left_x;
43 int top_left_y;
44 int bottom_right_x;
45 int bottom_right_y;
48 struct grub_colored_char
50 /* An Unicode codepoint. */
51 struct grub_unicode_glyph code;
53 /* Color values. */
54 grub_video_color_t fg_color;
55 grub_video_color_t bg_color;
58 struct grub_virtual_screen
60 /* Dimensions of the virtual screen in pixels. */
61 unsigned int width;
62 unsigned int height;
64 /* Offset in the display in pixels. */
65 unsigned int offset_x;
66 unsigned int offset_y;
68 /* TTY Character sizes in pixes. */
69 unsigned int normal_char_width;
70 unsigned int normal_char_height;
72 /* Virtual screen TTY size in characters. */
73 unsigned int columns;
74 unsigned int rows;
76 /* Current cursor location in characters. */
77 unsigned int cursor_x;
78 unsigned int cursor_y;
80 /* Current cursor state. */
81 int cursor_state;
83 /* Font settings. */
84 grub_font_t font;
86 /* Terminal color settings. */
87 grub_uint8_t standard_color_setting;
88 grub_uint8_t term_color;
90 /* Color settings. */
91 grub_video_color_t fg_color;
92 grub_video_color_t bg_color;
93 grub_video_color_t bg_color_display;
95 /* Text buffer for virtual screen. Contains (columns * rows) number
96 of entries. */
97 struct grub_colored_char *text_buffer;
99 int total_scroll;
101 int functional;
104 struct grub_gfxterm_window
106 unsigned x;
107 unsigned y;
108 unsigned width;
109 unsigned height;
110 int double_repaint;
113 static struct grub_video_render_target *render_target;
114 void (*grub_gfxterm_decorator_hook) (void) = NULL;
115 static struct grub_gfxterm_window window;
116 static struct grub_virtual_screen virtual_screen;
117 static int repaint_scheduled = 0;
118 static int repaint_was_scheduled = 0;
120 static void destroy_window (void);
122 static struct grub_video_render_target *text_layer;
124 struct grub_gfxterm_background grub_gfxterm_background;
126 static struct grub_dirty_region dirty_region;
128 static void dirty_region_reset (void);
130 static int dirty_region_is_empty (void);
132 static void dirty_region_add (int x, int y,
133 unsigned int width, unsigned int height);
135 static unsigned int calculate_normal_character_width (grub_font_t font);
137 static unsigned char calculate_character_width (struct grub_font_glyph *glyph);
139 static void grub_gfxterm_refresh (struct grub_term_output *term __attribute__ ((unused)));
141 static grub_size_t
142 grub_gfxterm_getcharwidth (struct grub_term_output *term __attribute__ ((unused)),
143 const struct grub_unicode_glyph *c);
145 static void
146 set_term_color (grub_uint8_t term_color)
148 struct grub_video_render_target *old_target;
150 /* Save previous target and switch to text layer. */
151 grub_video_get_active_render_target (&old_target);
152 grub_video_set_active_render_target (text_layer);
154 /* Map terminal color to text layer compatible video colors. */
155 virtual_screen.fg_color = grub_video_map_color(term_color & 0x0f);
157 /* Special case: use black as transparent color. */
158 if (((term_color >> 4) & 0x0f) == 0)
160 virtual_screen.bg_color = grub_video_map_rgba(0, 0, 0, 0);
162 else
164 virtual_screen.bg_color = grub_video_map_color((term_color >> 4) & 0x0f);
167 /* Restore previous target. */
168 grub_video_set_active_render_target (old_target);
171 static void
172 clear_char (struct grub_colored_char *c)
174 grub_unicode_destroy_glyph (&c->code);
175 grub_unicode_set_glyph_from_code (&c->code, ' ');
176 c->fg_color = virtual_screen.fg_color;
177 c->bg_color = virtual_screen.bg_color;
180 static void
181 grub_virtual_screen_free (void)
183 virtual_screen.functional = 0;
185 /* If virtual screen has been allocated, free it. */
186 if (virtual_screen.text_buffer != 0)
188 unsigned i;
189 for (i = 0;
190 i < virtual_screen.columns * virtual_screen.rows;
191 i++)
192 grub_unicode_destroy_glyph (&virtual_screen.text_buffer[i].code);
193 grub_free (virtual_screen.text_buffer);
196 /* Reset virtual screen data. */
197 grub_memset (&virtual_screen, 0, sizeof (virtual_screen));
199 /* Free render targets. */
200 grub_video_delete_render_target (text_layer);
201 text_layer = 0;
204 static grub_err_t
205 grub_virtual_screen_setup (unsigned int x, unsigned int y,
206 unsigned int width, unsigned int height,
207 grub_font_t font)
209 unsigned int i;
211 /* Free old virtual screen. */
212 grub_virtual_screen_free ();
214 /* Initialize with default data. */
215 virtual_screen.font = font;
216 virtual_screen.width = width;
217 virtual_screen.height = height;
218 virtual_screen.offset_x = x;
219 virtual_screen.offset_y = y;
220 virtual_screen.normal_char_width =
221 calculate_normal_character_width (virtual_screen.font);
222 virtual_screen.normal_char_height =
223 grub_font_get_max_char_height (virtual_screen.font);
224 if (virtual_screen.normal_char_height == 0)
225 virtual_screen.normal_char_height = 16;
226 virtual_screen.cursor_x = 0;
227 virtual_screen.cursor_y = 0;
228 virtual_screen.cursor_state = 1;
229 virtual_screen.total_scroll = 0;
231 /* Calculate size of text buffer. */
232 virtual_screen.columns = virtual_screen.width / virtual_screen.normal_char_width;
233 virtual_screen.rows = virtual_screen.height / virtual_screen.normal_char_height;
235 /* Allocate memory for text buffer. */
236 virtual_screen.text_buffer =
237 (struct grub_colored_char *) grub_malloc (virtual_screen.columns
238 * virtual_screen.rows
239 * sizeof (*virtual_screen.text_buffer));
240 if (grub_errno != GRUB_ERR_NONE)
241 return grub_errno;
243 /* Create new render target for text layer. */
244 grub_video_create_render_target (&text_layer,
245 virtual_screen.width,
246 virtual_screen.height,
247 GRUB_VIDEO_MODE_TYPE_INDEX_COLOR
248 | GRUB_VIDEO_MODE_TYPE_ALPHA);
249 if (grub_errno != GRUB_ERR_NONE)
250 return grub_errno;
252 /* As we want to have colors compatible with rendering target,
253 we can only have those after mode is initialized. */
254 grub_video_set_active_render_target (text_layer);
256 virtual_screen.standard_color_setting = DEFAULT_STANDARD_COLOR;
258 virtual_screen.term_color = virtual_screen.standard_color_setting;
260 set_term_color (virtual_screen.term_color);
262 grub_video_set_active_render_target (render_target);
264 virtual_screen.bg_color_display =
265 grub_video_map_rgba_color (grub_gfxterm_background.default_bg_color);
267 /* Clear out text buffer. */
268 for (i = 0; i < virtual_screen.columns * virtual_screen.rows; i++)
270 virtual_screen.text_buffer[i].code.ncomb = 0;
271 clear_char (&(virtual_screen.text_buffer[i]));
273 if (grub_errno)
274 return grub_errno;
276 virtual_screen.functional = 1;
278 return GRUB_ERR_NONE;
281 void
282 grub_gfxterm_schedule_repaint (void)
284 repaint_scheduled = 1;
287 grub_err_t
288 grub_gfxterm_set_window (struct grub_video_render_target *target,
289 int x, int y, int width, int height,
290 int double_repaint,
291 grub_font_t font, int border_width)
293 /* Clean up any prior instance. */
294 destroy_window ();
296 /* Set the render target. */
297 render_target = target;
299 /* Create virtual screen. */
300 if (grub_virtual_screen_setup (border_width, border_width,
301 width - 2 * border_width,
302 height - 2 * border_width,
303 font)
304 != GRUB_ERR_NONE)
306 return grub_errno;
309 /* Set window bounds. */
310 window.x = x;
311 window.y = y;
312 window.width = width;
313 window.height = height;
314 window.double_repaint = double_repaint;
316 dirty_region_reset ();
317 grub_gfxterm_schedule_repaint ();
319 return grub_errno;
322 static grub_err_t
323 grub_gfxterm_fullscreen (void)
325 const char *font_name;
326 struct grub_video_mode_info mode_info;
327 grub_video_color_t color;
328 grub_err_t err;
329 int double_redraw;
330 grub_font_t font;
332 err = grub_video_get_info (&mode_info);
333 /* Figure out what mode we ended up. */
334 if (err)
335 return err;
337 grub_video_set_active_render_target (GRUB_VIDEO_RENDER_TARGET_DISPLAY);
339 double_redraw = mode_info.mode_type & GRUB_VIDEO_MODE_TYPE_DOUBLE_BUFFERED
340 && !(mode_info.mode_type & GRUB_VIDEO_MODE_TYPE_UPDATING_SWAP);
342 /* Make sure screen is set to the default background color. */
343 color = grub_video_map_rgba_color (grub_gfxterm_background.default_bg_color);
344 grub_video_fill_rect (color, 0, 0, mode_info.width, mode_info.height);
345 if (double_redraw)
347 grub_video_swap_buffers ();
348 grub_video_fill_rect (color, 0, 0, mode_info.width, mode_info.height);
351 /* Select the font to use. */
352 font_name = grub_env_get ("gfxterm_font");
353 if (! font_name)
354 font_name = ""; /* Allow fallback to any font. */
356 font = grub_font_get (font_name);
357 if (!font)
358 return grub_error (GRUB_ERR_BAD_FONT, "no font loaded");
360 grub_gfxterm_decorator_hook = NULL;
362 return grub_gfxterm_set_window (GRUB_VIDEO_RENDER_TARGET_DISPLAY,
363 0, 0, mode_info.width, mode_info.height,
364 double_redraw,
365 font, DEFAULT_BORDER_WIDTH);
368 static grub_err_t
369 grub_gfxterm_term_init (struct grub_term_output *term __attribute__ ((unused)))
371 char *tmp;
372 grub_err_t err;
373 const char *modevar;
375 /* Parse gfxmode environment variable if set. */
376 modevar = grub_env_get ("gfxmode");
377 if (! modevar || *modevar == 0)
378 err = grub_video_set_mode (DEFAULT_VIDEO_MODE,
379 GRUB_VIDEO_MODE_TYPE_PURE_TEXT, 0);
380 else
382 tmp = grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
383 if (!tmp)
384 return grub_errno;
385 err = grub_video_set_mode (tmp, GRUB_VIDEO_MODE_TYPE_PURE_TEXT, 0);
386 grub_free (tmp);
389 if (err)
390 return err;
392 err = grub_gfxterm_fullscreen ();
393 if (err)
394 grub_video_restore ();
396 return err;
399 static void
400 destroy_window (void)
402 grub_virtual_screen_free ();
405 static grub_err_t
406 grub_gfxterm_term_fini (struct grub_term_output *term __attribute__ ((unused)))
408 unsigned i;
409 destroy_window ();
410 grub_video_restore ();
412 for (i = 0; i < virtual_screen.columns * virtual_screen.rows; i++)
414 grub_unicode_destroy_glyph (&virtual_screen.text_buffer[i].code);
415 virtual_screen.text_buffer[i].code.ncomb = 0;
416 virtual_screen.text_buffer[i].code.base = 0;
419 /* Clear error state. */
420 grub_errno = GRUB_ERR_NONE;
421 return GRUB_ERR_NONE;
424 static void
425 redraw_screen_rect (unsigned int x, unsigned int y,
426 unsigned int width, unsigned int height)
428 grub_video_color_t color;
429 grub_video_rect_t saved_view;
431 grub_video_set_active_render_target (render_target);
432 /* Save viewport and set it to our window. */
433 grub_video_get_viewport ((unsigned *) &saved_view.x,
434 (unsigned *) &saved_view.y,
435 (unsigned *) &saved_view.width,
436 (unsigned *) &saved_view.height);
437 grub_video_set_viewport (window.x, window.y, window.width, window.height);
439 if (grub_gfxterm_background.bitmap)
441 /* Render bitmap as background. */
442 grub_video_blit_bitmap (grub_gfxterm_background.bitmap,
443 GRUB_VIDEO_BLIT_REPLACE, x, y,
444 x, y,
445 width, height);
447 /* If bitmap is smaller than requested blit area, use background
448 color. */
449 color = virtual_screen.bg_color_display;
451 /* Fill right side of the bitmap if needed. */
452 if ((x + width >= grub_gfxterm_background.bitmap->mode_info.width)
453 && (y < grub_gfxterm_background.bitmap->mode_info.height))
455 int w = (x + width) - grub_gfxterm_background.bitmap->mode_info.width;
456 int h = height;
457 unsigned int tx = x;
459 if (y + height >= grub_gfxterm_background.bitmap->mode_info.height)
461 h = grub_gfxterm_background.bitmap->mode_info.height - y;
464 if (grub_gfxterm_background.bitmap->mode_info.width > tx)
466 tx = grub_gfxterm_background.bitmap->mode_info.width;
469 /* Render background layer. */
470 grub_video_fill_rect (color, tx, y, w, h);
473 /* Fill bottom side of the bitmap if needed. */
474 if (y + height >= grub_gfxterm_background.bitmap->mode_info.height)
476 int h = (y + height) - grub_gfxterm_background.bitmap->mode_info.height;
477 unsigned int ty = y;
479 if (grub_gfxterm_background.bitmap->mode_info.height > ty)
481 ty = grub_gfxterm_background.bitmap->mode_info.height;
484 /* Render background layer. */
485 grub_video_fill_rect (color, x, ty, width, h);
488 else
490 /* Render background layer. */
491 color = virtual_screen.bg_color_display;
492 grub_video_fill_rect (color, x, y, width, height);
495 if (grub_gfxterm_background.blend_text_bg)
496 /* Render text layer as blended. */
497 grub_video_blit_render_target (text_layer, GRUB_VIDEO_BLIT_BLEND, x, y,
498 x - virtual_screen.offset_x,
499 y - virtual_screen.offset_y,
500 width, height);
501 else
502 /* Render text layer as replaced (to get texts background color). */
503 grub_video_blit_render_target (text_layer, GRUB_VIDEO_BLIT_REPLACE, x, y,
504 x - virtual_screen.offset_x,
505 y - virtual_screen.offset_y,
506 width, height);
508 /* Restore saved viewport. */
509 grub_video_set_viewport (saved_view.x, saved_view.y,
510 saved_view.width, saved_view.height);
511 grub_video_set_active_render_target (render_target);
514 static void
515 dirty_region_reset (void)
517 dirty_region.top_left_x = -1;
518 dirty_region.top_left_y = -1;
519 dirty_region.bottom_right_x = -1;
520 dirty_region.bottom_right_y = -1;
521 repaint_was_scheduled = 0;
524 static int
525 dirty_region_is_empty (void)
527 if ((dirty_region.top_left_x == -1)
528 || (dirty_region.top_left_y == -1)
529 || (dirty_region.bottom_right_x == -1)
530 || (dirty_region.bottom_right_y == -1))
531 return 1;
532 return 0;
535 static void
536 dirty_region_add_real (int x, int y, unsigned int width, unsigned int height)
538 if (dirty_region_is_empty ())
540 dirty_region.top_left_x = x;
541 dirty_region.top_left_y = y;
542 dirty_region.bottom_right_x = x + width - 1;
543 dirty_region.bottom_right_y = y + height - 1;
545 else
547 if (x < dirty_region.top_left_x)
548 dirty_region.top_left_x = x;
549 if (y < dirty_region.top_left_y)
550 dirty_region.top_left_y = y;
551 if ((x + (int)width - 1) > dirty_region.bottom_right_x)
552 dirty_region.bottom_right_x = x + width - 1;
553 if ((y + (int)height - 1) > dirty_region.bottom_right_y)
554 dirty_region.bottom_right_y = y + height - 1;
558 static void
559 dirty_region_add (int x, int y, unsigned int width, unsigned int height)
561 if ((width == 0) || (height == 0))
562 return;
564 if (repaint_scheduled)
566 dirty_region_add_real (0, 0,
567 window.width, window.height);
568 repaint_scheduled = 0;
569 repaint_was_scheduled = 1;
571 dirty_region_add_real (x, y, width, height);
574 static void
575 dirty_region_add_virtualscreen (void)
577 /* Mark virtual screen as dirty. */
578 dirty_region_add (virtual_screen.offset_x, virtual_screen.offset_y,
579 virtual_screen.width, virtual_screen.height);
583 static void
584 dirty_region_redraw (void)
586 int x;
587 int y;
588 int width;
589 int height;
591 if (dirty_region_is_empty ())
592 return;
594 x = dirty_region.top_left_x;
595 y = dirty_region.top_left_y;
597 width = dirty_region.bottom_right_x - x + 1;
598 height = dirty_region.bottom_right_y - y + 1;
600 if (repaint_was_scheduled && grub_gfxterm_decorator_hook)
601 grub_gfxterm_decorator_hook ();
603 redraw_screen_rect (x, y, width, height);
606 static inline void
607 paint_char (unsigned cx, unsigned cy)
609 struct grub_colored_char *p;
610 struct grub_font_glyph *glyph;
611 grub_video_color_t color;
612 grub_video_color_t bgcolor;
613 unsigned int x;
614 unsigned int y;
615 int ascent;
616 unsigned int height;
617 unsigned int width;
619 if (cy + virtual_screen.total_scroll >= virtual_screen.rows)
620 return;
622 /* Find out active character. */
623 p = (virtual_screen.text_buffer
624 + cx + (cy * virtual_screen.columns));
626 if (!p->code.base)
627 return;
629 /* Get glyph for character. */
630 glyph = grub_font_construct_glyph (virtual_screen.font, &p->code);
631 if (!glyph)
633 grub_errno = GRUB_ERR_NONE;
634 return;
636 ascent = grub_font_get_ascent (virtual_screen.font);
638 width = virtual_screen.normal_char_width * calculate_character_width(glyph);
639 height = virtual_screen.normal_char_height;
641 color = p->fg_color;
642 bgcolor = p->bg_color;
644 x = cx * virtual_screen.normal_char_width;
645 y = (cy + virtual_screen.total_scroll) * virtual_screen.normal_char_height;
647 /* Render glyph to text layer. */
648 grub_video_set_active_render_target (text_layer);
649 grub_video_fill_rect (bgcolor, x, y, width, height);
650 grub_font_draw_glyph (glyph, color, x, y + ascent);
651 grub_video_set_active_render_target (render_target);
653 /* Mark character to be drawn. */
654 dirty_region_add (virtual_screen.offset_x + x, virtual_screen.offset_y + y,
655 width, height);
658 static inline void
659 write_char (void)
661 paint_char (virtual_screen.cursor_x, virtual_screen.cursor_y);
664 static inline void
665 draw_cursor (int show)
667 unsigned int x;
668 unsigned int y;
669 unsigned int width;
670 unsigned int height;
671 grub_video_color_t color;
673 write_char ();
675 if (!show)
676 return;
678 if (virtual_screen.cursor_y + virtual_screen.total_scroll
679 >= virtual_screen.rows)
680 return;
682 /* Determine cursor properties and position on text layer. */
683 x = virtual_screen.cursor_x * virtual_screen.normal_char_width;
684 width = virtual_screen.normal_char_width;
685 color = virtual_screen.fg_color;
686 y = ((virtual_screen.cursor_y + virtual_screen.total_scroll)
687 * virtual_screen.normal_char_height
688 + grub_font_get_ascent (virtual_screen.font));
689 height = 2;
691 /* Render cursor to text layer. */
692 grub_video_set_active_render_target (text_layer);
693 grub_video_fill_rect (color, x, y, width, height);
694 grub_video_set_active_render_target (render_target);
696 /* Mark cursor to be redrawn. */
697 dirty_region_add (virtual_screen.offset_x + x,
698 virtual_screen.offset_y + y,
699 width, height);
702 static void
703 real_scroll (void)
705 unsigned int i, j, was_scroll;
706 grub_video_color_t color;
708 if (!virtual_screen.total_scroll)
709 return;
711 /* If we have bitmap, re-draw screen, otherwise scroll physical screen too. */
712 if (grub_gfxterm_background.bitmap)
714 /* Scroll physical screen. */
715 grub_video_set_active_render_target (text_layer);
716 color = virtual_screen.bg_color;
717 grub_video_scroll (color, 0, -virtual_screen.normal_char_height
718 * virtual_screen.total_scroll);
720 /* Mark virtual screen to be redrawn. */
721 dirty_region_add_virtualscreen ();
723 else
725 grub_video_rect_t saved_view;
727 /* Remove cursor. */
728 draw_cursor (0);
730 grub_video_set_active_render_target (render_target);
732 i = window.double_repaint ? 2 : 1;
734 color = virtual_screen.bg_color_display;
736 while (i--)
738 /* Save viewport and set it to our window. */
739 grub_video_get_viewport ((unsigned *) &saved_view.x,
740 (unsigned *) &saved_view.y,
741 (unsigned *) &saved_view.width,
742 (unsigned *) &saved_view.height);
744 grub_video_set_viewport (window.x, window.y, window.width,
745 window.height);
747 /* Clear new border area. */
748 grub_video_fill_rect (color,
749 virtual_screen.offset_x,
750 virtual_screen.offset_y,
751 virtual_screen.width,
752 virtual_screen.normal_char_height
753 * virtual_screen.total_scroll);
755 grub_video_set_active_render_target (render_target);
756 dirty_region_redraw ();
758 /* Scroll physical screen. */
759 grub_video_scroll (color, 0, -virtual_screen.normal_char_height
760 * virtual_screen.total_scroll);
762 /* Restore saved viewport. */
763 grub_video_set_viewport (saved_view.x, saved_view.y,
764 saved_view.width, saved_view.height);
766 if (i)
767 grub_video_swap_buffers ();
769 dirty_region_reset ();
771 /* Scroll physical screen. */
772 grub_video_set_active_render_target (text_layer);
773 color = virtual_screen.bg_color;
774 grub_video_scroll (color, 0, -virtual_screen.normal_char_height
775 * virtual_screen.total_scroll);
777 grub_video_set_active_render_target (render_target);
781 was_scroll = virtual_screen.total_scroll;
782 virtual_screen.total_scroll = 0;
784 if (was_scroll > virtual_screen.rows)
785 was_scroll = virtual_screen.rows;
787 /* Draw shadow part. */
788 for (i = virtual_screen.rows - was_scroll;
789 i < virtual_screen.rows; i++)
790 for (j = 0; j < virtual_screen.columns; j++)
791 paint_char (j, i);
793 /* Draw cursor if visible. */
794 if (virtual_screen.cursor_state)
795 draw_cursor (1);
798 static void
799 scroll_up (void)
801 unsigned int i;
803 /* Clear first line in text buffer. */
804 for (i = 0; i < virtual_screen.columns; i++)
805 grub_unicode_destroy_glyph (&virtual_screen.text_buffer[i].code);
807 /* Scroll text buffer with one line to up. */
808 grub_memmove (virtual_screen.text_buffer,
809 virtual_screen.text_buffer + virtual_screen.columns,
810 sizeof (*virtual_screen.text_buffer)
811 * virtual_screen.columns
812 * (virtual_screen.rows - 1));
814 /* Clear last line in text buffer. */
815 for (i = virtual_screen.columns * (virtual_screen.rows - 1);
816 i < virtual_screen.columns * virtual_screen.rows;
817 i++)
818 clear_char (&(virtual_screen.text_buffer[i]));
820 virtual_screen.total_scroll++;
823 static void
824 grub_gfxterm_putchar (struct grub_term_output *term,
825 const struct grub_unicode_glyph *c)
827 if (!virtual_screen.functional)
828 return;
830 if (c->base == '\a')
831 /* FIXME */
832 return;
834 /* Erase current cursor, if any. */
835 if (virtual_screen.cursor_state)
836 draw_cursor (0);
838 if (c->base == '\b' || c->base == '\n' || c->base == '\r')
840 switch (c->base)
842 case '\b':
843 if (virtual_screen.cursor_x > 0)
844 virtual_screen.cursor_x--;
845 break;
847 case '\n':
848 if (virtual_screen.cursor_y >= virtual_screen.rows - 1)
849 scroll_up ();
850 else
851 virtual_screen.cursor_y++;
852 break;
854 case '\r':
855 virtual_screen.cursor_x = 0;
856 break;
859 else
861 struct grub_colored_char *p;
862 unsigned char char_width;
864 /* Calculate actual character width for glyph. This is number of
865 times of normal_font_width. */
866 char_width = grub_gfxterm_getcharwidth (term, c);
868 /* If we are about to exceed line length, wrap to next line. */
869 if (virtual_screen.cursor_x + char_width > virtual_screen.columns)
871 if (virtual_screen.cursor_y >= virtual_screen.rows - 1)
872 scroll_up ();
873 else
874 virtual_screen.cursor_y++;
877 /* Find position on virtual screen, and fill information. */
878 p = (virtual_screen.text_buffer +
879 virtual_screen.cursor_x +
880 virtual_screen.cursor_y * virtual_screen.columns);
881 grub_unicode_destroy_glyph (&p->code);
882 grub_unicode_set_glyph (&p->code, c);
883 grub_errno = GRUB_ERR_NONE;
884 p->fg_color = virtual_screen.fg_color;
885 p->bg_color = virtual_screen.bg_color;
887 /* If we have large glyph, add fixup info. */
888 if (char_width > 1)
890 unsigned i;
892 for (i = 1; i < char_width && p + i <
893 virtual_screen.text_buffer + virtual_screen.columns
894 * virtual_screen.rows; i++)
896 grub_unicode_destroy_glyph (&p[i].code);
897 p[i].code.base = 0;
901 /* Draw glyph. */
902 write_char ();
904 /* Make sure we scroll screen when needed and wrap line correctly. */
905 virtual_screen.cursor_x += char_width;
906 if (virtual_screen.cursor_x >= virtual_screen.columns)
908 virtual_screen.cursor_x = 0;
910 if (virtual_screen.cursor_y >= virtual_screen.rows - 1)
911 scroll_up ();
912 else
913 virtual_screen.cursor_y++;
917 /* Redraw cursor if it should be visible. */
918 /* Note: This will redraw the character as well, which means that the
919 above call to write_char is redundant when the cursor is showing. */
920 if (virtual_screen.cursor_state)
921 draw_cursor (1);
924 /* Use ASCII characters to determine normal character width. */
925 static unsigned int
926 calculate_normal_character_width (grub_font_t font)
928 struct grub_font_glyph *glyph;
929 unsigned int width = 0;
930 unsigned int i;
932 /* Get properties of every printable ASCII character. */
933 for (i = 32; i < 127; i++)
935 glyph = grub_font_get_glyph (font, i);
937 /* Skip unknown characters. Should never happen on normal conditions. */
938 if (! glyph)
939 continue;
941 if (glyph->device_width > width)
942 width = glyph->device_width;
944 if (!width)
945 return 8;
947 return width;
950 static unsigned char
951 calculate_character_width (struct grub_font_glyph *glyph)
953 if (! glyph || glyph->device_width == 0)
954 return 1;
956 return (glyph->device_width
957 + (virtual_screen.normal_char_width - 1))
958 / virtual_screen.normal_char_width;
961 static grub_size_t
962 grub_gfxterm_getcharwidth (struct grub_term_output *term __attribute__ ((unused)),
963 const struct grub_unicode_glyph *c)
965 int dev_width;
966 dev_width = grub_font_get_constructed_device_width (virtual_screen.font, c);
968 if (dev_width == 0)
969 return 1;
971 return (dev_width + (virtual_screen.normal_char_width - 1))
972 / virtual_screen.normal_char_width;
975 static struct grub_term_coordinate
976 grub_virtual_screen_getwh (struct grub_term_output *term __attribute__ ((unused)))
978 return (struct grub_term_coordinate) { virtual_screen.columns, virtual_screen.rows };
981 static struct grub_term_coordinate
982 grub_virtual_screen_getxy (struct grub_term_output *term __attribute__ ((unused)))
984 return (struct grub_term_coordinate) { virtual_screen.cursor_x, virtual_screen.cursor_y };
987 static void
988 grub_gfxterm_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
989 struct grub_term_coordinate pos)
991 if (pos.x >= virtual_screen.columns)
992 pos.x = virtual_screen.columns - 1;
994 if (pos.y >= virtual_screen.rows)
995 pos.y = virtual_screen.rows - 1;
997 /* Erase current cursor, if any. */
998 if (virtual_screen.cursor_state)
999 draw_cursor (0);
1001 virtual_screen.cursor_x = pos.x;
1002 virtual_screen.cursor_y = pos.y;
1004 /* Draw cursor if visible. */
1005 if (virtual_screen.cursor_state)
1006 draw_cursor (1);
1009 static void
1010 grub_virtual_screen_cls (struct grub_term_output *term __attribute__ ((unused)))
1012 grub_uint32_t i;
1014 for (i = 0; i < virtual_screen.columns * virtual_screen.rows; i++)
1015 clear_char (&(virtual_screen.text_buffer[i]));
1017 virtual_screen.cursor_x = virtual_screen.cursor_y = 0;
1020 static void
1021 grub_gfxterm_cls (struct grub_term_output *term)
1023 grub_video_color_t color;
1025 /* Clear virtual screen. */
1026 grub_virtual_screen_cls (term);
1028 /* Clear text layer. */
1029 grub_video_set_active_render_target (text_layer);
1030 color = virtual_screen.bg_color;
1031 grub_video_fill_rect (color, 0, 0,
1032 virtual_screen.width, virtual_screen.height);
1033 grub_video_set_active_render_target (render_target);
1035 /* Mark virtual screen to be redrawn. */
1036 dirty_region_add_virtualscreen ();
1038 grub_gfxterm_refresh (term);
1041 static void
1042 grub_virtual_screen_setcolorstate (struct grub_term_output *term __attribute__ ((unused)),
1043 grub_term_color_state state)
1045 switch (state)
1047 case GRUB_TERM_COLOR_STANDARD:
1048 virtual_screen.term_color = virtual_screen.standard_color_setting;
1049 break;
1051 case GRUB_TERM_COLOR_NORMAL:
1052 virtual_screen.term_color = grub_term_normal_color;
1053 break;
1055 case GRUB_TERM_COLOR_HIGHLIGHT:
1056 virtual_screen.term_color = grub_term_highlight_color;
1057 break;
1059 default:
1060 break;
1063 /* Change color to virtual terminal. */
1064 set_term_color (virtual_screen.term_color);
1067 static void
1068 grub_gfxterm_setcursor (struct grub_term_output *term __attribute__ ((unused)),
1069 int on)
1071 if (virtual_screen.cursor_state != on)
1073 if (virtual_screen.cursor_state)
1074 draw_cursor (0);
1075 else
1076 draw_cursor (1);
1078 virtual_screen.cursor_state = on;
1082 static void
1083 grub_gfxterm_refresh (struct grub_term_output *term __attribute__ ((unused)))
1085 real_scroll ();
1087 /* Redraw only changed regions. */
1088 dirty_region_redraw ();
1090 grub_video_swap_buffers ();
1092 if (window.double_repaint)
1093 dirty_region_redraw ();
1094 dirty_region_reset ();
1097 static struct grub_term_output grub_video_term =
1099 .name = "gfxterm",
1100 .init = grub_gfxterm_term_init,
1101 .fini = grub_gfxterm_term_fini,
1102 .putchar = grub_gfxterm_putchar,
1103 .getcharwidth = grub_gfxterm_getcharwidth,
1104 .getwh = grub_virtual_screen_getwh,
1105 .getxy = grub_virtual_screen_getxy,
1106 .gotoxy = grub_gfxterm_gotoxy,
1107 .cls = grub_gfxterm_cls,
1108 .setcolorstate = grub_virtual_screen_setcolorstate,
1109 .setcursor = grub_gfxterm_setcursor,
1110 .refresh = grub_gfxterm_refresh,
1111 .fullscreen = grub_gfxterm_fullscreen,
1112 .flags = GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS,
1113 .progress_update_divisor = GRUB_PROGRESS_SLOW,
1114 .next = 0
1117 void
1118 grub_gfxterm_video_update_color (void)
1120 struct grub_video_render_target *old_target;
1122 grub_video_get_active_render_target (&old_target);
1123 grub_video_set_active_render_target (text_layer);
1124 virtual_screen.bg_color = grub_video_map_rgba_color (grub_gfxterm_background.default_bg_color);
1125 grub_video_set_active_render_target (old_target);
1126 virtual_screen.bg_color_display =
1127 grub_video_map_rgba_color (grub_gfxterm_background.default_bg_color);
1130 void
1131 grub_gfxterm_get_dimensions (unsigned *width, unsigned *height)
1133 *width = window.width;
1134 *height = window.height;
1137 GRUB_MOD_INIT(gfxterm)
1139 grub_term_register_output ("gfxterm", &grub_video_term);
1142 GRUB_MOD_FINI(gfxterm)
1144 grub_term_unregister_output (&grub_video_term);