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

Last change on this file since 984 was 984, checked in by Sam Hocevar, 15 years ago
  • Fixed UTF-8 support in ncurses.
  • Set ncurses's priority higher than slang.
  • Property svn:keywords set to Id
File size: 12.0 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 984 2006-05-25 22:32:03Z 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{
181    return 0;
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
258    if(intkey == KEY_MOUSE)
259    {
260        MEVENT mevent;
261        getmouse(&mevent);
262
263        switch(mevent.bstate)
264        {
[980]265#define PRESS(x) ev->data.mouse.button = x; \
266                 ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev)
267#define RELEASE(x) ev->data.mouse.button = x; \
268                   ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev)
269#define CLICK(x) PRESS(x); RELEASE(x)
270            case BUTTON1_PRESSED: PRESS(1); break;
271            case BUTTON1_RELEASED: RELEASE(1); break;
272            case BUTTON1_CLICKED: CLICK(1); break;
273            case BUTTON1_DOUBLE_CLICKED: CLICK(1); CLICK(1); break;
274            case BUTTON1_TRIPLE_CLICKED: CLICK(1); CLICK(1); CLICK(1); break;
275            case BUTTON1_RESERVED_EVENT: break;
[548]276
[980]277            case BUTTON2_PRESSED: PRESS(2); break;
278            case BUTTON2_RELEASED: RELEASE(2); break;
279            case BUTTON2_CLICKED: CLICK(2); break;
280            case BUTTON2_DOUBLE_CLICKED: CLICK(2); CLICK(2); break;
281            case BUTTON2_TRIPLE_CLICKED: CLICK(2); CLICK(2); CLICK(2); break;
282            case BUTTON2_RESERVED_EVENT: break;
[548]283
[980]284            case BUTTON3_PRESSED: PRESS(3); break;
285            case BUTTON3_RELEASED: RELEASE(3); break;
286            case BUTTON3_CLICKED: CLICK(3); break;
287            case BUTTON3_DOUBLE_CLICKED: CLICK(3); CLICK(3); break;
288            case BUTTON3_TRIPLE_CLICKED: CLICK(3); CLICK(3); CLICK(3); break;
289            case BUTTON3_RESERVED_EVENT: break;
[548]290
[980]291            case BUTTON4_PRESSED: PRESS(4); break;
292            case BUTTON4_RELEASED: RELEASE(4); break;
293            case BUTTON4_CLICKED: CLICK(4); break;
294            case BUTTON4_DOUBLE_CLICKED: CLICK(4); CLICK(4); break;
295            case BUTTON4_TRIPLE_CLICKED: CLICK(4); CLICK(4); CLICK(4); break;
296            case BUTTON4_RESERVED_EVENT: break;
[548]297
298            default:
299                break;
[980]300#undef PRESS
301#undef RELEASE
302#undef CLICK
[548]303        }
304
[811]305        if(dp->mouse.x == (unsigned int)mevent.x &&
306           dp->mouse.y == (unsigned int)mevent.y)
307            return _pop_event(dp, ev);
[548]308
[811]309        dp->mouse.x = mevent.x;
310        dp->mouse.y = mevent.y;
[548]311
[681]312        ev->type = CACA_EVENT_MOUSE_MOTION;
[811]313        ev->data.mouse.x = dp->mouse.x;
314        ev->data.mouse.y = dp->mouse.y;
[681]315        return 1;
[548]316    }
317
318    switch(intkey)
319    {
[810]320        case KEY_UP: ev->data.key.ch = CACA_KEY_UP; break;
321        case KEY_DOWN: ev->data.key.ch = CACA_KEY_DOWN; break;
322        case KEY_LEFT: ev->data.key.ch = CACA_KEY_LEFT; break;
323        case KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break;
[548]324
[810]325        case KEY_IC: ev->data.key.ch = CACA_KEY_INSERT; break;
326        case KEY_DC: ev->data.key.ch = CACA_KEY_DELETE; break;
[974]327        case 0x7f:
[971]328        case KEY_BACKSPACE: ev->data.key.ch = CACA_KEY_BACKSPACE; break;
[810]329        case KEY_HOME: ev->data.key.ch = CACA_KEY_HOME; break;
330        case KEY_END: ev->data.key.ch = CACA_KEY_END; break;
331        case KEY_PPAGE: ev->data.key.ch = CACA_KEY_PAGEUP; break;
332        case KEY_NPAGE: ev->data.key.ch = CACA_KEY_PAGEDOWN; break;
[548]333
[810]334        case KEY_F(1): ev->data.key.ch = CACA_KEY_F1; break;
335        case KEY_F(2): ev->data.key.ch = CACA_KEY_F2; break;
336        case KEY_F(3): ev->data.key.ch = CACA_KEY_F3; break;
337        case KEY_F(4): ev->data.key.ch = CACA_KEY_F4; break;
338        case KEY_F(5): ev->data.key.ch = CACA_KEY_F5; break;
339        case KEY_F(6): ev->data.key.ch = CACA_KEY_F6; break;
340        case KEY_F(7): ev->data.key.ch = CACA_KEY_F7; break;
341        case KEY_F(8): ev->data.key.ch = CACA_KEY_F8; break;
342        case KEY_F(9): ev->data.key.ch = CACA_KEY_F9; break;
343        case KEY_F(10): ev->data.key.ch = CACA_KEY_F10; break;
344        case KEY_F(11): ev->data.key.ch = CACA_KEY_F11; break;
345        case KEY_F(12): ev->data.key.ch = CACA_KEY_F12; break;
[681]346
[980]347        default:
348            /* Unknown key */
349            ev->type = CACA_EVENT_NONE; return 0;
[548]350    }
351
[681]352    ev->type = CACA_EVENT_KEY_PRESS;
[969]353    ev->data.key.utf32 = 0;
[681]354    ev->data.key.utf8[0] = '\0';
355    return 1;
[548]356}
357
[539]358/*
359 * XXX: following functions are local
360 */
[281]361
[984]362#if defined HAVE_SIGNAL
[348]363static RETSIGTYPE sigwinch_handler(int sig)
364{
[811]365    sigwinch_d->resize.resized = 1;
[348]366
[613]367    signal(SIGWINCH, sigwinch_handler);
[348]368}
369#endif
370
[984]371#if defined HAVE_GETENV && defined HAVE_PUTENV
[550]372static void ncurses_check_terminal(void)
373{
374    char *term, *colorterm, *other;
375
376    term = getenv("TERM");
377    colorterm = getenv("COLORTERM");
378
379    if(term && !strcmp(term, "xterm"))
380    {
381        /* If we are using gnome-terminal, it's really a 16 colour terminal */
382        if(colorterm && !strcmp(colorterm, "gnome-terminal"))
383        {
384            SCREEN *screen;
385            screen = newterm("xterm-16color", stdout, stdin);
386            if(screen == NULL)
387                return;
388            endwin();
389            (void)putenv("TERM=xterm-16color");
390            return;
391        }
392
393        /* Ditto if we are using Konsole */
394        other = getenv("KONSOLE_DCOP_SESSION");
395        if(other)
396        {
397            SCREEN *screen;
398            screen = newterm("xterm-16color", stdout, stdin);
399            if(screen == NULL)
400                return;
401            endwin();
402            (void)putenv("TERM=xterm-16color");
403            return;
404        }
405    }
406}
407#endif
408
[811]409static void ncurses_write_utf32(uint32_t ch)
[629]410{
[984]411#if defined HAVE_NCURSESW_NCURSES_H
[929]412    char buf[10];
[629]413    int bytes;
414
[962]415    bytes = cucul_utf32_to_utf8(buf, ch);
[629]416    buf[bytes] = '\0';
417    addstr(buf);
418#else
[929]419    addch(ch < 0x80 ? ch : '?');
[629]420#endif
421}
422
[539]423/*
424 * Driver initialisation
425 */
[527]426
[811]427int ncurses_install(caca_display_t *dp)
[511]428{
[811]429    dp->drv.driver = CACA_DRIVER_NCURSES;
[527]430
[811]431    dp->drv.init_graphics = ncurses_init_graphics;
432    dp->drv.end_graphics = ncurses_end_graphics;
[819]433    dp->drv.set_display_title = ncurses_set_display_title;
434    dp->drv.get_display_width = ncurses_get_display_width;
435    dp->drv.get_display_height = ncurses_get_display_height;
[811]436    dp->drv.display = ncurses_display;
437    dp->drv.handle_resize = ncurses_handle_resize;
438    dp->drv.get_event = ncurses_get_event;
439    dp->drv.set_mouse = NULL;
[689]440
[684]441    return 0;
[511]442}
443
[539]444#endif /* USE_NCURSES */
[527]445
Note: See TracBrowser for help on using the repository browser.