gfxterm.c 30.8 KB
Newer Older
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 9
 *  (at your option) any later version.
 *
10
 *  GRUB is distributed in the hope that it will be useful,
11 12 13 14 15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  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 19 20 21 22 23 24 25 26
 */

#include <grub/term.h>
#include <grub/types.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/font.h>
#include <grub/mm.h>
#include <grub/env.h>
#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>
32

33 34
GRUB_MOD_LICENSE ("GPLv3+");

Robert Millan's avatar
Robert Millan committed
35
#define DEFAULT_VIDEO_MODE	"auto"
36 37
#define DEFAULT_BORDER_WIDTH	10

38
#define DEFAULT_STANDARD_COLOR  0x07
39 40 41 42 43 44 45 46 47 48 49 50

struct grub_dirty_region
{
  int top_left_x;
  int top_left_y;
  int bottom_right_x;
  int bottom_right_y;
};

struct grub_colored_char
{
  /* An Unicode codepoint.  */
51
  struct grub_unicode_glyph code;
52 53 54 55 56 57 58 59

  /* Color values.  */
  grub_video_color_t fg_color;
  grub_video_color_t bg_color;
};

struct grub_virtual_screen
{
60
  /* Dimensions of the virtual screen in pixels.  */
61 62 63
  unsigned int width;
  unsigned int height;

64
  /* Offset in the display in pixels.  */
65 66 67
  unsigned int offset_x;
  unsigned int offset_y;

68 69 70
  /* TTY Character sizes in pixes.  */
  unsigned int normal_char_width;
  unsigned int normal_char_height;
71

72
  /* Virtual screen TTY size in characters.  */
73 74 75
  unsigned int columns;
  unsigned int rows;

76
  /* Current cursor location in characters.  */
77 78
  unsigned int cursor_x;
  unsigned int cursor_y;
79 80

  /* Current cursor state. */
81
  int cursor_state;
82

83 84 85
  /* Font settings. */
  grub_font_t font;

86 87 88
  /* Terminal color settings.  */
  grub_uint8_t standard_color_setting;
  grub_uint8_t term_color;
89

90 91 92
  /* Color settings.  */
  grub_video_color_t fg_color;
  grub_video_color_t bg_color;
phcoder's avatar
phcoder committed
93
  grub_video_color_t bg_color_display;
94 95 96 97

  /* Text buffer for virtual screen.  Contains (columns * rows) number
     of entries.  */
  struct grub_colored_char *text_buffer;
98 99

  int total_scroll;
100 101

  int functional;
102 103
};

104 105 106 107 108 109 110
struct grub_gfxterm_window
{
  unsigned x;
  unsigned y;
  unsigned width;
  unsigned height;
  int double_repaint;
111 112
};

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 118
static int repaint_scheduled = 0;
static int repaint_was_scheduled = 0;
119

120
static void destroy_window (void);
121 122 123

static struct grub_video_render_target *text_layer;

124
struct grub_gfxterm_background grub_gfxterm_background;
125

126 127 128 129 130 131
static struct grub_dirty_region dirty_region;

static void dirty_region_reset (void);

static int dirty_region_is_empty (void);

132
static void dirty_region_add (int x, int y,
133 134
                              unsigned int width, unsigned int height);

135 136 137 138
static unsigned int calculate_normal_character_width (grub_font_t font);

static unsigned char calculate_character_width (struct grub_font_glyph *glyph);

139
static void grub_gfxterm_refresh (struct grub_term_output *term __attribute__ ((unused)));
140

141
static grub_size_t
142 143
grub_gfxterm_getcharwidth (struct grub_term_output *term __attribute__ ((unused)),
			   const struct grub_unicode_glyph *c);
144

145 146 147 148 149 150 151 152 153 154 155
static void
set_term_color (grub_uint8_t term_color)
{
  struct grub_video_render_target *old_target;

  /* Save previous target and switch to text layer.  */
  grub_video_get_active_render_target (&old_target);
  grub_video_set_active_render_target (text_layer);

  /* Map terminal color to text layer compatible video colors.  */
  virtual_screen.fg_color = grub_video_map_color(term_color & 0x0f);
156

157 158 159 160
  /* Special case: use black as transparent color.  */
  if (((term_color >> 4) & 0x0f) == 0)
    {
      virtual_screen.bg_color = grub_video_map_rgba(0, 0, 0, 0);
161
    }
162 163 164 165 166 167 168 169 170
  else
    {
      virtual_screen.bg_color = grub_video_map_color((term_color >> 4) & 0x0f);
    }

  /* Restore previous target.  */
  grub_video_set_active_render_target (old_target);
}

