Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

driver_ncurses.c 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 < 0x7f)
  205. {
  206. ev->type = CACA_EVENT_KEY_PRESS;
  207. ev->data.key.ch = intkey;
  208. ev->data.key.utf32 = intkey;
  209. ev->data.key.utf8[0] = intkey;
  210. ev->data.key.utf8[1] = '\0';
  211. return 1;
  212. }
  213. if(intkey == KEY_MOUSE)
  214. {
  215. MEVENT mevent;
  216. getmouse(&mevent);
  217. switch(mevent.bstate)
  218. {
  219. #define PRESS(x) ev->data.mouse.button = x; \
  220. ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev)
  221. #define RELEASE(x) ev->data.mouse.button = x; \
  222. ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev)
  223. #define CLICK(x) PRESS(x); RELEASE(x)
  224. case BUTTON1_PRESSED: PRESS(1); break;
  225. case BUTTON1_RELEASED: RELEASE(1); break;
  226. case BUTTON1_CLICKED: CLICK(1); break;
  227. case BUTTON1_DOUBLE_CLICKED: CLICK(1); CLICK(1); break;
  228. case BUTTON1_TRIPLE_CLICKED: CLICK(1); CLICK(1); CLICK(1); break;
  229. case BUTTON1_RESERVED_EVENT: break;
  230. case BUTTON2_PRESSED: PRESS(2); break;
  231. case BUTTON2_RELEASED: RELEASE(2); break;
  232. case BUTTON2_CLICKED: CLICK(2); break;
  233. case BUTTON2_DOUBLE_CLICKED: CLICK(2); CLICK(2); break;
  234. case BUTTON2_TRIPLE_CLICKED: CLICK(2); CLICK(2); CLICK(2); break;
  235. case BUTTON2_RESERVED_EVENT: break;
  236. case BUTTON3_PRESSED: PRESS(3); break;
  237. case BUTTON3_RELEASED: RELEASE(3); break;
  238. case BUTTON3_CLICKED: CLICK(3); break;
  239. case BUTTON3_DOUBLE_CLICKED: CLICK(3); CLICK(3); break;
  240. case BUTTON3_TRIPLE_CLICKED: CLICK(3); CLICK(3); CLICK(3); break;
  241. case BUTTON3_RESERVED_EVENT: break;
  242. case BUTTON4_PRESSED: PRESS(4); break;
  243. case BUTTON4_RELEASED: RELEASE(4); break;
  244. case BUTTON4_CLICKED: CLICK(4); break;
  245. case BUTTON4_DOUBLE_CLICKED: CLICK(4); CLICK(4); break;
  246. case BUTTON4_TRIPLE_CLICKED: CLICK(4); CLICK(4); CLICK(4); break;
  247. case BUTTON4_RESERVED_EVENT: break;
  248. default:
  249. break;
  250. #undef PRESS
  251. #undef RELEASE
  252. #undef CLICK
  253. }
  254. if(dp->mouse.x == (unsigned int)mevent.x &&
  255. dp->mouse.y == (unsigned int)mevent.y)
  256. return _pop_event(dp, ev);
  257. dp->mouse.x = mevent.x;
  258. dp->mouse.y = mevent.y;
  259. ev->type = CACA_EVENT_MOUSE_MOTION;
  260. ev->data.mouse.x = dp->mouse.x;
  261. ev->data.mouse.y = dp->mouse.y;
  262. return 1;
  263. }
  264. switch(intkey)
  265. {
  266. case KEY_UP: ev->data.key.ch = CACA_KEY_UP; break;
  267. case KEY_DOWN: ev->data.key.ch = CACA_KEY_DOWN; break;
  268. case KEY_LEFT: ev->data.key.ch = CACA_KEY_LEFT; break;
  269. case KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break;
  270. case KEY_IC: ev->data.key.ch = CACA_KEY_INSERT; break;
  271. case KEY_DC: ev->data.key.ch = CACA_KEY_DELETE; break;
  272. case 0x7f:
  273. case KEY_BACKSPACE: ev->data.key.ch = CACA_KEY_BACKSPACE; break;
  274. case KEY_HOME: ev->data.key.ch = CACA_KEY_HOME; break;
  275. case KEY_END: ev->data.key.ch = CACA_KEY_END; break;
  276. case KEY_PPAGE: ev->data.key.ch = CACA_KEY_PAGEUP; break;
  277. case KEY_NPAGE: ev->data.key.ch = CACA_KEY_PAGEDOWN; break;
  278. case KEY_F(1): ev->data.key.ch = CACA_KEY_F1; break;
  279. case KEY_F(2): ev->data.key.ch = CACA_KEY_F2; break;
  280. case KEY_F(3): ev->data.key.ch = CACA_KEY_F3; break;
  281. case KEY_F(4): ev->data.key.ch = CACA_KEY_F4; break;
  282. case KEY_F(5): ev->data.key.ch = CACA_KEY_F5; break;
  283. case KEY_F(6): ev->data.key.ch = CACA_KEY_F6; break;
  284. case KEY_F(7): ev->data.key.ch = CACA_KEY_F7; break;
  285. case KEY_F(8): ev->data.key.ch = CACA_KEY_F8; break;
  286. case KEY_F(9): ev->data.key.ch = CACA_KEY_F9; break;
  287. case KEY_F(10): ev->data.key.ch = CACA_KEY_F10; break;
  288. case KEY_F(11): ev->data.key.ch = CACA_KEY_F11; break;
  289. case KEY_F(12): ev->data.key.ch = CACA_KEY_F12; break;
  290. default:
  291. /* Unknown key */
  292. ev->type = CACA_EVENT_NONE; return 0;
  293. }
  294. ev->type = CACA_EVENT_KEY_PRESS;
  295. ev->data.key.utf32 = 0;
  296. ev->data.key.utf8[0] = '\0';
  297. return 1;
  298. }
  299. /*
  300. * XXX: following functions are local
  301. */
  302. #if defined(HAVE_SIGNAL)
  303. static RETSIGTYPE sigwinch_handler(int sig)
  304. {
  305. sigwinch_d->resize.resized = 1;
  306. signal(SIGWINCH, sigwinch_handler);
  307. }
  308. #endif
  309. #if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
  310. static void ncurses_check_terminal(void)
  311. {
  312. char *term, *colorterm, *other;
  313. term = getenv("TERM");
  314. colorterm = getenv("COLORTERM");
  315. if(term && !strcmp(term, "xterm"))
  316. {
  317. /* If we are using gnome-terminal, it's really a 16 colour terminal */
  318. if(colorterm && !strcmp(colorterm, "gnome-terminal"))
  319. {
  320. SCREEN *screen;
  321. screen = newterm("xterm-16color", stdout, stdin);
  322. if(screen == NULL)
  323. return;
  324. endwin();
  325. (void)putenv("TERM=xterm-16color");
  326. return;
  327. }
  328. /* Ditto if we are using Konsole */
  329. other = getenv("KONSOLE_DCOP_SESSION");
  330. if(other)
  331. {
  332. SCREEN *screen;
  333. screen = newterm("xterm-16color", stdout, stdin);
  334. if(screen == NULL)
  335. return;
  336. endwin();
  337. (void)putenv("TERM=xterm-16color");
  338. return;
  339. }
  340. }
  341. }
  342. #endif
  343. static void ncurses_write_utf32(uint32_t ch)
  344. {
  345. #if defined(HAVE_NCURSESW_NCURSES_H)
  346. char buf[10];
  347. int bytes;
  348. bytes = cucul_utf32_to_utf8(buf, ch);
  349. buf[bytes] = '\0';
  350. addstr(buf);
  351. #else
  352. addch(ch < 0x80 ? ch : '?');
  353. #endif
  354. }
  355. /*
  356. * Driver initialisation
  357. */
  358. int ncurses_install(caca_display_t *dp)
  359. {
  360. dp->drv.driver = CACA_DRIVER_NCURSES;
  361. dp->drv.init_graphics = ncurses_init_graphics;
  362. dp->drv.end_graphics = ncurses_end_graphics;
  363. dp->drv.set_display_title = ncurses_set_display_title;
  364. dp->drv.get_display_width = ncurses_get_display_width;
  365. dp->drv.get_display_height = ncurses_get_display_height;
  366. dp->drv.display = ncurses_display;
  367. dp->drv.handle_resize = ncurses_handle_resize;
  368. dp->drv.get_event = ncurses_get_event;
  369. dp->drv.set_mouse = NULL;
  370. return 0;
  371. }
  372. #endif /* USE_NCURSES */