Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

477 righe
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. attrset(kk->drv.p->attr[*attr++]);
  165. addch(*chars++ & 0x7f);
  166. }
  167. }
  168. refresh();
  169. }
  170. static void ncurses_handle_resize(caca_t *kk, unsigned int *new_width,
  171. unsigned int *new_height)
  172. {
  173. struct winsize size;
  174. *new_width = kk->qq->width;
  175. *new_height = kk->qq->height;
  176. if(ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0)
  177. {
  178. *new_width = size.ws_col;
  179. *new_height = size.ws_row;
  180. #if defined(HAVE_RESIZE_TERM)
  181. resize_term(*new_height, *new_width);
  182. #else
  183. resizeterm(*new_height, *new_width);
  184. #endif
  185. wrefresh(curscr);
  186. }
  187. }
  188. static unsigned int ncurses_get_event(caca_t *kk)
  189. {
  190. unsigned int event;
  191. int intkey;
  192. if(kk->resize_event)
  193. {
  194. kk->resize_event = 0;
  195. kk->resize = 1;
  196. return CACA_EVENT_RESIZE;
  197. }
  198. intkey = getch();
  199. if(intkey == ERR)
  200. return CACA_EVENT_NONE;
  201. if(intkey < 0x100)
  202. {
  203. return CACA_EVENT_KEY_PRESS | intkey;
  204. }
  205. if(intkey == KEY_MOUSE)
  206. {
  207. MEVENT mevent;
  208. getmouse(&mevent);
  209. switch(mevent.bstate)
  210. {
  211. case BUTTON1_PRESSED:
  212. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 1);
  213. break;
  214. case BUTTON1_RELEASED:
  215. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 1);
  216. break;
  217. case BUTTON1_CLICKED:
  218. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 1);
  219. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 1);
  220. break;
  221. case BUTTON1_DOUBLE_CLICKED:
  222. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 1);
  223. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 1);
  224. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 1);
  225. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 1);
  226. break;
  227. case BUTTON1_TRIPLE_CLICKED:
  228. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 1);
  229. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 1);
  230. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 1);
  231. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 1);
  232. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 1);
  233. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 1);
  234. break;
  235. case BUTTON1_RESERVED_EVENT:
  236. break;
  237. case BUTTON2_PRESSED:
  238. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 2);
  239. break;
  240. case BUTTON2_RELEASED:
  241. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 2);
  242. break;
  243. case BUTTON2_CLICKED:
  244. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 2);
  245. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 2);
  246. break;
  247. case BUTTON2_DOUBLE_CLICKED:
  248. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 2);
  249. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 2);
  250. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 2);
  251. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 2);
  252. break;
  253. case BUTTON2_TRIPLE_CLICKED:
  254. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 2);
  255. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 2);
  256. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 2);
  257. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 2);
  258. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 2);
  259. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 2);
  260. break;
  261. case BUTTON2_RESERVED_EVENT:
  262. break;
  263. case BUTTON3_PRESSED:
  264. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 3);
  265. break;
  266. case BUTTON3_RELEASED:
  267. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 3);
  268. break;
  269. case BUTTON3_CLICKED:
  270. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 3);
  271. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 3);
  272. break;
  273. case BUTTON3_DOUBLE_CLICKED:
  274. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 3);
  275. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 3);
  276. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 3);
  277. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 3);
  278. break;
  279. case BUTTON3_TRIPLE_CLICKED:
  280. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 3);
  281. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 3);
  282. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 3);
  283. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 3);
  284. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 3);
  285. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 3);
  286. break;
  287. case BUTTON3_RESERVED_EVENT:
  288. break;
  289. case BUTTON4_PRESSED:
  290. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 4);
  291. break;
  292. case BUTTON4_RELEASED:
  293. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 4);
  294. break;
  295. case BUTTON4_CLICKED:
  296. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 4);
  297. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 4);
  298. break;
  299. case BUTTON4_DOUBLE_CLICKED:
  300. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 4);
  301. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 4);
  302. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 4);
  303. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 4);
  304. break;
  305. case BUTTON4_TRIPLE_CLICKED:
  306. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 4);
  307. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 4);
  308. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 4);
  309. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 4);
  310. _push_event(kk, CACA_EVENT_MOUSE_PRESS | 4);
  311. _push_event(kk, CACA_EVENT_MOUSE_RELEASE | 4);
  312. break;
  313. case BUTTON4_RESERVED_EVENT:
  314. break;
  315. default:
  316. break;
  317. }
  318. if(kk->mouse.x == (unsigned int)mevent.x &&
  319. kk->mouse.y == (unsigned int)mevent.y)
  320. return _pop_event(kk);
  321. kk->mouse.x = mevent.x;
  322. kk->mouse.y = mevent.y;
  323. return CACA_EVENT_MOUSE_MOTION | (kk->mouse.x << 12) | kk->mouse.y;
  324. }
  325. event = CACA_EVENT_KEY_PRESS;
  326. switch(intkey)
  327. {
  328. case KEY_UP: return event | CACA_KEY_UP;
  329. case KEY_DOWN: return event | CACA_KEY_DOWN;
  330. case KEY_LEFT: return event | CACA_KEY_LEFT;
  331. case KEY_RIGHT: return event | CACA_KEY_RIGHT;
  332. case KEY_IC: return event | CACA_KEY_INSERT;
  333. case KEY_DC: return event | CACA_KEY_DELETE;
  334. case KEY_HOME: return event | CACA_KEY_HOME;
  335. case KEY_END: return event | CACA_KEY_END;
  336. case KEY_PPAGE: return event | CACA_KEY_PAGEUP;
  337. case KEY_NPAGE: return event | CACA_KEY_PAGEDOWN;
  338. case KEY_F(1): return event | CACA_KEY_F1;
  339. case KEY_F(2): return event | CACA_KEY_F2;
  340. case KEY_F(3): return event | CACA_KEY_F3;
  341. case KEY_F(4): return event | CACA_KEY_F4;
  342. case KEY_F(5): return event | CACA_KEY_F5;
  343. case KEY_F(6): return event | CACA_KEY_F6;
  344. case KEY_F(7): return event | CACA_KEY_F7;
  345. case KEY_F(8): return event | CACA_KEY_F8;
  346. case KEY_F(9): return event | CACA_KEY_F9;
  347. case KEY_F(10): return event | CACA_KEY_F10;
  348. case KEY_F(11): return event | CACA_KEY_F11;
  349. case KEY_F(12): return event | CACA_KEY_F12;
  350. }
  351. return CACA_EVENT_NONE;
  352. }
  353. /*
  354. * XXX: following functions are local
  355. */
  356. #if defined(HAVE_SIGNAL)
  357. static RETSIGTYPE sigwinch_handler(int sig)
  358. {
  359. sigwinch_kk->resize_event = 1;
  360. signal(SIGWINCH, sigwinch_handler);;
  361. }
  362. #endif
  363. #if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
  364. static void ncurses_check_terminal(void)
  365. {
  366. char *term, *colorterm, *other;
  367. term = getenv("TERM");
  368. colorterm = getenv("COLORTERM");
  369. if(term && !strcmp(term, "xterm"))
  370. {
  371. /* If we are using gnome-terminal, it's really a 16 colour terminal */
  372. if(colorterm && !strcmp(colorterm, "gnome-terminal"))
  373. {
  374. SCREEN *screen;
  375. screen = newterm("xterm-16color", stdout, stdin);
  376. if(screen == NULL)
  377. return;
  378. endwin();
  379. (void)putenv("TERM=xterm-16color");
  380. return;
  381. }
  382. /* Ditto if we are using Konsole */
  383. other = getenv("KONSOLE_DCOP_SESSION");
  384. if(other)
  385. {
  386. SCREEN *screen;
  387. screen = newterm("xterm-16color", stdout, stdin);
  388. if(screen == NULL)
  389. return;
  390. endwin();
  391. (void)putenv("TERM=xterm-16color");
  392. return;
  393. }
  394. }
  395. }
  396. #endif
  397. /*
  398. * Driver initialisation
  399. */
  400. void ncurses_init_driver(caca_t *kk)
  401. {
  402. kk->drv.driver = CACA_DRIVER_NCURSES;
  403. kk->drv.init_graphics = ncurses_init_graphics;
  404. kk->drv.end_graphics = ncurses_end_graphics;
  405. kk->drv.set_window_title = ncurses_set_window_title;
  406. kk->drv.get_window_width = ncurses_get_window_width;
  407. kk->drv.get_window_height = ncurses_get_window_height;
  408. kk->drv.display = ncurses_display;
  409. kk->drv.handle_resize = ncurses_handle_resize;
  410. kk->drv.get_event = ncurses_get_event;
  411. }
  412. #endif /* USE_NCURSES */