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

Last change on this file since 2299 was 2299, checked in by Sam Hocevar, 12 years ago
  • Remove #include <stdint.h> etc. from "common.h". Instead, make sure that <cucul.h> will provide the C99 types, even if libcaca has been installed.
  • Rename what's left of "common.h" to "stubs.h".
  • Remove all references to erroneous <inttypes.h> from source files.
  • Property svn:keywords set to Id
File size: 11.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 2299 2008-04-19 12:42:50Z 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
23#if !defined(__KERNEL__)
24#   include <stdlib.h>
25#   include <string.h>
26#   include <stdio.h>
27#   if defined(USE_PLUGINS)
28#       if defined(HAVE_DLFCN_H)
29#           include <dlfcn.h>
30#       endif
31#   endif
32#endif
33
34#include "cucul.h"
35#include "caca.h"
36#include "caca_internals.h"
37
38#if defined(USE_PLUGINS)
39#   define gl_install(p) caca_plugin_install(p, "gl")
40#   define x11_install(p) caca_plugin_install(p, "x11")
41#endif
42
43static int caca_can_resize(caca_display_t *);
44static int caca_install_driver(caca_display_t *, char const *);
45static int caca_uninstall_driver(caca_display_t *);
46static int caca_select_driver(caca_display_t *, char const *);
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 no cucul canvas is provided, a new one is created. Its handle can be
59 *  retrieved using caca_get_canvas() and it is automatically destroyed when
60 *  caca_free_display() is called.
61 *
62 *  See also caca_create_display_with_driver().
63 *
64 *  If an error occurs, NULL is returned and \b errno is set accordingly:
65 *  - \c ENOMEM Not enough memory.
66 *  - \c ENODEV Graphical device could not be initialised.
67 *
68 *  \param cv The cucul canvas or NULL to create a canvas automatically.
69 *  \return The caca graphical context or NULL if an error occurred.
70 */
71caca_display_t * caca_create_display(cucul_canvas_t *cv)
72{
73    return caca_create_display_with_driver(cv, NULL);
74}
75
76/** \brief Attach a specific caca graphical context to a cucul canvas.
77 *
78 *  Create a graphical context using device-dependent features (ncurses for
79 *  terminals, an X11 window, a DOS command window...) that attaches to a
80 *  libcucul canvas. Everything that gets drawn in the libcucul canvas can
81 *  then be displayed by the libcaca driver.
82 *
83 *  If no cucul canvas is provided, a new one is created. Its handle can be
84 *  retrieved using caca_get_canvas() and it is automatically destroyed when
85 *  caca_free_display() is called.
86 *
87 *  If no driver name is provided, \e libcaca will try to autodetect the best
88 *  output driver it can.
89 *
90 *  See also caca_create_display().
91 *
92 *  If an error occurs, NULL is returned and \b errno is set accordingly:
93 *  - \c ENOMEM Not enough memory.
94 *  - \c ENODEV Graphical device could not be initialised.
95 *
96 *  \param cv The cucul canvas or NULL to create a canvas automatically.
97 *  \param driver A string describing the desired output driver or NULL to
98 *                choose the best driver automatically.
99 *  \return The caca graphical context or NULL if an error occurred.
100 */
101caca_display_t * caca_create_display_with_driver(cucul_canvas_t *cv,
102                                                 char const *driver)
103{
104    caca_display_t *dp = malloc(sizeof(caca_display_t));
105
106    if(!dp)
107    {
108        seterrno(ENOMEM);
109        return NULL;
110    }
111
112    if((dp->autorelease = (cv == NULL)))
113    {
114        cv = cucul_create_canvas(0, 0);
115    }
116
117    dp->cv = cv;
118
119    if(cucul_manage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp))
120    {
121        if(dp->autorelease)
122            cucul_free_canvas(dp->cv);
123        free(dp);
124        seterrno(EBUSY);
125        return NULL;
126    }
127
128    if(caca_install_driver(dp, driver))
129    {
130        cucul_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
131        if(dp->autorelease)
132            cucul_free_canvas(dp->cv);
133        free(dp);
134        seterrno(ENODEV);
135        return NULL;
136    }
137
138    return dp;
139}
140
141/** \brief Get available display drivers
142 *
143 *  Return a list of available display drivers. The list is a NULL-terminated
144 *  array of strings, interleaving a string containing the internal value for
145 *  the display driver, and a string containing the natural language
146 *  description for that driver.
147 *
148 *  This function never fails.
149 *
150 *  \param dp Display object.
151 *  \return An array of strings.
152 */
153char const * const * caca_get_display_driver_list(void)
154{
155    static char const * const list[] =
156    {
157#if defined(USE_COCOA)
158        "cocoa", "Mac OS X Cocoa",
159#endif
160#if defined(USE_WIN32)
161        "win32", "Windows console",
162#endif
163#if defined(USE_CONIO)
164        "conio", "MS-DOS conio",
165#endif
166#if defined(USE_X11)
167        "x11", "X11 graphical window",
168#endif
169#if defined(USE_GL)
170        "gl", "OpenGL window",
171#endif
172#if defined(USE_SLANG)
173        "slang", "S-Lang console library",
174#endif
175#if defined(USE_NCURSES)
176        "ncurses", "ncurses console library",
177#endif
178#if defined(USE_VGA)
179        "vga", "direct VGA memory",
180#endif
181#if !defined(__KERNEL__)
182        "raw", "raw libcaca output",
183#endif
184        NULL, NULL
185    };
186
187    return list;
188}
189
190/** \brief Return a caca graphical context's current output driver.
191 *
192 *  Return the given display's current output driver.
193 *
194 *  This function never fails.
195 *
196 *  \param dp The caca display.
197 *  \return A static string.
198 */
199char const * caca_get_display_driver(caca_display_t *dp)
200{
201    return dp->drv.driver;
202}
203
204/** \brief Set the output driver.
205 *
206 *  Dynamically change the given display's output driver.
207 *
208 *  FIXME: decide what to do in case of failure
209 *
210 *  \param dp The caca display.
211 *  \param driver A string describing the desired output driver or NULL to
212 *                choose the best driver automatically.
213 *  \return 0 in case of success, -1 if an error occurred.
214 */
215int caca_set_display_driver(caca_display_t *dp, char const *driver)
216{
217    caca_uninstall_driver(dp);
218    if(caca_install_driver(dp, driver))
219    {
220        seterrno(ENODEV);
221        return -1;
222    }
223
224    return 0;
225}
226
227/** \brief Detach a caca graphical context from a cucul backend context.
228 *
229 *  Detach a graphical context from its cucul backend and destroy it. The
230 *  libcucul canvas continues to exist and other graphical contexts can be
231 *  attached to it afterwards.
232 *
233 *  If the cucul canvas was automatically created by caca_create_display(),
234 *  it is automatically destroyed and any handle to it becomes invalid.
235 *
236 *  This function never fails.
237 *
238 *  \param dp The libcaca graphical context.
239 *  \return This function always returns 0.
240 */
241int caca_free_display(caca_display_t *dp)
242{
243    caca_uninstall_driver(dp);
244    cucul_unmanage_canvas(dp->cv, (int (*)(void *))caca_can_resize, (void *)dp);
245    if(dp->autorelease)
246        cucul_free_canvas(dp->cv);
247    free(dp);
248
249    return 0;
250}
251
252/** \brief Get the canvas attached to a caca graphical context.
253 *
254 *  Return a handle on the \e cucul_canvas_t object that was either attached
255 *  or created by caca_create_display().
256 *
257 *  This function never fails.
258 *
259 *  \param dp The libcaca graphical context.
260 *  \return The libcucul canvas.
261 */
262cucul_canvas_t * caca_get_canvas(caca_display_t *dp)
263{
264    return dp->cv;
265}
266
267/** \brief Return the \e libcaca version.
268 *
269 *  Return a read-only string with the \e libcaca version information.
270 *
271 *  This function never fails.
272 *
273 *  \return The \e libcaca version information.
274 */
275char const * caca_get_version(void)
276{
277    return VERSION;
278}
279
280/*
281 * XXX: The following functions are local.
282 */
283
284static int caca_can_resize(caca_display_t *dp)
285{
286    return dp->resize.allow;
287}
288
289static int caca_install_driver(caca_display_t *dp, char const *driver)
290{
291#if defined(USE_PLUGINS)
292    dp->plugin = NULL;
293#endif
294
295    if(caca_select_driver(dp, driver))
296    {
297#if defined(USE_PLUGINS)
298        if(dp->plugin)
299            dlclose(dp->plugin);
300#endif
301        return -1;
302    }
303
304    if(dp->drv.init_graphics(dp))
305    {
306#if defined(USE_PLUGINS)
307        if(dp->plugin)
308            dlclose(dp->plugin);
309#endif
310        return -1;
311    }
312
313    /* Graphics stuff */
314    dp->delay = 0;
315    dp->rendertime = 0;
316
317    /* Events stuff */
318#if defined(USE_SLANG) || defined(USE_NCURSES)
319    dp->events.key_timer.last_sec = 0;
320    dp->events.key_timer.last_usec = 0;
321    dp->events.last_key_ticks = 0;
322    dp->events.autorepeat_ticks = 0;
323    dp->events.last_key_event.type = CACA_EVENT_NONE;
324#endif
325#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
326    dp->events.queue = 0;
327#endif
328
329    dp->timer.last_sec = 0;
330    dp->timer.last_usec = 0;
331    dp->lastticks = 0;
332
333    /* Mouse position */
334    dp->mouse.x = cucul_get_canvas_width(dp->cv) / 2;
335    dp->mouse.y = cucul_get_canvas_height(dp->cv) / 2;
336
337    /* Resize events */
338    dp->resize.resized = 0;
339    dp->resize.allow = 0;
340
341    return 0;
342}
343
344static int caca_uninstall_driver(caca_display_t *dp)
345{
346    dp->drv.end_graphics(dp);
347#if defined(USE_PLUGINS)
348    if(dp->plugin)
349        dlclose(dp->plugin);
350#endif
351
352    return 0;
353}
354
355static int caca_select_driver(caca_display_t *dp, char const *driver)
356{
357    char const *var = driver;
358#if defined(HAVE_GETENV)
359    if(!var)
360        var = getenv("CACA_DRIVER");
361#endif
362
363#if defined(HAVE_STRCASECMP)
364    /* If the environment variable was set, use it */
365    if(var && *var)
366    {
367#if defined(USE_COCOA)
368        if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
369#endif
370#if defined(USE_WIN32)
371        if(!strcasecmp(var, "win32")) return win32_install(dp);
372#endif
373#if defined(USE_CONIO)
374        if(!strcasecmp(var, "conio")) return conio_install(dp);
375#endif
376#if defined(USE_X11)
377        if(!strcasecmp(var, "x11")) return x11_install(dp);
378#endif
379#if defined(USE_GL)
380        if(!strcasecmp(var, "gl")) return gl_install(dp);
381#endif
382#if !defined(__KERNEL__)
383        if(!strcasecmp(var, "raw")) return raw_install(dp);
384#endif
385#if defined(USE_SLANG)
386        if(!strcasecmp(var, "slang")) return slang_install(dp);
387#endif
388#if defined(USE_NCURSES)
389        if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
390#endif
391#if defined(USE_VGA)
392        if(!strcasecmp(var, "vga")) return vga_install(dp);
393#endif
394        return -1;
395    }
396#endif
397
398#if defined(USE_COCOA)
399    if(cocoa_install(dp) == 0) return 0;
400#endif
401#if defined(USE_WIN32)
402    if(win32_install(dp) == 0) return 0;
403#endif
404#if defined(USE_CONIO)
405    if(conio_install(dp) == 0) return 0;
406#endif
407#if defined(USE_VGA)
408    if(vga_install(dp) == 0) return 0;
409#endif
410#if defined(USE_X11)
411    if(x11_install(dp) == 0) return 0;
412#endif
413#if defined(USE_GL)
414    if(gl_install(dp) == 0) return 0;
415#endif
416    /* ncurses has a higher priority than slang because it has better colour
417     * support across terminal types, despite being slightly slower. */
418#if defined(USE_NCURSES)
419    if(ncurses_install(dp) == 0) return 0;
420#endif
421#if defined(USE_SLANG)
422    if(slang_install(dp) == 0) return 0;
423#endif
424
425    return -1;
426}
427
428#if defined(USE_PLUGINS)
429static int caca_plugin_install(caca_display_t *dp, char const *driver)
430{
431    char buf[512];
432    int (*sym) (caca_display_t *);
433
434    sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, driver);
435    dp->plugin = dlopen(buf, RTLD_NOW);
436    if(!dp->plugin)
437    {
438        sprintf(buf, "lib%s_plugin.so", driver);
439        dp->plugin = dlopen(buf, RTLD_NOW);
440        if(!dp->plugin)
441            return -1;
442    }
443
444    sprintf(buf, "%s_install", driver);
445    sym = dlsym(dp->plugin, buf);
446    if(!sym)
447    {
448        dlclose(dp->plugin);
449        return -1;
450    }
451
452    return sym(dp);
453}
454#endif
455
Note: See TracBrowser for help on using the repository browser.