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.
 
 
 
 
 

228 lines
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. #if defined(USE_SLANG) || defined(USE_NCURSES)
  104. /* We could use SLsmg_cls(), but drawing empty lines is much faster */
  105. int x = ee_get_width(), y = ee_get_height();
  106. char *empty_line = malloc((x + 1) * sizeof(char));
  107. memset(empty_line, ' ', x);
  108. empty_line[x] = '\0';
  109. for(y; y--;)
  110. {
  111. ee_goto(0, y);
  112. ee_putstr(empty_line);
  113. }
  114. free(empty_line);
  115. #else
  116. /* Use dummy driver */
  117. #endif
  118. }
  119. static int64_t local_time(void)
  120. {
  121. struct timeval tv;
  122. int64_t now;
  123. gettimeofday(&tv, NULL);
  124. now = tv.tv_sec;
  125. now *= 1000000;
  126. now += tv.tv_usec;
  127. return now;
  128. }
  129. #define DELAY 40000
  130. void ee_refresh(void)
  131. {
  132. static int64_t local_clock = 0;
  133. int64_t now;
  134. ee_goto(0, 0);
  135. if(!local_clock)
  136. {
  137. /* Initialize local_clock */
  138. local_clock = local_time();
  139. }
  140. if(local_time() > local_clock + 10000)
  141. {
  142. /* If we are late, we shouldn't display anything */
  143. }
  144. #ifdef USE_SLANG
  145. SLsmg_refresh();
  146. #elif USE_NCURSES
  147. refresh();
  148. #else
  149. /* Use dummy driver */
  150. #endif
  151. now = local_time();
  152. if(now < local_clock + DELAY - 10000)
  153. {
  154. usleep(local_clock + DELAY - 10000 - now);
  155. }
  156. local_clock += DELAY;
  157. }
  158. void ee_end(void)
  159. {
  160. #ifdef USE_SLANG
  161. SLtt_set_cursor_visibility(1);
  162. SLang_reset_tty();
  163. SLsmg_reset_smg();
  164. #elif USE_NCURSES
  165. curs_set(1);
  166. endwin();
  167. #else
  168. /* Use dummy driver */
  169. #endif
  170. }
  171. char ee_get_key(void)
  172. {
  173. #ifdef USE_SLANG
  174. if(SLang_input_pending(0))
  175. {
  176. return SLang_getkey();
  177. }
  178. #elif USE_NCURSES
  179. char key = getch();
  180. if(key != ERR)
  181. {
  182. return key;
  183. }
  184. #else
  185. return 0;
  186. #endif
  187. return 0;
  188. }