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.
 
 
 
 
 

309 rivejä
6.9 KiB

  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "config.h"
  23. #if defined(USE_SLANG)
  24. # include <slang.h>
  25. #elif defined(USE_NCURSES)
  26. # include <curses.h>
  27. #elif defined(USE_CONIO)
  28. # include <dos.h>
  29. # include <conio.h>
  30. # if defined(SCREENUPDATE_IN_PC_H)
  31. # include <pc.h>
  32. # endif
  33. #else
  34. # error "no graphics library detected"
  35. #endif
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. #include <string.h>
  39. #include <sys/time.h>
  40. #include <time.h>
  41. #include "caca.h"
  42. #include "caca_internals.h"
  43. static unsigned int _caca_delay;
  44. static unsigned int _caca_rendertime;
  45. char *_caca_empty_line;
  46. char *_caca_scratch_line;
  47. #if defined(USE_NCURSES)
  48. int _caca_attr[16];
  49. #endif
  50. #if defined(USE_CONIO)
  51. static struct text_info ti;
  52. char *_caca_screen;
  53. #endif
  54. int caca_init(void)
  55. {
  56. #if defined(USE_SLANG)
  57. static char *slang_colors[16] =
  58. {
  59. "black",
  60. "blue",
  61. "green",
  62. "cyan",
  63. "red",
  64. "magenta",
  65. "brown",
  66. "lightgray",
  67. "gray",
  68. "brightblue",
  69. "brightgreen",
  70. "brightcyan",
  71. "brightred",
  72. "brightmagenta",
  73. "yellow",
  74. "white",
  75. };
  76. int i;
  77. /* Initialize slang library */
  78. SLsig_block_signals();
  79. SLtt_get_terminfo();
  80. if(SLkp_init() == -1)
  81. {
  82. SLsig_unblock_signals();
  83. return -1;
  84. }
  85. SLang_init_tty(-1, 0, 1);
  86. if(SLsmg_init_smg() == -1)
  87. {
  88. SLsig_unblock_signals();
  89. return -1;
  90. }
  91. SLsig_unblock_signals();
  92. SLsmg_cls();
  93. SLtt_set_cursor_visibility(0);
  94. SLsmg_refresh();
  95. for(i = 0; i < 16; i++)
  96. SLtt_set_color(i + 1, NULL, slang_colors[i], "black");
  97. #elif defined(USE_NCURSES)
  98. int i;
  99. initscr();
  100. keypad(stdscr, TRUE);
  101. nonl();
  102. cbreak();
  103. noecho();
  104. nodelay(stdscr, TRUE);
  105. curs_set(0);
  106. start_color();
  107. init_pair(1 + EE_BLACK, COLOR_BLACK, COLOR_BLACK);
  108. init_pair(1 + EE_BLUE, COLOR_BLUE, COLOR_BLACK);
  109. init_pair(1 + EE_GREEN, COLOR_GREEN, COLOR_BLACK);
  110. init_pair(1 + EE_CYAN, COLOR_CYAN, COLOR_BLACK);
  111. init_pair(1 + EE_RED, COLOR_RED, COLOR_BLACK);
  112. init_pair(1 + EE_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
  113. init_pair(1 + EE_BROWN, COLOR_YELLOW, COLOR_BLACK);
  114. init_pair(1 + EE_LIGHTGRAY, COLOR_WHITE, COLOR_BLACK);
  115. init_pair(1 + EE_DARKGRAY, COLOR_BLACK, COLOR_BLACK);
  116. init_pair(1 + EE_LIGHTBLUE, COLOR_BLUE, COLOR_BLACK);
  117. init_pair(1 + EE_LIGHTGREEN, COLOR_GREEN, COLOR_BLACK);
  118. init_pair(1 + EE_LIGHTCYAN, COLOR_CYAN, COLOR_BLACK);
  119. init_pair(1 + EE_LIGHTRED, COLOR_RED, COLOR_BLACK);
  120. init_pair(1 + EE_LIGHTMAGENTA, COLOR_MAGENTA, COLOR_BLACK);
  121. init_pair(1 + EE_YELLOW, COLOR_YELLOW, COLOR_BLACK);
  122. init_pair(1 + EE_WHITE, COLOR_WHITE, COLOR_BLACK);
  123. for(i = 0; i < 8; i++)
  124. {
  125. _caca_attr[i] = COLOR_PAIR(1 + i);
  126. _caca_attr[i + 8] = A_BOLD | COLOR_PAIR(1 + i);
  127. }
  128. #elif defined(USE_CONIO)
  129. gettextinfo(&ti);
  130. _caca_screen = malloc(2 * ti.screenwidth * ti.screenheight);
  131. if(_caca_screen == NULL)
  132. return -1;
  133. _wscroll = 0;
  134. _setcursortype(_NOCURSOR);
  135. clrscr();
  136. # if defined(SCREENUPDATE_IN_PC_H)
  137. ScreenRetrieve(_caca_screen);
  138. # else
  139. /* FIXME */
  140. # endif
  141. #endif
  142. _caca_empty_line = malloc(caca_get_width() + 1);
  143. memset(_caca_empty_line, ' ', caca_get_width());
  144. _caca_empty_line[caca_get_width()] = '\0';
  145. _caca_scratch_line = malloc(caca_get_width() + 1);
  146. _caca_delay = 0;
  147. _caca_rendertime = 0;
  148. return 0;
  149. }
  150. unsigned int caca_get_width(void)
  151. {
  152. #if defined(USE_SLANG)
  153. return SLtt_Screen_Cols;
  154. #elif defined(USE_NCURSES)
  155. return COLS;
  156. #elif defined(USE_CONIO)
  157. return ti.screenwidth;
  158. #endif
  159. }
  160. unsigned int caca_get_height(void)
  161. {
  162. #if defined(USE_SLANG)
  163. return SLtt_Screen_Rows;
  164. #elif defined(USE_NCURSES)
  165. return LINES;
  166. #else
  167. return ti.screenheight;
  168. #endif
  169. }
  170. void caca_set_delay(unsigned int usec)
  171. {
  172. _caca_delay = usec;
  173. }
  174. unsigned int caca_get_rendertime(void)
  175. {
  176. return _caca_rendertime;
  177. }
  178. const char *caca_get_color_name(unsigned int color)
  179. {
  180. static const char *color_names[16] =
  181. {
  182. "black",
  183. "blue",
  184. "green",
  185. "cyan",
  186. "red",
  187. "magenta",
  188. "brown",
  189. "light gray",
  190. "dark gray",
  191. "light blue",
  192. "light green",
  193. "light cyan",
  194. "light red",
  195. "light magenta",
  196. "yellow",
  197. "white",
  198. };
  199. if(color < 0 || color > 15)
  200. return "unknown color";
  201. return color_names[color];
  202. }
  203. static unsigned int _caca_getticks(void)
  204. {
  205. static unsigned int last_sec = 0, last_usec = 0;
  206. struct timeval tv;
  207. unsigned int ticks = 0;
  208. gettimeofday(&tv, NULL);
  209. if(last_sec != 0)
  210. {
  211. ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec);
  212. }
  213. last_sec = tv.tv_sec;
  214. last_usec = tv.tv_usec;
  215. return ticks;
  216. }
  217. void caca_refresh(void)
  218. {
  219. #define IDLE_USEC 10000
  220. static unsigned int lastticks = 0;
  221. unsigned int ticks = lastticks + _caca_getticks();
  222. #if defined(USE_SLANG)
  223. SLsmg_refresh();
  224. #elif defined(USE_NCURSES)
  225. refresh();
  226. #elif defined(USE_CONIO)
  227. # if defined(SCREENUPDATE_IN_PC_H)
  228. ScreenUpdate(_caca_screen);
  229. # else
  230. /* FIXME */
  231. # endif
  232. #endif
  233. /* Wait until _caca_delay + time of last call */
  234. ticks += _caca_getticks();
  235. for(; ticks < _caca_delay - IDLE_USEC; ticks += _caca_getticks())
  236. usleep(IDLE_USEC);
  237. /* Update the sliding mean of the render time */
  238. _caca_rendertime = (7 * _caca_rendertime + ticks) / 8;
  239. lastticks = ticks - _caca_delay;
  240. /* If we drifted too much, it's bad, bad, bad. */
  241. if(lastticks > _caca_delay)
  242. lastticks = 0;
  243. }
  244. void caca_end(void)
  245. {
  246. #if defined(USE_SLANG)
  247. SLtt_set_cursor_visibility(1);
  248. SLang_reset_tty();
  249. SLsmg_reset_smg();
  250. #elif defined(USE_NCURSES)
  251. curs_set(1);
  252. endwin();
  253. #elif defined(USE_CONIO)
  254. _wscroll = 1;
  255. textcolor((enum COLORS)WHITE);
  256. textbackground((enum COLORS)BLACK);
  257. gotoxy(caca_get_width(), caca_get_height());
  258. cputs("\r\n");
  259. _setcursortype(_NORMALCURSOR);
  260. #endif
  261. }