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

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