source: toilet/trunk/src/term.c @ 1241

Last change on this file since 1241 was 1241, checked in by Sam Hocevar, 14 years ago
  • Make stdin input line-aware so that we don't have to wait until EOF to display stuff.
  • Reorganise stuff and fix memory leaks.
  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1/*
2 *  TOIlet        The Other Implementation’s letters
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: term.c 1241 2006-10-26 13:07:58Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 * This file contains text to canvas rendering functions.
16 */
17
18#include "config.h"
19
20#if defined(HAVE_INTTYPES_H)
21#   include <inttypes.h>
22#endif
23#include <stdlib.h>
24#include <cucul.h>
25
26#include "toilet.h"
27#include "render.h"
28
29static int feed_tiny(context_t *, uint32_t);
30static int flush_tiny(context_t *);
31static int end_tiny(context_t *);
32
33int init_tiny(context_t *cx)
34{
35    cx->ew = 16;
36    cx->eh = 2;
37    cx->x = cx->y = 0;
38    cx->w = cx->h = 0;
39    cx->cv = cucul_create_canvas(cx->ew, cx->eh);
40
41    cx->feed = feed_tiny;
42    cx->flush = flush_tiny;
43    cx->end = end_tiny;
44
45    return 0;
46}
47
48static int feed_tiny(context_t *cx, uint32_t ch)
49{
50    switch(ch)
51    {
52        case (uint32_t)'\r':
53            return 0;
54        case (uint32_t)'\n':
55            cx->x = 0;
56            cx->y++;
57            return 0;
58        case (uint32_t)'\t':
59            cx->x = (cx->x & ~7) + 8;
60            return 0;
61    }
62
63    /* Check whether we reached the end of the screen */
64    if(cx->x && cx->x + 1 > cx->term_width)
65    {
66        cx->x = 0;
67        cx->y++;
68    }
69
70    /* Check whether the current canvas is large enough */
71    if(cx->x + 1 > cx->w)
72    {
73        cx->w = cx->x + 1 < cx->term_width ? cx->x + 1 : cx->term_width;
74        if(cx->w > cx->ew)
75            cx->ew = cx->ew + cx->ew / 2;
76    }
77
78    if(cx->y + 1 > cx->h)
79    {
80        cx->h = cx->y + 1;
81        if(cx->h > cx->eh)
82            cx->eh = cx->eh + cx->eh / 2;
83    }
84
85    cucul_set_canvas_size(cx->cv, cx->ew, cx->eh);
86
87    cucul_putchar(cx->cv, cx->x, cx->y, ch);
88    cx->x++;
89
90    return 0;
91}
92
93static int flush_tiny(context_t *cx)
94{
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);
103
104    return 0;
105}
106
107static int end_tiny(context_t *cx)
108{
109    cucul_free_canvas(cx->cv);
110
111    return 0;
112}
113
Note: See TracBrowser for help on using the repository browser.