171 172 173
static void
clear_char (struct grub_colored_char *c)
{
174
  grub_unicode_destroy_glyph (&c->code);
175
  grub_unicode_set_glyph_from_code (&c->code, ' ');
176 177 178 179
  c->fg_color = virtual_screen.fg_color;
  c->bg_color = virtual_screen.bg_color;
}

180 181 182
static void
grub_virtual_screen_free (void)
{
183 184
  virtual_screen.functional = 0;

185 186
  /* If virtual screen has been allocated, free it.  */
  if (virtual_screen.text_buffer != 0)
187 188 189 190 191
    {
      unsigned i;
      for (i = 0;
	   i < virtual_screen.columns * virtual_screen.rows;
	   i++)
192
	grub_unicode_destroy_glyph (&virtual_screen.text_buffer[i].code);
193 194
      grub_free (virtual_screen.text_buffer);
    }
195 196 197

  /* Reset virtual screen data.  */
  grub_memset (&virtual_screen, 0, sizeof (virtual_screen));
198

199 200 201 202 203 204 205
  /* Free render targets.  */
  grub_video_delete_render_target (text_layer);
  text_layer = 0;
}

static grub_err_t
grub_virtual_screen_setup (unsigned int x, unsigned int y,
206
                           unsigned int width, unsigned int height,
207
			   grub_font_t font)
208
{
209 210
  unsigned int i;

211 212 213 214
  /* Free old virtual screen.  */
  grub_virtual_screen_free ();

  /* Initialize with default data.  */
215
  virtual_screen.font = font;
216 217 218 219
  virtual_screen.width = width;
  virtual_screen.height = height;
  virtual_screen.offset_x = x;
  virtual_screen.offset_y = y;
220 221 222 223
  virtual_screen.normal_char_width =
    calculate_normal_character_width (virtual_screen.font);
  virtual_screen.normal_char_height =
    grub_font_get_max_char_height (virtual_screen.font);
224 225
  if (virtual_screen.normal_char_height == 0)
    virtual_screen.normal_char_height = 16;
226 227 228
  virtual_screen.cursor_x = 0;
  virtual_screen.cursor_y = 0;
  virtual_screen.cursor_state = 1;
229
  virtual_screen.total_scroll = 0;
230 231

  /* Calculate size of text buffer.  */
232 233
  virtual_screen.columns = virtual_screen.width / virtual_screen.normal_char_width;
  virtual_screen.rows = virtual_screen.height / virtual_screen.normal_char_height;
234 235 236 237

  /* Allocate memory for text buffer.  */
  virtual_screen.text_buffer =
    (struct grub_colored_char *) grub_malloc (virtual_screen.columns
238 239
                                              * virtual_screen.rows
                                              * sizeof (*virtual_screen.text_buffer));
240 241 242 243 244 245 246
  if (grub_errno != GRUB_ERR_NONE)
    return grub_errno;

  /* Create new render target for text layer.  */
  grub_video_create_render_target (&text_layer,
                                   virtual_screen.width,
                                   virtual_screen.height,
247
                                   GRUB_VIDEO_MODE_TYPE_INDEX_COLOR
248 249 250
                                   | GRUB_VIDEO_MODE_TYPE_ALPHA);
  if (grub_errno != GRUB_ERR_NONE)
    return grub_errno;
251

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);
255

256
  virtual_screen.standard_color_setting = DEFAULT_STANDARD_COLOR;
257

258
  virtual_screen.term_color = virtual_screen.standard_color_setting;
259

260 261
  set_term_color (virtual_screen.term_color);

262
  grub_video_set_active_render_target (render_target);
263

264
  virtual_screen.bg_color_display =
265
    grub_video_map_rgba_color (grub_gfxterm_background.default_bg_color);
266

267
  /* Clear out text buffer. */
268
  for (i = 0; i < virtual_screen.columns * virtual_screen.rows; i++)
269
    {
270
      virtual_screen.text_buffer[i].code.ncomb = 0;
271 272
      clear_char (&(virtual_screen.text_buffer[i]));
    }
273 274
  if (grub_errno)
    return grub_errno;
275

276 277 278
  virtual_screen.functional = 1;

  return GRUB_ERR_NONE;
279 280
}

281 282
void
grub_gfxterm_schedule_repaint (void)
283
{
284
  repaint_scheduled = 1;
285 286
}

287 288 289 290
grub_err_t
grub_gfxterm_set_window (struct grub_video_render_target *target,
			 int x, int y, int width, int height,
			 int double_repaint,
291
			 grub_font_t font, int border_width)
