]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_xcurses.c
ncurses 6.3 - patch 20221203
[ncurses.git] / test / dots_xcurses.c
1 /****************************************************************************
2  * Copyright 2018-2020,2022 Thomas E. Dickey                                *
3  * Copyright 2017 Free Software Foundation, Inc.                            *
4  *                                                                          *
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:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
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.                               *
23  *                                                                          *
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       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /*
31  * Author: Thomas E. Dickey
32  *
33  * $Id: dots_xcurses.c,v 1.28 2022/12/04 00:40:11 tom Exp $
34  *
35  * A simple demo of the wide-curses interface used for comparison with termcap.
36  */
37 #include <test.priv.h>
38
39 #if !defined(_NC_WINDOWS)
40 #include <sys/time.h>
41 #endif
42
43 #include <time.h>
44
45 #if USE_WIDEC_SUPPORT
46
47 #if HAVE_ALLOC_PAIR
48 #define NewPair(n) x_option ? ((void *)&(n)) : NULL
49 #else
50 #define NewPair(n) NULL
51 #endif
52
53 #define InitPair(p,fg,bg) init_pair((short) (p), (short) (fg), (short) (bg))
54
55 static bool interrupted = FALSE;
56 static long total_chars = 0;
57 static time_t started;
58
59 #if HAVE_ALLOC_PAIR
60 static bool x_option = FALSE;
61 #endif
62
63 static void
64 cleanup(void)
65 {
66     endwin();
67
68     fflush(stdout);
69     fprintf(stderr, "\n\n%ld total cells, rate %.2f/sec\n",
70             total_chars,
71             ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
72 }
73
74 static void
75 onsig(int n GCC_UNUSED)
76 {
77     interrupted = TRUE;
78 }
79
80 static double
81 ranf(void)
82 {
83     long r = (rand() & 077777);
84     return ((double) r / 32768.);
85 }
86
87 static int
88 mypair(int fg, int bg)
89 {
90     int result;
91 #if HAVE_ALLOC_PAIR
92     if (x_option) {
93         result = alloc_pair(fg, bg);
94     } else
95 #endif
96     {
97         int pair = (fg * COLORS) + bg;
98         result = (pair >= COLOR_PAIRS) ? -1 : pair;
99     }
100     return result;
101 }
102
103 static void
104 set_colors(int fg, int bg)
105 {
106     int pair = mypair(fg, bg);
107     if (pair > 0) {
108         (void) color_set((short) pair, NewPair(pair));
109     }
110 }
111
112 static void
113 usage(int ok)
114 {
115     static const char *msg[] =
116     {
117         "Usage: dots_xcurses [options]"
118         ,""
119         ,USAGE_COMMON
120         ,"Options:"
121         ," -T TERM  override $TERM"
122 #if HAVE_USE_DEFAULT_COLORS
123         ," -d       invoke use_default_colors()"
124 #endif
125 #if HAVE_USE_ENV
126         ," -e       allow environment $LINES / $COLUMNS"
127 #endif
128         ," -m SIZE  set margin (default: 2)"
129         ," -r SECS  self-interrupt/exit after specified number of seconds"
130         ," -s MSECS delay 1% of the time (default: 1 msecs)"
131 #if HAVE_ALLOC_PAIR
132         ," -x       use alloc_pair() rather than init_pair()"
133 #endif
134     };
135     size_t n;
136
137     for (n = 0; n < SIZEOF(msg); n++)
138         fprintf(stderr, "%s\n", msg[n]);
139
140     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
141 }
142 /* *INDENT-OFF* */
143 VERSION_COMMON()
144 /* *INDENT-ON* */
145
146 int
147 main(int argc, char *argv[])
148 {
149     int fg, bg, ch;
150     wchar_t wch[2];
151     double r;
152     double c;
153 #if HAVE_USE_DEFAULT_COLORS
154     bool d_option = FALSE;
155 #endif
156     int m_option = 2;
157     int r_option = 0;
158     int s_option = 1;
159     size_t need;
160     char *my_env;
161
162     while ((ch = getopt(argc, argv, OPTS_COMMON "T:dem:r:s:x")) != -1) {
163         switch (ch) {
164         case 'T':
165             need = 6 + strlen(optarg);
166             my_env = malloc(need);
167             _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
168             putenv(my_env);
169             break;
170 #if HAVE_USE_DEFAULT_COLORS
171         case 'd':
172             d_option = TRUE;
173             break;
174 #endif
175 #if HAVE_USE_ENV
176         case 'e':
177             use_env(TRUE);
178             break;
179 #endif
180         case 'm':
181             m_option = atoi(optarg);
182             break;
183         case 'r':
184             r_option = atoi(optarg);
185             break;
186         case 's':
187             s_option = atoi(optarg);
188             break;
189 #if HAVE_ALLOC_PAIR
190         case 'x':
191             x_option = TRUE;
192             break;
193 #endif
194         case OPTS_VERSION:
195             show_version(argv);
196             ExitProgram(EXIT_SUCCESS);
197         default:
198             usage(ch == OPTS_USAGE);
199             /* NOTREACHED */
200         }
201     }
202
203     setlocale(LC_ALL, "");
204     srand((unsigned) time(0));
205
206     SetupAlarm(r_option);
207     InitAndCatch(initscr(), onsig);
208     if (has_colors()) {
209         start_color();
210 #if HAVE_USE_DEFAULT_COLORS
211         if (d_option)
212             use_default_colors();
213 #endif
214 #if HAVE_ALLOC_PAIR
215         if (x_option) {
216             ;                   /* nothing */
217         } else
218 #endif
219         {
220             for (fg = 0; fg < COLORS; fg++) {
221                 for (bg = 0; bg < COLORS; bg++) {
222                     int pair;
223                     if (interrupted) {
224                         cleanup();
225                         ExitProgram(EXIT_FAILURE);
226                     }
227                     pair = mypair(fg, bg);
228                     if (pair > 0) {
229                         InitPair(pair, fg, bg);
230                     }
231                 }
232             }
233         }
234     }
235
236     r = (double) (LINES - (2 * m_option));
237     c = (double) (COLS - (2 * m_option));
238     started = time((time_t *) 0);
239
240     fg = COLOR_WHITE;
241     bg = COLOR_BLACK;
242     wch[1] = 0;
243     while (!interrupted) {
244         int x = (int) (c * ranf()) + m_option;
245         int y = (int) (r * ranf()) + m_option;
246         int p = (ranf() > 0.9) ? '*' : ' ';
247
248         move(y, x);
249         if (has_colors()) {
250             int z = (int) (ranf() * COLORS);
251             if (ranf() > 0.01) {
252                 set_colors(fg = z, bg);
253             } else {
254                 set_colors(fg, bg = z);
255                 if (s_option)
256                     napms(s_option);
257             }
258         } else {
259             if (ranf() <= 0.01) {
260                 if (ranf() > 0.6) {
261                     attr_on(WA_REVERSE, NULL);
262                 } else {
263                     attr_off(WA_REVERSE, NULL);
264                 }
265                 if (s_option)
266                     napms(s_option);
267             }
268         }
269         wch[0] = (wchar_t) p;
270         addnwstr(wch, 1);
271         refresh();
272         ++total_chars;
273     }
274     cleanup();
275     ExitProgram(EXIT_SUCCESS);
276 }
277
278 #else
279 int
280 main(void)
281 {
282     printf("This program requires the wide-ncurses library\n");
283     ExitProgram(EXIT_FAILURE);
284 }
285 #endif