1 /****************************************************************************
2 * Copyright (c) 1998-2017,2018 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
30 @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
31 @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
32 @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
33 @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
34 @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
35 @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
36 @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
37 @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
38 @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
41 Caltech High Energy Physics
44 Hacks to turn this into a test frame for cursor movement:
45 Eric S. Raymond <esr@snark.thyrsus.com>
48 July 1995 (esr): worms is now in living color! :-)
50 This program makes a good torture-test for the ncurses cursor-optimization
51 code. You can use -T to set the worm move interval over which movement
52 traces will be dumped. The program stops and waits for one character of
53 input at the beginning and end of the interval.
55 $Id: worm.c,v 1.79 2018/07/21 21:40:00 tom Exp $
58 #include <test.priv.h>
60 #ifndef NCURSES_VERSION
71 #define MAX_LENGTH 1024
73 static chtype flavor[] =
75 'O', '*', '#', '$', '%', '0', '@',
77 static const int xinc[] =
79 1, 1, 1, 0, -1, -1, -1, 0
82 -1, 0, 1, 1, 1, 0, -1, -1
96 static unsigned long sequence = 0;
97 static bool quitting = FALSE;
99 static WORM worm[MAX_WORMS];
102 static int last_x, last_y;
104 static const char *field;
105 static int length = 16, number = 3;
106 static chtype trail = ' ';
108 static unsigned pending;
110 static int generation, trace_start, trace_end;
113 static const struct options {
202 safe_wgetch(WINDOW *w, void *data GCC_UNUSED)
207 safe_wrefresh(WINDOW *w, void *data GCC_UNUSED)
215 failed(const char *s)
219 ExitProgram(EXIT_FAILURE);
226 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
231 onsig(int sig GCC_UNUSED)
234 ExitProgram(EXIT_FAILURE);
240 long r = (rand() & 077777);
241 return ((double) r / 32768.);
245 draw_worm(WINDOW *win, void *data)
247 WORM *w = (WORM *) data;
248 const struct options *op;
249 unsigned mask = (unsigned) (~(1 << (w - worm)));
250 chtype attrs = w->attrs | ((mask & pending) ? A_REVERSE : 0);
258 if ((x = w->xpos[h = w->head]) < 0) {
259 wmove(win, y = w->ypos[h] = last_y, x = w->xpos[h] = 0);
274 if (w->xpos[w->head = h] >= 0) {
280 && --refs[y1][x1] == 0) {
302 : normal))))[w->orientation];
307 Trace(("done - draw_worm"));
310 w->orientation = op->opts[0];
313 w->orientation = op->opts[(int) (ranf() * (double) op->nopts)];
318 x += xinc[w->orientation];
319 y += yinc[w->orientation];
336 quit_worm(int bitnum)
338 pending = (pending | (unsigned) (1 << bitnum));
339 napms(10); /* let the other thread(s) have a chance */
340 pending = (pending & (unsigned) ~(1 << bitnum));
345 start_worm(void *arg)
347 unsigned long compare = 0;
348 Trace(("start_worm"));
349 while (!quit_worm((int) (((struct worm *) arg) - worm))) {
350 while (compare < sequence) {
353 use_window(stdscr, draw_worm, arg);
355 draw_worm(stdscr, arg);
359 Trace(("...start_worm (done)"));
372 static bool first = TRUE;
375 for (n = 0, w = &worm[0]; n < number; n++, w++) {
376 (void) pthread_create(&(w->thread), NULL, start_worm, w);
380 for (n = 0, w = &worm[0]; n < number; n++, w++) {
383 USING_WINDOW2(stdscr, draw_worm, w)
398 ch = USING_WINDOW1(stdscr, wgetch, safe_wgetch);
404 update_refs(WINDOW *win, void *data)
410 if (last_x != COLS - 1) {
411 for (y = 0; y <= last_y; y++) {
412 refs[y] = typeRealloc(int, (size_t) COLS, refs[y]);
414 failed("update_refs");
415 for (x = last_x + 1; x < COLS; x++)
420 if (last_y != LINES - 1) {
421 for (y = LINES; y <= last_y; y++)
424 refs = typeRealloc(int *, (size_t) LINES, refs);
425 for (y = last_y + 1; y < LINES; y++) {
426 refs[y] = typeMalloc(int, (size_t) COLS);
428 failed("update_refs");
429 for (x = 0; x < COLS; x++)
441 static const char *msg[] =
443 "Usage: worm [options]"
446 #if HAVE_USE_DEFAULT_COLORS
447 ," -d invoke use_default_colors"
449 ," -f fill screen with copies of \"WORM\" at start"
450 ," -l <n> set length of worms"
451 ," -n <n> set number of worms"
452 ," -t leave trail of \".\""
454 ," -T <start>,<end> set trace interval"
455 ," -N suppress cursor-movement optimization"
460 for (n = 0; n < SIZEOF(msg); n++)
461 fprintf(stderr, "%s\n", msg[n]);
463 ExitProgram(EXIT_FAILURE);
467 main(int argc, char *argv[])
475 #if HAVE_USE_DEFAULT_COLORS
479 setlocale(LC_ALL, "");
481 while ((ch = getopt(argc, argv, "dfl:n:tT:N")) != -1) {
483 #if HAVE_USE_DEFAULT_COLORS
492 if ((length = atoi(optarg)) < 2 || length > MAX_LENGTH) {
493 fprintf(stderr, "%s: Invalid length\n", *argv);
498 if ((number = atoi(optarg)) < 1 || number > MAX_WORMS) {
499 fprintf(stderr, "%s: Invalid number of worms\n", *argv);
508 if (sscanf(optarg, "%d,%d", &trace_start, &trace_end) != 2)
512 _nc_optimize_enable ^= OPTIMIZE_ALL; /* declared by ncurses */
523 signal(SIGINT, onsig);
536 int bg = COLOR_BLACK;
538 #if HAVE_USE_DEFAULT_COLORS
539 if (opt_d && (use_default_colors() == OK))
543 #define SET_COLOR(num, fg) \
544 init_pair(num+1, (short) fg, (short) bg); \
545 flavor[num] |= (chtype) COLOR_PAIR(num+1) | A_BOLD
547 SET_COLOR(0, COLOR_GREEN);
548 SET_COLOR(1, COLOR_RED);
549 SET_COLOR(2, COLOR_CYAN);
550 SET_COLOR(3, COLOR_WHITE);
551 SET_COLOR(4, COLOR_MAGENTA);
552 SET_COLOR(5, COLOR_BLUE);
553 SET_COLOR(6, COLOR_YELLOW);
558 refs = typeMalloc(int *, (size_t) max_refs);
559 for (y = 0; y < max_refs; y++) {
560 refs[y] = typeMalloc(int, (size_t) COLS);
561 for (x = 0; x < COLS; x++) {
567 /* if addressing the lower right corner doesn't work in your curses */
568 refs[last_y][last_x] = 1;
569 #endif /* BADCORNER */
571 for (n = number, w = &worm[0]; --n >= 0; w++) {
572 w->attrs = flavor[(unsigned) n % SIZEOF(flavor)];
576 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
577 fprintf(stderr, "%s: out of memory\n", *argv);
578 ExitProgram(EXIT_FAILURE);
581 for (x = length; --x >= 0;)
583 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
584 fprintf(stderr, "%s: out of memory\n", *argv);
585 ExitProgram(EXIT_FAILURE);
588 for (y = length; --y >= 0;)
594 for (y = last_y; --y >= 0;) {
595 for (x = COLS; --x >= 0;) {
596 addch((chtype) (*p++));
602 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
603 nodelay(stdscr, TRUE);
607 if ((ch = get_input()) > 0) {
609 if (trace_start || trace_end) {
610 if (generation == trace_start) {
613 } else if (generation == trace_end) {
623 if (ch == KEY_RESIZE) {
624 USING_WINDOW(stdscr, update_refs);
629 * Make it simple to put this into single-step mode, or resume
630 * normal operation -T.Dickey
635 Trace(("done - quitting"));
637 } else if (ch == 's') {
638 nodelay(stdscr, FALSE);
639 } else if (ch == ' ') {
640 nodelay(stdscr, TRUE);
644 done = draw_all_worms();
646 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
652 for (y = 0; y < max_refs; y++) {
656 for (n = number, w = &worm[0]; --n >= 0; w++) {
663 * Do this just in case one of the threads did not really exit.
665 Trace(("join all threads"));
666 for (n = 0; n < number; n++) {
667 pthread_join(worm[n].thread, NULL);
670 ExitProgram(EXIT_SUCCESS);