]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_curses.c
ncurses 6.4 - patch 20240414
[ncurses.git] / test / dots_curses.c
1 /****************************************************************************
2  * Copyright 2018-2022,2023 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.25 2023/01/07 17:21:48 tom 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(int ok)
91 {
92     static const char *msg[] =
93     {
94         "Usage: dots_curses [options]"
95         ,""
96         ,USAGE_COMMON
97         ,"Options:"
98         ," -T TERM  override $TERM"
99 #if HAVE_USE_DEFAULT_COLORS
100         ," -d       invoke use_default_colors()"
101 #endif
102 #if HAVE_USE_ENV
103         ," -e       allow environment $LINES / $COLUMNS"
104 #endif
105         ," -m SIZE  set margin (default: 2)"
106         ," -r SECS  self-interrupt/exit after specified number of seconds"
107         ," -s MSECS delay 1% of the time (default: 1 msecs)"
108     };
109     size_t n;
110
111     for (n = 0; n < SIZEOF(msg); n++)
112         fprintf(stderr, "%s\n", msg[n]);
113
114     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
115 }
116 /* *INDENT-OFF* */
117 VERSION_COMMON()
118 /* *INDENT-ON* */
119
120 int
121 main(int argc, char *argv[])
122 {
123     int ch;
124     int fg, bg;
125     double r;
126     double c;
127 #if HAVE_USE_DEFAULT_COLORS
128     bool d_option = FALSE;
129 #endif
130     int m_option = 2;
131     int r_option = 0;
132     int s_option = 1;
133     size_t need;
134     char *my_env;
135
136     while ((ch = getopt(argc, argv, OPTS_COMMON "T:dem:r:s:")) != -1) {
137         switch (ch) {
138         case 'T':
139             need = 6 + strlen(optarg);
140             if ((my_env = malloc(need)) != NULL) {
141                 _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
142                 putenv(my_env);
143             }
144             break;
145 #if HAVE_USE_DEFAULT_COLORS
146         case 'd':
147             d_option = TRUE;
148             break;
149 #endif
150 #if HAVE_USE_ENV
151         case 'e':
152             use_env(TRUE);
153             break;
154 #endif
155         case 'm':
156             m_option = atoi(optarg);
157             break;
158         case 'r':
159             r_option = atoi(optarg);
160             break;
161         case 's':
162             s_option = atoi(optarg);
163             break;
164         case OPTS_VERSION:
165             show_version(argv);
166             ExitProgram(EXIT_SUCCESS);
167         default:
168             usage(ch == OPTS_USAGE);
169             /* NOTREACHED */
170         }
171     }
172
173     srand((unsigned) time(0));
174
175     SetupAlarm(r_option);
176     InitAndCatch(initscr(), onsig);
177
178     if (has_colors()) {
179         start_color();
180 #if HAVE_USE_DEFAULT_COLORS
181         if (d_option)
182             use_default_colors();
183 #endif
184         for (fg = 0; fg < COLORS; fg++) {
185             for (bg = 0; bg < COLORS; bg++) {
186                 int pair;
187                 if (interrupted) {
188                     cleanup();
189                     ExitProgram(EXIT_FAILURE);
190                 }
191                 pair = mypair(fg, bg);
192                 if (pair > 0)
193                     init_pair((short) pair, (short) fg, (short) bg);
194             }
195         }
196     }
197
198     r = (double) (LINES - (m_option * 2));
199     c = (double) (COLS - (m_option * 2));
200     started = time((time_t *) 0);
201
202     fg = COLOR_WHITE;
203     bg = COLOR_BLACK;
204     while (!interrupted) {
205         int x = (int) (c * ranf()) + m_option;
206         int y = (int) (r * ranf()) + m_option;
207         int p = (ranf() > 0.9) ? '*' : ' ';
208
209         move(y, x);
210         if (has_colors()) {
211             int z = (int) (ranf() * COLORS);
212             if (ranf() > 0.01) {
213                 set_colors(fg = z, bg);
214                 attron(COLOR_PAIR(mypair(fg, bg)));
215             } else {
216                 set_colors(fg, bg = z);
217                 if (s_option)
218                     napms(s_option);
219             }
220         } else {
221             if (ranf() <= 0.01) {
222                 if (ranf() > 0.6) {
223                     attron(A_REVERSE);
224                 } else {
225                     attroff(A_REVERSE);
226                 }
227                 if (s_option)
228                     napms(s_option);
229             }
230         }
231         AddCh(p);
232         refresh();
233         ++total_chars;
234     }
235     cleanup();
236     ExitProgram(EXIT_SUCCESS);
237 }