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

Last change on this file since 2061 was 2061, checked in by Sam Hocevar, 12 years ago
  • Allow caca_create_display()'s argument to be NULL. It will automatically create a canvas when so.
  • Add caca_get_canvas() to retrieve the cucul canvas.
  • Property svn:keywords set to Id
File size: 7.8 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 2061 2007-11-25 17:13:08Z 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/*
202 * XXX: The following functions are local.
203 */
204
205static int caca_can_resize(caca_display_t *dp)
206{
207    return dp->resize.allow;
208}
209
210static int caca_select_driver(caca_display_t *dp)
211{
212#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
213    char *var = getenv("CACA_DRIVER");
214
215    /* If the environment variable was set, use it */
216    if(var && *var)
217    {
218#if defined(USE_COCOA)
219        if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
220#endif
221#if defined(USE_WIN32)
222        if(!strcasecmp(var, "win32")) return win32_install(dp);
223#endif
224#if defined(USE_CONIO)
225        if(!strcasecmp(var, "conio")) return conio_install(dp);
226#endif
227#if defined(USE_X11)
228        if(!strcasecmp(var, "x11")) return x11_install(dp);
229#endif
230#if defined(USE_GL)
231        if(!strcasecmp(var, "gl")) return gl_install(dp);
232#endif
233#if !defined(__KERNEL__)
234        if(!strcasecmp(var, "raw")) return raw_install(dp);
235#endif
236#if defined(USE_SLANG)
237        if(!strcasecmp(var, "slang")) return slang_install(dp);
238#endif
239#if defined(USE_NCURSES)
240        if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
241#endif
242#if defined(USE_VGA)
243        if(!strcasecmp(var, "vga")) return vga_install(dp);
244#endif
245        return -1;
246    }
247#endif
248
249#if defined(USE_COCOA)
250    if(cocoa_install(dp) == 0) return 0;
251#endif
252#if defined(USE_WIN32)
253    if(win32_install(dp) == 0) return 0;
254#endif
255#if defined(USE_CONIO)
256    if(conio_install(dp) == 0) return 0;
257#endif
258#if defined(USE_VGA)
259    if(vga_install(dp) == 0) return 0;
260#endif
261#if defined(USE_X11)
262    if(x11_install(dp) == 0) return 0;
263#endif
264#if defined(USE_GL)
265    if(gl_install(dp) == 0) return 0;
266#endif
267    /* ncurses has a higher priority than slang because it has better colour
268     * support across terminal types, despite being slightly slower. */
269#if defined(USE_NCURSES)
270    if(ncurses_install(dp) == 0) return 0;
271#endif
272#if defined(USE_SLANG)
273    if(slang_install(dp) == 0) return 0;
274#endif
275
276    return -1;
277}
278
279#if defined(USE_PLUGINS)
280static int caca_plugin_install(char const *name, caca_display_t *dp)
281{
282    char buf[512];
283    int (*sym) (caca_display_t *);
284
285    sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, name);
286    dp->plugin = dlopen(buf, RTLD_NOW);
287    if(!dp->plugin)
288    {
289        sprintf(buf, "lib%s_plugin.so", name);
290        dp->plugin = dlopen(buf, RTLD_NOW);
291        if(!dp->plugin)
292            return -1;
293    }
294
295    sprintf(buf, "%s_install", name);
296    sym = dlsym(dp->plugin, buf);
297    if(!sym)
298    {
299        dlclose(dp->plugin);
300        return -1;
301    }
302
303    return sym(dp);
304}
305#endif
306
Note: See TracBrowser for help on using the repository browser.