blob: 05aa8545100cee50a91d598c39ad4977c5492acf [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2019-2022,2023 Thomas E. Dickey *
3 * Copyright 1999-2013,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
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 *
micky3879b9f5e72025-07-08 18:04:53 -040033 * $Id: dots.c,v 1.45 2023/01/07 17:21:48 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010034 *
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
Steve Kondikae271bc2015-11-15 02:50:53 +010044static bool interrupted = FALSE;
45static long total_chars = 0;
46static time_t started;
47
48static
49TPUTS_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
63static bool
64outs(const char *s)
65{
micky3879b9f5e72025-07-08 18:04:53 -040066 if (VALID_STRING(s)) {
Steve Kondikae271bc2015-11-15 02:50:53 +010067 tputs(s, 1, outc);
68 return TRUE;
69 }
70 return FALSE;
71}
72
73static void
74cleanup(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
micky3879b9f5e72025-07-08 18:04:53 -040082 fflush(stdout);
83 fprintf(stderr, "\n\n%ld total cells, rate %.2f/sec\n",
84 total_chars,
85 ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
Steve Kondikae271bc2015-11-15 02:50:53 +010086}
87
88static void
89onsig(int n GCC_UNUSED)
90{
91 interrupted = TRUE;
92}
93
94static double
95ranf(void)
96{
97 long r = (rand() & 077777);
98 return ((double) r / 32768.);
99}
100
micky3879b9f5e72025-07-08 18:04:53 -0400101static int
102get_number(NCURSES_CONST char *cap, int map)
Steve Kondikae271bc2015-11-15 02:50:53 +0100103{
micky3879b9f5e72025-07-08 18:04:53 -0400104 int result = map;
105 if (cap != 0) {
106 int check = tigetnum(cap);
107 if (check > 0)
108 result = check;
109 }
110 return result;
111}
112
113static void
114usage(int ok)
115{
116 static const char *msg[] =
117 {
118 "Usage: dots [options]"
119 ,""
120 ,USAGE_COMMON
121 ,"Options:"
122 ," -T TERM override $TERM"
123#if HAVE_USE_ENV
124 ," -e allow environment $LINES / $COLUMNS"
125#endif
126 ," -f use tigetnum rather than <term.h> mapping"
127 ," -m SIZE set margin (default: 2)"
128 ," -r SECS self-interrupt/exit after specified number of seconds"
129 ," -s MSECS delay 1% of the time (default: 1 msecs)"
130 };
131 size_t n;
132
133 for (n = 0; n < SIZEOF(msg); n++)
134 fprintf(stderr, "%s\n", msg[n]);
135
136 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
137}
138/* *INDENT-OFF* */
139VERSION_COMMON()
140/* *INDENT-ON* */
141
142int
143main(int argc, char *argv[])
144{
145 int ch;
Steve Kondikae271bc2015-11-15 02:50:53 +0100146 double r;
147 double c;
148 int my_colors;
micky3879b9f5e72025-07-08 18:04:53 -0400149 int f_option = 0;
150 int m_option = 2;
151 int r_option = 0;
152 int s_option = 1;
153 size_t need;
154 char *my_env;
Steve Kondikae271bc2015-11-15 02:50:53 +0100155
micky3879b9f5e72025-07-08 18:04:53 -0400156 while ((ch = getopt(argc, argv, OPTS_COMMON "T:efm:r:s:")) != -1) {
157 switch (ch) {
158 case 'T':
159 need = 6 + strlen(optarg);
160 if ((my_env = malloc(need)) != NULL) {
161 _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
162 putenv(my_env);
163 }
164 break;
165#if HAVE_USE_ENV
166 case 'e':
167 use_env(TRUE);
168 break;
169#endif
170 case 'f':
171 f_option = 1;
172 break;
173 case 'm':
174 m_option = atoi(optarg);
175 break;
176 case 'r':
177 r_option = atoi(optarg);
178 break;
179 case 's':
180 s_option = atoi(optarg);
181 break;
182 case OPTS_VERSION:
183 show_version(argv);
184 ExitProgram(EXIT_SUCCESS);
185 default:
186 usage(ch == OPTS_USAGE);
187 /* NOTREACHED */
188 }
189 }
190
191 SetupAlarm(r_option);
192 InitAndCatch(setupterm((char *) 0, 1, (int *) 0), onsig);
Steve Kondikae271bc2015-11-15 02:50:53 +0100193
194 srand((unsigned) time(0));
micky3879b9f5e72025-07-08 18:04:53 -0400195
Steve Kondikae271bc2015-11-15 02:50:53 +0100196 outs(clear_screen);
197 outs(cursor_invisible);
micky3879b9f5e72025-07-08 18:04:53 -0400198
199#define GetNumber(ln,sn) get_number(f_option ? #sn : 0, ln)
200 my_colors = GetNumber(max_colors, colors);
Steve Kondikae271bc2015-11-15 02:50:53 +0100201 if (my_colors > 1) {
micky3879b9f5e72025-07-08 18:04:53 -0400202 if (!VALID_STRING(set_a_foreground)
203 || !VALID_STRING(set_a_background)
204 || (!VALID_STRING(orig_colors) && !VALID_STRING(orig_pair)))
Steve Kondikae271bc2015-11-15 02:50:53 +0100205 my_colors = -1;
206 }
207
micky3879b9f5e72025-07-08 18:04:53 -0400208 r = (double) (GetNumber(lines, lines) - (m_option * 2));
209 c = (double) (GetNumber(columns, cols) - (m_option * 2));
Steve Kondikae271bc2015-11-15 02:50:53 +0100210 started = time((time_t *) 0);
211
212 while (!interrupted) {
micky3879b9f5e72025-07-08 18:04:53 -0400213 int x = (int) (c * ranf()) + m_option;
214 int y = (int) (r * ranf()) + m_option;
215 int p = (ranf() > 0.9) ? '*' : ' ';
Steve Kondikae271bc2015-11-15 02:50:53 +0100216
217 tputs(tparm3(cursor_address, y, x), 1, outc);
218 if (my_colors > 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400219 int z = (int) (ranf() * my_colors);
Steve Kondikae271bc2015-11-15 02:50:53 +0100220 if (ranf() > 0.01) {
221 tputs(tparm2(set_a_foreground, z), 1, outc);
222 } else {
223 tputs(tparm2(set_a_background, z), 1, outc);
micky3879b9f5e72025-07-08 18:04:53 -0400224 if (s_option)
225 napms(s_option);
Steve Kondikae271bc2015-11-15 02:50:53 +0100226 }
micky3879b9f5e72025-07-08 18:04:53 -0400227 } else if (VALID_STRING(exit_attribute_mode)
228 && VALID_STRING(enter_reverse_mode)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100229 if (ranf() <= 0.01) {
230 outs((ranf() > 0.6)
231 ? enter_reverse_mode
232 : exit_attribute_mode);
micky3879b9f5e72025-07-08 18:04:53 -0400233 if (s_option)
234 napms(s_option);
Steve Kondikae271bc2015-11-15 02:50:53 +0100235 }
236 }
237 outc(p);
238 fflush(stdout);
239 ++total_chars;
240 }
241 cleanup();
242 ExitProgram(EXIT_SUCCESS);
243}
244#else
245int
micky3879b9f5e72025-07-08 18:04:53 -0400246main(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100247{
248 fprintf(stderr, "This program requires terminfo\n");
249 exit(EXIT_FAILURE);
250}
251#endif