source: ttyvaders/trunk/libee/ee.c @ 82

Last change on this file since 82 was 82, checked in by Sam Hocevar, 17 years ago
  • libee/ee.c: Exact line fill in ee_clear().
  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
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 82 2003-11-09 12:48:09Z 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#include <stdlib.h>
26#include <unistd.h>
27#include <sys/time.h>
28#include <time.h>
29
30#include "ee.h"
31
32int ee_init(void)
33{
34#ifdef USE_SLANG
35    static char * colors[] = { "black", "green", "yellow", "white",
36        "red", "gray", "lightgray", "blue", "cyan", "magenta", NULL };
37    int i;
38
39    /* Initialize slang library */
40    SLsig_block_signals();
41    SLtt_get_terminfo();
42
43    if(SLkp_init() == -1)
44    {
45        SLsig_unblock_signals();
46        return -1;
47    }
48
49    SLang_init_tty(-1, 0, 1);
50
51    if(SLsmg_init_smg() == -1)
52    {
53        SLsig_unblock_signals();
54        return -1;
55    }
56
57    SLsig_unblock_signals();
58
59    SLsmg_cls();
60    SLtt_set_cursor_visibility(0);
61    SLsmg_refresh();
62
63    for(i = 0; colors[i]; i++)
64    {
65        SLtt_set_color(i + 1, NULL, colors[i], "black");
66    }
67
68#elif USE_NCURSES
69    /* Initialize ncurses library */
70    initscr();
71    keypad(stdscr, TRUE);
72    nonl();
73    cbreak();
74    noecho();
75    nodelay(stdscr, TRUE);
76    curs_set(0);
77
78    start_color();
79
80    init_pair(EE_BLACK, COLOR_BLACK, COLOR_BLACK);
81    init_pair(EE_GREEN, COLOR_GREEN, COLOR_BLACK);
82    init_pair(EE_YELLOW, COLOR_YELLOW, COLOR_BLACK);
83    init_pair(EE_WHITE, COLOR_WHITE, COLOR_BLACK);
84    init_pair(EE_RED, COLOR_RED, COLOR_BLACK);
85    init_pair(EE_GRAY, COLOR_WHITE, COLOR_BLACK); // XXX
86    init_pair(EE_LIGHTGRAY, COLOR_WHITE, COLOR_BLACK); // XXX
87    init_pair(EE_BLUE, COLOR_BLUE, COLOR_BLACK);
88    init_pair(EE_CYAN, COLOR_CYAN, COLOR_BLACK);
89    init_pair(EE_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
90
91#else
92    /* Dummy driver */
93
94#endif
95
96    return 0;
97}
98
99int ee_get_width(void)
100{
101#ifdef USE_SLANG
102    return SLtt_Screen_Cols;
103#elif USE_NCURSES
104    return COLS;
105#else
106    return 80;
107#endif
108}
109
110int ee_get_height(void)
111{
112#ifdef USE_SLANG
113    return SLtt_Screen_Rows;
114#elif USE_NCURSES
115    return LINES;
116#else
117    return 25;
118#endif
119}
120
121void ee_clear(void)
122{
123#if defined(USE_SLANG) || defined(USE_NCURSES)
124    /* We could use SLsmg_cls(), but drawing empty lines is much faster */
125    int x = ee_get_width(), y = ee_get_height();
126    char *empty_line = malloc((x + 1) * sizeof(char));
127
128    memset(empty_line, ' ', x);
129    empty_line[x] = '\0';
130
131    for(y; y--;)
132    {
133        ee_goto(0, y);
134        ee_putstr(empty_line);
135    }
136
137    free(empty_line);
138#else
139    /* Use dummy driver */
140#endif
141}
142
143static int64_t local_time(void)
144{
145    struct timeval tv;
146    int64_t now;
147
148    gettimeofday(&tv, NULL);
149    now = tv.tv_sec;
150    now *= 1000000;
151    now += tv.tv_usec;
152    return now;
153}
154
155#define DELAY 40000
156void ee_refresh(void)
157{
158    static int64_t local_clock = 0;
159    int64_t now;
160
161    ee_goto(0, 0);
162
163    if(!local_clock)
164    {
165        /* Initialize local_clock */
166        local_clock = local_time();
167    }
168
169    if(local_time() > local_clock + 10000)
170    {
171        /* If we are late, we shouldn't display anything */
172    }
173
174#ifdef USE_SLANG
175    SLsmg_refresh();
176#elif USE_NCURSES
177    refresh();
178#else
179    /* Use dummy driver */
180#endif
181
182    now = local_time();
183
184    if(now < local_clock + DELAY - 10000)
185    {
186        usleep(local_clock + DELAY - 10000 - now);
187    }
188
189    local_clock += DELAY;
190}
191
192void ee_end(void)
193{
194#ifdef USE_SLANG
195    SLtt_set_cursor_visibility(1);
196    SLang_reset_tty();
197    SLsmg_reset_smg();
198#elif USE_NCURSES
199    curs_set(1);
200    endwin();
201#else
202    /* Use dummy driver */
203#endif
204}
205
206char ee_get_key(void)
207{
208#ifdef USE_SLANG
209    if(SLang_input_pending(0))
210    {
211        return SLang_getkey();
212    }
213#elif USE_NCURSES
214    char key = getch();
215
216    if(key != ERR)
217    {
218        return key;
219    }
220#else
221    return 0;
222
223#endif
224
225    return 0;
226}
227
Note: See TracBrowser for help on using the repository browser.