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

Last change on this file since 1044 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
RevLine 
[35]1/*
[672]2 *  libcaca       Colour ASCII-Art library
[527]3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
[268]4 *                All Rights Reserved
[35]5 *
[769]6 *  $Id: driver_ncurses.c 1006 2006-08-02 13:12:43Z sam $
7 *
[268]8 *  This library is free software; you can redistribute it and/or
[522]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.
[35]12 */
[17]13
[769]14/*
[540]15 *  This file contains the libcaca Ncurses input and output driver
[205]16 */
17
[63]18#include "config.h"
[859]19#include "common.h"
[63]20
[984]21#if defined USE_NCURSES
[539]22
[984]23#if defined HAVE_NCURSESW_NCURSES_H
[629]24#   include <ncursesw/ncurses.h>
[984]25#elif defined HAVE_NCURSES_NCURSES_H
[629]26#   include <ncurses/ncurses.h>
[984]27#elif defined HAVE_NCURSES_H
[539]28#   include <ncurses.h>
29#else
30#   include <curses.h>
31#endif
32
[550]33#include <stdlib.h>
34#include <string.h>
35
[984]36#if defined HAVE_SIGNAL_H
[348]37#   include <signal.h>
38#endif
[984]39#if defined HAVE_SYS_IOCTL_H
[348]40#   include <sys/ioctl.h>
41#endif
[984]42#if defined HAVE_LOCALE_H
43#   include <locale.h>
44#endif
[348]45
[185]46#include "caca.h"
47#include "caca_internals.h"
[524]48#include "cucul.h"
49#include "cucul_internals.h"
[17]50
[281]51/*
52 * Local functions
53 */
[540]54
[984]55#if defined HAVE_SIGNAL
[348]56static RETSIGTYPE sigwinch_handler(int);
[811]57static caca_display_t *sigwinch_d; /* FIXME: we ought to get rid of this */
[348]58#endif
[984]59#if defined HAVE_GETENV && defined HAVE_PUTENV
[550]60static void ncurses_check_terminal(void);
61#endif
[629]62static void ncurses_write_utf32(uint32_t);
[348]63
[550]64struct driver_private
65{
66    int attr[16*16];
67    mmask_t oldmask;
68};
69
[811]70static int ncurses_init_graphics(caca_display_t *dp)
[539]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    };
[300]93
[539]94    mmask_t newmask;
95    int fg, bg, max;
[483]96
[811]97    dp->drv.p = malloc(sizeof(struct driver_private));
[550]98
[984]99#if defined HAVE_GETENV && defined HAVE_PUTENV
[550]100    ncurses_check_terminal();
101#endif
102
[984]103#if defined HAVE_SIGNAL
[811]104    sigwinch_d = dp;
[348]105    signal(SIGWINCH, sigwinch_handler);
106#endif
107
[984]108#if defined HAVE_LOCALE_H
109    setlocale(LC_ALL, "");
110#endif
111
[539]112    initscr();
113    keypad(stdscr, TRUE);
114    nonl();
115    raw();
116    noecho();
117    nodelay(stdscr, TRUE);
118    curs_set(0);
[227]119
[539]120    /* Activate mouse */
121    newmask = REPORT_MOUSE_POSITION | ALL_MOUSE_EVENTS;
[811]122    mousemask(newmask, &dp->drv.p->oldmask);
[539]123    mouseinterval(-1); /* No click emulation */
[231]124
[539]125    /* Set the escape delay to a ridiculously low value */
126    ESCDELAY = 10;
[227]127
[539]128    /* Activate colour */
129    start_color();
[227]130
[539]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;
[227]138
[539]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]);
[811]147            dp->drv.p->attr[fg + 16 * bg] = COLOR_PAIR(col);
[227]148
[539]149            if(max == 8)
[265]150            {
[539]151                /* Bright fg on simple bg */
[811]152                dp->drv.p->attr[fg + 8 + 16 * bg] = A_BOLD | COLOR_PAIR(col);
[539]153                /* Simple fg on bright bg */
[811]154                dp->drv.p->attr[fg + 16 * (bg + 8)] = A_BLINK
[539]155                                                    | COLOR_PAIR(col);
156                /* Bright fg on bright bg */
[811]157                dp->drv.p->attr[fg + 8 + 16 * (bg + 8)] = A_BLINK | A_BOLD
[263]158                                                        | COLOR_PAIR(col);
[227]159            }
[265]160        }
[263]161
[813]162    _cucul_set_canvas_size(dp->cv, COLS, LINES);
[300]163
[227]164    return 0;
165}
166
[811]167static int ncurses_end_graphics(caca_display_t *dp)
[251]168{
[811]169    mousemask(dp->drv.p->oldmask, NULL);
[539]170    curs_set(1);
171    noraw();
172    endwin();
[300]173
[811]174    free(dp->drv.p);
[550]175
[251]176    return 0;
177}
178
[819]179static int ncurses_set_display_title(caca_display_t *dp, char const *title)
[343]180{
[1006]181    return -1;
[343]182}
183
[819]184static unsigned int ncurses_get_display_width(caca_display_t *dp)
[352]185{
186    /* Fallback to a 6x10 font */
[811]187    return dp->cv->width * 6;
[352]188}
189
[819]190static unsigned int ncurses_get_display_height(caca_display_t *dp)
[352]191{
192    /* Fallback to a 6x10 font */
[811]193    return dp->cv->height * 10;
[352]194}
195
[811]196static void ncurses_display(caca_display_t *dp)
[227]197{
[539]198    int x, y;
[811]199    uint32_t *attr = dp->cv->attr;
200    uint32_t *chars = dp->cv->chars;
201    for(y = 0; y < (int)dp->cv->height; y++)
[265]202    {
[539]203        move(y, 0);
[811]204        for(x = dp->cv->width; x--; )
[527]205        {
[811]206            attrset(dp->drv.p->attr[_cucul_argb32_to_ansi8(*attr++)]);
[629]207            ncurses_write_utf32(*chars++);
[527]208        }
[265]209    }
[539]210    refresh();
[227]211}
212
[811]213static void ncurses_handle_resize(caca_display_t *dp)
[347]214{
[539]215    struct winsize size;
[347]216
[984]217#if defined HAVE_SYS_IOCTL_H
[539]218    if(ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0)
[347]219    {
[811]220        dp->resize.w = size.ws_col;
221        dp->resize.h = size.ws_row;
[984]222#if defined HAVE_RESIZE_TERM
[811]223        resize_term(dp->resize.h, dp->resize.w);
[364]224#else
[811]225        resizeterm(*dp->resize.h, *dp->resize.w);
[364]226#endif
[539]227        wrefresh(curscr);
[553]228        return;
[347]229    }
[822]230#endif
[553]231
232    /* Fallback */
[811]233    dp->resize.w = dp->cv->width;
234    dp->resize.h = dp->cv->height;
[347]235}
236
[811]237static int ncurses_get_event(caca_display_t *dp, caca_event_t *ev)
[548]238{
239    int intkey;
240
241    intkey = getch();
242    if(intkey == ERR)
[681]243    {
244        ev->type = CACA_EVENT_NONE;
245        return 0;
246    }
[548]247
[974]248    if(intkey < 0x7f)
[548]249    {
[681]250        ev->type = CACA_EVENT_KEY_PRESS;
[810]251        ev->data.key.ch = intkey;
[971]252        ev->data.key.utf32 = intkey;
253        ev->data.key.utf8[0] = intkey;
254        ev->data.key.utf8[1] = '\0';
[681]255        return 1;
[548]256    }
257
[986]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
[548]291    if(intkey == KEY_MOUSE)
292    {
293        MEVENT mevent;
294        getmouse(&mevent);
295
296        switch(mevent.bstate)
297        {
[980]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;
[548]309
[980]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;
[548]316
[980]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;
[548]323
[980]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;
[548]330
331            default:
332                break;
[980]333#undef PRESS
334#undef RELEASE
335#undef CLICK
[548]336        }
337
[811]338        if(dp->mouse.x == (unsigned int)mevent.x &&
339           dp->mouse.y == (unsigned int)mevent.y)
340            return _pop_event(dp, ev);
[548]341
[811]342        dp->mouse.x = mevent.x;
343        dp->mouse.y = mevent.y;
[548]344
[681]345        ev->type = CACA_EVENT_MOUSE_MOTION;
[811]346        ev->data.mouse.x = dp->mouse.x;
347        ev->data.mouse.y = dp->mouse.y;
[681]348        return 1;
[548]349    }
350
351    switch(intkey)
352    {
[810]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;
[548]357
[810]358        case KEY_IC: ev->data.key.ch = CACA_KEY_INSERT; break;
359        case KEY_DC: ev->data.key.ch = CACA_KEY_DELETE; break;
[974]360        case 0x7f:
[971]361        case KEY_BACKSPACE: ev->data.key.ch = CACA_KEY_BACKSPACE; break;
[810]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;
[548]366
[810]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;
[681]379
[980]380        default:
381            /* Unknown key */
382            ev->type = CACA_EVENT_NONE; return 0;
[548]383    }
384
[681]385    ev->type = CACA_EVENT_KEY_PRESS;
[969]386    ev->data.key.utf32 = 0;
[681]387    ev->data.key.utf8[0] = '\0';
388    return 1;
[548]389}
390
[539]391/*
392 * XXX: following functions are local
393 */
[281]394
[984]395#if defined HAVE_SIGNAL
[348]396static RETSIGTYPE sigwinch_handler(int sig)
397{
[811]398    sigwinch_d->resize.resized = 1;
[348]399
[613]400    signal(SIGWINCH, sigwinch_handler);
[348]401}
402#endif
403
[984]404#if defined HAVE_GETENV && defined HAVE_PUTENV
[550]405static void ncurses_check_terminal(void)
406{
[986]407    char *term, *colorterm;
[550]408
409    term = getenv("TERM");
410    colorterm = getenv("COLORTERM");
411
[986]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"))
[550]420    {
[986]421        SCREEN *screen;
422        screen = newterm("xterm-16color", stdout, stdin);
423        if(screen == NULL)
[550]424            return;
[986]425        endwin();
426        (void)putenv("TERM=xterm-16color");
427        return;
[550]428    }
429}
430#endif
431
[811]432static void ncurses_write_utf32(uint32_t ch)
[629]433{
[984]434#if defined HAVE_NCURSESW_NCURSES_H
[929]435    char buf[10];
[629]436    int bytes;
437
[962]438    bytes = cucul_utf32_to_utf8(buf, ch);
[629]439    buf[bytes] = '\0';
440    addstr(buf);
441#else
[929]442    addch(ch < 0x80 ? ch : '?');
[629]443#endif
444}
445
[539]446/*
447 * Driver initialisation
448 */
[527]449
[811]450int ncurses_install(caca_display_t *dp)
[511]451{
[811]452    dp->drv.driver = CACA_DRIVER_NCURSES;
[527]453
[811]454    dp->drv.init_graphics = ncurses_init_graphics;
455    dp->drv.end_graphics = ncurses_end_graphics;
[819]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;
[811]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;
[689]463
[684]464    return 0;
[511]465}
466
[539]467#endif /* USE_NCURSES */
[527]468
Note: See TracBrowser for help on using the repository browser.