echochar.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /****************************************************************************
  2. * Copyright (c) 2006-2007,2008 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /*
  29. * $Id: echochar.c,v 1.6 2008/04/12 22:06:04 tom Exp $
  30. *
  31. * Demonstrate the echochar function (compare to dots.c).
  32. * Thomas Dickey - 2006/11/4
  33. */
  34. #include <test.priv.h>
  35. #include <time.h>
  36. #define valid(s) ((s != 0) && s != (char *)-1)
  37. static bool interrupted = FALSE;
  38. static long total_chars = 0;
  39. static time_t started;
  40. static void
  41. cleanup(void)
  42. {
  43. endwin();
  44. printf("\n\n%ld total chars, rate %.2f/sec\n",
  45. total_chars,
  46. ((double) (total_chars) / (time((time_t *) 0) - started)));
  47. }
  48. static void
  49. onsig(int n GCC_UNUSED)
  50. {
  51. interrupted = TRUE;
  52. }
  53. static float
  54. ranf(void)
  55. {
  56. long r = (rand() & 077777);
  57. return ((float) r / 32768.);
  58. }
  59. static void
  60. set_color(char *my_pairs, int fg, int bg)
  61. {
  62. int pair = (fg * COLORS) + bg;
  63. if (!my_pairs[pair]) {
  64. init_pair(pair, fg, bg);
  65. }
  66. attron(COLOR_PAIR(pair));
  67. }
  68. int
  69. main(
  70. int argc GCC_UNUSED,
  71. char *argv[]GCC_UNUSED)
  72. {
  73. int ch, x, y, z, p;
  74. float r;
  75. float c;
  76. bool use_colors;
  77. bool opt_r = FALSE;
  78. char *my_pairs = 0;
  79. int last_fg = 0;
  80. int last_bg = 0;
  81. while ((ch = getopt(argc, argv, "r")) != -1) {
  82. switch (ch) {
  83. case 'r':
  84. opt_r = TRUE;
  85. break;
  86. default:
  87. fprintf(stderr, "usage: echochar [-r]\n");
  88. ExitProgram(EXIT_FAILURE);
  89. }
  90. }
  91. CATCHALL(onsig);
  92. initscr();
  93. use_colors = has_colors();
  94. if (use_colors) {
  95. start_color();
  96. if (COLOR_PAIRS > 0) {
  97. my_pairs = typeCalloc(char, COLOR_PAIRS);
  98. }
  99. use_colors = (my_pairs != 0);
  100. }
  101. srand((unsigned) time(0));
  102. curs_set(0);
  103. r = (float) (LINES - 4);
  104. c = (float) (COLS - 4);
  105. started = time((time_t *) 0);
  106. while (!interrupted) {
  107. x = (int) (c * ranf()) + 2;
  108. y = (int) (r * ranf()) + 2;
  109. p = (ranf() > 0.9) ? '*' : ' ';
  110. move(y, x);
  111. if (use_colors > 0) {
  112. z = (int) (ranf() * COLORS);
  113. if (ranf() > 0.01) {
  114. set_color(my_pairs, z, last_bg);
  115. last_fg = z;
  116. } else {
  117. set_color(my_pairs, last_fg, z);
  118. last_bg = z;
  119. napms(1);
  120. }
  121. } else {
  122. if (ranf() <= 0.01) {
  123. if (ranf() > 0.6)
  124. attron(A_REVERSE);
  125. else
  126. attroff(A_REVERSE);
  127. napms(1);
  128. }
  129. }
  130. if (opt_r) {
  131. addch(UChar(p));
  132. refresh();
  133. } else {
  134. echochar(UChar(p));
  135. }
  136. ++total_chars;
  137. }
  138. cleanup();
  139. ExitProgram(EXIT_SUCCESS);
  140. }