3 * @(#) Copyright (C) 2001 Tyler Pierce (tyler@alumni.brown.edu)
4 * The full program, with documentation, is available at:
5 * http://freshmeat.net/projects/fdm
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation. No representations are made about the suitability of this
12 * software for any purpose. It is provided "as is" without express or
24 #ifdef VERMICULATE_STANDALONE
28 #include <X11/Xutil.h>
29 #include <X11/Xatom.h>
30 #include <X11/Xresource.h>
32 #include "screenhack.h"
34 #endif /* VERMICULATE_STANDALONE */
37 #define degs2 (degs/2)
38 #define degs4 (degs/4)
39 #define degs8 (degs/8)
40 #define dtor 0.0174532925 /* pi / degs2; */
42 #define tailmax (thrmax * 2 + 1)
44 #define ymax (hei - 1)
46 #define xmax (wid - 1)
51 #define wraparound(VAL,LOWER,UPPER) { \
53 VAL -= UPPER - LOWER; \
54 else if (VAL < LOWER) \
55 VAL += UPPER - LOWER; }
56 #define arrcpy(DEST,SRC) memcpy (DEST, SRC, sizeof(DEST))
59 typedef unsigned char banktype[thrmax];
61 typedef struct linedata
63 int deg, spiturn, turnco, turnsize;
69 int tmode, tsc, tslen, tclim, otslen, ctinc, reclen, recpos, circturn, prey,
71 int xrec[rlmax + 1], yrec[rlmax + 1];
73 Bool filled, killwalls, vhfollow,
74 selfbounce, tailfollow, realbounce, little;
78 #ifdef VERMICULATE_STANDALONE
79 static XEvent myevent;
80 static Bool use_root = False;
81 static unsigned char rgb[256][3];
84 char *progclass = "Vermiculate";
91 XrmOptionDescRec options[] = {
92 {"-speed", ".speed", XrmoptionSepArg, 0},
93 {"-instring", ".instring", XrmoptionSepArg, 0},
96 #endif /* VERMICULATE_STANDALONE */
98 static Display *mydpy;
99 static Window mywindow;
101 static Colormap mycmap;
102 static XWindowAttributes xgwa;
103 static Bool neednewkey = True;
104 static XColor mycolors[tailmax];
106 static int hei = 500, wid = 500, speed = 100;
107 static Bool erasing = True;
108 static char *instring = 0;
109 static int max_ticks;
111 static struct stringAndSpeed
118 { "]]]]]]]]7ces123400-9-8#c123456#s9998880004#ma3#car9ma6#c-#r1", 600} ,
119 { "bmarrrr#c1234#lc5678#lyet]", 600} ,
120 { "AEBMN222222223#CAR9CAD4CAOV", 150} ,
121 { "mn333#c23#f1#]]]]]]]]]]]3bc9#r9#c78#f9#ma4#", 600} ,
122 { "AEBMN22222#CAD4CAORc1#f2#c1#r6", 100} ,
123 /* { "mn6666666#c1i#f1#y2#sy2#vyety1#ry13i#l", 40} , */
124 { "aebmnrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr#", 500} ,
125 { "bg+++++++++++++++++++++++#mnrrrrrrrrrrrrrrrrrrrrrrr#y1#k", 500} ,
126 { "BMN22222223#CAD4CAOVYAS", 150} ,
127 /* { "aebmnrrrrrrrrrrrrrrrr#yaryakg--#", 100} , */
128 { "mn6rrrrrrrrrrrrrrr#by1i#lcalc1#fnyav", 200 } ,
129 { "mn1rrrrrrrrrrrrrrr#by1i#lcalc1#fn", 200 }
132 static real sinof[degs], cosof[degs], tanof[degs];
133 static unsigned char *point;
135 static linedata thread[thrmax];
136 static banktype bank;
137 static int bankt, boxw, boxh, curviness, gridden, ogd, bordcorn;
138 static unsigned char bordcol, threads;
139 static char ch, boolop;
142 wasakeypressed (void)
144 if (!neednewkey || *instring != 0)
147 #ifdef VERMICULATE_STANDALONE
148 return !(neednewkey =
149 !XCheckWindowEvent (mydpy, mywindow, KeyPressMask, &myevent));
152 #endif /* VERMICULATE_STANDALONE */
161 #ifdef VERMICULATE_STANDALONE
165 XWindowEvent (mydpy, mywindow, KeyPressMask, &myevent);
166 XLookupString (&myevent.xkey, key_buffer, 1, &key_sym, NULL);
167 readkey_result = key_sym;
169 readkey_result = '#';
170 #endif /* VERMICULATE_STANDALONE */
175 readkey_result = *instring;
178 return toupper (readkey_result);
182 random1 (unsigned int i)
184 return (ya_random () % i);
202 XClearWindow (mydpy, mywindow);
203 memset (point, 0, wid * hei);
207 sp (int x, int y, int c)
209 XSetForeground (mydpy, mygc, mycolors[c].pixel);
210 XDrawPoint (mydpy, mywindow, mygc, x, y);
211 point[(wid * y) + x] = c;
217 return point[(wid * y) + x];
221 redraw (int x, int y, int width, int height)
224 for (xc = x; xc <= x + width - 1; xc++)
225 for (yc = y; yc <= y + height - 1; yc++)
226 if (point[wid * yc + xc] != 0)
227 sp (xc, yc, point[wid * yc + xc]);
231 palupdate (Bool forceUpdate)
233 if (forceUpdate || *instring == 0)
235 #ifdef VERMICULATE_STANDALONE
237 for (colnum = 0; colnum < tailmax; colnum++)
239 mycolors[colnum].red = rgb[colnum][0] << 10;
240 mycolors[colnum].green = rgb[colnum][1] << 10;
241 mycolors[colnum].blue = rgb[colnum][2] << 10;
242 mycolors[colnum].flags = DoRed | DoBlue | DoGreen;
243 XAllocColor (mydpy, mycmap, &mycolors[colnum]);
245 #endif /* VERMICULATE_STANDALONE */
246 redraw (xmin, ymin, wid, hei);
253 #ifdef VERMICULATE_STANDALONE
255 for (co = 1; co <= 255; co++)
256 for (ro = 0; ro <= 2; ro++)
258 rgb[co][ro] = random1 (20);
260 rgb[co][ro] = random1 (64);
261 for (ro = 0; ro <= 2; ro++)
264 int ncolors = tailmax - 1;
265 make_random_colormap (mydpy,
267 mycmap, &mycolors[1], &ncolors, True, True, 0, True);
268 if (ncolors < tailmax - 1)
271 for (c = 1; c < tailmax; c++)
272 mycolors[c].pixel = WhitePixel (mydpy, DefaultScreen (mydpy));
274 #endif /* VERMICULATE_STANDALONE */
278 gridupdate (Bool interruptible)
282 for (x = 0; x <= xmax && !(wasakeypressed () && interruptible); x += boxw)
283 for (y = 0; y <= ymax; y += boxh)
285 if (random1 (15) < gridden)
287 #define lesser(A,B) ( ((A)<(B)) ? (A) : (B) )
288 int max = lesser (x + boxw, xmax);
290 for (xc = x; xc <= max; xc++)
293 if (random1 (15) < gridden)
295 int max = lesser (y + boxh, ymax);
297 for (yc = y; yc <= max; yc++)
308 if (bordcorn == 0 || bordcorn == 1)
312 if (bordcorn == 0 || bordcorn == 3)
318 for (x = xmin; x <= xmax; x++)
319 sp (x, ybord, bordcol);
320 for (y = ymin; y <= ymax; y++)
321 sp (ybord, y, bordcol);
326 inbank (unsigned char thr)
330 for (c = 1; c <= bankt; c++)
331 if (bank[c - 1] == thr)
339 unsigned char thr = 1;
340 #ifdef VERMICULATE_STANDALONE
342 unsigned char orgb[256][3];
345 for (co = 2; co <= tailmax; co++)
346 for (ro = 0; ro <= 2; ro++)
348 #endif /* VERMICULATE_STANDALONE */
354 thr = thr % threads + 1;
355 #ifdef VERMICULATE_STANDALONE
356 for (co = 1; co <= threads; co++)
358 for (ro = 0; ro <= 2; ro++)
359 rgb[co + 1][ro] = 25;
361 for (ro = 0; ro <= 1; ro++)
362 rgb[co + 1][ro] = 60;
364 for (ro = 0; ro <= 2; ro++)
365 rgb[thr + 1][ro] = 60;
366 #endif /* VERMICULATE_STANDALONE */
380 wraparound (thr, 1, threads + 1);
382 while (inbank (thr));
385 bank[++bankt - 1] = thr;
388 bank[++bankt - 1] = ch - '0';
389 if (bank[bankt - 1] > threads)
397 for (c = 1; c <= threads; c++)
399 tbank[++tbankt - 1] = c;
401 arrcpy (bank, tbank);
411 for (c = 1; c <= threads; c++)
412 if (thread[c - 1].tmode == ch - '0')
413 bank[++bankt - 1] = c;
419 for (bankt = 1; bankt <= threads; bankt++)
420 bank[bankt - 1] = bankt;
424 for (bankt = 1; bankt <= thrmax; bankt++)
425 bank[bankt - 1] = bankt;
430 while (!(bankt >= threads || ch == 'N' || ch == '\15' || ch == '#'));
431 if (bankt == 0 && ch != 'N')
436 #ifdef VERMICULATE_STANDALONE
438 #endif /* VERMICULATE_STANDALONE */
443 bankmod (Bool * Bool_)
460 newonscreen (unsigned char thr)
462 linedata *LP = &thread[thr - 1];
465 LP->reclen = (LP->little) ?
466 random1 (10) + 5 : random1 (rlmax - 30) + 30;
467 LP->deg = random1 (degs);
468 LP->y = random1 (hei);
469 LP->x = random1 (wid);
472 LP->turnsize = random1 (4) + 2;
476 firstinit (unsigned char thr)
478 linedata *LP = &thread[thr - 1];
482 LP->slice = degs / 3;
485 LP->selfbounce = False;
486 LP->realbounce = False;
487 LP->vhfollow = False;
488 LP->tailfollow = False;
489 LP->killwalls = False;
491 LP->ctinc = random1 (2) * 2 - 1;
492 LP->circturn = ((thr % 2) * 2 - 1) * ((thr - 1) % 7 + 1);
501 LP->tclim = (unsigned char) (((real) degs) / 2 / 12);
509 int n = random1 (sizeof (sampleStrings) / sizeof (sampleStrings[0]));
510 instring = sampleStrings[n].str;
511 speed = sampleStrings[n].speed;
525 for (thr = 1; thr <= thrmax; thr++)
533 for (d = degs - 1; d >= 0; d--)
535 sinof[d] = sin (d * dtor);
536 cosof[d] = cos (d * dtor);
538 tanof[d] = tanof[d + 1];
540 tanof[d] = tan (d * dtor);
547 move (unsigned char thr)
549 linedata *LP = &thread[thr - 1];
556 LP->deg += random1 (2 * LP->turnsize + 1) - LP->turnsize;
559 if (LP->slice == degs || LP->slice == degs2 || LP->slice == degs4)
561 if (LP->orichar == 'D')
563 if (LP->deg % degs4 != degs8)
564 LP->deg = degs4 * random1 (4) + degs8;
566 else if (LP->orichar == 'V')
567 if (LP->deg % degs4 != 0)
568 LP->deg = degs4 * random1 (4);
570 if (random1 (100) == 0)
573 LP->deg = LP->deg - degs4 + random1 (degs2);
575 LP->deg += (random1 (2) * 2 - 1) * LP->slice;
579 LP->deg += LP->circturn;
582 if (abs (LP->spiturn) > 11)
585 LP->deg += LP->spiturn;
586 if (random1 (15 - abs (LP->spiturn)) == 0)
588 LP->spiturn += LP->ctinc;
589 if (abs (LP->spiturn) > 10)
594 LP->turnco = abs (LP->turnco) - 1;
597 LP->turnco = curviness + random1 (10);
600 LP->deg += LP->circturn;
603 if (abs (LP->turnco) == 1)
604 LP->turnco *= -1 * (random1 (degs2 / abs (LP->circturn)) + 5);
605 else if (LP->turnco == 0)
607 else if (LP->turnco > 0)
610 LP->deg += LP->circturn;
617 if (LP->turnco > LP->tclim)
620 LP->tsc = (LP->tsc % LP->tslen) + 1;
622 LP->deg += LP->turnseq[LP->tsc - 1];
629 if (LP->tailfollow || LP->prey == thr)
631 dx = thread[LP->prey - 1].xrec[thread[LP->prey - 1].recpos] - LP->x;
632 dy = thread[LP->prey - 1].yrec[thread[LP->prey - 1].recpos] - LP->y;
636 dx = thread[LP->prey - 1].x - LP->x;
637 dy = thread[LP->prey - 1].y - LP->y;
641 ((fabs (dx) > fabs (dy)) ?
654 1 * degs8 : 7 * degs8) : ((dy > 0) ? 3 * degs8 : 5 * degs8));
655 if (desdeg - desdeg % degs4 != LP->deg - LP->deg % degs4
660 /* Using atan2 here doesn't seem to slow things down: */
661 desdeg = atan2 (dy, dx) / dtor;
662 wraparound (desdeg, 0, degs);
664 if (abs (desdeg - LP->deg) <= abs (LP->circturn))
669 ((desdeg - LP->deg > degs2) ?
670 -abs (LP->circturn) : abs (LP->circturn))
671 : ((LP->deg - desdeg > degs2) ?
672 abs (LP->circturn) : -abs (LP->circturn));
677 dy / dx) ? -abs (LP->circturn) : abs (LP->circturn);
680 wraparound (LP->deg, 0, degs);
682 unsigned char oldcol;
683 real oldy = LP->y, oldx = LP->x;
684 LP->x += cosof[LP->deg];
685 wraparound (LP->x, xmin, xmax + 1);
686 LP->y += sinof[LP->deg];
687 wraparound (LP->y, ymin, ymax + 1);
688 #define xi ((int) LP->x)
689 #define yi ((int) LP->y)
691 oldcol = gp (xi, yi);
694 Bool vertwall = False, horiwall = False;
695 if (oldcol == 1 && ((LP->killwalls && gridden > 0) || LP->realbounce))
697 vertwall = (gp (xi, (int) oldy) == 1);
698 horiwall = (gp ((int) oldx, yi) == 1);
700 if (oldcol == 1 && LP->realbounce && (vertwall || horiwall))
703 LP->deg = -LP->deg + degs2;
709 if ((oldcol != LP->col && LP->realbounce)
710 || (oldcol == LP->col && LP->selfbounce))
711 LP->deg += degs4 * (random1 (2) * 2 - 1);
712 else if (oldcol != LP->col)
715 if (LP->killwalls && gridden > 0 && oldcol == 1)
717 if (vertwall && xi + 1 <= xmax)
720 for (yy = yi - yi % boxh;
721 yy <= yi - yi % boxh + boxh && yy <= ymax; yy++)
722 if (gp (xi + 1, yy) != 1 || yy == ymax)
725 if (horiwall && yi + 1 <= ymax)
728 for (xx = xi - xi % boxw;
729 xx <= xi - xi % boxw + boxw && xx <= xmax; xx++)
730 if (gp (xx, yi + 1) != 1 || xx == xmax)
734 if (oldcol != LP->col || LP->selfbounce)
739 wraparound (LP->deg, 0, degs);
743 sp (xi, yi, LP->col);
747 sp (LP->xrec[LP->recpos], LP->yrec[LP->recpos], 0);
749 sp (LP->xrec[LP->recpos], LP->yrec[LP->recpos], LP->col + thrmax);
751 LP->yrec[LP->recpos] = yi;
752 LP->xrec[LP->recpos] = xi;
753 if (LP->recpos == LP->reclen - 1)
755 if (LP->filled && !erasing)
762 wraparound (nextco, 0, LP->reclen);
763 if (LP->yrec[co] != LP->yrec[nextco]
764 || LP->xrec[co] != LP->xrec[nextco])
768 while (!(!LP->dead || co == LP->recpos));
771 wraparound (LP->recpos, 0, LP->reclen);
776 vermiculate_main (void)
778 int had_instring = (instring != 0);
780 Bool halted = False, autopal = False, cleared;
781 point = (unsigned char *) malloc (wid * hei);
790 for (thr = 1; thr <= threads; thr++)
803 while (wasakeypressed ())
815 unsigned char othreads = threads;
824 thread[++threads - 1].tmode = ch - '0';
827 thread[++threads - 1].tmode =
828 random1 (tmodes - '0') + 1;
832 while (!(ch == '\15' || ch == '#'
833 || threads == thrmax));
853 /* Careful! The following macro needs to be at the beginning of any
854 block in which it's invoked, since it declares variables: */
855 #define forallinbank(LDP) linedata *LDP; int bankc; \
857 (LDP = &thread[bank[bankc - 1] - 1], \
858 bankc <= bankt); bankc++)
860 forallinbank (L) L->slice = degs / (ch - '0');
865 forallinbank (L) L->slice = 0;
874 L->otslen = L->tslen;
889 L->turnseq[L->tslen - 1] = ch - '0';
891 L->turnseq[L->tslen - 1] *= -1;
893 L->turnseq[L->tslen - 1] *= -1;
899 while (!(ch == '\15' || ch == '#'
900 || thread[bank[0] - 1].tslen == 50));
907 L->tslen = L->otslen;
908 for (c = 1; c <= L->tslen; c++)
909 seqSum += L->turnseq[c - 1];
914 (int) (((real) degs2) / abs (seqSum));
915 L->tsc = random1 (L->tslen) + 1;
931 L->tmode = random1 (tmodes - '0') + 1;
941 forallinbank (L) L->orichar = ch;
947 arrcpy (fbank, bank);
952 for (bankc = 1; bankc <= fbankt; bankc++)
954 linedata *L = &thread[fbank[bankc - 1] - 1];
958 L->prey = bank[0 + (bankc - 1) % bankt];
965 forallinbank (L) L->prey = bank[bankc % bankt];
971 forallinbank (L) switch (ch)
974 L->circturn = 10 - (ch - '0');
977 L->circturn = random1 (7) + 1;
999 bankmod (&L->selfbounce);
1002 bankmod (&L->vhfollow);
1005 bankmod (&L->realbounce);
1008 bankmod (&L->little);
1012 bankmod (&L->tailfollow);
1015 bankmod (&L->killwalls);
1027 bordcorn = (bordcorn + 1) % 4;
1038 for (c = 1; c <= thrmax; c++)
1039 thread[c - 1].tmode = ch - '0';
1055 Bool gridchanged = True;
1096 gridchanged = False;
1098 if (dimch == 'W' || dimch == 'B')
1100 if (dimch == 'H' || dimch == 'B')
1107 while (!(ch == '\15' || ch == '#' || ch == 'O'));
1115 bordcol = 1 - bordcol;
1125 if (speed > SPEEDMAX)
1137 if (threads < thrmax)
1138 newonscreen (++threads);
1143 linedata *L = &thread[threads - 1];
1144 int lastpos = (L->filled) ? L->reclen - 1 : L->recpos;
1146 for (c = 0; c <= lastpos; c++)
1147 sp (L->xrec[c], L->yrec[c], 0);
1154 #ifdef VERMICULATE_STANDALONE
1157 while (XCheckWindowEvent
1158 (mydpy, mywindow, StructureNotifyMask | ExposureMask, &xe))
1161 case ConfigureNotify:
1162 wid = xe.xconfigure.width;
1163 hei = xe.xconfigure.height;
1165 point = (unsigned char *) malloc (wid * hei);
1170 redraw (xe.xexpose.x,
1171 xe.xexpose.y, xe.xexpose.width,
1177 screenhack_handle_events (mydpy);
1178 #endif /* VERMICULATE_STANDALONE */
1182 Bool alltrap = True;
1184 for (thr = 1; thr <= threads; thr++)
1187 if (alltrap) /* all threads are trapped */
1189 if (speed != SPEEDMAX)
1193 if (tick++ > max_ticks && !had_instring)
1202 while (!(halted || cleared));
1210 XSetWindowBackground (mydpy, mywindow,
1211 BlackPixel (mydpy, DefaultScreen (mydpy)));
1213 XGetWindowAttributes (mydpy, mywindow, &xgwa);
1216 mycmap = xgwa.colormap;
1220 XGetGCValues (mydpy, XDefaultGC (mydpy, XDefaultScreen (mydpy)),
1221 GCForeground, &mygcv);
1222 mygc = XCreateGC (mydpy, mywindow, GCForeground, &mygcv);
1226 #ifdef VERMICULATE_STANDALONE
1227 /* Function Name: GetVRoot (slightly changed from the X Windows FAQ)
1228 * Description: Gets the root window, even if it's a virtual root
1229 * Arguments: the display and the screen
1230 * Returns: the root window for the client
1233 GetVRoot (Display * dpy, int scr)
1235 Window rootReturn, parentReturn, *children;
1236 unsigned int numChildren;
1237 Window root = RootWindow (dpy, scr);
1238 Atom __SWM_VROOT = None;
1241 __SWM_VROOT = XInternAtom (dpy, "__SWM_VROOT", False);
1242 XQueryTree (dpy, root, &rootReturn, &parentReturn, &children, &numChildren);
1243 for (i = 0; i < numChildren; i++)
1247 unsigned long int nitems, bytesafter;
1248 Window *newRoot = NULL;
1250 if (XGetWindowProperty (dpy, children[i], __SWM_VROOT, 0, 1,
1251 False, XA_WINDOW, &actual_type, &actual_format,
1252 &nitems, &bytesafter,
1253 (unsigned char **) &newRoot) == Success
1261 XFree ((char *) children);
1266 main (int argc, char **argv)
1269 if ((mydpy = XOpenDisplay (NULL)) == NULL)
1271 fprintf (stderr, "%s: cannot connect to X server %s\n", argv[0],
1272 XDisplayName (NULL));
1276 for (argnum = 1; argnum < argc; argnum++)
1278 if (!strcmp (argv[argnum], "-geometry"))
1281 unsigned int uh, uw;
1282 XParseGeometry (argv[++argnum], &x, &y, &uw, &uh);
1286 else if (!strcmp (argv[argnum], "-instring"))
1287 instring = argv[++argnum];
1288 else if (!strcmp (argv[argnum], "-root"))
1290 else if (!strcmp (argv[argnum], "-speed"))
1291 speed = atoi (argv[++argnum]);
1295 "\nvermiculate options are:"
1296 "\n -speed NUMBER: set speed, can be from 1 to %d."
1297 "\n -root: use root window."
1298 "\n -instring STRING: put STRING in kbd buffer."
1299 "\n -geometry WIDTHxHEIGHT \n", SPEEDMAX);
1305 mywindow = GetVRoot (mydpy, DefaultScreen (mydpy));
1307 mywindow = XCreateSimpleWindow (mydpy, DefaultRootWindow (mydpy), 0, 0,
1308 wid, hei, 0, 0, BlackPixel (mydpy,
1311 XStoreName (mydpy, mywindow, "vermiculate");
1312 XMapWindow (mydpy, mywindow);
1314 XSelectInput (mydpy, mywindow,
1315 KeyPressMask | ExposureMask | StructureNotifyMask);
1320 vermiculate_main ();
1327 screenhack (Display * d, Window w)
1331 instring = get_string_resource ("instring", "Instring");
1332 max_ticks = get_integer_resource ("ticks", "Integer");
1334 int temp = get_integer_resource ("speed", "Speed");
1339 mycolors[0].pixel = BlackPixel (mydpy, DefaultScreen (mydpy));
1340 vermiculate_main ();
1342 #endif /* VERMICULATE_STANDALONE */