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

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