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

Last change on this file since 1462 was 1462, checked in by Sam Hocevar, 14 years ago
  • Bwarf, typo in the no warranty clause.
  • Property svn:keywords set to Id
File size: 6.2 KB
RevLine 
[35]1/*
[672]2 *  libcaca       Colour ASCII-Art library
[699]3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
[268]4 *                All Rights Reserved
[35]5 *
[769]6 *  $Id: caca.c 1462 2006-12-12 01:53:54Z sam $
7 *
[1462]8 *  This library is free software. It comes without any warranty, to
[1452]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
[522]12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
[35]13 */
[17]14
[769]15/*
[268]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.
[205]19 */
20
[63]21#include "config.h"
[859]22#include "common.h"
[63]23
[565]24#if !defined(__KERNEL__)
25#   include <stdlib.h>
26#   include <string.h>
[1208]27#   include <stdio.h>
28#   if defined(USE_PLUGINS)
29#       if defined(HAVE_DLFCN_H)
30#           include <dlfcn.h>
31#       endif
32#   endif
[565]33#endif
[17]34
[524]35#include "cucul.h"
36#include "cucul_internals.h"
[185]37#include "caca.h"
38#include "caca_internals.h"
[17]39
[1208]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
[227]44
[1208]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
[810]50/** \brief Attach a caca graphical context to a cucul canvas.
[540]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 *
[1006]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 *
[811]61 *  \param cv The cucul cavas.
[540]62 *  \return The caca graphical context or NULL if an error occurred.
63 */
[819]64caca_display_t * caca_create_display(cucul_canvas_t * cv)
[17]65{
[811]66    caca_display_t *dp = malloc(sizeof(caca_display_t));
[524]67
[1006]68    if(!dp)
69    {
[1362]70        seterrno(ENOMEM);
[1006]71        return NULL;
72    }
73
[811]74    dp->cv = cv;
[1208]75#if defined(USE_PLUGINS)
76    dp->plugin = NULL;
77#endif
[232]78
[1006]79    if(caca_select_driver(dp))
[539]80    {
[1208]81#if defined(USE_PLUGINS)
82        if(dp->plugin)
83            dlclose(dp->plugin);
84#endif
[811]85        free(dp);
[1362]86        seterrno(ENODEV);
[524]87        return NULL;
[265]88    }
[79]89
[811]90    if(dp->drv.init_graphics(dp))
[335]91    {
[1208]92#if defined(USE_PLUGINS)
93        if(dp->plugin)
94            dlclose(dp->plugin);
95#endif
[811]96        free(dp);
[1362]97        seterrno(ENODEV);
[539]98        return NULL;
[483]99    }
[213]100
[540]101    /* Attached! */
[811]102    dp->cv->refcount++;
[540]103
104    /* Graphics stuff */
[811]105    dp->delay = 0;
106    dp->rendertime = 0;
[540]107
108    /* Events stuff */
109#if defined(USE_SLANG) || defined(USE_NCURSES)
[811]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;
[540]115#endif
[681]116#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
[811]117    dp->events.queue = 0;
[593]118#endif
[527]119
[811]120    dp->timer.last_sec = 0;
121    dp->timer.last_usec = 0;
122    dp->lastticks = 0;
[527]123
[553]124    /* Mouse position */
[811]125    dp->mouse.x = dp->cv->width / 2;
126    dp->mouse.y = dp->cv->height / 2;
[541]127
[553]128    /* Resize events */
[811]129    dp->resize.resized = 0;
[306]130
[811]131    return dp;
[17]132}
133
[540]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 *
[1006]140 *  This function never fails.
141 *
[811]142 *  \param dp The libcaca graphical context.
[1006]143 *  \return This function always returns 0.
[540]144 */
[1006]145int caca_free_display(caca_display_t *dp)
[17]146{
[811]147    dp->drv.end_graphics(dp);
[1208]148#if defined(USE_PLUGINS)
149    if(dp->plugin)
150        dlclose(dp->plugin);
151#endif
[811]152    dp->cv->refcount--;
153    free(dp);
[1006]154
155    return 0;
[17]156}
157
[268]158/*
159 * XXX: The following functions are local.
160 */
161
[1006]162static int caca_select_driver(caca_display_t *dp)
[265]163{
164#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
165    char *var = getenv("CACA_DRIVER");
[1396]166
[265]167    /* If the environment variable was set, use it */
168    if(var && *var)
[623]169    {
[1396]170#if defined(USE_COCOA)
171        if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
172#endif
[335]173#if defined(USE_WIN32)
[811]174        if(!strcasecmp(var, "win32")) return win32_install(dp);
[335]175#endif
[265]176#if defined(USE_CONIO)
[811]177        if(!strcasecmp(var, "conio")) return conio_install(dp);
[265]178#endif
[285]179#if defined(USE_X11)
[811]180        if(!strcasecmp(var, "x11")) return x11_install(dp);
[265]181#endif
[483]182#if defined(USE_GL)
[811]183        if(!strcasecmp(var, "gl")) return gl_install(dp);
[483]184#endif
[1054]185#if !defined(__KERNEL__)
[811]186        if(!strcasecmp(var, "raw")) return raw_install(dp);
[1054]187#endif
[265]188#if defined(USE_SLANG)
[811]189        if(!strcasecmp(var, "slang")) return slang_install(dp);
[265]190#endif
[285]191#if defined(USE_NCURSES)
[811]192        if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
[265]193#endif
[565]194#if defined(USE_VGA)
[811]195        if(!strcasecmp(var, "vga")) return vga_install(dp);
[565]196#endif
[684]197        return -1;
[265]198    }
199#endif
200
[1396]201#if defined(USE_COCOA)
202    if(cocoa_install(dp) == 0) return 0;
203#endif
[335]204#if defined(USE_WIN32)
[811]205    if(win32_install(dp) == 0) return 0;
[335]206#endif
[265]207#if defined(USE_CONIO)
[811]208    if(conio_install(dp) == 0) return 0;
[265]209#endif
[565]210#if defined(USE_VGA)
[811]211    if(vga_install(dp) == 0) return 0;
[565]212#endif
[285]213#if defined(USE_X11)
[811]214    if(x11_install(dp) == 0) return 0;
[265]215#endif
[483]216#if defined(USE_GL)
[811]217    if(gl_install(dp) == 0) return 0;
[483]218#endif
[984]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
[265]224#if defined(USE_SLANG)
[811]225    if(slang_install(dp) == 0) return 0;
[265]226#endif
[487]227
[539]228    return -1;
[265]229}
230
[1208]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.