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

Last change on this file since 540 was 540, checked in by Sam Hocevar, 15 years ago
  • Polished the driver split a bit (still no events, except resize events), properly credited authors and documented a few things.
  • Property svn:keywords set to Id
File size: 5.8 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 540 2006-03-07 09:17:35Z 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->resize = 0;
87    kk->resize_event = 0;
88
89    return kk;
90}
91
92/** \brief Detach a caca graphical context from a cucul backend context.
93 *
94 *  Detach a graphical context from its cucul backend and destroy it. The
95 *  libcucul canvas continues to exist and other graphical contexts can be
96 *  attached to it afterwards.
97 *
98 *  \param qq The caca graphical context.
99 */
100void caca_detach(caca_t *kk)
101{
102    kk->driver.end_graphics(kk);
103    kk->qq->refcount--;
104    free(kk);
105}
106
107/*
108 * XXX: The following functions are local.
109 */
110
111static int caca_init_driver(caca_t *kk)
112{
113#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
114    char *var = getenv("CACA_DRIVER");
115
116    /* If the environment variable was set, use it */
117    if(var && *var)
118    {
119#if defined(USE_WIN32)
120        if(!strcasecmp(var, "win32"))
121            win32_init_driver(kk);
122        else
123#endif
124#if defined(USE_CONIO)
125        if(!strcasecmp(var, "conio"))
126            conio_init_driver(kk);
127        else
128#endif
129#if defined(USE_X11)
130        if(!strcasecmp(var, "x11"))
131            x11_init_driver(kk);
132        else
133#endif
134#if defined(USE_GL)
135        if(!strcasecmp(var, "gl"))
136            gl_init_driver(kk);
137        else
138#endif
139#if defined(USE_SLANG)
140        if(!strcasecmp(var, "slang"))
141            slang_init_driver(kk);
142        else
143#endif
144#if defined(USE_NCURSES)
145        if(!strcasecmp(var, "ncurses"))
146            ncurses_init_driver(kk);
147        else
148#endif
149            return -1;
150
151        return 0;
152    }
153#endif
154
155#if defined(USE_WIN32)
156    win32_init_driver(kk);
157    return 0;
158#endif
159#if defined(USE_CONIO)
160    conio_init_driver(kk);
161    return 0;
162#endif
163#if defined(USE_X11)
164#if defined(HAVE_GETENV)
165    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
166#endif
167    {
168        x11_init_driver(kk);
169        return 0;
170    }
171#endif
172#if defined(USE_GL)
173#if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
174    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
175#endif
176    {
177        gl_init_driver(kk);
178        return 0;
179    }
180#endif
181#if defined(USE_SLANG)
182    slang_init_driver(kk);
183    return 0;
184#endif
185#if defined(USE_NCURSES)
186    slang_init_driver(kk);
187    return 0;
188#endif
189
190    return -1;
191}
192
193static void caca_check_terminal(caca_t *kk)
194{
195#if defined(HAVE_GETENV) && defined(HAVE_PUTENV) && \
196     (defined(USE_SLANG) || defined(USE_NCURSES))
197    char *term, *colorterm, *other;
198#endif
199
200#if defined(USE_SLANG)
201    if(kk->driver.driver != CACA_DRIVER_SLANG)
202#endif
203#if defined(USE_NCURSES)
204    if(kk->driver.driver != CACA_DRIVER_NCURSES)
205#endif
206    return;
207
208#if defined(HAVE_GETENV) && defined(HAVE_PUTENV) && \
209     (defined(USE_SLANG) || defined(USE_NCURSES))
210    term = getenv("TERM");
211    colorterm = getenv("COLORTERM");
212
213    if(term && !strcmp(term, "xterm"))
214    {
215        /* If we are using gnome-terminal, it's really a 16 colour terminal */
216        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
217        {
218#if defined(USE_NCURSES)
219            if(kk->driver.driver == CACA_DRIVER_NCURSES)
220            {
221                SCREEN *screen;
222                screen = newterm("xterm-16color", stdout, stdin);
223                if(screen == NULL)
224                    return;
225                endwin();
226            }
227#endif
228            (void)putenv("TERM=xterm-16color");
229            return;
230        }
231
232        /* Ditto if we are using Konsole */
233        other = getenv("KONSOLE_DCOP_SESSION");
234        if(other)
235        {
236#if defined(USE_NCURSES)
237            if(kk->driver.driver == CACA_DRIVER_NCURSES)
238            {
239                SCREEN *screen;
240                screen = newterm("xterm-16color", stdout, stdin);
241                if(screen == NULL)
242                    return;
243                endwin();
244            }
245#endif
246            (void)putenv("TERM=xterm-16color");
247            return;
248        }
249    }
250#endif
251}
252
Note: See TracBrowser for help on using the repository browser.