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.53 2008/01/19 20:56:38 tom Exp $
67 #include <test.priv.h>
73 static chtype flavor[] =
75 'O', '*', '#', '$', '%', '0', '@',
77 static const short 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;
102 static const char *field;
103 static int length = 16, number = 3;
104 static chtype trail = ' ';
107 static int generation, trace_start, trace_end;
110 static const struct options {
207 onsig(int sig GCC_UNUSED)
210 ExitProgram(EXIT_FAILURE);
216 long r = (rand() & 077777);
217 return ((float) r / 32768.);
221 draw_worm(WINDOW *win, void *data)
223 WORM *w = (WORM *) data;
224 const struct options *op;
230 int bottom = LINES - 1;
235 if ((x = w->xpos[h = w->head]) < 0) {
236 wmove(win, y = w->ypos[h] = bottom, x = w->xpos[h] = 0);
237 waddch(win, w->attrs);
251 if (w->xpos[w->head = h] >= 0) {
257 && --refs[y1][x1] == 0) {
279 : normal))))[w->orientation];
286 w->orientation = op->opts[0];
289 w->orientation = op->opts[(int) (ranf() * (float) op->nopts)];
294 x += xinc[w->orientation];
295 y += yinc[w->orientation];
300 waddch(win, w->attrs);
310 #if !defined(NCURSES_VERSION_PATCH) || (NCURSES_VERSION_PATCH < 20070915) || !NCURSES_EXT_FUNCS
312 use_window(WINDOW *win, int (*func) (WINDOW *, void *), void *data)
314 return func(win, data);
322 napms(10); /* let the other thread(s) have a chance */
327 start_worm(void *arg)
329 unsigned long compare = 0;
330 while (!quit_worm()) {
331 while (compare < sequence) {
333 use_window(stdscr, draw_worm, arg);
348 static bool first = TRUE;
351 for (n = 0, w = &worm[0]; n < number; n++, w++) {
353 rc = pthread_create(&(w->thread), NULL, start_worm, w);
357 for (n = 0, w = &worm[0]; n < number; n++, w++) {
358 if (use_window(stdscr, draw_worm, w))
370 ch = use_window(stdscr, (NCURSES_CALLBACK) wgetch, stdscr);
378 main(int argc, char *argv[])
387 setlocale(LC_ALL, "");
389 for (x = 1; x < argc; x++) {
401 if ((length = atoi(argv[x])) < 2 || length > 1024) {
402 fprintf(stderr, "%s: Invalid length\n", *argv);
403 ExitProgram(EXIT_FAILURE);
409 if ((number = atoi(argv[x])) < 1 || number > 40) {
410 fprintf(stderr, "%s: Invalid number of worms\n", *argv);
411 ExitProgram(EXIT_FAILURE);
419 trace_start = atoi(argv[++x]);
420 trace_end = atoi(argv[++x]);
423 _nc_optimize_enable ^= OPTIMIZE_ALL; /* declared by ncurses */
429 "usage: %s [-field] [-length #] [-number #] [-trail]\n", *argv);
430 ExitProgram(EXIT_FAILURE);
434 signal(SIGINT, onsig);
447 int bg = COLOR_BLACK;
449 #if HAVE_USE_DEFAULT_COLORS
450 if (use_default_colors() == OK)
454 #define SET_COLOR(num, fg) \
455 init_pair(num+1, fg, bg); \
456 flavor[num] |= COLOR_PAIR(num+1) | A_BOLD
458 SET_COLOR(0, COLOR_GREEN);
459 SET_COLOR(1, COLOR_RED);
460 SET_COLOR(2, COLOR_CYAN);
461 SET_COLOR(3, COLOR_WHITE);
462 SET_COLOR(4, COLOR_MAGENTA);
463 SET_COLOR(5, COLOR_BLUE);
464 SET_COLOR(6, COLOR_YELLOW);
468 refs = typeMalloc(short *, LINES);
469 for (y = 0; y < LINES; y++) {
470 refs[y] = typeMalloc(short, COLS);
471 for (x = 0; x < COLS; x++) {
477 /* if addressing the lower right corner doesn't work in your curses */
478 refs[bottom][last] = 1;
479 #endif /* BADCORNER */
481 for (n = number, w = &worm[0]; --n >= 0; w++) {
482 w->attrs = flavor[n % SIZEOF(flavor)];
486 if (!(ip = typeMalloc(short, (length + 1)))) {
487 fprintf(stderr, "%s: out of memory\n", *argv);
488 ExitProgram(EXIT_FAILURE);
491 for (x = length; --x >= 0;)
493 if (!(ip = typeMalloc(short, (length + 1)))) {
494 fprintf(stderr, "%s: out of memory\n", *argv);
495 ExitProgram(EXIT_FAILURE);
498 for (y = length; --y >= 0;)
504 for (y = bottom; --y >= 0;) {
505 for (x = COLS; --x >= 0;) {
506 addch((chtype) (*p++));
513 nodelay(stdscr, TRUE);
519 if ((ch = get_input()) > 0) {
521 if (trace_start || trace_end) {
522 if (generation == trace_start) {
525 } else if (generation == trace_end) {
534 if (ch == KEY_RESIZE) {
535 if (last != COLS - 1) {
536 for (y = 0; y <= bottom; y++) {
537 refs[y] = typeRealloc(short, COLS, refs[y]);
538 for (x = last + 1; x < COLS; x++)
543 if (bottom != LINES - 1) {
544 for (y = LINES; y <= bottom; y++)
546 refs = typeRealloc(short *, LINES, refs);
547 for (y = bottom + 1; y < LINES; y++) {
548 refs[y] = typeMalloc(short, COLS);
549 for (x = 0; x < COLS; x++)
557 * Make it simple to put this into single-step mode, or resume
558 * normal operation -T.Dickey
564 } else if (ch == 's') {
565 nodelay(stdscr, FALSE);
566 } else if (ch == ' ') {
567 nodelay(stdscr, TRUE);
571 done = draw_all_worms();
578 for (y = 0; y < LINES; y++) {
582 for (n = number, w = &worm[0]; --n >= 0; w++) {
589 * Do this just in case one of the threads did not really exit.
591 for (n = 0; n < number; n++) {
592 pthread_join(worm[n].thread, NULL);
595 ExitProgram(EXIT_SUCCESS);