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

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