]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_xcurses.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / dots_xcurses.c
1 /****************************************************************************
2  * Copyright 2018-2022,2023 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.29 2023/01/07 17:21:48 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             if ((my_env = malloc(need)) != NULL) {
167                 _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
168                 putenv(my_env);
169             }
170             break;
171 #if HAVE_USE_DEFAULT_COLORS
172         case 'd':
173             d_option = TRUE;
174             break;
175 #endif
176 #if HAVE_USE_ENV
177         case 'e':
178             use_env(TRUE);
179             break;
180 #endif
181         case 'm':
182             m_option = atoi(optarg);
183             break;
184         case 'r':
185             r_option = atoi(optarg);
186             break;
187         case 's':
188             s_option = atoi(optarg);
189             break;
190 #if HAVE_ALLOC_PAIR
191         case 'x':
192             x_option = TRUE;
193             break;
194 #endif
195         case OPTS_VERSION:
196             show_version(argv);
197             ExitProgram(EXIT_SUCCESS);
198         default:
199             usage(ch == OPTS_USAGE);
200             /* NOTREACHED */
201         }
202     }
203
204     setlocale(LC_ALL, "");
205     srand((unsigned) time(0));
206
207     SetupAlarm(r_option);
208     InitAndCatch(initscr(), onsig);
209     if (has_colors()) {
210         start_color();
211 #if HAVE_USE_DEFAULT_COLORS
212         if (d_option)
213             use_default_colors();
214 #endif
215 #if HAVE_ALLOC_PAIR
216         if (x_option) {
217             ;                   /* nothing */
218         } else
219 #endif
220         {
221             for (fg = 0; fg < COLORS; fg++) {
222                 for (bg = 0; bg < COLORS; bg++) {
223                     int pair;
224                     if (interrupted) {
225                         cleanup();
226                         ExitProgram(EXIT_FAILURE);
227                     }
228                     pair = mypair(fg, bg);
229                     if (pair > 0) {
230                         InitPair(pair, fg, bg);
231                     }
232                 }
233             }
234         }
235     }
236
237     r = (double) (LINES - (2 * m_option));
238     c = (double) (COLS - (2 * m_option));
239     started = time((time_t *) 0);
240
241     fg = COLOR_WHITE;
242     bg = COLOR_BLACK;
243     wch[1] = 0;
244     while (!interrupted) {
245         int x = (int) (c * ranf()) + m_option;
246         int y = (int) (r * ranf()) + m_option;
247         int p = (ranf() > 0.9) ? '*' : ' ';
248
249         move(y, x);
250         if (has_colors()) {
251             int z = (int) (ranf() * COLORS);
252             if (ranf() > 0.01) {
253                 set_colors(fg = z, bg);
254             } else {
255                 set_colors(fg, bg = z);
256                 if (s_option)
257                     napms(s_option);
258             }
259         } else {
260             if (ranf() <= 0.01) {
261                 if (ranf() > 0.6) {
262                     attr_on(WA_REVERSE, NULL);
263                 } else {
264                     attr_off(WA_REVERSE, NULL);
265                 }
266                 if (s_option)
267                     napms(s_option);
268             }
269         }
270         wch[0] = (wchar_t) p;
271         addnwstr(wch, 1);
272         refresh();
273         ++total_chars;
274     }
275     cleanup();
276     ExitProgram(EXIT_SUCCESS);
277 }
278
279 #else
280 int
281 main(void)
282 {
283     printf("This program requires the wide-ncurses library\n");
284     ExitProgram(EXIT_FAILURE);
285 }
286 #endif