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.
 
 
 
 
 
 

485 line
13 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. #include "common.h"
  18. #if defined USE_NCURSES
  19. #if defined HAVE_NCURSESW_NCURSES_H
  20. # include <ncursesw/ncurses.h>
  21. #elif defined HAVE_NCURSES_NCURSES_H
  22. # include <ncurses/ncurses.h>
  23. #elif defined HAVE_NCURSES_H
  24. # include <ncurses.h>
  25. #else
  26. # include <curses.h>
  27. #endif
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #if defined HAVE_SIGNAL_H
  31. # include <signal.h>
  32. #endif
  33. #if defined HAVE_SYS_IOCTL_H
  34. # include <sys/ioctl.h>
  35. #endif
  36. #if defined HAVE_LOCALE_H
  37. # include <locale.h>
  38. #endif
  39. #include "caca.h"
  40. #include "caca_internals.h"
  41. #include "cucul.h"
  42. #include "cucul_internals.h"
  43. /*
  44. * Local functions
  45. */
  46. #if defined HAVE_SIGNAL
  47. static RETSIGTYPE sigwinch_handler(int);
  48. static caca_display_t *sigwinch_d; /* FIXME: we ought to get rid of this */
  49. #endif
  50. #if defined HAVE_GETENV && defined HAVE_PUTENV
  51. static void ncurses_check_terminal(void);
  52. #endif
  53. static void ncurses_write_utf32(uint32_t);
  54. struct driver_private
  55. {
  56. int attr[16*16];
  57. mmask_t oldmask;
  58. };
  59. static int ncurses_init_graphics(caca_display_t *dp)
  60. {
  61. static int curses_colors[] =
  62. {
  63. /* Standard curses colours */
  64. COLOR_BLACK,
  65. COLOR_BLUE,
  66. COLOR_GREEN,
  67. COLOR_CYAN,
  68. COLOR_RED,
  69. COLOR_MAGENTA,
  70. COLOR_YELLOW,
  71. COLOR_WHITE,
  72. /* Extra values for xterm-16color */
  73. COLOR_BLACK + 8,
  74. COLOR_BLUE + 8,
  75. COLOR_GREEN + 8,
  76. COLOR_CYAN + 8,
  77. COLOR_RED + 8,
  78. COLOR_MAGENTA + 8,
  79. COLOR_YELLOW + 8,
  80. COLOR_WHITE + 8
  81. };
  82. mmask_t newmask;
  83. int fg, bg, max;
  84. dp->drv.p = malloc(sizeof(struct driver_private));
  85. #if defined HAVE_GETENV && defined HAVE_PUTENV
  86. ncurses_check_terminal();
  87. #endif
  88. #if defined HAVE_SIGNAL
  89. sigwinch_d = dp;
  90. signal(SIGWINCH, sigwinch_handler);
  91. #endif
  92. #if defined HAVE_LOCALE_H
  93. setlocale(LC_ALL, "");
  94. #endif
  95. _caca_set_term_title("caca for ncurses");
  96. initscr();
  97. keypad(stdscr, TRUE);
  98. nonl();
  99. raw();
  100. noecho();
  101. nodelay(stdscr, TRUE);
  102. curs_set(0);
  103. /* Activate mouse */
  104. newmask = REPORT_MOUSE_POSITION | ALL_MOUSE_EVENTS;
  105. mousemask(newmask, &dp->drv.p->oldmask);
  106. mouseinterval(-1); /* No click emulation */
  107. /* Set the escape delay to a ridiculously low value */
  108. ESCDELAY = 10;
  109. /* Activate colour */
  110. start_color();
  111. /* If COLORS == 16, it means the terminal supports full bright colours
  112. * using setab and setaf (will use \e[90m \e[91m etc. for colours >= 8),
  113. * we can build 16*16 colour pairs.
  114. * If COLORS == 8, it means the terminal does not know about bright
  115. * colours and we need to get them through A_BOLD and A_BLINK (\e[1m
  116. * and \e[5m). We can only build 8*8 colour pairs. */
  117. max = COLORS >= 16 ? 16 : 8;
  118. for(bg = 0; bg < max; bg++)
  119. for(fg = 0; fg < max; fg++)
  120. {
  121. /* Use ((max + 7 - fg) % max) instead of fg so that colour 0
  122. * is light gray on black. Some terminals don't like this
  123. * colour pair to be redefined. */
  124. int col = ((max + 7 - fg) % max) + max * bg;
  125. init_pair(col, curses_colors[fg], curses_colors[bg]);
  126. dp->drv.p->attr[fg + 16 * bg] = COLOR_PAIR(col);
  127. if(max == 8)
  128. {
  129. /* Bright fg on simple bg */
  130. dp->drv.p->attr[fg + 8 + 16 * bg] = A_BOLD | COLOR_PAIR(col);
  131. /* Simple fg on bright bg */
  132. dp->drv.p->attr[fg + 16 * (bg + 8)] = A_BLINK
  133. | COLOR_PAIR(col);
  134. /* Bright fg on bright bg */
  135. dp->drv.p->attr[fg + 8 + 16 * (bg + 8)] = A_BLINK | A_BOLD
  136. | COLOR_PAIR(col);
  137. }
  138. }
  139. _cucul_set_canvas_size(dp->cv, COLS, LINES);
  140. return 0;
  141. }
  142. static int ncurses_end_graphics(caca_display_t *dp)
  143. {
  144. _caca_set_term_title("");
  145. mousemask(dp->drv.p->oldmask, NULL);
  146. curs_set(1);
  147. noraw();
  148. endwin();
  149. free(dp->drv.p);
  150. return 0;
  151. }
  152. static int ncurses_set_display_title(caca_display_t *dp, char const *title)
  153. {
  154. _caca_set_term_title(title);
  155. return 0;
  156. }
  157. static unsigned int ncurses_get_display_width(caca_display_t *dp)
  158. {
  159. /* Fallback to a 6x10 font */
  160. return dp->cv->width * 6;
  161. }
  162. static unsigned int ncurses_get_display_height(caca_display_t *dp)
  163. {
  164. /* Fallback to a 6x10 font */
  165. return dp->cv->height * 10;
  166. }
  167. static void ncurses_display(caca_display_t *dp)
  168. {
  169. int x, y;
  170. uint32_t *attrs = dp->cv->attrs;
  171. uint32_t *chars = dp->cv->chars;
  172. for(y = 0; y < (int)dp->cv->height; y++)
  173. {
  174. move(y, 0);
  175. for(x = dp->cv->width; x--; )
  176. {
  177. attrset(dp->drv.p->attr[cucul_attr_to_ansi(*attrs++)]);
  178. ncurses_write_utf32(*chars++);
  179. }
  180. }
  181. refresh();
  182. }
  183. static void ncurses_handle_resize(caca_display_t *dp)
  184. {
  185. struct winsize size;
  186. #if defined HAVE_SYS_IOCTL_H
  187. if(ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0)
  188. {
  189. dp->resize.w = size.ws_col;
  190. dp->resize.h = size.ws_row;
  191. #if defined HAVE_RESIZE_TERM
  192. resize_term(dp->resize.h, dp->resize.w);
  193. #else
  194. resizeterm(*dp->resize.h, *dp->resize.w);
  195. #endif
  196. wrefresh(curscr);
  197. return;
  198. }
  199. #endif
  200. /* Fallback */
  201. dp->resize.w = dp->cv->width;
  202. dp->resize.h = dp->cv->height;
  203. }
  204. static int ncurses_get_event(caca_display_t *dp, caca_event_t *ev)
  205. {
  206. int intkey;
  207. intkey = getch();
  208. if(intkey == ERR)
  209. {
  210. ev->type = CACA_EVENT_NONE;
  211. return 0;
  212. }
  213. if(intkey < 0x7f)
  214. {
  215. ev->type = CACA_EVENT_KEY_PRESS;
  216. ev->data.key.ch = intkey;
  217. ev->data.key.utf32 = intkey;
  218. ev->data.key.utf8[0] = intkey;
  219. ev->data.key.utf8[1] = '\0';
  220. return 1;
  221. }
  222. /* If the key was UTF-8, parse the whole sequence */
  223. if(intkey >= 0x80 && intkey < 0x100)
  224. {
  225. int keys[7]; /* Necessary for ungetch(); */
  226. char utf8[7];
  227. uint32_t utf32;
  228. unsigned int i, bytes = 0;
  229. keys[0] = intkey;
  230. utf8[0] = intkey;
  231. for(i = 1; i < 6; i++)
  232. {
  233. keys[i] = getch();
  234. utf8[i] = (unsigned char)keys[i];
  235. }
  236. utf8[i] = '\0';
  237. utf32 = cucul_utf8_to_utf32(utf8, &bytes);
  238. while(i > bytes)
  239. ungetch(keys[--i]);
  240. if(bytes)
  241. {
  242. ev->type = CACA_EVENT_KEY_PRESS;
  243. ev->data.key.ch = 0;
  244. ev->data.key.utf32 = utf32;
  245. strcpy(ev->data.key.utf8, utf8);
  246. return 1;
  247. }
  248. }
  249. if(intkey == KEY_MOUSE)
  250. {
  251. MEVENT mevent;
  252. getmouse(&mevent);
  253. switch(mevent.bstate)
  254. {
  255. #define PRESS(x) ev->data.mouse.button = x; \
  256. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev)
  257. #define RELEASE(x) ev->data.mouse.button = x; \
  258. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev)
  259. #define CLICK(x) PRESS(x); RELEASE(x)
  260. case BUTTON1_PRESSED: PRESS(1); break;
  261. case BUTTON1_RELEASED: RELEASE(1); break;
  262. case BUTTON1_CLICKED: CLICK(1); break;
  263. case BUTTON1_DOUBLE_CLICKED: CLICK(1); CLICK(1); break;
  264. case BUTTON1_TRIPLE_CLICKED: CLICK(1); CLICK(1); CLICK(1); break;
  265. case BUTTON1_RESERVED_EVENT: break;
  266. case BUTTON2_PRESSED: PRESS(2); break;
  267. case BUTTON2_RELEASED: RELEASE(2); break;
  268. case BUTTON2_CLICKED: CLICK(2); break;
  269. case BUTTON2_DOUBLE_CLICKED: CLICK(2); CLICK(2); break;
  270. case BUTTON2_TRIPLE_CLICKED: CLICK(2); CLICK(2); CLICK(2); break;
  271. case BUTTON2_RESERVED_EVENT: break;
  272. case BUTTON3_PRESSED: PRESS(3); break;
  273. case BUTTON3_RELEASED: RELEASE(3); break;
  274. case BUTTON3_CLICKED: CLICK(3); break;
  275. case BUTTON3_DOUBLE_CLICKED: CLICK(3); CLICK(3); break;
  276. case BUTTON3_TRIPLE_CLICKED: CLICK(3); CLICK(3); CLICK(3); break;
  277. case BUTTON3_RESERVED_EVENT: break;
  278. case BUTTON4_PRESSED: PRESS(4); break;
  279. case BUTTON4_RELEASED: RELEASE(4); break;
  280. case BUTTON4_CLICKED: CLICK(4); break;
  281. case BUTTON4_DOUBLE_CLICKED: CLICK(4); CLICK(4); break;
  282. case BUTTON4_TRIPLE_CLICKED: CLICK(4); CLICK(4); CLICK(4); break;
  283. case BUTTON4_RESERVED_EVENT: break;
  284. default:
  285. break;
  286. #undef PRESS
  287. #undef RELEASE
  288. #undef CLICK
  289. }
  290. if(dp->mouse.x == (unsigned int)mevent.x &&
  291. dp->mouse.y == (unsigned int)mevent.y)
  292. return _pop_event(dp, ev);
  293. dp->mouse.x = mevent.x;
  294. dp->mouse.y = mevent.y;
  295. ev->type = CACA_EVENT_MOUSE_MOTION;
  296. ev->data.mouse.x = dp->mouse.x;
  297. ev->data.mouse.y = dp->mouse.y;
  298. return 1;
  299. }
  300. switch(intkey)
  301. {
  302. case KEY_UP: ev->data.key.ch = CACA_KEY_UP; break;
  303. case KEY_DOWN: ev->data.key.ch = CACA_KEY_DOWN; break;
  304. case KEY_LEFT: ev->data.key.ch = CACA_KEY_LEFT; break;
  305. case KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break;
  306. case KEY_IC: ev->data.key.ch = CACA_KEY_INSERT; break;
  307. case KEY_DC: ev->data.key.ch = CACA_KEY_DELETE; break;
  308. case 0x7f:
  309. case KEY_BACKSPACE: ev->data.key.ch = CACA_KEY_BACKSPACE; break;
  310. case KEY_HOME: ev->data.key.ch = CACA_KEY_HOME; break;
  311. case KEY_END: ev->data.key.ch = CACA_KEY_END; break;
  312. case KEY_PPAGE: ev->data.key.ch = CACA_KEY_PAGEUP; break;
  313. case KEY_NPAGE: ev->data.key.ch = CACA_KEY_PAGEDOWN; break;
  314. case KEY_F(1): ev->data.key.ch = CACA_KEY_F1; break;
  315. case KEY_F(2): ev->data.key.ch = CACA_KEY_F2; break;
  316. case KEY_F(3): ev->data.key.ch = CACA_KEY_F3; break;
  317. case KEY_F(4): ev->data.key.ch = CACA_KEY_F4; break;
  318. case KEY_F(5): ev->data.key.ch = CACA_KEY_F5; break;
  319. case KEY_F(6): ev->data.key.ch = CACA_KEY_F6; break;
  320. case KEY_F(7): ev->data.key.ch = CACA_KEY_F7; break;
  321. case KEY_F(8): ev->data.key.ch = CACA_KEY_F8; break;
  322. case KEY_F(9): ev->data.key.ch = CACA_KEY_F9; break;
  323. case KEY_F(10): ev->data.key.ch = CACA_KEY_F10; break;
  324. case KEY_F(11): ev->data.key.ch = CACA_KEY_F11; break;
  325. case KEY_F(12): ev->data.key.ch = CACA_KEY_F12; break;
  326. default:
  327. /* Unknown key */
  328. ev->type = CACA_EVENT_NONE; return 0;
  329. }
  330. ev->type = CACA_EVENT_KEY_PRESS;
  331. ev->data.key.utf32 = 0;
  332. ev->data.key.utf8[0] = '\0';
  333. return 1;
  334. }
  335. /*
  336. * XXX: following functions are local
  337. */
  338. #if defined HAVE_SIGNAL
  339. static RETSIGTYPE sigwinch_handler(int sig)
  340. {
  341. sigwinch_d->resize.resized = 1;
  342. signal(SIGWINCH, sigwinch_handler);
  343. }
  344. #endif
  345. #if defined HAVE_GETENV && defined HAVE_PUTENV
  346. static void ncurses_check_terminal(void)
  347. {
  348. char *term, *colorterm;
  349. term = getenv("TERM");
  350. colorterm = getenv("COLORTERM");
  351. if(!term || strcmp(term, "xterm"))
  352. return;
  353. /* If we are using gnome-terminal, it's really a 16 colour terminal.
  354. * Ditto if we are using xfce4-terminal, or Konsole. */
  355. if((colorterm && (!strcmp(colorterm, "gnome-terminal")
  356. || !strcmp(colorterm, "Terminal")))
  357. || getenv("KONSOLE_DCOP_SESSION"))
  358. {
  359. SCREEN *screen;
  360. screen = newterm("xterm-16color", stdout, stdin);
  361. if(screen == NULL)
  362. return;
  363. endwin();
  364. (void)putenv("TERM=xterm-16color");
  365. return;
  366. }
  367. }
  368. #endif
  369. static void ncurses_write_utf32(uint32_t ch)
  370. {
  371. #if defined HAVE_NCURSESW_NCURSES_H
  372. char buf[10];
  373. int bytes;
  374. #endif
  375. if(ch == CUCUL_MAGIC_FULLWIDTH)
  376. return;
  377. #if defined HAVE_NCURSESW_NCURSES_H
  378. bytes = cucul_utf32_to_utf8(buf, ch);
  379. buf[bytes] = '\0';
  380. addstr(buf);
  381. #else
  382. if(ch < 0x80)
  383. addch(ch)
  384. else if(cucul_utf32_is_fullwidth(ch))
  385. addstr("? ");
  386. else
  387. addch('?');
  388. #endif
  389. }
  390. /*
  391. * Driver initialisation
  392. */
  393. int ncurses_install(caca_display_t *dp)
  394. {
  395. dp->drv.driver = CACA_DRIVER_NCURSES;
  396. dp->drv.init_graphics = ncurses_init_graphics;
  397. dp->drv.end_graphics = ncurses_end_graphics;
  398. dp->drv.set_display_title = ncurses_set_display_title;
  399. dp->drv.get_display_width = ncurses_get_display_width;
  400. dp->drv.get_display_height = ncurses_get_display_height;
  401. dp->drv.display = ncurses_display;
  402. dp->drv.handle_resize = ncurses_handle_resize;
  403. dp->drv.get_event = ncurses_get_event;
  404. dp->drv.set_mouse = NULL;
  405. dp->drv.set_cursor = NULL;
  406. return 0;
  407. }
  408. #endif /* USE_NCURSES */