source: libcaca/trunk/caca/caca.c @ 565

Last change on this file since 565 was 565, checked in by Sam Hocevar, 14 years ago
  • Pure VGA output driver.
  • Property svn:keywords set to Id
File size: 4.6 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 caca.c
13 *  \version \$Id: caca.c 565 2006-03-09 12:41:02Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief Main \e libcaca functions
16 *
17 *  This file contains the main functions used by \e libcaca applications to
18 *  initialise the library, get the screen properties, set the framerate and
19 *  so on.
20 */
21
22#include "config.h"
23
24#if !defined(__KERNEL__)
25#   include <stdlib.h>
26#   include <string.h>
27#endif
28
29#include "cucul.h"
30#include "cucul_internals.h"
31#include "caca.h"
32#include "caca_internals.h"
33
34static int caca_init_driver(caca_t *kk);
35
36/** \brief Attach a caca graphical context to a cucul backend context.
37 *
38 *  Create a graphical context using device-dependent features (ncurses for
39 *  terminals, an X11 window, a DOS command window...) that attaches to a
40 *  libcucul canvas. Everything that gets drawn in the libcucul canvas can
41 *  then be displayed by the libcaca driver.
42 *
43 *  \param qq The cucul backend context.
44 *  \return The caca graphical context or NULL if an error occurred.
45 */
46caca_t * caca_attach(cucul_t * qq)
47{
48    caca_t *kk = malloc(sizeof(caca_t));
49
50    kk->qq = qq;
51
52    if(caca_init_driver(kk))
53    {
54        free(kk);
55        return NULL;
56    }
57
58    if(kk->drv.init_graphics(kk))
59    {
60        free(kk);
61        return NULL;
62    }
63
64    /* Attached! */
65    kk->qq->refcount++;
66
67    /* Graphics stuff */
68    kk->delay = 0;
69    kk->rendertime = 0;
70
71    /* Events stuff */
72#if defined(USE_SLANG) || defined(USE_NCURSES)
73    kk->events.key_timer.last_sec = 0;
74    kk->events.key_timer.last_usec = 0;
75    kk->events.last_key_ticks = 0;
76    kk->events.autorepeat_ticks = 0;
77    kk->events.last_key = 0;
78#endif
79
80    kk->timer.last_sec = 0;
81    kk->timer.last_usec = 0;
82    kk->lastticks = 0;
83
84    /* Mouse position */
85    kk->mouse.x = kk->qq->width / 2;
86    kk->mouse.y = kk->qq->height / 2;
87
88    /* Resize events */
89    kk->resize.resized = 0;
90
91    return kk;
92}
93
94/** \brief Detach a caca graphical context from a cucul backend context.
95 *
96 *  Detach a graphical context from its cucul backend and destroy it. The
97 *  libcucul canvas continues to exist and other graphical contexts can be
98 *  attached to it afterwards.
99 *
100 *  \param qq The caca graphical context.
101 */
102void caca_detach(caca_t *kk)
103{
104    kk->drv.end_graphics(kk);
105    kk->qq->refcount--;
106    free(kk);
107}
108
109/*
110 * XXX: The following functions are local.
111 */
112
113static int caca_init_driver(caca_t *kk)
114{
115#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
116    char *var = getenv("CACA_DRIVER");
117
118    /* If the environment variable was set, use it */
119    if(var && *var)
120    {
121#if defined(USE_WIN32)
122        if(!strcasecmp(var, "win32"))
123            win32_init_driver(kk);
124        else
125#endif
126#if defined(USE_CONIO)
127        if(!strcasecmp(var, "conio"))
128            conio_init_driver(kk);
129        else
130#endif
131#if defined(USE_X11)
132        if(!strcasecmp(var, "x11"))
133            x11_init_driver(kk);
134        else
135#endif
136#if defined(USE_GL)
137        if(!strcasecmp(var, "gl"))
138            gl_init_driver(kk);
139        else
140#endif
141#if defined(USE_SLANG)
142        if(!strcasecmp(var, "slang"))
143            slang_init_driver(kk);
144        else
145#endif
146#if defined(USE_NCURSES)
147        if(!strcasecmp(var, "ncurses"))
148            ncurses_init_driver(kk);
149        else
150#endif
151#if defined(USE_NETWORK)
152        if(!strcasecmp(var, "network"))
153            network_init_driver(kk);
154        else
155#endif
156#if defined(USE_VGA)
157        if(!strcasecmp(var, "vga"))
158            vga_init_driver(kk);
159        else
160#endif
161            return -1;
162
163        return 0;
164    }
165#endif
166
167#if defined(USE_WIN32)
168    win32_init_driver(kk);
169    return 0;
170#endif
171#if defined(USE_CONIO)
172    conio_init_driver(kk);
173    return 0;
174#endif
175#if defined(USE_VGA)
176    vga_init_driver(kk);
177    return 0;
178#endif
179#if defined(USE_X11)
180#if defined(HAVE_GETENV)
181    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
182#endif
183    {
184        x11_init_driver(kk);
185        return 0;
186    }
187#endif
188#if defined(USE_GL)
189#if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
190    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
191#endif
192    {
193        gl_init_driver(kk);
194        return 0;
195    }
196#endif
197#if defined(USE_SLANG)
198    slang_init_driver(kk);
199    return 0;
200#endif
201#if defined(USE_NCURSES)
202    slang_init_driver(kk);
203    return 0;
204#endif
205#if defined(USE_NETWORK)
206    network_init_driver(kk);
207    return 0;
208#endif
209
210    return -1;
211}
212
Note: See TracBrowser for help on using the repository browser.