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

Last change on this file since 593 was 593, checked in by Sam Hocevar, 15 years ago
  • Fixed an uninitialised variable in the event queue handling.
  • Property svn:keywords set to Id
File size: 4.7 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 593 2006-03-13 17:49:04Z 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#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
80    kk->events.queue = 0;
81#endif
82
83    kk->timer.last_sec = 0;
84    kk->timer.last_usec = 0;
85    kk->lastticks = 0;
86
87    /* Mouse position */
88    kk->mouse.x = kk->qq->width / 2;
89    kk->mouse.y = kk->qq->height / 2;
90
91    /* Resize events */
92    kk->resize.resized = 0;
93
94    return kk;
95}
96
97/** \brief Detach a caca graphical context from a cucul backend context.
98 *
99 *  Detach a graphical context from its cucul backend and destroy it. The
100 *  libcucul canvas continues to exist and other graphical contexts can be
101 *  attached to it afterwards.
102 *
103 *  \param qq The caca graphical context.
104 */
105void caca_detach(caca_t *kk)
106{
107    kk->drv.end_graphics(kk);
108    kk->qq->refcount--;
109    free(kk);
110}
111
112/*
113 * XXX: The following functions are local.
114 */
115
116static int caca_init_driver(caca_t *kk)
117{
118#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
119    char *var = getenv("CACA_DRIVER");
120   
121    /* If the environment variable was set, use it */
122    if(var && *var)
123        {
124#if defined(USE_WIN32)
125        if(!strcasecmp(var, "win32"))
126            win32_init_driver(kk);
127        else
128#endif
129#if defined(USE_CONIO)
130        if(!strcasecmp(var, "conio"))
131            conio_init_driver(kk);
132        else
133#endif
134#if defined(USE_X11)
135        if(!strcasecmp(var, "x11"))
136            x11_init_driver(kk);
137        else
138#endif
139#if defined(USE_GL)
140        if(!strcasecmp(var, "gl"))
141            gl_init_driver(kk);
142        else
143#endif
144#if defined(USE_SLANG)
145        if(!strcasecmp(var, "slang"))
146            slang_init_driver(kk);
147        else
148#endif
149#if defined(USE_NCURSES)
150        if(!strcasecmp(var, "ncurses"))
151            ncurses_init_driver(kk);
152        else
153#endif
154#if defined(USE_NETWORK)
155        if(!strcasecmp(var, "network"))
156            network_init_driver(kk);
157        else
158#endif
159#if defined(USE_VGA)
160        if(!strcasecmp(var, "vga"))
161            vga_init_driver(kk);
162        else
163#endif
164            return -1;
165
166        return 0;
167    }
168#endif
169
170#if defined(USE_WIN32)
171    win32_init_driver(kk);
172    return 0;
173#endif
174#if defined(USE_CONIO)
175    conio_init_driver(kk);
176    return 0;
177#endif
178#if defined(USE_VGA)
179    vga_init_driver(kk);
180    return 0;
181#endif
182#if defined(USE_X11)
183#if defined(HAVE_GETENV)
184    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
185#endif
186    {
187        x11_init_driver(kk);
188        return 0;
189    }
190#endif
191#if defined(USE_GL)
192#if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
193    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
194#endif
195    {
196        gl_init_driver(kk);
197        return 0;
198    }
199#endif
200#if defined(USE_SLANG)
201    slang_init_driver(kk);
202    return 0;
203#endif
204#if defined(USE_NCURSES)
205    slang_init_driver(kk);
206    return 0;
207#endif
208#if defined(USE_NETWORK)
209    network_init_driver(kk);
210    return 0;
211#endif
212
213    return -1;
214}
215
Note: See TracBrowser for help on using the repository browser.