Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

553 rindas
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. * $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 < 0x100)
  205. {
  206. ev->type = CACA_EVENT_KEY_PRESS;
  207. ev->data.key.ch = intkey;
  208. return 1;
  209. }
  210. if(intkey == KEY_MOUSE)
  211. {
  212. MEVENT mevent;
  213. getmouse(&mevent);
  214. switch(mevent.bstate)
  215. {
  216. case BUTTON1_PRESSED:
  217. ev->data.mouse.button = 1;
  218. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  219. break;
  220. case BUTTON1_RELEASED:
  221. ev->data.mouse.button = 1;
  222. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  223. break;
  224. case BUTTON1_CLICKED:
  225. ev->data.mouse.button = 1;
  226. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  227. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  228. break;
  229. case BUTTON1_DOUBLE_CLICKED:
  230. ev->data.mouse.button = 1;
  231. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  232. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  233. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  234. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  235. break;
  236. case BUTTON1_TRIPLE_CLICKED:
  237. ev->data.mouse.button = 1;
  238. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  239. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  240. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  241. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  242. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  243. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  244. break;
  245. case BUTTON1_RESERVED_EVENT:
  246. break;
  247. case BUTTON2_PRESSED:
  248. ev->data.mouse.button = 2;
  249. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  250. break;
  251. case BUTTON2_RELEASED:
  252. ev->data.mouse.button = 2;
  253. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  254. break;
  255. case BUTTON2_CLICKED:
  256. ev->data.mouse.button = 2;
  257. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  258. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  259. break;
  260. case BUTTON2_DOUBLE_CLICKED:
  261. ev->data.mouse.button = 2;
  262. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  263. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  264. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  265. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  266. break;
  267. case BUTTON2_TRIPLE_CLICKED:
  268. ev->data.mouse.button = 2;
  269. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  270. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  271. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  272. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  273. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  274. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  275. break;
  276. case BUTTON2_RESERVED_EVENT:
  277. break;
  278. case BUTTON3_PRESSED:
  279. ev->data.mouse.button = 3;
  280. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  281. break;
  282. case BUTTON3_RELEASED:
  283. ev->data.mouse.button = 3;
  284. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  285. break;
  286. case BUTTON3_CLICKED:
  287. ev->data.mouse.button = 3;
  288. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  289. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  290. break;
  291. case BUTTON3_DOUBLE_CLICKED:
  292. ev->data.mouse.button = 3;
  293. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  294. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  295. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  296. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  297. break;
  298. case BUTTON3_TRIPLE_CLICKED:
  299. ev->data.mouse.button = 3;
  300. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  301. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  302. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  303. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  304. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  305. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  306. break;
  307. case BUTTON3_RESERVED_EVENT:
  308. break;
  309. case BUTTON4_PRESSED:
  310. ev->data.mouse.button = 4;
  311. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  312. break;
  313. case BUTTON4_RELEASED:
  314. ev->data.mouse.button = 4;
  315. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  316. break;
  317. case BUTTON4_CLICKED:
  318. ev->data.mouse.button = 4;
  319. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  320. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  321. break;
  322. case BUTTON4_DOUBLE_CLICKED:
  323. ev->data.mouse.button = 4;
  324. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  325. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  326. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  327. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  328. break;
  329. case BUTTON4_TRIPLE_CLICKED:
  330. ev->data.mouse.button = 4;
  331. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  332. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  333. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  334. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  335. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev);
  336. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev);
  337. break;
  338. case BUTTON4_RESERVED_EVENT:
  339. break;
  340. default:
  341. break;
  342. }
  343. if(dp->mouse.x == (unsigned int)mevent.x &&
  344. dp->mouse.y == (unsigned int)mevent.y)
  345. return _pop_event(dp, ev);
  346. dp->mouse.x = mevent.x;
  347. dp->mouse.y = mevent.y;
  348. ev->type = CACA_EVENT_MOUSE_MOTION;
  349. ev->data.mouse.x = dp->mouse.x;
  350. ev->data.mouse.y = dp->mouse.y;
  351. return 1;
  352. }
  353. switch(intkey)
  354. {
  355. case KEY_UP: ev->data.key.ch = CACA_KEY_UP; break;
  356. case KEY_DOWN: ev->data.key.ch = CACA_KEY_DOWN; break;
  357. case KEY_LEFT: ev->data.key.ch = CACA_KEY_LEFT; break;
  358. case KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break;
  359. case KEY_IC: ev->data.key.ch = CACA_KEY_INSERT; break;
  360. case KEY_DC: ev->data.key.ch = CACA_KEY_DELETE; break;
  361. case KEY_HOME: ev->data.key.ch = CACA_KEY_HOME; break;
  362. case KEY_END: ev->data.key.ch = CACA_KEY_END; break;
  363. case KEY_PPAGE: ev->data.key.ch = CACA_KEY_PAGEUP; break;
  364. case KEY_NPAGE: ev->data.key.ch = CACA_KEY_PAGEDOWN; break;
  365. case KEY_F(1): ev->data.key.ch = CACA_KEY_F1; break;
  366. case KEY_F(2): ev->data.key.ch = CACA_KEY_F2; break;
  367. case KEY_F(3): ev->data.key.ch = CACA_KEY_F3; break;
  368. case KEY_F(4): ev->data.key.ch = CACA_KEY_F4; break;
  369. case KEY_F(5): ev->data.key.ch = CACA_KEY_F5; break;
  370. case KEY_F(6): ev->data.key.ch = CACA_KEY_F6; break;
  371. case KEY_F(7): ev->data.key.ch = CACA_KEY_F7; break;
  372. case KEY_F(8): ev->data.key.ch = CACA_KEY_F8; break;
  373. case KEY_F(9): ev->data.key.ch = CACA_KEY_F9; break;
  374. case KEY_F(10): ev->data.key.ch = CACA_KEY_F10; break;
  375. case KEY_F(11): ev->data.key.ch = CACA_KEY_F11; break;
  376. case KEY_F(12): ev->data.key.ch = CACA_KEY_F12; break;
  377. default: ev->type = CACA_EVENT_NONE; return 0;
  378. }
  379. ev->type = CACA_EVENT_KEY_PRESS;
  380. ev->data.key.ucs4 = 0;
  381. ev->data.key.utf8[0] = '\0';
  382. return 1;
  383. }
  384. /*
  385. * XXX: following functions are local
  386. */
  387. #if defined(HAVE_SIGNAL)
  388. static RETSIGTYPE sigwinch_handler(int sig)
  389. {
  390. sigwinch_d->resize.resized = 1;
  391. signal(SIGWINCH, sigwinch_handler);
  392. }
  393. #endif
  394. #if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
  395. static void ncurses_check_terminal(void)
  396. {
  397. char *term, *colorterm, *other;
  398. term = getenv("TERM");
  399. colorterm = getenv("COLORTERM");
  400. if(term && !strcmp(term, "xterm"))
  401. {
  402. /* If we are using gnome-terminal, it's really a 16 colour terminal */
  403. if(colorterm && !strcmp(colorterm, "gnome-terminal"))
  404. {
  405. SCREEN *screen;
  406. screen = newterm("xterm-16color", stdout, stdin);
  407. if(screen == NULL)
  408. return;
  409. endwin();
  410. (void)putenv("TERM=xterm-16color");
  411. return;
  412. }
  413. /* Ditto if we are using Konsole */
  414. other = getenv("KONSOLE_DCOP_SESSION");
  415. if(other)
  416. {
  417. SCREEN *screen;
  418. screen = newterm("xterm-16color", stdout, stdin);
  419. if(screen == NULL)
  420. return;
  421. endwin();
  422. (void)putenv("TERM=xterm-16color");
  423. return;
  424. }
  425. }
  426. }
  427. #endif
  428. static void ncurses_write_utf32(uint32_t ch)
  429. {
  430. #if defined(HAVE_NCURSESW_NCURSES_H)
  431. static const uint8_t mark[7] =
  432. {
  433. 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
  434. };
  435. char buf[10], *parser;
  436. int bytes;
  437. #endif
  438. if(ch < 0x80)
  439. {
  440. addch(ch);
  441. return;
  442. }
  443. #if defined(HAVE_NCURSESW_NCURSES_H)
  444. if(ch < 0x10000)
  445. {
  446. addch(ch); /* FIXME: doesn't work either */
  447. return;
  448. }
  449. bytes = (ch < 0x800) ? 2 : (ch < 0x10000) ? 3 : 4;
  450. buf[bytes] = '\0';
  451. parser = buf + bytes;
  452. switch(bytes)
  453. {
  454. case 4: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
  455. case 3: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
  456. case 2: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
  457. }
  458. *--parser = ch | mark[bytes];
  459. addstr(buf);
  460. #else
  461. addch(' ');
  462. #endif
  463. }
  464. /*
  465. * Driver initialisation
  466. */
  467. int ncurses_install(caca_display_t *dp)
  468. {
  469. dp->drv.driver = CACA_DRIVER_NCURSES;
  470. dp->drv.init_graphics = ncurses_init_graphics;
  471. dp->drv.end_graphics = ncurses_end_graphics;
  472. dp->drv.set_display_title = ncurses_set_display_title;
  473. dp->drv.get_display_width = ncurses_get_display_width;
  474. dp->drv.get_display_height = ncurses_get_display_height;
  475. dp->drv.display = ncurses_display;
  476. dp->drv.handle_resize = ncurses_handle_resize;
  477. dp->drv.get_event = ncurses_get_event;
  478. dp->drv.set_mouse = NULL;
  479. return 0;
  480. }
  481. #endif /* USE_NCURSES */