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

Last change on this file since 625 was 625, checked in by Sam Hocevar, 14 years ago
  • Partial Unicode support for the DOS/conio.h driver. Glyphs that have equivalents in CP437 0x7f - 0xff are translated.
  • Property svn:keywords set to Id
File size: 5.1 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 625 2006-03-15 20:30:39Z 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 lookup[] =
145    {
146        /* 0x7f */
147        0x2302,
148        /* 0x80 - 0x8f */
149        0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
150        0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
151        /* 0x90 - 0x9f */
152        0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
153        0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0x20a7, 0x192,
154        /* 0xa0 - 0xaf */
155        0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
156        0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
157        /* 0xb0 - 0xbf */
158        0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
159        0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
160        /* 0xc0 - 0xcf */
161        0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
162        0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
163        /* 0xd0 - 0xdf */
164        0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
165        0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
166        /* 0xe0 - 0xef */
167        0x3b1, 0xdf, 0x393, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
168        0x3a6, 0x398, 0x3a9, 0x3b4, 0x221e, 0x3c6, 0x3b5, 0x2229,
169        /* 0xf0 - 0xff */
170        0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
171        0xb0, 0x2219, 0xb7, 0x221a, 0x207f, 0xb2, 0x25a0, 0xa0
172    };
173
174    unsigned int i;
175 
176    if(c < 0x00000020)
177        return '?';
178
179    if(c < 0x00000080)
180        return c;
181
182    for(i = 0; i < sizeof(lookup) / sizeof(*lookup); i++)
183    {
184        if(lookup[i] == c)
185            return 0x7f + i;
186    }
187
188    return '?';
189}
190
191/*
192 * Driver initialisation
193 */
194
195void conio_init_driver(caca_t *kk)
196{
197    kk->drv.driver = CACA_DRIVER_CONIO;
198
199    kk->drv.init_graphics = conio_init_graphics;
200    kk->drv.end_graphics = conio_end_graphics;
201    kk->drv.set_window_title = conio_set_window_title;
202    kk->drv.get_window_width = conio_get_window_width;
203    kk->drv.get_window_height = conio_get_window_height;
204    kk->drv.display = conio_display;
205    kk->drv.handle_resize = conio_handle_resize;
206    kk->drv.get_event = conio_get_event;
207}
208
209#endif /* USE_CONIO */
210
Note: See TracBrowser for help on using the repository browser.