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

Last change on this file since 231 was 231, checked in by Sam Hocevar, 19 years ago
  • configure.ac: + Default to ncurses, not slang, because slang only has 128 colour pairs.
  • src/caca.c: + Disable scrolling to avoid hashmap scrolling optimization code.
  • src/graphics.c: + Swap fg and bg in the colour pair indexing, so that bg is always

right.

+ Disable alt charset support to exploit my patched slang.

  • Property svn:keywords set to Id
File size: 4.9 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 231 2003-11-29 14:41:37Z 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#   if defined(SCREENUPDATE_IN_PC_H)
42#       include <pc.h>
43#   endif
44#else
45#   error "no graphics library detected"
46#endif
47
48#include <stdlib.h>
49#include <string.h>
50
51#include "caca.h"
52#include "caca_internals.h"
53
54static void caca_init_terminal(void);
55
56char *_caca_empty_line;
57char *_caca_scratch_line;
58
59#if defined(USE_NCURSES)
60static mmask_t oldmask;
61int _caca_attr[16*16];
62#endif
63
64#if defined(USE_CONIO)
65static struct text_info ti;
66char *_caca_screen;
67#endif
68
69int caca_init(void)
70{
71    caca_init_terminal();
72
73#if defined(USE_SLANG)
74    /* Initialize slang library */
75    SLsig_block_signals();
76    SLtt_get_terminfo();
77
78    if(SLkp_init() == -1)
79    {
80        SLsig_unblock_signals();
81        return -1;
82    }
83
84    SLang_init_tty(-1, 0, 1);
85
86    if(SLsmg_init_smg() == -1)
87    {
88        SLsig_unblock_signals();
89        return -1;
90    }
91
92    SLsig_unblock_signals();
93
94    SLsmg_cls();
95    SLtt_set_cursor_visibility(0);
96    SLkp_define_keysym("\e[M", 1001);
97    SLtt_set_mouse_mode(1, 0);
98    SLsmg_refresh();
99
100    /* Disable scrolling so that hashmap scrolling optimization code
101     * does not cause ugly refreshes due to slow terminals */
102    SLtt_Term_Cannot_Scroll = 1;
103
104#elif defined(USE_NCURSES)
105    mmask_t newmask;
106
107    initscr();
108    keypad(stdscr, TRUE);
109    nonl();
110    cbreak();
111    noecho();
112    nodelay(stdscr, TRUE);
113    curs_set(0);
114
115    /* Activate mouse */
116    newmask = ALL_MOUSE_EVENTS;
117    mousemask(newmask, &oldmask);
118
119#elif defined(USE_CONIO)
120    _wscroll = 0;
121    _setcursortype(_NOCURSOR);
122    clrscr();
123
124#endif
125    if(_caca_init_graphics())
126        return -1;
127
128    return 0;
129}
130
131unsigned int caca_get_width(void)
132{
133#if defined(USE_SLANG)
134    return SLtt_Screen_Cols;
135#elif defined(USE_NCURSES)
136    return COLS;
137#elif defined(USE_CONIO)
138    return ti.screenwidth;
139#endif
140}
141
142unsigned int caca_get_height(void)
143{
144#if defined(USE_SLANG)
145    return SLtt_Screen_Rows;
146#elif defined(USE_NCURSES)
147    return LINES;
148#else
149    return ti.screenheight;
150#endif
151}
152
153const char *caca_get_color_name(unsigned int color)
154{
155    static const char *color_names[16] =
156    {
157        "black",
158        "blue",
159        "green",
160        "cyan",
161        "red",
162        "magenta",
163        "brown",
164        "light gray",
165        "dark gray",
166        "light blue",
167        "light green",
168        "light cyan",
169        "light red",
170        "light magenta",
171        "yellow",
172        "white",
173    };
174
175    if(color < 0 || color > 15)
176        return "unknown color";
177
178    return color_names[color];
179}
180
181void caca_end(void)
182{
183#if defined(USE_SLANG)
184    SLtt_set_mouse_mode(0, 0);
185    SLtt_set_cursor_visibility(1);
186    SLang_reset_tty();
187    SLsmg_reset_smg();
188#elif defined(USE_NCURSES)
189    mousemask(oldmask, NULL);
190    curs_set(1);
191    endwin();
192#elif defined(USE_CONIO)
193    _wscroll = 1;
194    textcolor((enum COLORS)WHITE);
195    textbackground((enum COLORS)BLACK);
196    gotoxy(caca_get_width(), caca_get_height());
197    cputs("\r\n");
198    _setcursortype(_NORMALCURSOR);
199#endif
200}
201
202static void caca_init_terminal(void)
203{
204#if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
205    char *term, *colorterm, *misc;
206
207    term = getenv("TERM");
208    colorterm = getenv("COLORTERM");
209
210    if(term && !strcmp(term, "xterm"))
211    {
212        /* If we are using gnome-terminal, it's really a 16 colour terminal */
213        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
214        {
215            (void)putenv("TERM=xterm-16color");
216            return;
217        }
218
219        /* Ditto if we are using Konsole */
220        misc = getenv("KONSOLE_DCOP_SESSION");
221        if(misc)
222        {
223            (void)putenv("TERM=xterm-16color");
224            return;
225        }
226    }
227#endif
228}
229
Note: See TracBrowser for help on using the repository browser.