]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_curses.c
ncurses 6.1 - patch 20180217
[ncurses.git] / test / dots_curses.c
1 /****************************************************************************
2  * Copyright (c) 2014,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_curses.c,v 1.12 2017/11/24 19:25:28 tom Exp $
33  *
34  * A simple demo of the 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 static bool interrupted = FALSE;
45 static long total_chars = 0;
46 static time_t started;
47
48 static void
49 cleanup(void)
50 {
51     endwin();
52
53     printf("\n\n%ld total cells, rate %.2f/sec\n",
54            total_chars,
55            ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
56 }
57
58 static void
59 onsig(int n GCC_UNUSED)
60 {
61     interrupted = TRUE;
62 }
63
64 static double
65 ranf(void)
66 {
67     long r = (rand() & 077777);
68     return ((double) r / 32768.);
69 }
70
71 static int
72 mypair(int fg, int bg)
73 {
74     int pair = (fg * COLORS) + bg;
75     return (pair >= COLOR_PAIRS) ? -1 : pair;
76 }
77
78 static void
79 set_colors(int fg, int bg)
80 {
81     int pair = mypair(fg, bg);
82     if (pair > 0) {
83         attron(COLOR_PAIR(mypair(fg, bg)));
84     }
85 }
86
87 static void
88 usage(void)
89 {
90     static const char *msg[] =
91     {
92         "Usage: dots_curses [options]"
93         ,""
94         ,"Options:"
95         ," -T TERM  override $TERM"
96 #if HAVE_USE_DEFAULT_COLORS
97         ," -d       invoke use_default_colors()"
98 #endif
99 #if HAVE_USE_ENV
100         ," -e       allow environment $LINES / $COLUMNS"
101 #endif
102         ," -m SIZE  set margin (default: 2)"
103         ," -s MSECS delay 1% of the time (default: 1 msecs)"
104     };
105     size_t n;
106
107     for (n = 0; n < SIZEOF(msg); n++)
108         fprintf(stderr, "%s\n", msg[n]);
109
110     ExitProgram(EXIT_FAILURE);
111 }
112
113 int
114 main(int argc, char *argv[])
115 {
116     int x, y, z, p;
117     int fg, bg;
118     double r;
119     double c;
120 #if HAVE_USE_DEFAULT_COLORS
121     bool d_option = FALSE;
122 #endif
123     int m_option = 2;
124     int s_option = 1;
125
126     while ((x = getopt(argc, argv, "T:dem:s:")) != -1) {
127         switch (x) {
128         case 'T':
129             putenv(strcat(strcpy(malloc(6 + strlen(optarg)), "TERM="), optarg));
130             break;
131 #if HAVE_USE_DEFAULT_COLORS
132         case 'd':
133             d_option = TRUE;
134             break;
135 #endif
136 #if HAVE_USE_ENV
137         case 'e':
138             use_env(TRUE);
139             break;
140 #endif
141         case 'm':
142             m_option = atoi(optarg);
143             break;
144         case 's':
145             s_option = atoi(optarg);
146             break;
147         default:
148             usage();
149             break;
150         }
151     }
152
153     srand((unsigned) time(0));
154
155     InitAndCatch(initscr(), onsig);
156     if (has_colors()) {
157         start_color();
158 #if HAVE_USE_DEFAULT_COLORS
159         if (d_option)
160             use_default_colors();
161 #endif
162         for (fg = 0; fg < COLORS; fg++) {
163             for (bg = 0; bg < COLORS; bg++) {
164                 int pair;
165                 if (interrupted) {
166                     cleanup();
167                     ExitProgram(EXIT_FAILURE);
168                 }
169                 pair = mypair(fg, bg);
170                 if (pair > 0)
171                     init_pair((short) pair, (short) fg, (short) bg);
172             }
173         }
174     }
175
176     r = (double) (LINES - (m_option * 2));
177     c = (double) (COLS - (m_option * 2));
178     started = time((time_t *) 0);
179
180     fg = COLOR_WHITE;
181     bg = COLOR_BLACK;
182     while (!interrupted) {
183         x = (int) (c * ranf()) + m_option;
184         y = (int) (r * ranf()) + m_option;
185         p = (ranf() > 0.9) ? '*' : ' ';
186
187         move(y, x);
188         if (has_colors()) {
189             z = (int) (ranf() * COLORS);
190             if (ranf() > 0.01) {
191                 set_colors(fg = z, bg);
192                 attron(COLOR_PAIR(mypair(fg, bg)));
193             } else {
194                 set_colors(fg, bg = z);
195                 napms(s_option);
196             }
197         } else {
198             if (ranf() <= 0.01) {
199                 if (ranf() > 0.6) {
200                     attron(A_REVERSE);
201                 } else {
202                     attroff(A_REVERSE);
203                 }
204                 napms(s_option);
205             }
206         }
207         AddCh(p);
208         refresh();
209         ++total_chars;
210     }
211     cleanup();
212     ExitProgram(EXIT_SUCCESS);
213 }