source: libcaca/trunk/src/caca.c @ 251

Last change on this file since 251 was 251, checked in by Sam Hocevar, 19 years ago
  • src/graphics.c src/io.c: + Beginning of an X11 driver. Currently we merely open the window and

check keyboard events.

  • Property svn:keywords set to Id
File size: 8.4 KB
Line 
1/*
2 *   libcaca       ASCII-Art library
3 *   Copyright (c) 2002, 2003 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 GNU Lesser General Public
8 *   License as published by the Free Software Foundation; either
9 *   version 2 of the License, or (at your option) any later version.
10 *
11 *   This library is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 *   Lesser General Public License for more details.
15 *
16 *   You should have received a copy of the GNU Lesser General Public
17 *   License along with this library; if not, write to the Free Software
18 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 *   02111-1307  USA
20 */
21
22/**  \file caca.c
23 *   \version \$Id: caca.c 251 2003-12-15 10:38:03Z sam $
24 *   \author Sam Hocevar <sam@zoy.org>
25 *   \brief Main \e libcaca functions
26 *
27 *   This file contains the main functions used by \e libcaca applications to
28 *   initialise the library, get the screen properties, set the framerate and
29 *   so on.
30 */
31
32#include "config.h"
33
34#if defined(USE_SLANG)
35#   include <slang.h>
36#elif defined(USE_NCURSES)
37#   include <curses.h>
38#elif defined(USE_CONIO)
39#   include <dos.h>
40#   include <conio.h>
41#elif defined(USE_X11)
42#   include <X11/Xlib.h>
43#else
44#   error "no graphics library detected"
45#endif
46
47#include <stdlib.h>
48#include <string.h>
49
50#include "caca.h"
51#include "caca_internals.h"
52
53static void caca_init_features(void);
54static void caca_init_terminal(void);
55
56#if defined(USE_NCURSES)
57static mmask_t oldmask;
58#endif
59
60int caca_init(void)
61{
62#if defined(USE_NCURSES)
63    mmask_t newmask;
64#endif
65
66    caca_init_features();
67    caca_init_terminal();
68
69#if defined(USE_SLANG)
70    /* Initialize slang library */
71    SLsig_block_signals();
72    SLtt_get_terminfo();
73
74    if(SLkp_init() == -1)
75    {
76        SLsig_unblock_signals();
77        return -1;
78    }
79
80    SLang_init_tty(-1, 0, 1);
81
82    if(SLsmg_init_smg() == -1)
83    {
84        SLsig_unblock_signals();
85        return -1;
86    }
87
88    SLsig_unblock_signals();
89
90    SLsmg_cls();
91    SLtt_set_cursor_visibility(0);
92    SLkp_define_keysym("\e[M", 1001);
93    SLtt_set_mouse_mode(1, 0);
94    SLsmg_refresh();
95
96    /* Disable scrolling so that hashmap scrolling optimization code
97     * does not cause ugly refreshes due to slow terminals */
98    SLtt_Term_Cannot_Scroll = 1;
99
100#elif defined(USE_NCURSES)
101    initscr();
102    keypad(stdscr, TRUE);
103    nonl();
104    cbreak();
105    noecho();
106    nodelay(stdscr, TRUE);
107    curs_set(0);
108
109    /* Activate mouse */
110    newmask = ALL_MOUSE_EVENTS;
111    mousemask(newmask, &oldmask);
112
113#elif defined(USE_CONIO)
114    _wscroll = 0;
115    _setcursortype(_NOCURSOR);
116    clrscr();
117
118#elif defined(USE_X11)
119    /* Nothing to do */
120
121#endif
122    if(_caca_init_graphics())
123        return -1;
124
125    return 0;
126}
127
128unsigned int caca_get_width(void)
129{
130    return _caca_width;
131}
132
133unsigned int caca_get_height(void)
134{
135    return _caca_height;
136}
137
138const char *caca_get_color_name(enum caca_color color)
139{
140    static const char *color_names[] =
141    {
142        "black",
143        "blue",
144        "green",
145        "cyan",
146        "red",
147        "magenta",
148        "brown",
149        "light gray",
150        "dark gray",
151        "light blue",
152        "light green",
153        "light cyan",
154        "light red",
155        "light magenta",
156        "yellow",
157        "white",
158    };
159
160    if(color < 0 || color > 15)
161        return "unknown";
162
163    return color_names[color];
164}
165
166enum caca_feature caca_get_feature(enum caca_feature feature)
167{
168    switch(feature)
169    {
170        case CACA_BACKGROUND:
171            return _caca_background;
172        case CACA_ANTIALIASING:
173            return _caca_antialiasing;
174        case CACA_DITHERING:
175            return _caca_dithering;
176
177        default:
178            return CACA_UNKNOWN_FEATURE;
179    }
180}
181
182void caca_set_feature(enum caca_feature feature)
183{
184    switch(feature)
185    {
186        case CACA_BACKGROUND:
187            feature = CACA_BACKGROUND_SOLID;
188        case CACA_BACKGROUND_BLACK:
189        case CACA_BACKGROUND_SOLID:
190            _caca_background = feature;
191            break;
192
193        case CACA_ANTIALIASING:
194            feature = CACA_ANTIALIASING_PREFILTER;
195        case CACA_ANTIALIASING_NONE:
196        case CACA_ANTIALIASING_PREFILTER:
197            _caca_antialiasing = feature;
198            break;
199
200        case CACA_DITHERING:
201            feature = CACA_DITHERING_ORDERED4;
202        case CACA_DITHERING_NONE:
203        case CACA_DITHERING_ORDERED2:
204        case CACA_DITHERING_ORDERED4:
205        case CACA_DITHERING_ORDERED8:
206        case CACA_DITHERING_RANDOM:
207            _caca_dithering = feature;
208            break;
209
210        case CACA_UNKNOWN_FEATURE:
211            break;
212    }
213}
214
215const char *caca_get_feature_name(enum caca_feature feature)
216{
217    switch(feature)
218    {
219        case CACA_BACKGROUND_BLACK: return "black background";
220        case CACA_BACKGROUND_SOLID: return "solid background";
221
222        case CACA_ANTIALIASING_NONE:      return "no antialiasing";
223        case CACA_ANTIALIASING_PREFILTER: return "prefilter antialiasing";
224
225        case CACA_DITHERING_NONE:     return "no dithering";
226        case CACA_DITHERING_ORDERED2: return "2x2 ordered dithering";
227        case CACA_DITHERING_ORDERED4: return "4x4 ordered dithering";
228        case CACA_DITHERING_ORDERED8: return "8x8 ordered dithering";
229        case CACA_DITHERING_RANDOM:   return "random dithering";
230
231        default: return "unknown";
232    }
233}
234
235void caca_end(void)
236{
237    _caca_end_graphics();
238
239#if defined(USE_SLANG)
240    SLtt_set_mouse_mode(0, 0);
241    SLtt_set_cursor_visibility(1);
242    SLang_reset_tty();
243    SLsmg_reset_smg();
244#elif defined(USE_NCURSES)
245    mousemask(oldmask, NULL);
246    curs_set(1);
247    endwin();
248#elif defined(USE_CONIO)
249    _wscroll = 1;
250    textcolor((enum COLORS)WHITE);
251    textbackground((enum COLORS)BLACK);
252    gotoxy(_caca_width, _caca_height);
253    cputs("\r\n");
254    _setcursortype(_NORMALCURSOR);
255#elif defined(USE_X11)
256    /* Nothing to do */
257#endif
258}
259
260static void caca_init_features(void)
261{
262    /* FIXME: if strcasecmp isn't available, use strcmp */
263#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
264    char *var;
265#endif
266
267    caca_set_feature(CACA_BACKGROUND);
268    caca_set_feature(CACA_ANTIALIASING);
269    caca_set_feature(CACA_DITHERING);
270
271#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
272    if((var = getenv("CACA_BACKGROUND")))
273    {
274        if(!strcasecmp("black", var))
275            caca_set_feature(CACA_BACKGROUND_BLACK);
276        else if(!strcasecmp("solid", var))
277            caca_set_feature(CACA_BACKGROUND_SOLID);
278    }
279
280    if((var = getenv("CACA_ANTIALIASING")))
281    {
282        if(!strcasecmp("none", var))
283            caca_set_feature(CACA_ANTIALIASING_NONE);
284        else if(!strcasecmp("prefilter", var))
285            caca_set_feature(CACA_ANTIALIASING_PREFILTER);
286    }
287
288    if((var = getenv("CACA_DITHERING")))
289    {
290        if(!strcasecmp("none", var))
291            caca_set_feature(CACA_DITHERING_NONE);
292        else if(!strcasecmp("ordered2", var))
293            caca_set_feature(CACA_DITHERING_ORDERED2);
294        else if(!strcasecmp("ordered4", var))
295            caca_set_feature(CACA_DITHERING_ORDERED4);
296        else if(!strcasecmp("ordered8", var))
297            caca_set_feature(CACA_DITHERING_ORDERED8);
298        else if(!strcasecmp("random", var))
299            caca_set_feature(CACA_DITHERING_RANDOM);
300    }
301#endif
302}
303
304static void caca_init_terminal(void)
305{
306#if defined(HAVE_GETENV) && defined(HAVE_PUTENV) \
307     && (defined(USE_SLANG) || defined(USE_NCURSES))
308    char *term, *colorterm, *other;
309
310    term = getenv("TERM");
311    colorterm = getenv("COLORTERM");
312
313    if(term && !strcmp(term, "xterm"))
314    {
315        /* If we are using gnome-terminal, it's really a 16 colour terminal */
316        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
317        {
318#if defined(USE_NCURSES)
319            SCREEN *screen;
320            screen = newterm("xterm-16color", stdout, stdin);
321            if(screen == NULL)
322                return;
323            endwin();
324#endif
325            (void)putenv("TERM=xterm-16color");
326            return;
327        }
328
329        /* Ditto if we are using Konsole */
330        other = getenv("KONSOLE_DCOP_SESSION");
331        if(other)
332        {
333#if defined(USE_NCURSES)
334            SCREEN *screen;
335            screen = newterm("xterm-16color", stdout, stdin);
336            if(screen == NULL)
337                return;
338            endwin();
339#endif
340            (void)putenv("TERM=xterm-16color");
341            return;
342        }
343    }
344#endif
345}
346
Note: See TracBrowser for help on using the repository browser.