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>
22 #include <grub/misc.h>
23 #include <grub/font.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
48 struct grub_colored_char
50 /* An Unicode codepoint. */
51 struct grub_unicode_glyph code
;
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. */
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. */
76 /* Current cursor location in characters. */
77 unsigned int cursor_x
;
78 unsigned int cursor_y
;
80 /* Current cursor state. */
86 /* Terminal color settings. */
87 grub_uint8_t standard_color_setting
;
88 grub_uint8_t term_color
;
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
97 struct grub_colored_char
*text_buffer
;
104 struct grub_gfxterm_window
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
)));
142 grub_gfxterm_getcharwidth (struct grub_term_output
*term
__attribute__ ((unused
)),
143 const struct grub_unicode_glyph
*c
);
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);
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
);
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
;
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)
190 i
< virtual_screen
.columns
* virtual_screen
.rows
;
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
);
205 grub_virtual_screen_setup (unsigned int x
, unsigned int y
,
206 unsigned int width
, unsigned int height
,
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
)
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
)
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
]));
276 virtual_screen
.functional
= 1;
278 return GRUB_ERR_NONE
;
282 grub_gfxterm_schedule_repaint (void)
284 repaint_scheduled
= 1;
288 grub_gfxterm_set_window (struct grub_video_render_target
*target
,
289 int x
, int y
, int width
, int height
,
291 grub_font_t font
, int border_width
)
293 /* Clean up any prior instance. */
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
,
309 /* Set window bounds. */
312 window
.width
= width
;
313 window
.height
= height
;
314 window
.double_repaint
= double_repaint
;
316 dirty_region_reset ();
317 grub_gfxterm_schedule_repaint ();
323 grub_gfxterm_fullscreen (void)
325 const char *font_name
;
326 struct grub_video_mode_info mode_info
;
327 grub_video_color_t color
;
332 err
= grub_video_get_info (&mode_info
);
333 /* Figure out what mode we ended up. */
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
);
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");
354 font_name
= ""; /* Allow fallback to any font. */
356 font
= grub_font_get (font_name
);
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
,
365 font
, DEFAULT_BORDER_WIDTH
);
369 grub_gfxterm_term_init (struct grub_term_output
*term
__attribute__ ((unused
)))
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);
382 tmp
= grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE
, modevar
);
385 err
= grub_video_set_mode (tmp
, GRUB_VIDEO_MODE_TYPE_PURE_TEXT
, 0);
392 err
= grub_gfxterm_fullscreen ();
394 grub_video_restore ();
400 destroy_window (void)
402 grub_virtual_screen_free ();
406 grub_gfxterm_term_fini (struct grub_term_output
*term
__attribute__ ((unused
)))
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
;
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
,
447 /* If bitmap is smaller than requested blit area, use background
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
;
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
;
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
);
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
,
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
,
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
);
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;
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))
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;
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;
559 dirty_region_add (int x
, int y
, unsigned int width
, unsigned int height
)
561 if ((width
== 0) || (height
== 0))
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
);
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
);
584 dirty_region_redraw (void)
591 if (dirty_region_is_empty ())
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
);
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
;
619 if (cy
+ virtual_screen
.total_scroll
>= virtual_screen
.rows
)
622 /* Find out active character. */
623 p
= (virtual_screen
.text_buffer
624 + cx
+ (cy
* virtual_screen
.columns
));
629 /* Get glyph for character. */
630 glyph
= grub_font_construct_glyph (virtual_screen
.font
, &p
->code
);
633 grub_errno
= GRUB_ERR_NONE
;
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
;
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
,
661 paint_char (virtual_screen
.cursor_x
, virtual_screen
.cursor_y
);
665 draw_cursor (int show
)
671 grub_video_color_t color
;
678 if (virtual_screen
.cursor_y
+ virtual_screen
.total_scroll
679 >= virtual_screen
.rows
)
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
));
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
,
705 unsigned int i
, j
, was_scroll
;
706 grub_video_color_t color
;
708 if (!virtual_screen
.total_scroll
)
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 ();
725 grub_video_rect_t saved_view
;
730 grub_video_set_active_render_target (render_target
);
732 i
= window
.double_repaint
? 2 : 1;
734 color
= virtual_screen
.bg_color_display
;
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
,
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
);
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
++)
793 /* Draw cursor if visible. */
794 if (virtual_screen
.cursor_state
)
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
;
818 clear_char (&(virtual_screen
.text_buffer
[i
]));
820 virtual_screen
.total_scroll
++;
824 grub_gfxterm_putchar (struct grub_term_output
*term
,
825 const struct grub_unicode_glyph
*c
)
827 if (!virtual_screen
.functional
)
834 /* Erase current cursor, if any. */
835 if (virtual_screen
.cursor_state
)
838 if (c
->base
== '\b' || c
->base
== '\n' || c
->base
== '\r')
843 if (virtual_screen
.cursor_x
> 0)
844 virtual_screen
.cursor_x
--;
848 if (virtual_screen
.cursor_y
>= virtual_screen
.rows
- 1)
851 virtual_screen
.cursor_y
++;
855 virtual_screen
.cursor_x
= 0;
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)
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. */
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
);
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)
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
)
924 /* Use ASCII characters to determine normal character width. */
926 calculate_normal_character_width (grub_font_t font
)
928 struct grub_font_glyph
*glyph
;
929 unsigned int width
= 0;
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. */
941 if (glyph
->device_width
> width
)
942 width
= glyph
->device_width
;
951 calculate_character_width (struct grub_font_glyph
*glyph
)
953 if (! glyph
|| glyph
->device_width
== 0)
956 return (glyph
->device_width
957 + (virtual_screen
.normal_char_width
- 1))
958 / virtual_screen
.normal_char_width
;
962 grub_gfxterm_getcharwidth (struct grub_term_output
*term
__attribute__ ((unused
)),
963 const struct grub_unicode_glyph
*c
)
966 dev_width
= grub_font_get_constructed_device_width (virtual_screen
.font
, c
);
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
};
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
)
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
)
1010 grub_virtual_screen_cls (struct grub_term_output
*term
__attribute__ ((unused
)))
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;
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
);
1042 grub_virtual_screen_setcolorstate (struct grub_term_output
*term
__attribute__ ((unused
)),
1043 grub_term_color_state state
)
1047 case GRUB_TERM_COLOR_STANDARD
:
1048 virtual_screen
.term_color
= virtual_screen
.standard_color_setting
;
1051 case GRUB_TERM_COLOR_NORMAL
:
1052 virtual_screen
.term_color
= grub_term_normal_color
;
1055 case GRUB_TERM_COLOR_HIGHLIGHT
:
1056 virtual_screen
.term_color
= grub_term_highlight_color
;
1063 /* Change color to virtual terminal. */
1064 set_term_color (virtual_screen
.term_color
);
1068 grub_gfxterm_setcursor (struct grub_term_output
*term
__attribute__ ((unused
)),
1071 if (virtual_screen
.cursor_state
!= on
)
1073 if (virtual_screen
.cursor_state
)
1078 virtual_screen
.cursor_state
= on
;
1083 grub_gfxterm_refresh (struct grub_term_output
*term
__attribute__ ((unused
)))
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
=
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
,
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
);
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
);