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

Last change on this file since 557 was 554, checked in by Jean-Yves Lamoureux, 14 years ago
  • Added very preliminary network driver
  • Property svn:keywords set to Id
File size: 4.4 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 554 2006-03-08 20:03:24Z jylam $
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    /* Mouse position */
83    kk->mouse.x = kk->qq->width / 2;
84    kk->mouse.y = kk->qq->height / 2;
85
86    /* Resize events */
87    kk->resize.resized = 0;
88
89    return kk;
90}
91
92/** \brief Detach a caca graphical context from a cucul backend context.
93 *
94 *  Detach a graphical context from its cucul backend and destroy it. The
95 *  libcucul canvas continues to exist and other graphical contexts can be
96 *  attached to it afterwards.
97 *
98 *  \param qq The caca graphical context.
99 */
100void caca_detach(caca_t *kk)
101{
102    kk->drv.end_graphics(kk);
103    kk->qq->refcount--;
104    free(kk);
105}
106
107/*
108 * XXX: The following functions are local.
109 */
110
111static int caca_init_driver(caca_t *kk)
112{
113#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
114    char *var = getenv("CACA_DRIVER");
115
116    /* If the environment variable was set, use it */
117    if(var && *var)
118    {
119#if defined(USE_WIN32)
120        if(!strcasecmp(var, "win32"))
121            win32_init_driver(kk);
122        else
123#endif
124#if defined(USE_CONIO)
125        if(!strcasecmp(var, "conio"))
126            conio_init_driver(kk);
127        else
128#endif
129#if defined(USE_X11)
130        if(!strcasecmp(var, "x11"))
131            x11_init_driver(kk);
132        else
133#endif
134#if defined(USE_GL)
135        if(!strcasecmp(var, "gl"))
136            gl_init_driver(kk);
137        else
138#endif
139#if defined(USE_SLANG)
140        if(!strcasecmp(var, "slang"))
141            slang_init_driver(kk);
142        else
143#endif
144#if defined(USE_NCURSES)
145        if(!strcasecmp(var, "ncurses"))
146            ncurses_init_driver(kk);
147        else
148#endif
149#if defined(USE_NETWORK)
150        if(!strcasecmp(var, "network"))
151            network_init_driver(kk);
152        else
153#endif
154            return -1;
155
156        return 0;
157    }
158#endif
159
160#if defined(USE_WIN32)
161    win32_init_driver(kk);
162    return 0;
163#endif
164#if defined(USE_CONIO)
165    conio_init_driver(kk);
166    return 0;
167#endif
168#if defined(USE_X11)
169#if defined(HAVE_GETENV)
170    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
171#endif
172    {
173        x11_init_driver(kk);
174        return 0;
175    }
176#endif
177#if defined(USE_GL)
178#if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
179    if(getenv("DISPLAY") && *(getenv("DISPLAY")))
180#endif
181    {
182        gl_init_driver(kk);
183        return 0;
184    }
185#endif
186#if defined(USE_SLANG)
187    slang_init_driver(kk);
188    return 0;
189#endif
190#if defined(USE_NCURSES)
191    slang_init_driver(kk);
192    return 0;
193#endif
194#if defined(USE_NETWORK)
195    network_init_driver(kk);
196    return 0;
197#endif
198
199    return -1;
200}
201
Note: See TracBrowser for help on using the repository browser.