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 21 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. dp->resize.allow = 1;
  246. caca_set_canvas_size(dp->cv, COLS, LINES);
  247. dp->resize.allow = 0;
  248. return 0;
  249. }
  250. static int ncurses_end_graphics(caca_display_t *dp)
  251. {
  252. _caca_set_term_title("");
  253. mousemask(dp->drv.p->oldmask, NULL);
  254. curs_set(1);
  255. noraw();
  256. endwin();
  257. #if defined HAVE_GETENV && defined HAVE_PUTENV
  258. ncurses_uninstall_terminal(dp);
  259. #endif
  260. free(dp->drv.p);
  261. return 0;
  262. }
  263. static int ncurses_set_display_title(caca_display_t *dp, char const *title)
  264. {
  265. _caca_set_term_title(title);
  266. return 0;
  267. }
  268. static int ncurses_get_display_width(caca_display_t const *dp)
  269. {
  270. /* Fallback to a 6x10 font */
  271. return caca_get_canvas_width(dp->cv) * 6;
  272. }
  273. static int ncurses_get_display_height(caca_display_t const *dp)
  274. {
  275. /* Fallback to a 6x10 font */
  276. return caca_get_canvas_height(dp->cv) * 10;
  277. }
  278. static void ncurses_display(caca_display_t *dp)
  279. {
  280. int x, y, i;
  281. for(i = 0; i < caca_get_dirty_rectangle_count(dp->cv); i++)
  282. {
  283. uint32_t const *cvchars, *cvattrs;
  284. int xmin, ymin, xmax, ymax;
  285. caca_get_dirty_rectangle(dp->cv, i, &xmin, &ymin, &xmax, &ymax);
  286. cvchars = (uint32_t const *)caca_get_canvas_chars(dp->cv)
  287. + xmin + ymin * dp->cv->width;
  288. cvattrs = (uint32_t const *)caca_get_canvas_attrs(dp->cv)
  289. + xmin + ymin * dp->cv->width;
  290. for(y = ymin; y <= ymax; y++)
  291. {
  292. move(y, xmin);
  293. for(x = xmin; x <= xmax; x++)
  294. {
  295. attrset(dp->drv.p->attr[caca_attr_to_ansi(*cvattrs++)]);
  296. ncurses_write_utf32(*cvchars++);
  297. }
  298. cvchars += dp->cv->width - (xmax - xmin) - 1;
  299. cvattrs += dp->cv->width - (xmax - xmin) - 1;
  300. }
  301. }
  302. x = caca_get_cursor_x(dp->cv);
  303. y = caca_get_cursor_y(dp->cv);
  304. move(y, x);
  305. refresh();
  306. }
  307. static void ncurses_handle_resize(caca_display_t *dp)
  308. {
  309. struct winsize size;
  310. #if defined HAVE_SYS_IOCTL_H
  311. if(ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0)
  312. {
  313. dp->resize.w = size.ws_col;
  314. dp->resize.h = size.ws_row;
  315. #if defined HAVE_RESIZE_TERM
  316. resize_term(dp->resize.h, dp->resize.w);
  317. #else
  318. resizeterm(dp->resize.h, dp->resize.w);
  319. #endif
  320. wrefresh(curscr);
  321. return;
  322. }
  323. #endif
  324. /* Fallback */
  325. dp->resize.w = caca_get_canvas_width(dp->cv);
  326. dp->resize.h = caca_get_canvas_height(dp->cv);
  327. }
  328. static int ncurses_get_event(caca_display_t *dp, caca_privevent_t *ev)
  329. {
  330. int intkey;
  331. intkey = getch();
  332. if(intkey == ERR)
  333. {
  334. ev->type = CACA_EVENT_NONE;
  335. return 0;
  336. }
  337. if(intkey < 0x7f)
  338. {
  339. ev->type = CACA_EVENT_KEY_PRESS;
  340. ev->data.key.ch = intkey;
  341. ev->data.key.utf32 = intkey;
  342. ev->data.key.utf8[0] = intkey;
  343. ev->data.key.utf8[1] = '\0';
  344. return 1;
  345. }
  346. /* If the key was UTF-8, parse the whole sequence */
  347. if(intkey >= 0x80 && intkey < 0x100)
  348. {
  349. int keys[7]; /* Necessary for ungetch(); */
  350. char utf8[7];
  351. uint32_t utf32;
  352. size_t i, bytes = 0;
  353. keys[0] = intkey;
  354. utf8[0] = intkey;
  355. for(i = 1; i < 6; i++)
  356. {
  357. keys[i] = getch();
  358. utf8[i] = (unsigned char)keys[i];
  359. }
  360. utf8[i] = '\0';
  361. utf32 = caca_utf8_to_utf32(utf8, &bytes);
  362. while(i > bytes)
  363. ungetch(keys[--i]);
  364. if(bytes)
  365. {
  366. ev->type = CACA_EVENT_KEY_PRESS;
  367. ev->data.key.ch = 0;
  368. ev->data.key.utf32 = utf32;
  369. strcpy(ev->data.key.utf8, utf8);
  370. return 1;
  371. }
  372. }
  373. if(intkey == KEY_MOUSE)
  374. {
  375. MEVENT mevent;
  376. getmouse(&mevent);
  377. switch(mevent.bstate)
  378. {
  379. #define PRESS(x) ev->data.mouse.button = x; \
  380. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev)
  381. #define RELEASE(x) ev->data.mouse.button = x; \
  382. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev)
  383. #define CLICK(x) PRESS(x); RELEASE(x)
  384. case BUTTON1_PRESSED: PRESS(1); break;
  385. case BUTTON1_RELEASED: RELEASE(1); break;
  386. case BUTTON1_CLICKED: CLICK(1); break;
  387. case BUTTON1_DOUBLE_CLICKED: CLICK(1); CLICK(1); break;
  388. case BUTTON1_TRIPLE_CLICKED: CLICK(1); CLICK(1); CLICK(1); break;
  389. case BUTTON1_RESERVED_EVENT: break;
  390. case BUTTON2_PRESSED: PRESS(2); break;
  391. case BUTTON2_RELEASED: RELEASE(2); break;
  392. case BUTTON2_CLICKED: CLICK(2); break;
  393. case BUTTON2_DOUBLE_CLICKED: CLICK(2); CLICK(2); break;
  394. case BUTTON2_TRIPLE_CLICKED: CLICK(2); CLICK(2); CLICK(2); break;
  395. case BUTTON2_RESERVED_EVENT: break;
  396. case BUTTON3_PRESSED: PRESS(3); break;
  397. case BUTTON3_RELEASED: RELEASE(3); break;
  398. case BUTTON3_CLICKED: CLICK(3); break;
  399. case BUTTON3_DOUBLE_CLICKED: CLICK(3); CLICK(3); break;
  400. case BUTTON3_TRIPLE_CLICKED: CLICK(3); CLICK(3); CLICK(3); break;
  401. case BUTTON3_RESERVED_EVENT: break;
  402. case BUTTON4_PRESSED: PRESS(4); break;
  403. case BUTTON4_RELEASED: RELEASE(4); break;
  404. case BUTTON4_CLICKED: CLICK(4); break;
  405. case BUTTON4_DOUBLE_CLICKED: CLICK(4); CLICK(4); break;
  406. case BUTTON4_TRIPLE_CLICKED: CLICK(4); CLICK(4); CLICK(4); break;
  407. case BUTTON4_RESERVED_EVENT: break;
  408. default:
  409. break;
  410. #undef PRESS
  411. #undef RELEASE
  412. #undef CLICK
  413. }
  414. if(dp->mouse.x == mevent.x && dp->mouse.y == mevent.y)
  415. return _pop_event(dp, ev);
  416. dp->mouse.x = mevent.x;
  417. dp->mouse.y = mevent.y;
  418. ev->type = CACA_EVENT_MOUSE_MOTION;
  419. ev->data.mouse.x = dp->mouse.x;
  420. ev->data.mouse.y = dp->mouse.y;
  421. return 1;
  422. }
  423. switch(intkey)
  424. {
  425. case KEY_UP: ev->data.key.ch = CACA_KEY_UP; break;
  426. case KEY_DOWN: ev->data.key.ch = CACA_KEY_DOWN; break;
  427. case KEY_LEFT: ev->data.key.ch = CACA_KEY_LEFT; break;
  428. case KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break;
  429. case KEY_IC: ev->data.key.ch = CACA_KEY_INSERT; break;
  430. case KEY_DC: ev->data.key.ch = CACA_KEY_DELETE; break;
  431. case 0x7f:
  432. case KEY_BACKSPACE: ev->data.key.ch = CACA_KEY_BACKSPACE; break;
  433. case KEY_HOME: ev->data.key.ch = CACA_KEY_HOME; break;
  434. case KEY_END: ev->data.key.ch = CACA_KEY_END; break;
  435. case KEY_PPAGE: ev->data.key.ch = CACA_KEY_PAGEUP; break;
  436. case KEY_NPAGE: ev->data.key.ch = CACA_KEY_PAGEDOWN; break;
  437. case KEY_F(1): ev->data.key.ch = CACA_KEY_F1; break;
  438. case KEY_F(2): ev->data.key.ch = CACA_KEY_F2; break;
  439. case KEY_F(3): ev->data.key.ch = CACA_KEY_F3; break;
  440. case KEY_F(4): ev->data.key.ch = CACA_KEY_F4; break;
  441. case KEY_F(5): ev->data.key.ch = CACA_KEY_F5; break;
  442. case KEY_F(6): ev->data.key.ch = CACA_KEY_F6; break;
  443. case KEY_F(7): ev->data.key.ch = CACA_KEY_F7; break;
  444. case KEY_F(8): ev->data.key.ch = CACA_KEY_F8; break;
  445. case KEY_F(9): ev->data.key.ch = CACA_KEY_F9; break;
  446. case KEY_F(10): ev->data.key.ch = CACA_KEY_F10; break;
  447. case KEY_F(11): ev->data.key.ch = CACA_KEY_F11; break;
  448. case KEY_F(12): ev->data.key.ch = CACA_KEY_F12; break;
  449. default:
  450. /* Unknown key */
  451. ev->type = CACA_EVENT_NONE; return 0;
  452. }
  453. ev->type = CACA_EVENT_KEY_PRESS;
  454. ev->data.key.utf32 = 0;
  455. ev->data.key.utf8[0] = '\0';
  456. return 1;
  457. }
  458. static void ncurses_set_cursor(caca_display_t *dp, int flags)
  459. {
  460. curs_set(flags ? 2 : 0);
  461. }
  462. /*
  463. * XXX: following functions are local
  464. */
  465. #if defined HAVE_SIGNAL
  466. static RETSIGTYPE sigwinch_handler(int sig)
  467. {
  468. sigwinch_d->resize.resized = 1;
  469. signal(SIGWINCH, sigwinch_handler);
  470. }
  471. #endif
  472. #if defined HAVE_GETENV && defined HAVE_PUTENV
  473. static void ncurses_install_terminal(caca_display_t *dp)
  474. {
  475. char *term, *colorterm;
  476. dp->drv.p->term = NULL;
  477. term = getenv("TERM");
  478. colorterm = getenv("COLORTERM");
  479. if(!term || strcmp(term, "xterm"))
  480. return;
  481. /* If we are using gnome-terminal, it's really a 16 colour terminal.
  482. * Ditto if we are using xfce4-terminal, or Konsole. */
  483. if((colorterm && (!strcmp(colorterm, "gnome-terminal")
  484. || !strcmp(colorterm, "Terminal")))
  485. || getenv("KONSOLE_DCOP_SESSION"))
  486. {
  487. SCREEN *screen;
  488. screen = newterm("xterm-16color", stdout, stdin);
  489. if(screen == NULL)
  490. return;
  491. endwin();
  492. (void)putenv("TERM=xterm-16color");
  493. dp->drv.p->term = strdup(term);
  494. return;
  495. }
  496. }
  497. static void ncurses_uninstall_terminal(caca_display_t *dp)
  498. {
  499. /* Needs to be persistent because we use putenv() */
  500. static char termenv[1024];
  501. if(!dp->drv.p->term)
  502. return;
  503. snprintf(termenv, 1023, "TERM=%s", dp->drv.p->term);
  504. free(dp->drv.p->term);
  505. (void)putenv(termenv);
  506. }
  507. #endif
  508. static void ncurses_write_utf32(uint32_t ch)
  509. {
  510. #if defined HAVE_NCURSESW_NCURSES_H
  511. char buf[10];
  512. int bytes;
  513. #endif
  514. if(ch == CACA_MAGIC_FULLWIDTH)
  515. return;
  516. #if defined HAVE_NCURSESW_NCURSES_H
  517. bytes = caca_utf32_to_utf8(buf, ch);
  518. buf[bytes] = '\0';
  519. addstr(buf);
  520. #else
  521. if(ch < 0x80)
  522. {
  523. addch(ch);
  524. }
  525. else
  526. {
  527. chtype cch;
  528. chtype cch2;
  529. cch = '?';
  530. cch2 = ' ';
  531. if ((ch > 0x0000ff00) && (ch < 0x0000ff5f))
  532. {
  533. cch = ch - 0x0000ff00 + ' ';
  534. }
  535. switch (ch)
  536. {
  537. case 0x000000a0: /* <nbsp> */
  538. case 0x00003000: /*   */
  539. cch = ' ';
  540. break;
  541. case 0x000000a3: /* £ */
  542. cch = ACS_STERLING;
  543. break;
  544. case 0x000000b0: /* ° */
  545. cch = ACS_DEGREE;
  546. break;
  547. case 0x000000b1: /* ± */
  548. cch = ACS_PLMINUS;
  549. break;
  550. case 0x000000b7: /* · */
  551. case 0x00002219: /* ∙ */
  552. case 0x000030fb: /* ・ */
  553. cch = ACS_BULLET;
  554. break;
  555. case 0x000003c0: /* π */
  556. cch = ACS_PI;
  557. break;
  558. case 0x00002018: /* ‘ */
  559. case 0x00002019: /* ’ */
  560. cch = '\'';
  561. break;
  562. case 0x0000201c: /* “ */
  563. case 0x0000201d: /* ” */
  564. cch = '"';
  565. break;
  566. case 0x00002190: /* ← */
  567. cch = ACS_LARROW;
  568. break;
  569. case 0x00002191: /* ↑ */
  570. cch = ACS_UARROW;
  571. break;
  572. case 0x00002192: /* → */
  573. cch = ACS_RARROW;
  574. break;
  575. case 0x00002193: /* ↓ */
  576. cch = ACS_DARROW;
  577. break;
  578. case 0x00002260: /* ≠ */
  579. cch = ACS_NEQUAL;
  580. break;
  581. case 0x00002261: /* ≡ */
  582. cch = '=';
  583. break;
  584. case 0x00002264: /* ≤ */
  585. cch = ACS_LEQUAL;
  586. break;
  587. case 0x00002265: /* ≥ */
  588. cch = ACS_GEQUAL;
  589. break;
  590. case 0x000023ba: /* ⎺ */
  591. cch = ACS_S1;
  592. cch2 = cch;
  593. break;
  594. case 0x000023bb: /* ⎻ */
  595. cch = ACS_S3;
  596. cch2 = cch;
  597. break;
  598. case 0x000023bc: /* ⎼ */
  599. cch = ACS_S7;
  600. cch2 = cch;
  601. break;
  602. case 0x000023bd: /* ⎽ */
  603. cch = ACS_S9;
  604. cch2 = cch;
  605. break;
  606. case 0x00002500: /* ─ */
  607. case 0x00002550: /* ═ */
  608. cch = ACS_HLINE;
  609. cch2 = cch;
  610. break;
  611. case 0x00002502: /* │ */
  612. case 0x00002551: /* ║ */
  613. cch = ACS_VLINE;
  614. break;
  615. case 0x0000250c: /* ┌ */
  616. case 0x00002552: /* ╒ */
  617. case 0x00002553: /* ╓ */
  618. case 0x00002554: /* ╔ */
  619. cch = ACS_ULCORNER;
  620. cch2 = ACS_HLINE;
  621. break;
  622. case 0x00002510: /* ┐ */
  623. case 0x00002555: /* ╕ */
  624. case 0x00002556: /* ╖ */
  625. case 0x00002557: /* ╗ */
  626. cch = ACS_URCORNER;
  627. break;
  628. case 0x00002514: /* └ */
  629. case 0x00002558: /* ╘ */
  630. case 0x00002559: /* ╙ */
  631. case 0x0000255a: /* ╚ */
  632. cch = ACS_LLCORNER;
  633. cch2 = ACS_HLINE;
  634. break;
  635. case 0x00002518: /* ┘ */
  636. case 0x0000255b: /* ╛ */
  637. case 0x0000255c: /* ╜ */
  638. case 0x0000255d: /* ╝ */
  639. cch = ACS_LRCORNER;
  640. break;
  641. case 0x0000251c: /* ├ */
  642. case 0x0000255e: /* ╞ */
  643. case 0x0000255f: /* ╟ */
  644. case 0x00002560: /* ╠ */
  645. cch = ACS_LTEE;
  646. cch2 = ACS_HLINE;
  647. break;
  648. case 0x00002524: /* ┤ */
  649. case 0x00002561: /* ╡ */
  650. case 0x00002562: /* ╢ */
  651. case 0x00002563: /* ╣ */
  652. cch = ACS_RTEE;
  653. break;
  654. case 0x0000252c: /* ┬ */
  655. case 0x00002564: /* ╤ */
  656. case 0x00002565: /* ╥ */
  657. case 0x00002566: /* ╦ */
  658. cch = ACS_TTEE;
  659. cch2 = ACS_HLINE;
  660. break;
  661. case 0x00002534: /* ┴ */
  662. case 0x00002567: /* ╧ */
  663. case 0x00002568: /* ╨ */
  664. case 0x00002569: /* ╩ */
  665. cch = ACS_BTEE;
  666. cch2 = ACS_HLINE;
  667. break;
  668. case 0x0000253c: /* ┼ */
  669. case 0x0000256a: /* ╪ */
  670. case 0x0000256b: /* ╫ */
  671. case 0x0000256c: /* ╬ */
  672. cch = ACS_PLUS;
  673. cch2 = ACS_HLINE;
  674. break;
  675. case 0x00002591: /* ░ */
  676. cch = ACS_BOARD;
  677. cch2 = cch;
  678. break;
  679. case 0x00002592: /* ▒ */
  680. case 0x00002593: /* ▓ */
  681. cch = ACS_CKBOARD;
  682. cch2 = cch;
  683. break;
  684. case 0x00002580: /* ▀ */
  685. case 0x00002584: /* ▄ */
  686. case 0x00002588: /* █ */
  687. case 0x0000258c: /* ▌ */
  688. case 0x00002590: /* ▐ */
  689. case 0x000025a0: /* ■ */
  690. case 0x000025ac: /* ▬ */
  691. case 0x000025ae: /* ▮ */
  692. cch = ACS_BLOCK;
  693. cch2 = cch;
  694. break;
  695. case 0x000025c6: /* ◆ */
  696. case 0x00002666: /* ♦ */
  697. cch = ACS_DIAMOND;
  698. break;
  699. case 0x00002022: /* • */
  700. case 0x000025cb: /* ○ */
  701. case 0x000025cf: /* ● */
  702. case 0x00002603: /* ☃ */
  703. case 0x0000263c: /* ☼ */
  704. cch = ACS_LANTERN;
  705. break;
  706. case 0x0000301c: /* 〜 */
  707. cch = '~';
  708. break;
  709. }
  710. addch(cch);
  711. if(caca_utf32_is_fullwidth(ch))
  712. {
  713. addch(cch2);
  714. }
  715. }
  716. #endif
  717. }
  718. /*
  719. * Driver initialisation
  720. */
  721. int ncurses_install(caca_display_t *dp)
  722. {
  723. dp->drv.id = CACA_DRIVER_NCURSES;
  724. dp->drv.driver = "ncurses";
  725. dp->drv.init_graphics = ncurses_init_graphics;
  726. dp->drv.end_graphics = ncurses_end_graphics;
  727. dp->drv.set_display_title = ncurses_set_display_title;
  728. dp->drv.get_display_width = ncurses_get_display_width;
  729. dp->drv.get_display_height = ncurses_get_display_height;
  730. dp->drv.display = ncurses_display;
  731. dp->drv.handle_resize = ncurses_handle_resize;
  732. dp->drv.get_event = ncurses_get_event;
  733. dp->drv.set_mouse = NULL;
  734. dp->drv.set_cursor = ncurses_set_cursor;
  735. return 0;
  736. }
  737. #endif /* USE_NCURSES */