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

Last change on this file since 2074 was 2074, checked in by Sam Hocevar, 12 years ago
  • Add cucul_get_version() and caca_get_version() and updated C++ bindings accordingly.
  • The C++ test example no longer needs "config.h".
  • Property svn:keywords set to Id
File size: 8.0 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: caca.c 2074 2007-11-26 01:04:32Z sam $
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains the main functions used by \e libcaca applications to
17 *  initialise the library, get the screen properties, set the framerate and
18 *  so on.
19 */
20
21#include "config.h"
22#include "common.h"
23
24#if !defined(__KERNEL__)
25#   include <stdlib.h>
26#   include <string.h>
27#   include <stdio.h>
28#   if defined(USE_PLUGINS)
29#       if defined(HAVE_DLFCN_H)
30#           include <dlfcn.h>
31#       endif
32#   endif
33#endif
34
35#include "cucul.h"
36#include "caca.h"
37#include "caca_internals.h"
38
39#if defined(USE_PLUGINS)
40#   define gl_install(p) caca_plugin_install("gl", p)
41#   define x11_install(p) caca_plugin_install("x11", p)
42#endif
43
44static int caca_can_resize(caca_display_t *);
45static int caca_select_driver(caca_display_t *);
46#if defined(USE_PLUGINS)
47static int caca_plugin_install(char const *, caca_display_t *);
48#endif
49
50/** \brief Attach a caca graphical context to a cucul canvas.
51 *
52 *  Create a graphical context using device-dependent features (ncurses for
53 *  terminals, an X11 window, a DOS command window...) that attaches to a
54 *  libcucul canvas. Everything that gets drawn in the libcucul canvas can
55 *  then be displayed by the libcaca driver.
56 *
57 *  If no cucul canvas is provided, a new one is created. Its handle can be
58 *  retrieved using caca_get_canvas() and it is automatically destroyed when
59 *  caca_free_display() is called.
60 *
61 *  If an error occurs, NULL is returned and \b errno is set accordingly:
62 *  - \c ENOMEM Not enough memory.
63 *  - \c ENODEV Graphical device could not be initialised.
64 *
65 *  \param cv The cucul cavas.
66 *  \return The caca graphical context or NULL if an error occurred.
67 */
68caca_display_t * caca_create_display(cucul_canvas_t *cv)
69{
70    caca_display_t *dp = malloc(sizeof(caca_display_t));
71
72    if(!dp)
73    {
74        seterrno(ENOMEM);
75        return NULL;
76    }
77
78    if((dp->autorelease = (cv == NULL)))
79    {
80        cv = cucul_create_canvas(0, 0);
81    }
82
83    dp->cv = cv;
84
85    if(cucul_manage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp))
86    {
87        if(dp->autorelease)
88            cucul_free_canvas(dp->cv);
89        free(dp);
90        seterrno(EBUSY);
91        return NULL;
92    }
93
94#if defined(USE_PLUGINS)
95    dp->plugin = NULL;
96#endif
97
98    if(caca_select_driver(dp))
99    {
100#if defined(USE_PLUGINS)
101        if(dp->plugin)
102            dlclose(dp->plugin);
103#endif
104        cucul_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
105        if(dp->autorelease)
106            cucul_free_canvas(dp->cv);
107        free(dp);
108        seterrno(ENODEV);
109        return NULL;
110    }
111
112    if(dp->drv.init_graphics(dp))
113    {
114#if defined(USE_PLUGINS)
115        if(dp->plugin)
116            dlclose(dp->plugin);
117#endif
118        cucul_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
119        if(dp->autorelease)
120            cucul_free_canvas(dp->cv);
121        free(dp);
122        seterrno(ENODEV);
123        return NULL;
124    }
125
126    /* Graphics stuff */
127    dp->delay = 0;
128    dp->rendertime = 0;
129
130    /* Events stuff */
131#if defined(USE_SLANG) || defined(USE_NCURSES)
132    dp->events.key_timer.last_sec = 0;
133    dp->events.key_timer.last_usec = 0;
134    dp->events.last_key_ticks = 0;
135    dp->events.autorepeat_ticks = 0;
136    dp->events.last_key_event.type = CACA_EVENT_NONE;
137#endif
138#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
139    dp->events.queue = 0;
140#endif
141
142    dp->timer.last_sec = 0;
143    dp->timer.last_usec = 0;
144    dp->lastticks = 0;
145
146    /* Mouse position */
147    dp->mouse.x = cucul_get_canvas_width(dp->cv) / 2;
148    dp->mouse.y = cucul_get_canvas_height(dp->cv) / 2;
149
150    /* Resize events */
151    dp->resize.resized = 0;
152    dp->resize.allow = 0;
153
154    return dp;
155}
156
157/** \brief Detach a caca graphical context from a cucul backend context.
158 *
159 *  Detach a graphical context from its cucul backend and destroy it. The
160 *  libcucul canvas continues to exist and other graphical contexts can be
161 *  attached to it afterwards.
162 *
163 *  If the cucul canvas was automatically created by caca_create_display(),
164 *  it is automatically destroyed and any handle to it becomes invalid.
165 *
166 *  This function never fails.
167 *
168 *  \param dp The libcaca graphical context.
169 *  \return This function always returns 0.
170 */
171int caca_free_display(caca_display_t *dp)
172{
173    dp->drv.end_graphics(dp);
174#if defined(USE_PLUGINS)
175    if(dp->plugin)
176        dlclose(dp->plugin);
177#endif
178    cucul_unmanage_canvas(dp->cv, (int (*)(void *))caca_can_resize, (void *)dp);
179    if(dp->autorelease)
180        cucul_free_canvas(dp->cv);
181    free(dp);
182
183    return 0;
184}
185
186/** \brief Get the canvas attached to a caca graphical context.
187 *
188 *  Return a handle on the \e cucul_canvas_t object that was either attached
189 *  or created by caca_create_display().
190 *
191 *  This function never fails.
192 *
193 *  \param dp The libcaca graphical context.
194 *  \return The libcucul canvas.
195 */
196cucul_canvas_t * caca_get_canvas(caca_display_t *dp)
197{
198    return dp->cv;
199}
200
201/** \brief Return the \e libcaca version.
202 *
203 *  Return a read-only string with the \e libcaca version information.
204 *
205 *  This function never fails.
206 *
207 *  \return The \e libcaca version information.
208 */
209char const * caca_get_version(void)
210{
211    return VERSION;
212}
213
214/*
215 * XXX: The following functions are local.
216 */
217
218static int caca_can_resize(caca_display_t *dp)
219{
220    return dp->resize.allow;
221}
222
223static int caca_select_driver(caca_display_t *dp)
224{
225#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
226    char *var = getenv("CACA_DRIVER");
227
228    /* If the environment variable was set, use it */
229    if(var && *var)
230    {
231#if defined(USE_COCOA)
232        if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
233#endif
234#if defined(USE_WIN32)
235        if(!strcasecmp(var, "win32")) return win32_install(dp);
236#endif
237#if defined(USE_CONIO)
238        if(!strcasecmp(var, "conio")) return conio_install(dp);
239#endif
240#if defined(USE_X11)
241        if(!strcasecmp(var, "x11")) return x11_install(dp);
242#endif
243#if defined(USE_GL)
244        if(!strcasecmp(var, "gl")) return gl_install(dp);
245#endif
246#if !defined(__KERNEL__)
247        if(!strcasecmp(var, "raw")) return raw_install(dp);
248#endif
249#if defined(USE_SLANG)
250        if(!strcasecmp(var, "slang")) return slang_install(dp);
251#endif
252#if defined(USE_NCURSES)
253        if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
254#endif
255#if defined(USE_VGA)
256        if(!strcasecmp(var, "vga")) return vga_install(dp);
257#endif
258        return -1;
259    }
260#endif
261
262#if defined(USE_COCOA)
263    if(cocoa_install(dp) == 0) return 0;
264#endif
265#if defined(USE_WIN32)
266    if(win32_install(dp) == 0) return 0;
267#endif
268#if defined(USE_CONIO)
269    if(conio_install(dp) == 0) return 0;
270#endif
271#if defined(USE_VGA)
272    if(vga_install(dp) == 0) return 0;
273#endif
274#if defined(USE_X11)
275    if(x11_install(dp) == 0) return 0;
276#endif
277#if defined(USE_GL)
278    if(gl_install(dp) == 0) return 0;
279#endif
280    /* ncurses has a higher priority than slang because it has better colour
281     * support across terminal types, despite being slightly slower. */
282#if defined(USE_NCURSES)
283    if(ncurses_install(dp) == 0) return 0;
284#endif
285#if defined(USE_SLANG)
286    if(slang_install(dp) == 0) return 0;
287#endif
288
289    return -1;
290}
291
292#if defined(USE_PLUGINS)
293static int caca_plugin_install(char const *name, caca_display_t *dp)
294{
295    char buf[512];
296    int (*sym) (caca_display_t *);
297
298    sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, name);
299    dp->plugin = dlopen(buf, RTLD_NOW);
300    if(!dp->plugin)
301    {
302        sprintf(buf, "lib%s_plugin.so", name);
303        dp->plugin = dlopen(buf, RTLD_NOW);
304        if(!dp->plugin)
305            return -1;
306    }
307
308    sprintf(buf, "%s_install", name);
309    sym = dlsym(dp->plugin, buf);
310    if(!sym)
311    {
312        dlclose(dp->plugin);
313        return -1;
314    }
315
316    return sym(dp);
317}
318#endif
319
Note: See TracBrowser for help on using the repository browser.