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

Last change on this file since 985 was 984, checked in by Sam Hocevar, 14 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
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 984 2006-05-25 22:32:03Z 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 0;
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(intkey == KEY_MOUSE)
259    {
260        MEVENT mevent;
261        getmouse(&mevent);
262
263        switch(mevent.bstate)
264        {
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;
276
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;
283
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;
290
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;
297
298            default:
299                break;
300#undef PRESS
301#undef RELEASE
302#undef CLICK
303        }
304
305        if(dp->mouse.x == (unsigned int)mevent.x &&
306           dp->mouse.y == (unsigned int)mevent.y)
307            return _pop_event(dp, ev);
308
309        dp->mouse.x = mevent.x;
310        dp->mouse.y = mevent.y;
311
312        ev->type = CACA_EVENT_MOUSE_MOTION;
313        ev->data.mouse.x = dp->mouse.x;
314        ev->data.mouse.y = dp->mouse.y;
315        return 1;
316    }
317
318    switch(intkey)
319    {
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;
324
325        case KEY_IC: ev->data.key.ch = CACA_KEY_INSERT; break;
326        case KEY_DC: ev->data.key.ch = CACA_KEY_DELETE; break;
327        case 0x7f:
328        case KEY_BACKSPACE: ev->data.key.ch = CACA_KEY_BACKSPACE; break;
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;
333
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;
346
347        default:
348            /* Unknown key */
349            ev->type = CACA_EVENT_NONE; return 0;
350    }
351
352    ev->type = CACA_EVENT_KEY_PRESS;
353    ev->data.key.utf32 = 0;
354    ev->data.key.utf8[0] = '\0';
355    return 1;
356}
357
358/*
359 * XXX: following functions are local
360 */
361
362#if defined HAVE_SIGNAL
363static RETSIGTYPE sigwinch_handler(int sig)
364{
365    sigwinch_d->resize.resized = 1;
366
367    signal(SIGWINCH, sigwinch_handler);
368}
369#endif
370
371#if defined HAVE_GETENV && defined HAVE_PUTENV
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
409static void ncurses_write_utf32(uint32_t ch)
410{
411#if defined HAVE_NCURSESW_NCURSES_H
412    char buf[10];
413    int bytes;
414
415    bytes = cucul_utf32_to_utf8(buf, ch);
416    buf[bytes] = '\0';
417    addstr(buf);
418#else
419    addch(ch < 0x80 ? ch : '?');
420#endif
421}
422
423/*
424 * Driver initialisation
425 */
426
427int ncurses_install(caca_display_t *dp)
428{
429    dp->drv.driver = CACA_DRIVER_NCURSES;
430
431    dp->drv.init_graphics = ncurses_init_graphics;
432    dp->drv.end_graphics = ncurses_end_graphics;
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;
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;
440
441    return 0;
442}
443
444#endif /* USE_NCURSES */
445
Note: See TracBrowser for help on using the repository browser.