]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_curses.c
ncurses 6.1 - patch 20190601
[ncurses.git] / test / dots_curses.c
1 /****************************************************************************
2  * Copyright (c) 2014-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_curses.c,v 1.14 2019/01/21 14:20:18 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(_WIN32)
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     size_t need;
126     char *my_env;
127
128     while ((x = getopt(argc, argv, "T:dem:s:")) != -1) {
129         switch (x) {
130         case 'T':
131             need = 6 + strlen(optarg);
132             my_env = malloc(need);
133             _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
134             putenv(my_env);
135             break;
136 #if HAVE_USE_DEFAULT_COLORS
137         case 'd':
138             d_option = TRUE;
139             break;
140 #endif
141 #if HAVE_USE_ENV
142         case 'e':
143             use_env(TRUE);
144             break;
145 #endif
146         case 'm':
147             m_option = atoi(optarg);
148             break;
149         case 's':
150             s_option = atoi(optarg);
151             break;
152         default:
153             usage();
154             break;
155         }
156     }
157
158     srand((unsigned) time(0));
159
160     InitAndCatch(initscr(), onsig);
161     if (has_colors()) {
162         start_color();
163 #if HAVE_USE_DEFAULT_COLORS
164         if (d_option)
165             use_default_colors();
166 #endif
167         for (fg = 0; fg < COLORS; fg++) {
168             for (bg = 0; bg < COLORS; bg++) {
169                 int pair;
170                 if (interrupted) {
171                     cleanup();
172                     ExitProgram(EXIT_FAILURE);
173                 }
174                 pair = mypair(fg, bg);
175                 if (pair > 0)
176                     init_pair((short) pair, (short) fg, (short) bg);
177             }
178         }
179     }
180
181     r = (double) (LINES - (m_option * 2));
182     c = (double) (COLS - (m_option * 2));
183     started = time((time_t *) 0);
184
185     fg = COLOR_WHITE;
186     bg = COLOR_BLACK;
187     while (!interrupted) {
188         x = (int) (c * ranf()) + m_option;
189         y = (int) (r * ranf()) + m_option;
190         p = (ranf() > 0.9) ? '*' : ' ';
191
192         move(y, x);
193         if (has_colors()) {
194             z = (int) (ranf() * COLORS);
195             if (ranf() > 0.01) {
196                 set_colors(fg = z, bg);
197                 attron(COLOR_PAIR(mypair(fg, bg)));
198             } else {
199                 set_colors(fg, bg = z);
200                 napms(s_option);
201             }
202         } else {
203             if (ranf() <= 0.01) {
204                 if (ranf() > 0.6) {
205                     attron(A_REVERSE);
206                 } else {
207                     attroff(A_REVERSE);
208                 }
209                 napms(s_option);
210             }
211         }
212         AddCh(p);
213         refresh();
214         ++total_chars;
215     }
216     cleanup();
217     ExitProgram(EXIT_SUCCESS);
218 }