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