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.83 2022/07/09 20:51:25 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;
111 static int generation, trace_start, trace_end;
114 static const struct options {
203 safe_wgetch(WINDOW *w, void *data GCC_UNUSED)
208 safe_wrefresh(WINDOW *w, void *data GCC_UNUSED)
216 failed(const char *s)
220 ExitProgram(EXIT_FAILURE);
227 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
232 onsig(int sig GCC_UNUSED)
235 ExitProgram(EXIT_FAILURE);
241 long r = (rand() & 077777);
242 return ((double) r / 32768.);
246 draw_worm(WINDOW *win, void *data)
248 WORM *w = (WORM *) data;
249 const struct options *op;
250 unsigned mask = (unsigned) (~(1 << (w - worm)));
251 chtype attrs = w->attrs | ((mask & pending) ? A_REVERSE : 0);
259 if ((x = w->xpos[h = w->head]) < 0) {
260 wmove(win, y = w->ypos[h] = last_y, x = w->xpos[h] = 0);
275 if (w->xpos[w->head = h] >= 0) {
281 && --refs[y1][x1] == 0) {
303 : normal))))[w->orientation];
308 Trace(("done - draw_worm"));
311 w->orientation = op->opts[0];
314 w->orientation = op->opts[(int) (ranf() * (double) op->nopts)];
319 x += xinc[w->orientation];
320 y += yinc[w->orientation];
337 quit_worm(int bitnum)
339 pending = (pending | (unsigned) (1 << bitnum));
340 napms(10); /* let the other thread(s) have a chance */
341 pending = (pending & (unsigned) ~(1 << bitnum));
346 start_worm(void *arg)
348 unsigned long compare = 0;
349 Trace(("start_worm"));
350 while (!quit_worm((int) (((struct worm *) arg) - worm))) {
351 while (compare < sequence) {
353 USING_WINDOW2(stdscr, draw_worm, arg);
356 Trace(("...start_worm (done)"));
369 static bool first = TRUE;
372 for (n = 0, w = &worm[0]; n < number; n++, w++) {
373 (void) pthread_create(&(w->thread), NULL, start_worm, w);
377 for (n = 0, w = &worm[0]; n < number; n++, w++) {
378 if (USING_WINDOW2(stdscr, draw_worm, w))
389 ch = USING_WINDOW1(stdscr, wgetch, safe_wgetch);
395 update_refs(WINDOW *win, void *data)
401 if (last_x != COLS - 1) {
402 for (y = 0; y <= last_y; y++) {
403 refs[y] = typeRealloc(int, (size_t) COLS, refs[y]);
405 failed("update_refs");
406 for (x = last_x + 1; x < COLS; x++)
411 if (last_y != LINES - 1) {
412 for (y = LINES; y <= last_y; y++)
415 refs = typeRealloc(int *, (size_t) LINES, refs);
416 for (y = last_y + 1; y < LINES; y++) {
417 refs[y] = typeMalloc(int, (size_t) COLS);
419 failed("update_refs");
420 for (x = 0; x < COLS; x++)
432 static const char *msg[] =
434 "Usage: worm [options]"
437 #if HAVE_USE_DEFAULT_COLORS
438 ," -d invoke use_default_colors"
440 ," -f fill screen with copies of \"WORM\" at start"
441 ," -l <n> set length of worms"
442 ," -n <n> set number of worms"
443 ," -t leave trail of \".\""
445 ," -T <start>,<end> set trace interval"
446 ," -N suppress cursor-movement optimization"
451 for (n = 0; n < SIZEOF(msg); n++)
452 fprintf(stderr, "%s\n", msg[n]);
454 ExitProgram(EXIT_FAILURE);
458 main(int argc, char *argv[])
466 #if HAVE_USE_DEFAULT_COLORS
470 setlocale(LC_ALL, "");
472 while ((ch = getopt(argc, argv, "dfl:n:tT:N")) != -1) {
474 #if HAVE_USE_DEFAULT_COLORS
483 if ((length = atoi(optarg)) < 2 || length > MAX_LENGTH) {
484 fprintf(stderr, "%s: Invalid length\n", *argv);
489 if ((number = atoi(optarg)) < 1 || number > MAX_WORMS) {
490 fprintf(stderr, "%s: Invalid number of worms\n", *argv);
499 if (sscanf(optarg, "%d,%d", &trace_start, &trace_end) != 2)
503 _nc_optimize_enable ^= OPTIMIZE_ALL; /* declared by ncurses */
514 signal(SIGINT, onsig);
527 int bg = COLOR_BLACK;
529 #if HAVE_USE_DEFAULT_COLORS
530 if (opt_d && (use_default_colors() == OK))
534 #define SET_COLOR(num, fg) \
535 init_pair(num+1, (short) fg, (short) bg); \
536 flavor[num] |= (chtype) COLOR_PAIR(num+1) | A_BOLD
538 SET_COLOR(0, COLOR_GREEN);
539 SET_COLOR(1, COLOR_RED);
540 SET_COLOR(2, COLOR_CYAN);
541 SET_COLOR(3, COLOR_WHITE);
542 SET_COLOR(4, COLOR_MAGENTA);
543 SET_COLOR(5, COLOR_BLUE);
544 SET_COLOR(6, COLOR_YELLOW);
549 refs = typeMalloc(int *, (size_t) max_refs);
550 for (y = 0; y < max_refs; y++) {
551 refs[y] = typeMalloc(int, (size_t) COLS);
552 for (x = 0; x < COLS; x++) {
558 /* if addressing the lower right corner doesn't work in your curses */
559 refs[last_y][last_x] = 1;
560 #endif /* BADCORNER */
562 for (n = number, w = &worm[0]; --n >= 0; w++) {
563 w->attrs = flavor[(unsigned) n % SIZEOF(flavor)];
567 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
568 fprintf(stderr, "%s: out of memory\n", *argv);
569 ExitProgram(EXIT_FAILURE);
572 for (x = length; --x >= 0;)
574 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
575 fprintf(stderr, "%s: out of memory\n", *argv);
576 ExitProgram(EXIT_FAILURE);
579 for (y = length; --y >= 0;)
585 for (y = last_y; --y >= 0;) {
586 for (x = COLS; --x >= 0;) {
587 addch((chtype) (*p++));
593 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
594 nodelay(stdscr, TRUE);
598 if ((ch = get_input()) > 0) {
600 if (trace_start || trace_end) {
601 if (generation == trace_start) {
602 curses_trace(TRACE_CALLS);
604 } else if (generation == trace_end) {
614 if (ch == KEY_RESIZE) {
615 USING_WINDOW(stdscr, update_refs);
620 * Make it simple to put this into single-step mode, or resume
621 * normal operation -T.Dickey
626 Trace(("done - quitting"));
628 } else if (ch == 's') {
629 nodelay(stdscr, FALSE);
630 } else if (ch == ' ') {
631 nodelay(stdscr, TRUE);
635 done = draw_all_worms();
637 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
643 for (y = 0; y < max_refs; y++) {
647 for (n = number, w = &worm[0]; --n >= 0; w++) {
654 * Do this just in case one of the threads did not really exit.
656 Trace(("join all threads"));
657 for (n = 0; n < number; n++) {
658 pthread_join(worm[n].thread, NULL);
661 ExitProgram(EXIT_SUCCESS);