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

Last change on this file since 819 was 819, checked in by Sam Hocevar, 14 years ago
  • More function mass-renaming: caca_attach -> caca_create_display, etc.
  • Property svn:keywords set to Id
File size: 3.8 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 819 2006-04-19 10:10:58Z sam $
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 *  This file contains the libcaca DOS/conio.h input and output driver
16 */
17
18#include "config.h"
19
20#if defined(USE_CONIO)
21
22#include <dos.h>
23#include <conio.h>
24#if defined(SCREENUPDATE_IN_PC_H)
25#   include <pc.h>
26#endif
27
28#include <stdlib.h>
29
30#include "caca.h"
31#include "caca_internals.h"
32#include "cucul.h"
33#include "cucul_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    _cucul_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth,
60                                   dp->drv.p->ti.screenheight);
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(dp->cv->width, dp->cv->height);
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 0;
82}
83
84static unsigned int conio_get_display_width(caca_display_t *dp)
85{
86    /* Fallback to a 6x10 font */
87    return dp->cv->width * 6;
88}
89
90static unsigned int conio_get_display_height(caca_display_t *dp)
91{
92    /* Fallback to a 6x10 font */
93    return dp->cv->height * 10;
94}
95
96static void conio_display(caca_display_t *dp)
97{
98    char *screen = dp->drv.p->screen;
99    uint32_t *attr = dp->cv->attr;
100    uint32_t *chars = dp->cv->chars;
101    int n;
102
103    for(n = dp->cv->height * dp->cv->width; n--; )
104    {
105        *screen++ = _cucul_utf32_to_cp437(*chars++);
106        *screen++ = _cucul_argb32_to_ansi8(*attr++);
107    }
108#   if defined(SCREENUPDATE_IN_PC_H)
109    ScreenUpdate(dp->drv.p->screen);
110#   else
111    /* FIXME */
112#   endif
113}
114
115static void conio_handle_resize(caca_display_t *dp)
116{
117    /* We know nothing about our window */
118    dp->resize.w = dp->cv->width;
119    dp->resize.h = dp->cv->height;
120}
121
122static int conio_get_event(caca_display_t *dp, caca_event_t *ev)
123{
124    unsigned char ch;
125    caca_event_t release;
126
127    if(!_conio_kbhit())
128    {
129        ev->type = CACA_EVENT_NONE;
130        return 0;
131    }
132
133    ch = getch();
134
135    ev->type = CACA_EVENT_KEY_PRESS;
136    ev->data.key.ch = ch;
137    ev->data.key.ucs4 = (uint32_t)ch;
138    ev->data.key.utf8[0] = ch;
139    ev->data.key.utf8[1] = '\0';
140
141    release = *ev;
142    release.type = CACA_EVENT_KEY_RELEASE;
143    _push_event(dp, &release);
144
145    return 1;
146}
147
148/*
149 * Driver initialisation
150 */
151
152int conio_install(caca_display_t *dp)
153{
154    dp->drv.driver = CACA_DRIVER_CONIO;
155
156    dp->drv.init_graphics = conio_init_graphics;
157    dp->drv.end_graphics = conio_end_graphics;
158    dp->drv.set_display_title = conio_set_display_title;
159    dp->drv.get_display_width = conio_get_display_width;
160    dp->drv.get_display_height = conio_get_display_height;
161    dp->drv.display = conio_display;
162    dp->drv.handle_resize = conio_handle_resize;
163    dp->drv.get_event = conio_get_event;
164    dp->drv.set_mouse = NULL;
165
166    return 0;
167}
168
169#endif /* USE_CONIO */
170
Note: See TracBrowser for help on using the repository browser.