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.
 
 
 
 
 
 

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