1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 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 ****************************************************************************/
31 * Author: Thomas E. Dickey
33 * $Id: dots_curses.c,v 1.21 2020/08/29 16:22:03 juergen Exp $
35 * A simple demo of the curses interface used for comparison with termcap.
37 #include <test.priv.h>
39 #if !defined(_NC_WINDOWS)
45 static bool interrupted = FALSE;
46 static long total_chars = 0;
47 static time_t started;
55 fprintf(stderr, "\n\n%ld total cells, rate %.2f/sec\n",
57 ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
61 onsig(int n GCC_UNUSED)
69 long r = (rand() & 077777);
70 return ((double) r / 32768.);
74 mypair(int fg, int bg)
76 int pair = (fg * COLORS) + bg;
77 return (pair >= COLOR_PAIRS) ? -1 : pair;
81 set_colors(int fg, int bg)
83 int pair = mypair(fg, bg);
85 attron(COLOR_PAIR(mypair(fg, bg)));
92 static const char *msg[] =
94 "Usage: dots_curses [options]"
97 ," -T TERM override $TERM"
98 #if HAVE_USE_DEFAULT_COLORS
99 ," -d invoke use_default_colors()"
102 ," -e allow environment $LINES / $COLUMNS"
104 ," -m SIZE set margin (default: 2)"
105 ," -r SECS self-interrupt/exit after specified number of seconds"
106 ," -s MSECS delay 1% of the time (default: 1 msecs)"
110 for (n = 0; n < SIZEOF(msg); n++)
111 fprintf(stderr, "%s\n", msg[n]);
113 ExitProgram(EXIT_FAILURE);
117 main(int argc, char *argv[])
123 #if HAVE_USE_DEFAULT_COLORS
124 bool d_option = FALSE;
132 while ((ch = getopt(argc, argv, "T:dem:r:s:")) != -1) {
135 need = 6 + strlen(optarg);
136 my_env = malloc(need);
137 _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
140 #if HAVE_USE_DEFAULT_COLORS
151 m_option = atoi(optarg);
154 r_option = atoi(optarg);
157 s_option = atoi(optarg);
165 srand((unsigned) time(0));
167 SetupAlarm(r_option);
168 InitAndCatch(initscr(), onsig);
172 #if HAVE_USE_DEFAULT_COLORS
174 use_default_colors();
176 for (fg = 0; fg < COLORS; fg++) {
177 for (bg = 0; bg < COLORS; bg++) {
181 ExitProgram(EXIT_FAILURE);
183 pair = mypair(fg, bg);
185 init_pair((short) pair, (short) fg, (short) bg);
190 r = (double) (LINES - (m_option * 2));
191 c = (double) (COLS - (m_option * 2));
192 started = time((time_t *) 0);
196 while (!interrupted) {
197 int x = (int) (c * ranf()) + m_option;
198 int y = (int) (r * ranf()) + m_option;
199 int p = (ranf() > 0.9) ? '*' : ' ';
203 int z = (int) (ranf() * COLORS);
205 set_colors(fg = z, bg);
206 attron(COLOR_PAIR(mypair(fg, bg)));
208 set_colors(fg, bg = z);
213 if (ranf() <= 0.01) {
228 ExitProgram(EXIT_SUCCESS);