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