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 13 KiB

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