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.77 2018/05/20 19:55:42 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 GCC_UNUSED)
409 if (last_x != COLS - 1) {
410 for (y = 0; y <= last_y; y++) {
411 refs[y] = typeRealloc(int, (size_t) COLS, refs[y]);
413 failed("update_refs");
414 for (x = last_x + 1; x < COLS; x++)
419 if (last_y != LINES - 1) {
420 for (y = LINES; y <= last_y; y++)
423 refs = typeRealloc(int *, (size_t) LINES, refs);
424 for (y = last_y + 1; y < LINES; y++) {
425 refs[y] = typeMalloc(int, (size_t) COLS);
427 failed("update_refs");
428 for (x = 0; x < COLS; x++)
440 static const char *msg[] =
442 "Usage: worm [options]"
445 #if HAVE_USE_DEFAULT_COLORS
446 ," -d invoke use_default_colors"
448 ," -f fill screen with copies of \"WORM\" at start"
449 ," -l <n> set length of worms"
450 ," -n <n> set number of worms"
451 ," -t leave trail of \".\""
453 ," -T <start>,<end> set trace interval"
454 ," -N suppress cursor-movement optimization"
459 for (n = 0; n < SIZEOF(msg); n++)
460 fprintf(stderr, "%s\n", msg[n]);
462 ExitProgram(EXIT_FAILURE);
466 main(int argc, char *argv[])
474 #if HAVE_USE_DEFAULT_COLORS
478 setlocale(LC_ALL, "");
480 while ((ch = getopt(argc, argv, "dfl:n:tT:N")) != -1) {
482 #if HAVE_USE_DEFAULT_COLORS
491 if ((length = atoi(optarg)) < 2 || length > MAX_LENGTH) {
492 fprintf(stderr, "%s: Invalid length\n", *argv);
497 if ((number = atoi(optarg)) < 1 || number > MAX_WORMS) {
498 fprintf(stderr, "%s: Invalid number of worms\n", *argv);
507 if (sscanf(optarg, "%d,%d", &trace_start, &trace_end) != 2)
511 _nc_optimize_enable ^= OPTIMIZE_ALL; /* declared by ncurses */
522 signal(SIGINT, onsig);
535 int bg = COLOR_BLACK;
537 #if HAVE_USE_DEFAULT_COLORS
538 if (opt_d && (use_default_colors() == OK))
542 #define SET_COLOR(num, fg) \
543 init_pair(num+1, (short) fg, (short) bg); \
544 flavor[num] |= (chtype) COLOR_PAIR(num+1) | A_BOLD
546 SET_COLOR(0, COLOR_GREEN);
547 SET_COLOR(1, COLOR_RED);
548 SET_COLOR(2, COLOR_CYAN);
549 SET_COLOR(3, COLOR_WHITE);
550 SET_COLOR(4, COLOR_MAGENTA);
551 SET_COLOR(5, COLOR_BLUE);
552 SET_COLOR(6, COLOR_YELLOW);
557 refs = typeMalloc(int *, (size_t) max_refs);
558 for (y = 0; y < max_refs; y++) {
559 refs[y] = typeMalloc(int, (size_t) COLS);
560 for (x = 0; x < COLS; x++) {
566 /* if addressing the lower right corner doesn't work in your curses */
567 refs[last_y][last_x] = 1;
568 #endif /* BADCORNER */
570 for (n = number, w = &worm[0]; --n >= 0; w++) {
571 w->attrs = flavor[(unsigned) n % SIZEOF(flavor)];
575 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
576 fprintf(stderr, "%s: out of memory\n", *argv);
577 ExitProgram(EXIT_FAILURE);
580 for (x = length; --x >= 0;)
582 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
583 fprintf(stderr, "%s: out of memory\n", *argv);
584 ExitProgram(EXIT_FAILURE);
587 for (y = length; --y >= 0;)
593 for (y = last_y; --y >= 0;) {
594 for (x = COLS; --x >= 0;) {
595 addch((chtype) (*p++));
601 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
602 nodelay(stdscr, TRUE);
606 if ((ch = get_input()) > 0) {
608 if (trace_start || trace_end) {
609 if (generation == trace_start) {
612 } else if (generation == trace_end) {
622 if (ch == KEY_RESIZE) {
623 USING_WINDOW(stdscr, update_refs);
628 * Make it simple to put this into single-step mode, or resume
629 * normal operation -T.Dickey
634 Trace(("done - quitting"));
636 } else if (ch == 's') {
637 nodelay(stdscr, FALSE);
638 } else if (ch == ' ') {
639 nodelay(stdscr, TRUE);
643 done = draw_all_worms();
645 USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
651 for (y = 0; y < max_refs; y++) {
655 for (n = number, w = &worm[0]; --n >= 0; w++) {
662 * Do this just in case one of the threads did not really exit.
664 Trace(("join all threads"));
665 for (n = 0; n < number; n++) {
666 pthread_join(worm[n].thread, NULL);
669 ExitProgram(EXIT_SUCCESS);