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

Last change on this file since 250 was 250, checked in by Sam Hocevar, 19 years ago
  • configure.ac debian/changelog: + Prepare for 0.3 release. + Check for strcasecmp().
  • src/caca.c: + Honor CACA_BACKGROUND, CACA_DITHERING and CACA_ANTIALIASING environment

variables.

  • Property svn:keywords set to Id
File size: 8.2 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 250 2003-12-12 16:19:44Z 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#else
42#   error "no graphics library detected"
43#endif
44
45#include <stdlib.h>
46#include <string.h>
47
48#include "caca.h"
49#include "caca_internals.h"
50
51static void caca_init_features(void);
52static void caca_init_terminal(void);
53
54#if defined(USE_NCURSES)
55static mmask_t oldmask;
56#endif
57
58int caca_init(void)
59{
60#if defined(USE_NCURSES)
61    mmask_t newmask;
62#endif
63
64    caca_init_features();
65    caca_init_terminal();
66
67#if defined(USE_SLANG)
68    /* Initialize slang library */
69    SLsig_block_signals();
70    SLtt_get_terminfo();
71
72    if(SLkp_init() == -1)
73    {
74        SLsig_unblock_signals();
75        return -1;
76    }
77
78    SLang_init_tty(-1, 0, 1);
79
80    if(SLsmg_init_smg() == -1)
81    {
82        SLsig_unblock_signals();
83        return -1;
84    }
85
86    SLsig_unblock_signals();
87
88    SLsmg_cls();
89    SLtt_set_cursor_visibility(0);
90    SLkp_define_keysym("\e[M", 1001);
91    SLtt_set_mouse_mode(1, 0);
92    SLsmg_refresh();
93
94    /* Disable scrolling so that hashmap scrolling optimization code
95     * does not cause ugly refreshes due to slow terminals */
96    SLtt_Term_Cannot_Scroll = 1;
97
98#elif defined(USE_NCURSES)
99    initscr();
100    keypad(stdscr, TRUE);
101    nonl();
102    cbreak();
103    noecho();
104    nodelay(stdscr, TRUE);
105    curs_set(0);
106
107    /* Activate mouse */
108    newmask = ALL_MOUSE_EVENTS;
109    mousemask(newmask, &oldmask);
110
111#elif defined(USE_CONIO)
112    _wscroll = 0;
113    _setcursortype(_NOCURSOR);
114    clrscr();
115
116#endif
117    if(_caca_init_graphics())
118        return -1;
119
120    return 0;
121}
122
123unsigned int caca_get_width(void)
124{
125    return _caca_width;
126}
127
128unsigned int caca_get_height(void)
129{
130    return _caca_height;
131}
132
133const char *caca_get_color_name(enum caca_color color)
134{
135    static const char *color_names[] =
136    {
137        "black",
138        "blue",
139        "green",
140        "cyan",
141        "red",
142        "magenta",
143        "brown",
144        "light gray",
145        "dark gray",
146        "light blue",
147        "light green",
148        "light cyan",
149        "light red",
150        "light magenta",
151        "yellow",
152        "white",
153    };
154
155    if(color < 0 || color > 15)
156        return "unknown";
157
158    return color_names[color];
159}
160
161enum caca_feature caca_get_feature(enum caca_feature feature)
162{
163    switch(feature)
164    {
165        case CACA_BACKGROUND:
166            return _caca_background;
167        case CACA_ANTIALIASING:
168            return _caca_antialiasing;
169        case CACA_DITHERING:
170            return _caca_dithering;
171
172        default:
173            return CACA_UNKNOWN_FEATURE;
174    }
175}
176
177void caca_set_feature(enum caca_feature feature)
178{
179    switch(feature)
180    {
181        case CACA_BACKGROUND:
182            feature = CACA_BACKGROUND_SOLID;
183        case CACA_BACKGROUND_BLACK:
184        case CACA_BACKGROUND_SOLID:
185            _caca_background = feature;
186            break;
187
188        case CACA_ANTIALIASING:
189            feature = CACA_ANTIALIASING_PREFILTER;
190        case CACA_ANTIALIASING_NONE:
191        case CACA_ANTIALIASING_PREFILTER:
192            _caca_antialiasing = feature;
193            break;
194
195        case CACA_DITHERING:
196            feature = CACA_DITHERING_ORDERED4;
197        case CACA_DITHERING_NONE:
198        case CACA_DITHERING_ORDERED2:
199        case CACA_DITHERING_ORDERED4:
200        case CACA_DITHERING_ORDERED8:
201        case CACA_DITHERING_RANDOM:
202            _caca_dithering = feature;
203            break;
204
205        case CACA_UNKNOWN_FEATURE:
206            break;
207    }
208}
209
210const char *caca_get_feature_name(enum caca_feature feature)
211{
212    switch(feature)
213    {
214        case CACA_BACKGROUND_BLACK: return "black background";
215        case CACA_BACKGROUND_SOLID: return "solid background";
216
217        case CACA_ANTIALIASING_NONE:      return "no antialiasing";
218        case CACA_ANTIALIASING_PREFILTER: return "prefilter antialiasing";
219
220        case CACA_DITHERING_NONE:     return "no dithering";
221        case CACA_DITHERING_ORDERED2: return "2x2 ordered dithering";
222        case CACA_DITHERING_ORDERED4: return "4x4 ordered dithering";
223        case CACA_DITHERING_ORDERED8: return "8x8 ordered dithering";
224        case CACA_DITHERING_RANDOM:   return "random dithering";
225
226        default: return "unknown";
227    }
228}
229
230void caca_end(void)
231{
232#if defined(USE_SLANG)
233    SLtt_set_mouse_mode(0, 0);
234    SLtt_set_cursor_visibility(1);
235    SLang_reset_tty();
236    SLsmg_reset_smg();
237#elif defined(USE_NCURSES)
238    mousemask(oldmask, NULL);
239    curs_set(1);
240    endwin();
241#elif defined(USE_CONIO)
242    _wscroll = 1;
243    textcolor((enum COLORS)WHITE);
244    textbackground((enum COLORS)BLACK);
245    gotoxy(_caca_width, _caca_height);
246    cputs("\r\n");
247    _setcursortype(_NORMALCURSOR);
248#endif
249}
250
251static void caca_init_features(void)
252{
253    /* FIXME: if strcasecmp isn't available, use strcmp */
254#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
255    char *var;
256#endif
257
258    caca_set_feature(CACA_BACKGROUND);
259    caca_set_feature(CACA_ANTIALIASING);
260    caca_set_feature(CACA_DITHERING);
261
262#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
263    if((var = getenv("CACA_BACKGROUND")))
264    {
265        if(!strcasecmp("black", var))
266            caca_set_feature(CACA_BACKGROUND_BLACK);
267        else if(!strcasecmp("solid", var))
268            caca_set_feature(CACA_BACKGROUND_SOLID);
269    }
270
271    if((var = getenv("CACA_ANTIALIASING")))
272    {
273        if(!strcasecmp("none", var))
274            caca_set_feature(CACA_ANTIALIASING_NONE);
275        else if(!strcasecmp("prefilter", var))
276            caca_set_feature(CACA_ANTIALIASING_PREFILTER);
277    }
278
279    if((var = getenv("CACA_DITHERING")))
280    {
281        if(!strcasecmp("none", var))
282            caca_set_feature(CACA_DITHERING_NONE);
283        else if(!strcasecmp("ordered2", var))
284            caca_set_feature(CACA_DITHERING_ORDERED2);
285        else if(!strcasecmp("ordered4", var))
286            caca_set_feature(CACA_DITHERING_ORDERED4);
287        else if(!strcasecmp("ordered8", var))
288            caca_set_feature(CACA_DITHERING_ORDERED8);
289        else if(!strcasecmp("random", var))
290            caca_set_feature(CACA_DITHERING_RANDOM);
291    }
292#endif
293}
294
295static void caca_init_terminal(void)
296{
297#if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
298    char *term, *colorterm, *other;
299
300    term = getenv("TERM");
301    colorterm = getenv("COLORTERM");
302
303    if(term && !strcmp(term, "xterm"))
304    {
305        /* If we are using gnome-terminal, it's really a 16 colour terminal */
306        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
307        {
308#if defined(USE_NCURSES)
309            SCREEN *screen;
310            screen = newterm("xterm-16color", stdout, stdin);
311            if(screen == NULL)
312                return;
313            endwin();
314#endif
315            (void)putenv("TERM=xterm-16color");
316            return;
317        }
318
319        /* Ditto if we are using Konsole */
320        other = getenv("KONSOLE_DCOP_SESSION");
321        if(other)
322        {
323#if defined(USE_NCURSES)
324            SCREEN *screen;
325            screen = newterm("xterm-16color", stdout, stdin);
326            if(screen == NULL)
327                return;
328            endwin();
329#endif
330            (void)putenv("TERM=xterm-16color");
331            return;
332        }
333    }
334#endif
335}
336
Note: See TracBrowser for help on using the repository browser.