Ignore:
Timestamp:
Oct 26, 2006, 3:07:58 PM (13 years ago)
Author:
Sam Hocevar
Message:
  • Make stdin input line-aware so that we don't have to wait until EOF to display stuff.
  • Reorganise stuff and fix memory leaks.
File:
1 copied

Legend:

Unmodified
Added
Removed
  • toilet/trunk/src/term.c

    r1240 r1241  
    2828
    2929static int feed_tiny(context_t *, uint32_t);
     30static int flush_tiny(context_t *);
    3031static int end_tiny(context_t *);
    31 
    32 static int feed_big(context_t *, uint32_t);
    33 static int end_big(context_t *);
    3432
    3533int init_tiny(context_t *cx)
     
    4240
    4341    cx->feed = feed_tiny;
     42    cx->flush = flush_tiny;
    4443    cx->end = end_tiny;
    4544
     
    9291}
    9392
    94 static int end_tiny(context_t *cx)
     93static int flush_tiny(context_t *cx)
    9594{
    96     cucul_set_canvas_size(cx->cv, cx->w, cx->h);
     95    cx->torender = cx->cv;
     96    cucul_set_canvas_size(cx->torender, cx->w, cx->h);
     97
     98    cx->ew = 16;
     99    cx->eh = 2;
     100    cx->x = cx->y = 0;
     101    cx->w = cx->h = 0;
     102    cx->cv = cucul_create_canvas(cx->ew, cx->eh);
    97103
    98104    return 0;
    99105}
    100106
    101 int init_big(context_t *cx)
     107static int end_tiny(context_t *cx)
    102108{
    103     char const * const * fonts;
    104 
    105     fonts = cucul_get_font_list();
    106     cx->f = cucul_load_font(fonts[0], 0);
    107     cx->buf = malloc(4 * cucul_get_font_width(cx->f)
    108                        * cucul_get_font_height(cx->f));
    109     cx->onechar = cucul_create_canvas(1, 1);
    110     cucul_set_color(cx->onechar, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK);
    111 
    112     cx->x = cx->y = 0;
    113     cx->w = cx->h = 0;
    114     cx->cv = cucul_create_canvas(1, 1);
    115 
    116     cx->feed = feed_big;
    117     cx->end = end_big;
     109    cucul_free_canvas(cx->cv);
    118110
    119111    return 0;
    120112}
    121113
    122 static int feed_big(context_t *cx, uint32_t ch)
    123 {
    124     unsigned int w = cucul_get_font_width(cx->f);
    125     unsigned int h = cucul_get_font_height(cx->f);
    126     unsigned int x, y;
    127 
    128     switch(ch)
    129     {
    130         case (uint32_t)'\r':
    131             return 0;
    132         case (uint32_t)'\n':
    133             cx->x = 0;
    134             cx->y += h;
    135             return 0;
    136         case (uint32_t)'\t':
    137             cx->x = (((cx->x / w) & ~7) + 8) * w;
    138             return 0;
    139     }
    140 
    141     /* Check whether we reached the end of the screen */
    142     if(cx->x && cx->x + w > cx->term_width)
    143     {
    144         cx->x = 0;
    145         cx->y += h;
    146     }
    147 
    148     /* Check whether the current canvas is large enough */
    149     if(cx->x + w > cx->w)
    150         cx->w = cx->x + w < cx->term_width ? cx->x + w : cx->term_width;
    151 
    152     if(cx->y + h > cx->h)
    153         cx->h = cx->y + h;
    154 
    155     cucul_set_canvas_size(cx->cv, cx->w, cx->h);
    156 
    157     /* Render our char */
    158     cucul_putchar(cx->onechar, 0, 0, ch);
    159     cucul_render_canvas(cx->onechar, cx->f, cx->buf, w, h, 4 * w);
    160 
    161     for(y = 0; y < h; y++)
    162        for(x = 0; x < w; x++)
    163     {
    164         unsigned char c = cx->buf[4 * (x + y * w) + 2];
    165 
    166         if(c >= 0xa0)
    167             cucul_putstr(cx->cv, cx->x + x, cx->y + y, "█");
    168         else if(c >= 0x80)
    169             cucul_putstr(cx->cv, cx->x + x, cx->y + y, "▓");
    170         else if(c >= 0x40)
    171             cucul_putstr(cx->cv, cx->x + x, cx->y + y, "▒");
    172         else if(c >= 0x20)
    173             cucul_putstr(cx->cv, cx->x + x, cx->y + y, "░");
    174     }
    175 
    176     /* Advance cursor */
    177     cx->x += w;
    178 
    179     return 0;
    180 }
    181 
    182 static int end_big(context_t *cx)
    183 {
    184     cucul_free_canvas(cx->onechar);
    185     free(cx->buf);
    186     cucul_free_font(cx->f);
    187 
    188     return 0;
    189 }
    190 
Note: See TracChangeset for help on using the changeset viewer.