Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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