]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots.c
d34473c1bb7e50f9fd16af350a6b6eb330cb3edb
[ncurses.git] / test / dots.c
1 /****************************************************************************
2  * Copyright 2019,2020 Thomas E. Dickey                                     *
3  * Copyright 1999-2013,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /*
31  * Author: Thomas E. Dickey <dickey@clark.net> 1999
32  *
33  * $Id: dots.c,v 1.36 2020/02/02 23:34:34 tom Exp $
34  *
35  * A simple demo of the terminfo interface.
36  */
37 #define USE_TINFO
38 #include <test.priv.h>
39
40 #if HAVE_SETUPTERM
41
42 #include <time.h>
43
44 static bool interrupted = FALSE;
45 static long total_chars = 0;
46 static time_t started;
47
48 static
49 TPUTS_PROTO(outc, c)
50 {
51     int rc = c;
52
53     if (interrupted) {
54         char tmp = (char) c;
55         if (write(STDOUT_FILENO, &tmp, (size_t) 1) == -1)
56             rc = EOF;
57     } else {
58         rc = putc(c, stdout);
59     }
60     TPUTS_RETURN(rc);
61 }
62
63 static bool
64 outs(const char *s)
65 {
66     if (VALID_STRING(s)) {
67         tputs(s, 1, outc);
68         return TRUE;
69     }
70     return FALSE;
71 }
72
73 static void
74 cleanup(void)
75 {
76     outs(exit_attribute_mode);
77     if (!outs(orig_colors))
78         outs(orig_pair);
79     outs(clear_screen);
80     outs(cursor_normal);
81
82     printf("\n\n%ld total cells, rate %.2f/sec\n",
83            total_chars,
84            ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
85 }
86
87 static void
88 onsig(int n GCC_UNUSED)
89 {
90     interrupted = TRUE;
91 }
92
93 static double
94 ranf(void)
95 {
96     long r = (rand() & 077777);
97     return ((double) r / 32768.);
98 }
99
100 static int
101 get_number(NCURSES_CONST char *cap, int map)
102 {
103     int result = map;
104     if (cap != 0) {
105         int check = tigetnum(cap);
106         if (check > 0)
107             result = check;
108     }
109     return result;
110 }
111
112 static void
113 usage(void)
114 {
115     static const char *msg[] =
116     {
117         "Usage: dots [options]"
118         ,""
119         ,"Options:"
120         ," -T TERM  override $TERM"
121 #if HAVE_USE_ENV
122         ," -e       allow environment $LINES / $COLUMNS"
123 #endif
124         ," -f       use tigetnum rather than <term.h> mapping"
125         ," -m SIZE  set margin (default: 2)"
126         ," -s MSECS delay 1% of the time (default: 1 msecs)"
127     };
128     size_t n;
129
130     for (n = 0; n < SIZEOF(msg); n++)
131         fprintf(stderr, "%s\n", msg[n]);
132
133     ExitProgram(EXIT_FAILURE);
134 }
135
136 int
137 main(int argc,
138      char *argv[])
139 {
140     int ch;
141     double r;
142     double c;
143     int my_colors;
144     int f_option = 0;
145     int m_option = 2;
146     int s_option = 1;
147     size_t need;
148     char *my_env;
149
150     while ((ch = getopt(argc, argv, "T:efm:s:")) != -1) {
151         switch (ch) {
152         case 'T':
153             need = 6 + strlen(optarg);
154             my_env = malloc(need);
155             _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
156             putenv(my_env);
157             break;
158 #if HAVE_USE_ENV
159         case 'e':
160             use_env(TRUE);
161             break;
162 #endif
163         case 'f':
164             f_option = 1;
165             break;
166         case 'm':
167             m_option = atoi(optarg);
168             break;
169         case 's':
170             s_option = atoi(optarg);
171             break;
172         default:
173             usage();
174             break;
175         }
176     }
177
178     InitAndCatch(setupterm((char *) 0, 1, (int *) 0), onsig);
179
180     srand((unsigned) time(0));
181
182     outs(clear_screen);
183     outs(cursor_invisible);
184
185 #define GetNumber(ln,sn) get_number(f_option ? #sn : 0, ln)
186     my_colors = GetNumber(max_colors, colors);
187     if (my_colors > 1) {
188         if (!VALID_STRING(set_a_foreground)
189             || !VALID_STRING(set_a_background)
190             || (!VALID_STRING(orig_colors) && !VALID_STRING(orig_pair)))
191             my_colors = -1;
192     }
193
194     r = (double) (GetNumber(lines, lines) - (m_option * 2));
195     c = (double) (GetNumber(columns, cols) - (m_option * 2));
196     started = time((time_t *) 0);
197
198     while (!interrupted) {
199         int x = (int) (c * ranf()) + m_option;
200         int y = (int) (r * ranf()) + m_option;
201         int p = (ranf() > 0.9) ? '*' : ' ';
202
203         tputs(tparm3(cursor_address, y, x), 1, outc);
204         if (my_colors > 0) {
205             int z = (int) (ranf() * my_colors);
206             if (ranf() > 0.01) {
207                 tputs(tparm2(set_a_foreground, z), 1, outc);
208             } else {
209                 tputs(tparm2(set_a_background, z), 1, outc);
210                 napms(s_option);
211             }
212         } else if (VALID_STRING(exit_attribute_mode)
213                    && VALID_STRING(enter_reverse_mode)) {
214             if (ranf() <= 0.01) {
215                 outs((ranf() > 0.6)
216                      ? enter_reverse_mode
217                      : exit_attribute_mode);
218                 napms(s_option);
219             }
220         }
221         outc(p);
222         fflush(stdout);
223         ++total_chars;
224     }
225     cleanup();
226     ExitProgram(EXIT_SUCCESS);
227 }
228 #else
229 int
230 main(int argc GCC_UNUSED,
231      char *argv[]GCC_UNUSED)
232 {
233     fprintf(stderr, "This program requires terminfo\n");
234     exit(EXIT_FAILURE);
235 }
236 #endif