1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 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.56 2008/02/23 23:08:57 tom Exp $
67 #include <test.priv.h>
76 #define MAX_LENGTH 1024
78 static chtype flavor[] =
80 'O', '*', '#', '$', '%', '0', '@',
82 static const short 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 = ' ';
113 static int generation, trace_start, trace_end;
116 static const struct options {
206 USING_WINDOW(stdscr, wrefresh);
212 onsig(int sig GCC_UNUSED)
215 ExitProgram(EXIT_FAILURE);
221 long r = (rand() & 077777);
222 return ((float) r / 32768.);
226 draw_worm(WINDOW *win, void *data)
228 WORM *w = (WORM *) data;
229 const struct options *op;
237 if ((x = w->xpos[h = w->head]) < 0) {
238 wmove(win, y = w->ypos[h] = last_y, x = w->xpos[h] = 0);
239 waddch(win, w->attrs);
253 if (w->xpos[w->head = h] >= 0) {
259 && --refs[y1][x1] == 0) {
281 : normal))))[w->orientation];
288 w->orientation = op->opts[0];
291 w->orientation = op->opts[(int) (ranf() * (float) op->nopts)];
296 x += xinc[w->orientation];
297 y += yinc[w->orientation];
302 waddch(win, w->attrs);
316 napms(10); /* let the other thread(s) have a chance */
321 start_worm(void *arg)
323 unsigned long compare = 0;
324 Trace(("start_worm"));
325 while (!quit_worm()) {
326 while (compare < sequence) {
328 use_window(stdscr, draw_worm, arg);
331 Trace(("...start_worm (done)"));
344 static bool first = TRUE;
347 for (n = 0, w = &worm[0]; n < number; n++, w++) {
349 rc = pthread_create(&(w->thread), NULL, start_worm, w);
353 for (n = 0, w = &worm[0]; n < number; n++, w++) {
354 if (use_window(stdscr, draw_worm, w))
365 ch = USING_WINDOW(stdscr, wgetch);
371 update_refs(WINDOW *win)
376 if (last_x != COLS - 1) {
377 for (y = 0; y <= last_y; y++) {
378 refs[y] = typeRealloc(short, COLS, refs[y]);
379 for (x = last_x + 1; x < COLS; x++)
384 if (last_y != LINES - 1) {
385 for (y = LINES; y <= last_y; y++)
387 refs = typeRealloc(short *, LINES, refs);
388 for (y = last_y + 1; y < LINES; y++) {
389 refs[y] = typeMalloc(short, COLS);
390 for (x = 0; x < COLS; x++)
400 main(int argc, char *argv[])
408 setlocale(LC_ALL, "");
410 for (x = 1; x < argc; x++) {
422 if ((length = atoi(argv[x])) < 2 || length > MAX_LENGTH) {
423 fprintf(stderr, "%s: Invalid length\n", *argv);
424 ExitProgram(EXIT_FAILURE);
430 if ((number = atoi(argv[x])) < 1 || number > MAX_WORMS) {
431 fprintf(stderr, "%s: Invalid number of worms\n", *argv);
432 ExitProgram(EXIT_FAILURE);
440 trace_start = atoi(argv[++x]);
441 trace_end = atoi(argv[++x]);
444 _nc_optimize_enable ^= OPTIMIZE_ALL; /* declared by ncurses */
450 "usage: %s [-field] [-length #] [-number #] [-trail]\n", *argv);
451 ExitProgram(EXIT_FAILURE);
455 signal(SIGINT, onsig);
468 int bg = COLOR_BLACK;
470 #if HAVE_USE_DEFAULT_COLORS
471 if (use_default_colors() == OK)
475 #define SET_COLOR(num, fg) \
476 init_pair(num+1, fg, bg); \
477 flavor[num] |= COLOR_PAIR(num+1) | A_BOLD
479 SET_COLOR(0, COLOR_GREEN);
480 SET_COLOR(1, COLOR_RED);
481 SET_COLOR(2, COLOR_CYAN);
482 SET_COLOR(3, COLOR_WHITE);
483 SET_COLOR(4, COLOR_MAGENTA);
484 SET_COLOR(5, COLOR_BLUE);
485 SET_COLOR(6, COLOR_YELLOW);
489 refs = typeMalloc(short *, LINES);
490 for (y = 0; y < LINES; y++) {
491 refs[y] = typeMalloc(short, COLS);
492 for (x = 0; x < COLS; x++) {
498 /* if addressing the lower right corner doesn't work in your curses */
499 refs[last_y][last_x] = 1;
500 #endif /* BADCORNER */
502 for (n = number, w = &worm[0]; --n >= 0; w++) {
503 w->attrs = flavor[n % SIZEOF(flavor)];
507 if (!(ip = typeMalloc(short, (length + 1)))) {
508 fprintf(stderr, "%s: out of memory\n", *argv);
509 ExitProgram(EXIT_FAILURE);
512 for (x = length; --x >= 0;)
514 if (!(ip = typeMalloc(short, (length + 1)))) {
515 fprintf(stderr, "%s: out of memory\n", *argv);
516 ExitProgram(EXIT_FAILURE);
519 for (y = length; --y >= 0;)
525 for (y = last_y; --y >= 0;) {
526 for (x = COLS; --x >= 0;) {
527 addch((chtype) (*p++));
533 USING_WINDOW(stdscr, wrefresh);
534 nodelay(stdscr, TRUE);
540 if ((ch = get_input()) > 0) {
542 if (trace_start || trace_end) {
543 if (generation == trace_start) {
546 } else if (generation == trace_end) {
556 if (ch == KEY_RESIZE) {
557 USING_WINDOW(stdscr, update_refs);
562 * Make it simple to put this into single-step mode, or resume
563 * normal operation -T.Dickey
569 } else if (ch == 's') {
570 nodelay(stdscr, FALSE);
571 } else if (ch == ' ') {
572 nodelay(stdscr, TRUE);
576 done = draw_all_worms();
578 USING_WINDOW(stdscr, wrefresh);
584 for (y = 0; y < LINES; y++) {
588 for (n = number, w = &worm[0]; --n >= 0; w++) {
595 * Do this just in case one of the threads did not really exit.
597 Trace(("join all threads"));
598 for (n = 0; n < number; n++) {
599 pthread_join(worm[n].thread, NULL);
602 ExitProgram(EXIT_SUCCESS);