]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots.c
ncurses 6.1 - patch 20181215
[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.33 2017/11/24 19:26:31 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
147     while ((x = getopt(argc, argv, "T:efm:s:")) != -1) {
148         switch (x) {
149         case 'T':
150             putenv(strcat(strcpy(malloc(6 + strlen(optarg)), "TERM="), optarg));
151             break;
152 #if HAVE_USE_ENV
153         case 'e':
154             use_env(TRUE);
155             break;
156 #endif
157         case 'f':
158             f_option = 1;
159             break;
160         case 'm':
161             m_option = atoi(optarg);
162             break;
163         case 's':
164             s_option = atoi(optarg);
165             break;
166         default:
167             usage();
168             break;
169         }
170     }
171
172     InitAndCatch(setupterm((char *) 0, 1, (int *) 0), onsig);
173
174     srand((unsigned) time(0));
175
176     outs(clear_screen);
177     outs(cursor_invisible);
178
179 #define GetNumber(ln,sn) get_number(f_option ? #sn : 0, ln)
180     my_colors = GetNumber(max_colors, colors);
181     if (my_colors > 1) {
182         if (!VALID_STRING(set_a_foreground)
183             || !VALID_STRING(set_a_background)
184             || (!VALID_STRING(orig_colors) && !VALID_STRING(orig_pair)))
185             my_colors = -1;
186     }
187
188     r = (double) (GetNumber(lines, lines) - (m_option * 2));
189     c = (double) (GetNumber(columns, cols) - (m_option * 2));
190     started = time((time_t *) 0);
191
192     while (!interrupted) {
193         x = (int) (c * ranf()) + m_option;
194         y = (int) (r * ranf()) + m_option;
195         p = (ranf() > 0.9) ? '*' : ' ';
196
197         tputs(tparm3(cursor_address, y, x), 1, outc);
198         if (my_colors > 0) {
199             z = (int) (ranf() * my_colors);
200             if (ranf() > 0.01) {
201                 tputs(tparm2(set_a_foreground, z), 1, outc);
202             } else {
203                 tputs(tparm2(set_a_background, z), 1, outc);
204                 napms(s_option);
205             }
206         } else if (VALID_STRING(exit_attribute_mode)
207                    && VALID_STRING(enter_reverse_mode)) {
208             if (ranf() <= 0.01) {
209                 outs((ranf() > 0.6)
210                      ? enter_reverse_mode
211                      : exit_attribute_mode);
212                 napms(s_option);
213             }
214         }
215         outc(p);
216         fflush(stdout);
217         ++total_chars;
218     }
219     cleanup();
220     ExitProgram(EXIT_SUCCESS);
221 }
222 #else
223 int
224 main(int argc GCC_UNUSED,
225      char *argv[]GCC_UNUSED)
226 {
227     fprintf(stderr, "This program requires terminfo\n");
228     exit(EXIT_FAILURE);
229 }
230 #endif