source: libcaca/trunk/caca/driver_conio.c @ 631

Last change on this file since 631 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.2 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_conio.c
13 *  \version \$Id: driver_conio.c 631 2006-03-16 12:58:55Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief DOS/conio.h driver
16 *
17 *  This file contains the libcaca DOS/conio.h input and output driver
18 */
19
20#include "config.h"
21
22#if defined(USE_CONIO)
23
24#include <dos.h>
25#include <conio.h>
26#if defined(SCREENUPDATE_IN_PC_H)
27#   include <pc.h>
28#endif
29
30#include <stdlib.h>
31
32#include "caca.h"
33#include "caca_internals.h"
34#include "cucul.h"
35#include "cucul_internals.h"
36
37static uint8_t conio_utf32_to_cp437(uint32_t);
38
39struct driver_private
40{
41    struct text_info ti;
42    char *screen;
43};
44
45static int conio_init_graphics(caca_t *kk)
46{
47    kk->drv.p = malloc(sizeof(struct driver_private));
48
49    _wscroll = 0;
50    _setcursortype(_NOCURSOR);
51    clrscr();
52
53    gettextinfo(&kk->drv.p->ti);
54    kk->drv.p->screen = malloc(2 * kk->drv.p->ti.screenwidth
55                                 * kk->drv.p->ti.screenheight * sizeof(char));
56    if(kk->drv.p->screen == NULL)
57        return -1;
58#   if defined(SCREENUPDATE_IN_PC_H)
59    ScreenRetrieve(kk->drv.p->screen);
60#   else
61    /* FIXME */
62#   endif
63    _cucul_set_size(kk->qq, kk->drv.p->ti.screenwidth,
64                            kk->drv.p->ti.screenheight);
65    return 0;
66}
67
68static int conio_end_graphics(caca_t *kk)
69{
70    _wscroll = 1;
71    textcolor((enum COLORS)WHITE);
72    textbackground((enum COLORS)BLACK);
73    gotoxy(kk->qq->width, kk->qq->height);
74    cputs("\r\n");
75    _setcursortype(_NORMALCURSOR);
76
77    free(kk->drv.p->screen);
78    free(kk->drv.p);
79
80    return 0;
81}
82
83static int conio_set_window_title(caca_t *kk, char const *title)
84{
85    return 0;
86}
87
88static unsigned int conio_get_window_width(caca_t *kk)
89{
90    /* Fallback to a 6x10 font */
91    return kk->qq->width * 6;
92}
93
94static unsigned int conio_get_window_height(caca_t *kk)
95{
96    /* Fallback to a 6x10 font */
97    return kk->qq->height * 10;
98}
99
100static void conio_display(caca_t *kk)
101{
102    char *screen = kk->drv.p->screen;
103    uint8_t *attr = kk->qq->attr;
104    uint32_t *chars = kk->qq->chars;
105    int n;
106
107    for(n = kk->qq->height * kk->qq->width; n--; )
108    {
109        *screen++ = conio_utf32_to_cp437(*chars++);
110        *screen++ = *attr++;
111    }
112#   if defined(SCREENUPDATE_IN_PC_H)
113    ScreenUpdate(kk->drv.p->screen);
114#   else
115    /* FIXME */
116#   endif
117}
118
119static void conio_handle_resize(caca_t *kk)
120{
121    /* We know nothing about our window */
122    kk->resize.w = kk->qq->width;
123    kk->resize.h = kk->qq->height;
124}
125
126static unsigned int conio_get_event(caca_t *kk)
127{
128    unsigned int event;
129
130    if(!_conio_kbhit())
131        return CACA_EVENT_NONE;
132
133    event = getch();
134    _push_event(kk, CACA_EVENT_KEY_RELEASE | event);
135    return CACA_EVENT_KEY_PRESS | event;
136}
137
138/*
139 * XXX: following functions are local
140 */
141
142static uint8_t conio_utf32_to_cp437(uint32_t c)
143{
144    static uint32_t const lookup1[] =
145    {
146        /* 0x01 - 0x0f: ☺ ☻ ♥ ♦ ♣ ♠ • ◘ ○ ◙ ♂ ♀ ♪ ♫ ☼ */
147                0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
148        0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
149        /* 0x10 - 0x1f: ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼ */
150        0x25ba, 0x25c4, 0x2195, 0x203c, 0xb6, 0xa7, 0x25ac, 0x21a8,
151        0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc
152    };
153
154    static uint32_t const lookup2[] =
155    {
156        /* 0x7f: ⌂ */
157        0x2302,
158        /* 0x80 - 0x8f: Ç ü é â ä à å ç ê ë è ï î ì Ä Å */
159        0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
160        0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
161        /* 0x90 - 0x9f: É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ */
162        0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
163        0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0x20a7, 0x192,
164        /* 0xa0 - 0xaf: á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « » */
165        0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
166        0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
167        /* 0xb0 - 0xbf: ░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐ */
168        0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
169        0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
170        /* 0xc0 - 0xcf: └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧ */
171        0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
172        0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
173        /* 0xd0 - 0xdf: ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐ ▀ */
174        0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
175        0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
176        /* 0xe0 - 0xef: α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ */
177        0x3b1, 0xdf, 0x393, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
178        0x3a6, 0x398, 0x3a9, 0x3b4, 0x221e, 0x3c6, 0x3b5, 0x2229,
179        /* 0xf0 - 0xff: ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■ <nbsp> */
180        0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
181        0xb0, 0x2219, 0xb7, 0x221a, 0x207f, 0xb2, 0x25a0, 0xa0
182    };
183
184    unsigned int i;
185 
186    if(c < 0x00000020)
187        return '?';
188
189    if(c < 0x00000080)
190        return c;
191
192    for(i = 0; i < sizeof(lookup1) / sizeof(*lookup1); i++)
193        if(lookup1[i] == c)
194            return 0x01 + i;
195
196    for(i = 0; i < sizeof(lookup2) / sizeof(*lookup2); i++)
197        if(lookup2[i] == c)
198            return 0x7f + i;
199
200    return '?';
201}
202
203/*
204 * Driver initialisation
205 */
206
207void conio_init_driver(caca_t *kk)
208{
209    kk->drv.driver = CACA_DRIVER_CONIO;
210
211    kk->drv.init_graphics = conio_init_graphics;
212    kk->drv.end_graphics = conio_end_graphics;
213    kk->drv.set_window_title = conio_set_window_title;
214    kk->drv.get_window_width = conio_get_window_width;
215    kk->drv.get_window_height = conio_get_window_height;
216    kk->drv.display = conio_display;
217    kk->drv.handle_resize = conio_handle_resize;
218    kk->drv.get_event = conio_get_event;
219}
220
221#endif /* USE_CONIO */
222
Note: See TracBrowser for help on using the repository browser.