1 /****************************************************************************
2 * Copyright (c) 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_xcurses.c,v 1.14 2017/12/09 21:04:41 tom Exp $
34 * A simple demo of the wide-curses interface used for comparison with termcap.
36 #include <test.priv.h>
38 #if !defined(__MINGW32__)
47 #define NewPair(n) x_option ? ((void *)&(n)) : NULL
49 #define NewPair(n) NULL
52 #define InitPair(p,fg,bg) init_pair((short) (p), (short) (fg), (short) (bg))
54 static bool interrupted = FALSE;
55 static long total_chars = 0;
56 static time_t started;
59 static bool x_option = FALSE;
67 printf("\n\n%ld total cells, rate %.2f/sec\n",
69 ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
73 onsig(int n GCC_UNUSED)
81 long r = (rand() & 077777);
82 return ((double) r / 32768.);
86 mypair(int fg, int bg)
91 result = alloc_pair(fg, bg);
95 int pair = (fg * COLORS) + bg;
96 result = (pair >= COLOR_PAIRS) ? -1 : pair;
102 set_colors(int fg, int bg)
104 int pair = mypair(fg, bg);
106 color_set((short) pair, NewPair(pair));
113 static const char *msg[] =
115 "Usage: dots_xcurses [options]"
118 ," -T TERM override $TERM"
119 #if HAVE_USE_DEFAULT_COLORS
120 ," -d invoke use_default_colors()"
123 ," -e allow environment $LINES / $COLUMNS"
125 ," -m SIZE set margin (default: 2)"
126 ," -s MSECS delay 1% of the time (default: 1 msecs)"
128 ," -x use alloc_pair() rather than init_pair()"
133 for (n = 0; n < SIZEOF(msg); n++)
134 fprintf(stderr, "%s\n", msg[n]);
136 ExitProgram(EXIT_FAILURE);
140 main(int argc, char *argv[])
148 #if HAVE_USE_DEFAULT_COLORS
149 bool d_option = FALSE;
154 while ((ch = getopt(argc, argv, "T:dem:s:x")) != -1) {
157 putenv(strcat(strcpy(malloc(6 + strlen(optarg)), "TERM="), optarg));
159 #if HAVE_USE_DEFAULT_COLORS
170 m_option = atoi(optarg);
173 s_option = atoi(optarg);
186 srand((unsigned) time(0));
188 InitAndCatch(initscr(), onsig);
191 #if HAVE_USE_DEFAULT_COLORS
193 use_default_colors();
201 for (fg = 0; fg < COLORS; fg++) {
202 for (bg = 0; bg < COLORS; bg++) {
205 ExitProgram(EXIT_FAILURE);
207 pair = mypair(fg, bg);
209 InitPair(pair, fg, bg);
216 r = (double) (LINES - (2 * m_option));
217 c = (double) (COLS - (2 * m_option));
218 started = time((time_t *) 0);
224 while (!interrupted) {
225 x = (int) (c * ranf()) + m_option;
226 y = (int) (r * ranf()) + m_option;
227 p = (ranf() > 0.9) ? '*' : ' ';
231 z = (int) (ranf() * COLORS);
233 set_colors(fg = z, bg);
235 set_colors(fg, bg = z);
239 if (ranf() <= 0.01) {
241 attr_on(WA_REVERSE, NULL);
243 attr_off(WA_REVERSE, NULL);
248 wch[0] = (wchar_t) p;
254 ExitProgram(EXIT_SUCCESS);
261 printf("This program requires the wide-ncurses library\n");
262 ExitProgram(EXIT_FAILURE);