Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

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