dots_mvcur.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /****************************************************************************
  2. * Copyright (c) 1999-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. * Author: Thomas E. Dickey - 2007
  30. *
  31. * $Id: dots_mvcur.c,v 1.2 2008/02/09 18:08:57 tom Exp $
  32. *
  33. * A simple demo of the terminfo interface, and mvcur.
  34. */
  35. #define USE_TINFO
  36. #include <test.priv.h>
  37. #if HAVE_SETUPTERM
  38. #include <time.h>
  39. #define valid(s) ((s != 0) && s != (char *)-1)
  40. static bool interrupted = FALSE;
  41. static long total_chars = 0;
  42. static time_t started;
  43. static int
  44. outc(TPUTS_ARG c)
  45. {
  46. if (interrupted) {
  47. char tmp = c;
  48. write(STDOUT_FILENO, &tmp, 1);
  49. } else {
  50. putc(c, stdout);
  51. }
  52. return 0;
  53. }
  54. static bool
  55. outs(char *s)
  56. {
  57. if (valid(s)) {
  58. tputs(s, 1, outc);
  59. return TRUE;
  60. }
  61. return FALSE;
  62. }
  63. static void
  64. cleanup(void)
  65. {
  66. outs(exit_attribute_mode);
  67. if (!outs(orig_colors))
  68. outs(orig_pair);
  69. outs(clear_screen);
  70. outs(cursor_normal);
  71. printf("\n\n%ld total chars, rate %.2f/sec\n",
  72. total_chars,
  73. ((double) (total_chars) / (time((time_t *) 0) - started)));
  74. }
  75. static void
  76. onsig(int n GCC_UNUSED)
  77. {
  78. interrupted = TRUE;
  79. }
  80. static float
  81. ranf(void)
  82. {
  83. long r = (rand() & 077777);
  84. return ((float) r / 32768.);
  85. }
  86. int
  87. main(
  88. int argc GCC_UNUSED,
  89. char *argv[]GCC_UNUSED)
  90. {
  91. int x0 = 1, y0 = 1;
  92. int x, y, z, p;
  93. float r;
  94. float c;
  95. SCREEN *sp;
  96. CATCHALL(onsig);
  97. srand((unsigned) time(0));
  98. sp = newterm((char *) 0, stdout, stdin);
  99. outs(clear_screen);
  100. outs(cursor_home);
  101. outs(cursor_invisible);
  102. if (max_colors > 1) {
  103. if (!valid(set_a_foreground)
  104. || !valid(set_a_background)
  105. || (!valid(orig_colors) && !valid(orig_pair)))
  106. max_colors = -1;
  107. }
  108. r = (float) (lines - 4);
  109. c = (float) (columns - 4);
  110. started = time((time_t *) 0);
  111. while (!interrupted) {
  112. x = (int) (c * ranf()) + 2;
  113. y = (int) (r * ranf()) + 2;
  114. p = (ranf() > 0.9) ? '*' : ' ';
  115. if (mvcur(y0, x0, y, x) != ERR) {
  116. x0 = x;
  117. y0 = y;
  118. }
  119. if (max_colors > 0) {
  120. z = (int) (ranf() * max_colors);
  121. if (ranf() > 0.01) {
  122. tputs(tparm2(set_a_foreground, z), 1, outc);
  123. } else {
  124. tputs(tparm2(set_a_background, z), 1, outc);
  125. napms(1);
  126. }
  127. } else if (valid(exit_attribute_mode)
  128. && valid(enter_reverse_mode)) {
  129. if (ranf() <= 0.01) {
  130. outs((ranf() > 0.6)
  131. ? enter_reverse_mode
  132. : exit_attribute_mode);
  133. napms(1);
  134. }
  135. }
  136. outc(p);
  137. fflush(stdout);
  138. ++total_chars;
  139. }
  140. cleanup();
  141. endwin();
  142. delscreen(sp);
  143. ExitProgram(EXIT_SUCCESS);
  144. }
  145. #else
  146. int
  147. main(int argc GCC_UNUSED,
  148. char *argv[]GCC_UNUSED)
  149. {
  150. fprintf(stderr, "This program requires terminfo\n");
  151. exit(EXIT_FAILURE);
  152. }
  153. #endif