You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

257 lines
6.2 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 graphics.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Character drawing
  15. *
  16. * This file contains character and string drawing functions.
  17. */
  18. #include "config.h"
  19. #if defined(HAVE_INTTYPES_H) || defined(_DOXYGEN_SKIP_ME)
  20. # include <inttypes.h>
  21. #else
  22. typedef unsigned int uint32_t;
  23. typedef unsigned char uint8_t;
  24. #endif
  25. #if defined(USE_NCURSES)
  26. #if defined(HAVE_NCURSES_H)
  27. # include <ncurses.h>
  28. #else
  29. # include <curses.h>
  30. #endif
  31. #include <stdio.h> /* BUFSIZ */
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #if defined(HAVE_UNISTD_H)
  35. # include <unistd.h>
  36. #endif
  37. #include <stdarg.h>
  38. #if defined(HAVE_SIGNAL_H)
  39. # include <signal.h>
  40. #endif
  41. #if defined(HAVE_SYS_IOCTL_H)
  42. # include <sys/ioctl.h>
  43. #endif
  44. #include "caca.h"
  45. #include "caca_internals.h"
  46. #include "cucul.h"
  47. #include "cucul_internals.h"
  48. int ncurses_init_graphics(caca_t *);
  49. int ncurses_end_graphics(caca_t *);
  50. int ncurses_set_window_title(caca_t *, char const *);
  51. unsigned int ncurses_get_window_width(caca_t *);
  52. unsigned int ncurses_get_window_height(caca_t *);
  53. void ncurses_display(caca_t *);
  54. void ncurses_handle_resize(caca_t *);
  55. /*
  56. * Local functions
  57. */
  58. #if defined(HAVE_SIGNAL)
  59. static RETSIGTYPE sigwinch_handler(int);
  60. static caca_t *sigwinch_kk; /* FIXME: we ought to get rid of this */
  61. #endif
  62. int ncurses_init_graphics(caca_t *kk)
  63. {
  64. static int curses_colors[] =
  65. {
  66. /* Standard curses colours */
  67. COLOR_BLACK,
  68. COLOR_BLUE,
  69. COLOR_GREEN,
  70. COLOR_CYAN,
  71. COLOR_RED,
  72. COLOR_MAGENTA,
  73. COLOR_YELLOW,
  74. COLOR_WHITE,
  75. /* Extra values for xterm-16color */
  76. COLOR_BLACK + 8,
  77. COLOR_BLUE + 8,
  78. COLOR_GREEN + 8,
  79. COLOR_CYAN + 8,
  80. COLOR_RED + 8,
  81. COLOR_MAGENTA + 8,
  82. COLOR_YELLOW + 8,
  83. COLOR_WHITE + 8
  84. };
  85. mmask_t newmask;
  86. int fg, bg, max;
  87. #if defined(HAVE_SIGNAL)
  88. sigwinch_kk = kk;
  89. signal(SIGWINCH, sigwinch_handler);
  90. #endif
  91. initscr();
  92. keypad(stdscr, TRUE);
  93. nonl();
  94. raw();
  95. noecho();
  96. nodelay(stdscr, TRUE);
  97. curs_set(0);
  98. /* Activate mouse */
  99. newmask = REPORT_MOUSE_POSITION | ALL_MOUSE_EVENTS;
  100. mousemask(newmask, &kk->ncurses.oldmask);
  101. mouseinterval(-1); /* No click emulation */
  102. /* Set the escape delay to a ridiculously low value */
  103. ESCDELAY = 10;
  104. /* Activate colour */
  105. start_color();
  106. /* If COLORS == 16, it means the terminal supports full bright colours
  107. * using setab and setaf (will use \e[90m \e[91m etc. for colours >= 8),
  108. * we can build 16*16 colour pairs.
  109. * If COLORS == 8, it means the terminal does not know about bright
  110. * colours and we need to get them through A_BOLD and A_BLINK (\e[1m
  111. * and \e[5m). We can only build 8*8 colour pairs. */
  112. max = COLORS >= 16 ? 16 : 8;
  113. for(bg = 0; bg < max; bg++)
  114. for(fg = 0; fg < max; fg++)
  115. {
  116. /* Use ((max + 7 - fg) % max) instead of fg so that colour 0
  117. * is light gray on black. Some terminals don't like this
  118. * colour pair to be redefined. */
  119. int col = ((max + 7 - fg) % max) + max * bg;
  120. init_pair(col, curses_colors[fg], curses_colors[bg]);
  121. kk->ncurses.attr[fg + 16 * bg] = COLOR_PAIR(col);
  122. if(max == 8)
  123. {
  124. /* Bright fg on simple bg */
  125. kk->ncurses.attr[fg + 8 + 16 * bg] = A_BOLD | COLOR_PAIR(col);
  126. /* Simple fg on bright bg */
  127. kk->ncurses.attr[fg + 16 * (bg + 8)] = A_BLINK
  128. | COLOR_PAIR(col);
  129. /* Bright fg on bright bg */
  130. kk->ncurses.attr[fg + 8 + 16 * (bg + 8)] = A_BLINK | A_BOLD
  131. | COLOR_PAIR(col);
  132. }
  133. }
  134. cucul_set_size(kk->qq, COLS, LINES);
  135. return 0;
  136. }
  137. int ncurses_end_graphics(caca_t *kk)
  138. {
  139. mousemask(kk->ncurses.oldmask, NULL);
  140. curs_set(1);
  141. noraw();
  142. endwin();
  143. return 0;
  144. }
  145. int ncurses_set_window_title(caca_t *kk, char const *title)
  146. {
  147. return 0;
  148. }
  149. unsigned int ncurses_get_window_width(caca_t *kk)
  150. {
  151. /* Fallback to a 6x10 font */
  152. return kk->qq->width * 6;
  153. }
  154. unsigned int ncurses_get_window_height(caca_t *kk)
  155. {
  156. /* Fallback to a 6x10 font */
  157. return kk->qq->height * 10;
  158. }
  159. 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->ncurses.attr[*attr++]);
  170. addch(*chars++ & 0x7f);
  171. }
  172. }
  173. refresh();
  174. }
  175. void ncurses_handle_resize(caca_t *kk)
  176. {
  177. unsigned int new_width, new_height;
  178. struct winsize size;
  179. new_width = kk->qq->width;
  180. new_height = kk->qq->height;
  181. if(ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0)
  182. {
  183. new_width = size.ws_col;
  184. new_height = size.ws_row;
  185. #if defined(HAVE_RESIZE_TERM)
  186. resize_term(new_height, new_width);
  187. #else
  188. resizeterm(new_height, new_width);
  189. #endif
  190. wrefresh(curscr);
  191. }
  192. }
  193. /*
  194. * XXX: following functions are local
  195. */
  196. #if defined(HAVE_SIGNAL)
  197. static RETSIGTYPE sigwinch_handler(int sig)
  198. {
  199. sigwinch_kk->resize_event = 1;
  200. signal(SIGWINCH, sigwinch_handler);;
  201. }
  202. #endif
  203. /*
  204. * Driver initialisation
  205. */
  206. void ncurses_init_driver(caca_t *kk)
  207. {
  208. kk->driver.driver = CACA_DRIVER_NCURSES;
  209. kk->driver.init_graphics = ncurses_init_graphics;
  210. kk->driver.end_graphics = ncurses_end_graphics;
  211. kk->driver.set_window_title = ncurses_set_window_title;
  212. kk->driver.get_window_width = ncurses_get_window_width;
  213. kk->driver.get_window_height = ncurses_get_window_height;
  214. kk->driver.display = ncurses_display;
  215. kk->driver.handle_resize = ncurses_handle_resize;
  216. }
  217. #endif /* USE_NCURSES */