]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_xcurses.c
abf0a00ae030b4dfc03b3522290f0f1162f34c7a
[ncurses.git] / test / dots_xcurses.c
1 /****************************************************************************
2  * Copyright (c) 2017 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.8 2017/10/12 00:20:41 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(__MINGW32__)
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 #ifdef NCURSES_VERSION
59 static bool d_option = FALSE;
60 static bool x_option = FALSE;
61 #endif
62
63 static void
64 cleanup(void)
65 {
66     endwin();
67
68     printf("\n\n%ld total cells, rate %.2f/sec\n",
69            total_chars,
70            ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
71 }
72
73 static void
74 onsig(int n GCC_UNUSED)
75 {
76     interrupted = TRUE;
77 }
78
79 static double
80 ranf(void)
81 {
82     long r = (rand() & 077777);
83     return ((double) r / 32768.);
84 }
85
86 static int
87 mypair(int fg, int bg)
88 {
89     int result;
90 #if HAVE_ALLOC_PAIR
91     if (x_option) {
92         result = alloc_pair(fg, bg);
93     } else
94 #endif
95     {
96         int pair = (fg * COLORS) + bg;
97         result = (pair >= COLOR_PAIRS) ? -1 : pair;
98     }
99     return result;
100 }
101
102 static void
103 set_colors(int fg, int bg)
104 {
105     int pair = mypair(fg, bg);
106     if (pair > 0) {
107         color_set((short) pair, NewPair(pair));
108     }
109 }
110
111 #if defined(NCURSES_VERSION)
112 static void
113 usage(void)
114 {
115     static const char *msg[] =
116     {
117         "Usage: firework [options]"
118         ,""
119         ,"Options:"
120 #if HAVE_USE_DEFAULT_COLORS
121         ," -d       invoke use_default_colors()"
122 #endif
123 #if HAVE_ALLOC_PAIR
124         ," -x       use alloc_pair() rather than init_pair()"
125 #endif
126     };
127     size_t n;
128
129     for (n = 0; n < SIZEOF(msg); n++)
130         fprintf(stderr, "%s\n", msg[n]);
131
132     ExitProgram(EXIT_FAILURE);
133 }
134 #endif
135
136 int
137 main(int argc GCC_UNUSED,
138      char *argv[]GCC_UNUSED)
139 {
140     int margin = 2;
141     int x, y, z, p;
142     int fg, bg, ch;
143     wchar_t wch[2];
144     int pair;
145     double r;
146     double c;
147
148 #if defined(NCURSES_VERSION)
149     while ((ch = getopt(argc, argv, "dx")) != -1) {
150         switch (ch) {
151         case 'd':
152             d_option = TRUE;
153             break;
154 #if HAVE_ALLOC_PAIR
155         case 'x':
156             x_option = TRUE;
157             break;
158 #endif
159         default:
160             usage();
161             break;
162         }
163     }
164 #endif
165
166     srand((unsigned) time(0));
167
168     InitAndCatch(initscr(), onsig);
169     if (has_colors()) {
170         start_color();
171 #if HAVE_USE_DEFAULT_COLORS
172         if (d_option)
173             use_default_colors();
174 #endif
175         if (x_option) {
176             ;                   /* nothing */
177         } else {
178             for (fg = 0; fg < COLORS; fg++) {
179                 for (bg = 0; bg < COLORS; bg++) {
180                     pair = mypair(fg, bg);
181                     if (pair > 0) {
182                         InitPair(pair, fg, bg);
183                     }
184                 }
185             }
186         }
187     }
188
189     r = (double) (LINES - (2 * margin));
190     c = (double) (COLS - (2 * margin));
191     started = time((time_t *) 0);
192
193     fg = COLOR_WHITE;
194     bg = COLOR_BLACK;
195     pair = 0;
196     wch[1] = 0;
197     while (!interrupted) {
198         x = (int) (c * ranf()) + margin;
199         y = (int) (r * ranf()) + margin;
200         p = (ranf() > 0.9) ? '*' : ' ';
201
202         move(y, x);
203         if (has_colors()) {
204             z = (int) (ranf() * COLORS);
205             if (ranf() > 0.01) {
206                 set_colors(fg = z, bg);
207             } else {
208                 set_colors(fg, bg = z);
209                 napms(1);
210             }
211         } else {
212             if (ranf() <= 0.01) {
213                 if (ranf() > 0.6) {
214                     attr_on(WA_REVERSE, NULL);
215                 } else {
216                     attr_off(WA_REVERSE, NULL);
217                 }
218                 napms(1);
219             }
220         }
221         wch[0] = p;
222         addnwstr(wch, 1);
223         refresh();
224         ++total_chars;
225     }
226     cleanup();
227     ExitProgram(EXIT_SUCCESS);
228 }
229
230 #else
231 int
232 main(void)
233 {
234     printf("This program requires the wide-ncurses library\n");
235     ExitProgram(EXIT_FAILURE);
236 }
237 #endif