292
{
293 294
  /* Clean up any prior instance.  */
  destroy_window ();
295

296 297
  /* Set the render target.  */
  render_target = target;
298

299 300 301 302
  /* Create virtual screen.  */
  if (grub_virtual_screen_setup (border_width, border_width, 
                                 width - 2 * border_width, 
                                 height - 2 * border_width, 
303
                                 font) 
304
      != GRUB_ERR_NONE)
305
    {
306
      return grub_errno;
307
    }
308

309 310 311 312 313 314 315 316
  /* Set window bounds.  */
  window.x = x;
  window.y = y;
  window.width = width;
  window.height = height;
  window.double_repaint = double_repaint;

  dirty_region_reset ();
317
  grub_gfxterm_schedule_repaint ();
318 319 320 321

  return grub_errno;
}

322
static grub_err_t
323
grub_gfxterm_fullscreen (void)
324
{
325 326
  const char *font_name;
  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;
331 332 333 334 335 336

  err = grub_video_get_info (&mode_info);
  /* Figure out what mode we ended up.  */
  if (err)
    return err;

337 338
  grub_video_set_active_render_target (GRUB_VIDEO_RENDER_TARGET_DISPLAY);

339 340 341
  double_redraw = mode_info.mode_type & GRUB_VIDEO_MODE_TYPE_DOUBLE_BUFFERED
    && !(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 346 347 348 349
  if (double_redraw)
    {
      grub_video_swap_buffers ();
      grub_video_fill_rect (color, 0, 0, mode_info.width, mode_info.height);
    }
350

351
  /* Select the font to use.  */
352 353
  font_name = grub_env_get ("gfxterm_font");
  if (! font_name)
354
    font_name = "";   /* Allow fallback to any font.  */
355

356 357 358 359
  font = grub_font_get (font_name);
  if (!font)
    return grub_error (GRUB_ERR_BAD_FONT, "no font loaded");

360 361 362 363
  grub_gfxterm_decorator_hook = NULL;

  return grub_gfxterm_set_window (GRUB_VIDEO_RENDER_TARGET_DISPLAY,
				  0, 0, mode_info.width, mode_info.height,
364
				  double_redraw,
365
				  font, DEFAULT_BORDER_WIDTH);
366 367 368
}

static grub_err_t
369
grub_gfxterm_term_init (struct grub_term_output *term __attribute__ ((unused)))
370 371 372 373 374
{
  char *tmp;
  grub_err_t err;
  const char *modevar;

375 376
  /* Parse gfxmode environment variable if set.  */
  modevar = grub_env_get ("gfxmode");
377
  if (! modevar || *modevar == 0)
378 379
    err = grub_video_set_mode (DEFAULT_VIDEO_MODE,
			       GRUB_VIDEO_MODE_TYPE_PURE_TEXT, 0);
380
  else
381
    {
382
      tmp = grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
383 384
      if (!tmp)
	return grub_errno;
385
      err = grub_video_set_mode (tmp, GRUB_VIDEO_MODE_TYPE_PURE_TEXT, 0);
386
      grub_free (tmp);
387 388
    }

389 390 391
  if (err)
    return err;

392
  err = grub_gfxterm_fullscreen ();
393
  if (err)
394
    grub_video_restore ();
395

396
  return err;
397 398
}

399 400
static void
destroy_window (void)
401 402
{
  grub_virtual_screen_free ();
403
}
404

405
static grub_err_t
406
grub_gfxterm_term_fini (struct grub_term_output *term __attribute__ ((unused)))
407
{
408
  unsigned i;
409
  destroy_window ();
410 411
  grub_video_restore ();

412 413
  for (i = 0; i < virtual_screen.columns * virtual_screen.rows; i++)
    {
414 415
      grub_unicode_destroy_glyph (&virtual_screen.text_buffer[i].code);
      virtual_screen.text_buffer[i].code.ncomb = 0;
416
      virtual_screen.text_buffer[i].code.base = 0;
417 418
    }

419
  /* Clear error state.  */
420
  grub_errno = GRUB_ERR_NONE;
421 422 423 424
  return GRUB_ERR_NONE;
}

static void
425
redraw_screen_rect (unsigned int x, unsigned int y,
426 427 428
                    unsigned int width, unsigned int height)
{
  grub_video_color_t color;
429
  grub_video_rect_t saved_view;
430

431 432 433 434 435 436 437
  grub_video_set_active_render_target (render_target);
  /* Save viewport and set it to our window.  */
  grub_video_get_viewport ((unsigned *) &saved_view.x, 
                           (unsigned *) &saved_view.y, 
                           (unsigned *) &saved_view.width, 
                           (unsigned *) &saved_view.height);
  grub_video_set_viewport (window.x, window.y, window.width, window.height);
438

439
  if (grub_gfxterm_background.bitmap)
440 441
    {
      /* Render bitmap as background.  */
442 443
      grub_video_blit_bitmap (grub_gfxterm_background.bitmap,
			      GRUB_VIDEO_BLIT_REPLACE, x, y,
444
			      x, y,
445
                              width, height);
446 447

      /* If bitmap is smaller than requested blit area, use background
448
         color.  */
phcoder's avatar
phcoder committed
449
      color = virtual_screen.bg_color_display;
450 451

      /* Fill right side of the bitmap if needed.  */
452 453
      if ((x + width >= grub_gfxterm_background.bitmap->mode_info.width)
	  && (y < grub_gfxterm_background.bitmap->mode_info.height))
454
        {
455
          int w = (x + width) - grub_gfxterm_background.bitmap->mode_info.width;
456 457 458
          int h = height;
          unsigned int tx = x;

459
          if (y + height >= grub_gfxterm_background.bitmap->mode_info.height)
460
            {
461
              h = grub_gfxterm_background.bitmap->mode_info.height - y;
462
            }
463

464
          if (grub_gfxterm_background.bitmap->mode_info.width > tx)
465
            {
466
              tx = grub_gfxterm_background.bitmap->mode_info.width;
467
            }
468

469
          /* Render background layer.  */
470
	  grub_video_fill_rect (color, tx, y, w, h);
471
        }
472

473
      /* Fill bottom side of the bitmap if needed.  */
474
      if (y + height >= grub_gfxterm_background.bitmap->mode_info.height)
475
        {
476
          int h = (y + height) - grub_gfxterm_background.bitmap->mode_info.height;
477
          unsigned int ty = y;
478

479
          if (grub_gfxterm_background.bitmap->mode_info.height > ty)
480
            {
481
              ty = grub_gfxterm_background.bitmap->mode_info.height;
482
            }
483

484
          /* Render background layer.  */
485
	  grub_video_fill_rect (color, x, ty, width, h);
486 487 488 489 490
        }
    }
  else
    {
      /* Render background layer.  */
phcoder's avatar
phcoder committed
491
      color = virtual_screen.bg_color_display;
492 493
      grub_video_fill_rect (color, x, y, width, height);
    }
494

495
  if (grub_gfxterm_background.blend_text_bg)
496 497 498 499 500 501 502 503 504 505 506 507
    /* Render text layer as blended.  */
    grub_video_blit_render_target (text_layer, GRUB_VIDEO_BLIT_BLEND, x, y,
                                   x - virtual_screen.offset_x,
                                   y - virtual_screen.offset_y,
                                   width, height);
  else
    /* Render text layer as replaced (to get texts background color).  */
    grub_video_blit_render_target (text_layer, GRUB_VIDEO_BLIT_REPLACE, x, y,
                                   x - virtual_screen.offset_x,
                                   y - virtual_screen.offset_y,
                                   width, height);

508 509 510 511
  /* Restore saved viewport.  */
  grub_video_set_viewport (saved_view.x, saved_view.y,
                           saved_view.width, saved_view.height);
  grub_video_set_active_render_target (render_target);
512 513 514 515 516 517 518 519 520
}

static void
dirty_region_reset (void)
{
  dirty_region.top_left_x = -1;
  dirty_region.top_left_y = -1;
  dirty_region.bottom_right_x = -1;
  dirty_region.bottom_right_y = -1;
521
  repaint_was_scheduled = 0;
522 523 524 525 526 527 528 529 530 531 532 533 534 535
}

static int
dirty_region_is_empty (void)
{
  if ((dirty_region.top_left_x == -1)
      || (dirty_region.top_left_y == -1)
      || (dirty_region.bottom_right_x == -1)
      || (dirty_region.bottom_right_y == -1))
    return 1;
  return 0;
}

static void
536
dirty_region_add_real (int x, int y, unsigned int width, unsigned int height)
537 538 539 540 541 542 543
{
  if (dirty_region_is_empty ())
    {
      dirty_region.top_left_x = x;
      dirty_region.top_left_y = y;
      dirty_region.bottom_right_x = x + width - 1;
      dirty_region.bottom_right_y = y + height - 1;
544
    }
545 546 547 548 549 550 551 552 553 554 555
  else
    {
      if (x < dirty_region.top_left_x)
        dirty_region.top_left_x = x;
      if (y < dirty_region.top_left_y)
        dirty_region.top_left_y = y;
      if ((x + (int)width - 1) > dirty_region.bottom_right_x)
        dirty_region.bottom_right_x = x + width - 1;
      if ((y + (int)height - 1) > dirty_region.bottom_right_y)
        dirty_region.bottom_right_y = y + height - 1;
    }
556
}
557

558 559 560 561 562 563 564 565
static void
dirty_region_add (int x, int y, unsigned int width, unsigned int height)
{
  if ((width == 0) || (height == 0))
    return;

  if (repaint_scheduled)
    {
566 567
      dirty_region_add_real (0, 0,
			     window.width, window.height);
568 569 570 571 572 573
      repaint_scheduled = 0;
      repaint_was_scheduled = 1;
    }
  dirty_region_add_real (x, y, width, height);
}

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
static void
dirty_region_add_virtualscreen (void)
{
  /* Mark virtual screen as dirty.  */
  dirty_region_add (virtual_screen.offset_x, virtual_screen.offset_y,
                    virtual_screen.width, virtual_screen.height);
}


static void
dirty_region_redraw (void)
{
  int x;
  int y;
  int width;
  int height;
590

591 592
  if (dirty_region_is_empty ())
    return;
593

594 595
  x = dirty_region.top_left_x;
  y = dirty_region.top_left_y;
596

597 598
  width = dirty_region.bottom_right_x - x + 1;
  height = dirty_region.bottom_right_y - y + 1;
599

600
  if (repaint_was_scheduled && grub_gfxterm_decorator_hook)
601
    grub_gfxterm_decorator_hook ();
602

603 604 605
  redraw_screen_rect (x, y, width, height);
}

606 607
static inline void
paint_char (unsigned cx, unsigned cy)
608 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 614
  unsigned int x;
  unsigned int y;
615
  int ascent;
616 617
  unsigned int height;
  unsigned int width;
618

619 620 621
  if (cy + virtual_screen.total_scroll >= virtual_screen.rows)
    return;

622 623
  /* Find out active character.  */
  p = (virtual_screen.text_buffer
624
       + cx + (cy * virtual_screen.columns));
625

626
  if (!p->code.base)
627
    return;
628 629

  /* Get glyph for character.  */
630
  glyph = grub_font_construct_glyph (virtual_screen.font, &p->code);
631 632 633 634 635
  if (!glyph)
    {
      grub_errno = GRUB_ERR_NONE;
      return;
    }
636
  ascent = grub_font_get_ascent (virtual_screen.font);
637

638 639
  width = virtual_screen.normal_char_width * calculate_character_width(glyph);
  height = virtual_screen.normal_char_height;
640

641 642 643
  color = p->fg_color;
  bgcolor = p->bg_color;

644 645
  x = cx * virtual_screen.normal_char_width;
  y = (cy + virtual_screen.total_scroll) * virtual_screen.normal_char_height;
646 647 648

  /* Render glyph to text layer.  */
  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);
