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

Last change on this file since 2135 was 2135, checked in by Sam Hocevar, 13 years ago
  • Implemented caca_get_display_driver_list(), unused yet.
  • Property svn:keywords set to Id
File size: 9.3 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 2135 2007-12-12 07:41:00Z 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/** \brief Get available display drivers
215 *
216 *  Return a list of available display drivers. The list is a NULL-terminated
217 *  array of strings, interleaving a string containing the internal value for
218 *  the display driver, and a string containing the natural language
219 *  description for that driver.
220 *
221 *  This function never fails.
222 *
223 *  \param dp Display object.
224 *  \return An array of strings.
225 */
226char const * const * caca_get_display_driver_list(caca_display_t *dp)
227{
228    static char const * const list[] =
229    {
230        "none", "No antialiasing",
231        "prefilter", "Prefilter antialiasing",
232#if defined(USE_COCOA)
233        "cocoa", "Mac OS X Cocoa",
234#endif
235#if defined(USE_WIN32)
236        "win32", "Windows console",
237#endif
238#if defined(USE_CONIO)
239        "conio", "MS-DOS conio",
240#endif
241#if defined(USE_X11)
242        "x11", "X11 graphical window",
243#endif
244#if defined(USE_GL)
245        "gl", "OpenGL window",
246#endif
247#if !defined(__KERNEL__)
248        "raw", "raw libcaca output",
249#endif
250#if defined(USE_SLANG)
251        "slang", "S-Lang console library",
252#endif
253#if defined(USE_NCURSES)
254        "ncurses", "ncurses console library",
255#endif
256#if defined(USE_VGA)
257        "vga", "direct VGA memory",
258#endif
259        NULL, NULL
260    };
261
262    return list;
263}
264
265/*
266 * XXX: The following functions are local.
267 */
268
269static int caca_can_resize(caca_display_t *dp)
270{
271    return dp->resize.allow;
272}
273
274static int caca_select_driver(caca_display_t *dp)
275{
276#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
277    char *var = getenv("CACA_DRIVER");
278
279    /* If the environment variable was set, use it */
280    if(var && *var)
281    {
282#if defined(USE_COCOA)
283        if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
284#endif
285#if defined(USE_WIN32)
286        if(!strcasecmp(var, "win32")) return win32_install(dp);
287#endif
288#if defined(USE_CONIO)
289        if(!strcasecmp(var, "conio")) return conio_install(dp);
290#endif
291#if defined(USE_X11)
292        if(!strcasecmp(var, "x11")) return x11_install(dp);
293#endif
294#if defined(USE_GL)
295        if(!strcasecmp(var, "gl")) return gl_install(dp);
296#endif
297#if !defined(__KERNEL__)
298        if(!strcasecmp(var, "raw")) return raw_install(dp);
299#endif
300#if defined(USE_SLANG)
301        if(!strcasecmp(var, "slang")) return slang_install(dp);
302#endif
303#if defined(USE_NCURSES)
304        if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
305#endif
306#if defined(USE_VGA)
307        if(!strcasecmp(var, "vga")) return vga_install(dp);
308#endif
309        return -1;
310    }
311#endif
312
313#if defined(USE_COCOA)
314    if(cocoa_install(dp) == 0) return 0;
315#endif
316#if defined(USE_WIN32)
317    if(win32_install(dp) == 0) return 0;
318#endif
319#if defined(USE_CONIO)
320    if(conio_install(dp) == 0) return 0;
321#endif
322#if defined(USE_VGA)
323    if(vga_install(dp) == 0) return 0;
324#endif
325#if defined(USE_X11)
326    if(x11_install(dp) == 0) return 0;
327#endif
328#if defined(USE_GL)
329    if(gl_install(dp) == 0) return 0;
330#endif
331    /* ncurses has a higher priority than slang because it has better colour
332     * support across terminal types, despite being slightly slower. */
333#if defined(USE_NCURSES)
334    if(ncurses_install(dp) == 0) return 0;
335#endif
336#if defined(USE_SLANG)
337    if(slang_install(dp) == 0) return 0;
338#endif
339
340    return -1;
341}
342
343#if defined(USE_PLUGINS)
344static int caca_plugin_install(char const *name, caca_display_t *dp)
345{
346    char buf[512];
347    int (*sym) (caca_display_t *);
348
349    sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, name);
350    dp->plugin = dlopen(buf, RTLD_NOW);
351    if(!dp->plugin)
352    {
353        sprintf(buf, "lib%s_plugin.so", name);
354        dp->plugin = dlopen(buf, RTLD_NOW);
355        if(!dp->plugin)
356            return -1;
357    }
358
359    sprintf(buf, "%s_install", name);
360    sym = dlsym(dp->plugin, buf);
361    if(!sym)
362    {
363        dlclose(dp->plugin);
364        return -1;
365    }
366
367    return sym(dp);
368}
369#endif
370
Note: See TracBrowser for help on using the repository browser.