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

Last change on this file since 980 was 980, checked in by Sam Hocevar, 14 years ago
  • Minor cosmetic changes in the test programs.
  • Code enhancements in the ncurses driver.
  • Added a comment to explain why slang has a higher priority than ncurses.
  • Property svn:keywords set to Id
File size: 4.4 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 980 2006-05-25 19:58:21Z 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#endif
27
28#include "cucul.h"
29#include "cucul_internals.h"
30#include "caca.h"
31#include "caca_internals.h"
32
33static int caca_init_driver(caca_display_t *dp);
34
35/** \brief Attach a caca graphical context to a cucul canvas.
36 *
37 *  Create a graphical context using device-dependent features (ncurses for
38 *  terminals, an X11 window, a DOS command window...) that attaches to a
39 *  libcucul canvas. Everything that gets drawn in the libcucul canvas can
40 *  then be displayed by the libcaca driver.
41 *
42 *  \param cv The cucul cavas.
43 *  \return The caca graphical context or NULL if an error occurred.
44 */
45caca_display_t * caca_create_display(cucul_canvas_t * cv)
46{
47    caca_display_t *dp = malloc(sizeof(caca_display_t));
48
49    dp->cv = cv;
50
51    if(caca_init_driver(dp))
52    {
53        free(dp);
54        return NULL;
55    }
56
57    if(dp->drv.init_graphics(dp))
58    {
59        free(dp);
60        return NULL;
61    }
62
63    /* Attached! */
64    dp->cv->refcount++;
65
66    /* Graphics stuff */
67    dp->delay = 0;
68    dp->rendertime = 0;
69
70    /* Events stuff */
71#if defined(USE_SLANG) || defined(USE_NCURSES)
72    dp->events.key_timer.last_sec = 0;
73    dp->events.key_timer.last_usec = 0;
74    dp->events.last_key_ticks = 0;
75    dp->events.autorepeat_ticks = 0;
76    dp->events.last_key_event.type = CACA_EVENT_NONE;
77#endif
78#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
79    dp->events.queue = 0;
80#endif
81
82    dp->timer.last_sec = 0;
83    dp->timer.last_usec = 0;
84    dp->lastticks = 0;
85
86    /* Mouse position */
87    dp->mouse.x = dp->cv->width / 2;
88    dp->mouse.y = dp->cv->height / 2;
89
90    /* Resize events */
91    dp->resize.resized = 0;
92
93    return dp;
94}
95
96/** \brief Detach a caca graphical context from a cucul backend context.
97 *
98 *  Detach a graphical context from its cucul backend and destroy it. The
99 *  libcucul canvas continues to exist and other graphical contexts can be
100 *  attached to it afterwards.
101 *
102 *  \param dp The libcaca graphical context.
103 */
104void caca_free_display(caca_display_t *dp)
105{
106    dp->drv.end_graphics(dp);
107    dp->cv->refcount--;
108    free(dp);
109}
110
111/*
112 * XXX: The following functions are local.
113 */
114
115static int caca_init_driver(caca_display_t *dp)
116{
117#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
118    char *var = getenv("CACA_DRIVER");
119   
120    /* If the environment variable was set, use it */
121    if(var && *var)
122    {
123#if defined(USE_WIN32)
124        if(!strcasecmp(var, "win32")) return win32_install(dp);
125#endif
126#if defined(USE_CONIO)
127        if(!strcasecmp(var, "conio")) return conio_install(dp);
128#endif
129#if defined(USE_X11)
130        if(!strcasecmp(var, "x11")) return x11_install(dp);
131#endif
132#if defined(USE_GL)
133        if(!strcasecmp(var, "gl")) return gl_install(dp);
134#endif
135        if(!strcasecmp(var, "raw")) return raw_install(dp);
136#if defined(USE_SLANG)
137        if(!strcasecmp(var, "slang")) return slang_install(dp);
138#endif
139#if defined(USE_NCURSES)
140        if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
141#endif
142#if defined(USE_VGA)
143        if(!strcasecmp(var, "vga")) return vga_install(dp);
144#endif
145        return -1;
146    }
147#endif
148
149#if defined(USE_WIN32)
150    if(win32_install(dp) == 0) return 0;
151#endif
152#if defined(USE_CONIO)
153    if(conio_install(dp) == 0) return 0;
154#endif
155#if defined(USE_VGA)
156    if(vga_install(dp) == 0) return 0;
157#endif
158#if defined(USE_X11)
159    if(x11_install(dp) == 0) return 0;
160#endif
161#if defined(USE_GL)
162    if(gl_install(dp) == 0) return 0;
163#endif
164    /* slang has a higher priority than ncurses because though ncurses handles
165     * color a bit better across terminals, its UTF-8 supports seems broken. */
166#if defined(USE_SLANG)
167    if(slang_install(dp) == 0) return 0;
168#endif
169#if defined(USE_NCURSES)
170    if(ncurses_install(dp) == 0) return 0;
171#endif
172
173    return -1;
174}
175
Note: See TracBrowser for help on using the repository browser.