652 653 654

  /* Mark character to be drawn.  */
  dirty_region_add (virtual_screen.offset_x + x, virtual_screen.offset_y + y,
655
                    width, height);
656 657
}

658 659 660 661 662 663 664
static inline void
write_char (void)
{
  paint_char (virtual_screen.cursor_x, virtual_screen.cursor_y);
}

static inline void
665
draw_cursor (int show)
666
{
667 668 669 670 671 672
  unsigned int x;
  unsigned int y;
  unsigned int width;
  unsigned int height;
  grub_video_color_t color;
  
673
  write_char ();
674

675 676
  if (!show)
    return;
677

678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
  if (virtual_screen.cursor_y + virtual_screen.total_scroll
      >= virtual_screen.rows)
    return;

  /* Determine cursor properties and position on text layer. */
  x = virtual_screen.cursor_x * virtual_screen.normal_char_width;
  width = virtual_screen.normal_char_width;
  color = virtual_screen.fg_color;
  y = ((virtual_screen.cursor_y + virtual_screen.total_scroll)
       * virtual_screen.normal_char_height
       + grub_font_get_ascent (virtual_screen.font));
  height = 2;
  
  /* Render cursor to text layer.  */
  grub_video_set_active_render_target (text_layer);
  grub_video_fill_rect (color, x, y, width, height);
  grub_video_set_active_render_target (render_target);
  
  /* Mark cursor to be redrawn.  */
  dirty_region_add (virtual_screen.offset_x + x,
		    virtual_screen.offset_y + y,
		    width, height);
700 701 702
}

