1 /****************************************************************************
2 * Copyright 2019-2022,2023 Thomas E. Dickey *
3 * Copyright 2006-2014,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 * $Id: echochar.c,v 1.26 2023/05/27 20:13:10 tom Exp $
32 * Demonstrate the echochar function (compare to dots.c).
33 * Thomas Dickey - 2006/11/4
36 #include <test.priv.h>
40 static bool interrupted = FALSE;
41 static long total_chars = 0;
42 static time_t started;
49 printf("\n\n%ld total cells, rate %.2f/sec\n",
51 ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
55 onsig(int n GCC_UNUSED)
63 long r = (rand() & 077777);
64 return ((double) r / 32768.);
68 set_color(const char *const my_pairs, int fg, int bg)
70 int pair = (fg * COLORS) + bg;
71 if (pair < COLOR_PAIRS) {
72 if (!my_pairs[pair]) {
73 init_pair((short) pair,
77 attron(COLOR_PAIR(pair));
84 static const char *msg[] =
90 ," -r use addch/refresh rather than echochar()"
94 for (n = 0; n < SIZEOF(msg); n++)
95 fprintf(stderr, "%s\n", msg[n]);
97 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
104 main(int argc, char *argv[])
115 while ((ch = getopt(argc, argv, OPTS_COMMON "r")) != -1) {
122 ExitProgram(EXIT_SUCCESS);
124 usage(ch == OPTS_USAGE);
129 InitAndCatch(initscr(), onsig);
131 use_colors = has_colors();
134 if (COLOR_PAIRS > 0) {
135 my_pairs = typeCalloc(char, (size_t) COLOR_PAIRS);
137 use_colors = (my_pairs != 0);
140 srand((unsigned) time(0));
144 r = (double) (LINES - 4);
145 c = (double) (COLS - 4);
146 started = time((time_t *) 0);
148 while (!interrupted) {
149 int x = (int) (c * ranf()) + 2;
150 int y = (int) (r * ranf()) + 2;
151 int p = (ranf() > 0.9) ? '*' : ' ';
154 if (use_colors > 0) {
155 int z = (int) (ranf() * COLORS);
157 set_color(my_pairs, z, last_bg);
160 set_color(my_pairs, last_fg, z);
165 if (ranf() <= 0.01) {
183 ExitProgram(EXIT_SUCCESS);