1 /****************************************************************************
2 * Copyright (c) 2014-2018,2019 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
30 * Author: Thomas E. Dickey
32 * $Id: dots_curses.c,v 1.15 2019/08/24 21:50:35 tom Exp $
34 * A simple demo of the curses interface used for comparison with termcap.
36 #include <test.priv.h>
44 static bool interrupted = FALSE;
45 static long total_chars = 0;
46 static time_t started;
53 printf("\n\n%ld total cells, rate %.2f/sec\n",
55 ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
59 onsig(int n GCC_UNUSED)
67 long r = (rand() & 077777);
68 return ((double) r / 32768.);
72 mypair(int fg, int bg)
74 int pair = (fg * COLORS) + bg;
75 return (pair >= COLOR_PAIRS) ? -1 : pair;
79 set_colors(int fg, int bg)
81 int pair = mypair(fg, bg);
83 attron(COLOR_PAIR(mypair(fg, bg)));
90 static const char *msg[] =
92 "Usage: dots_curses [options]"
95 ," -T TERM override $TERM"
96 #if HAVE_USE_DEFAULT_COLORS
97 ," -d invoke use_default_colors()"
100 ," -e allow environment $LINES / $COLUMNS"
102 ," -m SIZE set margin (default: 2)"
103 ," -s MSECS delay 1% of the time (default: 1 msecs)"
107 for (n = 0; n < SIZEOF(msg); n++)
108 fprintf(stderr, "%s\n", msg[n]);
110 ExitProgram(EXIT_FAILURE);
114 main(int argc, char *argv[])
120 #if HAVE_USE_DEFAULT_COLORS
121 bool d_option = FALSE;
128 while ((ch = getopt(argc, argv, "T:dem:s:")) != -1) {
131 need = 6 + strlen(optarg);
132 my_env = malloc(need);
133 _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
136 #if HAVE_USE_DEFAULT_COLORS
147 m_option = atoi(optarg);
150 s_option = atoi(optarg);
158 srand((unsigned) time(0));
160 InitAndCatch(initscr(), onsig);
163 #if HAVE_USE_DEFAULT_COLORS
165 use_default_colors();
167 for (fg = 0; fg < COLORS; fg++) {
168 for (bg = 0; bg < COLORS; bg++) {
172 ExitProgram(EXIT_FAILURE);
174 pair = mypair(fg, bg);
176 init_pair((short) pair, (short) fg, (short) bg);
181 r = (double) (LINES - (m_option * 2));
182 c = (double) (COLS - (m_option * 2));
183 started = time((time_t *) 0);
187 while (!interrupted) {
188 int x = (int) (c * ranf()) + m_option;
189 int y = (int) (r * ranf()) + m_option;
190 int p = (ranf() > 0.9) ? '*' : ' ';
194 int z = (int) (ranf() * COLORS);
196 set_colors(fg = z, bg);
197 attron(COLOR_PAIR(mypair(fg, bg)));
199 set_colors(fg, bg = z);
203 if (ranf() <= 0.01) {
217 ExitProgram(EXIT_SUCCESS);