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 200 2003-11-19 17:49:43Z 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 | |
---|
49 | static unsigned int _caca_delay; |
---|
50 | static unsigned int _caca_rendertime; |
---|
51 | char *_caca_empty_line; |
---|
52 | char *_caca_scratch_line; |
---|
53 | |
---|
54 | #if defined(USE_NCURSES) |
---|
55 | int _caca_attr[16]; |
---|
56 | #endif |
---|
57 | |
---|
58 | #if defined(USE_CONIO) |
---|
59 | static struct text_info ti; |
---|
60 | char *_caca_screen; |
---|
61 | #endif |
---|
62 | |
---|
63 | int 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 | if(SLsmg_init_smg() == -1) |
---|
101 | { |
---|
102 | SLsig_unblock_signals(); |
---|
103 | return -1; |
---|
104 | } |
---|
105 | |
---|
106 | SLsig_unblock_signals(); |
---|
107 | |
---|
108 | SLsmg_cls(); |
---|
109 | SLtt_set_cursor_visibility(0); |
---|
110 | SLkp_define_keysym("\e[M", 1001); |
---|
111 | SLtt_set_mouse_mode(1, 0); |
---|
112 | SLsmg_refresh(); |
---|
113 | |
---|
114 | for(i = 0; i < 16; i++) |
---|
115 | SLtt_set_color(i + 1, NULL, slang_colors[i], "black"); |
---|
116 | |
---|
117 | #elif defined(USE_NCURSES) |
---|
118 | int i; |
---|
119 | |
---|
120 | initscr(); |
---|
121 | keypad(stdscr, TRUE); |
---|
122 | nonl(); |
---|
123 | cbreak(); |
---|
124 | noecho(); |
---|
125 | nodelay(stdscr, TRUE); |
---|
126 | curs_set(0); |
---|
127 | |
---|
128 | start_color(); |
---|
129 | |
---|
130 | init_pair(1 + CACA_COLOR_BLACK, COLOR_BLACK, COLOR_BLACK); |
---|
131 | init_pair(1 + CACA_COLOR_BLUE, COLOR_BLUE, COLOR_BLACK); |
---|
132 | init_pair(1 + CACA_COLOR_GREEN, COLOR_GREEN, COLOR_BLACK); |
---|
133 | init_pair(1 + CACA_COLOR_CYAN, COLOR_CYAN, COLOR_BLACK); |
---|
134 | init_pair(1 + CACA_COLOR_RED, COLOR_RED, COLOR_BLACK); |
---|
135 | init_pair(1 + CACA_COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK); |
---|
136 | init_pair(1 + CACA_COLOR_BROWN, COLOR_YELLOW, COLOR_BLACK); |
---|
137 | init_pair(1 + CACA_COLOR_LIGHTGRAY, COLOR_WHITE, COLOR_BLACK); |
---|
138 | init_pair(1 + CACA_COLOR_DARKGRAY, COLOR_BLACK, COLOR_BLACK); |
---|
139 | init_pair(1 + CACA_COLOR_LIGHTBLUE, COLOR_BLUE, COLOR_BLACK); |
---|
140 | init_pair(1 + CACA_COLOR_LIGHTGREEN, COLOR_GREEN, COLOR_BLACK); |
---|
141 | init_pair(1 + CACA_COLOR_LIGHTCYAN, COLOR_CYAN, COLOR_BLACK); |
---|
142 | init_pair(1 + CACA_COLOR_LIGHTRED, COLOR_RED, COLOR_BLACK); |
---|
143 | init_pair(1 + CACA_COLOR_LIGHTMAGENTA, COLOR_MAGENTA, COLOR_BLACK); |
---|
144 | init_pair(1 + CACA_COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK); |
---|
145 | init_pair(1 + CACA_COLOR_WHITE, COLOR_WHITE, COLOR_BLACK); |
---|
146 | |
---|
147 | for(i = 0; i < 8; i++) |
---|
148 | { |
---|
149 | _caca_attr[i] = COLOR_PAIR(1 + i); |
---|
150 | _caca_attr[i + 8] = A_BOLD | COLOR_PAIR(1 + i); |
---|
151 | } |
---|
152 | |
---|
153 | #elif defined(USE_CONIO) |
---|
154 | gettextinfo(&ti); |
---|
155 | _caca_screen = malloc(2 * ti.screenwidth * ti.screenheight); |
---|
156 | if(_caca_screen == NULL) |
---|
157 | return -1; |
---|
158 | _wscroll = 0; |
---|
159 | _setcursortype(_NOCURSOR); |
---|
160 | clrscr(); |
---|
161 | # if defined(SCREENUPDATE_IN_PC_H) |
---|
162 | ScreenRetrieve(_caca_screen); |
---|
163 | # else |
---|
164 | /* FIXME */ |
---|
165 | # endif |
---|
166 | |
---|
167 | #endif |
---|
168 | _caca_empty_line = malloc(caca_get_width() + 1); |
---|
169 | memset(_caca_empty_line, ' ', caca_get_width()); |
---|
170 | _caca_empty_line[caca_get_width()] = '\0'; |
---|
171 | |
---|
172 | _caca_scratch_line = malloc(caca_get_width() + 1); |
---|
173 | |
---|
174 | _caca_delay = 0; |
---|
175 | _caca_rendertime = 0; |
---|
176 | |
---|
177 | return 0; |
---|
178 | } |
---|
179 | |
---|
180 | unsigned int caca_get_width(void) |
---|
181 | { |
---|
182 | #if defined(USE_SLANG) |
---|
183 | return SLtt_Screen_Cols; |
---|
184 | #elif defined(USE_NCURSES) |
---|
185 | return COLS; |
---|
186 | #elif defined(USE_CONIO) |
---|
187 | return ti.screenwidth; |
---|
188 | #endif |
---|
189 | } |
---|
190 | |
---|
191 | unsigned int caca_get_height(void) |
---|
192 | { |
---|
193 | #if defined(USE_SLANG) |
---|
194 | return SLtt_Screen_Rows; |
---|
195 | #elif defined(USE_NCURSES) |
---|
196 | return LINES; |
---|
197 | #else |
---|
198 | return ti.screenheight; |
---|
199 | #endif |
---|
200 | } |
---|
201 | |
---|
202 | void caca_set_delay(unsigned int usec) |
---|
203 | { |
---|
204 | _caca_delay = usec; |
---|
205 | } |
---|
206 | |
---|
207 | unsigned int caca_get_rendertime(void) |
---|
208 | { |
---|
209 | return _caca_rendertime; |
---|
210 | } |
---|
211 | |
---|
212 | const char *caca_get_color_name(unsigned int color) |
---|
213 | { |
---|
214 | static const char *color_names[16] = |
---|
215 | { |
---|
216 | "black", |
---|
217 | "blue", |
---|
218 | "green", |
---|
219 | "cyan", |
---|
220 | "red", |
---|
221 | "magenta", |
---|
222 | "brown", |
---|
223 | "light gray", |
---|
224 | "dark gray", |
---|
225 | "light blue", |
---|
226 | "light green", |
---|
227 | "light cyan", |
---|
228 | "light red", |
---|
229 | "light magenta", |
---|
230 | "yellow", |
---|
231 | "white", |
---|
232 | }; |
---|
233 | |
---|
234 | if(color < 0 || color > 15) |
---|
235 | return "unknown color"; |
---|
236 | |
---|
237 | return color_names[color]; |
---|
238 | } |
---|
239 | |
---|
240 | static unsigned int _caca_getticks(void) |
---|
241 | { |
---|
242 | static unsigned int last_sec = 0, last_usec = 0; |
---|
243 | |
---|
244 | struct timeval tv; |
---|
245 | unsigned int ticks = 0; |
---|
246 | |
---|
247 | gettimeofday(&tv, NULL); |
---|
248 | |
---|
249 | if(last_sec != 0) |
---|
250 | { |
---|
251 | ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec); |
---|
252 | } |
---|
253 | |
---|
254 | last_sec = tv.tv_sec; |
---|
255 | last_usec = tv.tv_usec; |
---|
256 | |
---|
257 | return ticks; |
---|
258 | } |
---|
259 | |
---|
260 | void caca_refresh(void) |
---|
261 | { |
---|
262 | #define IDLE_USEC 10000 |
---|
263 | static int lastticks = 0; |
---|
264 | int ticks = lastticks + _caca_getticks(); |
---|
265 | |
---|
266 | #if defined(USE_SLANG) |
---|
267 | SLsmg_refresh(); |
---|
268 | #elif defined(USE_NCURSES) |
---|
269 | refresh(); |
---|
270 | #elif defined(USE_CONIO) |
---|
271 | # if defined(SCREENUPDATE_IN_PC_H) |
---|
272 | ScreenUpdate(_caca_screen); |
---|
273 | # else |
---|
274 | /* FIXME */ |
---|
275 | # endif |
---|
276 | #endif |
---|
277 | |
---|
278 | /* Wait until _caca_delay + time of last call */ |
---|
279 | ticks += _caca_getticks(); |
---|
280 | for(; ticks + IDLE_USEC < (int)_caca_delay; ticks += _caca_getticks()) |
---|
281 | usleep(IDLE_USEC); |
---|
282 | |
---|
283 | /* Update the sliding mean of the render time */ |
---|
284 | _caca_rendertime = (7 * _caca_rendertime + ticks) / 8; |
---|
285 | |
---|
286 | lastticks = ticks - _caca_delay; |
---|
287 | |
---|
288 | /* If we drifted too much, it's bad, bad, bad. */ |
---|
289 | if(lastticks > (int)_caca_delay) |
---|
290 | lastticks = 0; |
---|
291 | } |
---|
292 | |
---|
293 | void caca_end(void) |
---|
294 | { |
---|
295 | #if defined(USE_SLANG) |
---|
296 | SLtt_set_mouse_mode(0, 0); |
---|
297 | SLtt_set_cursor_visibility(1); |
---|
298 | SLang_reset_tty(); |
---|
299 | SLsmg_reset_smg(); |
---|
300 | #elif defined(USE_NCURSES) |
---|
301 | curs_set(1); |
---|
302 | endwin(); |
---|
303 | #elif defined(USE_CONIO) |
---|
304 | _wscroll = 1; |
---|
305 | textcolor((enum COLORS)WHITE); |
---|
306 | textbackground((enum COLORS)BLACK); |
---|
307 | gotoxy(caca_get_width(), caca_get_height()); |
---|
308 | cputs("\r\n"); |
---|
309 | _setcursortype(_NORMALCURSOR); |
---|
310 | #endif |
---|
311 | } |
---|
312 | |
---|