blob: 1b1d81a05bcbc0b2ffc37f3b67cf1a49e2347687 [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2020,2022 Thomas E. Dickey *
3 * Copyright 1998-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/*
micky3879b9f5e72025-07-08 18:04:53 -040030 * $Id: rain.c,v 1.57 2022/12/04 00:40:11 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010031 */
32#include <test.priv.h>
micky3879b9f5e72025-07-08 18:04:53 -040033#include <popup_msg.h>
Steve Kondikae271bc2015-11-15 02:50:53 +010034
35/* rain 11/3/1980 EPS/CITHEP */
36
37#ifdef USE_PTHREADS
38#include <pthread.h>
39#endif
40
41WANT_USE_WINDOW();
42
43#define MAX_THREADS 10
44#define MAX_DROP 5
45
46struct DATA;
47
48typedef void (*DrawPart) (struct DATA *);
49
50typedef struct DATA {
51 int y, x;
52#ifdef USE_PTHREADS
53 DrawPart func;
54 int state;
55#endif
56} DATA;
57
58#ifdef USE_PTHREADS
59pthread_cond_t cond_next_drop;
micky3879b9f5e72025-07-08 18:04:53 -040060pthread_mutex_t mutex_drop_data;
Steve Kondikae271bc2015-11-15 02:50:53 +010061pthread_mutex_t mutex_next_drop;
62static int used_threads;
63
64typedef struct {
65 pthread_t myself;
66 long counter;
67} STATS;
68
69static STATS drop_threads[MAX_THREADS];
70#endif
71
micky3879b9f5e72025-07-08 18:04:53 -040072#if HAVE_USE_WINDOW
73static int
74safe_wgetch(WINDOW *w, void *data GCC_UNUSED)
75{
76 return wgetch(w);
77}
78#endif
79
Steve Kondikae271bc2015-11-15 02:50:53 +010080static void
81onsig(int n GCC_UNUSED)
82{
micky3879b9f5e72025-07-08 18:04:53 -040083 stop_curses();
Steve Kondikae271bc2015-11-15 02:50:53 +010084 ExitProgram(EXIT_FAILURE);
85}
86
87static double
88ranf(void)
89{
90 long r = (rand() & 077777);
91 return ((double) r / 32768.);
92}
93
94static int
95random_x(void)
96{
97 return (int) (((double) (COLS - 4) * ranf()) + 2);
98}
99
100static int
101random_y(void)
102{
103 return (int) (((double) (LINES - 4) * ranf()) + 2);
104}
105
106static int
107next_j(int j)
108{
109 if (j == 0)
110 j = MAX_DROP - 1;
111 else
112 --j;
113 if (has_colors()) {
114 int z = (int) (3 * ranf());
115 (void) attrset(AttrArg(COLOR_PAIR(z), (z ? A_BOLD : A_NORMAL)));
116 }
117 return j;
118}
119
120static void
121part1(DATA * drop)
122{
123 MvAddCh(drop->y, drop->x, '.');
124}
125
126static void
127part2(DATA * drop)
128{
129 MvAddCh(drop->y, drop->x, 'o');
130}
131
132static void
133part3(DATA * drop)
134{
135 MvAddCh(drop->y, drop->x, 'O');
136}
137
138static void
139part4(DATA * drop)
140{
141 MvAddCh(drop->y - 1, drop->x, '-');
142 MvAddStr(drop->y, drop->x - 1, "|.|");
143 MvAddCh(drop->y + 1, drop->x, '-');
144}
145
146static void
147part5(DATA * drop)
148{
149 MvAddCh(drop->y - 2, drop->x, '-');
150 MvAddStr(drop->y - 1, drop->x - 1, "/ \\");
151 MvAddStr(drop->y, drop->x - 2, "| O |");
152 MvAddStr(drop->y + 1, drop->x - 1, "\\ /");
153 MvAddCh(drop->y + 2, drop->x, '-');
154}
155
156static void
157part6(DATA * drop)
158{
159 MvAddCh(drop->y - 2, drop->x, ' ');
160 MvAddStr(drop->y - 1, drop->x - 1, " ");
161 MvAddStr(drop->y, drop->x - 2, " ");
162 MvAddStr(drop->y + 1, drop->x - 1, " ");
163 MvAddCh(drop->y + 2, drop->x, ' ');
164}
165
166#ifdef USE_PTHREADS
167static void
168napsome(void)
169{
170 napms(60);
171}
172
173/*
174 * This runs inside the use_window() mutex.
175 */
176static int
177really_draw(WINDOW *win, void *arg)
178{
179 DATA *data = (DATA *) arg;
180
181 (void) win;
182 next_j(data->state);
183 data->func(data);
184 refresh();
185 return OK;
186}
187
188static void
189draw_part(void (*func) (DATA *), int state, DATA * data)
190{
191 data->func = func;
192 data->state = state;
193 use_window(stdscr, really_draw, (void *) data);
194 napsome();
195}
196
197/*
198 * Tell the threads that one of them can start work on a new raindrop.
199 * They may all be busy if we're sending requests too rapidly.
200 */
201static int
202put_next_drop(void)
203{
micky3879b9f5e72025-07-08 18:04:53 -0400204 pthread_cond_broadcast(&cond_next_drop);
Steve Kondikae271bc2015-11-15 02:50:53 +0100205 pthread_mutex_unlock(&mutex_next_drop);
206
207 return 0;
208}
209
210/*
211 * Wait until we're assigned the task of drawing a new raindrop.
212 */
213static int
214get_next_drop(void)
215{
216 pthread_mutex_lock(&mutex_next_drop);
217 pthread_cond_wait(&cond_next_drop, &mutex_next_drop);
218
219 return TRUE;
220}
221
222static void *
223draw_drop(void *arg)
224{
225 DATA mydata;
226 int mystats;
227
228 /*
229 * Find myself in the list of threads so we can count the number of loops.
230 */
231 for (mystats = 0; mystats < MAX_THREADS; ++mystats) {
micky3879b9f5e72025-07-08 18:04:53 -0400232#if defined(_NC_WINDOWS) && !defined(__WINPTHREADS_VERSION)
Steve Kondikae271bc2015-11-15 02:50:53 +0100233 if (drop_threads[mystats].myself.p == pthread_self().p)
234#else
235 if (drop_threads[mystats].myself == pthread_self())
236#endif
237 break;
238 }
239
240 do {
241 if (mystats < MAX_THREADS)
242 drop_threads[mystats].counter++;
243
244 /*
245 * Make a copy of caller's data. We're cheating for the cases after
246 * the first loop since we still have a pointer into the main thread
247 * to the data which it uses for setting up this thread (but it has
248 * been modified to use different coordinates).
249 */
micky3879b9f5e72025-07-08 18:04:53 -0400250 pthread_mutex_lock(&mutex_drop_data);
Steve Kondikae271bc2015-11-15 02:50:53 +0100251 mydata = *(DATA *) arg;
micky3879b9f5e72025-07-08 18:04:53 -0400252 pthread_mutex_unlock(&mutex_drop_data);
Steve Kondikae271bc2015-11-15 02:50:53 +0100253
254 draw_part(part1, 0, &mydata);
255 draw_part(part2, 1, &mydata);
256 draw_part(part3, 2, &mydata);
257 draw_part(part4, 3, &mydata);
258 draw_part(part5, 4, &mydata);
259 draw_part(part6, 0, &mydata);
micky3879b9f5e72025-07-08 18:04:53 -0400260
Steve Kondikae271bc2015-11-15 02:50:53 +0100261 } while (get_next_drop());
262
263 return NULL;
264}
265
266/*
267 * The description of pthread_create() is misleading, since it implies that
268 * threads will exit cleanly after their function returns.
269 *
270 * Since they do not (and the number of threads is limited by system
271 * resources), make a limited number of threads, and signal any that are
272 * waiting when we want a thread past that limit.
273 */
274static int
275start_drop(DATA * data)
276{
277 int rc;
278
279 if (!used_threads) {
280 /* mutex and condition for signalling thread */
281 pthread_mutex_init(&mutex_next_drop, NULL);
282 pthread_cond_init(&cond_next_drop, NULL);
283 }
284
285 if (used_threads < MAX_THREADS) {
286 rc = pthread_create(&(drop_threads[used_threads].myself),
287 NULL,
288 draw_drop,
289 data);
290 ++used_threads;
291 } else {
292 rc = put_next_drop();
293 }
294 return rc;
295}
296#endif
297
298static int
299get_input(void)
300{
micky3879b9f5e72025-07-08 18:04:53 -0400301 return USING_WINDOW1(stdscr, wgetch, safe_wgetch);
Steve Kondikae271bc2015-11-15 02:50:53 +0100302}
303
micky3879b9f5e72025-07-08 18:04:53 -0400304static void
305usage(int ok)
Steve Kondikae271bc2015-11-15 02:50:53 +0100306{
micky3879b9f5e72025-07-08 18:04:53 -0400307 static const char *msg[] =
308 {
309 "Usage: rain [options]"
310 ,""
311 ,USAGE_COMMON
312 ,"Options:"
313#if HAVE_USE_DEFAULT_COLORS
314 ," -d invoke use_default_colors"
315#endif
316 };
317 size_t n;
318
319 for (n = 0; n < SIZEOF(msg); n++)
320 fprintf(stderr, "%s\n", msg[n]);
321
322 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
323}
324/* *INDENT-OFF* */
325VERSION_COMMON()
326/* *INDENT-ON* */
327
328int
329main(int argc, char *argv[])
330{
331 static const char *help[] =
332 {
333 "Commands:",
334 " q/Q exit the program",
335 " s do single-step",
336 " <space> undo single-step",
337 "",
338 0
339 };
340
Steve Kondikae271bc2015-11-15 02:50:53 +0100341 bool done = FALSE;
342 DATA drop;
343#ifndef USE_PTHREADS
344 DATA last[MAX_DROP];
345#endif
346 int j = 0;
micky3879b9f5e72025-07-08 18:04:53 -0400347 int ch;
348#if HAVE_USE_DEFAULT_COLORS
349 bool d_option = FALSE;
350#endif
351
352 while ((ch = getopt(argc, argv, OPTS_COMMON "d")) != -1) {
353 switch (ch) {
354#if HAVE_USE_DEFAULT_COLORS
355 case 'd':
356 d_option = TRUE;
357 break;
358#endif
359 case OPTS_VERSION:
360 show_version(argv);
361 ExitProgram(EXIT_SUCCESS);
362 default:
363 usage(ch == OPTS_USAGE);
364 /* NOTREACHED */
365 }
366 }
367 if (optind < argc)
368 usage(FALSE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100369
370 setlocale(LC_ALL, "");
371
micky3879b9f5e72025-07-08 18:04:53 -0400372 InitAndCatch(initscr(), onsig);
Steve Kondikae271bc2015-11-15 02:50:53 +0100373 if (has_colors()) {
374 int bg = COLOR_BLACK;
375 start_color();
376#if HAVE_USE_DEFAULT_COLORS
micky3879b9f5e72025-07-08 18:04:53 -0400377 if (d_option && (use_default_colors() == OK))
Steve Kondikae271bc2015-11-15 02:50:53 +0100378 bg = -1;
379#endif
380 init_pair(1, COLOR_BLUE, (short) bg);
381 init_pair(2, COLOR_CYAN, (short) bg);
382 }
383 nl();
384 noecho();
385 curs_set(0);
386 timeout(0);
387
micky3879b9f5e72025-07-08 18:04:53 -0400388#ifdef USE_PTHREADS
389 pthread_mutex_init(&mutex_drop_data, NULL);
390#else /* !USE_PTHREADS */
Steve Kondikae271bc2015-11-15 02:50:53 +0100391 for (j = MAX_DROP; --j >= 0;) {
392 last[j].x = random_x();
393 last[j].y = random_y();
394 }
395 j = 0;
396#endif
397
398 while (!done) {
micky3879b9f5e72025-07-08 18:04:53 -0400399#ifdef USE_PTHREADS
400 pthread_mutex_lock(&mutex_drop_data);
401
Steve Kondikae271bc2015-11-15 02:50:53 +0100402 drop.x = random_x();
403 drop.y = random_y();
404
Steve Kondikae271bc2015-11-15 02:50:53 +0100405 if (start_drop(&drop) != 0) {
406 beep();
407 }
micky3879b9f5e72025-07-08 18:04:53 -0400408
409 pthread_mutex_unlock(&mutex_drop_data);
Steve Kondikae271bc2015-11-15 02:50:53 +0100410#else
micky3879b9f5e72025-07-08 18:04:53 -0400411 drop.x = random_x();
412 drop.y = random_y();
413
Steve Kondikae271bc2015-11-15 02:50:53 +0100414 /*
415 * The non-threaded code draws parts of each drop on each loop.
416 */
417 part1(&drop);
418
419 part2(&last[j]);
420
421 j = next_j(j);
422 part3(&last[j]);
423
424 j = next_j(j);
425 part4(&last[j]);
426
427 j = next_j(j);
428 part5(&last[j]);
429
430 j = next_j(j);
431 part6(&last[j]);
432
433 last[j] = drop;
434#endif
435
436 switch (get_input()) {
437 case ('q'):
438 case ('Q'):
439 done = TRUE;
440 break;
441 case 's':
442 nodelay(stdscr, FALSE);
443 break;
444 case ' ':
445 nodelay(stdscr, TRUE);
446 break;
447#ifdef KEY_RESIZE
448 case (KEY_RESIZE):
449 break;
450#endif
micky3879b9f5e72025-07-08 18:04:53 -0400451 case HELP_KEY_1:
452 popup_msg(stdscr, help);
453 break;
454 case ERR:
455 break;
456 default:
457 beep();
Steve Kondikae271bc2015-11-15 02:50:53 +0100458 }
459 napms(50);
460 }
micky3879b9f5e72025-07-08 18:04:53 -0400461 stop_curses();
Steve Kondikae271bc2015-11-15 02:50:53 +0100462#ifdef USE_PTHREADS
463 printf("Counts per thread:\n");
464 for (j = 0; j < MAX_THREADS; ++j)
465 printf(" %d:%ld\n", j, drop_threads[j].counter);
466#endif
467 ExitProgram(EXIT_SUCCESS);
468}