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.
 
 
 
 
 
 

471 satır
14 KiB

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