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.
 
 
 
 
 

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