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