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

Last change on this file since 238 was 238, checked in by Sam Hocevar, 18 years ago
  • src/bitmap.c: + Fixed an endianness issue in the byte reader. + Decreased precision in RGB and HSV values to avoid overflows. + New dithering method: 2x2 ordered. + New renderer, with background colour awareness.
  • configure.ac: + Check for <endian.h>.
  • examples/demo.c examples/view.c: + Fixed an endianness issue in cacaview. + Adapted code to the additional dithering method.
  • Property svn:keywords set to Id
File size: 5.7 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 238 2003-11-30 15:01:55Z 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        "2x2 ordered",
189        "4x4 ordered",
190        "8x8 ordered",
191        "random"
192    };
193
194    if(dithering < 0 || dithering > 4)
195        return "unknown";
196
197    return dithering_names[dithering];
198}
199
200void caca_end(void)
201{
202#if defined(USE_SLANG)
203    SLtt_set_mouse_mode(0, 0);
204    SLtt_set_cursor_visibility(1);
205    SLang_reset_tty();
206    SLsmg_reset_smg();
207#elif defined(USE_NCURSES)
208    mousemask(oldmask, NULL);
209    curs_set(1);
210    endwin();
211#elif defined(USE_CONIO)
212    _wscroll = 1;
213    textcolor((enum COLORS)WHITE);
214    textbackground((enum COLORS)BLACK);
215    gotoxy(caca_get_width(), caca_get_height());
216    cputs("\r\n");
217    _setcursortype(_NORMALCURSOR);
218#endif
219}
220
221static void caca_init_terminal(void)
222{
223#if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
224    char *term, *colorterm, *other;
225
226    term = getenv("TERM");
227    colorterm = getenv("COLORTERM");
228
229    if(term && !strcmp(term, "xterm"))
230    {
231        /* If we are using gnome-terminal, it's really a 16 colour terminal */
232        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
233        {
234#if defined(USE_NCURSES)
235            SCREEN *screen;
236            screen = newterm("xterm-16color", stdout, stdin);
237            if(screen == NULL)
238                return;
239            endwin();
240#endif
241            (void)putenv("TERM=xterm-16color");
242            return;
243        }
244
245        /* Ditto if we are using Konsole */
246        other = getenv("KONSOLE_DCOP_SESSION");
247        if(other)
248        {
249#if defined(USE_NCURSES)
250            SCREEN *screen;
251            screen = newterm("xterm-16color", stdout, stdin);
252            if(screen == NULL)
253                return;
254            endwin();
255#endif
256            (void)putenv("TERM=xterm-16color");
257            return;
258        }
259    }
260#endif
261}
262
Note: See TracBrowser for help on using the repository browser.