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.84 2022/07/23 17:06:16 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]"
457 #if HAVE_USE_DEFAULT_COLORS
458 ," -d invoke use_default_colors"
460 ," -f fill screen with copies of \"WORM\" at start"
461 ," -l <n> set length of worms"
462 ," -n <n> set number of worms"
463 ," -t leave trail of \".\""
465 ," -T <start>,<end> set trace interval"
466 ," -N suppress cursor-movement optimization"
471 for (n = 0; n < SIZEOF(msg); n++)
472 fprintf(stderr, "%s\n", msg[n]);
474 ExitProgram(EXIT_FAILURE);
478 main(int argc, char *argv[])
486 #if HAVE_USE_DEFAULT_COLORS
490 setlocale(LC_ALL, "");
492 while ((ch = getopt(argc, argv, "dfl:n:tT:N")) != -1) {
494 #if HAVE_USE_DEFAULT_COLORS
503 if ((length = atoi(optarg)) < 2 || length > MAX_LENGTH) {
504 fprintf(stderr, "%s: Invalid length\n", *argv);
509 if ((number = atoi(optarg)) < 1 || number > MAX_WORMS) {
510 fprintf(stderr, "%s: Invalid number of worms\n", *argv);
519 if (sscanf(optarg, "%d,%d", &trace_start, &trace_end) != 2)
523 _nc_optimize_enable ^= OPTIMIZE_ALL; /* declared by ncurses */
534 signal(SIGINT, onsig);
547 int bg = COLOR_BLACK;
549 #if HAVE_USE_DEFAULT_COLORS
550 if (opt_d && (use_default_colors() == OK))
554 #define SET_COLOR(num, fg) \
555 init_pair(num+1, (short) fg, (short) bg); \
556 flavor[num] |= (chtype) COLOR_PAIR(num+1) | A_BOLD
558 SET_COLOR(0, COLOR_GREEN);
559 SET_COLOR(1, COLOR_RED);
560 SET_COLOR(2, COLOR_CYAN);
561 SET_COLOR(3, COLOR_WHITE);
562 SET_COLOR(4, COLOR_MAGENTA);
563 SET_COLOR(5, COLOR_BLUE);
564 SET_COLOR(6, COLOR_YELLOW);
569 refs = typeMalloc(int *, (size_t) max_refs);
570 for (y = 0; y < max_refs; y++) {
571 refs[y] = typeMalloc(int, (size_t) COLS);
572 for (x = 0; x < COLS; x++) {
578 /* if addressing the lower right corner doesn't work in your curses */
579 refs[last_y][last_x] = 1;
580 #endif /* BADCORNER */
582 for (n = number, w = &worm[0]; --n >= 0; w++) {
583 w->attrs = flavor[(unsigned) n % SIZEOF(flavor)];
587 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
588 fprintf(stderr, "%s: out of memory\n", *argv);
589 ExitProgram(EXIT_FAILURE);
592 for (x = length; --x >= 0;)
594 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
595 fprintf(stderr, "%s: out of memory\n", *argv);
596 ExitProgram(EXIT_FAILURE);
599 for (y = length; --y >= 0;)
605 for (y = last_y; --y >= 0;) {
606 for (x = COLS; --x >= 0;) {
607 addch((chtype) (*p++));
613 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
614 nodelay(stdscr, TRUE);
617 pthread_mutex_init(&pending_mutex, NULL);
622 if ((ch = get_input()) > 0) {
624 if (trace_start || trace_end) {
625 if (generation == trace_start) {
626 curses_trace(TRACE_CALLS);
628 } else if (generation == trace_end) {
638 if (ch == KEY_RESIZE) {
639 USING_WINDOW(stdscr, update_refs);
644 * Make it simple to put this into single-step mode, or resume
645 * normal operation -T.Dickey
650 Trace(("done - quitting"));
652 } else if (ch == 's') {
653 nodelay(stdscr, FALSE);
654 } else if (ch == ' ') {
655 nodelay(stdscr, TRUE);
659 done = draw_all_worms();
661 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
667 for (y = 0; y < max_refs; y++) {
671 for (n = number, w = &worm[0]; --n >= 0; w++) {
678 * Do this just in case one of the threads did not really exit.
680 Trace(("join all threads"));
681 for (n = 0; n < number; n++) {
682 pthread_join(worm[n].thread, NULL);
685 ExitProgram(EXIT_SUCCESS);