1 /****************************************************************************
2 * Copyright 2018-2020,2022 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
31 @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
32 @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
33 @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
34 @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
35 @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
36 @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
37 @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
38 @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
39 @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
42 Caltech High Energy Physics
45 Hacks to turn this into a test frame for cursor movement:
46 Eric S. Raymond <esr@snark.thyrsus.com>
49 July 1995 (esr): worms is now in living color! :-)
51 This program makes a good torture-test for the ncurses cursor-optimization
52 code. You can use -T to set the worm move interval over which movement
53 traces will be dumped. The program stops and waits for one character of
54 input at the beginning and end of the interval.
56 $Id: worm.c,v 1.89 2022/12/24 20:46:49 tom Exp $
59 #include <test.priv.h>
61 #ifndef NCURSES_VERSION
72 #define MAX_LENGTH 1024
74 static chtype flavor[] =
76 'O', '*', '#', '$', '%', '0', '@',
78 static const int xinc[] =
80 1, 1, 1, 0, -1, -1, -1, 0
83 -1, 0, 1, 1, 1, 0, -1, -1
97 static unsigned long sequence = 0;
98 static bool quitting = FALSE;
100 static WORM worm[MAX_WORMS];
103 static int last_x, last_y;
105 static const char *field;
106 static int length = 16, number = 3;
107 static chtype trail = ' ';
109 static unsigned pending;
112 #define Locked(statement) { \
113 pthread_mutex_lock(&pending_mutex); \
115 pthread_mutex_unlock(&pending_mutex); \
117 pthread_mutex_t pending_mutex;
119 #define Locked(statement) statement
123 static int generation, trace_start, trace_end;
126 static const struct options {
215 safe_wgetch(WINDOW *w, void *data GCC_UNUSED)
220 safe_wrefresh(WINDOW *w, void *data GCC_UNUSED)
228 failed(const char *s)
232 ExitProgram(EXIT_FAILURE);
239 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
244 onsig(int sig GCC_UNUSED)
247 ExitProgram(EXIT_FAILURE);
253 long r = (rand() & 077777);
254 return ((double) r / 32768.);
258 draw_worm(WINDOW *win, void *data)
260 WORM *w = (WORM *) data;
261 const struct options *op;
262 unsigned mask = (unsigned) (~(1 << (w - worm)));
272 Locked(is_pending = ((mask & pending) != 0));
274 attrs = w->attrs | (is_pending ? A_REVERSE : 0);
276 if ((x = w->xpos[h = w->head]) < 0) {
277 wmove(win, y = w->ypos[h] = last_y, x = w->xpos[h] = 0);
292 if (w->xpos[w->head = h] >= 0) {
298 && --refs[y1][x1] == 0) {
320 : normal))))[w->orientation];
325 Trace(("done - draw_worm"));
328 w->orientation = op->opts[0];
331 w->orientation = op->opts[(int) (ranf() * (double) op->nopts)];
336 x += xinc[w->orientation];
337 y += yinc[w->orientation];
354 quit_worm(int bitnum)
356 Locked(pending = (pending | (unsigned) (1 << bitnum)));
358 napms(10); /* let the other thread(s) have a chance */
360 Locked(pending = (pending & (unsigned) ~(1 << bitnum)));
366 start_worm(void *arg)
368 unsigned long compare = 0;
369 Trace(("start_worm"));
370 while (!quit_worm((int) (((struct worm *) arg) - worm))) {
373 Locked(done = (compare >= sequence));
377 USING_WINDOW2(stdscr, draw_worm, arg);
380 Trace(("...start_worm (done)"));
393 static bool first = TRUE;
396 for (n = 0, w = &worm[0]; n < number; n++, w++) {
397 (void) pthread_create(&(w->thread), NULL, start_worm, w);
401 for (n = 0, w = &worm[0]; n < number; n++, w++) {
402 if (USING_WINDOW2(stdscr, draw_worm, w))
413 ch = USING_WINDOW1(stdscr, wgetch, safe_wgetch);
419 update_refs(WINDOW *win, void *data)
425 if (last_x != COLS - 1) {
426 for (y = 0; y <= last_y; y++) {
427 refs[y] = typeRealloc(int, (size_t) COLS, refs[y]);
429 failed("update_refs");
430 for (x = last_x + 1; x < COLS; x++)
435 if (last_y != LINES - 1) {
436 for (y = LINES; y <= last_y; y++)
439 refs = typeRealloc(int *, (size_t) LINES, refs);
440 for (y = last_y + 1; y < LINES; y++) {
441 refs[y] = typeMalloc(int, (size_t) COLS);
443 failed("update_refs");
444 for (x = 0; x < COLS; x++)
456 static const char *msg[] =
458 "Usage: worm [options]"
462 #if HAVE_USE_DEFAULT_COLORS
463 ," -d invoke use_default_colors"
465 ," -f fill screen with copies of \"WORM\" at start"
466 ," -l <n> set length of worms"
467 ," -n <n> set number of worms"
468 ," -t leave trail of \".\""
470 ," -T <start>,<end> set trace interval"
471 ," -N suppress cursor-movement optimization"
476 for (n = 0; n < SIZEOF(msg); n++)
477 fprintf(stderr, "%s\n", msg[n]);
479 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
486 main(int argc, char *argv[])
494 #if HAVE_USE_DEFAULT_COLORS
498 setlocale(LC_ALL, "");
500 while ((ch = getopt(argc, argv, OPTS_COMMON "dfl:n:tT:N")) != -1) {
502 #if HAVE_USE_DEFAULT_COLORS
511 if ((length = atoi(optarg)) < 2 || length > MAX_LENGTH) {
512 fprintf(stderr, "%s: Invalid length\n", *argv);
517 if ((number = atoi(optarg)) < 1 || number > MAX_WORMS) {
518 fprintf(stderr, "%s: Invalid number of worms\n", *argv);
527 if (sscanf(optarg, "%d,%d", &trace_start, &trace_end) != 2)
531 _nc_optimize_enable ^= OPTIMIZE_ALL; /* declared by ncurses */
536 ExitProgram(EXIT_SUCCESS);
538 usage(ch == OPTS_USAGE);
545 signal(SIGINT, onsig);
558 int bg = COLOR_BLACK;
560 #if HAVE_USE_DEFAULT_COLORS
561 if (opt_d && (use_default_colors() == OK))
565 #define SET_COLOR(num, fg) \
566 init_pair(num+1, (short) fg, (short) bg); \
567 flavor[num] |= (chtype) COLOR_PAIR(num+1) | A_BOLD
569 SET_COLOR(0, COLOR_GREEN);
570 SET_COLOR(1, COLOR_RED);
571 SET_COLOR(2, COLOR_CYAN);
572 SET_COLOR(3, COLOR_WHITE);
573 SET_COLOR(4, COLOR_MAGENTA);
574 SET_COLOR(5, COLOR_BLUE);
575 SET_COLOR(6, COLOR_YELLOW);
580 refs = typeMalloc(int *, (size_t) max_refs);
581 for (y = 0; y < max_refs; y++) {
582 refs[y] = typeMalloc(int, (size_t) COLS);
583 for (x = 0; x < COLS; x++) {
589 /* if addressing the lower right corner doesn't work in your curses */
590 refs[last_y][last_x] = 1;
591 #endif /* BADCORNER */
593 for (n = number, w = &worm[0]; --n >= 0; w++) {
594 w->attrs = flavor[(unsigned) n % SIZEOF(flavor)];
598 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
599 fprintf(stderr, "%s: out of memory\n", *argv);
600 ExitProgram(EXIT_FAILURE);
603 for (x = length; --x >= 0;)
605 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
606 fprintf(stderr, "%s: out of memory\n", *argv);
607 ExitProgram(EXIT_FAILURE);
610 for (y = length; --y >= 0;)
616 for (y = last_y; --y >= 0;) {
617 for (x = COLS; --x >= 0;) {
618 addch((chtype) (*p++));
624 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
625 nodelay(stdscr, TRUE);
628 pthread_mutex_init(&pending_mutex, NULL);
633 if ((ch = get_input()) > 0) {
635 if (trace_start || trace_end) {
636 if (generation == trace_start) {
637 curses_trace(TRACE_CALLS);
639 } else if (generation == trace_end) {
649 if (ch == KEY_RESIZE) {
650 USING_WINDOW(stdscr, update_refs);
655 * Make it simple to put this into single-step mode, or resume
656 * normal operation -T.Dickey
661 Trace(("done - quitting"));
663 } else if (ch == 's') {
664 nodelay(stdscr, FALSE);
665 } else if (ch == ' ') {
666 nodelay(stdscr, TRUE);
670 done = draw_all_worms();
672 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
679 * Do this just in case one of the threads did not really exit.
681 Trace(("join all threads"));
682 for (n = 0; n < number; n++) {
683 pthread_join(worm[n].thread, NULL);
687 for (y = 0; y < max_refs; y++) {
691 for (n = number, w = &worm[0]; --n >= 0; w++) {
696 ExitProgram(EXIT_SUCCESS);