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ů.
 
 
 
 
 
 

552 řádky
17 KiB

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