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