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.
 
 
 
 
 

290 lines
6.0 KiB

  1. /*
  2. * libee 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 "ee.h"
  42. #include "ee_internals.h"
  43. /* Global array with color names */
  44. char *ee_color_names[16] =
  45. {
  46. "black",
  47. "blue",
  48. "green",
  49. "cyan",
  50. "red",
  51. "magenta",
  52. "brown",
  53. "lightgray",
  54. "darkgray",
  55. "lightblue",
  56. "lightgreen",
  57. "lightcyan",
  58. "lightred",
  59. "lightmagenta",
  60. "yellow",
  61. "white",
  62. };
  63. static int _ee_delay;
  64. #if defined(USE_NCURSES)
  65. int _ee_attr[16];
  66. #endif
  67. #if defined(USE_CONIO)
  68. static struct text_info ti;
  69. char *_ee_screen;
  70. #endif
  71. int ee_init(void)
  72. {
  73. #if defined(USE_SLANG)
  74. static char *slang_colors[16] =
  75. {
  76. "black",
  77. "blue",
  78. "green",
  79. "cyan",
  80. "red",
  81. "magenta",
  82. "brown",
  83. "lightgray",
  84. "gray",
  85. "brightblue",
  86. "brightgreen",
  87. "brightcyan",
  88. "brightred",
  89. "brightmagenta",
  90. "yellow",
  91. "white",
  92. };
  93. int i;
  94. /* Initialize slang library */
  95. SLsig_block_signals();
  96. SLtt_get_terminfo();
  97. if(SLkp_init() == -1)
  98. {
  99. SLsig_unblock_signals();
  100. return -1;
  101. }
  102. SLang_init_tty(-1, 0, 1);
  103. if(SLsmg_init_smg() == -1)
  104. {
  105. SLsig_unblock_signals();
  106. return -1;
  107. }
  108. SLsig_unblock_signals();
  109. SLsmg_cls();
  110. SLtt_set_cursor_visibility(0);
  111. SLsmg_refresh();
  112. for(i = 0; i < 16; i++)
  113. SLtt_set_color(i + 1, NULL, slang_colors[i], "black");
  114. #elif defined(USE_NCURSES)
  115. int i;
  116. initscr();
  117. keypad(stdscr, TRUE);
  118. nonl();
  119. cbreak();
  120. noecho();
  121. nodelay(stdscr, TRUE);
  122. curs_set(0);
  123. start_color();
  124. init_pair(1 + EE_BLACK, COLOR_BLACK, COLOR_BLACK);
  125. init_pair(1 + EE_BLUE, COLOR_BLUE, COLOR_BLACK);
  126. init_pair(1 + EE_GREEN, COLOR_GREEN, COLOR_BLACK);
  127. init_pair(1 + EE_CYAN, COLOR_CYAN, COLOR_BLACK);
  128. init_pair(1 + EE_RED, COLOR_RED, COLOR_BLACK);
  129. init_pair(1 + EE_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
  130. init_pair(1 + EE_BROWN, COLOR_YELLOW, COLOR_BLACK);
  131. init_pair(1 + EE_LIGHTGRAY, COLOR_WHITE, COLOR_BLACK);
  132. init_pair(1 + EE_DARKGRAY, COLOR_BLACK, COLOR_BLACK);
  133. init_pair(1 + EE_LIGHTBLUE, COLOR_BLUE, COLOR_BLACK);
  134. init_pair(1 + EE_LIGHTGREEN, COLOR_GREEN, COLOR_BLACK);
  135. init_pair(1 + EE_LIGHTCYAN, COLOR_CYAN, COLOR_BLACK);
  136. init_pair(1 + EE_LIGHTRED, COLOR_RED, COLOR_BLACK);
  137. init_pair(1 + EE_LIGHTMAGENTA, COLOR_MAGENTA, COLOR_BLACK);
  138. init_pair(1 + EE_YELLOW, COLOR_YELLOW, COLOR_BLACK);
  139. init_pair(1 + EE_WHITE, COLOR_WHITE, COLOR_BLACK);
  140. for(i = 0; i < 8; i++)
  141. {
  142. _ee_attr[i] = COLOR_PAIR(1 + i);
  143. _ee_attr[i + 8] = A_BOLD | COLOR_PAIR(1 + i);
  144. }
  145. #elif defined(USE_CONIO)
  146. _wscroll = 0;
  147. _setcursortype(_NOCURSOR);
  148. clrscr();
  149. gettextinfo(&ti);
  150. _ee_screen = malloc(2 * ti.screenwidth * ti.screenheight);
  151. # if defined(SCREENUPDATE_IN_PC_H)
  152. ScreenRetrieve(_ee_screen);
  153. # else
  154. /* FIXME */
  155. # endif
  156. #endif
  157. _ee_delay = 0;
  158. return 0;
  159. }
  160. void ee_set_delay(int usec)
  161. {
  162. _ee_delay = usec;
  163. }
  164. int ee_get_width(void)
  165. {
  166. #if defined(USE_SLANG)
  167. return SLtt_Screen_Cols;
  168. #elif defined(USE_NCURSES)
  169. return COLS;
  170. #elif defined(USE_CONIO)
  171. return ti.screenwidth;
  172. #endif
  173. }
  174. int ee_get_height(void)
  175. {
  176. #if defined(USE_SLANG)
  177. return SLtt_Screen_Rows;
  178. #elif defined(USE_NCURSES)
  179. return LINES;
  180. #else
  181. return ti.screenheight;
  182. #endif
  183. }
  184. #if !defined(USE_CONIO)
  185. static int64_t local_time(void)
  186. {
  187. struct timeval tv;
  188. int64_t now;
  189. gettimeofday(&tv, NULL);
  190. now = tv.tv_sec;
  191. now *= 1000000;
  192. now += tv.tv_usec;
  193. return now;
  194. }
  195. #endif
  196. void ee_refresh(void)
  197. {
  198. #if !defined(USE_CONIO)
  199. static int64_t local_clock = 0;
  200. int64_t now;
  201. if(!local_clock)
  202. {
  203. /* Initialize local_clock */
  204. local_clock = local_time();
  205. }
  206. if(local_time() > local_clock + 10000)
  207. {
  208. /* If we are late, we shouldn't display anything */
  209. }
  210. #endif
  211. #if defined(USE_SLANG)
  212. SLsmg_refresh();
  213. #elif defined(USE_NCURSES)
  214. refresh();
  215. #elif defined(USE_CONIO)
  216. # if defined(SCREENUPDATE_IN_PC_H)
  217. ScreenUpdate(_ee_screen);
  218. # else
  219. /* FIXME */
  220. # endif
  221. #endif
  222. #if !defined(USE_CONIO)
  223. now = local_time();
  224. if(now < local_clock + _ee_delay - 10000)
  225. {
  226. usleep(local_clock + _ee_delay - 10000 - now);
  227. }
  228. local_clock += _ee_delay;
  229. #else
  230. delay(5);
  231. #endif
  232. }
  233. void ee_end(void)
  234. {
  235. #if defined(USE_SLANG)
  236. SLtt_set_cursor_visibility(1);
  237. SLang_reset_tty();
  238. SLsmg_reset_smg();
  239. #elif defined(USE_NCURSES)
  240. curs_set(1);
  241. endwin();
  242. #elif defined(USE_CONIO)
  243. _wscroll = 1;
  244. textcolor((enum COLORS)WHITE);
  245. textbackground((enum COLORS)BLACK);
  246. gotoxy(ee_get_width(), ee_get_height());
  247. cputs("\r\n");
  248. _setcursortype(_NORMALCURSOR);
  249. #endif
  250. }