Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ncurses.c 22 KiB

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