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

Last change on this file since 1024 was 1006, checked in by Sam Hocevar, 14 years ago
  • Fixed libcaca prototypes so that all functions use the errno mechanism and return a value.
  • Property svn:keywords set to Id
File size: 4.9 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 1006 2006-08-02 13:12:43Z 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#include "common.h"
22
23#if !defined(__KERNEL__)
24#   include <stdlib.h>
25#   include <string.h>
26#   if defined(HAVE_ERRNO_H)
27#       include <errno.h>
28#   endif
29#endif
30
31#include "cucul.h"
32#include "cucul_internals.h"
33#include "caca.h"
34#include "caca_internals.h"
35
36static int caca_select_driver(caca_display_t *dp);
37
38/** \brief Attach a caca graphical context to a cucul canvas.
39 *
40 *  Create a graphical context using device-dependent features (ncurses for
41 *  terminals, an X11 window, a DOS command window...) that attaches to a
42 *  libcucul canvas. Everything that gets drawn in the libcucul canvas can
43 *  then be displayed by the libcaca driver.
44 *
45 *  If an error occurs, NULL is returned and \b errno is set accordingly:
46 *  - \c ENOMEM Not enough memory.
47 *  - \c ENODEV Graphical device could not be initialised.
48 *
49 *  \param cv The cucul cavas.
50 *  \return The caca graphical context or NULL if an error occurred.
51 */
52caca_display_t * caca_create_display(cucul_canvas_t * cv)
53{
54    caca_display_t *dp = malloc(sizeof(caca_display_t));
55
56    if(!dp)
57    {
58#if defined(HAVE_ERRNO_H)
59        errno = ENOMEM;
60#endif
61        return NULL;
62    }
63
64    dp->cv = cv;
65
66    if(caca_select_driver(dp))
67    {
68        free(dp);
69#if defined(HAVE_ERRNO_H)
70        errno = ENODEV;
71#endif
72        return NULL;
73    }
74
75    if(dp->drv.init_graphics(dp))
76    {
77        free(dp);
78#if defined(HAVE_ERRNO_H)
79        errno = ENODEV;
80#endif
81        return NULL;
82    }
83
84    /* Attached! */
85    dp->cv->refcount++;
86
87    /* Graphics stuff */
88    dp->delay = 0;
89    dp->rendertime = 0;
90
91    /* Events stuff */
92#if defined(USE_SLANG) || defined(USE_NCURSES)
93    dp->events.key_timer.last_sec = 0;
94    dp->events.key_timer.last_usec = 0;
95    dp->events.last_key_ticks = 0;
96    dp->events.autorepeat_ticks = 0;
97    dp->events.last_key_event.type = CACA_EVENT_NONE;
98#endif
99#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
100    dp->events.queue = 0;
101#endif
102
103    dp->timer.last_sec = 0;
104    dp->timer.last_usec = 0;
105    dp->lastticks = 0;
106
107    /* Mouse position */
108    dp->mouse.x = dp->cv->width / 2;
109    dp->mouse.y = dp->cv->height / 2;
110
111    /* Resize events */
112    dp->resize.resized = 0;
113
114    return dp;
115}
116
117/** \brief Detach a caca graphical context from a cucul backend context.
118 *
119 *  Detach a graphical context from its cucul backend and destroy it. The
120 *  libcucul canvas continues to exist and other graphical contexts can be
121 *  attached to it afterwards.
122 *
123 *  This function never fails.
124 *
125 *  \param dp The libcaca graphical context.
126 *  \return This function always returns 0.
127 */
128int caca_free_display(caca_display_t *dp)
129{
130    dp->drv.end_graphics(dp);
131    dp->cv->refcount--;
132    free(dp);
133
134    return 0;
135}
136
137/*
138 * XXX: The following functions are local.
139 */
140
141static int caca_select_driver(caca_display_t *dp)
142{
143#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
144    char *var = getenv("CACA_DRIVER");
145   
146    /* If the environment variable was set, use it */
147    if(var && *var)
148    {
149#if defined(USE_WIN32)
150        if(!strcasecmp(var, "win32")) return win32_install(dp);
151#endif
152#if defined(USE_CONIO)
153        if(!strcasecmp(var, "conio")) return conio_install(dp);
154#endif
155#if defined(USE_X11)
156        if(!strcasecmp(var, "x11")) return x11_install(dp);
157#endif
158#if defined(USE_GL)
159        if(!strcasecmp(var, "gl")) return gl_install(dp);
160#endif
161        if(!strcasecmp(var, "raw")) return raw_install(dp);
162#if defined(USE_SLANG)
163        if(!strcasecmp(var, "slang")) return slang_install(dp);
164#endif
165#if defined(USE_NCURSES)
166        if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
167#endif
168#if defined(USE_VGA)
169        if(!strcasecmp(var, "vga")) return vga_install(dp);
170#endif
171        return -1;
172    }
173#endif
174
175#if defined(USE_WIN32)
176    if(win32_install(dp) == 0) return 0;
177#endif
178#if defined(USE_CONIO)
179    if(conio_install(dp) == 0) return 0;
180#endif
181#if defined(USE_VGA)
182    if(vga_install(dp) == 0) return 0;
183#endif
184#if defined(USE_X11)
185    if(x11_install(dp) == 0) return 0;
186#endif
187#if defined(USE_GL)
188    if(gl_install(dp) == 0) return 0;
189#endif
190    /* ncurses has a higher priority than slang because it has better colour
191     * support across terminal types, despite being slightly slower. */
192#if defined(USE_NCURSES)
193    if(ncurses_install(dp) == 0) return 0;
194#endif
195#if defined(USE_SLANG)
196    if(slang_install(dp) == 0) return 0;
197#endif
198
199    return -1;
200}
201
Note: See TracBrowser for help on using the repository browser.