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.

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