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

Last change on this file since 541 was 541, checked in by Sam Hocevar, 15 years ago
  • Got rid of static variables in the event code.
  • Property svn:keywords set to Id
File size: 5.9 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 541 2006-03-07 10:37:59Z 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#include <stdlib.h>
25#include <string.h>
26
27#include "cucul.h"
28#include "cucul_internals.h"
29#include "caca.h"
30#include "caca_internals.h"
31
32static int caca_init_driver(caca_t *kk);
33static void caca_check_terminal(caca_t *kk);
34
35/** \brief Attach a caca graphical context to a cucul backend context.
36 *
37 *  Create a graphical context using device-dependent features (ncurses for
38 *  terminals, an X11 window, a DOS command window...) that attaches to a
39 *  libcucul canvas. Everything that gets drawn in the libcucul canvas can
40 *  then be displayed by the libcaca driver.
41 *
42 *  \param qq The cucul backend context.
43 *  \return The caca graphical context or NULL if an error occurred.
44 */
45caca_t * caca_attach(cucul_t * qq)
46{
47    caca_t *kk = malloc(sizeof(caca_t));
48
49    kk->qq = qq;
50
51    if(caca_init_driver(kk))
52    {
53        free(kk);
54        return NULL;
55    }
56
57    /* Only needed for slang and ncurses */
58    caca_check_terminal(kk);
59
60    if(kk->driver.init_graphics(kk))
61    {
62        free(kk);
63        return NULL;
64    }
65
66    /* Attached! */
67    kk->qq->refcount++;
68
69    /* Graphics stuff */
70    kk->delay = 0;
71    kk->rendertime = 0;
72
73    /* Events stuff */
74#if defined(USE_SLANG) || defined(USE_NCURSES)
75    kk->events.key_timer.last_sec = 0;
76    kk->events.key_timer.last_usec = 0;
77    kk->events.last_key_ticks = 0;
78    kk->events.autorepeat_ticks = 0;
79    kk->events.last_key = 0;
80#endif
81
82    kk->timer.last_sec = 0;
83    kk->timer.last_usec = 0;
84    kk->lastticks = 0;
85
86    kk->mouse_x = kk->qq->width / 2;
87    kk->mouse_y = kk->qq->height / 2;
88
89    kk->resize = 0;
90    kk->resize_event = 0;
91
92    return kk;
93}
94
95/** \brief Detach a caca graphical context from a cucul backend context.
96 *
97 *  Detach a graphical context from its cucul backend and destroy it. The
98 *  libcucul canvas continues to exist and other graphical contexts can be
99 *  attached to it afterwards.
100 *
101 *  \param qq The caca graphical context.
102 */
103void caca_detach(caca_t *kk)
104{
105    kk->driver.end_graphics(kk);
106    kk->qq->refcount--;
107    free(kk);
108}
109
110/*
111 * XXX: The following functions are local.
112 */
113
114static int caca_init_driver(caca_t *kk)
115{
116#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
117    char *var = getenv("CACA_DRIVER");
118
119    /* If the environment variable was set, use it */
120    if(var && *var)
121    {
122#if defined(USE_WIN32)
123        if(!strcasecmp(var, "win32"))
124            win32_init_driver(kk);
125        else
126#endif
127#if defined(USE_CONIO)
128        if(!strcasecmp(var, "conio"))
129            conio_init_driver(kk);
130        else
131#endif
132#if defined(USE_X11)
133        if(!strcasecmp(var, "x11"))
134            x11_init_driver(kk);
135        else
136#endif
137#if defined(USE_GL)
138        if(!strcasecmp(var, "gl"))
139            gl_init_driver(kk);
140        else
141#endif
142#if defined(USE_SLANG)
143        if(!strcasecmp(var, "slang"))
144            slang_init_driver(kk);
145        else
146#endif
147#if defined(USE_NCURSES)
148        if(!strcasecmp(var, "ncurses"))
149            ncurses_init_driver(kk);
150        else
151#endif
152            return -1;
153
154        return 0;
155    }
156#endif
157
158#if defined(USE_WIN32)
159    win32_init_driver(kk);
160    return 0;
161#endif
162#if defined(USE_CONIO)
163    conio_init_driver(kk);
164    return 0;
165#endif
166#if defined(USE_X11)
167#if defined(HAVE_GETENV)
168    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
169#endif
170    {
171        x11_init_driver(kk);
172        return 0;
173    }
174#endif
175#if defined(USE_GL)
176#if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
177    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
178#endif
179    {
180        gl_init_driver(kk);
181        return 0;
182    }
183#endif
184#if defined(USE_SLANG)
185    slang_init_driver(kk);
186    return 0;
187#endif
188#if defined(USE_NCURSES)
189    slang_init_driver(kk);
190    return 0;
191#endif
192
193    return -1;
194}
195
196static void caca_check_terminal(caca_t *kk)
197{
198#if defined(HAVE_GETENV) && defined(HAVE_PUTENV) && \
199     (defined(USE_SLANG) || defined(USE_NCURSES))
200    char *term, *colorterm, *other;
201#endif
202
203#if defined(USE_SLANG)
204    if(kk->driver.driver != CACA_DRIVER_SLANG)
205#endif
206#if defined(USE_NCURSES)
207    if(kk->driver.driver != CACA_DRIVER_NCURSES)
208#endif
209    return;
210
211#if defined(HAVE_GETENV) && defined(HAVE_PUTENV) && \
212     (defined(USE_SLANG) || defined(USE_NCURSES))
213    term = getenv("TERM");
214    colorterm = getenv("COLORTERM");
215
216    if(term && !strcmp(term, "xterm"))
217    {
218        /* If we are using gnome-terminal, it's really a 16 colour terminal */
219        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
220        {
221#if defined(USE_NCURSES)
222            if(kk->driver.driver == CACA_DRIVER_NCURSES)
223            {
224                SCREEN *screen;
225                screen = newterm("xterm-16color", stdout, stdin);
226                if(screen == NULL)
227                    return;
228                endwin();
229            }
230#endif
231            (void)putenv("TERM=xterm-16color");
232            return;
233        }
234
235        /* Ditto if we are using Konsole */
236        other = getenv("KONSOLE_DCOP_SESSION");
237        if(other)
238        {
239#if defined(USE_NCURSES)
240            if(kk->driver.driver == CACA_DRIVER_NCURSES)
241            {
242                SCREEN *screen;
243                screen = newterm("xterm-16color", stdout, stdin);
244                if(screen == NULL)
245                    return;
246                endwin();
247            }
248#endif
249            (void)putenv("TERM=xterm-16color");
250            return;
251        }
252    }
253#endif
254}
255
Note: See TracBrowser for help on using the repository browser.