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

Last change on this file since 2821 was 2821, checked in by Sam Hocevar, 11 years ago

Starting refactoring to get rid of libcucul. The initial reason for the
split is rendered moot by the plugin system: when enabled, binaries do
not link directly with libX11 or libGL. I hope this is a step towards
more consisteny and clarity.

  • Property svn:keywords set to Id
File size: 4.4 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 2821 2008-09-27 13:12:46Z 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
21#if defined(USE_CONIO)
22
23#include <dos.h>
24#include <conio.h>
25#if defined(SCREENUPDATE_IN_PC_H)
26#   include <pc.h>
27#endif
28
29#include <stdlib.h>
30
31#include "caca.h"
32#include "caca_internals.h"
33
34struct driver_private
35{
36    struct text_info ti;
37    char *screen;
38};
39
40static int conio_init_graphics(caca_display_t *dp)
41{
42    dp->drv.p = malloc(sizeof(struct driver_private));
43
44    _wscroll = 0;
45    _setcursortype(_NOCURSOR);
46    clrscr();
47
48    gettextinfo(&dp->drv.p->ti);
49    dp->drv.p->screen = malloc(2 * dp->drv.p->ti.screenwidth
50                                 * dp->drv.p->ti.screenheight * sizeof(char));
51    if(dp->drv.p->screen == NULL)
52        return -1;
53#   if defined(SCREENUPDATE_IN_PC_H)
54    ScreenRetrieve(dp->drv.p->screen);
55#   else
56    /* FIXME */
57#   endif
58    dp->resize.allow = 1;
59    caca_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth,
60                                  dp->drv.p->ti.screenheight);
61    dp->resize.allow = 0;
62
63    return 0;
64}
65
66static int conio_end_graphics(caca_display_t *dp)
67{
68    _wscroll = 1;
69    textcolor((enum COLORS)WHITE);
70    textbackground((enum COLORS)BLACK);
71    gotoxy(caca_get_canvas_width(dp->cv), caca_get_canvas_height(dp->cv));
72    cputs("\r\n");
73    _setcursortype(_NORMALCURSOR);
74
75    free(dp->drv.p->screen);
76    free(dp->drv.p);
77
78    return 0;
79}
80
81static int conio_set_display_title(caca_display_t *dp, char const *title)
82{
83    return -1;
84}
85
86static int conio_get_display_width(caca_display_t const *dp)
87{
88    /* Fallback to a 6x10 font */
89    return caca_get_canvas_width(dp->cv) * 6;
90}
91
92static int conio_get_display_height(caca_display_t const *dp)
93{
94    /* Fallback to a 6x10 font */
95    return caca_get_canvas_height(dp->cv) * 10;
96}
97
98static void conio_display(caca_display_t *dp)
99{
100    char *screen = dp->drv.p->screen;
101    uint32_t const *chars = (uint32_t const *)caca_get_canvas_chars(dp->cv);
102    uint32_t const *attrs = (uint32_t const *)caca_get_canvas_attrs(dp->cv);
103    int width = caca_get_canvas_width(dp->cv);
104    int height = caca_get_canvas_height(dp->cv);
105    int n;
106
107    for(n = height * width; n--; )
108    {
109        char ch = caca_utf32_to_cp437(*chars++);
110        if(n && *chars == CACA_MAGIC_FULLWIDTH)
111        {
112            *screen++ = '[';
113            *screen++ = caca_attr_to_ansi(*attrs++);
114            ch = ']';
115            chars++;
116            n--;
117        }
118        *screen++ = ch;
119        *screen++ = caca_attr_to_ansi(*attrs++);
120    }
121#   if defined(SCREENUPDATE_IN_PC_H)
122    ScreenUpdate(dp->drv.p->screen);
123#   else
124    /* FIXME */
125#   endif
126}
127
128static void conio_handle_resize(caca_display_t *dp)
129{
130    /* We know nothing about our window */
131    dp->resize.w = caca_get_canvas_width(dp->cv);
132    dp->resize.h = caca_get_canvas_height(dp->cv);
133}
134
135static int conio_get_event(caca_display_t *dp, caca_privevent_t *ev)
136{
137    uint8_t ch;
138    caca_privevent_t release;
139
140    if(!_conio_kbhit())
141    {
142        ev->type = CACA_EVENT_NONE;
143        return 0;
144    }
145
146    ch = getch();
147
148    ev->type = CACA_EVENT_KEY_PRESS;
149    ev->data.key.ch = ch;
150    ev->data.key.utf32 = ch;
151    ev->data.key.utf8[0] = ch;
152    ev->data.key.utf8[1] = '\0';
153
154    release = *ev;
155    release.type = CACA_EVENT_KEY_RELEASE;
156    _push_event(dp, &release);
157
158    return 1;
159}
160
161/*
162 * Driver initialisation
163 */
164
165int conio_install(caca_display_t *dp)
166{
167    dp->drv.id = CACA_DRIVER_CONIO;
168    dp->drv.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.