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

Last change on this file since 247 was 247, checked in by Sam Hocevar, 17 years ago
  • src/caca.c: + Moved global variables to src/graphics.c.
  • src/bitmap.c: + Sanitised prototypes.
  • Property svn:keywords set to Id
File size: 5.2 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 247 2003-12-11 16:56:35Z 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#else
42#   error "no graphics library detected"
43#endif
44
45#include <stdlib.h>
46#include <string.h>
47
48#include "caca.h"
49#include "caca_internals.h"
50
51static void caca_init_terminal(void);
52
53#if defined(USE_NCURSES)
54static mmask_t oldmask;
55#endif
56
57int caca_init(void)
58{
59#if defined(USE_NCURSES)
60    mmask_t newmask;
61#endif
62
63    caca_init_terminal();
64
65#if defined(USE_SLANG)
66    /* Initialize slang library */
67    SLsig_block_signals();
68    SLtt_get_terminfo();
69
70    if(SLkp_init() == -1)
71    {
72        SLsig_unblock_signals();
73        return -1;
74    }
75
76    SLang_init_tty(-1, 0, 1);
77
78    if(SLsmg_init_smg() == -1)
79    {
80        SLsig_unblock_signals();
81        return -1;
82    }
83
84    SLsig_unblock_signals();
85
86    SLsmg_cls();
87    SLtt_set_cursor_visibility(0);
88    SLkp_define_keysym("\e[M", 1001);
89    SLtt_set_mouse_mode(1, 0);
90    SLsmg_refresh();
91
92    /* Disable scrolling so that hashmap scrolling optimization code
93     * does not cause ugly refreshes due to slow terminals */
94    SLtt_Term_Cannot_Scroll = 1;
95
96#elif defined(USE_NCURSES)
97    initscr();
98    keypad(stdscr, TRUE);
99    nonl();
100    cbreak();
101    noecho();
102    nodelay(stdscr, TRUE);
103    curs_set(0);
104
105    /* Activate mouse */
106    newmask = ALL_MOUSE_EVENTS;
107    mousemask(newmask, &oldmask);
108
109#elif defined(USE_CONIO)
110    _wscroll = 0;
111    _setcursortype(_NOCURSOR);
112    clrscr();
113
114#endif
115    if(_caca_init_graphics())
116        return -1;
117
118    return 0;
119}
120
121unsigned int caca_get_width(void)
122{
123    return _caca_width;
124}
125
126unsigned int caca_get_height(void)
127{
128    return _caca_height;
129}
130
131const char *caca_get_color_name(enum caca_color color)
132{
133    static const char *color_names[] =
134    {
135        "black",
136        "blue",
137        "green",
138        "cyan",
139        "red",
140        "magenta",
141        "brown",
142        "light gray",
143        "dark gray",
144        "light blue",
145        "light green",
146        "light cyan",
147        "light red",
148        "light magenta",
149        "yellow",
150        "white",
151    };
152
153    if(color < 0 || color > 15)
154        return "unknown";
155
156    return color_names[color];
157}
158
159const char *caca_get_dithering_name(enum caca_dithering dithering)
160{
161    static const char *dithering_names[] =
162    {
163        "no",
164        "2x2 ordered",
165        "4x4 ordered",
166        "8x8 ordered",
167        "random"
168    };
169
170    if(dithering < 0 || dithering > 4)
171        return "unknown";
172
173    return dithering_names[dithering];
174}
175
176void caca_end(void)
177{
178#if defined(USE_SLANG)
179    SLtt_set_mouse_mode(0, 0);
180    SLtt_set_cursor_visibility(1);
181    SLang_reset_tty();
182    SLsmg_reset_smg();
183#elif defined(USE_NCURSES)
184    mousemask(oldmask, NULL);
185    curs_set(1);
186    endwin();
187#elif defined(USE_CONIO)
188    _wscroll = 1;
189    textcolor((enum COLORS)WHITE);
190    textbackground((enum COLORS)BLACK);
191    gotoxy(_caca_width, _caca_height);
192    cputs("\r\n");
193    _setcursortype(_NORMALCURSOR);
194#endif
195}
196
197static void caca_init_terminal(void)
198{
199#if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
200    char *term, *colorterm, *other;
201
202    term = getenv("TERM");
203    colorterm = getenv("COLORTERM");
204
205    if(term && !strcmp(term, "xterm"))
206    {
207        /* If we are using gnome-terminal, it's really a 16 colour terminal */
208        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
209        {
210#if defined(USE_NCURSES)
211            SCREEN *screen;
212            screen = newterm("xterm-16color", stdout, stdin);
213            if(screen == NULL)
214                return;
215            endwin();
216#endif
217            (void)putenv("TERM=xterm-16color");
218            return;
219        }
220
221        /* Ditto if we are using Konsole */
222        other = getenv("KONSOLE_DCOP_SESSION");
223        if(other)
224        {
225#if defined(USE_NCURSES)
226            SCREEN *screen;
227            screen = newterm("xterm-16color", stdout, stdin);
228            if(screen == NULL)
229                return;
230            endwin();
231#endif
232            (void)putenv("TERM=xterm-16color");
233            return;
234        }
235    }
236#endif
237}
238
Note: See TracBrowser for help on using the repository browser.