| 1 | /* |
|---|
| 2 | * libcaca Colour ASCII-Art library |
|---|
| 3 | * Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net> |
|---|
| 4 | * 2007 Ben Wiley Sittler <bsittler@gmail.com> |
|---|
| 5 | * All Rights Reserved |
|---|
| 6 | * |
|---|
| 7 | * $Id$ |
|---|
| 8 | * |
|---|
| 9 | * This library is free software. It comes without any warranty, to |
|---|
| 10 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 11 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 12 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 13 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | /* |
|---|
| 17 | * This file contains the libcaca Ncurses input and output driver |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include "config.h" |
|---|
| 21 | |
|---|
| 22 | #if defined USE_NCURSES |
|---|
| 23 | |
|---|
| 24 | #if defined HAVE_NCURSESW_NCURSES_H |
|---|
| 25 | # include <ncursesw/ncurses.h> |
|---|
| 26 | #elif defined HAVE_NCURSES_NCURSES_H |
|---|
| 27 | # include <ncurses/ncurses.h> |
|---|
| 28 | #elif defined HAVE_NCURSES_H |
|---|
| 29 | # include <ncurses.h> |
|---|
| 30 | #else |
|---|
| 31 | # include <curses.h> |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | #include <stdlib.h> |
|---|
| 35 | #include <string.h> |
|---|
| 36 | |
|---|
| 37 | #if defined HAVE_UNISTD_H |
|---|
| 38 | # include <unistd.h> |
|---|
| 39 | #endif |
|---|
| 40 | #if defined HAVE_SIGNAL_H |
|---|
| 41 | # include <signal.h> |
|---|
| 42 | #endif |
|---|
| 43 | #if defined HAVE_SYS_IOCTL_H |
|---|
| 44 | # include <sys/ioctl.h> |
|---|
| 45 | #endif |
|---|
| 46 | #if defined HAVE_LOCALE_H |
|---|
| 47 | # include <locale.h> |
|---|
| 48 | #endif |
|---|
| 49 | #if defined HAVE_TERMIOS_H |
|---|
| 50 | # include <termios.h> |
|---|
| 51 | #endif |
|---|
| 52 | |
|---|
| 53 | #include "caca.h" |
|---|
| 54 | #include "caca_internals.h" |
|---|
| 55 | |
|---|
| 56 | /* |
|---|
| 57 | * Emulation for missing ACS_* in older curses |
|---|
| 58 | */ |
|---|
| 59 | |
|---|
| 60 | #ifndef ACS_BLOCK |
|---|
| 61 | #define ACS_BLOCK '#' |
|---|
| 62 | #endif |
|---|
| 63 | |
|---|
| 64 | #ifndef ACS_BOARD |
|---|
| 65 | #define ACS_BOARD '#' |
|---|
| 66 | #endif |
|---|
| 67 | |
|---|
| 68 | #ifndef ACS_BTEE |
|---|
| 69 | #define ACS_BTEE '+' |
|---|
| 70 | #endif |
|---|
| 71 | |
|---|
| 72 | #ifndef ACS_BULLET |
|---|
| 73 | #define ACS_BULLET '.' |
|---|
| 74 | #endif |
|---|
| 75 | |
|---|
| 76 | #ifndef ACS_CKBOARD |
|---|
| 77 | #define ACS_CKBOARD ':' |
|---|
| 78 | #endif |
|---|
| 79 | |
|---|
| 80 | #ifndef ACS_DARROW |
|---|
| 81 | #define ACS_DARROW 'v' |
|---|
| 82 | #endif |
|---|
| 83 | |
|---|
| 84 | #ifndef ACS_DEGREE |
|---|
| 85 | #define ACS_DEGREE '\'' |
|---|
| 86 | #endif |
|---|
| 87 | |
|---|
| 88 | #ifndef ACS_DIAMOND |
|---|
| 89 | #define ACS_DIAMOND '+' |
|---|
| 90 | #endif |
|---|
| 91 | |
|---|
| 92 | #ifndef ACS_GEQUAL |
|---|
| 93 | #define ACS_GEQUAL '>' |
|---|
| 94 | #endif |
|---|
| 95 | |
|---|
| 96 | #ifndef ACS_HLINE |
|---|
| 97 | #define ACS_HLINE '-' |
|---|
| 98 | #endif |
|---|
| 99 | |
|---|
| 100 | #ifndef ACS_LANTERN |
|---|
| 101 | #define ACS_LANTERN '#' |
|---|
| 102 | #endif |
|---|
| 103 | |
|---|
| 104 | #ifndef ACS_LARROW |
|---|
| 105 | #define ACS_LARROW '<' |
|---|
| 106 | #endif |
|---|
| 107 | |
|---|
| 108 | #ifndef ACS_LEQUAL |
|---|
| 109 | #define ACS_LEQUAL '<' |
|---|
| 110 | #endif |
|---|
| 111 | |
|---|
| 112 | #ifndef ACS_LLCORNER |
|---|
| 113 | #define ACS_LLCORNER '+' |
|---|
| 114 | #endif |
|---|
| 115 | |
|---|
| 116 | #ifndef ACS_LRCORNER |
|---|
| 117 | #define ACS_LRCORNER '+' |
|---|
| 118 | #endif |
|---|
| 119 | |
|---|
| 120 | #ifndef ACS_LTEE |
|---|
| 121 | #define ACS_LTEE '+' |
|---|
| 122 | #endif |
|---|
| 123 | |
|---|
| 124 | #ifndef ACS_NEQUAL |
|---|
| 125 | #define ACS_NEQUAL '!' |
|---|
| 126 | #endif |
|---|
| 127 | |
|---|
| 128 | #ifndef ACS_PI |
|---|
| 129 | #define ACS_PI '*' |
|---|
| 130 | #endif |
|---|
| 131 | |
|---|
| 132 | #ifndef ACS_STERLING |
|---|
| 133 | #define ACS_STERLING 'f' |
|---|
| 134 | #endif |
|---|
| 135 | |
|---|
| 136 | #ifndef ACS_PLMINUS |
|---|
| 137 | #define ACS_PLMINUS '#' |
|---|
| 138 | #endif |
|---|
| 139 | |
|---|
| 140 | #ifndef ACS_PLUS |
|---|
| 141 | #define ACS_PLUS '+' |
|---|
| 142 | #endif |
|---|
| 143 | |
|---|
| 144 | #ifndef ACS_RARROW |
|---|
| 145 | #define ACS_RARROW '>' |
|---|
| 146 | #endif |
|---|
| 147 | |
|---|
| 148 | #ifndef ACS_RTEE |
|---|
| 149 | #define ACS_RTEE '+' |
|---|
| 150 | #endif |
|---|
| 151 | |
|---|
| 152 | #ifndef ACS_S1 |
|---|
| 153 | #define ACS_S1 '-' |
|---|
| 154 | #endif |
|---|
| 155 | |
|---|
| 156 | #ifndef ACS_S3 |
|---|
| 157 | #define ACS_S3 '-' |
|---|
| 158 | #endif |
|---|
| 159 | |
|---|
| 160 | #ifndef ACS_S7 |
|---|
| 161 | #define ACS_S7 '-' |
|---|
| 162 | #endif |
|---|
| 163 | |
|---|
| 164 | #ifndef ACS_S9 |
|---|
| 165 | #define ACS_S9 '-' |
|---|
| 166 | #endif |
|---|
| 167 | |
|---|
| 168 | #ifndef ACS_TTEE |
|---|
| 169 | #define ACS_TTEE '+' |
|---|
| 170 | #endif |
|---|
| 171 | |
|---|
| 172 | #ifndef ACS_UARROW |
|---|
| 173 | #define ACS_UARROW '^' |
|---|
| 174 | #endif |
|---|
| 175 | |
|---|
| 176 | #ifndef ACS_ULCORNER |
|---|
| 177 | #define ACS_ULCORNER '+' |
|---|
| 178 | #endif |
|---|
| 179 | |
|---|
| 180 | #ifndef ACS_URCORNER |
|---|
| 181 | #define ACS_URCORNER '+' |
|---|
| 182 | #endif |
|---|
| 183 | |
|---|
| 184 | #ifndef ACS_VLINE |
|---|
| 185 | #define ACS_VLINE '|' |
|---|
| 186 | #endif |
|---|
| 187 | |
|---|
| 188 | /* |
|---|
| 189 | * Local functions |
|---|
| 190 | */ |
|---|
| 191 | |
|---|
| 192 | #if defined HAVE_SIGNAL |
|---|
| 193 | static RETSIGTYPE sigwinch_handler(int); |
|---|
| 194 | static caca_display_t *sigwinch_d; /* FIXME: we ought to get rid of this */ |
|---|
| 195 | #endif |
|---|
| 196 | #if defined HAVE_GETENV && defined HAVE_PUTENV |
|---|
| 197 | static void ncurses_install_terminal(caca_display_t *); |
|---|
| 198 | static void ncurses_uninstall_terminal(caca_display_t *); |
|---|
| 199 | #endif |
|---|
| 200 | static void ncurses_write_utf32(uint32_t); |
|---|
| 201 | |
|---|
| 202 | struct driver_private |
|---|
| 203 | { |
|---|
| 204 | int attr[16*16]; |
|---|
| 205 | mmask_t oldmask; |
|---|
| 206 | char *term; |
|---|
| 207 | }; |
|---|
| 208 | |
|---|
| 209 | static int ncurses_init_graphics(caca_display_t *dp) |
|---|
| 210 | { |
|---|
| 211 | static int curses_colors[] = |
|---|
| 212 | { |
|---|
| 213 | /* Standard curses colours */ |
|---|
| 214 | COLOR_BLACK, |
|---|
| 215 | COLOR_BLUE, |
|---|
| 216 | COLOR_GREEN, |
|---|
| 217 | COLOR_CYAN, |
|---|
| 218 | COLOR_RED, |
|---|
| 219 | COLOR_MAGENTA, |
|---|
| 220 | COLOR_YELLOW, |
|---|
| 221 | COLOR_WHITE, |
|---|
| 222 | /* Extra values for xterm-16color */ |
|---|
| 223 | COLOR_BLACK + 8, |
|---|
| 224 | COLOR_BLUE + 8, |
|---|
| 225 | COLOR_GREEN + 8, |
|---|
| 226 | COLOR_CYAN + 8, |
|---|
| 227 | COLOR_RED + 8, |
|---|
| 228 | COLOR_MAGENTA + 8, |
|---|
| 229 | COLOR_YELLOW + 8, |
|---|
| 230 | COLOR_WHITE + 8 |
|---|
| 231 | }; |
|---|
| 232 | |
|---|
| 233 | mmask_t newmask; |
|---|
| 234 | int fg, bg, max; |
|---|
| 235 | |
|---|
| 236 | dp->drv.p = malloc(sizeof(struct driver_private)); |
|---|
| 237 | |
|---|
| 238 | #if defined HAVE_GETENV && defined HAVE_PUTENV |
|---|
| 239 | ncurses_install_terminal(dp); |
|---|
| 240 | #endif |
|---|
| 241 | |
|---|
| 242 | #if defined HAVE_SIGNAL |
|---|
| 243 | sigwinch_d = dp; |
|---|
| 244 | signal(SIGWINCH, sigwinch_handler); |
|---|
| 245 | #endif |
|---|
| 246 | |
|---|
| 247 | #if defined HAVE_LOCALE_H |
|---|
| 248 | setlocale(LC_ALL, ""); |
|---|
| 249 | #endif |
|---|
| 250 | |
|---|
| 251 | _caca_set_term_title("caca for ncurses"); |
|---|
| 252 | |
|---|
| 253 | initscr(); |
|---|
| 254 | keypad(stdscr, TRUE); |
|---|
| 255 | nonl(); |
|---|
| 256 | raw(); |
|---|
| 257 | noecho(); |
|---|
| 258 | nodelay(stdscr, TRUE); |
|---|
| 259 | curs_set(0); |
|---|
| 260 | |
|---|
| 261 | /* Activate mouse */ |
|---|
| 262 | newmask = REPORT_MOUSE_POSITION | ALL_MOUSE_EVENTS; |
|---|
| 263 | mousemask(newmask, &dp->drv.p->oldmask); |
|---|
| 264 | mouseinterval(-1); /* No click emulation */ |
|---|
| 265 | |
|---|
| 266 | /* Set the escape delay to a ridiculously low value */ |
|---|
| 267 | ESCDELAY = 10; |
|---|
| 268 | |
|---|
| 269 | /* Activate colour */ |
|---|
| 270 | start_color(); |
|---|
| 271 | |
|---|
| 272 | /* If COLORS == 16, it means the terminal supports full bright colours |
|---|
| 273 | * using setab and setaf (will use \e[90m \e[91m etc. for colours >= 8), |
|---|
| 274 | * we can build 16*16 colour pairs. |
|---|
| 275 | * If COLORS == 8, it means the terminal does not know about bright |
|---|
| 276 | * colours and we need to get them through A_BOLD and A_BLINK (\e[1m |
|---|
| 277 | * and \e[5m). We can only build 8*8 colour pairs. */ |
|---|
| 278 | max = COLORS >= 16 ? 16 : 8; |
|---|
| 279 | |
|---|
| 280 | for(bg = 0; bg < max; bg++) |
|---|
| 281 | for(fg = 0; fg < max; fg++) |
|---|
| 282 | { |
|---|
| 283 | /* Use ((max + 7 - fg) % max) instead of fg so that colour 0 |
|---|
| 284 | * is light gray on black. Some terminals don't like this |
|---|
| 285 | * colour pair to be redefined. */ |
|---|
| 286 | int col = ((max + 7 - fg) % max) + max * bg; |
|---|
| 287 | init_pair(col, curses_colors[fg], curses_colors[bg]); |
|---|
| 288 | dp->drv.p->attr[fg + 16 * bg] = COLOR_PAIR(col); |
|---|
| 289 | |
|---|
| 290 | if(max == 8) |
|---|
| 291 | { |
|---|
| 292 | /* Bright fg on simple bg */ |
|---|
| 293 | dp->drv.p->attr[fg + 8 + 16 * bg] = A_BOLD | COLOR_PAIR(col); |
|---|
| 294 | /* Simple fg on bright bg */ |
|---|
| 295 | dp->drv.p->attr[fg + 16 * (bg + 8)] = A_BLINK |
|---|
| 296 | | COLOR_PAIR(col); |
|---|
| 297 | /* Bright fg on bright bg */ |
|---|
| 298 | dp->drv.p->attr[fg + 8 + 16 * (bg + 8)] = A_BLINK | A_BOLD |
|---|
| 299 | | COLOR_PAIR(col); |
|---|
| 300 | } |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | caca_add_dirty_rect(dp->cv, 0, 0, dp->cv->width, dp->cv->height); |
|---|
| 304 | dp->resize.allow = 1; |
|---|
| 305 | caca_set_canvas_size(dp->cv, COLS, LINES); |
|---|
| 306 | dp->resize.allow = 0; |
|---|
| 307 | |
|---|
| 308 | return 0; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | static int ncurses_end_graphics(caca_display_t *dp) |
|---|
| 312 | { |
|---|
| 313 | _caca_set_term_title(""); |
|---|
| 314 | mousemask(dp->drv.p->oldmask, NULL); |
|---|
| 315 | curs_set(1); |
|---|
| 316 | noraw(); |
|---|
| 317 | endwin(); |
|---|
| 318 | |
|---|
| 319 | #if defined HAVE_GETENV && defined HAVE_PUTENV |
|---|
| 320 | ncurses_uninstall_terminal(dp); |
|---|
| 321 | #endif |
|---|
| 322 | |
|---|
| 323 | free(dp->drv.p); |
|---|
| 324 | |
|---|
| 325 | return 0; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | static int ncurses_set_display_title(caca_display_t *dp, char const *title) |
|---|
| 329 | { |
|---|
| 330 | _caca_set_term_title(title); |
|---|
| 331 | |
|---|
| 332 | return 0; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | static int ncurses_get_display_width(caca_display_t const *dp) |
|---|
| 336 | { |
|---|
| 337 | /* Fallback to a 6x10 font */ |
|---|
| 338 | return caca_get_canvas_width(dp->cv) * 6; |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | static int ncurses_get_display_height(caca_display_t const *dp) |
|---|
| 342 | { |
|---|
| 343 | /* Fallback to a 6x10 font */ |
|---|
| 344 | return caca_get_canvas_height(dp->cv) * 10; |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | static void ncurses_display(caca_display_t *dp) |
|---|
| 348 | { |
|---|
| 349 | int x, y, i; |
|---|
| 350 | |
|---|
| 351 | for(i = 0; i < caca_get_dirty_rect_count(dp->cv); i++) |
|---|
| 352 | { |
|---|
| 353 | uint32_t const *cvchars, *cvattrs; |
|---|
| 354 | int dx, dy, dw, dh; |
|---|
| 355 | |
|---|
| 356 | caca_get_dirty_rect(dp->cv, i, &dx, &dy, &dw, &dh); |
|---|
| 357 | |
|---|
| 358 | cvchars = (uint32_t const *)caca_get_canvas_chars(dp->cv) |
|---|
| 359 | + dx + dy * dp->cv->width; |
|---|
| 360 | cvattrs = (uint32_t const *)caca_get_canvas_attrs(dp->cv) |
|---|
| 361 | + dx + dy * dp->cv->width; |
|---|
| 362 | |
|---|
| 363 | for(y = dy; y < dy + dh; y++) |
|---|
| 364 | { |
|---|
| 365 | move(y, dx); |
|---|
| 366 | for(x = dx; x < dx + dw; x++) |
|---|
| 367 | { |
|---|
| 368 | (void)attrset(dp->drv.p->attr[caca_attr_to_ansi(*cvattrs++)]); |
|---|
| 369 | ncurses_write_utf32(*cvchars++); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | cvchars += dp->cv->width - dw; |
|---|
| 373 | cvattrs += dp->cv->width - dw; |
|---|
| 374 | } |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | x = caca_wherex(dp->cv); |
|---|
| 378 | y = caca_wherey(dp->cv); |
|---|
| 379 | move(y, x); |
|---|
| 380 | |
|---|
| 381 | refresh(); |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | static void ncurses_handle_resize(caca_display_t *dp) |
|---|
| 385 | { |
|---|
| 386 | struct winsize size; |
|---|
| 387 | |
|---|
| 388 | #if defined HAVE_SYS_IOCTL_H |
|---|
| 389 | if(ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) |
|---|
| 390 | { |
|---|
| 391 | dp->resize.w = size.ws_col; |
|---|
| 392 | dp->resize.h = size.ws_row; |
|---|
| 393 | #if defined HAVE_RESIZE_TERM |
|---|
| 394 | resize_term(dp->resize.h, dp->resize.w); |
|---|
| 395 | #else |
|---|
| 396 | resizeterm(dp->resize.h, dp->resize.w); |
|---|
| 397 | #endif |
|---|
| 398 | wrefresh(curscr); |
|---|
| 399 | return; |
|---|
| 400 | } |
|---|
| 401 | #endif |
|---|
| 402 | |
|---|
| 403 | /* Fallback */ |
|---|
| 404 | dp->resize.w = caca_get_canvas_width(dp->cv); |
|---|
| 405 | dp->resize.h = caca_get_canvas_height(dp->cv); |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | static int ncurses_get_event(caca_display_t *dp, caca_privevent_t *ev) |
|---|
| 409 | { |
|---|
| 410 | int intkey; |
|---|
| 411 | |
|---|
| 412 | intkey = getch(); |
|---|
| 413 | if(intkey == ERR) |
|---|
| 414 | { |
|---|
| 415 | ev->type = CACA_EVENT_NONE; |
|---|
| 416 | return 0; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | if(intkey < 0x7f) |
|---|
| 420 | { |
|---|
| 421 | ev->type = CACA_EVENT_KEY_PRESS; |
|---|
| 422 | ev->data.key.ch = intkey; |
|---|
| 423 | ev->data.key.utf32 = intkey; |
|---|
| 424 | ev->data.key.utf8[0] = intkey; |
|---|
| 425 | ev->data.key.utf8[1] = '\0'; |
|---|
| 426 | return 1; |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | /* If the key was UTF-8, parse the whole sequence */ |
|---|
| 430 | if(intkey >= 0x80 && intkey < 0x100) |
|---|
| 431 | { |
|---|
| 432 | int keys[7]; /* Necessary for ungetch(); */ |
|---|
| 433 | char utf8[7]; |
|---|
| 434 | uint32_t utf32; |
|---|
| 435 | size_t i, bytes = 0; |
|---|
| 436 | |
|---|
| 437 | keys[0] = intkey; |
|---|
| 438 | utf8[0] = intkey; |
|---|
| 439 | |
|---|
| 440 | for(i = 1; i < 6; i++) |
|---|
| 441 | { |
|---|
| 442 | keys[i] = getch(); |
|---|
| 443 | utf8[i] = (unsigned char)keys[i]; |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | utf8[i] = '\0'; |
|---|
| 447 | utf32 = caca_utf8_to_utf32(utf8, &bytes); |
|---|
| 448 | |
|---|
| 449 | while(i > bytes) |
|---|
| 450 | ungetch(keys[--i]); |
|---|
| 451 | |
|---|
| 452 | if(bytes) |
|---|
| 453 | { |
|---|
| 454 | ev->type = CACA_EVENT_KEY_PRESS; |
|---|
| 455 | ev->data.key.ch = 0; |
|---|
| 456 | ev->data.key.utf32 = utf32; |
|---|
| 457 | strcpy(ev->data.key.utf8, utf8); |
|---|
| 458 | return 1; |
|---|
| 459 | } |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | if(intkey == KEY_MOUSE) |
|---|
| 463 | { |
|---|
| 464 | MEVENT mevent; |
|---|
| 465 | getmouse(&mevent); |
|---|
| 466 | |
|---|
| 467 | switch(mevent.bstate) |
|---|
| 468 | { |
|---|
| 469 | #define PRESS(x) ev->data.mouse.button = x; \ |
|---|
| 470 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev) |
|---|
| 471 | #define RELEASE(x) ev->data.mouse.button = x; \ |
|---|
| 472 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev) |
|---|
| 473 | #define CLICK(x) PRESS(x); RELEASE(x) |
|---|
| 474 | case BUTTON1_PRESSED: PRESS(1); break; |
|---|
| 475 | case BUTTON1_RELEASED: RELEASE(1); break; |
|---|
| 476 | case BUTTON1_CLICKED: CLICK(1); break; |
|---|
| 477 | case BUTTON1_DOUBLE_CLICKED: CLICK(1); CLICK(1); break; |
|---|
| 478 | case BUTTON1_TRIPLE_CLICKED: CLICK(1); CLICK(1); CLICK(1); break; |
|---|
| 479 | case BUTTON1_RESERVED_EVENT: break; |
|---|
| 480 | |
|---|
| 481 | case BUTTON2_PRESSED: PRESS(2); break; |
|---|
| 482 | case BUTTON2_RELEASED: RELEASE(2); break; |
|---|
| 483 | case BUTTON2_CLICKED: CLICK(2); break; |
|---|
| 484 | case BUTTON2_DOUBLE_CLICKED: CLICK(2); CLICK(2); break; |
|---|
| 485 | case BUTTON2_TRIPLE_CLICKED: CLICK(2); CLICK(2); CLICK(2); break; |
|---|
| 486 | case BUTTON2_RESERVED_EVENT: break; |
|---|
| 487 | |
|---|
| 488 | case BUTTON3_PRESSED: PRESS(3); break; |
|---|
| 489 | case BUTTON3_RELEASED: RELEASE(3); break; |
|---|
| 490 | case BUTTON3_CLICKED: CLICK(3); break; |
|---|
| 491 | case BUTTON3_DOUBLE_CLICKED: CLICK(3); CLICK(3); break; |
|---|
| 492 | case BUTTON3_TRIPLE_CLICKED: CLICK(3); CLICK(3); CLICK(3); break; |
|---|
| 493 | case BUTTON3_RESERVED_EVENT: break; |
|---|
| 494 | |
|---|
| 495 | case BUTTON4_PRESSED: PRESS(4); break; |
|---|
| 496 | case BUTTON4_RELEASED: RELEASE(4); break; |
|---|
| 497 | case BUTTON4_CLICKED: CLICK(4); break; |
|---|
| 498 | case BUTTON4_DOUBLE_CLICKED: CLICK(4); CLICK(4); break; |
|---|
| 499 | case BUTTON4_TRIPLE_CLICKED: CLICK(4); CLICK(4); CLICK(4); break; |
|---|
| 500 | case BUTTON4_RESERVED_EVENT: break; |
|---|
| 501 | |
|---|
| 502 | default: |
|---|
| 503 | break; |
|---|
| 504 | #undef PRESS |
|---|
| 505 | #undef RELEASE |
|---|
| 506 | #undef CLICK |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | if(dp->mouse.x == mevent.x && dp->mouse.y == mevent.y) |
|---|
| 510 | return _pop_event(dp, ev); |
|---|
| 511 | |
|---|
| 512 | dp->mouse.x = mevent.x; |
|---|
| 513 | dp->mouse.y = mevent.y; |
|---|
| 514 | |
|---|
| 515 | ev->type = CACA_EVENT_MOUSE_MOTION; |
|---|
| 516 | ev->data.mouse.x = dp->mouse.x; |
|---|
| 517 | ev->data.mouse.y = dp->mouse.y; |
|---|
| 518 | return 1; |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | switch(intkey) |
|---|
| 522 | { |
|---|
| 523 | case KEY_UP: ev->data.key.ch = CACA_KEY_UP; break; |
|---|
| 524 | case KEY_DOWN: ev->data.key.ch = CACA_KEY_DOWN; break; |
|---|
| 525 | case KEY_LEFT: ev->data.key.ch = CACA_KEY_LEFT; break; |
|---|
| 526 | case KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break; |
|---|
| 527 | |
|---|
| 528 | case KEY_IC: ev->data.key.ch = CACA_KEY_INSERT; break; |
|---|
| 529 | case KEY_DC: ev->data.key.ch = CACA_KEY_DELETE; break; |
|---|
| 530 | case 0x7f: |
|---|
| 531 | case KEY_BACKSPACE: ev->data.key.ch = CACA_KEY_BACKSPACE; break; |
|---|
| 532 | case KEY_HOME: ev->data.key.ch = CACA_KEY_HOME; break; |
|---|
| 533 | case KEY_END: ev->data.key.ch = CACA_KEY_END; break; |
|---|
| 534 | case KEY_PPAGE: ev->data.key.ch = CACA_KEY_PAGEUP; break; |
|---|
| 535 | case KEY_NPAGE: ev->data.key.ch = CACA_KEY_PAGEDOWN; break; |
|---|
| 536 | |
|---|
| 537 | case KEY_F(1): ev->data.key.ch = CACA_KEY_F1; break; |
|---|
| 538 | case KEY_F(2): ev->data.key.ch = CACA_KEY_F2; break; |
|---|
| 539 | case KEY_F(3): ev->data.key.ch = CACA_KEY_F3; break; |
|---|
| 540 | case KEY_F(4): ev->data.key.ch = CACA_KEY_F4; break; |
|---|
| 541 | case KEY_F(5): ev->data.key.ch = CACA_KEY_F5; break; |
|---|
| 542 | case KEY_F(6): ev->data.key.ch = CACA_KEY_F6; break; |
|---|
| 543 | case KEY_F(7): ev->data.key.ch = CACA_KEY_F7; break; |
|---|
| 544 | case KEY_F(8): ev->data.key.ch = CACA_KEY_F8; break; |
|---|
| 545 | case KEY_F(9): ev->data.key.ch = CACA_KEY_F9; break; |
|---|
| 546 | case KEY_F(10): ev->data.key.ch = CACA_KEY_F10; break; |
|---|
| 547 | case KEY_F(11): ev->data.key.ch = CACA_KEY_F11; break; |
|---|
| 548 | case KEY_F(12): ev->data.key.ch = CACA_KEY_F12; break; |
|---|
| 549 | |
|---|
| 550 | default: |
|---|
| 551 | /* Unknown key */ |
|---|
| 552 | ev->type = CACA_EVENT_NONE; return 0; |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | ev->type = CACA_EVENT_KEY_PRESS; |
|---|
| 556 | ev->data.key.utf32 = 0; |
|---|
| 557 | ev->data.key.utf8[0] = '\0'; |
|---|
| 558 | return 1; |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | static void ncurses_set_cursor(caca_display_t *dp, int flags) |
|---|
| 562 | { |
|---|
| 563 | curs_set(flags ? 2 : 0); |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | /* |
|---|
| 567 | * XXX: following functions are local |
|---|
| 568 | */ |
|---|
| 569 | |
|---|
| 570 | #if defined HAVE_SIGNAL |
|---|
| 571 | static RETSIGTYPE sigwinch_handler(int sig) |
|---|
| 572 | { |
|---|
| 573 | sigwinch_d->resize.resized = 1; |
|---|
| 574 | |
|---|
| 575 | signal(SIGWINCH, sigwinch_handler); |
|---|
| 576 | } |
|---|
| 577 | #endif |
|---|
| 578 | |
|---|
| 579 | #if defined HAVE_GETENV && defined HAVE_PUTENV |
|---|
| 580 | static void ncurses_install_terminal(caca_display_t *dp) |
|---|
| 581 | { |
|---|
| 582 | char *term, *colorterm; |
|---|
| 583 | |
|---|
| 584 | dp->drv.p->term = NULL; |
|---|
| 585 | |
|---|
| 586 | term = getenv("TERM"); |
|---|
| 587 | colorterm = getenv("COLORTERM"); |
|---|
| 588 | |
|---|
| 589 | if(!term || strcmp(term, "xterm")) |
|---|
| 590 | return; |
|---|
| 591 | |
|---|
| 592 | /* If we are using gnome-terminal, it's really a 16 colour terminal. |
|---|
| 593 | * Ditto if we are using xfce4-terminal, or Konsole. */ |
|---|
| 594 | if((colorterm && (!strcmp(colorterm, "gnome-terminal") |
|---|
| 595 | || !strcmp(colorterm, "Terminal"))) |
|---|
| 596 | || getenv("KONSOLE_DCOP_SESSION")) |
|---|
| 597 | { |
|---|
| 598 | SCREEN *screen; |
|---|
| 599 | screen = newterm("xterm-16color", stdout, stdin); |
|---|
| 600 | if(screen == NULL) |
|---|
| 601 | return; |
|---|
| 602 | endwin(); |
|---|
| 603 | (void)putenv("TERM=xterm-16color"); |
|---|
| 604 | dp->drv.p->term = strdup(term); |
|---|
| 605 | return; |
|---|
| 606 | } |
|---|
| 607 | } |
|---|
| 608 | |
|---|
| 609 | static void ncurses_uninstall_terminal(caca_display_t *dp) |
|---|
| 610 | { |
|---|
| 611 | /* Needs to be persistent because we use putenv() */ |
|---|
| 612 | static char termenv[1024]; |
|---|
| 613 | |
|---|
| 614 | if(!dp->drv.p->term) |
|---|
| 615 | return; |
|---|
| 616 | |
|---|
| 617 | snprintf(termenv, 1023, "TERM=%s", dp->drv.p->term); |
|---|
| 618 | free(dp->drv.p->term); |
|---|
| 619 | (void)putenv(termenv); |
|---|
| 620 | } |
|---|
| 621 | #endif |
|---|
| 622 | |
|---|
| 623 | static void ncurses_write_utf32(uint32_t ch) |
|---|
| 624 | { |
|---|
| 625 | #if defined HAVE_NCURSESW_NCURSES_H |
|---|
| 626 | char buf[10]; |
|---|
| 627 | int bytes; |
|---|
| 628 | #endif |
|---|
| 629 | |
|---|
| 630 | if(ch == CACA_MAGIC_FULLWIDTH) |
|---|
| 631 | return; |
|---|
| 632 | |
|---|
| 633 | #if defined HAVE_NCURSESW_NCURSES_H |
|---|
| 634 | bytes = caca_utf32_to_utf8(buf, ch); |
|---|
| 635 | buf[bytes] = '\0'; |
|---|
| 636 | addstr(buf); |
|---|
| 637 | #else |
|---|
| 638 | if(ch < 0x80) |
|---|
| 639 | { |
|---|
| 640 | addch(ch); |
|---|
| 641 | } |
|---|
| 642 | else |
|---|
| 643 | { |
|---|
| 644 | chtype cch; |
|---|
| 645 | chtype cch2; |
|---|
| 646 | |
|---|
| 647 | cch = '?'; |
|---|
| 648 | cch2 = ' '; |
|---|
| 649 | if ((ch > 0x0000ff00) && (ch < 0x0000ff5f)) |
|---|
| 650 | { |
|---|
| 651 | cch = ch - 0x0000ff00 + ' '; |
|---|
| 652 | } |
|---|
| 653 | switch (ch) |
|---|
| 654 | { |
|---|
| 655 | case 0x000000a0: /* <nbsp> */ |
|---|
| 656 | case 0x00003000: /* */ |
|---|
| 657 | cch = ' '; |
|---|
| 658 | break; |
|---|
| 659 | case 0x000000a3: /* £ */ |
|---|
| 660 | cch = ACS_STERLING; |
|---|
| 661 | break; |
|---|
| 662 | case 0x000000b0: /* ° */ |
|---|
| 663 | cch = ACS_DEGREE; |
|---|
| 664 | break; |
|---|
| 665 | case 0x000000b1: /* ± */ |
|---|
| 666 | cch = ACS_PLMINUS; |
|---|
| 667 | break; |
|---|
| 668 | case 0x000000b7: /* · */ |
|---|
| 669 | case 0x00002219: /* ∙ */ |
|---|
| 670 | case 0x000030fb: /* ・ */ |
|---|
| 671 | cch = ACS_BULLET; |
|---|
| 672 | break; |
|---|
| 673 | case 0x000003c0: /* π */ |
|---|
| 674 | cch = ACS_PI; |
|---|
| 675 | break; |
|---|
| 676 | case 0x00002018: /* ‘ */ |
|---|
| 677 | case 0x00002019: /* ’ */ |
|---|
| 678 | cch = '\''; |
|---|
| 679 | break; |
|---|
| 680 | case 0x0000201c: /* “ */ |
|---|
| 681 | case 0x0000201d: /* ” */ |
|---|
| 682 | cch = '"'; |
|---|
| 683 | break; |
|---|
| 684 | case 0x00002190: /* ← */ |
|---|
| 685 | cch = ACS_LARROW; |
|---|
| 686 | break; |
|---|
| 687 | case 0x00002191: /* ↑ */ |
|---|
| 688 | cch = ACS_UARROW; |
|---|
| 689 | break; |
|---|
| 690 | case 0x00002192: /* → */ |
|---|
| 691 | cch = ACS_RARROW; |
|---|
| 692 | break; |
|---|
| 693 | case 0x00002193: /* ↓ */ |
|---|
| 694 | cch = ACS_DARROW; |
|---|
| 695 | break; |
|---|
| 696 | case 0x00002260: /* ≠ */ |
|---|
| 697 | cch = ACS_NEQUAL; |
|---|
| 698 | break; |
|---|
| 699 | case 0x00002261: /* ≡ */ |
|---|
| 700 | cch = '='; |
|---|
| 701 | break; |
|---|
| 702 | case 0x00002264: /* ≤ */ |
|---|
| 703 | cch = ACS_LEQUAL; |
|---|
| 704 | break; |
|---|
| 705 | case 0x00002265: /* ≥ */ |
|---|
| 706 | cch = ACS_GEQUAL; |
|---|
| 707 | break; |
|---|
| 708 | case 0x000023ba: /* ⎺ */ |
|---|
| 709 | cch = ACS_S1; |
|---|
| 710 | cch2 = cch; |
|---|
| 711 | break; |
|---|
| 712 | case 0x000023bb: /* ⎻ */ |
|---|
| 713 | cch = ACS_S3; |
|---|
| 714 | cch2 = cch; |
|---|
| 715 | break; |
|---|
| 716 | case 0x000023bc: /* ⎼ */ |
|---|
| 717 | cch = ACS_S7; |
|---|
| 718 | cch2 = cch; |
|---|
| 719 | break; |
|---|
| 720 | case 0x000023bd: /* ⎽ */ |
|---|
| 721 | cch = ACS_S9; |
|---|
| 722 | cch2 = cch; |
|---|
| 723 | break; |
|---|
| 724 | case 0x00002500: /* ─ */ |
|---|
| 725 | case 0x00002550: /* ═ */ |
|---|
| 726 | cch = ACS_HLINE; |
|---|
| 727 | cch2 = cch; |
|---|
| 728 | break; |
|---|
| 729 | case 0x00002502: /* │ */ |
|---|
| 730 | case 0x00002551: /* ║ */ |
|---|
| 731 | cch = ACS_VLINE; |
|---|
| 732 | break; |
|---|
| 733 | case 0x0000250c: /* ┌ */ |
|---|
| 734 | case 0x00002552: /* ╒ */ |
|---|
| 735 | case 0x00002553: /* ╓ */ |
|---|
| 736 | case 0x00002554: /* ╔ */ |
|---|
| 737 | cch = ACS_ULCORNER; |
|---|
| 738 | cch2 = ACS_HLINE; |
|---|
| 739 | break; |
|---|
| 740 | case 0x00002510: /* ┐ */ |
|---|
| 741 | case 0x00002555: /* ╕ */ |
|---|
| 742 | case 0x00002556: /* ╖ */ |
|---|
| 743 | case 0x00002557: /* ╗ */ |
|---|
| 744 | cch = ACS_URCORNER; |
|---|
| 745 | break; |
|---|
| 746 | case 0x00002514: /* └ */ |
|---|
| 747 | case 0x00002558: /* ╘ */ |
|---|
| 748 | case 0x00002559: /* ╙ */ |
|---|
| 749 | case 0x0000255a: /* ╚ */ |
|---|
| 750 | cch = ACS_LLCORNER; |
|---|
| 751 | cch2 = ACS_HLINE; |
|---|
| 752 | break; |
|---|
| 753 | case 0x00002518: /* ┘ */ |
|---|
| 754 | case 0x0000255b: /* ╛ */ |
|---|
| 755 | case 0x0000255c: /* ╜ */ |
|---|
| 756 | case 0x0000255d: /* ╝ */ |
|---|
| 757 | cch = ACS_LRCORNER; |
|---|
| 758 | break; |
|---|
| 759 | case 0x0000251c: /* ├ */ |
|---|
| 760 | case 0x0000255e: /* ╞ */ |
|---|
| 761 | case 0x0000255f: /* ╟ */ |
|---|
| 762 | case 0x00002560: /* ╠ */ |
|---|
| 763 | cch = ACS_LTEE; |
|---|
| 764 | cch2 = ACS_HLINE; |
|---|
| 765 | break; |
|---|
| 766 | case 0x00002524: /* ┤ */ |
|---|
| 767 | case 0x00002561: /* ╡ */ |
|---|
| 768 | case 0x00002562: /* ╢ */ |
|---|
| 769 | case 0x00002563: /* ╣ */ |
|---|
| 770 | cch = ACS_RTEE; |
|---|
| 771 | break; |
|---|
| 772 | case 0x0000252c: /* ┬ */ |
|---|
| 773 | case 0x00002564: /* ╤ */ |
|---|
| 774 | case 0x00002565: /* ╥ */ |
|---|
| 775 | case 0x00002566: /* ╦ */ |
|---|
| 776 | cch = ACS_TTEE; |
|---|
| 777 | cch2 = ACS_HLINE; |
|---|
| 778 | break; |
|---|
| 779 | case 0x00002534: /* ┴ */ |
|---|
| 780 | case 0x00002567: /* ╧ */ |
|---|
| 781 | case 0x00002568: /* ╨ */ |
|---|
| 782 | case 0x00002569: /* ╩ */ |
|---|
| 783 | cch = ACS_BTEE; |
|---|
| 784 | cch2 = ACS_HLINE; |
|---|
| 785 | break; |
|---|
| 786 | case 0x0000253c: /* ┼ */ |
|---|
| 787 | case 0x0000256a: /* ╪ */ |
|---|
| 788 | case 0x0000256b: /* ╫ */ |
|---|
| 789 | case 0x0000256c: /* ╬ */ |
|---|
| 790 | cch = ACS_PLUS; |
|---|
| 791 | cch2 = ACS_HLINE; |
|---|
| 792 | break; |
|---|
| 793 | case 0x00002591: /* ░ */ |
|---|
| 794 | cch = ACS_BOARD; |
|---|
| 795 | cch2 = cch; |
|---|
| 796 | break; |
|---|
| 797 | case 0x00002592: /* ▒ */ |
|---|
| 798 | case 0x00002593: /* ▓ */ |
|---|
| 799 | cch = ACS_CKBOARD; |
|---|
| 800 | cch2 = cch; |
|---|
| 801 | break; |
|---|
| 802 | case 0x00002580: /* ▀ */ |
|---|
| 803 | case 0x00002584: /* ▄ */ |
|---|
| 804 | case 0x00002588: /* █ */ |
|---|
| 805 | case 0x0000258c: /* ▌ */ |
|---|
| 806 | case 0x00002590: /* ▐ */ |
|---|
| 807 | case 0x000025a0: /* ■ */ |
|---|
| 808 | case 0x000025ac: /* ▬ */ |
|---|
| 809 | case 0x000025ae: /* ▮ */ |
|---|
| 810 | cch = ACS_BLOCK; |
|---|
| 811 | cch2 = cch; |
|---|
| 812 | break; |
|---|
| 813 | case 0x000025c6: /* ◆ */ |
|---|
| 814 | case 0x00002666: /* ♦ */ |
|---|
| 815 | cch = ACS_DIAMOND; |
|---|
| 816 | break; |
|---|
| 817 | case 0x00002022: /* • */ |
|---|
| 818 | case 0x000025cb: /* ○ */ |
|---|
| 819 | case 0x000025cf: /* ● */ |
|---|
| 820 | case 0x00002603: /* ☃ */ |
|---|
| 821 | case 0x0000263c: /* ☼ */ |
|---|
| 822 | cch = ACS_LANTERN; |
|---|
| 823 | break; |
|---|
| 824 | case 0x0000301c: /* 〜 */ |
|---|
| 825 | cch = '~'; |
|---|
| 826 | break; |
|---|
| 827 | } |
|---|
| 828 | addch(cch); |
|---|
| 829 | if(caca_utf32_is_fullwidth(ch)) |
|---|
| 830 | { |
|---|
| 831 | addch(cch2); |
|---|
| 832 | } |
|---|
| 833 | } |
|---|
| 834 | #endif |
|---|
| 835 | } |
|---|
| 836 | |
|---|
| 837 | /* |
|---|
| 838 | * Driver initialisation |
|---|
| 839 | */ |
|---|
| 840 | |
|---|
| 841 | int ncurses_install(caca_display_t *dp) |
|---|
| 842 | { |
|---|
| 843 | dp->drv.id = CACA_DRIVER_NCURSES; |
|---|
| 844 | dp->drv.driver = "ncurses"; |
|---|
| 845 | |
|---|
| 846 | dp->drv.init_graphics = ncurses_init_graphics; |
|---|
| 847 | dp->drv.end_graphics = ncurses_end_graphics; |
|---|
| 848 | dp->drv.set_display_title = ncurses_set_display_title; |
|---|
| 849 | dp->drv.get_display_width = ncurses_get_display_width; |
|---|
| 850 | dp->drv.get_display_height = ncurses_get_display_height; |
|---|
| 851 | dp->drv.display = ncurses_display; |
|---|
| 852 | dp->drv.handle_resize = ncurses_handle_resize; |
|---|
| 853 | dp->drv.get_event = ncurses_get_event; |
|---|
| 854 | dp->drv.set_mouse = NULL; |
|---|
| 855 | dp->drv.set_cursor = ncurses_set_cursor; |
|---|
| 856 | |
|---|
| 857 | return 0; |
|---|
| 858 | } |
|---|
| 859 | |
|---|
| 860 | #endif /* USE_NCURSES */ |
|---|
| 861 | |
|---|