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

Last change on this file since 82 was 82, checked in by Sam Hocevar, 17 years ago
  • libee/ee.c: Exact line fill in ee_clear().
  • Property svn:keywords set to Id
File size: 4.4 KB
RevLine 
[36]1/*
[80]2 *   libee         ASCII-Art library
3 *   Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
[36]4 *                 All Rights Reserved
5 *
[76]6 *   $Id: ee.c 82 2003-11-09 12:48:09Z sam $
[36]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 */
[18]22
[64]23#include "config.h"
24
[18]25#include <stdlib.h>
[64]26#include <unistd.h>
[76]27#include <sys/time.h>
28#include <time.h>
[18]29
[80]30#include "ee.h"
[18]31
[80]32int ee_init(void)
[18]33{
34#ifdef USE_SLANG
[80]35    static char * colors[] = { "black", "green", "yellow", "white",
36        "red", "gray", "lightgray", "blue", "cyan", "magenta", NULL };
37    int i;
38
[18]39    /* Initialize slang library */
40    SLsig_block_signals();
41    SLtt_get_terminfo();
42
[80]43    if(SLkp_init() == -1)
[18]44    {
45        SLsig_unblock_signals();
[80]46        return -1;
[18]47    }
48
[80]49    SLang_init_tty(-1, 0, 1);
[18]50
[80]51    if(SLsmg_init_smg() == -1)
[18]52    {
53        SLsig_unblock_signals();
[80]54        return -1;
[18]55    }
56
57    SLsig_unblock_signals();
58
59    SLsmg_cls();
[80]60    SLtt_set_cursor_visibility(0);
[18]61    SLsmg_refresh();
[80]62
63    for(i = 0; colors[i]; i++)
64    {
65        SLtt_set_color(i + 1, NULL, colors[i], "black");
66    }
67
[52]68#elif USE_NCURSES
[18]69    /* Initialize ncurses library */
70    initscr();
71    keypad(stdscr, TRUE);
72    nonl();
73    cbreak();
74    noecho();
75    nodelay(stdscr, TRUE);
[80]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
[52]91#else
92    /* Dummy driver */
[80]93
[18]94#endif
95
96    return 0;
97}
98
[80]99int ee_get_width(void)
[18]100{
101#ifdef USE_SLANG
[80]102    return SLtt_Screen_Cols;
[52]103#elif USE_NCURSES
[80]104    return COLS;
[52]105#else
[80]106    return 80;
[18]107#endif
108}
109
[80]110int ee_get_height(void)
[18]111{
112#ifdef USE_SLANG
[80]113    return SLtt_Screen_Rows;
[52]114#elif USE_NCURSES
[80]115    return LINES;
[52]116#else
[80]117    return 25;
[18]118#endif
119}
120
[80]121void ee_clear(void)
[64]122{
[82]123#if defined(USE_SLANG) || defined(USE_NCURSES)
124    /* We could use SLsmg_cls(), but drawing empty lines is much faster */
125    int x = ee_get_width(), y = ee_get_height();
126    char *empty_line = malloc((x + 1) * sizeof(char));
127
128    memset(empty_line, ' ', x);
129    empty_line[x] = '\0';
130
131    for(y; y--;)
[76]132    {
[80]133        ee_goto(0, y);
[82]134        ee_putstr(empty_line);
[76]135    }
[82]136
137    free(empty_line);
[64]138#else
139    /* Use dummy driver */
140#endif
141}
142
[76]143static int64_t local_time(void)
[18]144{
[76]145    struct timeval tv;
146    int64_t now;
147
148    gettimeofday(&tv, NULL);
149    now = tv.tv_sec;
150    now *= 1000000;
151    now += tv.tv_usec;
152    return now;
[18]153}
154
[76]155#define DELAY 40000
[80]156void ee_refresh(void)
[18]157{
[76]158    static int64_t local_clock = 0;
159    int64_t now;
160
[80]161    ee_goto(0, 0);
[76]162
[80]163    if(!local_clock)
[76]164    {
165        /* Initialize local_clock */
166        local_clock = local_time();
167    }
168
[80]169    if(local_time() > local_clock + 10000)
[76]170    {
171        /* If we are late, we shouldn't display anything */
172    }
173
[18]174#ifdef USE_SLANG
175    SLsmg_refresh();
[52]176#elif USE_NCURSES
177    refresh();
[18]178#else
[52]179    /* Use dummy driver */
[18]180#endif
[76]181
182    now = local_time();
183
[80]184    if(now < local_clock + DELAY - 10000)
[76]185    {
[80]186        usleep(local_clock + DELAY - 10000 - now);
[76]187    }
188
189    local_clock += DELAY;
[18]190}
191
[80]192void ee_end(void)
[18]193{
194#ifdef USE_SLANG
[80]195    SLtt_set_cursor_visibility(1);
[18]196    SLang_reset_tty();
197    SLsmg_reset_smg();
[52]198#elif USE_NCURSES
[80]199    curs_set(1);
[52]200    endwin();
[18]201#else
[52]202    /* Use dummy driver */
[18]203#endif
204}
205
[80]206char ee_get_key(void)
207{
208#ifdef USE_SLANG
209    if(SLang_input_pending(0))
210    {
211        return SLang_getkey();
212    }
213#elif USE_NCURSES
214    char key = getch();
215
216    if(key != ERR)
217    {
218        return key;
219    }
220#else
221    return 0;
222
223#endif
224
225    return 0;
226}
227
Note: See TracBrowser for help on using the repository browser.