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

Last change on this file since 699 was 699, checked in by Sam Hocevar, 14 years ago
  • Created a minimal raw driver that outputs stuff to stdout.
  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 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 699 2006-03-27 18:07:18Z 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(!strcasecmp(var, "raw")) return raw_install(kk);
137#if defined(USE_SLANG)
138        if(!strcasecmp(var, "slang")) return slang_install(kk);
139#endif
140#if defined(USE_NCURSES)
141        if(!strcasecmp(var, "ncurses")) return ncurses_install(kk);
142#endif
143#if defined(USE_NETWORK)
144        if(!strcasecmp(var, "network")) return network_install(kk);
145#endif
146#if defined(USE_VGA)
147        if(!strcasecmp(var, "vga")) return vga_install(kk);
148#endif
149        return -1;
150    }
151#endif
152
153#if defined(USE_WIN32)
154    if(win32_install(kk) == 0) return 0;
155#endif
156#if defined(USE_CONIO)
157    if(conio_install(kk) == 0) return 0;
158#endif
159#if defined(USE_VGA)
160    if(vga_install(kk) == 0) return 0;
161#endif
162#if defined(USE_X11)
163    if(x11_install(kk) == 0) return 0;
164#endif
165#if defined(USE_GL)
166    if(gl_install(kk) == 0) return 0;
167#endif
168#if defined(USE_SLANG)
169    if(slang_install(kk) == 0) return 0;
170#endif
171#if defined(USE_NCURSES)
172    if(ncurses_install(kk) == 0) return 0;
173#endif
174
175    return -1;
176}
177
Note: See TracBrowser for help on using the repository browser.