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

Last change on this file since 2056 was 2056, checked in by Sam Hocevar, 12 years ago
  • Introduce cucul_get_canvas_chars() and cucul_get_canvas_attrs() to avoid direct cv->chars and cv->attrs access by display drivers.
  • Property svn:keywords set to Id
File size: 6.7 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 2056 2007-11-25 14:12:01Z 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 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    if(cucul_manage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp))
75    {
76        free(dp);
77        seterrno(EBUSY);
78        return NULL;
79    }
80
81    dp->cv = cv;
82#if defined(USE_PLUGINS)
83    dp->plugin = NULL;
84#endif
85
86    if(caca_select_driver(dp))
87    {
88#if defined(USE_PLUGINS)
89        if(dp->plugin)
90            dlclose(dp->plugin);
91#endif
92        cucul_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
93        free(dp);
94        seterrno(ENODEV);
95        return NULL;
96    }
97
98    if(dp->drv.init_graphics(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        free(dp);
106        seterrno(ENODEV);
107        return NULL;
108    }
109
110    /* Graphics stuff */
111    dp->delay = 0;
112    dp->rendertime = 0;
113
114    /* Events stuff */
115#if defined(USE_SLANG) || defined(USE_NCURSES)
116    dp->events.key_timer.last_sec = 0;
117    dp->events.key_timer.last_usec = 0;
118    dp->events.last_key_ticks = 0;
119    dp->events.autorepeat_ticks = 0;
120    dp->events.last_key_event.type = CACA_EVENT_NONE;
121#endif
122#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
123    dp->events.queue = 0;
124#endif
125
126    dp->timer.last_sec = 0;
127    dp->timer.last_usec = 0;
128    dp->lastticks = 0;
129
130    /* Mouse position */
131    dp->mouse.x = cucul_get_canvas_width(dp->cv) / 2;
132    dp->mouse.y = cucul_get_canvas_height(dp->cv) / 2;
133
134    /* Resize events */
135    dp->resize.resized = 0;
136    dp->resize.allow = 0;
137
138    return dp;
139}
140
141/** \brief Detach a caca graphical context from a cucul backend context.
142 *
143 *  Detach a graphical context from its cucul backend and destroy it. The
144 *  libcucul canvas continues to exist and other graphical contexts can be
145 *  attached to it afterwards.
146 *
147 *  This function never fails.
148 *
149 *  \param dp The libcaca graphical context.
150 *  \return This function always returns 0.
151 */
152int caca_free_display(caca_display_t *dp)
153{
154    dp->drv.end_graphics(dp);
155#if defined(USE_PLUGINS)
156    if(dp->plugin)
157        dlclose(dp->plugin);
158#endif
159    cucul_unmanage_canvas(dp->cv, (int (*)(void *))caca_can_resize, (void *)dp);
160    free(dp);
161
162    return 0;
163}
164
165/*
166 * XXX: The following functions are local.
167 */
168
169static int caca_can_resize(caca_display_t *dp)
170{
171    return dp->resize.allow;
172}
173
174static int caca_select_driver(caca_display_t *dp)
175{
176#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
177    char *var = getenv("CACA_DRIVER");
178
179    /* If the environment variable was set, use it */
180    if(var && *var)
181    {
182#if defined(USE_COCOA)
183        if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
184#endif
185#if defined(USE_WIN32)
186        if(!strcasecmp(var, "win32")) return win32_install(dp);
187#endif
188#if defined(USE_CONIO)
189        if(!strcasecmp(var, "conio")) return conio_install(dp);
190#endif
191#if defined(USE_X11)
192        if(!strcasecmp(var, "x11")) return x11_install(dp);
193#endif
194#if defined(USE_GL)
195        if(!strcasecmp(var, "gl")) return gl_install(dp);
196#endif
197#if !defined(__KERNEL__)
198        if(!strcasecmp(var, "raw")) return raw_install(dp);
199#endif
200#if defined(USE_SLANG)
201        if(!strcasecmp(var, "slang")) return slang_install(dp);
202#endif
203#if defined(USE_NCURSES)
204        if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
205#endif
206#if defined(USE_VGA)
207        if(!strcasecmp(var, "vga")) return vga_install(dp);
208#endif
209        return -1;
210    }
211#endif
212
213#if defined(USE_COCOA)
214    if(cocoa_install(dp) == 0) return 0;
215#endif
216#if defined(USE_WIN32)
217    if(win32_install(dp) == 0) return 0;
218#endif
219#if defined(USE_CONIO)
220    if(conio_install(dp) == 0) return 0;
221#endif
222#if defined(USE_VGA)
223    if(vga_install(dp) == 0) return 0;
224#endif
225#if defined(USE_X11)
226    if(x11_install(dp) == 0) return 0;
227#endif
228#if defined(USE_GL)
229    if(gl_install(dp) == 0) return 0;
230#endif
231    /* ncurses has a higher priority than slang because it has better colour
232     * support across terminal types, despite being slightly slower. */
233#if defined(USE_NCURSES)
234    if(ncurses_install(dp) == 0) return 0;
235#endif
236#if defined(USE_SLANG)
237    if(slang_install(dp) == 0) return 0;
238#endif
239
240    return -1;
241}
242
243#if defined(USE_PLUGINS)
244static int caca_plugin_install(char const *name, caca_display_t *dp)
245{
246    char buf[512];
247    int (*sym) (caca_display_t *);
248
249    sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, name);
250    dp->plugin = dlopen(buf, RTLD_NOW);
251    if(!dp->plugin)
252    {
253        sprintf(buf, "lib%s_plugin.so", name);
254        dp->plugin = dlopen(buf, RTLD_NOW);
255        if(!dp->plugin)
256            return -1;
257    }
258
259    sprintf(buf, "%s_install", name);
260    sym = dlsym(dp->plugin, buf);
261    if(!sym)
262    {
263        dlclose(dp->plugin);
264        return -1;
265    }
266
267    return sym(dp);
268}
269#endif
270
Note: See TracBrowser for help on using the repository browser.