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

Last change on this file since 227 was 227, checked in by Sam Hocevar, 19 years ago
  • src/caca.c src/graphics.c: + Moved graphics stuff from caca.c to graphics.c. + Added a few kludges: if gnome-terminal or konsole are detected, switch

to TERM=xterm-16color to benefit from extended colour palette.

  • Property svn:keywords set to Id
File size: 4.7 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 227 2003-11-27 22:29:13Z 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#elif defined(USE_NCURSES)
101    mmask_t newmask;
102
103    initscr();
104    keypad(stdscr, TRUE);
105    nonl();
106    cbreak();
107    noecho();
108    nodelay(stdscr, TRUE);
109    curs_set(0);
110
111    /* Activate mouse */
112    newmask = ALL_MOUSE_EVENTS;
113    mousemask(newmask, &oldmask);
114
115#elif defined(USE_CONIO)
116    _wscroll = 0;
117    _setcursortype(_NOCURSOR);
118    clrscr();
119
120#endif
121    if(_caca_init_graphics())
122        return -1;
123
124    return 0;
125}
126
127unsigned int caca_get_width(void)
128{
129#if defined(USE_SLANG)
130    return SLtt_Screen_Cols;
131#elif defined(USE_NCURSES)
132    return COLS;
133#elif defined(USE_CONIO)
134    return ti.screenwidth;
135#endif
136}
137
138unsigned int caca_get_height(void)
139{
140#if defined(USE_SLANG)
141    return SLtt_Screen_Rows;
142#elif defined(USE_NCURSES)
143    return LINES;
144#else
145    return ti.screenheight;
146#endif
147}
148
149const char *caca_get_color_name(unsigned int color)
150{
151    static const char *color_names[16] =
152    {
153        "black",
154        "blue",
155        "green",
156        "cyan",
157        "red",
158        "magenta",
159        "brown",
160        "light gray",
161        "dark gray",
162        "light blue",
163        "light green",
164        "light cyan",
165        "light red",
166        "light magenta",
167        "yellow",
168        "white",
169    };
170
171    if(color < 0 || color > 15)
172        return "unknown color";
173
174    return color_names[color];
175}
176
177void caca_end(void)
178{
179#if defined(USE_SLANG)
180    SLtt_set_mouse_mode(0, 0);
181    SLtt_set_cursor_visibility(1);
182    SLang_reset_tty();
183    SLsmg_reset_smg();
184#elif defined(USE_NCURSES)
185    mousemask(oldmask, NULL);
186    curs_set(1);
187    endwin();
188#elif defined(USE_CONIO)
189    _wscroll = 1;
190    textcolor((enum COLORS)WHITE);
191    textbackground((enum COLORS)BLACK);
192    gotoxy(caca_get_width(), caca_get_height());
193    cputs("\r\n");
194    _setcursortype(_NORMALCURSOR);
195#endif
196}
197
198static void caca_init_terminal(void)
199{
200#if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
201    char *term, *colorterm, *misc;
202
203    term = getenv("TERM");
204    colorterm = getenv("COLORTERM");
205
206    if(term && !strcmp(term, "xterm"))
207    {
208        /* If we are using gnome-terminal, it's really a 16 colour terminal */
209        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
210        {
211            (void)putenv("TERM=xterm-16color");
212            return;
213        }
214
215        /* Ditto if we are using Konsole */
216        misc = getenv("KONSOLE_DCOP_SESSION");
217        if(misc)
218        {
219            (void)putenv("TERM=xterm-16color");
220            return;
221        }
222    }
223#endif
224}
225
Note: See TracBrowser for help on using the repository browser.