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.
 
 
 
 
 
 

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