1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 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_xcurses.c,v 1.19 2020/02/02 23:34:34 tom Exp $
35 * A simple demo of the wide-curses interface used for comparison with termcap.
37 #include <test.priv.h>
48 #define NewPair(n) x_option ? ((void *)&(n)) : NULL
50 #define NewPair(n) NULL
53 #define InitPair(p,fg,bg) init_pair((short) (p), (short) (fg), (short) (bg))
55 static bool interrupted = FALSE;
56 static long total_chars = 0;
57 static time_t started;
60 static bool x_option = FALSE;
68 printf("\n\n%ld total cells, rate %.2f/sec\n",
70 ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
74 onsig(int n GCC_UNUSED)
82 long r = (rand() & 077777);
83 return ((double) r / 32768.);
87 mypair(int fg, int bg)
92 result = alloc_pair(fg, bg);
96 int pair = (fg * COLORS) + bg;
97 result = (pair >= COLOR_PAIRS) ? -1 : pair;
103 set_colors(int fg, int bg)
105 int pair = mypair(fg, bg);
107 (void) color_set((short) pair, NewPair(pair));
114 static const char *msg[] =
116 "Usage: dots_xcurses [options]"
119 ," -T TERM override $TERM"
120 #if HAVE_USE_DEFAULT_COLORS
121 ," -d invoke use_default_colors()"
124 ," -e allow environment $LINES / $COLUMNS"
126 ," -m SIZE set margin (default: 2)"
127 ," -s MSECS delay 1% of the time (default: 1 msecs)"
129 ," -x use alloc_pair() rather than init_pair()"
134 for (n = 0; n < SIZEOF(msg); n++)
135 fprintf(stderr, "%s\n", msg[n]);
137 ExitProgram(EXIT_FAILURE);
141 main(int argc, char *argv[])
147 #if HAVE_USE_DEFAULT_COLORS
148 bool d_option = FALSE;
155 while ((ch = getopt(argc, argv, "T:dem:s:x")) != -1) {
158 need = 6 + strlen(optarg);
159 my_env = malloc(need);
160 _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
163 #if HAVE_USE_DEFAULT_COLORS
174 m_option = atoi(optarg);
177 s_option = atoi(optarg);
190 srand((unsigned) time(0));
192 InitAndCatch(initscr(), onsig);
195 #if HAVE_USE_DEFAULT_COLORS
197 use_default_colors();
205 for (fg = 0; fg < COLORS; fg++) {
206 for (bg = 0; bg < COLORS; bg++) {
210 ExitProgram(EXIT_FAILURE);
212 pair = mypair(fg, bg);
214 InitPair(pair, fg, bg);
221 r = (double) (LINES - (2 * m_option));
222 c = (double) (COLS - (2 * m_option));
223 started = time((time_t *) 0);
228 while (!interrupted) {
229 int x = (int) (c * ranf()) + m_option;
230 int y = (int) (r * ranf()) + m_option;
231 int p = (ranf() > 0.9) ? '*' : ' ';
235 int z = (int) (ranf() * COLORS);
237 set_colors(fg = z, bg);
239 set_colors(fg, bg = z);
243 if (ranf() <= 0.01) {
245 attr_on(WA_REVERSE, NULL);
247 attr_off(WA_REVERSE, NULL);
252 wch[0] = (wchar_t) p;
258 ExitProgram(EXIT_SUCCESS);
265 printf("This program requires the wide-ncurses library\n");
266 ExitProgram(EXIT_FAILURE);