]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots.c
ncurses 6.1 - patch 20190302
[ncurses.git] / test / dots.c
1 /****************************************************************************
2  * Copyright (c) 1999-2017,2019 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.34 2019/01/21 14:20:18 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(NCURSES_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 #if HAVE_USE_ENV
121         ," -e       allow environment $LINES / $COLUMNS"
122 #endif
123         ," -f       use tigetnum rather than <term.h> mapping"
124         ," -m SIZE  set margin (default: 2)"
125         ," -s MSECS delay 1% of the time (default: 1 msecs)"
126     };
127     size_t n;
128
129     for (n = 0; n < SIZEOF(msg); n++)
130         fprintf(stderr, "%s\n", msg[n]);
131
132     ExitProgram(EXIT_FAILURE);
133 }
134
135 int
136 main(int argc,
137      char *argv[])
138 {
139     int x, y, z, p;
140     double r;
141     double c;
142     int my_colors;
143     int f_option = 0;
144     int m_option = 2;
145     int s_option = 1;
146     size_t need;
147     char *my_env;
148
149     while ((x = getopt(argc, argv, "T:efm:s:")) != -1) {
150         switch (x) {
151         case 'T':
152             need = 6 + strlen(optarg);
153             my_env = malloc(need);
154             _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
155             putenv(my_env);
156             break;
157 #if HAVE_USE_ENV
158         case 'e':
159             use_env(TRUE);
160             break;
161 #endif
162         case 'f':
163             f_option = 1;
164             break;
165         case 'm':
166             m_option = atoi(optarg);
167             break;
168         case 's':
169             s_option = atoi(optarg);
170             break;
171         default:
172             usage();
173             break;
174         }
175     }
176
177     InitAndCatch(setupterm((char *) 0, 1, (int *) 0), onsig);
178
179     srand((unsigned) time(0));
180
181     outs(clear_screen);
182     outs(cursor_invisible);
183
184 #define GetNumber(ln,sn) get_number(f_option ? #sn : 0, ln)
185     my_colors = GetNumber(max_colors, colors);
186     if (my_colors > 1) {
187         if (!VALID_STRING(set_a_foreground)
188             || !VALID_STRING(set_a_background)
189             || (!VALID_STRING(orig_colors) && !VALID_STRING(orig_pair)))
190             my_colors = -1;
191     }
192
193     r = (double) (GetNumber(lines, lines) - (m_option * 2));
194     c = (double) (GetNumber(columns, cols) - (m_option * 2));
195     started = time((time_t *) 0);
196
197     while (!interrupted) {
198         x = (int) (c * ranf()) + m_option;
199         y = (int) (r * ranf()) + m_option;
200         p = (ranf() > 0.9) ? '*' : ' ';
201
202         tputs(tparm3(cursor_address, y, x), 1, outc);
203         if (my_colors > 0) {
204             z = (int) (ranf() * my_colors);
205             if (ranf() > 0.01) {
206                 tputs(tparm2(set_a_foreground, z), 1, outc);
207             } else {
208                 tputs(tparm2(set_a_background, z), 1, outc);
209                 napms(s_option);
210             }
211         } else if (VALID_STRING(exit_attribute_mode)
212                    && VALID_STRING(enter_reverse_mode)) {
213             if (ranf() <= 0.01) {
214                 outs((ranf() > 0.6)
215                      ? enter_reverse_mode
216                      : exit_attribute_mode);
217                 napms(s_option);
218             }
219         }
220         outc(p);
221         fflush(stdout);
222         ++total_chars;
223     }
224     cleanup();
225     ExitProgram(EXIT_SUCCESS);
226 }
227 #else
228 int
229 main(int argc GCC_UNUSED,
230      char *argv[]GCC_UNUSED)
231 {
232     fprintf(stderr, "This program requires terminfo\n");
233     exit(EXIT_FAILURE);
234 }
235 #endif