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

Last change on this file since 80 was 80, checked in by Sam Hocevar, 17 years ago
  • libee is now standalone.
  • 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 80 2003-11-09 12:40:53Z 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#ifdef USE_SLANG
124    //SLsmg_cls();
125    int y;
126    for(y = 0; y < ee_get_height(); y++)
127    {
128        ee_goto(0, y);
129        ee_putstr("                                                                                ");
130    }
131#elif USE_NCURSES
132    //clear();
133    int y;
134    for(y = 0; y < ee_get_height(); y++)
135    {
136        ee_goto(0, y);
137        ee_putstr("                                                                                ");
138    }
139#else
140    /* Use dummy driver */
141#endif
142}
143
144static int64_t local_time(void)
145{
146    struct timeval tv;
147    int64_t now;
148
149    gettimeofday(&tv, NULL);
150    now = tv.tv_sec;
151    now *= 1000000;
152    now += tv.tv_usec;
153    return now;
154}
155
156#define DELAY 40000
157void ee_refresh(void)
158{
159    static int64_t local_clock = 0;
160    int64_t now;
161
162    ee_goto(0, 0);
163
164    if(!local_clock)
165    {
166        /* Initialize local_clock */
167        local_clock = local_time();
168    }
169
170    if(local_time() > local_clock + 10000)
171    {
172        /* If we are late, we shouldn't display anything */
173    }
174
175#ifdef USE_SLANG
176    SLsmg_refresh();
177#elif USE_NCURSES
178    refresh();
179#else
180    /* Use dummy driver */
181#endif
182
183    now = local_time();
184
185    if(now < local_clock + DELAY - 10000)
186    {
187        usleep(local_clock + DELAY - 10000 - now);
188    }
189
190    local_clock += DELAY;
191}
192
193void ee_end(void)
194{
195#ifdef USE_SLANG
196    SLtt_set_cursor_visibility(1);
197    SLang_reset_tty();
198    SLsmg_reset_smg();
199#elif USE_NCURSES
200    curs_set(1);
201    endwin();
202#else
203    /* Use dummy driver */
204#endif
205}
206
207char ee_get_key(void)
208{
209#ifdef USE_SLANG
210    if(SLang_input_pending(0))
211    {
212        return SLang_getkey();
213    }
214#elif USE_NCURSES
215    char key = getch();
216
217    if(key != ERR)
218    {
219        return key;
220    }
221#else
222    return 0;
223
224#endif
225
226    return 0;
227}
228
Note: See TracBrowser for help on using the repository browser.