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

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