source: libcaca/trunk/caca/driver/vga.c @ 3624

Last change on this file since 3624 was 3624, checked in by Sam Hocevar, 10 years ago

Fix a typo in the VGA driver.

  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  $Id: vga.c 3624 2009-08-03 23:26:23Z sam $
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains the libcaca VGA input and output driver
17 */
18
19#include "config.h"
20
21#if defined(USE_VGA)
22
23#include "caca.h"
24#include "caca_internals.h"
25
26/* Address of the VGA screen */
27#define VGA_SCREEN ((char *)(intptr_t)0x000b8000)
28
29static uint8_t const vga_colors[][4] =
30{
31    /* Colour values range from 0x00 to 0x3f */
32    { 0,      0x00, 0x00, 0x00 },
33    { 1,      0x00, 0x00, 0x1f },
34    { 2,      0x00, 0x1f, 0x00 },
35    { 3,      0x00, 0x1f, 0x1f },
36    { 4,      0x1f, 0x00, 0x00 },
37    { 5,      0x1f, 0x00, 0x1f },
38    { 0x14,   0x1f, 0x1f, 0x00 },
39    { 7,      0x1f, 0x1f, 0x1f },
40
41    { 0x38,   0x0f, 0x0f, 0x0f },
42    { 0x39,   0x0f, 0x0f, 0x3f },
43    { 0x3a,   0x0f, 0x3f, 0x0f },
44    { 0x3b,   0x0f, 0x3f, 0x3f },
45    { 0x3c,   0x3f, 0x0f, 0x0f },
46    { 0x3d,   0x3f, 0x0f, 0x3f },
47    { 0x3e,   0x3f, 0x3f, 0x0f },
48    { 0x3f,   0x3f, 0x3f, 0x3f },
49};
50
51static int vga_init_graphics(caca_display_t *dp)
52{
53    int i;
54    uint8_t tmp;
55
56    /* Blank screen */
57    memset(VGA_SCREEN, 0, 80 * 25 * 2);
58
59    /* Fill VGA palette */
60    for(i = 0; i < 16; i++)
61    {
62        outb(vga_colors[i][0], 0x3c8);
63        outb(vga_colors[i][1], 0x3c9);
64        outb(vga_colors[i][2], 0x3c9);
65        outb(vga_colors[i][3], 0x3c9);
66    }
67
68    /* Hide cursor */
69    outb(0x0a, 0x3d4);
70    tmp = inb(0x3d5);
71    tmp |= 0x20;
72    outb(0x0a, 0x3d4);
73    outb(tmp, 0x3d5);
74
75    caca_add_dirty_rect(dp->cv, 0, 0, dp->cv->width, dp->cv->height);
76    dp->resize.allow = 1;
77    caca_set_canvas_size(dp->cv, 80, 25); /* We don't have much choice */
78    dp->resize.allow = 0;
79
80    return 0;
81}
82
83static int vga_end_graphics(caca_display_t *dp)
84{
85    uint8_t tmp;
86
87    /* Show cursor again */
88    outb(0x0a, 0x3d4);
89    tmp = inb(0x3d5);
90    tmp &= 0xdf;
91    outb(0x0a, 0x3d4);
92    outb(tmp, 0x3d5);
93
94    return 0;
95}
96
97static int vga_set_display_title(caca_display_t *dp, char const *title)
98{
99    /* Unsupported, of course. */
100    return -1;
101}
102
103static int vga_get_display_width(caca_display_t const *dp)
104{
105    /* Fallback to a 320x200 screen */
106    return 320;
107}
108
109static int vga_get_display_height(caca_display_t const *dp)
110{
111    /* Fallback to a 320x200 screen */
112    return 200;
113}
114
115static void vga_display(caca_display_t *dp)
116{
117    int x, y, i;
118
119    for(i = 0; i < caca_get_dirty_rect_count(dp->cv); i++)
120    {
121        char *screen = (char *)(intptr_t)0x000b8000;
122        uint32_t const *cvchars, *cvattrs;
123        int dx, dy, dw, dh;
124
125        caca_get_dirty_rect(dp->cv, i, &dx, &dy, &dw, &dh);
126
127        cvchars = (uint32_t const *)caca_get_canvas_chars(dp->cv)
128                    + dx + dy * dp->cv->width;
129        cvattrs = (uint32_t const *)caca_get_canvas_attrs(dp->cv)
130                    + dx + dy * dp->cv->width;
131
132        screen += dy * dp->cv->width + dx;
133
134        for(y = dy; y < dy + dh; y++)
135        {
136            for(x = dx; x < dx + dw; x++)
137            {
138                char ch = caca_utf32_to_cp437(*cvchars++);
139                if(x < dx + dw - 1 && *cvchars == CACA_MAGIC_FULLWIDTH)
140                {
141                    *screen++ = '[';
142                    *screen++ = caca_attr_to_ansi(*cvattrs++);
143                    ch = ']';
144                    cvchars++;
145                    x++;
146                }
147                *screen++ = ch;
148                *screen++ = caca_attr_to_ansi(*cvattrs++);
149            }
150
151            cvchars += dp->cv->width - dw;
152            cvattrs += dp->cv->width - dw;
153            screen += 2 * (dp->cv->width - dw);
154        }
155    }
156}
157
158static void vga_handle_resize(caca_display_t *dp)
159{
160    /* We know nothing about our window */
161    dp->resize.w = caca_get_canvas_width(dp->cv);
162    dp->resize.h = caca_get_canvas_height(dp->cv);
163}
164
165static int vga_get_event(caca_display_t *dp, caca_privevent_t *ev)
166{
167    /* FIXME */
168    ev->type = CACA_EVENT_NONE;
169    return 0;
170}
171
172/*
173 * Driver initialisation
174 */
175
176int vga_install(caca_display_t *dp)
177{
178    dp->drv.id = CACA_DRIVER_VGA;
179    dp->drv.driver = "vga";
180
181    dp->drv.init_graphics = vga_init_graphics;
182    dp->drv.end_graphics = vga_end_graphics;
183    dp->drv.set_display_title = vga_set_display_title;
184    dp->drv.get_display_width = vga_get_display_width;
185    dp->drv.get_display_height = vga_get_display_height;
186    dp->drv.display = vga_display;
187    dp->drv.handle_resize = vga_handle_resize;
188    dp->drv.get_event = vga_get_event;
189    dp->drv.set_mouse = NULL;
190    dp->drv.set_cursor = NULL;
191
192    return 0;
193}
194
195#endif /* USE_VGA */
196
Note: See TracBrowser for help on using the repository browser.