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

Last change on this file since 1452 was 1452, checked in by Sam Hocevar, 14 years ago
  • Add a no warranty clause to the code.
  • Property svn:keywords set to Id
File size: 6.2 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 1452 2006-12-11 15:48:46Z sam $
7 *
8 *  This library is free software. It commes 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 "cucul_internals.h"
37#include "caca.h"
38#include "caca_internals.h"
39
40#if defined(USE_PLUGINS)
41#   define gl_install(p) caca_plugin_install("gl", p)
42#   define x11_install(p) caca_plugin_install("x11", p)
43#endif
44
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 an error occurs, NULL is returned and \b errno is set accordingly:
58 *  - \c ENOMEM Not enough memory.
59 *  - \c ENODEV Graphical device could not be initialised.
60 *
61 *  \param cv The cucul cavas.
62 *  \return The caca graphical context or NULL if an error occurred.
63 */
64caca_display_t * caca_create_display(cucul_canvas_t * cv)
65{
66    caca_display_t *dp = malloc(sizeof(caca_display_t));
67
68    if(!dp)
69    {
70        seterrno(ENOMEM);
71        return NULL;
72    }
73
74    dp->cv = cv;
75#if defined(USE_PLUGINS)
76    dp->plugin = NULL;
77#endif
78
79    if(caca_select_driver(dp))
80    {
81#if defined(USE_PLUGINS)
82        if(dp->plugin)
83            dlclose(dp->plugin);
84#endif
85        free(dp);
86        seterrno(ENODEV);
87        return NULL;
88    }
89
90    if(dp->drv.init_graphics(dp))
91    {
92#if defined(USE_PLUGINS)
93        if(dp->plugin)
94            dlclose(dp->plugin);
95#endif
96        free(dp);
97        seterrno(ENODEV);
98        return NULL;
99    }
100
101    /* Attached! */
102    dp->cv->refcount++;
103
104    /* Graphics stuff */
105    dp->delay = 0;
106    dp->rendertime = 0;
107
108    /* Events stuff */
109#if defined(USE_SLANG) || defined(USE_NCURSES)
110    dp->events.key_timer.last_sec = 0;
111    dp->events.key_timer.last_usec = 0;
112    dp->events.last_key_ticks = 0;
113    dp->events.autorepeat_ticks = 0;
114    dp->events.last_key_event.type = CACA_EVENT_NONE;
115#endif
116#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
117    dp->events.queue = 0;
118#endif
119
120    dp->timer.last_sec = 0;
121    dp->timer.last_usec = 0;
122    dp->lastticks = 0;
123
124    /* Mouse position */
125    dp->mouse.x = dp->cv->width / 2;
126    dp->mouse.y = dp->cv->height / 2;
127
128    /* Resize events */
129    dp->resize.resized = 0;
130
131    return dp;
132}
133
134/** \brief Detach a caca graphical context from a cucul backend context.
135 *
136 *  Detach a graphical context from its cucul backend and destroy it. The
137 *  libcucul canvas continues to exist and other graphical contexts can be
138 *  attached to it afterwards.
139 *
140 *  This function never fails.
141 *
142 *  \param dp The libcaca graphical context.
143 *  \return This function always returns 0.
144 */
145int caca_free_display(caca_display_t *dp)
146{
147    dp->drv.end_graphics(dp);
148#if defined(USE_PLUGINS)
149    if(dp->plugin)
150        dlclose(dp->plugin);
151#endif
152    dp->cv->refcount--;
153    free(dp);
154
155    return 0;
156}
157
158/*
159 * XXX: The following functions are local.
160 */
161
162static int caca_select_driver(caca_display_t *dp)
163{
164#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
165    char *var = getenv("CACA_DRIVER");
166
167    /* If the environment variable was set, use it */
168    if(var && *var)
169    {
170#if defined(USE_COCOA)
171        if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
172#endif
173#if defined(USE_WIN32)
174        if(!strcasecmp(var, "win32")) return win32_install(dp);
175#endif
176#if defined(USE_CONIO)
177        if(!strcasecmp(var, "conio")) return conio_install(dp);
178#endif
179#if defined(USE_X11)
180        if(!strcasecmp(var, "x11")) return x11_install(dp);
181#endif
182#if defined(USE_GL)
183        if(!strcasecmp(var, "gl")) return gl_install(dp);
184#endif
185#if !defined(__KERNEL__)
186        if(!strcasecmp(var, "raw")) return raw_install(dp);
187#endif
188#if defined(USE_SLANG)
189        if(!strcasecmp(var, "slang")) return slang_install(dp);
190#endif
191#if defined(USE_NCURSES)
192        if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
193#endif
194#if defined(USE_VGA)
195        if(!strcasecmp(var, "vga")) return vga_install(dp);
196#endif
197        return -1;
198    }
199#endif
200
201#if defined(USE_COCOA)
202    if(cocoa_install(dp) == 0) return 0;
203#endif
204#if defined(USE_WIN32)
205    if(win32_install(dp) == 0) return 0;
206#endif
207#if defined(USE_CONIO)
208    if(conio_install(dp) == 0) return 0;
209#endif
210#if defined(USE_VGA)
211    if(vga_install(dp) == 0) return 0;
212#endif
213#if defined(USE_X11)
214    if(x11_install(dp) == 0) return 0;
215#endif
216#if defined(USE_GL)
217    if(gl_install(dp) == 0) return 0;
218#endif
219    /* ncurses has a higher priority than slang because it has better colour
220     * support across terminal types, despite being slightly slower. */
221#if defined(USE_NCURSES)
222    if(ncurses_install(dp) == 0) return 0;
223#endif
224#if defined(USE_SLANG)
225    if(slang_install(dp) == 0) return 0;
226#endif
227
228    return -1;
229}
230
231#if defined(USE_PLUGINS)
232static int caca_plugin_install(char const *name, caca_display_t *dp)
233{
234    char buf[512];
235    int (*sym) (caca_display_t *);
236
237    sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, name);
238    dp->plugin = dlopen(buf, RTLD_NOW);
239    if(!dp->plugin)
240    {
241        sprintf(buf, "lib%s_plugin.so", name);
242        dp->plugin = dlopen(buf, RTLD_NOW);
243        if(!dp->plugin)
244            return -1;
245    }
246
247    sprintf(buf, "%s_install", name);
248    sym = dlsym(dp->plugin, buf);
249    if(!sym)
250    {
251        dlclose(dp->plugin);
252        return -1;
253    }
254
255    return sym(dp);
256}
257#endif
258
Note: See TracBrowser for help on using the repository browser.