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

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