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

Last change on this file since 684 was 684, checked in by Sam Hocevar, 15 years ago
  • Allow the driver initialisation to fail, for instance when $DISPLAY = "".
  • Property svn:keywords set to Id
File size: 3.7 KB
RevLine 
[35]1/*
[672]2 *  libcaca       Colour ASCII-Art library
[527]3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
[268]4 *                All Rights Reserved
[35]5 *
[268]6 *  This library is free software; you can redistribute it and/or
[522]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.
[35]10 */
[17]11
[540]12/** \file driver_conio.c
[268]13 *  \version \$Id: driver_conio.c 684 2006-03-24 09:48:20Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
[540]15 *  \brief DOS/conio.h driver
[205]16 *
[540]17 *  This file contains the libcaca DOS/conio.h input and output driver
[205]18 */
19
[63]20#include "config.h"
21
[539]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
[550]30#include <stdlib.h>
31
[185]32#include "caca.h"
33#include "caca_internals.h"
[524]34#include "cucul.h"
35#include "cucul_internals.h"
[17]36
[550]37struct driver_private
38{
39    struct text_info ti;
40    char *screen;
41};
42
[540]43static int conio_init_graphics(caca_t *kk)
[227]44{
[550]45    kk->drv.p = malloc(sizeof(struct driver_private));
46
[539]47    _wscroll = 0;
48    _setcursortype(_NOCURSOR);
49    clrscr();
[348]50
[550]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)
[539]55        return -1;
[227]56#   if defined(SCREENUPDATE_IN_PC_H)
[550]57    ScreenRetrieve(kk->drv.p->screen);
[227]58#   else
[539]59    /* FIXME */
[227]60#   endif
[582]61    _cucul_set_size(kk->qq, kk->drv.p->ti.screenwidth,
62                            kk->drv.p->ti.screenheight);
[227]63    return 0;
64}
65
[540]66static int conio_end_graphics(caca_t *kk)
[251]67{
[539]68    _wscroll = 1;
69    textcolor((enum COLORS)WHITE);
70    textbackground((enum COLORS)BLACK);
[543]71    gotoxy(kk->qq->width, kk->qq->height);
[539]72    cputs("\r\n");
73    _setcursortype(_NORMALCURSOR);
[300]74
[550]75    free(kk->drv.p->screen);
76    free(kk->drv.p);
[539]77
[251]78    return 0;
79}
80
[540]81static int conio_set_window_title(caca_t *kk, char const *title)
[343]82{
83    return 0;
84}
85
[540]86static unsigned int conio_get_window_width(caca_t *kk)
[352]87{
88    /* Fallback to a 6x10 font */
[524]89    return kk->qq->width * 6;
[352]90}
91
[540]92static unsigned int conio_get_window_height(caca_t *kk)
[352]93{
94    /* Fallback to a 6x10 font */
[524]95    return kk->qq->height * 10;
[352]96}
97
[540]98static void conio_display(caca_t *kk)
[227]99{
[550]100    char *screen = kk->drv.p->screen;
[539]101    uint8_t *attr = kk->qq->attr;
102    uint32_t *chars = kk->qq->chars;
[567]103    int n;
104
[539]105    for(n = kk->qq->height * kk->qq->width; n--; )
[265]106    {
[643]107        *screen++ = _cucul_utf32_to_cp437(*chars++);
[539]108        *screen++ = *attr++;
[265]109    }
[227]110#   if defined(SCREENUPDATE_IN_PC_H)
[550]111    ScreenUpdate(kk->drv.p->screen);
[227]112#   else
[539]113    /* FIXME */
[227]114#   endif
[539]115}
[261]116
[553]117static void conio_handle_resize(caca_t *kk)
[539]118{
[553]119    /* We know nothing about our window */
120    kk->resize.w = kk->qq->width;
121    kk->resize.h = kk->qq->height;
[227]122}
123
[681]124static int conio_get_event(caca_t *kk, struct caca_event *ev)
[548]125{
[681]126    unsigned char ch;
127    struct caca_event release;
[548]128
129    if(!_conio_kbhit())
[681]130    {
131        ev->type = CACA_EVENT_NONE;
132        return 0;
133    }
[548]134
[681]135    ch = getch();
136
137    ev->type = CACA_EVENT_KEY_PRESS;
138    ev->data.key.c = ch;
139    ev->data.key.ucs4 = (uint32_t)ch;
140    ev->data.key.utf8[0] = ch;
141    ev->data.key.utf8[1] = '\0';
142
143    release = *ev;
144    release.type = CACA_EVENT_KEY_RELEASE;
145    _push_event(kk, &release);
146
147    return 1;
[548]148}
149
[347]150/*
[539]151 * Driver initialisation
[347]152 */
[524]153
[684]154int conio_install(caca_t *kk)
[347]155{
[550]156    kk->drv.driver = CACA_DRIVER_CONIO;
[347]157
[550]158    kk->drv.init_graphics = conio_init_graphics;
159    kk->drv.end_graphics = conio_end_graphics;
160    kk->drv.set_window_title = conio_set_window_title;
161    kk->drv.get_window_width = conio_get_window_width;
162    kk->drv.get_window_height = conio_get_window_height;
163    kk->drv.display = conio_display;
164    kk->drv.handle_resize = conio_handle_resize;
165    kk->drv.get_event = conio_get_event;
[684]166
167    return 0;
[347]168}
169
[539]170#endif /* USE_CONIO */
[281]171
Note: See TracBrowser for help on using the repository browser.