source: libcaca/trunk/caca/driver/conio.c @ 4369

Last change on this file since 4369 was 4369, checked in by Sam Hocevar, 12 years ago

Add the copyright unit test and update copyright information everywhere.

  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2010 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  This library is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13/*
14 *  This file contains the libcaca DOS/conio.h input and output driver
15 */
16
17#include "config.h"
18
19#if defined(USE_CONIO)
20
21#include <dos.h>
22#include <conio.h>
23#if defined(SCREENUPDATE_IN_PC_H)
24#   include <pc.h>
25#endif
26
27#include <stdlib.h>
28
29#include "caca.h"
30#include "caca_internals.h"
31
32struct driver_private
33{
34    struct text_info ti;
35    char *screen;
36};
37
38static int conio_init_graphics(caca_display_t *dp)
39{
40    dp->drv.p = malloc(sizeof(struct driver_private));
41
42    _wscroll = 0;
43    _setcursortype(_NOCURSOR);
44    clrscr();
45
46    gettextinfo(&dp->drv.p->ti);
47    dp->drv.p->screen = malloc(2 * dp->drv.p->ti.screenwidth
48                                 * dp->drv.p->ti.screenheight * sizeof(char));
49    if(dp->drv.p->screen == NULL)
50        return -1;
51#   if defined(SCREENUPDATE_IN_PC_H)
52    ScreenRetrieve(dp->drv.p->screen);
53#   else
54    /* FIXME */
55#   endif
56    dp->resize.allow = 1;
57    caca_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth,
58                                  dp->drv.p->ti.screenheight);
59    dp->resize.allow = 0;
60
61    return 0;
62}
63
64static int conio_end_graphics(caca_display_t *dp)
65{
66    _wscroll = 1;
67    textcolor((enum COLORS)WHITE);
68    textbackground((enum COLORS)BLACK);
69    gotoxy(caca_get_canvas_width(dp->cv), caca_get_canvas_height(dp->cv));
70    cputs("\r\n");
71    _setcursortype(_NORMALCURSOR);
72
73    free(dp->drv.p->screen);
74    free(dp->drv.p);
75
76    return 0;
77}
78
79static int conio_set_display_title(caca_display_t *dp, char const *title)
80{
81    return -1;
82}
83
84static int conio_get_display_width(caca_display_t const *dp)
85{
86    /* Fallback to a 6x10 font */
87    return caca_get_canvas_width(dp->cv) * 6;
88}
89
90static int conio_get_display_height(caca_display_t const *dp)
91{
92    /* Fallback to a 6x10 font */
93    return caca_get_canvas_height(dp->cv) * 10;
94}
95
96static void conio_display(caca_display_t *dp)
97{
98    char *screen = dp->drv.p->screen;
99    uint32_t const *chars = caca_get_canvas_chars(dp->cv);
100    uint32_t const *attrs = caca_get_canvas_attrs(dp->cv);
101    int width = caca_get_canvas_width(dp->cv);
102    int height = caca_get_canvas_height(dp->cv);
103    int n;
104
105    for(n = height * width; n--; )
106    {
107        char ch = caca_utf32_to_cp437(*chars++);
108        if(n && *chars == CACA_MAGIC_FULLWIDTH)
109        {
110            *screen++ = '[';
111            *screen++ = caca_attr_to_ansi(*attrs++);
112            ch = ']';
113            chars++;
114            n--;
115        }
116        *screen++ = ch;
117        *screen++ = caca_attr_to_ansi(*attrs++);
118    }
119#   if defined(SCREENUPDATE_IN_PC_H)
120    ScreenUpdate(dp->drv.p->screen);
121#   else
122    /* FIXME */
123#   endif
124}
125
126static void conio_handle_resize(caca_display_t *dp)
127{
128    /* We know nothing about our window */
129    dp->resize.w = caca_get_canvas_width(dp->cv);
130    dp->resize.h = caca_get_canvas_height(dp->cv);
131}
132
133static int conio_get_event(caca_display_t *dp, caca_privevent_t *ev)
134{
135    uint8_t ch;
136    caca_privevent_t release;
137
138    if(!_conio_kbhit())
139    {
140        ev->type = CACA_EVENT_NONE;
141        return 0;
142    }
143
144    ch = getch();
145
146    ev->type = CACA_EVENT_KEY_PRESS;
147    ev->data.key.ch = ch;
148    ev->data.key.utf32 = ch;
149    ev->data.key.utf8[0] = ch;
150    ev->data.key.utf8[1] = '\0';
151
152    release = *ev;
153    release.type = CACA_EVENT_KEY_RELEASE;
154    _push_event(dp, &release);
155
156    return 1;
157}
158
159/*
160 * Driver initialisation
161 */
162
163int conio_install(caca_display_t *dp)
164{
165    dp->drv.id = CACA_DRIVER_CONIO;
166    dp->drv.driver = "conio";
167
168    dp->drv.init_graphics = conio_init_graphics;
169    dp->drv.end_graphics = conio_end_graphics;
170    dp->drv.set_display_title = conio_set_display_title;
171    dp->drv.get_display_width = conio_get_display_width;
172    dp->drv.get_display_height = conio_get_display_height;
173    dp->drv.display = conio_display;
174    dp->drv.handle_resize = conio_handle_resize;
175    dp->drv.get_event = conio_get_event;
176    dp->drv.set_mouse = NULL;
177    dp->drv.set_cursor = NULL;
178
179    return 0;
180}
181
182#endif /* USE_CONIO */
183
Note: See TracBrowser for help on using the repository browser.