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

Last change on this file since 2055 was 2055, checked in by Sam Hocevar, 13 years ago
  • Add cucul_manage_canvas() and cucul_unmanage_canvas(). Display drivers no longer need to access the cv->refcount private member and cucul_set_canvas_size() is no longer needed.
  • Property svn:keywords set to Id
File size: 4.2 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_conio.c 2055 2007-11-25 14:11:58Z 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 DOS/conio.h input and output driver
17 */
18
19#include "config.h"
20#include "common.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
37struct driver_private
38{
39    struct text_info ti;
40    char *screen;
41};
42
43static int conio_init_graphics(caca_display_t *dp)
44{
45    dp->drv.p = malloc(sizeof(struct driver_private));
46
47    _wscroll = 0;
48    _setcursortype(_NOCURSOR);
49    clrscr();
50
51    gettextinfo(&dp->drv.p->ti);
52    dp->drv.p->screen = malloc(2 * dp->drv.p->ti.screenwidth
53                                 * dp->drv.p->ti.screenheight * sizeof(char));
54    if(dp->drv.p->screen == NULL)
55        return -1;
56#   if defined(SCREENUPDATE_IN_PC_H)
57    ScreenRetrieve(dp->drv.p->screen);
58#   else
59    /* FIXME */
60#   endif
61    dp->resize.allow = 1;
62    cucul_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth,
63                                  dp->drv.p->ti.screenheight);
64    dp->resize.allow = 0;
65
66    return 0;
67}
68
69static int conio_end_graphics(caca_display_t *dp)
70{
71    _wscroll = 1;
72    textcolor((enum COLORS)WHITE);
73    textbackground((enum COLORS)BLACK);
74    gotoxy(dp->cv->width, dp->cv->height);
75    cputs("\r\n");
76    _setcursortype(_NORMALCURSOR);
77
78    free(dp->drv.p->screen);
79    free(dp->drv.p);
80
81    return 0;
82}
83
84static int conio_set_display_title(caca_display_t *dp, char const *title)
85{
86    return -1;
87}
88
89static unsigned int conio_get_display_width(caca_display_t const *dp)
90{
91    /* Fallback to a 6x10 font */
92    return dp->cv->width * 6;
93}
94
95static unsigned int conio_get_display_height(caca_display_t const *dp)
96{
97    /* Fallback to a 6x10 font */
98    return dp->cv->height * 10;
99}
100
101static void conio_display(caca_display_t *dp)
102{
103    char *screen = dp->drv.p->screen;
104    uint32_t *attrs = dp->cv->attrs;
105    uint32_t *chars = dp->cv->chars;
106    unsigned int n;
107
108    for(n = dp->cv->height * dp->cv->width; n--; )
109    {
110        char ch = cucul_utf32_to_cp437(*chars++);
111        if(n && *chars == CUCUL_MAGIC_FULLWIDTH)
112        {
113            *screen++ = '[';
114            *screen++ = cucul_attr_to_ansi(*attrs++);
115            ch = ']';
116            chars++;
117            n--;
118        }
119        *screen++ = ch;
120        *screen++ = cucul_attr_to_ansi(*attrs++);
121    }
122#   if defined(SCREENUPDATE_IN_PC_H)
123    ScreenUpdate(dp->drv.p->screen);
124#   else
125    /* FIXME */
126#   endif
127}
128
129static void conio_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 conio_get_event(caca_display_t *dp, caca_privevent_t *ev)
137{
138    unsigned char ch;
139    caca_privevent_t release;
140
141    if(!_conio_kbhit())
142    {
143        ev->type = CACA_EVENT_NONE;
144        return 0;
145    }
146
147    ch = getch();
148
149    ev->type = CACA_EVENT_KEY_PRESS;
150    ev->data.key.ch = ch;
151    ev->data.key.utf32 = (uint32_t)ch;
152    ev->data.key.utf8[0] = ch;
153    ev->data.key.utf8[1] = '\0';
154
155    release = *ev;
156    release.type = CACA_EVENT_KEY_RELEASE;
157    _push_event(dp, &release);
158
159    return 1;
160}
161
162/*
163 * Driver initialisation
164 */
165
166int conio_install(caca_display_t *dp)
167{
168    dp->drv.driver = CACA_DRIVER_CONIO;
169
170    dp->drv.init_graphics = conio_init_graphics;
171    dp->drv.end_graphics = conio_end_graphics;
172    dp->drv.set_display_title = conio_set_display_title;
173    dp->drv.get_display_width = conio_get_display_width;
174    dp->drv.get_display_height = conio_get_display_height;
175    dp->drv.display = conio_display;
176    dp->drv.handle_resize = conio_handle_resize;
177    dp->drv.get_event = conio_get_event;
178    dp->drv.set_mouse = NULL;
179    dp->drv.set_cursor = NULL;
180
181    return 0;
182}
183
184#endif /* USE_CONIO */
185
Note: See TracBrowser for help on using the repository browser.