Ignore:
Timestamp:
Nov 27, 2003, 11:29:13 PM (17 years ago)
Author:
Sam Hocevar
Message:
  • src/caca.c src/graphics.c: + Moved graphics stuff from caca.c to graphics.c. + Added a few kludges: if gnome-terminal or konsole are detected, switch

to TERM=xterm-16color to benefit from extended colour palette.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/src/caca.c

    r226 r227  
    4747
    4848#include <stdlib.h>
    49 #include <unistd.h>
    5049#include <string.h>
    51 #include <sys/time.h>
    52 #include <time.h>
    5350
    5451#include "caca.h"
    5552#include "caca_internals.h"
    5653
    57 static unsigned int _caca_delay;
    58 static unsigned int _caca_rendertime;
     54static void caca_init_terminal(void);
     55
    5956char *_caca_empty_line;
    6057char *_caca_scratch_line;
     
    7269int caca_init(void)
    7370{
    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)
    9874    /* Initialize slang library */
    9975    SLsig_block_signals();
     
    12298    SLsmg_refresh();
    12399
    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)
    154101    mmask_t newmask;
    155102
     
    166113    mousemask(newmask, &oldmask);
    167114
    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)
    199116    _wscroll = 0;
    200117    _setcursortype(_NOCURSOR);
    201118    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;
    217123
    218124    return 0;
     
    239145    return ti.screenheight;
    240146#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;
    251147}
    252148
     
    279175}
    280176
    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 10000
    304     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 #   else
    315     /* FIXME */
    316 #   endif
    317 #endif
    318 
    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 
    334177void caca_end(void)
    335178{
     
    353196}
    354197
     198static 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
Note: See TracChangeset for help on using the changeset viewer.