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

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