blob: 1a553213c48a67cca193f2724540f1b114b40310 [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2022,2023 Thomas E. Dickey *
3 * Copyright 2014,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
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 *
micky3879b9f5e72025-07-08 18:04:53 -040033 * $Id: dots_curses.c,v 1.25 2023/01/07 17:21:48 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010034 *
35 * A simple demo of the curses interface used for comparison with termcap.
36 */
37#include <test.priv.h>
38
micky3879b9f5e72025-07-08 18:04:53 -040039#if !defined(_NC_WINDOWS)
Steve Kondikae271bc2015-11-15 02:50:53 +010040#include <sys/time.h>
41#endif
42
43#include <time.h>
44
Steve Kondikae271bc2015-11-15 02:50:53 +010045static bool interrupted = FALSE;
46static long total_chars = 0;
47static time_t started;
48
49static void
50cleanup(void)
51{
52 endwin();
53
micky3879b9f5e72025-07-08 18:04:53 -040054 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)));
Steve Kondikae271bc2015-11-15 02:50:53 +010058}
59
60static void
61onsig(int n GCC_UNUSED)
62{
63 interrupted = TRUE;
64}
65
66static double
67ranf(void)
68{
69 long r = (rand() & 077777);
70 return ((double) r / 32768.);
71}
72
73static int
74mypair(int fg, int bg)
75{
76 int pair = (fg * COLORS) + bg;
77 return (pair >= COLOR_PAIRS) ? -1 : pair;
78}
79
80static void
81set_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
micky3879b9f5e72025-07-08 18:04:53 -040089static void
90usage(int ok)
Steve Kondikae271bc2015-11-15 02:50:53 +010091{
micky3879b9f5e72025-07-08 18:04:53 -040092 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* */
117VERSION_COMMON()
118/* *INDENT-ON* */
119
120int
121main(int argc, char *argv[])
122{
123 int ch;
Steve Kondikae271bc2015-11-15 02:50:53 +0100124 int fg, bg;
125 double r;
126 double c;
micky3879b9f5e72025-07-08 18:04:53 -0400127#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;
Steve Kondikae271bc2015-11-15 02:50:53 +0100135
micky3879b9f5e72025-07-08 18:04:53 -0400136 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 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100172
173 srand((unsigned) time(0));
174
micky3879b9f5e72025-07-08 18:04:53 -0400175 SetupAlarm(r_option);
176 InitAndCatch(initscr(), onsig);
177
Steve Kondikae271bc2015-11-15 02:50:53 +0100178 if (has_colors()) {
179 start_color();
micky3879b9f5e72025-07-08 18:04:53 -0400180#if HAVE_USE_DEFAULT_COLORS
181 if (d_option)
182 use_default_colors();
183#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100184 for (fg = 0; fg < COLORS; fg++) {
185 for (bg = 0; bg < COLORS; bg++) {
micky3879b9f5e72025-07-08 18:04:53 -0400186 int pair;
187 if (interrupted) {
188 cleanup();
189 ExitProgram(EXIT_FAILURE);
190 }
191 pair = mypair(fg, bg);
Steve Kondikae271bc2015-11-15 02:50:53 +0100192 if (pair > 0)
193 init_pair((short) pair, (short) fg, (short) bg);
194 }
195 }
196 }
197
micky3879b9f5e72025-07-08 18:04:53 -0400198 r = (double) (LINES - (m_option * 2));
199 c = (double) (COLS - (m_option * 2));
Steve Kondikae271bc2015-11-15 02:50:53 +0100200 started = time((time_t *) 0);
201
202 fg = COLOR_WHITE;
203 bg = COLOR_BLACK;
204 while (!interrupted) {
micky3879b9f5e72025-07-08 18:04:53 -0400205 int x = (int) (c * ranf()) + m_option;
206 int y = (int) (r * ranf()) + m_option;
207 int p = (ranf() > 0.9) ? '*' : ' ';
Steve Kondikae271bc2015-11-15 02:50:53 +0100208
209 move(y, x);
210 if (has_colors()) {
micky3879b9f5e72025-07-08 18:04:53 -0400211 int z = (int) (ranf() * COLORS);
Steve Kondikae271bc2015-11-15 02:50:53 +0100212 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);
micky3879b9f5e72025-07-08 18:04:53 -0400217 if (s_option)
218 napms(s_option);
Steve Kondikae271bc2015-11-15 02:50:53 +0100219 }
220 } else {
221 if (ranf() <= 0.01) {
222 if (ranf() > 0.6) {
223 attron(A_REVERSE);
224 } else {
225 attroff(A_REVERSE);
226 }
micky3879b9f5e72025-07-08 18:04:53 -0400227 if (s_option)
228 napms(s_option);
Steve Kondikae271bc2015-11-15 02:50:53 +0100229 }
230 }
micky3879b9f5e72025-07-08 18:04:53 -0400231 AddCh(p);
Steve Kondikae271bc2015-11-15 02:50:53 +0100232 refresh();
233 ++total_chars;
234 }
235 cleanup();
236 ExitProgram(EXIT_SUCCESS);
237}