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

Last change on this file since 684 was 684, checked in by Sam Hocevar, 15 years ago
  • Allow the driver initialisation to fail, for instance when $DISPLAY = "".
  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
10 */
11
12/** \file caca.c
13 *  \version \$Id: caca.c 684 2006-03-24 09:48:20Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief Main \e libcaca functions
16 *
17 *  This file contains the main functions used by \e libcaca applications to
18 *  initialise the library, get the screen properties, set the framerate and
19 *  so on.
20 */
21
22#include "config.h"
23
24#if !defined(__KERNEL__)
25#   include <stdlib.h>
26#   include <string.h>
27#endif
28
29#include "cucul.h"
30#include "cucul_internals.h"
31#include "caca.h"
32#include "caca_internals.h"
33
34static int caca_init_driver(caca_t *kk);
35
36/** \brief Attach a caca graphical context to a cucul backend context.
37 *
38 *  Create a graphical context using device-dependent features (ncurses for
39 *  terminals, an X11 window, a DOS command window...) that attaches to a
40 *  libcucul canvas. Everything that gets drawn in the libcucul canvas can
41 *  then be displayed by the libcaca driver.
42 *
43 *  \param qq The cucul backend context.
44 *  \return The caca graphical context or NULL if an error occurred.
45 */
46caca_t * caca_attach(cucul_t * qq)
47{
48    caca_t *kk = malloc(sizeof(caca_t));
49
50    kk->qq = qq;
51
52    if(caca_init_driver(kk))
53    {
54        free(kk);
55        return NULL;
56    }
57
58    if(kk->drv.init_graphics(kk))
59    {
60        free(kk);
61        return NULL;
62    }
63
64    /* Attached! */
65    kk->qq->refcount++;
66
67    /* Graphics stuff */
68    kk->delay = 0;
69    kk->rendertime = 0;
70
71    /* Events stuff */
72#if defined(USE_SLANG) || defined(USE_NCURSES)
73    kk->events.key_timer.last_sec = 0;
74    kk->events.key_timer.last_usec = 0;
75    kk->events.last_key_ticks = 0;
76    kk->events.autorepeat_ticks = 0;
77    kk->events.last_key_event.type = CACA_EVENT_NONE;
78#endif
79#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
80    kk->events.queue = 0;
81#endif
82
83    kk->timer.last_sec = 0;
84    kk->timer.last_usec = 0;
85    kk->lastticks = 0;
86
87    /* Mouse position */
88    kk->mouse.x = kk->qq->width / 2;
89    kk->mouse.y = kk->qq->height / 2;
90
91    /* Resize events */
92    kk->resize.resized = 0;
93
94    return kk;
95}
96
97/** \brief Detach a caca graphical context from a cucul backend context.
98 *
99 *  Detach a graphical context from its cucul backend and destroy it. The
100 *  libcucul canvas continues to exist and other graphical contexts can be
101 *  attached to it afterwards.
102 *
103 *  \param qq The caca graphical context.
104 */
105void caca_detach(caca_t *kk)
106{
107    kk->drv.end_graphics(kk);
108    kk->qq->refcount--;
109    free(kk);
110}
111
112/*
113 * XXX: The following functions are local.
114 */
115
116static int caca_init_driver(caca_t *kk)
117{
118#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
119    char *var = getenv("CACA_DRIVER");
120   
121    /* If the environment variable was set, use it */
122    if(var && *var)
123    {
124#if defined(USE_WIN32)
125        if(!strcasecmp(var, "win32")) return win32_install(kk);
126#endif
127#if defined(USE_CONIO)
128        if(!strcasecmp(var, "conio")) return conio_install(kk);
129#endif
130#if defined(USE_X11)
131        if(!strcasecmp(var, "x11")) return x11_install(kk);
132#endif
133#if defined(USE_GL)
134        if(!strcasecmp(var, "gl")) return gl_install(kk);
135#endif
136#if defined(USE_SLANG)
137        if(!strcasecmp(var, "slang")) return slang_install(kk);
138#endif
139#if defined(USE_NCURSES)
140        if(!strcasecmp(var, "ncurses")) return ncurses_install(kk);
141#endif
142#if defined(USE_NETWORK)
143        if(!strcasecmp(var, "network")) return network_install(kk);
144#endif
145#if defined(USE_VGA)
146        if(!strcasecmp(var, "vga")) return vga_install(kk);
147#endif
148        return -1;
149    }
150#endif
151
152#if defined(USE_WIN32)
153    if(win32_install(kk) == 0) return 0;
154#endif
155#if defined(USE_CONIO)
156    if(conio_install(kk) == 0) return 0;
157#endif
158#if defined(USE_VGA)
159    if(vga_install(kk) == 0) return 0;
160#endif
161#if defined(USE_X11)
162    if(x11_install(kk) == 0) return 0;
163#endif
164#if defined(USE_GL)
165    if(gl_install(kk) == 0) return 0;
166#endif
167#if defined(USE_SLANG)
168    if(slang_install(kk) == 0) return 0;
169#endif
170#if defined(USE_NCURSES)
171    if(ncurses_install(kk) == 0) return 0;
172#endif
173#if defined(USE_NETWORK)
174    if(network_install(kk) == 0) return 0;
175#endif
176
177    return -1;
178}
179
Note: See TracBrowser for help on using the repository browser.