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

Last change on this file since 567 was 567, checked in by Sam Hocevar, 14 years ago
  • Cosmetic change.
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1/*
2 *  libcaca       ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
10 */
11
12/** \file driver_conio.c
13 *  \version \$Id: driver_conio.c 567 2006-03-09 12:44:49Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief DOS/conio.h driver
16 *
17 *  This file contains the libcaca DOS/conio.h input and output driver
18 */
19
20#include "config.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 "caca.h"
33#include "caca_internals.h"
34#include "cucul.h"
35#include "cucul_internals.h"
36
37struct driver_private
38{
39    struct text_info ti;
40    char *screen;
41};
42
43static int conio_init_graphics(caca_t *kk)
44{
45    kk->drv.p = malloc(sizeof(struct driver_private));
46
47    _wscroll = 0;
48    _setcursortype(_NOCURSOR);
49    clrscr();
50
51    gettextinfo(&kk->drv.p->ti);
52    kk->drv.p->screen = malloc(2 * kk->drv.p->ti.screenwidth
53                                 * kk->drv.p->ti.screenheight * sizeof(char));
54    if(kk->drv.p->screen == NULL)
55        return -1;
56#   if defined(SCREENUPDATE_IN_PC_H)
57    ScreenRetrieve(kk->drv.p->screen);
58#   else
59    /* FIXME */
60#   endif
61    cucul_set_size(kk->qq, kk->drv.p->ti.screenwidth,
62                           kk->drv.p->ti.screenheight);
63    return 0;
64}
65
66static int conio_end_graphics(caca_t *kk)
67{
68    _wscroll = 1;
69    textcolor((enum COLORS)WHITE);
70    textbackground((enum COLORS)BLACK);
71    gotoxy(kk->qq->width, kk->qq->height);
72    cputs("\r\n");
73    _setcursortype(_NORMALCURSOR);
74
75    free(kk->drv.p->screen);
76    free(kk->drv.p);
77
78    return 0;
79}
80
81static int conio_set_window_title(caca_t *kk, char const *title)
82{
83    return 0;
84}
85
86static unsigned int conio_get_window_width(caca_t *kk)
87{
88    /* Fallback to a 6x10 font */
89    return kk->qq->width * 6;
90}
91
92static unsigned int conio_get_window_height(caca_t *kk)
93{
94    /* Fallback to a 6x10 font */
95    return kk->qq->height * 10;
96}
97
98static void conio_display(caca_t *kk)
99{
100    char *screen = kk->drv.p->screen;
101    uint8_t *attr = kk->qq->attr;
102    uint32_t *chars = kk->qq->chars;
103    int n;
104
105    for(n = kk->qq->height * kk->qq->width; n--; )
106    {
107        uint32_t c = *chars++;
108
109        if(c > 0x00000020 && c < 0x00000080)
110            *screen++ = (char)c;
111        else
112            *screen++ = ' ';
113
114        *screen++ = *attr++;
115    }
116#   if defined(SCREENUPDATE_IN_PC_H)
117    ScreenUpdate(kk->drv.p->screen);
118#   else
119    /* FIXME */
120#   endif
121}
122
123static void conio_handle_resize(caca_t *kk)
124{
125    /* We know nothing about our window */
126    kk->resize.w = kk->qq->width;
127    kk->resize.h = kk->qq->height;
128}
129
130static unsigned int conio_get_event(caca_t *kk)
131{
132    unsigned int event;
133
134    if(!_conio_kbhit())
135        return CACA_EVENT_NONE;
136
137    event = getch();
138    _push_event(kk, CACA_EVENT_KEY_RELEASE | event);
139    return CACA_EVENT_KEY_PRESS | event;
140}
141
142/*
143 * Driver initialisation
144 */
145
146void conio_init_driver(caca_t *kk)
147{
148    kk->drv.driver = CACA_DRIVER_CONIO;
149
150    kk->drv.init_graphics = conio_init_graphics;
151    kk->drv.end_graphics = conio_end_graphics;
152    kk->drv.set_window_title = conio_set_window_title;
153    kk->drv.get_window_width = conio_get_window_width;
154    kk->drv.get_window_height = conio_get_window_height;
155    kk->drv.display = conio_display;
156    kk->drv.handle_resize = conio_handle_resize;
157    kk->drv.get_event = conio_get_event;
158}
159
160#endif /* USE_CONIO */
161
Note: See TracBrowser for help on using the repository browser.