summaryrefslogtreecommitdiff
path: root/console-client/vga.c
blob: 304adbc1fc3f6e07cad583a2736f2134e27d2c30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/* vga.c - The VGA device display driver.
   Copyright (C) 2002 Free Software Foundation, Inc.
   Written by Marcus Brinkmann.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, 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
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#include <assert.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <unistd.h>
#include <iconv.h>
#include <argp.h>
#include <string.h>
#include <stdint.h>

#include <sys/io.h>
#include <sys/mman.h>
#include <sys/fcntl.h>
#include <cthreads.h>
#include <hurd/console.h>

#include "driver.h"
#include "timer.h"

#include "vga-hw.h"
#include "vga-support.h"
#include "bdf.h"
#include "vga-dynafont.h"
#include "vga-dynacolor.h"
#include "unicode.h"


#define VGA_DISP_WIDTH 80
#define VGA_DISP_HEIGHT 25

/* The font file.  */
#define DEFAULT_VGA_FONT "/lib/hurd/fonts/vga-system.bdf"
static char *vga_display_font;

#define DEFAULT_VGA_FONT_ITALIC "/lib/hurd/fonts/vga-system-italic.bdf"
static char *vga_display_font_italic;

#define DEFAULT_VGA_FONT_BOLD "/lib/hurd/fonts/vga-system-bold.bdf"
static char *vga_display_font_bold;

#define DEFAULT_VGA_FONT_BOLD_ITALIC \
	"/lib/hurd/fonts/vga-system-bold-italic.bdf"
static char *vga_display_font_bold_italic;

/* The timer used for flashing the screen.  */
static struct timer_list vga_display_timer;

/* The lock that protects the color palette manipulation.  */
static struct mutex vga_display_lock;

/* Forward declaration.  */
static struct display_ops vga_display_ops;


struct refchr
{
  unsigned int used : 1;
  unsigned int chr : 9;
  unsigned int attr : 8;
};


struct vga_display
{
  /* The VGA font for this display.  */
  dynafont_t df;
  int df_size;

  /* The color palette.  */
  dynacolor_t dc;

  int width;
  int height;

  /* Current attribute.  */
  int cur_conchar_attr_init;
  conchar_attr_t cur_conchar_attr;
  char cur_attr;

  /* Remember for each cell on the display the glyph written to it and
     the colors (in the upper byte) assigned.  0 means unassigned.  */

  struct refchr refmatrix[VGA_DISP_HEIGHT][VGA_DISP_WIDTH];
};


static void
vga_display_invert_border (void)
{
  unsigned char col[3];

  mutex_lock (&vga_display_lock);
  vga_read_palette (0, col, 1);
  col[0] = 0xff - col[0];
  col[1] = 0xff - col[1];
  col[2] = 0xff - col[2];
  vga_write_palette (0, col, 1);
  mutex_unlock (&vga_display_lock);
}


static int
vga_display_flash_off (void *dummy)
{
  vga_display_invert_border ();
  return 0;
}


static error_t
vga_display_flash (void *handle)
{
  if (timer_remove (&vga_display_timer))
    vga_display_invert_border ();
  vga_display_invert_border ();
  vga_display_timer.expires = fetch_jiffies () + 10;
  timer_add (&vga_display_timer);
  return 0;
}