static void
703
real_scroll (void)
704
{
705
  unsigned int i, j, was_scroll;
706 707
  grub_video_color_t color;

708 709
  if (!virtual_screen.total_scroll)
    return;
710

711
  /* If we have bitmap, re-draw screen, otherwise scroll physical screen too.  */
712
  if (grub_gfxterm_background.bitmap)
713
    {
714 715 716
      /* Scroll physical screen.  */
      grub_video_set_active_render_target (text_layer);
      color = virtual_screen.bg_color;
717 718
      grub_video_scroll (color, 0, -virtual_screen.normal_char_height
			 * virtual_screen.total_scroll);
719

720 721 722 723
      /* Mark virtual screen to be redrawn.  */
      dirty_region_add_virtualscreen ();
    }
  else
724
    {
725 726
      grub_video_rect_t saved_view;

727
      /* Remove cursor.  */
728
      draw_cursor (0);
729

730 731 732
      grub_video_set_active_render_target (render_target);

      i = window.double_repaint ? 2 : 1;
733

734
      color = virtual_screen.bg_color_display;
735 736 737

      while (i--)
	{
738 739 740 741 742 743 744 745 746
	  /* Save viewport and set it to our window.  */
	  grub_video_get_viewport ((unsigned *) &saved_view.x, 
				   (unsigned *) &saved_view.y, 
				   (unsigned *) &saved_view.width, 
				   (unsigned *) &saved_view.height);

	  grub_video_set_viewport (window.x, window.y, window.width,
				   window.height);

747 748 749 750 751
	  /* Clear new border area.  */
	  grub_video_fill_rect (color,
				virtual_screen.offset_x,
				virtual_screen.offset_y,
				virtual_screen.width,
752 753
				virtual_screen.normal_char_height
				* virtual_screen.total_scroll);
754

755
	  grub_video_set_active_render_target (render_target);
756 757 758
	  dirty_region_redraw ();

	  /* Scroll physical screen.  */
759 760
	  grub_video_scroll (color, 0, -virtual_screen.normal_char_height
			     * virtual_screen.total_scroll);
761

762 763 764 765
	  /* Restore saved viewport.  */
	  grub_video_set_viewport (saved_view.x, saved_view.y,
				   saved_view.width, saved_view.height);

766 767 768 769
	  if (i)
	    grub_video_swap_buffers ();
	}
      dirty_region_reset ();
770

771
      /* Scroll physical screen.  */
772 773
      grub_video_set_active_render_target (text_layer);
      color = virtual_screen.bg_color;
774 775
      grub_video_scroll (color, 0, -virtual_screen.normal_char_height
			 * virtual_screen.total_scroll);
776

777
      grub_video_set_active_render_target (render_target);
778

779
    }
780

781 782 783
  was_scroll = virtual_screen.total_scroll;
  virtual_screen.total_scroll = 0;

784 785 786
  if (was_scroll > virtual_screen.rows)
    was_scroll = virtual_screen.rows;

787 788 789 790 791 792
  /* Draw shadow part.  */
  for (i = virtual_screen.rows - was_scroll;
       i < virtual_screen.rows; i++)
    for (j = 0; j < virtual_screen.columns; j++)
      paint_char (j, i);

793 794 795
  /* Draw cursor if visible.  */
  if (virtual_screen.cursor_state)
    draw_cursor (1);
796 797
}

