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

Last change on this file since 632 was 631, checked in by Sam Hocevar, 14 years ago
  • Duplicated the conio.h Unicode->CP437 conversion into the VGA driver.
  • Cosmetic code changes.
  • Property svn:keywords set to Id
File size: 6.3 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 631 2006-03-16 12:58:55Z 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
29static uint8_t vga_utf32_to_cp437(uint32_t);
30
31/* Address of the VGA screen */
32#define VGA_SCREEN ((char *)(intptr_t)0x000b8000)
33
34static uint8_t const vga_colors[][4] =
35{
36    /* Colour values range from 0x00 to 0x3f */
37    { 0,      0x00, 0x00, 0x00 },
38    { 1,      0x00, 0x00, 0x1f },
39    { 2,      0x00, 0x1f, 0x00 },
40    { 3,      0x00, 0x1f, 0x1f },
41    { 4,      0x1f, 0x00, 0x00 },
42    { 5,      0x1f, 0x00, 0x1f },
43    { 0x14,   0x1f, 0x1f, 0x00 },
44    { 7,      0x1f, 0x1f, 0x1f },
45
46    { 0x38,   0x0f, 0x0f, 0x0f },
47    { 0x39,   0x0f, 0x0f, 0x3f },
48    { 0x3a,   0x0f, 0x3f, 0x0f },
49    { 0x3b,   0x0f, 0x3f, 0x3f },
50    { 0x3c,   0x3f, 0x0f, 0x0f },
51    { 0x3d,   0x3f, 0x0f, 0x3f },
52    { 0x3e,   0x3f, 0x3f, 0x0f },
53    { 0x3f,   0x3f, 0x3f, 0x3f },
54};
55
56static int vga_init_graphics(caca_t *kk)
57{
58    int i;
59    uint8_t tmp;
60
61    /* Blank screen */
62    memset(VGA_SCREEN, 0, 80 * 25 * 2);
63
64    /* Fill VGA palette */
65    for(i = 0; i < 16; i++)
66    {
67        outb(vga_colors[i][0], 0x3c8);
68        outb(vga_colors[i][1], 0x3c9);
69        outb(vga_colors[i][2], 0x3c9);
70        outb(vga_colors[i][3], 0x3c9);
71    }
72
73    /* Hide cursor */
74    outb(0x0a, 0x3d4);
75    tmp = inb(0x3d5);
76    tmp |= 0x20;
77    outb(0x0a, 0x3d4);
78    outb(tmp, 0x3d5);
79
80    /* We don't have much choice */
81    _cucul_set_size(kk->qq, 80, 25);
82
83    return 0;
84}
85
86static int vga_end_graphics(caca_t *kk)
87{
88    uint8_t tmp;
89
90    /* Show cursor again */
91    outb(0x0a, 0x3d4);
92    tmp = inb(0x3d5);
93    tmp &= 0xdf;
94    outb(0x0a, 0x3d4);
95    outb(tmp, 0x3d5);
96
97    return 0;
98}
99
100static int vga_set_window_title(caca_t *kk, char const *title)
101{
102    /* Unsupported, of course. */
103    return 0;
104}
105
106static unsigned int vga_get_window_width(caca_t *kk)
107{
108    /* Fallback to a 320x200 screen */
109    return 320;
110}
111
112static unsigned int vga_get_window_height(caca_t *kk)
113{
114    /* Fallback to a 320x200 screen */
115    return 200;
116}
117
118static void vga_display(caca_t *kk)
119{
120    char *screen = (char *)(intptr_t)0x000b8000;
121    uint8_t *attr = kk->qq->attr;
122    uint32_t *chars = kk->qq->chars;
123    int n;
124
125    for(n = kk->qq->height * kk->qq->width; n--; )
126    {
127        *screen++ = vga_utf32_to_cp437(*chars++);
128        *screen++ = *attr++;
129    }
130}
131
132static void vga_handle_resize(caca_t *kk)
133{
134    /* We know nothing about our window */
135    kk->resize.w = kk->qq->width;
136    kk->resize.h = kk->qq->height;
137}
138
139static unsigned int vga_get_event(caca_t *kk)
140{
141    /* FIXME */
142    return CACA_EVENT_NONE;
143}
144
145/*
146 * XXX: following functions are local
147 */
148
149static uint8_t vga_utf32_to_cp437(uint32_t c)
150{
151    static uint32_t const lookup1[] =
152    {
153        /* 0x01 - 0x0f: ☺ ☻ ♥ ♦ ♣ ♠ • ◘ ○ ◙ ♂ ♀ ♪ ♫ ☼ */
154                0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
155        0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
156        /* 0x10 - 0x1f: ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼ */
157        0x25ba, 0x25c4, 0x2195, 0x203c, 0xb6, 0xa7, 0x25ac, 0x21a8,
158        0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc
159    };
160
161    static uint32_t const lookup2[] =
162    {
163        /* 0x7f: ⌂ */
164        0x2302,
165        /* 0x80 - 0x8f: Ç ü é â ä à å ç ê ë è ï î ì Ä Å */
166        0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
167        0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
168        /* 0x90 - 0x9f: É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ */
169        0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
170        0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0x20a7, 0x192,
171        /* 0xa0 - 0xaf: á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « » */
172        0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
173        0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
174        /* 0xb0 - 0xbf: ░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐ */
175        0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
176        0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
177        /* 0xc0 - 0xcf: └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧ */
178        0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
179        0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
180        /* 0xd0 - 0xdf: ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐ ▀ */
181        0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
182        0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
183        /* 0xe0 - 0xef: α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ */
184        0x3b1, 0xdf, 0x393, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
185        0x3a6, 0x398, 0x3a9, 0x3b4, 0x221e, 0x3c6, 0x3b5, 0x2229,
186        /* 0xf0 - 0xff: ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■ <nbsp> */
187        0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
188        0xb0, 0x2219, 0xb7, 0x221a, 0x207f, 0xb2, 0x25a0, 0xa0
189    };
190
191    unsigned int i;
192
193    if(c < 0x00000020)
194        return '?';
195
196    if(c < 0x00000080)
197        return c;
198
199    for(i = 0; i < sizeof(lookup1) / sizeof(*lookup1); i++)
200        if(lookup1[i] == c)
201            return 0x01 + i;
202
203    for(i = 0; i < sizeof(lookup2) / sizeof(*lookup2); i++)
204        if(lookup2[i] == c)
205            return 0x7f + i;
206
207    return '?';
208}
209
210/*
211 * Driver initialisation
212 */
213
214void vga_init_driver(caca_t *kk)
215{
216    kk->drv.driver = CACA_DRIVER_VGA;
217
218    kk->drv.init_graphics = vga_init_graphics;
219    kk->drv.end_graphics = vga_end_graphics;
220    kk->drv.set_window_title = vga_set_window_title;
221    kk->drv.get_window_width = vga_get_window_width;
222    kk->drv.get_window_height = vga_get_window_height;
223    kk->drv.display = vga_display;
224    kk->drv.handle_resize = vga_handle_resize;
225    kk->drv.get_event = vga_get_event;
226}
227
228#endif /* USE_VGA */
229
Note: See TracBrowser for help on using the repository browser.