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

Last change on this file since 550 was 550, checked in by Sam Hocevar, 14 years ago
  • Finished moving everything driver-specific to the apropriate driver_*.c files. Phew. Now they just need a few comments.
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1/*
2 *  libcaca       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 550 2006-03-08 10:33:01Z 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#include <stdlib.h>
25#include <string.h>
26
27#include "cucul.h"
28#include "cucul_internals.h"
29#include "caca.h"
30#include "caca_internals.h"
31
32static int caca_init_driver(caca_t *kk);
33
34/** \brief Attach a caca graphical context to a cucul backend context.
35 *
36 *  Create a graphical context using device-dependent features (ncurses for
37 *  terminals, an X11 window, a DOS command window...) that attaches to a
38 *  libcucul canvas. Everything that gets drawn in the libcucul canvas can
39 *  then be displayed by the libcaca driver.
40 *
41 *  \param qq The cucul backend context.
42 *  \return The caca graphical context or NULL if an error occurred.
43 */
44caca_t * caca_attach(cucul_t * qq)
45{
46    caca_t *kk = malloc(sizeof(caca_t));
47
48    kk->qq = qq;
49
50    if(caca_init_driver(kk))
51    {
52        free(kk);
53        return NULL;
54    }
55
56    if(kk->drv.init_graphics(kk))
57    {
58        free(kk);
59        return NULL;
60    }
61
62    /* Attached! */
63    kk->qq->refcount++;
64
65    /* Graphics stuff */
66    kk->delay = 0;
67    kk->rendertime = 0;
68
69    /* Events stuff */
70#if defined(USE_SLANG) || defined(USE_NCURSES)
71    kk->events.key_timer.last_sec = 0;
72    kk->events.key_timer.last_usec = 0;
73    kk->events.last_key_ticks = 0;
74    kk->events.autorepeat_ticks = 0;
75    kk->events.last_key = 0;
76#endif
77
78    kk->timer.last_sec = 0;
79    kk->timer.last_usec = 0;
80    kk->lastticks = 0;
81
82    kk->mouse_x = kk->qq->width / 2;
83    kk->mouse_y = kk->qq->height / 2;
84
85    kk->resize = 0;
86    kk->resize_event = 0;
87
88    return kk;
89}
90
91/** \brief Detach a caca graphical context from a cucul backend context.
92 *
93 *  Detach a graphical context from its cucul backend and destroy it. The
94 *  libcucul canvas continues to exist and other graphical contexts can be
95 *  attached to it afterwards.
96 *
97 *  \param qq The caca graphical context.
98 */
99void caca_detach(caca_t *kk)
100{
101    kk->drv.end_graphics(kk);
102    kk->qq->refcount--;
103    free(kk);
104}
105
106/*
107 * XXX: The following functions are local.
108 */
109
110static int caca_init_driver(caca_t *kk)
111{
112#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
113    char *var = getenv("CACA_DRIVER");
114
115    /* If the environment variable was set, use it */
116    if(var && *var)
117    {
118#if defined(USE_WIN32)
119        if(!strcasecmp(var, "win32"))
120            win32_init_driver(kk);
121        else
122#endif
123#if defined(USE_CONIO)
124        if(!strcasecmp(var, "conio"))
125            conio_init_driver(kk);
126        else
127#endif
128#if defined(USE_X11)
129        if(!strcasecmp(var, "x11"))
130            x11_init_driver(kk);
131        else
132#endif
133#if defined(USE_GL)
134        if(!strcasecmp(var, "gl"))
135            gl_init_driver(kk);
136        else
137#endif
138#if defined(USE_SLANG)
139        if(!strcasecmp(var, "slang"))
140            slang_init_driver(kk);
141        else
142#endif
143#if defined(USE_NCURSES)
144        if(!strcasecmp(var, "ncurses"))
145            ncurses_init_driver(kk);
146        else
147#endif
148            return -1;
149
150        return 0;
151    }
152#endif
153
154#if defined(USE_WIN32)
155    win32_init_driver(kk);
156    return 0;
157#endif
158#if defined(USE_CONIO)
159    conio_init_driver(kk);
160    return 0;
161#endif
162#if defined(USE_X11)
163#if defined(HAVE_GETENV)
164    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
165#endif
166    {
167        x11_init_driver(kk);
168        return 0;
169    }
170#endif
171#if defined(USE_GL)
172#if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
173    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
174#endif
175    {
176        gl_init_driver(kk);
177        return 0;
178    }
179#endif
180#if defined(USE_SLANG)
181    slang_init_driver(kk);
182    return 0;
183#endif
184#if defined(USE_NCURSES)
185    slang_init_driver(kk);
186    return 0;
187#endif
188
189    return -1;
190}
191
Note: See TracBrowser for help on using the repository browser.