Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

297 рядки
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. void ee_set_delay(int usec)
  167. {
  168. _ee_delay = usec;
  169. }
  170. int ee_get_width(void)
  171. {
  172. #if defined(USE_SLANG)
  173. return SLtt_Screen_Cols;
  174. #elif defined(USE_NCURSES)
  175. return COLS;
  176. #elif defined(USE_CONIO)
  177. return ti.screenwidth;
  178. #endif
  179. }
  180. int ee_get_height(void)
  181. {
  182. #if defined(USE_SLANG)
  183. return SLtt_Screen_Rows;
  184. #elif defined(USE_NCURSES)
  185. return LINES;
  186. #else
  187. return ti.screenheight;
  188. #endif
  189. }
  190. #if !defined(USE_CONIO)
  191. static int64_t local_time(void)
  192. {
  193. struct timeval tv;
  194. int64_t now;
  195. gettimeofday(&tv, NULL);
  196. now = tv.tv_sec;
  197. now *= 1000000;
  198. now += tv.tv_usec;
  199. return now;
  200. }
  201. #endif
  202. void ee_refresh(void)
  203. {
  204. #if !defined(USE_CONIO)
  205. static int64_t local_clock = 0;
  206. int64_t now;
  207. if(!local_clock)
  208. {
  209. /* Initialize local_clock */
  210. local_clock = local_time();
  211. }
  212. if(local_time() > local_clock + 10000)
  213. {
  214. /* If we are late, we shouldn't display anything */
  215. }
  216. #endif
  217. #if defined(USE_SLANG)
  218. SLsmg_refresh();
  219. #elif defined(USE_NCURSES)
  220. refresh();
  221. #elif defined(USE_CONIO)
  222. # if defined(SCREENUPDATE_IN_PC_H)
  223. ScreenUpdate(_ee_screen);
  224. # else
  225. /* FIXME */
  226. # endif
  227. #endif
  228. #if !defined(USE_CONIO)
  229. now = local_time();
  230. if(now < local_clock + _ee_delay - 10000)
  231. {
  232. usleep(local_clock + _ee_delay - 10000 - now);
  233. }
  234. local_clock += _ee_delay;
  235. #else
  236. delay(5);
  237. #endif
  238. }
  239. void ee_end(void)
  240. {
  241. #if defined(USE_SLANG)
  242. SLtt_set_cursor_visibility(1);
  243. SLang_reset_tty();
  244. SLsmg_reset_smg();
  245. #elif defined(USE_NCURSES)
  246. curs_set(1);
  247. endwin();
  248. #elif defined(USE_CONIO)
  249. _wscroll = 1;
  250. textcolor((enum COLORS)WHITE);
  251. textbackground((enum COLORS)BLACK);
  252. gotoxy(ee_get_width(), ee_get_height());
  253. cputs("\r\n");
  254. _setcursortype(_NORMALCURSOR);
  255. #endif
  256. }