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

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