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.
 
 
 
 
 
 

476 lines
14 KiB

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