1 | /* |
---|
2 | * libee ASCII-Art library |
---|
3 | * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: ee.c 181 2003-11-15 12:42:38Z sam $ |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or modify |
---|
9 | * it under the terms of the GNU General Public License as published by |
---|
10 | * the Free Software Foundation; either version 2 of the License, or |
---|
11 | * (at your option) any later version. |
---|
12 | * |
---|
13 | * This program 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 |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "config.h" |
---|
24 | |
---|
25 | #if defined(USE_SLANG) |
---|
26 | # include <slang.h> |
---|
27 | #elif defined(USE_NCURSES) |
---|
28 | # include <curses.h> |
---|
29 | #elif defined(USE_CONIO) |
---|
30 | # include <dos.h> |
---|
31 | # include <conio.h> |
---|
32 | # if defined(SCREENUPDATE_IN_PC_H) |
---|
33 | # include <pc.h> |
---|
34 | # endif |
---|
35 | #else |
---|
36 | # error "no graphics library detected" |
---|
37 | #endif |
---|
38 | |
---|
39 | #include <stdlib.h> |
---|
40 | #include <unistd.h> |
---|
41 | #include <string.h> |
---|
42 | #include <sys/time.h> |
---|
43 | #include <time.h> |
---|
44 | |
---|
45 | #include "ee.h" |
---|
46 | #include "ee_internals.h" |
---|
47 | |
---|
48 | static unsigned int _ee_delay; |
---|
49 | static unsigned int _ee_rendertime; |
---|
50 | char *_ee_empty_line; |
---|
51 | char *_ee_scratch_line; |
---|
52 | |
---|
53 | #if defined(USE_NCURSES) |
---|
54 | int _ee_attr[16]; |
---|
55 | #endif |
---|
56 | |
---|
57 | #if defined(USE_CONIO) |
---|
58 | static struct text_info ti; |
---|
59 | char *_ee_screen; |
---|
60 | #endif |
---|
61 | |
---|
62 | int ee_init(void) |
---|
63 | { |
---|
64 | #if defined(USE_SLANG) |
---|
65 | static char *slang_colors[16] = |
---|
66 | { |
---|
67 | "black", |
---|
68 | "blue", |
---|
69 | "green", |
---|
70 | "cyan", |
---|
71 | "red", |
---|
72 | "magenta", |
---|
73 | "brown", |
---|
74 | "lightgray", |
---|
75 | "gray", |
---|
76 | "brightblue", |
---|
77 | "brightgreen", |
---|
78 | "brightcyan", |
---|
79 | "brightred", |
---|
80 | "brightmagenta", |
---|
81 | "yellow", |
---|
82 | "white", |
---|
83 | }; |
---|
84 | |
---|
85 | int i; |
---|
86 | |
---|
87 | /* Initialize slang library */ |
---|
88 | SLsig_block_signals(); |
---|
89 | SLtt_get_terminfo(); |
---|
90 | |
---|
91 | if(SLkp_init() == -1) |
---|
92 | { |
---|
93 | SLsig_unblock_signals(); |
---|
94 | return -1; |
---|
95 | } |
---|
96 | |
---|
97 | SLang_init_tty(-1, 0, 1); |
---|
98 | |
---|
99 | if(SLsmg_init_smg() == -1) |
---|
100 | { |
---|
101 | SLsig_unblock_signals(); |
---|
102 | return -1; |
---|
103 | } |
---|
104 | |
---|
105 | SLsig_unblock_signals(); |
---|
106 | |
---|
107 | SLsmg_cls(); |
---|
108 | SLtt_set_cursor_visibility(0); |
---|
109 | SLsmg_refresh(); |
---|
110 | |
---|
111 | for(i = 0; i < 16; i++) |
---|
112 | SLtt_set_color(i + 1, NULL, slang_colors[i], "black"); |
---|
113 | |
---|
114 | #elif defined(USE_NCURSES) |
---|
115 | int i; |
---|
116 | |
---|
117 | initscr(); |
---|
118 | keypad(stdscr, TRUE); |
---|
119 | nonl(); |
---|
120 | cbreak(); |
---|
121 | noecho(); |
---|
122 | nodelay(stdscr, TRUE); |
---|
123 | curs_set(0); |
---|
124 | |
---|
125 | start_color(); |
---|
126 | |
---|
127 | init_pair(1 + EE_BLACK, COLOR_BLACK, COLOR_BLACK); |
---|
128 | init_pair(1 + EE_BLUE, COLOR_BLUE, COLOR_BLACK); |
---|
129 | init_pair(1 + EE_GREEN, COLOR_GREEN, COLOR_BLACK); |
---|
130 | init_pair(1 + EE_CYAN, COLOR_CYAN, COLOR_BLACK); |
---|
131 | init_pair(1 + EE_RED, COLOR_RED, COLOR_BLACK); |
---|
132 | init_pair(1 + EE_MAGENTA, COLOR_MAGENTA, COLOR_BLACK); |
---|
133 | init_pair(1 + EE_BROWN, COLOR_YELLOW, COLOR_BLACK); |
---|
134 | init_pair(1 + EE_LIGHTGRAY, COLOR_WHITE, COLOR_BLACK); |
---|
135 | init_pair(1 + EE_DARKGRAY, COLOR_BLACK, COLOR_BLACK); |
---|
136 | init_pair(1 + EE_LIGHTBLUE, COLOR_BLUE, COLOR_BLACK); |
---|
137 | init_pair(1 + EE_LIGHTGREEN, COLOR_GREEN, COLOR_BLACK); |
---|
138 | init_pair(1 + EE_LIGHTCYAN, COLOR_CYAN, COLOR_BLACK); |
---|
139 | init_pair(1 + EE_LIGHTRED, COLOR_RED, COLOR_BLACK); |
---|
140 | init_pair(1 + EE_LIGHTMAGENTA, COLOR_MAGENTA, COLOR_BLACK); |
---|
141 | init_pair(1 + EE_YELLOW, COLOR_YELLOW, COLOR_BLACK); |
---|
142 | init_pair(1 + EE_WHITE, COLOR_WHITE, COLOR_BLACK); |
---|
143 | |
---|
144 | for(i = 0; i < 8; i++) |
---|
145 | { |
---|
146 | _ee_attr[i] = COLOR_PAIR(1 + i); |
---|
147 | _ee_attr[i + 8] = A_BOLD | COLOR_PAIR(1 + i); |
---|
148 | } |
---|
149 | |
---|
150 | #elif defined(USE_CONIO) |
---|
151 | gettextinfo(&ti); |
---|
152 | _ee_screen = malloc(2 * ti.screenwidth * ti.screenheight); |
---|
153 | if(_ee_screen == NULL) |
---|
154 | return -1; |
---|
155 | _wscroll = 0; |
---|
156 | _setcursortype(_NOCURSOR); |
---|
157 | clrscr(); |
---|
158 | # if defined(SCREENUPDATE_IN_PC_H) |
---|
159 | ScreenRetrieve(_ee_screen); |
---|
160 | # else |
---|
161 | /* FIXME */ |
---|
162 | # endif |
---|
163 | |
---|
164 | #endif |
---|
165 | _ee_empty_line = malloc(ee_get_width() + 1); |
---|
166 | memset(_ee_empty_line, ' ', ee_get_width()); |
---|
167 | _ee_empty_line[ee_get_width()] = '\0'; |
---|
168 | |
---|
169 | _ee_scratch_line = malloc(ee_get_width() + 1); |
---|
170 | |
---|
171 | _ee_delay = 0; |
---|
172 | _ee_rendertime = 0; |
---|
173 | |
---|
174 | return 0; |
---|
175 | } |
---|
176 | |
---|
177 | unsigned int ee_get_width(void) |
---|
178 | { |
---|
179 | #if defined(USE_SLANG) |
---|
180 | return SLtt_Screen_Cols; |
---|
181 | #elif defined(USE_NCURSES) |
---|
182 | return COLS; |
---|
183 | #elif defined(USE_CONIO) |
---|
184 | return ti.screenwidth; |
---|
185 | #endif |
---|
186 | } |
---|
187 | |
---|
188 | unsigned int ee_get_height(void) |
---|
189 | { |
---|
190 | #if defined(USE_SLANG) |
---|
191 | return SLtt_Screen_Rows; |
---|
192 | #elif defined(USE_NCURSES) |
---|
193 | return LINES; |
---|
194 | #else |
---|
195 | return ti.screenheight; |
---|
196 | #endif |
---|
197 | } |
---|
198 | |
---|
199 | void ee_set_delay(unsigned int usec) |
---|
200 | { |
---|
201 | _ee_delay = usec; |
---|
202 | } |
---|
203 | |
---|
204 | unsigned int ee_get_rendertime(void) |
---|
205 | { |
---|
206 | return _ee_rendertime; |
---|
207 | } |
---|
208 | |
---|
209 | const char *ee_get_color_name(unsigned int color) |
---|
210 | { |
---|
211 | static const char *color_names[16] = |
---|
212 | { |
---|
213 | "black", |
---|
214 | "blue", |
---|
215 | "green", |
---|
216 | "cyan", |
---|
217 | "red", |
---|
218 | "magenta", |
---|
219 | "brown", |
---|
220 | "light gray", |
---|
221 | "dark gray", |
---|
222 | "light blue", |
---|
223 | "light green", |
---|
224 | "light cyan", |
---|
225 | "light red", |
---|
226 | "light magenta", |
---|
227 | "yellow", |
---|
228 | "white", |
---|
229 | }; |
---|
230 | |
---|
231 | if(color < 0 || color > 15) |
---|
232 | return "unknown color"; |
---|
233 | |
---|
234 | return color_names[color]; |
---|
235 | } |
---|
236 | |
---|
237 | static unsigned int _ee_getticks(void) |
---|
238 | { |
---|
239 | static unsigned int last_sec = 0, last_usec = 0; |
---|
240 | |
---|
241 | struct timeval tv; |
---|
242 | unsigned int ticks = 0; |
---|
243 | |
---|
244 | gettimeofday(&tv, NULL); |
---|
245 | |
---|
246 | if(last_sec != 0) |
---|
247 | { |
---|
248 | ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec); |
---|
249 | } |
---|
250 | |
---|
251 | last_sec = tv.tv_sec; |
---|
252 | last_usec = tv.tv_usec; |
---|
253 | |
---|
254 | return ticks; |
---|
255 | } |
---|
256 | |
---|
257 | void ee_refresh(void) |
---|
258 | { |
---|
259 | #define IDLE_USEC 10000 |
---|
260 | static unsigned int lastticks = 0; |
---|
261 | unsigned int ticks = lastticks + _ee_getticks(); |
---|
262 | |
---|
263 | #if defined(USE_SLANG) |
---|
264 | SLsmg_refresh(); |
---|
265 | #elif defined(USE_NCURSES) |
---|
266 | refresh(); |
---|
267 | #elif defined(USE_CONIO) |
---|
268 | # if defined(SCREENUPDATE_IN_PC_H) |
---|
269 | ScreenUpdate(_ee_screen); |
---|
270 | # else |
---|
271 | /* FIXME */ |
---|
272 | # endif |
---|
273 | #endif |
---|
274 | |
---|
275 | /* Wait until _ee_delay + time of last call */ |
---|
276 | ticks += _ee_getticks(); |
---|
277 | for(; ticks < _ee_delay - IDLE_USEC; ticks += _ee_getticks()) |
---|
278 | usleep(IDLE_USEC); |
---|
279 | |
---|
280 | /* Update the sliding mean of the render time */ |
---|
281 | _ee_rendertime = (7 * _ee_rendertime + ticks) / 8; |
---|
282 | |
---|
283 | lastticks = ticks - _ee_delay; |
---|
284 | |
---|
285 | /* If we drifted too much, it's bad, bad, bad. */ |
---|
286 | if(lastticks > _ee_delay) |
---|
287 | lastticks = 0; |
---|
288 | } |
---|
289 | |
---|
290 | void ee_end(void) |
---|
291 | { |
---|
292 | #if defined(USE_SLANG) |
---|
293 | SLtt_set_cursor_visibility(1); |
---|
294 | SLang_reset_tty(); |
---|
295 | SLsmg_reset_smg(); |
---|
296 | #elif defined(USE_NCURSES) |
---|
297 | curs_set(1); |
---|
298 | endwin(); |
---|
299 | #elif defined(USE_CONIO) |
---|
300 | _wscroll = 1; |
---|
301 | textcolor((enum COLORS)WHITE); |
---|
302 | textbackground((enum COLORS)BLACK); |
---|
303 | gotoxy(ee_get_width(), ee_get_height()); |
---|
304 | cputs("\r\n"); |
---|
305 | _setcursortype(_NORMALCURSOR); |
---|
306 | #endif |
---|
307 | } |
---|
308 | |
---|