No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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