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

Last change on this file since 2299 was 2299, checked in by Sam Hocevar, 12 years ago
  • Remove #include <stdint.h> etc. from "common.h". Instead, make sure that <cucul.h> will provide the C99 types, even if libcaca has been installed.
  • Rename what's left of "common.h" to "stubs.h".
  • Remove all references to erroneous <inttypes.h> from source files.
  • 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 2299 2008-04-19 12:42:50Z 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 "cucul.h"
32#include "caca.h"
33#include "caca_internals.h"
34
35struct driver_private
36{
37    struct text_info ti;
38    char *screen;
39};
40
41static int conio_init_graphics(caca_display_t *dp)
42{
43    dp->drv.p = malloc(sizeof(struct driver_private));
44
45    _wscroll = 0;
46    _setcursortype(_NOCURSOR);
47    clrscr();
48
49    gettextinfo(&dp->drv.p->ti);
50    dp->drv.p->screen = malloc(2 * dp->drv.p->ti.screenwidth
51                                 * dp->drv.p->ti.screenheight * sizeof(char));
52    if(dp->drv.p->screen == NULL)
53        return -1;
54#   if defined(SCREENUPDATE_IN_PC_H)
55    ScreenRetrieve(dp->drv.p->screen);
56#   else
57    /* FIXME */
58#   endif
59    dp->resize.allow = 1;
60    cucul_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth,
61                                  dp->drv.p->ti.screenheight);
62    dp->resize.allow = 0;
63
64    return 0;
65}
66
67static int conio_end_graphics(caca_display_t *dp)
68{
69    _wscroll = 1;
70    textcolor((enum COLORS)WHITE);
71    textbackground((enum COLORS)BLACK);
72    gotoxy(cucul_get_canvas_width(dp->cv), cucul_get_canvas_height(dp->cv));
73    cputs("\r\n");
74    _setcursortype(_NORMALCURSOR);
75
76    free(dp->drv.p->screen);
77    free(dp->drv.p);
78
79    return 0;
80}
81
82static int conio_set_display_title(caca_display_t *dp, char const *title)
83{
84    return -1;
85}
86
87static unsigned int conio_get_display_width(caca_display_t const *dp)
88{
89    /* Fallback to a 6x10 font */
90    return cucul_get_canvas_width(dp->cv) * 6;
91}
92
93static unsigned int conio_get_display_height(caca_display_t const *dp)
94{
95    /* Fallback to a 6x10 font */
96    return cucul_get_canvas_height(dp->cv) * 10;
97}
98
99static void conio_display(caca_display_t *dp)
100{
101    char *screen = dp->drv.p->screen;
102    uint32_t const *chars = (uint32_t const *)cucul_get_canvas_chars(dp->cv);
103    uint32_t const *attrs = (uint32_t const *)cucul_get_canvas_attrs(dp->cv);
104    unsigned int width = cucul_get_canvas_width(dp->cv);
105    unsigned int height = cucul_get_canvas_height(dp->cv);
106    unsigned int n;
107
108    for(n = height * 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 = cucul_get_canvas_width(dp->cv);
133    dp->resize.h = cucul_get_canvas_height(dp->cv);
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.id = CACA_DRIVER_CONIO;
169    dp->drv.driver = "conio";
170
171    dp->drv.init_graphics = conio_init_graphics;
172    dp->drv.end_graphics = conio_end_graphics;
173    dp->drv.set_display_title = conio_set_display_title;
174    dp->drv.get_display_width = conio_get_display_width;
175    dp->drv.get_display_height = conio_get_display_height;
176    dp->drv.display = conio_display;
177    dp->drv.handle_resize = conio_handle_resize;
178    dp->drv.get_event = conio_get_event;
179    dp->drv.set_mouse = NULL;
180    dp->drv.set_cursor = NULL;
181
182    return 0;
183}
184
185#endif /* USE_CONIO */
186
Note: See TracBrowser for help on using the repository browser.