missing ncurses sources
[tomato.git] / release / src / router / libncurses / test / dots_mvcur.c
blob558683d56fdb822d5bf22da94ab549996833b082
1 /****************************************************************************
2 * Copyright (c) 2007-2008,2009 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 ****************************************************************************/
30 * Author: Thomas E. Dickey - 2007
32 * $Id: dots_mvcur.c,v 1.6 2010/11/14 01:00:44 tom Exp $
34 * A simple demo of the terminfo interface, and mvcur.
36 #define USE_TINFO
37 #include <test.priv.h>
39 #if HAVE_SETUPTERM
41 #include <time.h>
43 #define valid(s) ((s != 0) && s != (char *)-1)
45 static bool interrupted = FALSE;
46 static long total_chars = 0;
47 static time_t started;
49 static int
50 outc(TPUTS_ARG c)
52 int rc = c;
54 if (interrupted) {
55 char tmp = (char) c;
56 if (write(STDOUT_FILENO, &tmp, 1) == -1)
57 rc = EOF;
58 } else {
59 if (putc(c, stdout) == EOF)
60 rc = EOF;
62 return rc;
65 static bool
66 outs(char *s)
68 if (valid(s)) {
69 tputs(s, 1, outc);
70 return TRUE;
72 return FALSE;
75 static void
76 cleanup(void)
78 outs(exit_attribute_mode);
79 if (!outs(orig_colors))
80 outs(orig_pair);
81 outs(clear_screen);
82 outs(cursor_normal);
84 printf("\n\n%ld total chars, rate %.2f/sec\n",
85 total_chars,
86 ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
89 static void
90 onsig(int n GCC_UNUSED)
92 interrupted = TRUE;
95 static double
96 ranf(void)
98 long r = (rand() & 077777);
99 return ((double) r / 32768.);
103 main(int argc GCC_UNUSED,
104 char *argv[]GCC_UNUSED)
106 int x0 = 1, y0 = 1;
107 int x, y, z, p;
108 double r;
109 double c;
110 SCREEN *sp;
112 CATCHALL(onsig);
114 srand((unsigned) time(0));
115 sp = newterm((char *) 0, stdout, stdin);
116 outs(clear_screen);
117 outs(cursor_home);
118 outs(cursor_invisible);
119 if (max_colors > 1) {
120 if (!valid(set_a_foreground)
121 || !valid(set_a_background)
122 || (!valid(orig_colors) && !valid(orig_pair)))
123 max_colors = -1;
126 r = (double) (lines - 4);
127 c = (double) (columns - 4);
128 started = time((time_t *) 0);
130 while (!interrupted) {
131 x = (int) (c * ranf()) + 2;
132 y = (int) (r * ranf()) + 2;
133 p = (ranf() > 0.9) ? '*' : ' ';
135 if (mvcur(y0, x0, y, x) != ERR) {
136 x0 = x;
137 y0 = y;
140 if (max_colors > 0) {
141 z = (int) (ranf() * max_colors);
142 if (ranf() > 0.01) {
143 tputs(tparm2(set_a_foreground, z), 1, outc);
144 } else {
145 tputs(tparm2(set_a_background, z), 1, outc);
146 napms(1);
148 } else if (valid(exit_attribute_mode)
149 && valid(enter_reverse_mode)) {
150 if (ranf() <= 0.01) {
151 outs((ranf() > 0.6)
152 ? enter_reverse_mode
153 : exit_attribute_mode);
154 napms(1);
157 outc(p);
158 fflush(stdout);
159 ++total_chars;
161 cleanup();
162 endwin();
163 delscreen(sp);
164 ExitProgram(EXIT_SUCCESS);
166 #else
168 main(int argc GCC_UNUSED,
169 char *argv[]GCC_UNUSED)
171 fprintf(stderr, "This program requires terminfo\n");
172 exit(EXIT_FAILURE);
174 #endif