798 799 800 801 802
static void
scroll_up (void)
{
  unsigned int i;

803
  /* Clear first line in text buffer.  */
804
  for (i = 0; i < virtual_screen.columns; i++)
805 806
    grub_unicode_destroy_glyph (&virtual_screen.text_buffer[i].code);
  
807 808
  /* Scroll text buffer with one line to up.  */
  grub_memmove (virtual_screen.text_buffer,
809
                virtual_screen.text_buffer + virtual_screen.columns,
810 811 812 813 814 815 816 817
                sizeof (*virtual_screen.text_buffer)
                * virtual_screen.columns
                * (virtual_screen.rows - 1));

  /* Clear last line in text buffer.  */
  for (i = virtual_screen.columns * (virtual_screen.rows - 1);
       i < virtual_screen.columns * virtual_screen.rows;
       i++)
818
    clear_char (&(virtual_screen.text_buffer[i]));
819

820
  virtual_screen.total_scroll++;
821 822 823
}

static void
824 825
grub_gfxterm_putchar (struct grub_term_output *term,
		      const struct grub_unicode_glyph *c)
826
{
827 828 829
  if (!virtual_screen.functional)
    return;

830
  if (c->base == '\a')
831 832 833
    /* FIXME */
    return;

834 835 836 837
  /* Erase current cursor, if any.  */
  if (virtual_screen.cursor_state)
    draw_cursor (0);

838
  if (c->base == '\b' || c->base == '\n' || c->base == '\r')
839
    {
840
      switch (c->base)
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
        {
        case '\b':
          if (virtual_screen.cursor_x > 0)
            virtual_screen.cursor_x--;
          break;

        case '\n':
          if (virtual_screen.cursor_y >= virtual_screen.rows - 1)
            scroll_up ();
          else
            virtual_screen.cursor_y++;
          break;

        case '\r':
          virtual_screen.cursor_x = 0;
          break;
        }
858 859 860 861
    }
  else
    {
      struct grub_colored_char *p;
862 863 864 865
      unsigned char char_width;

      /* Calculate actual character width for glyph. This is number of
         times of normal_font_width.  */
866
      char_width = grub_gfxterm_getcharwidth (term, c);
867 868

      /* If we are about to exceed line length, wrap to next line.  */
869
      if (virtual_screen.cursor_x + char_width > virtual_screen.columns)
870 871 872 873 874 875
	{
          if (virtual_screen.cursor_y >= virtual_screen.rows - 1)
            scroll_up ();
          else
            virtual_screen.cursor_y++;
	}
876 877 878

      /* Find position on virtual screen, and fill information.  */
      p = (virtual_screen.text_buffer +
879 880
           virtual_screen.cursor_x +
           virtual_screen.cursor_y * virtual_screen.columns);
881
      grub_unicode_destroy_glyph (&p->code);
882 883
      grub_unicode_set_glyph (&p->code, c);
      grub_errno = GRUB_ERR_NONE;
884 885 886 887
      p->fg_color = virtual_screen.fg_color;
      p->bg_color = virtual_screen.bg_color;

      /* If we have large glyph, add fixup info.  */
888
      if (char_width > 1)
889 890 891
        {
          unsigned i;

892 893 894
          for (i = 1; i < char_width && p + i < 
		 virtual_screen.text_buffer + virtual_screen.columns
		 * virtual_screen.rows; i++)
895
	      {
896
		grub_unicode_destroy_glyph (&p[i].code);
897
		p[i].code.base = 0;
898
	      }
899 900
        }

901 902
      /* Draw glyph.  */
      write_char ();
903

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)
907 908 909 910 911 912 913 914
        {
          virtual_screen.cursor_x = 0;

          if (virtual_screen.cursor_y >= virtual_screen.rows - 1)
            scroll_up ();
          else
            virtual_screen.cursor_y++;
        }
915
    }
