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

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

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

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