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

Last change on this file since 539 was 539, checked in by Sam Hocevar, 14 years ago
  • Split caca/graphics.c into driver-specific files. Resize is currently broken, and event handling is not yet in the driver-specific files, but I will of course fix that later.
  • Property svn:keywords set to Id
File size: 5.1 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 539 2006-03-06 23:01: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#if defined(HAVE_INTTYPES_H) || defined(_DOXYGEN_SKIP_ME)
25#   include <inttypes.h>
26#else
27typedef unsigned int uint32_t;
28typedef unsigned char uint8_t;
29#endif
30
31#include <stdlib.h>
32#include <string.h>
33
34#include "cucul.h"
35#include "cucul_internals.h"
36#include "caca.h"
37#include "caca_internals.h"
38
39static int caca_init_driver(caca_t *kk);
40static void caca_init_terminal(caca_t *kk);
41
42caca_t * caca_attach(cucul_t * qq)
43{
44    int ret;
45    caca_t *kk = malloc(sizeof(caca_t));
46
47    ret = caca_init_driver(kk);
48
49    if(ret)
50    {
51        free(kk);
52        return NULL;
53    }
54
55    qq->refcount++;
56    kk->qq = qq;
57
58    /* Only for slang and ncurses */
59    caca_init_terminal(kk);
60
61    if(_caca_init_graphics(kk))
62    {
63        qq->refcount--;
64        free(kk);
65        return NULL;
66    }
67
68    /* Initialise events stuff */
69    kk->events.key_timer.last_sec = 0;
70    kk->events.key_timer.last_usec = 0;
71    kk->events.last_key_ticks = 0;
72    kk->events.autorepeat_ticks = 0;
73    kk->events.last_key = 0;
74
75    kk->timer.last_sec = 0;
76    kk->timer.last_usec = 0;
77    kk->lastticks = 0;
78
79    kk->resize = 0;
80    kk->resize_event = 0;
81
82    return kk;
83}
84
85void caca_detach(caca_t *kk)
86{
87    kk->driver.end_graphics(kk);
88    kk->qq->refcount--;
89    free(kk);
90}
91
92/*
93 * XXX: The following functions are local.
94 */
95
96static int caca_init_driver(caca_t *kk)
97{
98#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
99    char *var = getenv("CACA_DRIVER");
100
101    /* If the environment variable was set, use it */
102    if(var && *var)
103    {
104#if defined(USE_WIN32)
105        if(!strcasecmp(var, "win32"))
106            win32_init_driver(kk);
107        else
108#endif
109#if defined(USE_CONIO)
110        if(!strcasecmp(var, "conio"))
111            conio_init_driver(kk);
112        else
113#endif
114#if defined(USE_X11)
115        if(!strcasecmp(var, "x11"))
116            x11_init_driver(kk);
117        else
118#endif
119#if defined(USE_GL)
120        if(!strcasecmp(var, "gl"))
121            gl_init_driver(kk);
122        else
123#endif
124#if defined(USE_SLANG)
125        if(!strcasecmp(var, "slang"))
126            slang_init_driver(kk);
127        else
128#endif
129#if defined(USE_NCURSES)
130        if(!strcasecmp(var, "ncurses"))
131            ncurses_init_driver(kk);
132        else
133#endif
134            return -1;
135
136        return 0;
137    }
138#endif
139
140#if defined(USE_WIN32)
141    win32_init_driver(kk);
142    return 0;
143#endif
144#if defined(USE_CONIO)
145    conio_init_driver(kk);
146    return 0;
147#endif
148#if defined(USE_X11)
149#if defined(HAVE_GETENV)
150    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
151#endif
152    {
153        x11_init_driver(kk);
154        return 0;
155    }
156#endif
157#if defined(USE_GL)
158#if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
159    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
160#endif
161    {
162        gl_init_driver(kk);
163        return 0;
164    }
165#endif
166#if defined(USE_SLANG)
167    slang_init_driver(kk);
168    return 0;
169#endif
170#if defined(USE_NCURSES)
171    slang_init_driver(kk);
172    return 0;
173#endif
174
175    return -1;
176}
177
178static void caca_init_terminal(caca_t *kk)
179{
180#if defined(HAVE_GETENV) && defined(HAVE_PUTENV) && \
181     (defined(USE_SLANG) || defined(USE_NCURSES))
182    char *term, *colorterm, *other;
183#endif
184
185#if defined(USE_SLANG)
186    if(kk->driver.driver != CACA_DRIVER_SLANG)
187#endif
188#if defined(USE_NCURSES)
189    if(kk->driver.driver != CACA_DRIVER_NCURSES)
190#endif
191    return;
192
193#if defined(HAVE_GETENV) && defined(HAVE_PUTENV) && \
194     (defined(USE_SLANG) || defined(USE_NCURSES))
195    term = getenv("TERM");
196    colorterm = getenv("COLORTERM");
197
198    if(term && !strcmp(term, "xterm"))
199    {
200        /* If we are using gnome-terminal, it's really a 16 colour terminal */
201        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
202        {
203#if defined(USE_NCURSES)
204            if(kk->driver.driver == CACA_DRIVER_NCURSES)
205            {
206                SCREEN *screen;
207                screen = newterm("xterm-16color", stdout, stdin);
208                if(screen == NULL)
209                    return;
210                endwin();
211            }
212#endif
213            (void)putenv("TERM=xterm-16color");
214            return;
215        }
216
217        /* Ditto if we are using Konsole */
218        other = getenv("KONSOLE_DCOP_SESSION");
219        if(other)
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#endif
236}
237
Note: See TracBrowser for help on using the repository browser.