916 917 918 919 920 921

  /* Redraw cursor if it should be visible.  */
  /* Note: This will redraw the character as well, which means that the
     above call to write_char is redundant when the cursor is showing.  */
  if (virtual_screen.cursor_state)
    draw_cursor (1);
922 923
}

924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
/* Use ASCII characters to determine normal character width.  */
static unsigned int
calculate_normal_character_width (grub_font_t font)
{
  struct grub_font_glyph *glyph;
  unsigned int width = 0;
  unsigned int i;

  /* Get properties of every printable ASCII character.  */
  for (i = 32; i < 127; i++)
    {
      glyph = grub_font_get_glyph (font, i);

      /* Skip unknown characters.  Should never happen on normal conditions.  */
      if (! glyph)
	continue;

      if (glyph->device_width > width)
	width = glyph->device_width;
    }
944 945
  if (!width)
    return 8;
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960

  return width;
}

static unsigned char
calculate_character_width (struct grub_font_glyph *glyph)
{
  if (! glyph || glyph->device_width == 0)
    return 1;

  return (glyph->device_width
          + (virtual_screen.normal_char_width - 1))
         / virtual_screen.normal_char_width;
}

961
static grub_size_t
962 963
grub_gfxterm_getcharwidth (struct grub_term_output *term __attribute__ ((unused)),
			   const struct grub_unicode_glyph *c)
964
{
965 966
  int dev_width;
  dev_width = grub_font_get_constructed_device_width (virtual_screen.font, c);
967

968 969
  if (dev_width == 0)
    return 1;
970

971 972
  return (dev_width + (virtual_screen.normal_char_width - 1))
    / virtual_screen.normal_char_width;
973 974
}

975
static struct grub_term_coordinate
976
grub_virtual_screen_getwh (struct grub_term_output *term __attribute__ ((unused)))
977
{
978
  return (struct grub_term_coordinate) { virtual_screen.columns, virtual_screen.rows };
979 980
}

981
static struct grub_term_coordinate
982
grub_virtual_screen_getxy (struct grub_term_output *term __attribute__ ((unused)))
983
{
984
  return (struct grub_term_coordinate) { virtual_screen.cursor_x, virtual_screen.cursor_y };
985 986 987
}

static void
988
grub_gfxterm_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
989
		     struct grub_term_coordinate pos)
990
{
991 992
  if (pos.x >= virtual_screen.columns)
    pos.x = virtual_screen.columns - 1;
993

994 995
  if (pos.y >= virtual_screen.rows)
    pos.y = virtual_screen.rows - 1;
996

997
  /* Erase current cursor, if any.  */
998
  if (virtual_screen.cursor_state)
999
    draw_cursor (0);
1000

1001 1002
  virtual_screen.cursor_x = pos.x;
  virtual_screen.cursor_y = pos.y;
1003

1004
  /* Draw cursor if visible.  */
1005
  if (virtual_screen.cursor_state)
1006
    draw_cursor (1);
1007 1008 1009
}

