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

Last change on this file since 90 was 90, checked in by Sam Hocevar, 17 years ago
  • libee/io.c: + Moved ee_get_key() here.
  • libee/math.c: + Moved r00t() here and renamed it to ee_sqrt(). + Moved GET_RAND() here and renamed it to ee_rand().
  • src/math.c: + Removed this deprecated file.
  • Property svn:keywords set to Id
File size: 4.1 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 90 2003-11-09 13:32:04Z 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 <string.h>
28#include <sys/time.h>
29#include <time.h>
30
31#include "ee.h"
32
33int ee_init(void)
34{
35#ifdef USE_SLANG
36    static char * colors[] = { "black", "green", "yellow", "white",
37        "red", "gray", "lightgray", "blue", "cyan", "magenta", NULL };
38    int i;
39
40    /* Initialize slang library */
41    SLsig_block_signals();
42    SLtt_get_terminfo();
43
44    if(SLkp_init() == -1)
45    {
46        SLsig_unblock_signals();
47        return -1;
48    }
49
50    SLang_init_tty(-1, 0, 1);
51
52    if(SLsmg_init_smg() == -1)
53    {
54        SLsig_unblock_signals();
55        return -1;
56    }
57
58    SLsig_unblock_signals();
59
60    SLsmg_cls();
61    SLtt_set_cursor_visibility(0);
62    SLsmg_refresh();
63
64    for(i = 0; colors[i]; i++)
65    {
66        SLtt_set_color(i + 1, NULL, colors[i], "black");
67    }
68
69#elif USE_NCURSES
70    /* Initialize ncurses library */
71    initscr();
72    keypad(stdscr, TRUE);
73    nonl();
74    cbreak();
75    noecho();
76    nodelay(stdscr, TRUE);
77    curs_set(0);
78
79    start_color();
80
81    init_pair(EE_BLACK, COLOR_BLACK, COLOR_BLACK);
82    init_pair(EE_GREEN, COLOR_GREEN, COLOR_BLACK);
83    init_pair(EE_YELLOW, COLOR_YELLOW, COLOR_BLACK);
84    init_pair(EE_WHITE, COLOR_WHITE, COLOR_BLACK);
85    init_pair(EE_RED, COLOR_RED, COLOR_BLACK);
86    init_pair(EE_GRAY, COLOR_WHITE, COLOR_BLACK); // XXX
87    init_pair(EE_LIGHTGRAY, COLOR_WHITE, COLOR_BLACK); // XXX
88    init_pair(EE_BLUE, COLOR_BLUE, COLOR_BLACK);
89    init_pair(EE_CYAN, COLOR_CYAN, COLOR_BLACK);
90    init_pair(EE_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
91
92#else
93    /* Dummy driver */
94
95#endif
96
97    return 0;
98}
99
100int ee_get_width(void)
101{
102#ifdef USE_SLANG
103    return SLtt_Screen_Cols;
104#elif USE_NCURSES
105    return COLS;
106#else
107    return 80;
108#endif
109}
110
111int ee_get_height(void)
112{
113#ifdef USE_SLANG
114    return SLtt_Screen_Rows;
115#elif USE_NCURSES
116    return LINES;
117#else
118    return 25;
119#endif
120}
121
122void ee_clear(void)
123{
124#if defined(USE_SLANG) || defined(USE_NCURSES)
125    /* We could use SLsmg_cls(), but drawing empty lines is much faster */
126    int x = ee_get_width(), y = ee_get_height();
127    char *empty_line = malloc((x + 1) * sizeof(char));
128
129    memset(empty_line, ' ', x);
130    empty_line[x] = '\0';
131
132    while(y--)
133    {
134        ee_goto(0, y);
135        ee_putstr(empty_line);
136    }
137
138    free(empty_line);
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
Note: See TracBrowser for help on using the repository browser.