25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

driver_ncurses.c 15 KiB

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