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

Last change on this file since 156 was 156, checked in by Sam Hocevar, 16 years ago
  • MS-DOS port of libee, using <conio.h>.
  • Property svn:keywords set to Id
File size: 4.3 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 156 2003-11-12 16:23:18Z 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#ifdef USE_SLANG
26#   include <slang.h>
27#elif USE_NCURSES
28#   include <curses.h>
29#elif USE_CONIO
30#   include <conio.h>
31#else
32#   error "no graphics library detected"
33#endif
34
35#include <stdlib.h>
36#include <unistd.h>
37#include <string.h>
38#include <sys/time.h>
39#include <time.h>
40
41#include "ee.h"
42
43static int _ee_delay;
44#ifdef USE_CONIO
45static struct text_info ti;
46#endif
47
48int ee_init(void)
49{
50#ifdef USE_SLANG
51    static char * colors[] = { "black", "green", "yellow", "white",
52        "red", "gray", "lightgray", "blue", "cyan", "magenta", NULL };
53    int i;
54
55    /* Initialize slang library */
56    SLsig_block_signals();
57    SLtt_get_terminfo();
58
59    if(SLkp_init() == -1)
60    {
61        SLsig_unblock_signals();
62        return -1;
63    }
64
65    SLang_init_tty(-1, 0, 1);
66
67    if(SLsmg_init_smg() == -1)
68    {
69        SLsig_unblock_signals();
70        return -1;
71    }
72
73    SLsig_unblock_signals();
74
75    SLsmg_cls();
76    SLtt_set_cursor_visibility(0);
77    SLsmg_refresh();
78
79    for(i = 0; colors[i]; i++)
80    {
81        SLtt_set_color(i + 1, NULL, colors[i], "black");
82    }
83
84#elif USE_NCURSES
85    /* Initialize ncurses library */
86    initscr();
87    keypad(stdscr, TRUE);
88    nonl();
89    cbreak();
90    noecho();
91    nodelay(stdscr, TRUE);
92    curs_set(0);
93
94    start_color();
95
96    init_pair(EE_BLACK, COLOR_BLACK, COLOR_BLACK);
97    init_pair(EE_GREEN, COLOR_GREEN, COLOR_BLACK);
98    init_pair(EE_YELLOW, COLOR_YELLOW, COLOR_BLACK);
99    init_pair(EE_WHITE, COLOR_WHITE, COLOR_BLACK);
100    init_pair(EE_RED, COLOR_RED, COLOR_BLACK);
101    init_pair(EE_GRAY, COLOR_WHITE, COLOR_BLACK); // XXX
102    init_pair(EE_LIGHTGRAY, COLOR_WHITE, COLOR_BLACK); // XXX
103    init_pair(EE_BLUE, COLOR_BLUE, COLOR_BLACK);
104    init_pair(EE_CYAN, COLOR_CYAN, COLOR_BLACK);
105    init_pair(EE_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
106
107#elif USE_CONIO
108    _wscroll = 0;
109    _setcursortype(_NOCURSOR);
110    clrscr();
111    gettextinfo(&ti);
112//window(2, 2, 20, 20);
113
114#endif
115    _ee_delay = 0;
116
117    return 0;
118}
119
120void ee_set_delay(int delay)
121{
122    _ee_delay = delay;
123}
124
125int ee_get_width(void)
126{
127#ifdef USE_SLANG
128    return SLtt_Screen_Cols;
129#elif USE_NCURSES
130    return COLS;
131#elif USE_CONIO
132    return ti.screenwidth;
133#endif
134}
135
136int ee_get_height(void)
137{
138#ifdef USE_SLANG
139    return SLtt_Screen_Rows;
140#elif USE_NCURSES
141    return LINES;
142#else
143    return ti.screenheight;
144#endif
145}
146
147#ifndef USE_CONIO
148static int64_t local_time(void)
149{
150    struct timeval tv;
151    int64_t now;
152
153    gettimeofday(&tv, NULL);
154    now = tv.tv_sec;
155    now *= 1000000;
156    now += tv.tv_usec;
157    return now;
158}
159#endif
160
161void ee_refresh(void)
162{
163#ifndef USE_CONIO
164    static int64_t local_clock = 0;
165    int64_t now;
166
167    if(!local_clock)
168    {
169        /* Initialize local_clock */
170        local_clock = local_time();
171    }
172
173    if(local_time() > local_clock + 10000)
174    {
175        /* If we are late, we shouldn't display anything */
176    }
177#endif
178
179#ifdef USE_SLANG
180    SLsmg_refresh();
181#elif USE_NCURSES
182    refresh();
183#elif USE_CONIO
184    /* Do nothing? */
185#endif
186
187#ifndef USE_CONIO
188    now = local_time();
189
190    if(now < local_clock + _ee_delay - 10000)
191    {
192        usleep(local_clock + _ee_delay - 10000 - now);
193    }
194
195    local_clock += _ee_delay;
196#endif
197}
198
199void ee_end(void)
200{
201#ifdef USE_SLANG
202    SLtt_set_cursor_visibility(1);
203    SLang_reset_tty();
204    SLsmg_reset_smg();
205#elif USE_NCURSES
206    curs_set(1);
207    endwin();
208#elif USE_CONIO
209    _wscroll = 1;
210    ee_set_color(EE_WHITE);
211    ee_putstr(ee_get_width(), ee_get_height()-1, "\r\n");
212    _setcursortype(_NORMALCURSOR);
213#endif
214}
215
Note: See TracBrowser for help on using the repository browser.