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.88 2022/12/04 00:40:11 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))) {
371 while (compare < sequence) {
373 USING_WINDOW2(stdscr, draw_worm, arg);
376 Trace(("...start_worm (done)"));
389 static bool first = TRUE;
392 for (n = 0, w = &worm[0]; n < number; n++, w++) {
393 (void) pthread_create(&(w->thread), NULL, start_worm, w);
397 for (n = 0, w = &worm[0]; n < number; n++, w++) {
398 if (USING_WINDOW2(stdscr, draw_worm, w))
409 ch = USING_WINDOW1(stdscr, wgetch, safe_wgetch);
415 update_refs(WINDOW *win, void *data)
421 if (last_x != COLS - 1) {
422 for (y = 0; y <= last_y; y++) {
423 refs[y] = typeRealloc(int, (size_t) COLS, refs[y]);
425 failed("update_refs");
426 for (x = last_x + 1; x < COLS; x++)
431 if (last_y != LINES - 1) {
432 for (y = LINES; y <= last_y; y++)
435 refs = typeRealloc(int *, (size_t) LINES, refs);
436 for (y = last_y + 1; y < LINES; y++) {
437 refs[y] = typeMalloc(int, (size_t) COLS);
439 failed("update_refs");
440 for (x = 0; x < COLS; x++)
452 static const char *msg[] =
454 "Usage: worm [options]"
458 #if HAVE_USE_DEFAULT_COLORS
459 ," -d invoke use_default_colors"
461 ," -f fill screen with copies of \"WORM\" at start"
462 ," -l <n> set length of worms"
463 ," -n <n> set number of worms"
464 ," -t leave trail of \".\""
466 ," -T <start>,<end> set trace interval"
467 ," -N suppress cursor-movement optimization"
472 for (n = 0; n < SIZEOF(msg); n++)
473 fprintf(stderr, "%s\n", msg[n]);
475 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
482 main(int argc, char *argv[])
490 #if HAVE_USE_DEFAULT_COLORS
494 setlocale(LC_ALL, "");
496 while ((ch = getopt(argc, argv, OPTS_COMMON "dfl:n:tT:N")) != -1) {
498 #if HAVE_USE_DEFAULT_COLORS
507 if ((length = atoi(optarg)) < 2 || length > MAX_LENGTH) {
508 fprintf(stderr, "%s: Invalid length\n", *argv);
513 if ((number = atoi(optarg)) < 1 || number > MAX_WORMS) {
514 fprintf(stderr, "%s: Invalid number of worms\n", *argv);
523 if (sscanf(optarg, "%d,%d", &trace_start, &trace_end) != 2)
527 _nc_optimize_enable ^= OPTIMIZE_ALL; /* declared by ncurses */
532 ExitProgram(EXIT_SUCCESS);
534 usage(ch == OPTS_USAGE);
541 signal(SIGINT, onsig);
554 int bg = COLOR_BLACK;
556 #if HAVE_USE_DEFAULT_COLORS
557 if (opt_d && (use_default_colors() == OK))
561 #define SET_COLOR(num, fg) \
562 init_pair(num+1, (short) fg, (short) bg); \
563 flavor[num] |= (chtype) COLOR_PAIR(num+1) | A_BOLD
565 SET_COLOR(0, COLOR_GREEN);
566 SET_COLOR(1, COLOR_RED);
567 SET_COLOR(2, COLOR_CYAN);
568 SET_COLOR(3, COLOR_WHITE);
569 SET_COLOR(4, COLOR_MAGENTA);
570 SET_COLOR(5, COLOR_BLUE);
571 SET_COLOR(6, COLOR_YELLOW);
576 refs = typeMalloc(int *, (size_t) max_refs);
577 for (y = 0; y < max_refs; y++) {
578 refs[y] = typeMalloc(int, (size_t) COLS);
579 for (x = 0; x < COLS; x++) {
585 /* if addressing the lower right corner doesn't work in your curses */
586 refs[last_y][last_x] = 1;
587 #endif /* BADCORNER */
589 for (n = number, w = &worm[0]; --n >= 0; w++) {
590 w->attrs = flavor[(unsigned) n % SIZEOF(flavor)];
594 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
595 fprintf(stderr, "%s: out of memory\n", *argv);
596 ExitProgram(EXIT_FAILURE);
599 for (x = length; --x >= 0;)
601 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
602 fprintf(stderr, "%s: out of memory\n", *argv);
603 ExitProgram(EXIT_FAILURE);
606 for (y = length; --y >= 0;)
612 for (y = last_y; --y >= 0;) {
613 for (x = COLS; --x >= 0;) {
614 addch((chtype) (*p++));
620 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
621 nodelay(stdscr, TRUE);
624 pthread_mutex_init(&pending_mutex, NULL);
629 if ((ch = get_input()) > 0) {
631 if (trace_start || trace_end) {
632 if (generation == trace_start) {
633 curses_trace(TRACE_CALLS);
635 } else if (generation == trace_end) {
645 if (ch == KEY_RESIZE) {
646 USING_WINDOW(stdscr, update_refs);
651 * Make it simple to put this into single-step mode, or resume
652 * normal operation -T.Dickey
657 Trace(("done - quitting"));
659 } else if (ch == 's') {
660 nodelay(stdscr, FALSE);
661 } else if (ch == ' ') {
662 nodelay(stdscr, TRUE);
666 done = draw_all_worms();
668 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
675 * Do this just in case one of the threads did not really exit.
677 Trace(("join all threads"));
678 for (n = 0; n < number; n++) {
679 pthread_join(worm[n].thread, NULL);
683 for (y = 0; y < max_refs; y++) {
687 for (n = number, w = &worm[0]; --n >= 0; w++) {
692 ExitProgram(EXIT_SUCCESS);