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

Last change on this file since 2305 was 2305, checked in by Sam Hocevar, 12 years ago
  • Remove all unsigned ints from exported functions. Signed arithmetic is far better for error checking.
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: driver_vga.c 2305 2008-04-19 19:25:52Z 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 "cucul.h"
24#include "caca.h"
25#include "caca_internals.h"
26
27/* Address of the VGA screen */
28#define VGA_SCREEN ((char *)(intptr_t)0x000b8000)
29
30static uint8_t const vga_colors[][4] =
31{
32    /* Colour values range from 0x00 to 0x3f */
33    { 0,      0x00, 0x00, 0x00 },
34    { 1,      0x00, 0x00, 0x1f },
35    { 2,      0x00, 0x1f, 0x00 },
36    { 3,      0x00, 0x1f, 0x1f },
37    { 4,      0x1f, 0x00, 0x00 },
38    { 5,      0x1f, 0x00, 0x1f },
39    { 0x14,   0x1f, 0x1f, 0x00 },
40    { 7,      0x1f, 0x1f, 0x1f },
41
42    { 0x38,   0x0f, 0x0f, 0x0f },
43    { 0x39,   0x0f, 0x0f, 0x3f },
44    { 0x3a,   0x0f, 0x3f, 0x0f },
45    { 0x3b,   0x0f, 0x3f, 0x3f },
46    { 0x3c,   0x3f, 0x0f, 0x0f },
47    { 0x3d,   0x3f, 0x0f, 0x3f },
48    { 0x3e,   0x3f, 0x3f, 0x0f },
49    { 0x3f,   0x3f, 0x3f, 0x3f },
50};
51
52static int vga_init_graphics(caca_display_t *dp)
53{
54    int i;
55    uint8_t tmp;
56
57    /* Blank screen */
58    memset(VGA_SCREEN, 0, 80 * 25 * 2);
59
60    /* Fill VGA palette */
61    for(i = 0; i < 16; i++)
62    {
63        outb(vga_colors[i][0], 0x3c8);
64        outb(vga_colors[i][1], 0x3c9);
65        outb(vga_colors[i][2], 0x3c9);
66        outb(vga_colors[i][3], 0x3c9);
67    }
68
69    /* Hide cursor */
70    outb(0x0a, 0x3d4);
71    tmp = inb(0x3d5);
72    tmp |= 0x20;
73    outb(0x0a, 0x3d4);
74    outb(tmp, 0x3d5);
75
76    /* We don't have much choice */
77    dp->resize.allow = 1;
78    cucul_set_canvas_size(dp->cv, 80, 25);
79    dp->resize.allow = 0;
80
81    return 0;
82}
83
84static int vga_end_graphics(caca_display_t *dp)
85{
86    uint8_t tmp;
87
88    /* Show cursor again */
89    outb(0x0a, 0x3d4);
90    tmp = inb(0x3d5);
91    tmp &= 0xdf;
92    outb(0x0a, 0x3d4);
93    outb(tmp, 0x3d5);
94
95    return 0;
96}
97
98static int vga_set_display_title(caca_display_t *dp, char const *title)
99{
100    /* Unsupported, of course. */
101    return -1;
102}
103
104static int vga_get_display_width(caca_display_t const *dp)
105{
106    /* Fallback to a 320x200 screen */
107    return 320;
108}
109
110static int vga_get_display_height(caca_display_t const *dp)
111{
112    /* Fallback to a 320x200 screen */
113    return 200;
114}
115
116static void vga_display(caca_display_t *dp)
117{
118    char *screen = (char *)(intptr_t)0x000b8000;
119    uint32_t const *cvchars = (uint32_t const *)cucul_get_canvas_chars(dp->cv);
120    uint32_t const *cvattrs = (uint32_t const *)cucul_get_canvas_attrs(dp->cv);
121    int width = cucul_get_canvas_width(dp->cv);
122    int height = cucul_get_canvas_height(dp->cv);
123    int n;
124
125    for(n = height * width; n--; )
126    {
127        char ch = cucul_utf32_to_cp437(*cvchars++);
128        if(n && *cvchars == CUCUL_MAGIC_FULLWIDTH)
129        {
130            *screen++ = '[';
131            *screen++ = cucul_attr_to_ansi(*cvattrs++);
132            ch = ']';
133            cvchars++;
134            n--;
135        }
136        *screen++ = ch;
137        *screen++ = cucul_attr_to_ansi(*cvattrs++);
138    }
139}
140
141static void vga_handle_resize(caca_display_t *dp)
142{
143    /* We know nothing about our window */
144    dp->resize.w = cucul_get_canvas_width(dp->cv);
145    dp->resize.h = cucul_get_canvas_height(dp->cv);
146}
147
148static int vga_get_event(caca_display_t *dp, caca_privevent_t *ev)
149{
150    /* FIXME */
151    ev->type = CACA_EVENT_NONE;
152    return 0;
153}
154
155/*
156 * Driver initialisation
157 */
158
159int vga_install(caca_display_t *dp)
160{
161    dp->drv.id = CACA_DRIVER_VGA;
162    dp->drv.driver = "vga";
163
164    dp->drv.init_graphics = vga_init_graphics;
165    dp->drv.end_graphics = vga_end_graphics;
166    dp->drv.set_display_title = vga_set_display_title;
167    dp->drv.get_display_width = vga_get_display_width;
168    dp->drv.get_display_height = vga_get_display_height;
169    dp->drv.display = vga_display;
170    dp->drv.handle_resize = vga_handle_resize;
171    dp->drv.get_event = vga_get_event;
172    dp->drv.set_mouse = NULL;
173    dp->drv.set_cursor = NULL;
174
175    return 0;
176}
177
178#endif /* USE_VGA */
179
Note: See TracBrowser for help on using the repository browser.