File: conio.c

package info (click to toggle)
libcaca 0.99.beta19-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,200 kB
  • ctags: 3,824
  • sloc: ansic: 22,346; python: 2,316; cs: 1,213; cpp: 1,114; java: 916; objc: 836; makefile: 606; sh: 457; ruby: 193; asm: 65
file content (183 lines) | stat: -rw-r--r-- 4,402 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 *  libcaca       Colour ASCII-Art library
 *  Copyright (c) 2002-2012 Sam Hocevar <sam@hocevar.net>
 *                All Rights Reserved
 *
 *  This library is free software. It comes without any warranty, to
 *  the extent permitted by applicable law. You can redistribute it
 *  and/or modify it under the terms of the Do What the Fuck You Want
 *  to Public License, Version 2, as published by Sam Hocevar. See
 *  http://www.wtfpl.net/ for more details.
 */

/*
 *  This file contains the libcaca DOS/conio.h input and output driver
 */

#include "config.h"

#if defined(USE_CONIO)

#include <dos.h>
#include <conio.h>
#if defined(SCREENUPDATE_IN_PC_H)
#   include <pc.h>
#endif

#include <stdlib.h>

#include "caca.h"
#include "caca_internals.h"

struct driver_private
{
    struct text_info ti;
    char *screen;
};

static int conio_init_graphics(caca_display_t *dp)
{
    dp->drv.p = malloc(sizeof(struct driver_private));

    _wscroll = 0;
    _setcursortype(_NOCURSOR);
    clrscr();

    gettextinfo(&dp->drv.p->ti);
    dp->drv.p->screen = malloc(2 * dp->drv.p->ti.screenwidth
                                 * dp->drv.p->ti.screenheight * sizeof(char));
    if(dp->drv.p->screen == NULL)
        return -1;
#   if defined(SCREENUPDATE_IN_PC_H)
    ScreenRetrieve(dp->drv.p->screen);
#   else
    /* FIXME */
#   endif
    dp->resize.allow = 1;
    caca_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth,
                                  dp->drv.p->ti.screenheight);
    dp->resize.allow = 0;

    return 0;
}

static int conio_end_graphics(caca_display_t *dp)
{
    _wscroll = 1;
    textcolor((enum COLORS)WHITE);
    textbackground((enum COLORS)BLACK);
    gotoxy(caca_get_canvas_width(dp->cv), caca_get_canvas_height(dp->cv));
    cputs("\r\n");
    _setcursortype(_NORMALCURSOR);

    free(dp->drv.p->screen);
    free(dp->drv.p);

    return 0;
}

static int conio_set_display_title(caca_display_t *dp, char const *title)
{
    return -1;
}

static int conio_get_display_width(caca_display_t const *dp)
{
    /* Fallback to a 6x10 font */
    return caca_get_canvas_width(dp->cv) * 6;
}

static int conio_get_display_height(caca_display_t const *dp)
{
    /* Fallback to a 6x10 font */
    return caca_get_canvas_height(dp->cv) * 10;
}

static void conio_display(caca_display_t *dp)
{
    char *screen = dp->drv.p->screen;
    uint32_t const *chars = caca_get_canvas_chars(dp->cv);
    uint32_t const *attrs = caca_get_canvas_attrs(dp->cv);
    int width = caca_get_canvas_width(dp->cv);
    int height = caca_get_canvas_height(dp->cv);
    int n;

    for(n = height * width; n--; )
    {
        char ch = caca_utf32_to_cp437(*chars++);
        if(n && *chars == CACA_MAGIC_FULLWIDTH)
        {
            *screen++ = '[';
            *screen++ = caca_attr_to_ansi(*attrs++);
            ch = ']';
            chars++;
            n--;
        }
        *screen++ = ch;
        *screen++ = caca_attr_to_ansi(*attrs++);
    }
#   if defined(SCREENUPDATE_IN_PC_H)
    ScreenUpdate(dp->drv.p->screen);
#   else
    /* FIXME */
#   endif
}

static void conio_handle_resize(caca_display_t *dp)
{
    /* We know nothing about our window */
    dp->resize.w = caca_get_canvas_width(dp->cv);
    dp->resize.h = caca_get_canvas_height(dp->cv);
}

static int conio_get_event(caca_display_t *dp, caca_privevent_t *ev)
{
    uint8_t ch;
    caca_privevent_t release;

    if(!_conio_kbhit())
    {
        ev->type = CACA_EVENT_NONE;
        return 0;
    }

    ch = getch();

    ev->type = CACA_EVENT_KEY_PRESS;
    ev->data.key.ch = ch;
    ev->data.key.utf32 = ch;
    ev->data.key.utf8[0] = ch;
    ev->data.key.utf8[1] = '\0';

    release = *ev;
    release.type = CACA_EVENT_KEY_RELEASE;
    _push_event(dp, &release);

    return 1;
}

/*
 * Driver initialisation
 */

int conio_install(caca_display_t *dp)
{
    dp->drv.id = CACA_DRIVER_CONIO;
    dp->drv.driver = "conio";

    dp->drv.init_graphics = conio_init_graphics;
    dp->drv.end_graphics = conio_end_graphics;
    dp->drv.set_display_title = conio_set_display_title;
    dp->drv.get_display_width = conio_get_display_width;
    dp->drv.get_display_height = conio_get_display_height;
    dp->drv.display = conio_display;
    dp->drv.handle_resize = conio_handle_resize;
    dp->drv.get_event = conio_get_event;
    dp->drv.set_mouse = NULL;
    dp->drv.set_cursor = NULL;

    return 0;
}

#endif /* USE_CONIO */