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

Last change on this file since 1774 was 1462, checked in by Sam Hocevar, 14 years ago
  • Bwarf, typo in the no warranty clause.
  • Property svn:keywords set to Id
File size: 4.0 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 1462 2006-12-12 01:53:54Z 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#include "common.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_display_t *dp)
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    /* We don't have much choice */
79    _cucul_set_canvas_size(dp->cv, 80, 25);
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 unsigned int vga_get_display_width(caca_display_t *dp)
105{
106    /* Fallback to a 320x200 screen */
107    return 320;
108}
109
110static unsigned int vga_get_display_height(caca_display_t *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 *attrs = dp->cv->attrs;
120    uint32_t *chars = dp->cv->chars;
121    int n;
122
123    for(n = dp->cv->height * dp->cv->width; n--; )
124    {
125        char ch = cucul_utf32_to_cp437(*chars++);
126        if(n && *chars == CUCUL_MAGIC_FULLWIDTH)
127        {
128            *screen++ = '[';
129            *screen++ = cucul_attr_to_ansi(*attrs++);
130            ch = ']';
131            chars++;
132            n--;
133        }
134        *screen++ = ch;
135        *screen++ = cucul_attr_to_ansi(*attrs++);
136    }
137}
138
139static void vga_handle_resize(caca_display_t *dp)
140{
141    /* We know nothing about our window */
142    dp->resize.w = dp->cv->width;
143    dp->resize.h = dp->cv->height;
144}
145
146static int vga_get_event(caca_display_t *dp, caca_event_t *ev)
147{
148    /* FIXME */
149    ev->type = CACA_EVENT_NONE;
150    return 0;
151}
152
153/*
154 * Driver initialisation
155 */
156
157int vga_install(caca_display_t *dp)
158{
159    dp->drv.driver = CACA_DRIVER_VGA;
160
161    dp->drv.init_graphics = vga_init_graphics;
162    dp->drv.end_graphics = vga_end_graphics;
163    dp->drv.set_display_title = vga_set_display_title;
164    dp->drv.get_display_width = vga_get_display_width;
165    dp->drv.get_display_height = vga_get_display_height;
166    dp->drv.display = vga_display;
167    dp->drv.handle_resize = vga_handle_resize;
168    dp->drv.get_event = vga_get_event;
169    dp->drv.set_mouse = NULL;
170    dp->drv.set_cursor = NULL;
171
172    return 0;
173}
174
175#endif /* USE_VGA */
176
Note: See TracBrowser for help on using the repository browser.