]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_xcurses.c
ncurses 6.2 - patch 20200509
[ncurses.git] / test / dots_xcurses.c
1 /****************************************************************************
2  * Copyright 2018-2019,2020 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.22 2020/05/10 00:32:33 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(_WIN32)
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(void)
114 {
115     static const char *msg[] =
116     {
117         "Usage: dots_xcurses [options]"
118         ,""
119         ,"Options:"
120         ," -T TERM  override $TERM"
121 #if HAVE_USE_DEFAULT_COLORS
122         ," -d       invoke use_default_colors()"
123 #endif
124 #if HAVE_USE_ENV
125         ," -e       allow environment $LINES / $COLUMNS"
126 #endif
127         ," -m SIZE  set margin (default: 2)"
128         ," -r SECS  self-interrupt/exit after specified number of seconds"
129         ," -s MSECS delay 1% of the time (default: 1 msecs)"
130 #if HAVE_ALLOC_PAIR
131         ," -x       use alloc_pair() rather than init_pair()"
132 #endif
133     };
134     size_t n;
135
136     for (n = 0; n < SIZEOF(msg); n++)
137         fprintf(stderr, "%s\n", msg[n]);
138
139     ExitProgram(EXIT_FAILURE);
140 }
141
142 int
143 main(int argc, char *argv[])
144 {
145     int fg, bg, ch;
146     wchar_t wch[2];
147     double r;
148     double c;
149 #if HAVE_USE_DEFAULT_COLORS
150     bool d_option = FALSE;
151 #endif
152     int m_option = 2;
153     int r_option = 0;
154     int s_option = 1;
155     size_t need;
156     char *my_env;
157
158     while ((ch = getopt(argc, argv, "T:dem:r:s:x")) != -1) {
159         switch (ch) {
160         case 'T':
161             need = 6 + strlen(optarg);
162             my_env = malloc(need);
163             _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
164             putenv(my_env);
165             break;
166 #if HAVE_USE_DEFAULT_COLORS
167         case 'd':
168             d_option = TRUE;
169             break;
170 #endif
171 #if HAVE_USE_ENV
172         case 'e':
173             use_env(TRUE);
174             break;
175 #endif
176         case 'm':
177             m_option = atoi(optarg);
178             break;
179         case 'r':
180             r_option = atoi(optarg);
181             break;
182         case 's':
183             s_option = atoi(optarg);
184             break;
185 #if HAVE_ALLOC_PAIR
186         case 'x':
187             x_option = TRUE;
188             break;
189 #endif
190         default:
191             usage();
192             break;
193         }
194     }
195
196     srand((unsigned) time(0));
197
198     SetupAlarm(r_option);
199     InitAndCatch(initscr(), onsig);
200     if (has_colors()) {
201         start_color();
202 #if HAVE_USE_DEFAULT_COLORS
203         if (d_option)
204             use_default_colors();
205 #endif
206 #if HAVE_ALLOC_PAIR
207         if (x_option) {
208             ;                   /* nothing */
209         } else
210 #endif
211         {
212             for (fg = 0; fg < COLORS; fg++) {
213                 for (bg = 0; bg < COLORS; bg++) {
214                     int pair;
215                     if (interrupted) {
216                         cleanup();
217                         ExitProgram(EXIT_FAILURE);
218                     }
219                     pair = mypair(fg, bg);
220                     if (pair > 0) {
221                         InitPair(pair, fg, bg);
222                     }
223                 }
224             }
225         }
226     }
227
228     r = (double) (LINES - (2 * m_option));
229     c = (double) (COLS - (2 * m_option));
230     started = time((time_t *) 0);
231
232     fg = COLOR_WHITE;
233     bg = COLOR_BLACK;
234     wch[1] = 0;
235     while (!interrupted) {
236         int x = (int) (c * ranf()) + m_option;
237         int y = (int) (r * ranf()) + m_option;
238         int p = (ranf() > 0.9) ? '*' : ' ';
239
240         move(y, x);
241         if (has_colors()) {
242             int z = (int) (ranf() * COLORS);
243             if (ranf() > 0.01) {
244                 set_colors(fg = z, bg);
245             } else {
246                 set_colors(fg, bg = z);
247                 napms(s_option);
248             }
249         } else {
250             if (ranf() <= 0.01) {
251                 if (ranf() > 0.6) {
252                     attr_on(WA_REVERSE, NULL);
253                 } else {
254                     attr_off(WA_REVERSE, NULL);
255                 }
256                 napms(s_option);
257             }
258         }
259         wch[0] = (wchar_t) p;
260         addnwstr(wch, 1);
261         refresh();
262         ++total_chars;
263     }
264     cleanup();
265     ExitProgram(EXIT_SUCCESS);
266 }
267
268 #else
269 int
270 main(void)
271 {
272     printf("This program requires the wide-ncurses library\n");
273     ExitProgram(EXIT_FAILURE);
274 }
275 #endif