source: libcaca/trunk/caca/driver_vga.c @ 574

Last change on this file since 574 was 574, checked in by Sam Hocevar, 17 years ago
  • Fixed VGA colours. The values only range from 0 to 63.
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1/*
2 *  libcaca       ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
10 */
11
12/** \file driver_vga.c
13 *  \version \$Id: driver_vga.c 574 2006-03-09 16:57:32Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief VGA driver
16 *
17 *  This file contains the libcaca VGA input and output driver
18 */
19
20#include "config.h"
21
22#if defined(USE_VGA)
23
24#include "caca.h"
25#include "caca_internals.h"
26#include "cucul.h"
27#include "cucul_internals.h"
28
29/* Address of the VGA screen */
30#define VGA_SCREEN ((char *)(intptr_t)0x000b8000)
31
32static uint8_t const vga_colors[][4] =
33{
34    /* Colour values range from 0x00 to 0x3f */
35    { 0,      0x00, 0x00, 0x00 },
36    { 1,      0x00, 0x00, 0x1f },
37    { 2,      0x00, 0x1f, 0x00 },
38    { 3,      0x00, 0x1f, 0x1f },
39    { 4,      0x1f, 0x00, 0x00 },
40    { 5,      0x1f, 0x00, 0x1f },
41    { 0x14,   0x1f, 0x1f, 0x00 },
42    { 7,      0x1f, 0x1f, 0x1f },
43
44    { 0x38,   0x0f, 0x0f, 0x0f },
45    { 0x39,   0x0f, 0x0f, 0x3f },
46    { 0x3a,   0x0f, 0x3f, 0x0f },
47    { 0x3b,   0x0f, 0x3f, 0x3f },
48    { 0x3c,   0x3f, 0x0f, 0x0f },
49    { 0x3d,   0x3f, 0x0f, 0x3f },
50    { 0x3e,   0x3f, 0x3f, 0x0f },
51    { 0x3f,   0x3f, 0x3f, 0x3f },
52};
53
54static int vga_init_graphics(caca_t *kk)
55{
56    int i;
57    uint8_t tmp;
58
59    /* Blank screen */
60    memset(VGA_SCREEN, 0, 80 * 25 * 2);
61
62    /* Fill VGA palette */
63    for(i = 0; i < 16; i++)
64    {
65        outb(vga_colors[i][0], 0x3c8);
66        outb(vga_colors[i][1], 0x3c9);
67        outb(vga_colors[i][2], 0x3c9);
68        outb(vga_colors[i][3], 0x3c9);
69    }
70
71    /* Hide cursor */
72    outb(0x0a, 0x3d4);
73    tmp = inb(0x3d5);
74    tmp |= 0x20;
75    outb(0x0a, 0x3d4);
76    outb(tmp, 0x3d5);
77
78    return 0;
79}
80
81static int vga_end_graphics(caca_t *kk)
82{
83    uint8_t tmp;
84
85    /* Show cursor again */
86    outb(0x0a, 0x3d4);
87    tmp = inb(0x3d5);
88    tmp &= 0xdf;
89    outb(0x0a, 0x3d4);
90    outb(tmp, 0x3d5);
91
92    return 0;
93}
94
95static int vga_set_window_title(caca_t *kk, char const *title)
96{
97    /* Unsupported, of course. */
98    return 0;
99}
100
101static unsigned int vga_get_window_width(caca_t *kk)
102{
103    /* Fallback to a 6x10 font */
104    return kk->qq->width * 6;
105}
106
107static unsigned int vga_get_window_height(caca_t *kk)
108{
109    /* Fallback to a 6x10 font */
110    return kk->qq->height * 10;
111}
112
113static void vga_display(caca_t *kk)
114{
115    char *screen = (char *)(intptr_t)0x000b8000;
116    uint8_t *attr = kk->qq->attr;
117    uint32_t *chars = kk->qq->chars;
118    int n;
119
120    for(n = kk->qq->height * kk->qq->width; n--; )
121    {
122        uint32_t c = *chars++;
123
124        if(c > 0x00000020 && c < 0x00000080)
125            *screen++ = (char)c;
126        else
127            *screen++ = ' ';
128
129        *screen++ = *attr++;
130    }
131}
132
133static void vga_handle_resize(caca_t *kk)
134{
135    /* We know nothing about our window */
136    kk->resize.w = kk->qq->width;
137    kk->resize.h = kk->qq->height;
138}
139
140static unsigned int vga_get_event(caca_t *kk)
141{
142    /* FIXME */
143    return CACA_EVENT_NONE;
144}
145
146/*
147 * Driver initialisation
148 */
149
150void vga_init_driver(caca_t *kk)
151{
152    kk->drv.driver = CACA_DRIVER_VGA;
153
154    kk->drv.init_graphics = vga_init_graphics;
155    kk->drv.end_graphics = vga_end_graphics;
156    kk->drv.set_window_title = vga_set_window_title;
157    kk->drv.get_window_width = vga_get_window_width;
158    kk->drv.get_window_height = vga_get_window_height;
159    kk->drv.display = vga_display;
160    kk->drv.handle_resize = vga_handle_resize;
161    kk->drv.get_event = vga_get_event;
162}
163
164#endif /* USE_VGA */
165
Note: See TracBrowser for help on using the repository browser.