static void
1010
grub_virtual_screen_cls (struct grub_term_output *term __attribute__ ((unused)))
1011 1012 1013 1014
{
  grub_uint32_t i;

  for (i = 0; i < virtual_screen.columns * virtual_screen.rows; i++)
1015
    clear_char (&(virtual_screen.text_buffer[i]));
1016 1017 1018 1019 1020

  virtual_screen.cursor_x = virtual_screen.cursor_y = 0;
}

static void
1021
grub_gfxterm_cls (struct grub_term_output *term)
1022 1023 1024 1025
{
  grub_video_color_t color;

  /* Clear virtual screen.  */
1026
  grub_virtual_screen_cls (term);
1027

1028 1029
  /* Clear text layer.  */
  grub_video_set_active_render_target (text_layer);
1030
  color = virtual_screen.bg_color;
1031 1032 1033
  grub_video_fill_rect (color, 0, 0,
                        virtual_screen.width, virtual_screen.height);
  grub_video_set_active_render_target (render_target);
1034

1035 1036
  /* Mark virtual screen to be redrawn.  */
  dirty_region_add_virtualscreen ();
1037

1038
  grub_gfxterm_refresh (term);
1039 1040 1041
}

static void
1042
grub_virtual_screen_setcolorstate (struct grub_term_output *term __attribute__ ((unused)),
1043
				   grub_term_color_state state)
1044 1045 1046 1047
{
  switch (state)
    {
    case GRUB_TERM_COLOR_STANDARD:
1048 1049
      virtual_screen.term_color = virtual_screen.standard_color_setting;
      break;
1050

1051
    case GRUB_TERM_COLOR_NORMAL:
1052
      virtual_screen.term_color = grub_term_normal_color;
1053
      break;
1054

1055
    case GRUB_TERM_COLOR_HIGHLIGHT:
1056
      virtual_screen.term_color = grub_term_highlight_color;
1057
      break;
1058

1059 1060 1061
    default:
      break;
    }
1062 1063 1064 1065 1066 1067

  /* Change color to virtual terminal.  */
  set_term_color (virtual_screen.term_color);
}

static void
1068 1069
grub_gfxterm_setcursor (struct grub_term_output *term __attribute__ ((unused)),
			int on)
1070 1071 1072 1073
{
  if (virtual_screen.cursor_state != on)
    {
      if (virtual_screen.cursor_state)
1074
	draw_cursor (0);
1075
      else
1076
	draw_cursor (1);
1077 1078 1079 1080 1081 1082

      virtual_screen.cursor_state = on;
    }
}

static void
1083
grub_gfxterm_refresh (struct grub_term_output *term __attribute__ ((unused)))
1084
{
1085 1086
  real_scroll ();

1087 1088
  /* Redraw only changed regions.  */
  dirty_region_redraw ();
1089 1090

  grub_video_swap_buffers ();
1091

1092
  if (window.double_repaint)
1093 1094
    dirty_region_redraw ();
  dirty_region_reset ();
1095 1096
}

1097
static struct grub_term_output grub_video_term =
1098 1099
  {
    .name = "gfxterm",
1100 1101
    .init = grub_gfxterm_term_init,
    .fini = grub_gfxterm_term_fini,
1102 1103 1104 1105 1106 1107 1108 1109
    .putchar = grub_gfxterm_putchar,
    .getcharwidth = grub_gfxterm_getcharwidth,
    .getwh = grub_virtual_screen_getwh,
    .getxy = grub_virtual_screen_getxy,
    .gotoxy = grub_gfxterm_gotoxy,
    .cls = grub_gfxterm_cls,
    .setcolorstate = grub_virtual_screen_setcolorstate,
    .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 1115 1116
    .next = 0
  };

1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
void
grub_gfxterm_video_update_color (void)
{
  struct grub_video_render_target *old_target;

  grub_video_get_active_render_target (&old_target);
  grub_video_set_active_render_target (text_layer);
  virtual_screen.bg_color = grub_video_map_rgba_color (grub_gfxterm_background.default_bg_color);
  grub_video_set_active_render_target (old_target);
  virtual_screen.bg_color_display =
    grub_video_map_rgba_color (grub_gfxterm_background.default_bg_color);
}

void
grub_gfxterm_get_dimensions (unsigned *width, unsigned *height)
{
  *width = window.width;
  *height = window.height;
}
1136

1137
GRUB_MOD_INIT(gfxterm)
1138
{
1139
  grub_term_register_output ("gfxterm", &grub_video_term);
1140 1141
}

1142
GRUB_MOD_FINI(gfxterm)
1143
{
1144
  grub_term_unregister_output (&grub_video_term);
1145
}