1 /****************************************************************************
2 * Copyright (c) 1998-2008,2012 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! :-)
51 -f fill screen with copies of 'WORM' at start.
52 -l <n> set worm length
53 -n <n> set number of worms
54 -t make worms leave droppings
55 -T <start> <end> set trace interval
56 -S set single-stepping during trace interval
57 -N suppress cursor-movement optimization
59 This program makes a good torture-test for the ncurses cursor-optimization
60 code. You can use -T to set the worm move interval over which movement
61 traces will be dumped. The program stops and waits for one character of
62 input at the beginning and end of the interval.
64 $Id: worm.c,v 1.61 2012/10/27 19:37:17 tom Exp $
67 #include <test.priv.h>
76 #define MAX_LENGTH 1024
78 static chtype flavor[] =
80 'O', '*', '#', '$', '%', '0', '@',
82 static const int xinc[] =
84 1, 1, 1, 0, -1, -1, -1, 0
87 -1, 0, 1, 1, 1, 0, -1, -1
101 static unsigned long sequence = 0;
102 static bool quitting = FALSE;
104 static WORM worm[MAX_WORMS];
106 static int last_x, last_y;
108 static const char *field;
109 static int length = 16, number = 3;
110 static chtype trail = ' ';
112 static unsigned pending;
114 static int generation, trace_start, trace_end;
117 static const struct options {
205 failed(const char *s)
209 ExitProgram(EXIT_FAILURE);
215 USING_WINDOW(stdscr, wrefresh);
221 onsig(int sig GCC_UNUSED)
224 ExitProgram(EXIT_FAILURE);
230 long r = (rand() & 077777);
231 return ((double) r / 32768.);
235 draw_worm(WINDOW *win, void *data)
237 WORM *w = (WORM *) data;
238 const struct options *op;
239 unsigned mask = (unsigned) (~(1 << (w - worm)));
240 chtype attrs = w->attrs | ((mask & pending) ? A_REVERSE : 0);
248 if ((x = w->xpos[h = w->head]) < 0) {
249 wmove(win, y = w->ypos[h] = last_y, x = w->xpos[h] = 0);
264 if (w->xpos[w->head = h] >= 0) {
270 && --refs[y1][x1] == 0) {
292 : normal))))[w->orientation];
299 w->orientation = op->opts[0];
302 w->orientation = op->opts[(int) (ranf() * (double) op->nopts)];
307 x += xinc[w->orientation];
308 y += yinc[w->orientation];
325 quit_worm(int bitnum)
327 pending |= (1 << bitnum);
328 napms(10); /* let the other thread(s) have a chance */
329 pending &= ~(1 << bitnum);
334 start_worm(void *arg)
336 unsigned long compare = 0;
337 Trace(("start_worm"));
338 while (!quit_worm(((struct worm *) arg) - worm)) {
339 while (compare < sequence) {
342 use_window(stdscr, draw_worm, arg);
344 draw_worm(stdscr, arg);
348 Trace(("...start_worm (done)"));
361 static bool first = TRUE;
364 for (n = 0, w = &worm[0]; n < number; n++, w++) {
366 rc = pthread_create(&(w->thread), NULL, start_worm, w);
370 for (n = 0, w = &worm[0]; n < number; n++, w++) {
373 USING_WINDOW2(stdscr, draw_worm, w)
388 ch = USING_WINDOW(stdscr, wgetch);
394 update_refs(WINDOW *win)
399 if (last_x != COLS - 1) {
400 for (y = 0; y <= last_y; y++) {
401 refs[y] = typeRealloc(int, (size_t) COLS, refs[y]);
403 failed("update_refs");
404 for (x = last_x + 1; x < COLS; x++)
409 if (last_y != LINES - 1) {
410 for (y = LINES; y <= last_y; y++)
412 refs = typeRealloc(int *, (size_t) LINES, refs);
413 for (y = last_y + 1; y < LINES; y++) {
414 refs[y] = typeMalloc(int, (size_t) COLS);
416 failed("update_refs");
417 for (x = 0; x < COLS; x++)
427 main(int argc, char *argv[])
435 setlocale(LC_ALL, "");
437 for (x = 1; x < argc; x++) {
449 if ((length = atoi(argv[x])) < 2 || length > MAX_LENGTH) {
450 fprintf(stderr, "%s: Invalid length\n", *argv);
451 ExitProgram(EXIT_FAILURE);
457 if ((number = atoi(argv[x])) < 1 || number > MAX_WORMS) {
458 fprintf(stderr, "%s: Invalid number of worms\n", *argv);
459 ExitProgram(EXIT_FAILURE);
467 trace_start = atoi(argv[++x]);
468 trace_end = atoi(argv[++x]);
471 _nc_optimize_enable ^= OPTIMIZE_ALL; /* declared by ncurses */
477 "usage: %s [-field] [-length #] [-number #] [-trail]\n", *argv);
478 ExitProgram(EXIT_FAILURE);
482 signal(SIGINT, onsig);
495 int bg = COLOR_BLACK;
497 #if HAVE_USE_DEFAULT_COLORS
498 if (use_default_colors() == OK)
502 #define SET_COLOR(num, fg) \
503 init_pair(num+1, (short) fg, (short) bg); \
504 flavor[num] |= COLOR_PAIR(num+1) | A_BOLD
506 SET_COLOR(0, COLOR_GREEN);
507 SET_COLOR(1, COLOR_RED);
508 SET_COLOR(2, COLOR_CYAN);
509 SET_COLOR(3, COLOR_WHITE);
510 SET_COLOR(4, COLOR_MAGENTA);
511 SET_COLOR(5, COLOR_BLUE);
512 SET_COLOR(6, COLOR_YELLOW);
516 refs = typeMalloc(int *, (size_t) LINES);
517 for (y = 0; y < LINES; y++) {
518 refs[y] = typeMalloc(int, (size_t) COLS);
519 for (x = 0; x < COLS; x++) {
525 /* if addressing the lower right corner doesn't work in your curses */
526 refs[last_y][last_x] = 1;
527 #endif /* BADCORNER */
529 for (n = number, w = &worm[0]; --n >= 0; w++) {
530 w->attrs = flavor[(unsigned) n % SIZEOF(flavor)];
534 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
535 fprintf(stderr, "%s: out of memory\n", *argv);
536 ExitProgram(EXIT_FAILURE);
539 for (x = length; --x >= 0;)
541 if (!(ip = typeMalloc(int, (size_t) (length + 1)))) {
542 fprintf(stderr, "%s: out of memory\n", *argv);
543 ExitProgram(EXIT_FAILURE);
546 for (y = length; --y >= 0;)
552 for (y = last_y; --y >= 0;) {
553 for (x = COLS; --x >= 0;) {
554 addch((chtype) (*p++));
560 USING_WINDOW(stdscr, wrefresh);
561 nodelay(stdscr, TRUE);
567 if ((ch = get_input()) > 0) {
569 if (trace_start || trace_end) {
570 if (generation == trace_start) {
573 } else if (generation == trace_end) {
583 if (ch == KEY_RESIZE) {
584 USING_WINDOW(stdscr, update_refs);
589 * Make it simple to put this into single-step mode, or resume
590 * normal operation -T.Dickey
596 } else if (ch == 's') {
597 nodelay(stdscr, FALSE);
598 } else if (ch == ' ') {
599 nodelay(stdscr, TRUE);
603 done = draw_all_worms();
605 USING_WINDOW(stdscr, wrefresh);
611 for (y = 0; y < LINES; y++) {
615 for (n = number, w = &worm[0]; --n >= 0; w++) {
622 * Do this just in case one of the threads did not really exit.
624 Trace(("join all threads"));
625 for (n = 0; n < number; n++) {
626 pthread_join(worm[n].thread, NULL);
629 ExitProgram(EXIT_SUCCESS);