source: libcaca/trunk/caca/driver_slang.c @ 906

Last change on this file since 906 was 859, checked in by Sam Hocevar, 17 years ago
  • Removed duplicate uint*_t defines from *_internal.h and included common.h in all .c files that needed it.
  • Property svn:keywords set to Id
File size: 14.3 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: driver_slang.c 859 2006-04-24 20:35:59Z sam $
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 *  This file contains the libcaca SLang input and output driver
16 */
17
18#include "config.h"
19#include "common.h"
20
21#if defined(USE_SLANG)
22
23#if defined(HAVE_SLANG_SLANG_H)
24#   include <slang/slang.h>
25#else
26#   include <slang.h>
27#endif
28
29#include <stdlib.h>
30#include <string.h>
31
32#if defined(HAVE_SIGNAL_H)
33#   include <signal.h>
34#endif
35
36#include "caca.h"
37#include "caca_internals.h"
38#include "cucul.h"
39#include "cucul_internals.h"
40
41/*
42 * Global variables
43 */
44
45/* Tables generated by tools/optipal.c */
46static int const slang_palette[2*16*16] =
47{
48     1,  0,   2,  0,   3,  0,   4,  0,   5,  0,   6,  0,   7,  0,   8,  0,
49     9,  0,  10,  0,  11,  0,  12,  0,  13,  0,  14,  0,  15,  0,   0,  8,
50     8,  7,   7,  8,  15,  7,   7, 15,  15,  9,   9, 15,   1,  9,   9,  1,
51     7,  9,   9,  7,   8,  1,   1,  8,   0,  1,  15, 10,  10, 15,   2, 10,
52    10,  2,   7, 10,  10,  7,   8,  2,   2,  8,   0,  2,  15, 11,  11, 15,
53     3, 11,  11,  3,   7, 11,  11,  7,   8,  3,   3,  8,   0,  3,  15, 12,
54    12, 15,   4, 12,  12,  4,   7, 12,  12,  7,   8,  4,   4,  8,   0,  4,
55    15, 13,  13, 15,   5, 13,  13,  5,   7, 13,  13,  7,   8,  5,   5,  8,
56     0,  5,  15, 14,  14, 15,   6, 14,  14,  6,   7, 14,  14,  7,   8,  6,
57     6,  8,   0,  6,   4,  6,   6,  4,  12, 14,  14, 12,   6,  2,   2,  6,
58    14, 10,  10, 14,   2,  3,   3,  2,  10, 11,  11, 10,   3,  1,   1,  3,
59    11,  9,   9, 11,   1,  5,   5,  1,   9, 13,  13,  9,   5,  4,   4,  5,
60    13, 12,  12, 13,   4, 14,   6, 12,  12,  6,  14,  4,   6, 10,   2, 14,
61    14,  2,  10,  6,   2, 11,   3, 10,  10,  3,  11,  2,   3,  9,   1, 11,
62    11,  1,   9,  3,   1, 13,   5,  9,   9,  5,  13,  1,   5, 12,   4, 13,
63    13,  4,  12,  5,   0,  7,   0, 15,  15,  8,   8, 15,  15,  1,   7,  1,
64     1,  6,   2,  5,   3,  4,   4,  3,   5,  2,   6,  1,   0,  0,   1,  1,
65     9,  6,  10,  5,  11,  4,  12,  3,  13,  2,  14,  1,   2,  2,   3,  3,
66     4,  4,   5,  5,   6,  6,   7,  7,  14,  9,   1, 15,   8,  9,   8,  8,
67     9,  9,   1,  7,   0,  9,   9,  8,   6,  9,  13, 10,   2, 15,   8, 10,
68     7,  2,  15,  2,   2,  7,   0, 10,  10,  8,   5, 10,  12, 11,   3, 15,
69     8, 11,   7,  3,  15,  3,   3,  7,   0, 11,  11,  8,   4, 11,  11, 12,
70     4, 15,   8, 12,   7,  4,  15,  4,   4,  7,   0, 12,  12,  8,   3, 12,
71    10, 13,   5, 15,   8, 13,   7,  5,  15,  5,   5,  7,   0, 13,  13,  8,
72     2, 13,   9, 14,   6, 15,   8, 14,   7,  6,  15,  6,   6,  7,   0, 14,
73    14,  8,   1, 14,   5,  6,   2,  4,  13, 14,  10, 12,   4,  2,   3,  6,
74    12, 10,  11, 14,   6,  3,   1,  2,  14, 11,   9, 10,   2,  1,   5,  3,
75    10,  9,  13, 11,   3,  5,   4,  1,  11, 13,  12,  9,   1,  4,   6,  5,
76     9, 12,  14, 13,   5, 14,   2, 12,  13,  6,  10,  4,   4, 10,   3, 14,
77    12,  2,  11,  6,   6, 11,   1, 10,  14,  3,   9,  2,   2,  9,   5, 11,
78    10,  1,  13,  3,   3, 13,   4,  9,  11,  5,  12,  1,   1, 12,   6, 13,
79     9,  4,  14,  5,  10, 10,  11, 11,  12, 12,  13, 13,  14, 14,  15, 15,
80};
81
82static int const slang_assoc[16*16] =
83{
84    134, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
85    28, 135, 214, 86, 219, 91, 133, 127, 26, 23, 240, 112, 245, 117, 141, 126,
86    37, 211, 142, 83, 206, 132, 78, 160, 35, 237, 32, 109, 232, 140, 104, 161,
87    46, 87, 82, 143, 131, 215, 210, 169, 44, 113, 108, 41, 139, 241, 236, 170,
88    55, 222, 203, 130, 144, 94, 75, 178, 53, 248, 229, 138, 50, 120, 101, 179,
89    64, 90, 129, 218, 95, 145, 223, 187, 62, 116, 137, 244, 121, 59, 249, 188,
90    73, 128, 79, 207, 74, 202, 146, 196, 71, 136, 105, 233, 100, 228, 68, 197,
91    122, 153, 162, 171, 180, 189, 198, 147, 16, 25, 34, 43, 52, 61, 70, 18,
92    15, 27, 36, 45, 54, 63, 72, 17, 151, 155, 164, 173, 182, 191, 200, 124,
93    154, 22, 238, 110, 243, 115, 156, 24, 150, 152, 216, 88, 221, 93, 148, 20,
94    163, 235, 31, 107, 230, 165, 102, 33, 159, 213, 250, 85, 208, 157, 80, 29,
95    172, 111, 106, 40, 174, 239, 234, 42, 168, 89, 84, 251, 166, 217, 212, 38,
96    181, 246, 227, 183, 49, 118, 99, 51, 177, 224, 205, 175, 252, 96, 77, 47,
97    190, 114, 192, 242, 119, 58, 247, 60, 186, 92, 184, 220, 97, 253, 225, 56,
98    199, 201, 103, 231, 98, 226, 67, 69, 195, 193, 81, 209, 76, 204, 254, 65,
99    123, 149, 158, 167, 176, 185, 194, 19, 125, 21, 30, 39, 48, 57, 66, 255,
100};
101
102/*
103 * Local functions
104 */
105static void slang_init_palette(void);
106static void slang_write_utf32(uint32_t);
107
108#if defined(HAVE_SIGNAL)
109static RETSIGTYPE sigwinch_handler(int);
110static caca_display_t *sigwinch_d; /* FIXME: we ought to get rid of this */
111#endif
112#if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
113static void slang_check_terminal(void);
114#endif
115
116static int slang_init_graphics(caca_display_t *dp)
117{
118#if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
119    slang_check_terminal();
120#endif
121
122#if defined(HAVE_SIGNAL)
123    sigwinch_d = dp;
124    signal(SIGWINCH, sigwinch_handler);
125#endif
126
127    /* Initialise slang library */
128    SLsig_block_signals();
129    SLtt_get_terminfo();
130
131    if(SLkp_init() == -1)
132    {
133        SLsig_unblock_signals();
134        return -1;
135    }
136
137    SLang_init_tty(-1, 0, 1);
138
139    if(SLsmg_init_smg() == -1)
140    {
141        SLsig_unblock_signals();
142        return -1;
143    }
144
145    SLsig_unblock_signals();
146
147    SLsmg_cls();
148    SLtt_set_cursor_visibility(0);
149    SLkp_define_keysym("\e[M", 1001);
150    SLtt_set_mouse_mode(1, 0);
151    SLsmg_refresh();
152
153    /* Disable scrolling so that hashmap scrolling optimization code
154     * does not cause ugly refreshes due to slow terminals */
155    SLtt_Term_Cannot_Scroll = 1;
156
157    slang_init_palette();
158
159    /* Disable alt charset support so that we get a chance to have all
160     * 256 colour pairs */
161    SLtt_Has_Alt_Charset = 0;
162
163#ifdef HAVE_SLSMG_UTF8_ENABLE
164    SLsmg_utf8_enable(1); /* 1 == force, 0 == disable, -1 == autodetect */
165    SLtt_utf8_enable(1);
166#endif
167
168    _cucul_set_canvas_size(dp->cv, SLtt_Screen_Cols, SLtt_Screen_Rows);
169
170    return 0;
171}
172
173static int slang_end_graphics(caca_display_t *dp)
174{
175    SLtt_set_mouse_mode(0, 0);
176    SLtt_set_cursor_visibility(1);
177    SLang_reset_tty();
178    SLsmg_reset_smg();
179
180    return 0;
181}
182
183static int slang_set_display_title(caca_display_t *dp, char const *title)
184{
185    /* FIXME */
186    return 0;
187}
188
189static unsigned int slang_get_display_width(caca_display_t *dp)
190{
191    /* Fallback to a 6x10 font */
192    return dp->cv->width * 6;
193}
194
195static unsigned int slang_get_display_height(caca_display_t *dp)
196{
197    /* Fallback to a 6x10 font */
198    return dp->cv->height * 10;
199}
200
201static void slang_display(caca_display_t *dp)
202{
203    int x, y;
204    uint32_t *attr = dp->cv->attr;
205    uint32_t *chars = dp->cv->chars;
206    for(y = 0; y < (int)dp->cv->height; y++)
207    {
208        SLsmg_gotorc(y, 0);
209        for(x = dp->cv->width; x--; )
210        {
211            uint32_t ch = *chars++;
212
213#if defined(OPTIMISE_SLANG_PALETTE)
214            uint8_t fgcolor = _cucul_argb32_to_ansi4fg(*attr);
215            uint8_t bgcolor = _cucul_argb32_to_ansi4bg(*attr);
216
217            /* If foreground == background, just don't use this colour
218             * pair, and print a space instead of the real character. */
219            if(fgcolor != bgcolor)
220            {
221                SLsmg_set_color(slang_assoc[_cucul_argb32_to_ansi8(*attr++)]);
222                slang_write_utf32(ch);
223            }
224            else
225            {
226                if(fgcolor == CUCUL_COLOR_BLACK)
227                    fgcolor = CUCUL_COLOR_WHITE;
228                else if(fgcolor == CUCUL_COLOR_WHITE
229                         || fgcolor <= CUCUL_COLOR_LIGHTGRAY)
230                    fgcolor = CUCUL_COLOR_BLACK;
231                else
232                    fgcolor = CUCUL_COLOR_WHITE;
233                SLsmg_set_color(slang_assoc[fgcolor + 16 * bgcolor]);
234                SLsmg_write_char(' ');
235                attr++;
236            }
237#else
238            SLsmg_set_color(_cucul_argb32_to_ansi8(*attr++));
239            slang_write_utf32(ch);
240#endif
241        }
242    }
243    SLsmg_refresh();
244}
245
246static void slang_handle_resize(caca_display_t *dp)
247{
248    SLtt_get_screen_size();
249    dp->resize.w = SLtt_Screen_Cols;
250    dp->resize.h = SLtt_Screen_Rows;
251
252    if(dp->resize.w != dp->cv->width || dp->resize.h != dp->cv->height)
253        SLsmg_reinit_smg();
254}
255
256static int slang_get_event(caca_display_t *dp, caca_event_t *ev)
257{
258    int intkey;
259
260    if(!SLang_input_pending(0))
261    {
262        ev->type = CACA_EVENT_NONE;
263        return 0;
264    }
265
266    /* We first use SLang_getkey() to see whether Esc was pressed
267     * alone, then (if it wasn't) we unget the key and use SLkp_getkey()
268     * instead, so that escape sequences are interpreted. */
269    intkey = SLang_getkey();
270
271    if(intkey != 0x1b /* Esc */ || SLang_input_pending(0))
272    {
273        SLang_ungetkey(intkey);
274        intkey = SLkp_getkey();
275    }
276
277    /* If the key was ASCII, return it immediately */
278    if(intkey < 0x100)
279    {
280        ev->type = CACA_EVENT_KEY_PRESS;
281        ev->data.key.ch = intkey;
282        return 1;
283    }
284
285    if(intkey == 0x3e9)
286    {
287        int button = (SLang_getkey() - ' ' + 1) & 0xf;
288        unsigned int x = SLang_getkey() - '!';
289        unsigned int y = SLang_getkey() - '!';
290
291        ev->data.mouse.button = button;
292        ev->type = CACA_EVENT_MOUSE_PRESS;
293        _push_event(dp, ev);
294        ev->type = CACA_EVENT_MOUSE_RELEASE;
295        _push_event(dp, ev);
296
297        if(dp->mouse.x == x && dp->mouse.y == y)
298            return _pop_event(dp, ev);
299
300        dp->mouse.x = x;
301        dp->mouse.y = y;
302
303        ev->type = CACA_EVENT_MOUSE_MOTION;
304        ev->data.mouse.x = dp->mouse.x;
305        ev->data.mouse.y = dp->mouse.y;
306        return 1;
307    }
308
309    switch(intkey)
310    {
311        case SL_KEY_UP: ev->data.key.ch = CACA_KEY_UP; break;
312        case SL_KEY_DOWN: ev->data.key.ch = CACA_KEY_DOWN; break;
313        case SL_KEY_LEFT: ev->data.key.ch = CACA_KEY_LEFT; break;
314        case SL_KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break;
315
316        case SL_KEY_IC: ev->data.key.ch = CACA_KEY_INSERT; break;
317        case SL_KEY_DELETE: ev->data.key.ch = CACA_KEY_DELETE; break;
318        case SL_KEY_HOME: ev->data.key.ch = CACA_KEY_HOME; break;
319        case SL_KEY_END: ev->data.key.ch = CACA_KEY_END; break;
320        case SL_KEY_PPAGE: ev->data.key.ch = CACA_KEY_PAGEUP; break;
321        case SL_KEY_NPAGE: ev->data.key.ch = CACA_KEY_PAGEDOWN; break;
322
323        case SL_KEY_F(1): ev->data.key.ch = CACA_KEY_F1; break;
324        case SL_KEY_F(2): ev->data.key.ch = CACA_KEY_F2; break;
325        case SL_KEY_F(3): ev->data.key.ch = CACA_KEY_F3; break;
326        case SL_KEY_F(4): ev->data.key.ch = CACA_KEY_F4; break;
327        case SL_KEY_F(5): ev->data.key.ch = CACA_KEY_F5; break;
328        case SL_KEY_F(6): ev->data.key.ch = CACA_KEY_F6; break;
329        case SL_KEY_F(7): ev->data.key.ch = CACA_KEY_F7; break;
330        case SL_KEY_F(8): ev->data.key.ch = CACA_KEY_F8; break;
331        case SL_KEY_F(9): ev->data.key.ch = CACA_KEY_F9; break;
332        case SL_KEY_F(10): ev->data.key.ch = CACA_KEY_F10; break;
333        case SL_KEY_F(11): ev->data.key.ch = CACA_KEY_F11; break;
334        case SL_KEY_F(12): ev->data.key.ch = CACA_KEY_F12; break;
335
336        default: ev->type = CACA_EVENT_NONE; return 0;
337    }
338
339    ev->type = CACA_EVENT_KEY_PRESS;
340    ev->data.key.ucs4 = 0;
341    ev->data.key.utf8[0] = '\0';
342    return 1;
343}
344
345/*
346 * XXX: following functions are local
347 */
348
349static void slang_init_palette(void)
350{
351    /* See SLang ref., 5.4.4. */
352    static char *slang_colors[16] =
353    {
354        /* Standard colours */
355        "black",
356        "blue",
357        "green",
358        "cyan",
359        "red",
360        "magenta",
361        "brown",
362        "lightgray",
363        /* Bright colours */
364        "gray",
365        "brightblue",
366        "brightgreen",
367        "brightcyan",
368        "brightred",
369        "brightmagenta",
370        "yellow",
371        "white",
372    };
373
374#if defined(OPTIMISE_SLANG_PALETTE)
375    int i;
376
377    for(i = 0; i < 16 * 16; i++)
378        SLtt_set_color(i, NULL, slang_colors[slang_palette[i * 2]],
379                                slang_colors[slang_palette[i * 2 + 1]]);
380#else
381    int fg, bg;
382
383    for(bg = 0; bg < 16; bg++)
384        for(fg = 0; fg < 16; fg++)
385        {
386            int i = fg + 16 * bg;
387            SLtt_set_color(i, NULL, slang_colors[fg], slang_colors[bg]);
388        }
389#endif
390}
391
392static void slang_write_utf32(uint32_t ch)
393{
394#ifdef HAVE_SLSMG_UTF8_ENABLE
395    static const uint8_t mark[7] =
396    {
397        0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
398    };
399
400    char buf[10], *parser;
401    int bytes;
402#endif
403
404    if(ch < 0x80)
405    {
406        SLsmg_write_char(ch);
407        return;
408    }
409
410#ifdef HAVE_SLSMG_UTF8_ENABLE
411    bytes = (ch < 0x800) ? 2 : (ch < 0x10000) ? 3 : 4;
412    buf[bytes] = '\0';
413    parser = buf + bytes;
414
415    switch(bytes)
416    {
417        case 4: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
418        case 3: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
419        case 2: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
420    }
421    *--parser = ch | mark[bytes];
422
423    SLsmg_write_string(buf);
424#else
425    SLsmg_write_char(' ');
426#endif
427}
428
429#if defined(HAVE_SIGNAL)
430static RETSIGTYPE sigwinch_handler(int sig)
431{
432    sigwinch_d->resize.resized = 1;
433
434    signal(SIGWINCH, sigwinch_handler);
435}
436#endif
437
438#if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
439static void slang_check_terminal(void)
440{
441    char *term, *colorterm, *other;
442
443    term = getenv("TERM");
444    colorterm = getenv("COLORTERM");
445
446    if(term && !strcmp(term, "xterm"))
447    {
448        /* If we are using gnome-terminal, it's really a 16 colour terminal */
449        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
450        {
451            (void)putenv("TERM=xterm-16color");
452            return;
453        }
454
455        /* Ditto if we are using Konsole */
456        other = getenv("KONSOLE_DCOP_SESSION");
457        if(other)
458        {
459            (void)putenv("TERM=xterm-16color");
460            return;
461        }
462    }
463}
464#endif
465
466/*
467 * Driver initialisation
468 */
469
470int slang_install(caca_display_t *dp)
471{
472    dp->drv.driver = CACA_DRIVER_SLANG;
473
474    dp->drv.init_graphics = slang_init_graphics;
475    dp->drv.end_graphics = slang_end_graphics;
476    dp->drv.set_display_title = slang_set_display_title;
477    dp->drv.get_display_width = slang_get_display_width;
478    dp->drv.get_display_height = slang_get_display_height;
479    dp->drv.display = slang_display;
480    dp->drv.handle_resize = slang_handle_resize;
481    dp->drv.get_event = slang_get_event;
482    dp->drv.set_mouse = NULL;
483
484    return 0;
485}
486
487#endif /* USE_SLANG */
488
Note: See TracBrowser for help on using the repository browser.