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

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