source: libcaca/trunk/caca/driver_ncurses.c @ 1024

Last change on this file since 1024 was 1006, checked in by Sam Hocevar, 14 years ago
  • Fixed libcaca prototypes so that all functions use the errno mechanism and return a value.
  • Property svn:keywords set to Id
File size: 12.5 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_ncurses.c 1006 2006-08-02 13:12:43Z 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 Ncurses input and output driver
16 */
17
18#include "config.h"
19#include "common.h"
20
21#if defined USE_NCURSES
22
23#if defined HAVE_NCURSESW_NCURSES_H
24#   include <ncursesw/ncurses.h>
25#elif defined HAVE_NCURSES_NCURSES_H
26#   include <ncurses/ncurses.h>
27#elif defined HAVE_NCURSES_H
28#   include <ncurses.h>
29#else
30#   include <curses.h>
31#endif
32
33#include <stdlib.h>
34#include <string.h>
35
36#if defined HAVE_SIGNAL_H
37#   include <signal.h>
38#endif
39#if defined HAVE_SYS_IOCTL_H
40#   include <sys/ioctl.h>
41#endif
42#if defined HAVE_LOCALE_H
43#   include <locale.h>
44#endif
45
46#include "caca.h"
47#include "caca_internals.h"
48#include "cucul.h"
49#include "cucul_internals.h"
50
51/*
52 * Local functions
53 */
54
55#if defined HAVE_SIGNAL
56static RETSIGTYPE sigwinch_handler(int);
57static caca_display_t *sigwinch_d; /* FIXME: we ought to get rid of this */
58#endif
59#if defined HAVE_GETENV && defined HAVE_PUTENV
60static void ncurses_check_terminal(void);
61#endif
62static void ncurses_write_utf32(uint32_t);
63
64struct driver_private
65{
66    int attr[16*16];
67    mmask_t oldmask;
68};
69
70static int ncurses_init_graphics(caca_display_t *dp)
71{
72    static int curses_colors[] =
73    {
74        /* Standard curses colours */
75        COLOR_BLACK,
76        COLOR_BLUE,
77        COLOR_GREEN,
78        COLOR_CYAN,
79        COLOR_RED,
80        COLOR_MAGENTA,
81        COLOR_YELLOW,
82        COLOR_WHITE,
83        /* Extra values for xterm-16color */
84        COLOR_BLACK + 8,
85        COLOR_BLUE + 8,
86        COLOR_GREEN + 8,
87        COLOR_CYAN + 8,
88        COLOR_RED + 8,
89        COLOR_MAGENTA + 8,
90        COLOR_YELLOW + 8,
91        COLOR_WHITE + 8
92    };
93
94    mmask_t newmask;
95    int fg, bg, max;
96
97    dp->drv.p = malloc(sizeof(struct driver_private));
98
99#if defined HAVE_GETENV && defined HAVE_PUTENV
100    ncurses_check_terminal();
101#endif
102
103#if defined HAVE_SIGNAL
104    sigwinch_d = dp;
105    signal(SIGWINCH, sigwinch_handler);
106#endif
107
108#if defined HAVE_LOCALE_H
109    setlocale(LC_ALL, "");
110#endif
111
112    initscr();
113    keypad(stdscr, TRUE);
114    nonl();
115    raw();
116    noecho();
117    nodelay(stdscr, TRUE);
118    curs_set(0);
119
120    /* Activate mouse */
121    newmask = REPORT_MOUSE_POSITION | ALL_MOUSE_EVENTS;
122    mousemask(newmask, &dp->drv.p->oldmask);
123    mouseinterval(-1); /* No click emulation */
124
125    /* Set the escape delay to a ridiculously low value */
126    ESCDELAY = 10;
127
128    /* Activate colour */
129    start_color();
130
131    /* If COLORS == 16, it means the terminal supports full bright colours
132     * using setab and setaf (will use \e[90m \e[91m etc. for colours >= 8),
133     * we can build 16*16 colour pairs.
134     * If COLORS == 8, it means the terminal does not know about bright
135     * colours and we need to get them through A_BOLD and A_BLINK (\e[1m
136     * and \e[5m). We can only build 8*8 colour pairs. */
137    max = COLORS >= 16 ? 16 : 8;
138
139    for(bg = 0; bg < max; bg++)
140        for(fg = 0; fg < max; fg++)
141        {
142            /* Use ((max + 7 - fg) % max) instead of fg so that colour 0
143             * is light gray on black. Some terminals don't like this
144             * colour pair to be redefined. */
145            int col = ((max + 7 - fg) % max) + max * bg;
146            init_pair(col, curses_colors[fg], curses_colors[bg]);
147            dp->drv.p->attr[fg + 16 * bg] = COLOR_PAIR(col);
148
149            if(max == 8)
150            {
151                /* Bright fg on simple bg */
152                dp->drv.p->attr[fg + 8 + 16 * bg] = A_BOLD | COLOR_PAIR(col);
153                /* Simple fg on bright bg */
154                dp->drv.p->attr[fg + 16 * (bg + 8)] = A_BLINK
155                                                    | COLOR_PAIR(col);
156                /* Bright fg on bright bg */
157                dp->drv.p->attr[fg + 8 + 16 * (bg + 8)] = A_BLINK | A_BOLD
158                                                        | COLOR_PAIR(col);
159            }
160        }
161
162    _cucul_set_canvas_size(dp->cv, COLS, LINES);
163
164    return 0;
165}
166
167static int ncurses_end_graphics(caca_display_t *dp)
168{
169    mousemask(dp->drv.p->oldmask, NULL);
170    curs_set(1);
171    noraw();
172    endwin();
173
174    free(dp->drv.p);
175
176    return 0;
177}
178
179static int ncurses_set_display_title(caca_display_t *dp, char const *title)
180{
181    return -1;
182}
183
184static unsigned int ncurses_get_display_width(caca_display_t *dp)
185{
186    /* Fallback to a 6x10 font */
187    return dp->cv->width * 6;
188}
189
190static unsigned int ncurses_get_display_height(caca_display_t *dp)
191{
192    /* Fallback to a 6x10 font */
193    return dp->cv->height * 10;
194}
195
196static void ncurses_display(caca_display_t *dp)
197{
198    int x, y;
199    uint32_t *attr = dp->cv->attr;
200    uint32_t *chars = dp->cv->chars;
201    for(y = 0; y < (int)dp->cv->height; y++)
202    {
203        move(y, 0);
204        for(x = dp->cv->width; x--; )
205        {
206            attrset(dp->drv.p->attr[_cucul_argb32_to_ansi8(*attr++)]);
207            ncurses_write_utf32(*chars++);
208        }
209    }
210    refresh();
211}
212
213static void ncurses_handle_resize(caca_display_t *dp)
214{
215    struct winsize size;
216
217#if defined HAVE_SYS_IOCTL_H
218    if(ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0)
219    {
220        dp->resize.w = size.ws_col;
221        dp->resize.h = size.ws_row;
222#if defined HAVE_RESIZE_TERM
223        resize_term(dp->resize.h, dp->resize.w);
224#else
225        resizeterm(*dp->resize.h, *dp->resize.w);
226#endif
227        wrefresh(curscr);
228        return;
229    }
230#endif
231
232    /* Fallback */
233    dp->resize.w = dp->cv->width;
234    dp->resize.h = dp->cv->height;
235}
236
237static int ncurses_get_event(caca_display_t *dp, caca_event_t *ev)
238{
239    int intkey;
240
241    intkey = getch();
242    if(intkey == ERR)
243    {
244        ev->type = CACA_EVENT_NONE;
245        return 0;
246    }
247
248    if(intkey < 0x7f)
249    {
250        ev->type = CACA_EVENT_KEY_PRESS;
251        ev->data.key.ch = intkey;
252        ev->data.key.utf32 = intkey;
253        ev->data.key.utf8[0] = intkey;
254        ev->data.key.utf8[1] = '\0';
255        return 1;
256    }
257
258    /* If the key was UTF-8, parse the whole sequence */
259    if(intkey >= 0x80 && intkey < 0x100)
260    {
261        int keys[7]; /* Necessary for ungetch(); */
262        char utf8[7];
263        uint32_t utf32;
264        unsigned int i, bytes = 0;
265
266        keys[0] = intkey;
267        utf8[0] = intkey;
268
269        for(i = 1; i < 6; i++)
270        {
271            keys[i] = getch();
272            utf8[i] = (unsigned char)keys[i];
273        }
274
275        utf8[i] = '\0';
276        utf32 = cucul_utf8_to_utf32(utf8, &bytes);
277
278        while(i > bytes)
279            ungetch(keys[--i]);
280
281        if(bytes)
282        {
283            ev->type = CACA_EVENT_KEY_PRESS;
284            ev->data.key.ch = 0;
285            ev->data.key.utf32 = utf32;
286            strcpy(ev->data.key.utf8, utf8);
287            return 1;
288        }
289    }
290
291    if(intkey == KEY_MOUSE)
292    {
293        MEVENT mevent;
294        getmouse(&mevent);
295
296        switch(mevent.bstate)
297        {
298#define PRESS(x) ev->data.mouse.button = x; \
299                 ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev)
300#define RELEASE(x) ev->data.mouse.button = x; \
301                   ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev)
302#define CLICK(x) PRESS(x); RELEASE(x)
303            case BUTTON1_PRESSED: PRESS(1); break;
304            case BUTTON1_RELEASED: RELEASE(1); break;
305            case BUTTON1_CLICKED: CLICK(1); break;
306            case BUTTON1_DOUBLE_CLICKED: CLICK(1); CLICK(1); break;
307            case BUTTON1_TRIPLE_CLICKED: CLICK(1); CLICK(1); CLICK(1); break;
308            case BUTTON1_RESERVED_EVENT: break;
309
310            case BUTTON2_PRESSED: PRESS(2); break;
311            case BUTTON2_RELEASED: RELEASE(2); break;
312            case BUTTON2_CLICKED: CLICK(2); break;
313            case BUTTON2_DOUBLE_CLICKED: CLICK(2); CLICK(2); break;
314            case BUTTON2_TRIPLE_CLICKED: CLICK(2); CLICK(2); CLICK(2); break;
315            case BUTTON2_RESERVED_EVENT: break;
316
317            case BUTTON3_PRESSED: PRESS(3); break;
318            case BUTTON3_RELEASED: RELEASE(3); break;
319            case BUTTON3_CLICKED: CLICK(3); break;
320            case BUTTON3_DOUBLE_CLICKED: CLICK(3); CLICK(3); break;
321            case BUTTON3_TRIPLE_CLICKED: CLICK(3); CLICK(3); CLICK(3); break;
322            case BUTTON3_RESERVED_EVENT: break;
323
324            case BUTTON4_PRESSED: PRESS(4); break;
325            case BUTTON4_RELEASED: RELEASE(4); break;
326            case BUTTON4_CLICKED: CLICK(4); break;
327            case BUTTON4_DOUBLE_CLICKED: CLICK(4); CLICK(4); break;
328            case BUTTON4_TRIPLE_CLICKED: CLICK(4); CLICK(4); CLICK(4); break;
329            case BUTTON4_RESERVED_EVENT: break;
330
331            default:
332                break;
333#undef PRESS
334#undef RELEASE
335#undef CLICK
336        }
337
338        if(dp->mouse.x == (unsigned int)mevent.x &&
339           dp->mouse.y == (unsigned int)mevent.y)
340            return _pop_event(dp, ev);
341
342        dp->mouse.x = mevent.x;
343        dp->mouse.y = mevent.y;
344
345        ev->type = CACA_EVENT_MOUSE_MOTION;
346        ev->data.mouse.x = dp->mouse.x;
347        ev->data.mouse.y = dp->mouse.y;
348        return 1;
349    }
350
351    switch(intkey)
352    {
353        case KEY_UP: ev->data.key.ch = CACA_KEY_UP; break;
354        case KEY_DOWN: ev->data.key.ch = CACA_KEY_DOWN; break;
355        case KEY_LEFT: ev->data.key.ch = CACA_KEY_LEFT; break;
356        case KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break;
357
358        case KEY_IC: ev->data.key.ch = CACA_KEY_INSERT; break;
359        case KEY_DC: ev->data.key.ch = CACA_KEY_DELETE; break;
360        case 0x7f:
361        case KEY_BACKSPACE: ev->data.key.ch = CACA_KEY_BACKSPACE; break;
362        case KEY_HOME: ev->data.key.ch = CACA_KEY_HOME; break;
363        case KEY_END: ev->data.key.ch = CACA_KEY_END; break;
364        case KEY_PPAGE: ev->data.key.ch = CACA_KEY_PAGEUP; break;
365        case KEY_NPAGE: ev->data.key.ch = CACA_KEY_PAGEDOWN; break;
366
367        case KEY_F(1): ev->data.key.ch = CACA_KEY_F1; break;
368        case KEY_F(2): ev->data.key.ch = CACA_KEY_F2; break;
369        case KEY_F(3): ev->data.key.ch = CACA_KEY_F3; break;
370        case KEY_F(4): ev->data.key.ch = CACA_KEY_F4; break;
371        case KEY_F(5): ev->data.key.ch = CACA_KEY_F5; break;
372        case KEY_F(6): ev->data.key.ch = CACA_KEY_F6; break;
373        case KEY_F(7): ev->data.key.ch = CACA_KEY_F7; break;
374        case KEY_F(8): ev->data.key.ch = CACA_KEY_F8; break;
375        case KEY_F(9): ev->data.key.ch = CACA_KEY_F9; break;
376        case KEY_F(10): ev->data.key.ch = CACA_KEY_F10; break;
377        case KEY_F(11): ev->data.key.ch = CACA_KEY_F11; break;
378        case KEY_F(12): ev->data.key.ch = CACA_KEY_F12; break;
379
380        default:
381            /* Unknown key */
382            ev->type = CACA_EVENT_NONE; return 0;
383    }
384
385    ev->type = CACA_EVENT_KEY_PRESS;
386    ev->data.key.utf32 = 0;
387    ev->data.key.utf8[0] = '\0';
388    return 1;
389}
390
391/*
392 * XXX: following functions are local
393 */
394
395#if defined HAVE_SIGNAL
396static RETSIGTYPE sigwinch_handler(int sig)
397{
398    sigwinch_d->resize.resized = 1;
399
400    signal(SIGWINCH, sigwinch_handler);
401}
402#endif
403
404#if defined HAVE_GETENV && defined HAVE_PUTENV
405static void ncurses_check_terminal(void)
406{
407    char *term, *colorterm;
408
409    term = getenv("TERM");
410    colorterm = getenv("COLORTERM");
411
412    if(!term || strcmp(term, "xterm"))
413        return;
414
415    /* If we are using gnome-terminal, it's really a 16 colour terminal.
416     * Ditto if we are using xfce4-terminal, or Konsole. */
417    if((colorterm && (!strcmp(colorterm, "gnome-terminal")
418                       || !strcmp(colorterm, "Terminal")))
419         || getenv("KONSOLE_DCOP_SESSION"))
420    {
421        SCREEN *screen;
422        screen = newterm("xterm-16color", stdout, stdin);
423        if(screen == NULL)
424            return;
425        endwin();
426        (void)putenv("TERM=xterm-16color");
427        return;
428    }
429}
430#endif
431
432static void ncurses_write_utf32(uint32_t ch)
433{
434#if defined HAVE_NCURSESW_NCURSES_H
435    char buf[10];
436    int bytes;
437
438    bytes = cucul_utf32_to_utf8(buf, ch);
439    buf[bytes] = '\0';
440    addstr(buf);
441#else
442    addch(ch < 0x80 ? ch : '?');
443#endif
444}
445
446/*
447 * Driver initialisation
448 */
449
450int ncurses_install(caca_display_t *dp)
451{
452    dp->drv.driver = CACA_DRIVER_NCURSES;
453
454    dp->drv.init_graphics = ncurses_init_graphics;
455    dp->drv.end_graphics = ncurses_end_graphics;
456    dp->drv.set_display_title = ncurses_set_display_title;
457    dp->drv.get_display_width = ncurses_get_display_width;
458    dp->drv.get_display_height = ncurses_get_display_height;
459    dp->drv.display = ncurses_display;
460    dp->drv.handle_resize = ncurses_handle_resize;
461    dp->drv.get_event = ncurses_get_event;
462    dp->drv.set_mouse = NULL;
463
464    return 0;
465}
466
467#endif /* USE_NCURSES */
468
Note: See TracBrowser for help on using the repository browser.