Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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