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

Last change on this file since 1006 was 1006, checked in by Sam Hocevar, 14 years ago
  • Fixed libcaca prototypes so that all functions use the errno mechanism and return a value.
  • Property svn:keywords set to Id
File size: 3.7 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 1006 2006-08-02 13:12:43Z sam $
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 *  This file contains the libcaca VGA input and output driver
16 */
17
18#include "config.h"
19#include "common.h"
20
21#if defined(USE_VGA)
22
23#include "caca.h"
24#include "caca_internals.h"
25#include "cucul.h"
26#include "cucul_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    _cucul_set_canvas_size(dp->cv, 80, 25);
79
80    return 0;
81}
82
83static int vga_end_graphics(caca_display_t *dp)
84{
85    uint8_t tmp;
86
87    /* Show cursor again */
88    outb(0x0a, 0x3d4);
89    tmp = inb(0x3d5);
90    tmp &= 0xdf;
91    outb(0x0a, 0x3d4);
92    outb(tmp, 0x3d5);
93
94    return 0;
95}
96
97static int vga_set_display_title(caca_display_t *dp, char const *title)
98{
99    /* Unsupported, of course. */
100    return -1;
101}
102
103static unsigned int vga_get_display_width(caca_display_t *dp)
104{
105    /* Fallback to a 320x200 screen */
106    return 320;
107}
108
109static unsigned int vga_get_display_height(caca_display_t *dp)
110{
111    /* Fallback to a 320x200 screen */
112    return 200;
113}
114
115static void vga_display(caca_display_t *dp)
116{
117    char *screen = (char *)(intptr_t)0x000b8000;
118    uint32_t *attr = dp->cv->attr;
119    uint32_t *chars = dp->cv->chars;
120    int n;
121
122    for(n = dp->cv->height * dp->cv->width; n--; )
123    {
124        *screen++ = _cucul_utf32_to_cp437(*chars++);
125        *screen++ = _cucul_argb32_to_ansi8(*attr++);
126    }
127}
128
129static void vga_handle_resize(caca_display_t *dp)
130{
131    /* We know nothing about our window */
132    dp->resize.w = dp->cv->width;
133    dp->resize.h = dp->cv->height;
134}
135
136static int vga_get_event(caca_display_t *dp, caca_event-t *ev)
137{
138    /* FIXME */
139    ev->type = CACA_EVENT_NONE;
140    return 0;
141}
142
143/*
144 * Driver initialisation
145 */
146
147int vga_install(caca_display_t *dp)
148{
149    dp->drv.driver = CACA_DRIVER_VGA;
150
151    dp->drv.init_graphics = vga_init_graphics;
152    dp->drv.end_graphics = vga_end_graphics;
153    dp->drv.set_display_title = vga_set_display_title;
154    dp->drv.get_display_width = vga_get_display_width;
155    dp->drv.get_display_height = vga_get_display_height;
156    dp->drv.display = vga_display;
157    dp->drv.handle_resize = vga_handle_resize;
158    dp->drv.get_event = vga_get_event;
159    dp->drv.set_mouse = NULL;
160
161    return 0;
162}
163
164#endif /* USE_VGA */
165
Note: See TracBrowser for help on using the repository browser.