]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots.c
5490bcfba31ec79b41140fa913f9ea3519bed3de
[ncurses.git] / test / dots.c
1 /****************************************************************************
2  * Copyright (c) 1999-2013,2017 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 /*
30  * Author: Thomas E. Dickey <dickey@clark.net> 1999
31  *
32  * $Id: dots.c,v 1.31 2017/10/22 00:44:06 tom Exp $
33  *
34  * A simple demo of the terminfo interface.
35  */
36 #define USE_TINFO
37 #include <test.priv.h>
38
39 #if HAVE_SETUPTERM
40
41 #include <time.h>
42
43 static bool interrupted = FALSE;
44 static long total_chars = 0;
45 static time_t started;
46
47 static
48 TPUTS_PROTO(outc, c)
49 {
50     int rc = c;
51
52     if (interrupted) {
53         char tmp = (char) c;
54         if (write(STDOUT_FILENO, &tmp, (size_t) 1) == -1)
55             rc = EOF;
56     } else {
57         rc = putc(c, stdout);
58     }
59     TPUTS_RETURN(rc);
60 }
61
62 static bool
63 outs(const char *s)
64 {
65     if (VALID_STRING(s)) {
66         tputs(s, 1, outc);
67         return TRUE;
68     }
69     return FALSE;
70 }
71
72 static void
73 cleanup(void)
74 {
75     outs(exit_attribute_mode);
76     if (!outs(orig_colors))
77         outs(orig_pair);
78     outs(clear_screen);
79     outs(cursor_normal);
80
81     printf("\n\n%ld total cells, rate %.2f/sec\n",
82            total_chars,
83            ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
84 }
85
86 static void
87 onsig(int n GCC_UNUSED)
88 {
89     interrupted = TRUE;
90 }
91
92 static double
93 ranf(void)
94 {
95     long r = (rand() & 077777);
96     return ((double) r / 32768.);
97 }
98
99 static int
100 get_number(const char *cap, int map)
101 {
102     int result = map;
103     if (cap != 0) {
104         int check = tigetnum(cap);
105         if (check > 0)
106             result = check;
107     }
108     return result;
109 }
110
111 static void
112 usage(void)
113 {
114     static const char *msg[] =
115     {
116         "Usage: dots [options]"
117         ,""
118         ,"Options:"
119         ," -T TERM  override $TERM"
120         ," -e       allow environment $LINES / $COLUMNS"
121         ," -f       use tigetnum rather than <term.h> mapping"
122         ," -m SIZE  set margin (default: 2)"
123         ," -s MSECS delay 1% of the time (default: 1 msecs)"
124     };
125     size_t n;
126
127     for (n = 0; n < SIZEOF(msg); n++)
128         fprintf(stderr, "%s\n", msg[n]);
129
130     ExitProgram(EXIT_FAILURE);
131 }
132
133 int
134 main(int argc,
135      char *argv[])
136 {
137     int x, y, z, p;
138     double r;
139     double c;
140     int my_colors;
141     int f_option = 0;
142     int m_option = 2;
143     int s_option = 1;
144
145     while ((x = getopt(argc, argv, "T:efm:s:")) != -1) {
146         switch (x) {
147         case 'T':
148             putenv(strcat(strcpy(malloc(6 + strlen(optarg)), "TERM="), optarg));
149             break;
150         case 'e':
151             use_env(TRUE);
152             break;
153         case 'f':
154             f_option = 1;
155             break;
156         case 'm':
157             m_option = atoi(optarg);
158             break;
159         case 's':
160             s_option = atoi(optarg);
161             break;
162         default:
163             usage();
164             break;
165         }
166     }
167
168     InitAndCatch(setupterm((char *) 0, 1, (int *) 0), onsig);
169
170     srand((unsigned) time(0));
171
172     outs(clear_screen);
173     outs(cursor_invisible);
174
175 #define GetNumber(ln,sn) get_number(f_option ? #sn : 0, ln)
176     my_colors = GetNumber(max_colors, colors);
177     if (my_colors > 1) {
178         if (!VALID_STRING(set_a_foreground)
179             || !VALID_STRING(set_a_background)
180             || (!VALID_STRING(orig_colors) && !VALID_STRING(orig_pair)))
181             my_colors = -1;
182     }
183
184     r = (double) (GetNumber(lines, lines) - (m_option * 2));
185     c = (double) (GetNumber(columns, cols) - (m_option * 2));
186     started = time((time_t *) 0);
187
188     while (!interrupted) {
189         x = (int) (c * ranf()) + m_option;
190         y = (int) (r * ranf()) + m_option;
191         p = (ranf() > 0.9) ? '*' : ' ';
192
193         tputs(tparm3(cursor_address, y, x), 1, outc);
194         if (my_colors > 0) {
195             z = (int) (ranf() * my_colors);
196             if (ranf() > 0.01) {
197                 tputs(tparm2(set_a_foreground, z), 1, outc);
198             } else {
199                 tputs(tparm2(set_a_background, z), 1, outc);
200                 napms(s_option);
201             }
202         } else if (VALID_STRING(exit_attribute_mode)
203                    && VALID_STRING(enter_reverse_mode)) {
204             if (ranf() <= 0.01) {
205                 outs((ranf() > 0.6)
206                      ? enter_reverse_mode
207                      : exit_attribute_mode);
208                 napms(s_option);
209             }
210         }
211         outc(p);
212         fflush(stdout);
213         ++total_chars;
214     }
215     cleanup();
216     ExitProgram(EXIT_SUCCESS);
217 }
218 #else
219 int
220 main(int argc GCC_UNUSED,
221      char *argv[]GCC_UNUSED)
222 {
223     fprintf(stderr, "This program requires terminfo\n");
224     exit(EXIT_FAILURE);
225 }
226 #endif