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.
 
 
 
 
 
 

552 lines
17 KiB

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