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

Last change on this file since 199 was 199, checked in by Sam Hocevar, 19 years ago
  • src/io.c: + Renamed sdl_get_key() into sdl_get_event(). + Handle mouse clicks.
  • src/caca.c: + Activate the terminal's mouse handling if supported.
  • Property svn:keywords set to Id
File size: 7.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 *   $Id: caca.c 199 2003-11-19 17:32:10Z sam $
7 *
8 *   This library is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU Lesser General Public
10 *   License as published by the Free Software Foundation; either
11 *   version 2 of the License, or (at your option) any later version.
12 *
13 *   This library is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 *   Lesser General Public License for more details.
17 *
18 *   You should have received a copy of the GNU Lesser General Public
19 *   License along with this library; if not, write to the Free Software
20 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 *   02111-1307  USA
22 */
23
24#include "config.h"
25
26#if defined(USE_SLANG)
27#   include <slang.h>
28#elif defined(USE_NCURSES)
29#   include <curses.h>
30#elif defined(USE_CONIO)
31#   include <dos.h>
32#   include <conio.h>
33#   if defined(SCREENUPDATE_IN_PC_H)
34#       include <pc.h>
35#   endif
36#else
37#   error "no graphics library detected"
38#endif
39
40#include <stdlib.h>
41#include <unistd.h>
42#include <string.h>
43#include <sys/time.h>
44#include <time.h>
45
46#include "caca.h"
47#include "caca_internals.h"
48
49static unsigned int _caca_delay;
50static unsigned int _caca_rendertime;
51char *_caca_empty_line;
52char *_caca_scratch_line;
53
54#if defined(USE_NCURSES)
55int _caca_attr[16];
56#endif
57
58#if defined(USE_CONIO)
59static struct text_info ti;
60char *_caca_screen;
61#endif
62
63int caca_init(void)
64{
65#if defined(USE_SLANG)
66    static char *slang_colors[16] =
67    {
68        "black",
69        "blue",
70        "green",
71        "cyan",
72        "red",
73        "magenta",
74        "brown",
75        "lightgray",
76        "gray",
77        "brightblue",
78        "brightgreen",
79        "brightcyan",
80        "brightred",
81        "brightmagenta",
82        "yellow",
83        "white",
84    };
85
86    int i;
87
88    /* Initialize slang library */
89    SLsig_block_signals();
90    SLtt_get_terminfo();
91
92    if(SLkp_init() == -1)
93    {
94        SLsig_unblock_signals();
95        return -1;
96    }
97
98    SLang_init_tty(-1, 0, 1);
99
100    SLkp_define_keysym("\e[M", 1001);
101
102    if(SLsmg_init_smg() == -1)
103    {
104        SLsig_unblock_signals();
105        return -1;
106    }
107
108    SLsig_unblock_signals();
109
110    SLsmg_cls();
111    SLtt_set_cursor_visibility(0);
112    SLtt_set_mouse_mode (1, 0);
113    SLsmg_refresh();
114
115    for(i = 0; i < 16; i++)
116        SLtt_set_color(i + 1, NULL, slang_colors[i], "black");
117
118#elif defined(USE_NCURSES)
119    int i;
120
121    initscr();
122    keypad(stdscr, TRUE);
123    nonl();
124    cbreak();
125    noecho();
126    nodelay(stdscr, TRUE);
127    curs_set(0);
128
129    start_color();
130
131    init_pair(1 + CACA_COLOR_BLACK,        COLOR_BLACK,   COLOR_BLACK);
132    init_pair(1 + CACA_COLOR_BLUE,         COLOR_BLUE,    COLOR_BLACK);
133    init_pair(1 + CACA_COLOR_GREEN,        COLOR_GREEN,   COLOR_BLACK);
134    init_pair(1 + CACA_COLOR_CYAN,         COLOR_CYAN,    COLOR_BLACK);
135    init_pair(1 + CACA_COLOR_RED,          COLOR_RED,     COLOR_BLACK);
136    init_pair(1 + CACA_COLOR_MAGENTA,      COLOR_MAGENTA, COLOR_BLACK);
137    init_pair(1 + CACA_COLOR_BROWN,        COLOR_YELLOW,  COLOR_BLACK);
138    init_pair(1 + CACA_COLOR_LIGHTGRAY,    COLOR_WHITE,   COLOR_BLACK);
139    init_pair(1 + CACA_COLOR_DARKGRAY,     COLOR_BLACK,   COLOR_BLACK);
140    init_pair(1 + CACA_COLOR_LIGHTBLUE,    COLOR_BLUE,    COLOR_BLACK);
141    init_pair(1 + CACA_COLOR_LIGHTGREEN,   COLOR_GREEN,   COLOR_BLACK);
142    init_pair(1 + CACA_COLOR_LIGHTCYAN,    COLOR_CYAN,    COLOR_BLACK);
143    init_pair(1 + CACA_COLOR_LIGHTRED,     COLOR_RED,     COLOR_BLACK);
144    init_pair(1 + CACA_COLOR_LIGHTMAGENTA, COLOR_MAGENTA, COLOR_BLACK);
145    init_pair(1 + CACA_COLOR_YELLOW,       COLOR_YELLOW,  COLOR_BLACK);
146    init_pair(1 + CACA_COLOR_WHITE,        COLOR_WHITE,   COLOR_BLACK);
147
148    for(i = 0; i < 8; i++)
149    {
150        _caca_attr[i] = COLOR_PAIR(1 + i);
151        _caca_attr[i + 8] = A_BOLD | COLOR_PAIR(1 + i);
152    }
153
154#elif defined(USE_CONIO)
155    gettextinfo(&ti);
156    _caca_screen = malloc(2 * ti.screenwidth * ti.screenheight);
157    if(_caca_screen == NULL)
158        return -1;
159    _wscroll = 0;
160    _setcursortype(_NOCURSOR);
161    clrscr();
162#   if defined(SCREENUPDATE_IN_PC_H)
163    ScreenRetrieve(_caca_screen);
164#   else
165    /* FIXME */
166#   endif
167
168#endif
169    _caca_empty_line = malloc(caca_get_width() + 1);
170    memset(_caca_empty_line, ' ', caca_get_width());
171    _caca_empty_line[caca_get_width()] = '\0';
172
173    _caca_scratch_line = malloc(caca_get_width() + 1);
174
175    _caca_delay = 0;
176    _caca_rendertime = 0;
177
178    return 0;
179}
180
181unsigned int caca_get_width(void)
182{
183#if defined(USE_SLANG)
184    return SLtt_Screen_Cols;
185#elif defined(USE_NCURSES)
186    return COLS;
187#elif defined(USE_CONIO)
188    return ti.screenwidth;
189#endif
190}
191
192unsigned int caca_get_height(void)
193{
194#if defined(USE_SLANG)
195    return SLtt_Screen_Rows;
196#elif defined(USE_NCURSES)
197    return LINES;
198#else
199    return ti.screenheight;
200#endif
201}
202
203void caca_set_delay(unsigned int usec)
204{
205    _caca_delay = usec;
206}
207
208unsigned int caca_get_rendertime(void)
209{
210    return _caca_rendertime;
211}
212
213const char *caca_get_color_name(unsigned int color)
214{
215    static const char *color_names[16] =
216    {
217        "black",
218        "blue",
219        "green",
220        "cyan",
221        "red",
222        "magenta",
223        "brown",
224        "light gray",
225        "dark gray",
226        "light blue",
227        "light green",
228        "light cyan",
229        "light red",
230        "light magenta",
231        "yellow",
232        "white",
233    };
234
235    if(color < 0 || color > 15)
236        return "unknown color";
237
238    return color_names[color];
239}
240
241static unsigned int _caca_getticks(void)
242{
243    static unsigned int last_sec = 0, last_usec = 0;
244
245    struct timeval tv;
246    unsigned int ticks = 0;
247
248    gettimeofday(&tv, NULL);
249
250    if(last_sec != 0)
251    {
252        ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec);
253    }
254
255    last_sec = tv.tv_sec;
256    last_usec = tv.tv_usec;
257
258    return ticks;
259}
260
261void caca_refresh(void)
262{
263#define IDLE_USEC 10000
264    static int lastticks = 0;
265    int ticks = lastticks + _caca_getticks();
266
267#if defined(USE_SLANG)
268    SLsmg_refresh();
269#elif defined(USE_NCURSES)
270    refresh();
271#elif defined(USE_CONIO)
272#   if defined(SCREENUPDATE_IN_PC_H)
273    ScreenUpdate(_caca_screen);
274#   else
275    /* FIXME */
276#   endif
277#endif
278
279    /* Wait until _caca_delay + time of last call */
280    ticks += _caca_getticks();
281    for(; ticks + IDLE_USEC < (int)_caca_delay; ticks += _caca_getticks())
282        usleep(IDLE_USEC);
283
284    /* Update the sliding mean of the render time */
285    _caca_rendertime = (7 * _caca_rendertime + ticks) / 8;
286
287    lastticks = ticks - _caca_delay;
288
289    /* If we drifted too much, it's bad, bad, bad. */
290    if(lastticks > (int)_caca_delay)
291        lastticks = 0;
292}
293
294void caca_end(void)
295{
296#if defined(USE_SLANG)
297    SLtt_set_mouse_mode(0, 0);
298    SLtt_set_cursor_visibility(1);
299    SLang_reset_tty();
300    SLsmg_reset_smg();
301#elif defined(USE_NCURSES)
302    curs_set(1);
303    endwin();
304#elif defined(USE_CONIO)
305    _wscroll = 1;
306    textcolor((enum COLORS)WHITE);
307    textbackground((enum COLORS)BLACK);
308    gotoxy(caca_get_width(), caca_get_height());
309    cputs("\r\n");
310    _setcursortype(_NORMALCURSOR);
311#endif
312}
313
Note: See TracBrowser for help on using the repository browser.