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.
 
 
 
 
 

216 line
4.3 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. #ifdef USE_SLANG
  24. # include <slang.h>
  25. #elif USE_NCURSES
  26. # include <curses.h>
  27. #elif USE_CONIO
  28. # include <conio.h>
  29. #else
  30. # error "no graphics library detected"
  31. #endif
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <string.h>
  35. #include <sys/time.h>
  36. #include <time.h>
  37. #include "ee.h"
  38. static int _ee_delay;
  39. #ifdef USE_CONIO
  40. static struct text_info ti;
  41. #endif
  42. int ee_init(void)
  43. {
  44. #ifdef USE_SLANG
  45. static char * colors[] = { "black", "green", "yellow", "white",
  46. "red", "gray", "lightgray", "blue", "cyan", "magenta", NULL };
  47. int i;
  48. /* Initialize slang library */
  49. SLsig_block_signals();
  50. SLtt_get_terminfo();
  51. if(SLkp_init() == -1)
  52. {
  53. SLsig_unblock_signals();
  54. return -1;
  55. }
  56. SLang_init_tty(-1, 0, 1);
  57. if(SLsmg_init_smg() == -1)
  58. {
  59. SLsig_unblock_signals();
  60. return -1;
  61. }
  62. SLsig_unblock_signals();
  63. SLsmg_cls();
  64. SLtt_set_cursor_visibility(0);
  65. SLsmg_refresh();
  66. for(i = 0; colors[i]; i++)
  67. {
  68. SLtt_set_color(i + 1, NULL, colors[i], "black");
  69. }
  70. #elif USE_NCURSES
  71. /* Initialize ncurses library */
  72. initscr();
  73. keypad(stdscr, TRUE);
  74. nonl();
  75. cbreak();
  76. noecho();
  77. nodelay(stdscr, TRUE);
  78. curs_set(0);
  79. start_color();
  80. init_pair(EE_BLACK, COLOR_BLACK, COLOR_BLACK);
  81. init_pair(EE_GREEN, COLOR_GREEN, COLOR_BLACK);
  82. init_pair(EE_YELLOW, COLOR_YELLOW, COLOR_BLACK);
  83. init_pair(EE_WHITE, COLOR_WHITE, COLOR_BLACK);
  84. init_pair(EE_RED, COLOR_RED, COLOR_BLACK);
  85. init_pair(EE_GRAY, COLOR_WHITE, COLOR_BLACK); // XXX
  86. init_pair(EE_LIGHTGRAY, COLOR_WHITE, COLOR_BLACK); // XXX
  87. init_pair(EE_BLUE, COLOR_BLUE, COLOR_BLACK);
  88. init_pair(EE_CYAN, COLOR_CYAN, COLOR_BLACK);
  89. init_pair(EE_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
  90. #elif USE_CONIO
  91. _wscroll = 0;
  92. _setcursortype(_NOCURSOR);
  93. clrscr();
  94. gettextinfo(&ti);
  95. //window(2, 2, 20, 20);
  96. #endif
  97. _ee_delay = 0;
  98. return 0;
  99. }
  100. void ee_set_delay(int delay)
  101. {
  102. _ee_delay = delay;
  103. }
  104. int ee_get_width(void)
  105. {
  106. #ifdef USE_SLANG
  107. return SLtt_Screen_Cols;
  108. #elif USE_NCURSES
  109. return COLS;
  110. #elif USE_CONIO
  111. return ti.screenwidth;
  112. #endif
  113. }
  114. int ee_get_height(void)
  115. {
  116. #ifdef USE_SLANG
  117. return SLtt_Screen_Rows;
  118. #elif USE_NCURSES
  119. return LINES;
  120. #else
  121. return ti.screenheight;
  122. #endif
  123. }
  124. #ifndef USE_CONIO
  125. static int64_t local_time(void)
  126. {
  127. struct timeval tv;
  128. int64_t now;
  129. gettimeofday(&tv, NULL);
  130. now = tv.tv_sec;
  131. now *= 1000000;
  132. now += tv.tv_usec;
  133. return now;
  134. }
  135. #endif
  136. void ee_refresh(void)
  137. {
  138. #ifndef USE_CONIO
  139. static int64_t local_clock = 0;
  140. int64_t now;
  141. if(!local_clock)
  142. {
  143. /* Initialize local_clock */
  144. local_clock = local_time();
  145. }
  146. if(local_time() > local_clock + 10000)
  147. {
  148. /* If we are late, we shouldn't display anything */
  149. }
  150. #endif
  151. #ifdef USE_SLANG
  152. SLsmg_refresh();
  153. #elif USE_NCURSES
  154. refresh();
  155. #elif USE_CONIO
  156. /* Do nothing? */
  157. #endif
  158. #ifndef USE_CONIO
  159. now = local_time();
  160. if(now < local_clock + _ee_delay - 10000)
  161. {
  162. usleep(local_clock + _ee_delay - 10000 - now);
  163. }
  164. local_clock += _ee_delay;
  165. #endif
  166. }
  167. void ee_end(void)
  168. {
  169. #ifdef USE_SLANG
  170. SLtt_set_cursor_visibility(1);
  171. SLang_reset_tty();
  172. SLsmg_reset_smg();
  173. #elif USE_NCURSES
  174. curs_set(1);
  175. endwin();
  176. #elif USE_CONIO
  177. _wscroll = 1;
  178. ee_set_color(EE_WHITE);
  179. ee_putstr(ee_get_width(), ee_get_height()-1, "\r\n");
  180. _setcursortype(_NORMALCURSOR);
  181. #endif
  182. }