Changeset 227
- Timestamp:
- Nov 27, 2003, 11:29:13 PM (17 years ago)
- Location:
- libcaca/trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/configure.ac
r224 r227 26 26 27 27 AC_CHECK_HEADERS(inttypes.h) 28 AC_CHECK_FUNCS(vsnprintf )28 AC_CHECK_FUNCS(vsnprintf getenv putenv) 29 29 30 30 USE_SLANG=false -
libcaca/trunk/src/caca.c
r226 r227 47 47 48 48 #include <stdlib.h> 49 #include <unistd.h>50 49 #include <string.h> 51 #include <sys/time.h>52 #include <time.h>53 50 54 51 #include "caca.h" 55 52 #include "caca_internals.h" 56 53 57 static unsigned int _caca_delay;58 static unsigned int _caca_rendertime; 54 static void caca_init_terminal(void); 55 59 56 char *_caca_empty_line; 60 57 char *_caca_scratch_line; … … 72 69 int caca_init(void) 73 70 { 74 #if defined(USE_SLANG) 75 /* See SLang ref., 5.4.4. */ 76 static char *slang_colors[16] = 77 { 78 "black", 79 "blue", 80 "green", 81 "cyan", 82 "red", 83 "magenta", 84 "brown", 85 "lightgray", 86 "gray", 87 "brightblue", 88 "brightgreen", 89 "brightcyan", 90 "brightred", 91 "brightmagenta", 92 "yellow", 93 "white", 94 }; 95 96 int fg, bg; 97 71 caca_init_terminal(); 72 73 #if defined(USE_SLANG) 98 74 /* Initialize slang library */ 99 75 SLsig_block_signals(); … … 122 98 SLsmg_refresh(); 123 99 124 for(bg = 0; bg < 16; bg++) 125 for(fg = 0; fg < 16; fg++) 126 { 127 int i = fg + 16 * bg; 128 SLtt_set_color(i, NULL, slang_colors[fg], slang_colors[bg]); 129 } 130 131 #elif defined(USE_NCURSES) 132 static int curses_colors[] = 133 { 134 COLOR_BLACK, 135 COLOR_BLUE, 136 COLOR_GREEN, 137 COLOR_CYAN, 138 COLOR_RED, 139 COLOR_MAGENTA, 140 COLOR_YELLOW, 141 COLOR_WHITE, 142 /* Extra values for xterm-16color */ 143 COLOR_BLACK + 8, 144 COLOR_BLUE + 8, 145 COLOR_GREEN + 8, 146 COLOR_CYAN + 8, 147 COLOR_RED + 8, 148 COLOR_MAGENTA + 8, 149 COLOR_YELLOW + 8, 150 COLOR_WHITE + 8 151 }; 152 153 int fg, bg, max; 100 #elif defined(USE_NCURSES) 154 101 mmask_t newmask; 155 102 … … 166 113 mousemask(newmask, &oldmask); 167 114 168 /* Activate colour */ 169 start_color(); 170 171 max = COLORS >= 16 ? 16 : 8; 172 173 for(bg = 0; bg < max; bg++) 174 for(fg = 0; fg < max; fg++) 175 { 176 /* Use ((max + 7 - fg) % max) instead of fg so that colour 0 177 * is light gray on black, since some terminals don't like 178 * this colour pair to be redefined. */ 179 int col = ((max + 7 - fg) % max) + max * bg; 180 init_pair(col, curses_colors[fg], curses_colors[bg]); 181 _caca_attr[fg + 16 * bg] = COLOR_PAIR(col); 182 183 if(max == 8) 184 { 185 /* Bright fg on simple bg */ 186 _caca_attr[fg + 8 + 16 * bg] = A_BOLD | COLOR_PAIR(col); 187 /* Simple fg on bright bg */ 188 _caca_attr[fg + 16 * (bg + 8)] = A_BLINK | COLOR_PAIR(col); 189 /* Bright fg on bright bg */ 190 _caca_attr[fg + 8 + 16 * (bg + 8)] = A_BLINK | A_BOLD | COLOR_PAIR(col); 191 } 192 } 193 194 #elif defined(USE_CONIO) 195 gettextinfo(&ti); 196 _caca_screen = malloc(2 * ti.screenwidth * ti.screenheight); 197 if(_caca_screen == NULL) 198 return -1; 115 #elif defined(USE_CONIO) 199 116 _wscroll = 0; 200 117 _setcursortype(_NOCURSOR); 201 118 clrscr(); 202 # if defined(SCREENUPDATE_IN_PC_H) 203 ScreenRetrieve(_caca_screen); 204 # else 205 /* FIXME */ 206 # endif 207 208 #endif 209 _caca_empty_line = malloc(caca_get_width() + 1); 210 memset(_caca_empty_line, ' ', caca_get_width()); 211 _caca_empty_line[caca_get_width()] = '\0'; 212 213 _caca_scratch_line = malloc(caca_get_width() + 1); 214 215 _caca_delay = 0; 216 _caca_rendertime = 0; 119 120 #endif 121 if(_caca_init_graphics()) 122 return -1; 217 123 218 124 return 0; … … 239 145 return ti.screenheight; 240 146 #endif 241 }242 243 void caca_set_delay(unsigned int usec)244 {245 _caca_delay = usec;246 }247 248 unsigned int caca_get_rendertime(void)249 {250 return _caca_rendertime;251 147 } 252 148 … … 279 175 } 280 176 281 static unsigned int _caca_getticks(void)282 {283 static unsigned int last_sec = 0, last_usec = 0;284 285 struct timeval tv;286 unsigned int ticks = 0;287 288 gettimeofday(&tv, NULL);289 290 if(last_sec != 0)291 {292 ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec);293 }294 295 last_sec = tv.tv_sec;296 last_usec = tv.tv_usec;297 298 return ticks;299 }300 301 void caca_refresh(void)302 {303 #define IDLE_USEC 10000304 static int lastticks = 0;305 int ticks = lastticks + _caca_getticks();306 307 #if defined(USE_SLANG)308 SLsmg_refresh();309 #elif defined(USE_NCURSES)310 refresh();311 #elif defined(USE_CONIO)312 # if defined(SCREENUPDATE_IN_PC_H)313 ScreenUpdate(_caca_screen);314 # else315 /* FIXME */316 # endif317 #endif318 319 /* Wait until _caca_delay + time of last call */320 ticks += _caca_getticks();321 for(; ticks + IDLE_USEC < (int)_caca_delay; ticks += _caca_getticks())322 usleep(IDLE_USEC);323 324 /* Update the sliding mean of the render time */325 _caca_rendertime = (7 * _caca_rendertime + ticks) / 8;326 327 lastticks = ticks - _caca_delay;328 329 /* If we drifted too much, it's bad, bad, bad. */330 if(lastticks > (int)_caca_delay)331 lastticks = 0;332 }333 334 177 void caca_end(void) 335 178 { … … 353 196 } 354 197 198 static 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 -
libcaca/trunk/src/caca_internals.h
r205 r227 31 31 #define __CACA_INTERNALS_H__ 32 32 33 extern int _caca_init_graphics(void); 34 33 35 #if defined(USE_NCURSES) 34 36 extern int _caca_attr[]; -
libcaca/trunk/src/graphics.c
r226 r227 43 43 #include <string.h> 44 44 #include <stdlib.h> 45 #include <unistd.h> 45 46 #include <stdarg.h> 47 #include <sys/time.h> 48 #include <time.h> 46 49 47 50 #include "caca.h" 48 51 #include "caca_internals.h" 52 53 static unsigned int _caca_delay; 54 static unsigned int _caca_rendertime; 49 55 50 56 static enum caca_color _caca_fgcolor = CACA_COLOR_LIGHTGRAY; … … 187 193 } 188 194 195 int _caca_init_graphics(void) 196 { 197 #if defined(USE_SLANG) 198 /* See SLang ref., 5.4.4. */ 199 static char *slang_colors[16] = 200 { 201 "black", 202 "blue", 203 "green", 204 "cyan", 205 "red", 206 "magenta", 207 "brown", 208 "lightgray", 209 "gray", 210 "brightblue", 211 "brightgreen", 212 "brightcyan", 213 "brightred", 214 "brightmagenta", 215 "yellow", 216 "white", 217 }; 218 219 int fg, bg; 220 221 for(bg = 0; bg < 16; bg++) 222 for(fg = 0; fg < 16; fg++) 223 { 224 int i = fg + 16 * bg; 225 SLtt_set_color(i, NULL, slang_colors[fg], slang_colors[bg]); 226 } 227 228 #elif defined(USE_NCURSES) 229 static int curses_colors[] = 230 { 231 COLOR_BLACK, 232 COLOR_BLUE, 233 COLOR_GREEN, 234 COLOR_CYAN, 235 COLOR_RED, 236 COLOR_MAGENTA, 237 COLOR_YELLOW, 238 COLOR_WHITE, 239 /* Extra values for xterm-16color */ 240 COLOR_BLACK + 8, 241 COLOR_BLUE + 8, 242 COLOR_GREEN + 8, 243 COLOR_CYAN + 8, 244 COLOR_RED + 8, 245 COLOR_MAGENTA + 8, 246 COLOR_YELLOW + 8, 247 COLOR_WHITE + 8 248 }; 249 250 int fg, bg, max; 251 252 /* Activate colour */ 253 start_color(); 254 255 max = COLORS >= 16 ? 16 : 8; 256 257 for(bg = 0; bg < max; bg++) 258 for(fg = 0; fg < max; fg++) 259 { 260 /* Use ((max + 7 - fg) % max) instead of fg so that colour 0 261 * is light gray on black, since some terminals don't like 262 * this colour pair to be redefined. */ 263 int col = ((max + 7 - fg) % max) + max * bg; 264 init_pair(col, curses_colors[fg], curses_colors[bg]); 265 _caca_attr[fg + 16 * bg] = COLOR_PAIR(col); 266 267 if(max == 8) 268 { 269 /* Bright fg on simple bg */ 270 _caca_attr[fg + 8 + 16 * bg] = A_BOLD | COLOR_PAIR(col); 271 /* Simple fg on bright bg */ 272 _caca_attr[fg + 16 * (bg + 8)] = A_BLINK | COLOR_PAIR(col); 273 /* Bright fg on bright bg */ 274 _caca_attr[fg + 8 + 16 * (bg + 8)] = A_BLINK | A_BOLD | COLOR_PAIR(col); 275 } 276 } 277 278 #elif defined(USE_CONIO) 279 gettextinfo(&ti); 280 _caca_screen = malloc(2 * ti.screenwidth * ti.screenheight); 281 if(_caca_screen == NULL) 282 return -1; 283 # if defined(SCREENUPDATE_IN_PC_H) 284 ScreenRetrieve(_caca_screen); 285 # else 286 /* FIXME */ 287 # endif 288 289 #endif 290 _caca_empty_line = malloc(caca_get_width() + 1); 291 memset(_caca_empty_line, ' ', caca_get_width()); 292 _caca_empty_line[caca_get_width()] = '\0'; 293 294 _caca_scratch_line = malloc(caca_get_width() + 1); 295 296 _caca_delay = 0; 297 _caca_rendertime = 0; 298 299 return 0; 300 } 301 302 void caca_set_delay(unsigned int usec) 303 { 304 _caca_delay = usec; 305 } 306 307 unsigned int caca_get_rendertime(void) 308 { 309 return _caca_rendertime; 310 } 311 312 static unsigned int _caca_getticks(void) 313 { 314 static unsigned int last_sec = 0, last_usec = 0; 315 316 struct timeval tv; 317 unsigned int ticks = 0; 318 319 gettimeofday(&tv, NULL); 320 321 if(last_sec != 0) 322 { 323 ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec); 324 } 325 326 last_sec = tv.tv_sec; 327 last_usec = tv.tv_usec; 328 329 return ticks; 330 } 331 332 void caca_refresh(void) 333 { 334 #define IDLE_USEC 10000 335 static int lastticks = 0; 336 int ticks = lastticks + _caca_getticks(); 337 338 #if defined(USE_SLANG) 339 SLsmg_refresh(); 340 #elif defined(USE_NCURSES) 341 refresh(); 342 #elif defined(USE_CONIO) 343 # if defined(SCREENUPDATE_IN_PC_H) 344 ScreenUpdate(_caca_screen); 345 # else 346 /* FIXME */ 347 # endif 348 #endif 349 350 /* Wait until _caca_delay + time of last call */ 351 ticks += _caca_getticks(); 352 for(; ticks + IDLE_USEC < (int)_caca_delay; ticks += _caca_getticks()) 353 usleep(IDLE_USEC); 354 355 /* Update the sliding mean of the render time */ 356 _caca_rendertime = (7 * _caca_rendertime + ticks) / 8; 357 358 lastticks = ticks - _caca_delay; 359 360 /* If we drifted too much, it's bad, bad, bad. */ 361 if(lastticks > (int)_caca_delay) 362 lastticks = 0; 363 } 364
Note: See TracChangeset
for help on using the changeset viewer.