[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 | * |
---|
[268] | 6 | * This library is free software; you can redistribute it and/or |
---|
[522] | 7 | * modify it under the terms of the Do What The Fuck You Want To |
---|
| 8 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
| 9 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
[35] | 10 | */ |
---|
[17] | 11 | |
---|
[540] | 12 | /** \file driver_ncurses.c |
---|
[268] | 13 | * \version \$Id: driver_ncurses.c 728 2006-04-07 09:46:30Z sam $ |
---|
| 14 | * \author Sam Hocevar <sam@zoy.org> |
---|
[540] | 15 | * \brief Ncurses driver |
---|
[205] | 16 | * |
---|
[540] | 17 | * This file contains the libcaca Ncurses input and output driver |
---|
[205] | 18 | */ |
---|
| 19 | |
---|
[63] | 20 | #include "config.h" |
---|
| 21 | |
---|
[539] | 22 | #if defined(USE_NCURSES) |
---|
| 23 | |
---|
[629] | 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) |
---|
[539] | 29 | # include <ncurses.h> |
---|
| 30 | #else |
---|
| 31 | # include <curses.h> |
---|
| 32 | #endif |
---|
| 33 | |
---|
[550] | 34 | #include <stdlib.h> |
---|
| 35 | #include <string.h> |
---|
| 36 | |
---|
[348] | 37 | #if defined(HAVE_SIGNAL_H) |
---|
| 38 | # include <signal.h> |
---|
| 39 | #endif |
---|
| 40 | #if defined(HAVE_SYS_IOCTL_H) |
---|
| 41 | # include <sys/ioctl.h> |
---|
| 42 | #endif |
---|
| 43 | |
---|
[185] | 44 | #include "caca.h" |
---|
| 45 | #include "caca_internals.h" |
---|
[524] | 46 | #include "cucul.h" |
---|
| 47 | #include "cucul_internals.h" |
---|
[17] | 48 | |
---|
[281] | 49 | /* |
---|
| 50 | * Local functions |
---|
| 51 | */ |
---|
[540] | 52 | |
---|
[539] | 53 | #if defined(HAVE_SIGNAL) |
---|
[348] | 54 | static RETSIGTYPE sigwinch_handler(int); |
---|
[527] | 55 | static caca_t *sigwinch_kk; /* FIXME: we ought to get rid of this */ |
---|
[348] | 56 | #endif |
---|
[550] | 57 | #if defined(HAVE_GETENV) && defined(HAVE_PUTENV) |
---|
| 58 | static void ncurses_check_terminal(void); |
---|
| 59 | #endif |
---|
[629] | 60 | static void ncurses_write_utf32(uint32_t); |
---|
[348] | 61 | |
---|
[550] | 62 | struct driver_private |
---|
| 63 | { |
---|
| 64 | int attr[16*16]; |
---|
| 65 | mmask_t oldmask; |
---|
| 66 | }; |
---|
| 67 | |
---|
[540] | 68 | static int ncurses_init_graphics(caca_t *kk) |
---|
[539] | 69 | { |
---|
| 70 | static int curses_colors[] = |
---|
| 71 | { |
---|
| 72 | /* Standard curses colours */ |
---|
| 73 | COLOR_BLACK, |
---|
| 74 | COLOR_BLUE, |
---|
| 75 | COLOR_GREEN, |
---|
| 76 | COLOR_CYAN, |
---|
| 77 | COLOR_RED, |
---|
| 78 | COLOR_MAGENTA, |
---|
| 79 | COLOR_YELLOW, |
---|
| 80 | COLOR_WHITE, |
---|
| 81 | /* Extra values for xterm-16color */ |
---|
| 82 | COLOR_BLACK + 8, |
---|
| 83 | COLOR_BLUE + 8, |
---|
| 84 | COLOR_GREEN + 8, |
---|
| 85 | COLOR_CYAN + 8, |
---|
| 86 | COLOR_RED + 8, |
---|
| 87 | COLOR_MAGENTA + 8, |
---|
| 88 | COLOR_YELLOW + 8, |
---|
| 89 | COLOR_WHITE + 8 |
---|
| 90 | }; |
---|
[300] | 91 | |
---|
[539] | 92 | mmask_t newmask; |
---|
| 93 | int fg, bg, max; |
---|
[483] | 94 | |
---|
[550] | 95 | kk->drv.p = malloc(sizeof(struct driver_private)); |
---|
| 96 | |
---|
| 97 | #if defined(HAVE_GETENV) && defined(HAVE_PUTENV) |
---|
| 98 | ncurses_check_terminal(); |
---|
| 99 | #endif |
---|
| 100 | |
---|
[539] | 101 | #if defined(HAVE_SIGNAL) |
---|
[524] | 102 | sigwinch_kk = kk; |
---|
[348] | 103 | signal(SIGWINCH, sigwinch_handler); |
---|
| 104 | #endif |
---|
| 105 | |
---|
[539] | 106 | initscr(); |
---|
| 107 | keypad(stdscr, TRUE); |
---|
| 108 | nonl(); |
---|
| 109 | raw(); |
---|
| 110 | noecho(); |
---|
| 111 | nodelay(stdscr, TRUE); |
---|
| 112 | curs_set(0); |
---|
[227] | 113 | |
---|
[539] | 114 | /* Activate mouse */ |
---|
| 115 | newmask = REPORT_MOUSE_POSITION | ALL_MOUSE_EVENTS; |
---|
[550] | 116 | mousemask(newmask, &kk->drv.p->oldmask); |
---|
[539] | 117 | mouseinterval(-1); /* No click emulation */ |
---|
[231] | 118 | |
---|
[539] | 119 | /* Set the escape delay to a ridiculously low value */ |
---|
| 120 | ESCDELAY = 10; |
---|
[227] | 121 | |
---|
[539] | 122 | /* Activate colour */ |
---|
| 123 | start_color(); |
---|
[227] | 124 | |
---|
[539] | 125 | /* If COLORS == 16, it means the terminal supports full bright colours |
---|
| 126 | * using setab and setaf (will use \e[90m \e[91m etc. for colours >= 8), |
---|
| 127 | * we can build 16*16 colour pairs. |
---|
| 128 | * If COLORS == 8, it means the terminal does not know about bright |
---|
| 129 | * colours and we need to get them through A_BOLD and A_BLINK (\e[1m |
---|
| 130 | * and \e[5m). We can only build 8*8 colour pairs. */ |
---|
| 131 | max = COLORS >= 16 ? 16 : 8; |
---|
[227] | 132 | |
---|
[539] | 133 | for(bg = 0; bg < max; bg++) |
---|
| 134 | for(fg = 0; fg < max; fg++) |
---|
| 135 | { |
---|
| 136 | /* Use ((max + 7 - fg) % max) instead of fg so that colour 0 |
---|
| 137 | * is light gray on black. Some terminals don't like this |
---|
| 138 | * colour pair to be redefined. */ |
---|
| 139 | int col = ((max + 7 - fg) % max) + max * bg; |
---|
| 140 | init_pair(col, curses_colors[fg], curses_colors[bg]); |
---|
[550] | 141 | kk->drv.p->attr[fg + 16 * bg] = COLOR_PAIR(col); |
---|
[227] | 142 | |
---|
[539] | 143 | if(max == 8) |
---|
[265] | 144 | { |
---|
[539] | 145 | /* Bright fg on simple bg */ |
---|
[550] | 146 | kk->drv.p->attr[fg + 8 + 16 * bg] = A_BOLD | COLOR_PAIR(col); |
---|
[539] | 147 | /* Simple fg on bright bg */ |
---|
[550] | 148 | kk->drv.p->attr[fg + 16 * (bg + 8)] = A_BLINK |
---|
[539] | 149 | | COLOR_PAIR(col); |
---|
| 150 | /* Bright fg on bright bg */ |
---|
[550] | 151 | kk->drv.p->attr[fg + 8 + 16 * (bg + 8)] = A_BLINK | A_BOLD |
---|
[263] | 152 | | COLOR_PAIR(col); |
---|
[227] | 153 | } |
---|
[265] | 154 | } |
---|
[263] | 155 | |
---|
[582] | 156 | _cucul_set_size(kk->qq, COLS, LINES); |
---|
[300] | 157 | |
---|
[227] | 158 | return 0; |
---|
| 159 | } |
---|
| 160 | |
---|
[540] | 161 | static int ncurses_end_graphics(caca_t *kk) |
---|
[251] | 162 | { |
---|
[550] | 163 | mousemask(kk->drv.p->oldmask, NULL); |
---|
[539] | 164 | curs_set(1); |
---|
| 165 | noraw(); |
---|
| 166 | endwin(); |
---|
[300] | 167 | |
---|
[550] | 168 | free(kk->drv.p); |
---|
| 169 | |
---|
[251] | 170 | return 0; |
---|
| 171 | } |
---|
| 172 | |
---|
[540] | 173 | static int ncurses_set_window_title(caca_t *kk, char const *title) |
---|
[343] | 174 | { |
---|
| 175 | return 0; |
---|
| 176 | } |
---|
| 177 | |
---|
[540] | 178 | static unsigned int ncurses_get_window_width(caca_t *kk) |
---|
[352] | 179 | { |
---|
| 180 | /* Fallback to a 6x10 font */ |
---|
[524] | 181 | return kk->qq->width * 6; |
---|
[352] | 182 | } |
---|
| 183 | |
---|
[540] | 184 | static unsigned int ncurses_get_window_height(caca_t *kk) |
---|
[352] | 185 | { |
---|
| 186 | /* Fallback to a 6x10 font */ |
---|
[524] | 187 | return kk->qq->height * 10; |
---|
[352] | 188 | } |
---|
| 189 | |
---|
[540] | 190 | static void ncurses_display(caca_t *kk) |
---|
[227] | 191 | { |
---|
[539] | 192 | int x, y; |
---|
[724] | 193 | uint32_t *attr = kk->qq->attr; |
---|
[539] | 194 | uint32_t *chars = kk->qq->chars; |
---|
| 195 | for(y = 0; y < (int)kk->qq->height; y++) |
---|
[265] | 196 | { |
---|
[539] | 197 | move(y, 0); |
---|
| 198 | for(x = kk->qq->width; x--; ) |
---|
[527] | 199 | { |
---|
[728] | 200 | attrset(kk->drv.p->attr[_cucul_argb32_to_ansi8(*attr++)]); |
---|
[629] | 201 | ncurses_write_utf32(*chars++); |
---|
[527] | 202 | } |
---|
[265] | 203 | } |
---|
[539] | 204 | refresh(); |
---|
[227] | 205 | } |
---|
| 206 | |
---|
[553] | 207 | static void ncurses_handle_resize(caca_t *kk) |
---|
[347] | 208 | { |
---|
[539] | 209 | struct winsize size; |
---|
[347] | 210 | |
---|
[539] | 211 | if(ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) |
---|
[347] | 212 | { |
---|
[553] | 213 | kk->resize.w = size.ws_col; |
---|
| 214 | kk->resize.h = size.ws_row; |
---|
[364] | 215 | #if defined(HAVE_RESIZE_TERM) |
---|
[553] | 216 | resize_term(kk->resize.h, kk->resize.w); |
---|
[364] | 217 | #else |
---|
[553] | 218 | resizeterm(*kk->resize.h, *kk->resize.w); |
---|
[364] | 219 | #endif |
---|
[539] | 220 | wrefresh(curscr); |
---|
[553] | 221 | return; |
---|
[347] | 222 | } |
---|
[553] | 223 | |
---|
| 224 | /* Fallback */ |
---|
| 225 | kk->resize.w = kk->qq->width; |
---|
| 226 | kk->resize.h = kk->qq->height; |
---|
[347] | 227 | } |
---|
| 228 | |
---|
[681] | 229 | static int ncurses_get_event(caca_t *kk, struct caca_event *ev) |
---|
[548] | 230 | { |
---|
| 231 | int intkey; |
---|
| 232 | |
---|
| 233 | intkey = getch(); |
---|
| 234 | if(intkey == ERR) |
---|
[681] | 235 | { |
---|
| 236 | ev->type = CACA_EVENT_NONE; |
---|
| 237 | return 0; |
---|
| 238 | } |
---|
[548] | 239 | |
---|
| 240 | if(intkey < 0x100) |
---|
| 241 | { |
---|
[681] | 242 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
| 243 | ev->data.key.c = intkey; |
---|
| 244 | return 1; |
---|
[548] | 245 | } |
---|
| 246 | |
---|
| 247 | if(intkey == KEY_MOUSE) |
---|
| 248 | { |
---|
| 249 | MEVENT mevent; |
---|
| 250 | getmouse(&mevent); |
---|
| 251 | |
---|
| 252 | switch(mevent.bstate) |
---|
| 253 | { |
---|
| 254 | case BUTTON1_PRESSED: |
---|
[681] | 255 | ev->data.mouse.button = 1; |
---|
| 256 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
[548] | 257 | break; |
---|
| 258 | case BUTTON1_RELEASED: |
---|
[681] | 259 | ev->data.mouse.button = 1; |
---|
| 260 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 261 | break; |
---|
| 262 | case BUTTON1_CLICKED: |
---|
[681] | 263 | ev->data.mouse.button = 1; |
---|
| 264 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 265 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 266 | break; |
---|
| 267 | case BUTTON1_DOUBLE_CLICKED: |
---|
[681] | 268 | ev->data.mouse.button = 1; |
---|
| 269 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 270 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
| 271 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 272 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 273 | break; |
---|
| 274 | case BUTTON1_TRIPLE_CLICKED: |
---|
[681] | 275 | ev->data.mouse.button = 1; |
---|
| 276 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 277 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
| 278 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 279 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
| 280 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 281 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 282 | break; |
---|
| 283 | case BUTTON1_RESERVED_EVENT: |
---|
| 284 | break; |
---|
| 285 | |
---|
| 286 | case BUTTON2_PRESSED: |
---|
[681] | 287 | ev->data.mouse.button = 2; |
---|
| 288 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
[548] | 289 | break; |
---|
| 290 | case BUTTON2_RELEASED: |
---|
[681] | 291 | ev->data.mouse.button = 2; |
---|
| 292 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 293 | break; |
---|
| 294 | case BUTTON2_CLICKED: |
---|
[681] | 295 | ev->data.mouse.button = 2; |
---|
| 296 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 297 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 298 | break; |
---|
| 299 | case BUTTON2_DOUBLE_CLICKED: |
---|
[681] | 300 | ev->data.mouse.button = 2; |
---|
| 301 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 302 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
| 303 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 304 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 305 | break; |
---|
| 306 | case BUTTON2_TRIPLE_CLICKED: |
---|
[681] | 307 | ev->data.mouse.button = 2; |
---|
| 308 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 309 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
| 310 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 311 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
| 312 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 313 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 314 | break; |
---|
| 315 | case BUTTON2_RESERVED_EVENT: |
---|
| 316 | break; |
---|
| 317 | |
---|
| 318 | case BUTTON3_PRESSED: |
---|
[681] | 319 | ev->data.mouse.button = 3; |
---|
| 320 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
[548] | 321 | break; |
---|
| 322 | case BUTTON3_RELEASED: |
---|
[681] | 323 | ev->data.mouse.button = 3; |
---|
| 324 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 325 | break; |
---|
| 326 | case BUTTON3_CLICKED: |
---|
[681] | 327 | ev->data.mouse.button = 3; |
---|
| 328 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 329 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 330 | break; |
---|
| 331 | case BUTTON3_DOUBLE_CLICKED: |
---|
[681] | 332 | ev->data.mouse.button = 3; |
---|
| 333 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 334 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
| 335 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 336 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 337 | break; |
---|
| 338 | case BUTTON3_TRIPLE_CLICKED: |
---|
[681] | 339 | ev->data.mouse.button = 3; |
---|
| 340 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 341 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
| 342 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 343 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
| 344 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 345 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 346 | break; |
---|
| 347 | case BUTTON3_RESERVED_EVENT: |
---|
| 348 | break; |
---|
| 349 | |
---|
| 350 | case BUTTON4_PRESSED: |
---|
[681] | 351 | ev->data.mouse.button = 4; |
---|
| 352 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
[548] | 353 | break; |
---|
| 354 | case BUTTON4_RELEASED: |
---|
[681] | 355 | ev->data.mouse.button = 4; |
---|
| 356 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 357 | break; |
---|
| 358 | case BUTTON4_CLICKED: |
---|
[681] | 359 | ev->data.mouse.button = 4; |
---|
| 360 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 361 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 362 | break; |
---|
| 363 | case BUTTON4_DOUBLE_CLICKED: |
---|
[681] | 364 | ev->data.mouse.button = 4; |
---|
| 365 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 366 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
| 367 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 368 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 369 | break; |
---|
| 370 | case BUTTON4_TRIPLE_CLICKED: |
---|
[681] | 371 | ev->data.mouse.button = 4; |
---|
| 372 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 373 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
| 374 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 375 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
| 376 | ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(kk, ev); |
---|
| 377 | ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(kk, ev); |
---|
[548] | 378 | break; |
---|
| 379 | case BUTTON4_RESERVED_EVENT: |
---|
| 380 | break; |
---|
| 381 | |
---|
| 382 | default: |
---|
| 383 | break; |
---|
| 384 | } |
---|
| 385 | |
---|
[551] | 386 | if(kk->mouse.x == (unsigned int)mevent.x && |
---|
| 387 | kk->mouse.y == (unsigned int)mevent.y) |
---|
[681] | 388 | return _pop_event(kk, ev); |
---|
[548] | 389 | |
---|
[551] | 390 | kk->mouse.x = mevent.x; |
---|
| 391 | kk->mouse.y = mevent.y; |
---|
[548] | 392 | |
---|
[681] | 393 | ev->type = CACA_EVENT_MOUSE_MOTION; |
---|
| 394 | ev->data.mouse.x = kk->mouse.x; |
---|
| 395 | ev->data.mouse.y = kk->mouse.y; |
---|
| 396 | return 1; |
---|
[548] | 397 | } |
---|
| 398 | |
---|
| 399 | switch(intkey) |
---|
| 400 | { |
---|
[681] | 401 | case KEY_UP: ev->data.key.c = CACA_KEY_UP; break; |
---|
| 402 | case KEY_DOWN: ev->data.key.c = CACA_KEY_DOWN; break; |
---|
| 403 | case KEY_LEFT: ev->data.key.c = CACA_KEY_LEFT; break; |
---|
| 404 | case KEY_RIGHT: ev->data.key.c = CACA_KEY_RIGHT; break; |
---|
[548] | 405 | |
---|
[681] | 406 | case KEY_IC: ev->data.key.c = CACA_KEY_INSERT; break; |
---|
| 407 | case KEY_DC: ev->data.key.c = CACA_KEY_DELETE; break; |
---|
| 408 | case KEY_HOME: ev->data.key.c = CACA_KEY_HOME; break; |
---|
| 409 | case KEY_END: ev->data.key.c = CACA_KEY_END; break; |
---|
| 410 | case KEY_PPAGE: ev->data.key.c = CACA_KEY_PAGEUP; break; |
---|
| 411 | case KEY_NPAGE: ev->data.key.c = CACA_KEY_PAGEDOWN; break; |
---|
[548] | 412 | |
---|
[681] | 413 | case KEY_F(1): ev->data.key.c = CACA_KEY_F1; break; |
---|
| 414 | case KEY_F(2): ev->data.key.c = CACA_KEY_F2; break; |
---|
| 415 | case KEY_F(3): ev->data.key.c = CACA_KEY_F3; break; |
---|
| 416 | case KEY_F(4): ev->data.key.c = CACA_KEY_F4; break; |
---|
| 417 | case KEY_F(5): ev->data.key.c = CACA_KEY_F5; break; |
---|
| 418 | case KEY_F(6): ev->data.key.c = CACA_KEY_F6; break; |
---|
| 419 | case KEY_F(7): ev->data.key.c = CACA_KEY_F7; break; |
---|
| 420 | case KEY_F(8): ev->data.key.c = CACA_KEY_F8; break; |
---|
| 421 | case KEY_F(9): ev->data.key.c = CACA_KEY_F9; break; |
---|
| 422 | case KEY_F(10): ev->data.key.c = CACA_KEY_F10; break; |
---|
| 423 | case KEY_F(11): ev->data.key.c = CACA_KEY_F11; break; |
---|
| 424 | case KEY_F(12): ev->data.key.c = CACA_KEY_F12; break; |
---|
| 425 | |
---|
| 426 | default: ev->type = CACA_EVENT_NONE; return 0; |
---|
[548] | 427 | } |
---|
| 428 | |
---|
[681] | 429 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
| 430 | ev->data.key.ucs4 = 0; |
---|
| 431 | ev->data.key.utf8[0] = '\0'; |
---|
| 432 | return 1; |
---|
[548] | 433 | } |
---|
| 434 | |
---|
[539] | 435 | /* |
---|
| 436 | * XXX: following functions are local |
---|
| 437 | */ |
---|
[281] | 438 | |
---|
[539] | 439 | #if defined(HAVE_SIGNAL) |
---|
[348] | 440 | static RETSIGTYPE sigwinch_handler(int sig) |
---|
| 441 | { |
---|
[553] | 442 | sigwinch_kk->resize.resized = 1; |
---|
[348] | 443 | |
---|
[613] | 444 | signal(SIGWINCH, sigwinch_handler); |
---|
[348] | 445 | } |
---|
| 446 | #endif |
---|
| 447 | |
---|
[550] | 448 | #if defined(HAVE_GETENV) && defined(HAVE_PUTENV) |
---|
| 449 | static void ncurses_check_terminal(void) |
---|
| 450 | { |
---|
| 451 | char *term, *colorterm, *other; |
---|
| 452 | |
---|
| 453 | term = getenv("TERM"); |
---|
| 454 | colorterm = getenv("COLORTERM"); |
---|
| 455 | |
---|
| 456 | if(term && !strcmp(term, "xterm")) |
---|
| 457 | { |
---|
| 458 | /* If we are using gnome-terminal, it's really a 16 colour terminal */ |
---|
| 459 | if(colorterm && !strcmp(colorterm, "gnome-terminal")) |
---|
| 460 | { |
---|
| 461 | SCREEN *screen; |
---|
| 462 | screen = newterm("xterm-16color", stdout, stdin); |
---|
| 463 | if(screen == NULL) |
---|
| 464 | return; |
---|
| 465 | endwin(); |
---|
| 466 | (void)putenv("TERM=xterm-16color"); |
---|
| 467 | return; |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | /* Ditto if we are using Konsole */ |
---|
| 471 | other = getenv("KONSOLE_DCOP_SESSION"); |
---|
| 472 | if(other) |
---|
| 473 | { |
---|
| 474 | SCREEN *screen; |
---|
| 475 | screen = newterm("xterm-16color", stdout, stdin); |
---|
| 476 | if(screen == NULL) |
---|
| 477 | return; |
---|
| 478 | endwin(); |
---|
| 479 | (void)putenv("TERM=xterm-16color"); |
---|
| 480 | return; |
---|
| 481 | } |
---|
| 482 | } |
---|
| 483 | } |
---|
| 484 | #endif |
---|
| 485 | |
---|
[629] | 486 | static void ncurses_write_utf32(uint32_t c) |
---|
| 487 | { |
---|
| 488 | #if defined(HAVE_NCURSESW_NCURSES_H) |
---|
| 489 | static const uint8_t mark[7] = |
---|
| 490 | { |
---|
| 491 | 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC |
---|
| 492 | }; |
---|
| 493 | |
---|
| 494 | char buf[10], *parser; |
---|
| 495 | int bytes; |
---|
| 496 | #endif |
---|
| 497 | |
---|
| 498 | if(c < 0x80) |
---|
| 499 | { |
---|
| 500 | addch(c); |
---|
| 501 | return; |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | #if defined(HAVE_NCURSESW_NCURSES_H) |
---|
| 505 | if(c < 0x10000) |
---|
| 506 | { |
---|
| 507 | addch(c); /* FIXME: doesn't work either */ |
---|
| 508 | return; |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | bytes = (c < 0x800) ? 2 : (c < 0x10000) ? 3 : 4; |
---|
| 512 | buf[bytes] = '\0'; |
---|
| 513 | parser = buf + bytes; |
---|
| 514 | |
---|
| 515 | switch(bytes) |
---|
| 516 | { |
---|
| 517 | case 4: *--parser = (c | 0x80) & 0xbf; c >>= 6; |
---|
| 518 | case 3: *--parser = (c | 0x80) & 0xbf; c >>= 6; |
---|
| 519 | case 2: *--parser = (c | 0x80) & 0xbf; c >>= 6; |
---|
| 520 | } |
---|
| 521 | *--parser = c | mark[bytes]; |
---|
| 522 | |
---|
| 523 | addstr(buf); |
---|
| 524 | #else |
---|
| 525 | addch(' '); |
---|
| 526 | #endif |
---|
| 527 | } |
---|
| 528 | |
---|
[539] | 529 | /* |
---|
| 530 | * Driver initialisation |
---|
| 531 | */ |
---|
[527] | 532 | |
---|
[684] | 533 | int ncurses_install(caca_t *kk) |
---|
[511] | 534 | { |
---|
[550] | 535 | kk->drv.driver = CACA_DRIVER_NCURSES; |
---|
[527] | 536 | |
---|
[550] | 537 | kk->drv.init_graphics = ncurses_init_graphics; |
---|
| 538 | kk->drv.end_graphics = ncurses_end_graphics; |
---|
| 539 | kk->drv.set_window_title = ncurses_set_window_title; |
---|
| 540 | kk->drv.get_window_width = ncurses_get_window_width; |
---|
| 541 | kk->drv.get_window_height = ncurses_get_window_height; |
---|
| 542 | kk->drv.display = ncurses_display; |
---|
| 543 | kk->drv.handle_resize = ncurses_handle_resize; |
---|
| 544 | kk->drv.get_event = ncurses_get_event; |
---|
[689] | 545 | kk->drv.set_mouse = NULL; |
---|
| 546 | |
---|
[684] | 547 | return 0; |
---|
[511] | 548 | } |
---|
| 549 | |
---|
[539] | 550 | #endif /* USE_NCURSES */ |
---|
[527] | 551 | |
---|