source: libcaca/trunk/src/caca.c @ 236

Last change on this file since 236 was 236, checked in by Sam Hocevar, 19 years ago
  • src/caca.c: + Minor change to the dithering names.
  • TODO: + Added cacaview TODO.
  • doc/cacaview.1: + Wrote a manpage for cacaview.
  • examples/Makefile.am: + Moved the -DX_DISPLAY_MISSING=1 here.
  • examples/view.c: + Capital 'D' cycles through dithering modes in reverse order. + Sleep when there is nothing to do. + Cosmetic code reorganisation.
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1/*
2 *   libcaca       ASCII-Art library
3 *   Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   This library is free software; you can redistribute it and/or
7 *   modify it under the terms of the GNU Lesser General Public
8 *   License as published by the Free Software Foundation; either
9 *   version 2 of the License, or (at your option) any later version.
10 *
11 *   This library is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 *   Lesser General Public License for more details.
15 *
16 *   You should have received a copy of the GNU Lesser General Public
17 *   License along with this library; if not, write to the Free Software
18 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 *   02111-1307  USA
20 */
21
22/**  \file caca.c
23 *   \version \$Id: caca.c 236 2003-11-30 01:18:10Z sam $
24 *   \author Sam Hocevar <sam@zoy.org>
25 *   \brief Main \e libcaca functions
26 *
27 *   This file contains the main functions used by \e libcaca applications to
28 *   initialise the library, get the screen properties, set the framerate and
29 *   so on.
30 */
31
32#include "config.h"
33
34#if defined(USE_SLANG)
35#   include <slang.h>
36#elif defined(USE_NCURSES)
37#   include <curses.h>
38#elif defined(USE_CONIO)
39#   include <dos.h>
40#   include <conio.h>
41#   if defined(SCREENUPDATE_IN_PC_H)
42#       include <pc.h>
43#   endif
44#else
45#   error "no graphics library detected"
46#endif
47
48#include <stdlib.h>
49#include <string.h>
50
51#include "caca.h"
52#include "caca_internals.h"
53
54static void caca_init_terminal(void);
55
56char *_caca_empty_line;
57char *_caca_scratch_line;
58
59#if defined(USE_NCURSES)
60static mmask_t oldmask;
61int _caca_attr[16*16];
62#endif
63
64#if defined(USE_CONIO)
65static struct text_info ti;
66char *_caca_screen;
67#endif
68
69int caca_init(void)
70{
71#if defined(USE_NCURSES)
72    mmask_t newmask;
73#endif
74
75    caca_init_terminal();
76
77#if defined(USE_SLANG)
78    /* Initialize slang library */
79    SLsig_block_signals();
80    SLtt_get_terminfo();
81
82    if(SLkp_init() == -1)
83    {
84        SLsig_unblock_signals();
85        return -1;
86    }
87
88    SLang_init_tty(-1, 0, 1);
89
90    if(SLsmg_init_smg() == -1)
91    {
92        SLsig_unblock_signals();
93        return -1;
94    }
95
96    SLsig_unblock_signals();
97
98    SLsmg_cls();
99    SLtt_set_cursor_visibility(0);
100    SLkp_define_keysym("\e[M", 1001);
101    SLtt_set_mouse_mode(1, 0);
102    SLsmg_refresh();
103
104    /* Disable scrolling so that hashmap scrolling optimization code
105     * does not cause ugly refreshes due to slow terminals */
106    SLtt_Term_Cannot_Scroll = 1;
107
108#elif defined(USE_NCURSES)
109    initscr();
110    keypad(stdscr, TRUE);
111    nonl();
112    cbreak();
113    noecho();
114    nodelay(stdscr, TRUE);
115    curs_set(0);
116
117    /* Activate mouse */
118    newmask = ALL_MOUSE_EVENTS;
119    mousemask(newmask, &oldmask);
120
121#elif defined(USE_CONIO)
122    _wscroll = 0;
123    _setcursortype(_NOCURSOR);
124    clrscr();
125
126#endif
127    if(_caca_init_graphics())
128        return -1;
129
130    return 0;
131}
132
133unsigned int caca_get_width(void)
134{
135#if defined(USE_SLANG)
136    return SLtt_Screen_Cols;
137#elif defined(USE_NCURSES)
138    return COLS;
139#elif defined(USE_CONIO)
140    return ti.screenwidth;
141#endif
142}
143
144unsigned int caca_get_height(void)
145{
146#if defined(USE_SLANG)
147    return SLtt_Screen_Rows;
148#elif defined(USE_NCURSES)
149    return LINES;
150#else
151    return ti.screenheight;
152#endif
153}
154
155const char *caca_get_color_name(enum caca_color color)
156{
157    static const char *color_names[] =
158    {
159        "black",
160        "blue",
161        "green",
162        "cyan",
163        "red",
164        "magenta",
165        "brown",
166        "light gray",
167        "dark gray",
168        "light blue",
169        "light green",
170        "light cyan",
171        "light red",
172        "light magenta",
173        "yellow",
174        "white",
175    };
176
177    if(color < 0 || color > 15)
178        return "unknown";
179
180    return color_names[color];
181}
182
183const char *caca_get_dithering_name(enum caca_dithering dithering)
184{
185    static const char *dithering_names[] =
186    {
187        "no",
188        "4x4 ordered",
189        "8x8 ordered",
190        "random"
191    };
192
193    if(dithering < 0 || dithering > 3)
194        return "unknown";
195
196    return dithering_names[dithering];
197}
198
199void caca_end(void)
200{
201#if defined(USE_SLANG)
202    SLtt_set_mouse_mode(0, 0);
203    SLtt_set_cursor_visibility(1);
204    SLang_reset_tty();
205    SLsmg_reset_smg();
206#elif defined(USE_NCURSES)
207    mousemask(oldmask, NULL);
208    curs_set(1);
209    endwin();
210#elif defined(USE_CONIO)
211    _wscroll = 1;
212    textcolor((enum COLORS)WHITE);
213    textbackground((enum COLORS)BLACK);
214    gotoxy(caca_get_width(), caca_get_height());
215    cputs("\r\n");
216    _setcursortype(_NORMALCURSOR);
217#endif
218}
219
220static void caca_init_terminal(void)
221{
222#if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
223    char *term, *colorterm, *other;
224
225    term = getenv("TERM");
226    colorterm = getenv("COLORTERM");
227
228    if(term && !strcmp(term, "xterm"))
229    {
230        /* If we are using gnome-terminal, it's really a 16 colour terminal */
231        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
232        {
233#if defined(USE_NCURSES)
234            SCREEN *screen;
235            screen = newterm("xterm-16color", stdout, stdin);
236            if(screen == NULL)
237                return;
238            endwin();
239#endif
240            (void)putenv("TERM=xterm-16color");
241            return;
242        }
243
244        /* Ditto if we are using Konsole */
245        other = getenv("KONSOLE_DCOP_SESSION");
246        if(other)
247        {
248#if defined(USE_NCURSES)
249            SCREEN *screen;
250            screen = newterm("xterm-16color", stdout, stdin);
251            if(screen == NULL)
252                return;
253            endwin();
254#endif
255            (void)putenv("TERM=xterm-16color");
256            return;
257        }
258    }
259#endif
260}
261
Note: See TracBrowser for help on using the repository browser.