You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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