1 /****************************************************************************
2 * Copyright 2018-2019,2020 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.82 2020/02/02 23:34:34 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) {
354 use_window(stdscr, draw_worm, arg);
356 draw_worm(stdscr, arg);
360 Trace(("...start_worm (done)"));
373 static bool first = TRUE;
376 for (n = 0, w = &worm[0]; n < number; n++, w++) {
377 (void) pthread_create(&(w->thread), NULL, start_worm, w);
381 for (n = 0, w = &worm[0]; n < number; n++, w++) {
384 USING_WINDOW2(stdscr, draw_worm, w)
399 ch = USING_WINDOW1(stdscr, wgetch, safe_wgetch);
405 update_refs(WINDOW *win, void *data)
411 if (last_x != COLS - 1) {
412 for (y = 0; y <= last_y; y++) {
413 refs[y] = typeRealloc(int, (size_t) COLS, refs[y]);
415 failed("update_refs");
416 for (x = last_x + 1; x < COLS; x++)
421 if (last_y != LINES - 1) {
422 for (y = LINES; y <= last_y; y++)
425 refs = typeRealloc(int *, (size_t) LINES, refs);
426 for (y = last_y + 1; y < LINES; y++) {
427 refs[y] = typeMalloc(int, (size_t) COLS);
429 failed("update_refs");
430 for (x = 0; x < COLS; x++)
442 static const char *msg[] =
444 "Usage: worm [options]"
447 #if HAVE_USE_DEFAULT_COLORS
448 ," -d invoke use_default_colors"
450 ," -f fill screen with copies of \"WORM\" at start"
451 ," -l <n> set length of worms"
452 ," -n <n> set number of worms"
453 ," -t leave trail of \".\""
455 ," -T <start>,<end> set trace interval"
456 ," -N suppress cursor-movement optimization"
461 for (n = 0; n < SIZEOF(msg); n++)
462 fprintf(stderr, "%s\n", msg[n]);
464 ExitProgram(EXIT_FAILURE);
468 main(int argc, char *argv[])
476 #if HAVE_USE_DEFAULT_COLORS
480 setlocale(LC_ALL, "");
482 while ((ch = getopt(argc, argv, "dfl:n:tT:N")) != -1) {
484 #if HAVE_USE_DEFAULT_COLORS
493 if ((length = atoi(optarg)) < 2 || length > MAX_LENGTH) {
494 fprintf(stderr, "%s: Invalid length\n", *argv);
499 if ((number = atoi(optarg)) < 1 || number > MAX_WORMS) {
500 fprintf(stderr, "%s: Invalid number of worms\n", *argv);
509 if (sscanf(optarg, "%d,%d", &trace_start, &trace_end) != 2)
513 _nc_optimize_enable ^= OPTIMIZE_ALL; /* declared by ncurses */
524 signal(SIGINT, onsig);
537 int bg = COLOR_BLACK;
539 #if HAVE_USE_DEFAULT_COLORS
540 if (opt_d && (use_default_colors() == OK))
544 #define SET_COLOR(num, fg) \
545 init_pair(num+1, (short) fg, (short) bg); \
546 flavor[num] |= (chtype) COLOR_PAIR(num+1) | A_BOLD
548 SET_COLOR(0, COLOR_GREEN);
549 SET_COLOR(1, COLOR_RED);
550 SET_COLOR(2, COLOR_CYAN);
551 SET_COLOR(3, COLOR_WHITE);
552 SET_COLOR(4, COLOR_MAGENTA);
553 SET_COLOR(5, COLOR_BLUE);
554 SET_COLOR(6, COLOR_YELLOW);
559 refs = typeMalloc(int *, (size_t) max_refs);
560 for (y = 0; y < max_refs; y++) {
561 refs[y] = typeMalloc(int, (size_t) COLS);
562 for (x = 0; x < COLS; x++) {
568 /* if addressing the lower right corner doesn't work in your curses */
569 refs[last_y][last_x] = 1;
570 #endif /* BADCORNER */
572 for (n = number, w = &worm[0]; --n >= 0; w++) {
573 w->attrs = flavor[(unsigned) n % SIZEOF(flavor)];
577 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
578 fprintf(stderr, "%s: out of memory\n", *argv);
579 ExitProgram(EXIT_FAILURE);
582 for (x = length; --x >= 0;)
584 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
585 fprintf(stderr, "%s: out of memory\n", *argv);
586 ExitProgram(EXIT_FAILURE);
589 for (y = length; --y >= 0;)
595 for (y = last_y; --y >= 0;) {
596 for (x = COLS; --x >= 0;) {
597 addch((chtype) (*p++));
603 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
604 nodelay(stdscr, TRUE);
608 if ((ch = get_input()) > 0) {
610 if (trace_start || trace_end) {
611 if (generation == trace_start) {
612 curses_trace(TRACE_CALLS);
614 } else if (generation == trace_end) {
624 if (ch == KEY_RESIZE) {
625 USING_WINDOW(stdscr, update_refs);
630 * Make it simple to put this into single-step mode, or resume
631 * normal operation -T.Dickey
636 Trace(("done - quitting"));
638 } else if (ch == 's') {
639 nodelay(stdscr, FALSE);
640 } else if (ch == ' ') {
641 nodelay(stdscr, TRUE);
645 done = draw_all_worms();
647 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
653 for (y = 0; y < max_refs; y++) {
657 for (n = number, w = &worm[0]; --n >= 0; w++) {
664 * Do this just in case one of the threads did not really exit.
666 Trace(("join all threads"));
667 for (n = 0; n < number; n++) {
668 pthread_join(worm[n].thread, NULL);
671 ExitProgram(EXIT_SUCCESS);