1 /****************************************************************************
2 * Copyright (c) 2014,2017 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.10 2017/10/23 08:42:40 tom Exp $
34 * A simple demo of the curses interface used for comparison with termcap.
36 #include <test.priv.h>
38 #if !defined(__MINGW32__)
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()"
99 ," -e allow environment $LINES / $COLUMNS"
100 ," -m SIZE set margin (default: 2)"
101 ," -s MSECS delay 1% of the time (default: 1 msecs)"
105 for (n = 0; n < SIZEOF(msg); n++)
106 fprintf(stderr, "%s\n", msg[n]);
108 ExitProgram(EXIT_FAILURE);
112 main(int argc, char *argv[])
118 #if HAVE_USE_DEFAULT_COLORS
119 bool d_option = FALSE;
124 while ((x = getopt(argc, argv, "T:dem:s:")) != -1) {
127 putenv(strcat(strcpy(malloc(6 + strlen(optarg)), "TERM="), optarg));
129 #if HAVE_USE_DEFAULT_COLORS
140 m_option = atoi(optarg);
143 s_option = atoi(optarg);
151 srand((unsigned) time(0));
153 InitAndCatch(initscr(), onsig);
156 #if HAVE_USE_DEFAULT_COLORS
158 use_default_colors();
160 for (fg = 0; fg < COLORS; fg++) {
161 for (bg = 0; bg < COLORS; bg++) {
162 int pair = mypair(fg, bg);
164 init_pair((short) pair, (short) fg, (short) bg);
169 r = (double) (LINES - (m_option * 2));
170 c = (double) (COLS - (m_option * 2));
171 started = time((time_t *) 0);
175 while (!interrupted) {
176 x = (int) (c * ranf()) + m_option;
177 y = (int) (r * ranf()) + m_option;
178 p = (ranf() > 0.9) ? '*' : ' ';
182 z = (int) (ranf() * COLORS);
184 set_colors(fg = z, bg);
185 attron(COLOR_PAIR(mypair(fg, bg)));
187 set_colors(fg, bg = z);
191 if (ranf() <= 0.01) {
205 ExitProgram(EXIT_SUCCESS);