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

Last change on this file since 1254 was 1254, checked in by Sam Hocevar, 14 years ago
  • Use the LSB of alpha and blue components to encode additional flags such as bold or underline. No higher level support for these yet, but at least everything is consistent.
  • Created cucul_set_attr_ansi() and cucul_set_attr_argb() which are bound to replace cucul_set_color() and cucul_set_truecolor().
  • Property svn:keywords set to Id
File size: 4.0 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 1254 2006-10-28 23:53:46Z 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#include "common.h"
20
21#if defined(USE_CONIO)
22
23#include <dos.h>
24#include <conio.h>
25#if defined(SCREENUPDATE_IN_PC_H)
26#   include <pc.h>
27#endif
28
29#include <stdlib.h>
30
31#include "caca.h"
32#include "caca_internals.h"
33#include "cucul.h"
34#include "cucul_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    _cucul_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth,
61                                   dp->drv.p->ti.screenheight);
62    return 0;
63}
64
65static int conio_end_graphics(caca_display_t *dp)
66{
67    _wscroll = 1;
68    textcolor((enum COLORS)WHITE);
69    textbackground((enum COLORS)BLACK);
70    gotoxy(dp->cv->width, dp->cv->height);
71    cputs("\r\n");
72    _setcursortype(_NORMALCURSOR);
73
74    free(dp->drv.p->screen);
75    free(dp->drv.p);
76
77    return 0;
78}
79
80static int conio_set_display_title(caca_display_t *dp, char const *title)
81{
82    return -1;
83}
84
85static unsigned int conio_get_display_width(caca_display_t *dp)
86{
87    /* Fallback to a 6x10 font */
88    return dp->cv->width * 6;
89}
90
91static unsigned int conio_get_display_height(caca_display_t *dp)
92{
93    /* Fallback to a 6x10 font */
94    return dp->cv->height * 10;
95}
96
97static void conio_display(caca_display_t *dp)
98{
99    char *screen = dp->drv.p->screen;
100    uint32_t *attrs = dp->cv->attrs;
101    uint32_t *chars = dp->cv->chars;
102    unsigned int n;
103
104    for(n = dp->cv->height * dp->cv->width; n--; )
105    {
106        char ch = cucul_utf32_to_cp437(*chars++);
107        if(n && *chars == CUCUL_MAGIC_FULLWIDTH)
108        {
109            *screen++ = '[';
110            *screen++ = _cucul_attr_to_ansi8(*attrs++);
111            ch = ']';
112            chars++;
113            n--;
114        }
115        *screen++ = ch;
116        *screen++ = _cucul_attr_to_ansi8(*attrs++);
117    }
118#   if defined(SCREENUPDATE_IN_PC_H)
119    ScreenUpdate(dp->drv.p->screen);
120#   else
121    /* FIXME */
122#   endif
123}
124
125static void conio_handle_resize(caca_display_t *dp)
126{
127    /* We know nothing about our window */
128    dp->resize.w = dp->cv->width;
129    dp->resize.h = dp->cv->height;
130}
131
132static int conio_get_event(caca_display_t *dp, caca_event_t *ev)
133{
134    unsigned char ch;
135    caca_event_t release;
136
137    if(!_conio_kbhit())
138    {
139        ev->type = CACA_EVENT_NONE;
140        return 0;
141    }
142
143    ch = getch();
144
145    ev->type = CACA_EVENT_KEY_PRESS;
146    ev->data.key.ch = ch;
147    ev->data.key.utf32 = (uint32_t)ch;
148    ev->data.key.utf8[0] = ch;
149    ev->data.key.utf8[1] = '\0';
150
151    release = *ev;
152    release.type = CACA_EVENT_KEY_RELEASE;
153    _push_event(dp, &release);
154
155    return 1;
156}
157
158/*
159 * Driver initialisation
160 */
161
162int conio_install(caca_display_t *dp)
163{
164    dp->drv.driver = CACA_DRIVER_CONIO;
165
166    dp->drv.init_graphics = conio_init_graphics;
167    dp->drv.end_graphics = conio_end_graphics;
168    dp->drv.set_display_title = conio_set_display_title;
169    dp->drv.get_display_width = conio_get_display_width;
170    dp->drv.get_display_height = conio_get_display_height;
171    dp->drv.display = conio_display;
172    dp->drv.handle_resize = conio_handle_resize;
173    dp->drv.get_event = conio_get_event;
174    dp->drv.set_mouse = NULL;
175
176    return 0;
177}
178
179#endif /* USE_CONIO */
180
Note: See TracBrowser for help on using the repository browser.