/* Parse arguments at startup.  */
static error_t
parse_startup_args (int no_exit, int argc, char *argv[], int *next)
{
#define PARSE_FONT_OPT(x,y)				\
    do {						\
      if (!strcmp (argv[*next], x))			\
	{						\
	  (*next)++;					\
	  if (*next == argc)				\
	    {						\
	      if (no_exit)				\
		return EINVAL;				\
	      else					\
		error (1, 0, "option " x		\
		       " requires an argument");	\
	    }						\
	  if (vga_display_##y)				\
	    free (vga_display_##y);			\
	  vga_display_##y = strdup (argv[*next]);	\
	  if (!vga_display_##y)				\
	    {						\
	      if (no_exit)				\
		return errno;				\
	      else					\
		error (1, errno, "malloc failed");	\
	    }						\
	  (*next)++;					\
          continue;					\
	}						\
      } while (0)

  while (*next < argc)
    {
      PARSE_FONT_OPT ("--font", font);
      PARSE_FONT_OPT ("--font-italic", font_italic);
      PARSE_FONT_OPT ("--font-bold", font_bold);
      PARSE_FONT_OPT ("--font-bold-italic", font_bold_italic);

      break;
    }
  return 0;
}

/* Initialize the subsystem.  */
static error_t
vga_display_init (void **handle, int no_exit, int argc, char *argv[], int *next)
{
  error_t err;
  struct vga_display *disp;

  /* XXX Assert that we are called only once.  */
  mutex_init (&vga_display_lock);
  timer_clear (&vga_display_timer);
  vga_display_timer.fnc = &vga_display_flash_off;

  /* Parse the arguments.  */
  err = parse_startup_args (no_exit, argc, argv, next);
  if (err)
    return err;

  /* Create and initialize the display srtucture as much as
     possible.  */
  disp = calloc (1, sizeof *disp);
  if (!disp)
    return ENOMEM;

  /* Set this to 256 for full color support.  */
  disp->df_size = 512;
  disp->width = VGA_DISP_WIDTH;
  disp->height = VGA_DISP_HEIGHT;

  *handle = disp;
  return 0;
}


/* Start the driver.  */
static error_t
vga_display_start (void *handle)
{
  error_t err;
  struct vga_display *disp = handle;
  bdf_font_t font = NULL;
  bdf_font_t font_italic = NULL;
  bdf_font_t font_bold = NULL;
  bdf_font_t font_bold_italic = NULL;
  FILE *font_file;

  err = vga_init ();
  if (err)
    return err;

  dynacolor_init ();

#define LOAD_FONT(x,y)							\
  do {									\
  font_file = fopen (vga_display_##x ?: DEFAULT_VGA_##y, "r");		\
  if (font_file)							\
    {									\
      bdf_error_t bdferr = bdf_read (font_file, &x, NULL);		\
      if (bdferr)							\
	x = NULL;							\
      else								\
	bdf_sort_glyphs (x);						\
      fclose (font_file);						\
    }									\
  } while (0)

  LOAD_FONT (font, FONT);
  LOAD_FONT (font_italic, FONT_ITALIC);
  LOAD_FONT (font_bold, FONT_BOLD);
  LOAD_FONT (font_bold_italic, FONT_BOLD_ITALIC);

  err = dynafont_new (font, font_italic, font_bold, font_bold_italic,
		      disp->df_size, &disp->df);
  if (err)
    {
      free (disp);
      vga_fini ();
      return err;
    }
  dynafont_activate (disp->df);

  disp->dc = (disp->df_size == 512) ? dynacolor_init_8 : dynacolor_init_16;
  dynacolor_activate (&disp->dc);

  err = driver_add_display (&vga_display_ops, disp);
  if (err)
    {
      dynafont_free (disp->df);
      dynacolor_fini ();
      vga_fini ();
      free (disp);
    }
  return err;
}


/* Destroy the display HANDLE.  */
static error_t
vga_display_fini (void *handle, int force)
{
  struct vga_display *disp = handle;
  driver_remove_display (&vga_display_ops, disp);
  if (timer_remove (&vga_display_timer))
    vga_display_flash_off (0);

  dynafont_free (disp->df);
  free (disp);
  dynacolor_fini ();
  vga_fini ();
  if (vga_display_font)
    free (vga_display_font);
  if (vga_display_font_italic)
    free (vga_display_font_italic);
  if (vga_display_font_bold)
    free (vga_display_font_bold);
  if (vga_display_font_bold_italic)
    free (vga_display_font_bold_italic);

  return 0;
}


/* Set the cursor's position on display HANDLE to column COL and row
   ROW.  */
static error_t
vga_display_set_cursor_pos (void *handle, uint32_t col, uint32_t row)
{
  struct vga_display *disp = handle;
  unsigned int pos = row * disp->width + col;

  vga_set_cursor_pos (pos);

  return 0;
}


/* Set the cursor's state to STATE on display HANDLE.  */
static error_t
vga_display_set_cursor_status (void *handle, uint32_t state)
{
  struct vga_display *disp = handle;

  if (state != CONS_CURSOR_INVISIBLE)
    dynafont_set_cursor (disp->df, state == CONS_CURSOR_VERY_VISIBLE ? 1 : 0);
  vga_display_cursor (state == CONS_CURSOR_INVISIBLE ? 0 : 1);

  return 0;
}


/* Scroll the display by the desired amount.  The area that becomes
   free will be filled in a subsequent write call.  */
static error_t
vga_display_scroll (void *handle, int delta)
{
  struct vga_display *disp = handle;
  int count = abs(delta) * disp->width;
  int i;
  struct refchr *refpos;
      
  if (delta > 0)
    {
      memmove (vga_videomem, vga_videomem + 2 * count,
	       2 * disp->width * (disp->height - delta));
      refpos = &disp->refmatrix[0][0];
    }
  else
    {
      memmove (vga_videomem + 2 * count, vga_videomem,
	       2 * disp->width * (disp->height + delta));
      refpos = &disp->refmatrix[disp->height + delta][0];
    }
  
  for (i = 0; i < count; i++)
    {
      if (refpos->used)
	{
	  dynafont_release (disp->df, refpos->chr);
	  /* We intimately know that reference counting is only done
	     for the up to 8 colors mode.  */
	  dynacolor_release (disp->dc, refpos->attr & 7);
	  dynacolor_release (disp->dc, (refpos->attr >> 4) & 7);
	}
      refpos++;
    }

  if (delta > 0)
    {
      memmove (&disp->refmatrix[0][0], &disp->refmatrix[0][0] + count,
	       sizeof (struct refchr) * disp->width * (disp->height - delta));
      refpos = &disp->refmatrix[disp->height - delta][0];
    }
  else
    {
      memmove (&disp->refmatrix[0][0] + count, &disp->refmatrix[0][0],
	       sizeof (struct refchr) * disp->width * (disp->height + delta));
      refpos = &disp->refmatrix[0][0];
    }

  for (i = 0; i < count; i++)
    (refpos++)->used = 0;

  return 0;
}


/* Change the font on the console CONSOLE to font.  The old font will
   not be accessed by the vga console subsystem anymore after this
   call completed.  */
static void
vga_display_change_font (void *handle, bdf_font_t font)
{
  struct vga_display *disp = handle;

  dynafont_change_font (disp->df, font);
}


static inline char
vga_display_recalculate_attr (dynacolor_t *dc, conchar_attr_t attr)
{
  char vga_attr;
  signed char res_fgcol;
  signed char res_bgcol;
  signed char fgcol;
  signed char bgcol;

  /* VGA has way too few bits for this stuff.  The highest background
     color bit is also the blinking bit if blinking is enabled.  The
     highest foreground color bit is the font selector bit,
     unfortunately.  Underlining is enabled if foreground is ?001 and
     background ?000.  */
  
  /* Reversed means colors are reversed.  Note that this does not
     reverse the intensity.  */
  if (attr.reversed)
    {
      fgcol = attr.bgcol;
      bgcol = attr.fgcol;
    }
  else
    {
      fgcol = attr.fgcol;
      bgcol = attr.bgcol;
    }

  /* Set the foreground color.  */
  if (attr.concealed)
    fgcol = bgcol;
  else
    {
      /* Intensity bold and dim also affect the font selection bit.  */
      switch (attr.intensity)
	{
	case CONS_ATTR_INTENSITY_BOLD:
	  fgcol |= 1 << 3;
	  break;
	case CONS_ATTR_INTENSITY_DIM:
	  fgcol = CONS_COLOR_BLACK | 1 << 3;
	  break;
	case CONS_ATTR_INTENSITY_NORMAL:
	  break;
	}
    }

  /* Try to get the colors as desired.  This might change the palette,
     so we need to take the lock (in case a flash operation times
     out).  */
  mutex_lock (&vga_display_lock);
  res_bgcol = dynacolor_lookup (*dc, bgcol);
  res_fgcol = dynacolor_lookup (*dc, fgcol);
  mutex_unlock (&vga_display_lock);
  if (res_bgcol == -1 || res_fgcol == -1)
    dynacolor_replace_colors (dc, fgcol, bgcol, &res_fgcol, &res_bgcol);
  vga_attr = res_bgcol << 4 | res_fgcol;

  vga_attr |= attr.blinking << 7;
  
  /* XXX We can support underlined in a monochrome mode.  */
  return vga_attr;
}


/* Deallocate any scarce resources occupied by the LENGTH characters
   from column COL and row ROW.  */
static error_t
vga_display_clear (void *handle, size_t length, uint32_t col, uint32_t row)
{
  struct vga_display *disp = handle;
  struct refchr *refpos = &disp->refmatrix[row][col];

  while (length > 0)
    {
      if (refpos->used)
	{
	  dynafont_release (disp->df, refpos->chr);
	  /* We intimately know that reference counting is only done
	     for the up to 8 colors mode.  */
	  dynacolor_release (disp->dc, refpos->attr & 7);
	  dynacolor_release (disp->dc, (refpos->attr >> 4) & 7);
	  refpos->used = 0;
	}
      refpos++;
      length--;
    }
  return 0;
}

/* Write the text STR with LENGTH characters to column COL and row
   ROW.  */
static error_t
vga_display_write (void *handle, conchar_t *str, size_t length,
		   uint32_t col, uint32_t row)
{
  struct vga_display *disp = handle;
  char *pos = vga_videomem + 2 * (row * disp->width + col);
  struct refchr *refpos = &disp->refmatrix[row][col];

  /* Although all references to the current fgcol or bgcol could have
     been released here, for example due to a scroll operation, we
     know that the color slots have not been reused yet, as no routine
     but ours does color allocation.  This ensures that cur_attr is
     still valid.  XXX consider recalculating the attribute for more
     authentic (but less homogenous) colors anyway.  */

  while (length--)
    {
      int charval = dynafont_lookup (disp->df, str);
      if (!disp->cur_conchar_attr_init
	  || *(uint32_t *) &disp->cur_conchar_attr != *(uint32_t *) &str->attr)
	{
	  if (!disp->cur_conchar_attr_init)
	    disp->cur_conchar_attr_init = 1;
	  disp->cur_conchar_attr = str->attr;
	  disp->cur_attr = vga_display_recalculate_attr (&disp->dc, str->attr);
	}
      else
	{
	  /* Add two references to the colors.  See comment above for
	     why we can assume that this will succeed.  */
	  /* We intimately know that reference counting is only done
	     for the up to 8 colors mode.  */
	  dynacolor_add_ref (disp->dc, disp->cur_attr & 7);
	  dynacolor_add_ref (disp->dc, (disp->cur_attr >> 4) & 7);
	}

      *(pos++) = charval & 0xff;
      *(pos++) = disp->cur_attr
	| (disp->df_size == 512 ? (charval >> 5) & 0x8 : 0);

      /* Perform reference counting.  */
      if (refpos->used)
	{
	  dynafont_release (disp->df, refpos->chr);
	  /* We intimately know that reference counting is only done
	     for the up to 8 colors mode.  */
	  dynacolor_release (disp->dc, refpos->attr & 7);
	  dynacolor_release (disp->dc, (refpos->attr >> 4) & 7);
	}
      refpos->used = 1;
      refpos->chr = charval;
      refpos->attr = disp->cur_attr;
      refpos++;

      /* Wrap around displayed area.  */
      str++;
    }
  return 0;
}


struct driver_ops driver_vga_ops =
  {
    vga_display_init,
    vga_display_start,
    vga_display_fini,
  };

static struct display_ops vga_display_ops =
  {
    vga_display_set_cursor_pos,
    vga_display_set_cursor_status,
    vga_display_scroll,
    vga_display_clear,
    vga_display_write,
    NULL,
    vga_display_flash,
    NULL
  };