1a6bebf158c8b857a436e5298e4e20abd6d1ad41
[xscreensaver] / hacks / xmatrix.c
1 /* xscreensaver, Copyright (c) 1999, 2001 Jamie Zawinski <jwz@jwz.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  *
11  * Matrix -- simulate the text scrolls from the movie "The Matrix".
12  *
13  * The movie people distribute their own Windows/Mac screensaver that does
14  * a similar thing, so I wrote one for Unix.  However, that version (the
15  * Windows/Mac version at http://www.whatisthematrix.com/) doesn't match my
16  * memory of what the screens in the movie looked like, so my `xmatrix'
17  * does things differently.
18  *
19  *
20  *     ==========================================================
21  *
22  *         NOTE:
23  *
24  *         People just love to hack on this one.  I get sent
25  *         patches to this all the time saying, ``here, I made
26  *         it better!''  Mostly this hasn't been true.
27  *
28  *         If you've made changes to xmatrix, when you send me
29  *         your patch, please explain, in English, both *what*
30  *         your changes are, and *why* you think those changes
31  *         make this screensaver behave more like the displays
32  *         in the movie did.  I'd rather not have to read your
33  *         diffs to try and figure that out for myself...
34  *
35  *         In particular, note that the characters in the movie
36  *         were, in fact, low resolution and somewhat blurry/
37  *         washed out.  They also definitely scrolled a
38  *         character at a time, not a pixel at a time.
39  *
40  *     ==========================================================
41  *
42  */
43
44 #include "screenhack.h"
45 #include <stdio.h>
46 #include <X11/Xutil.h>
47
48 #ifdef HAVE_XPM
49 # include <X11/xpm.h>
50 # include "images/matrix0.xpm"
51 # include "images/matrix1.xpm"
52 # include "images/matrix2.xpm"
53 # include "images/matrix0b.xpm"
54 # include "images/matrix1b.xpm"
55 # include "images/matrix2b.xpm"
56 #endif
57
58 #include "images/matrix0.xbm"
59 #include "images/matrix1.xbm"
60 #include "images/matrix2.xbm"
61 #include "images/matrix0b.xbm"
62 #include "images/matrix1b.xbm"
63 #include "images/matrix2b.xbm"
64
65 #define CHAR_COLS 16
66 #define CHAR_ROWS 13
67 #define CHAR_MAPS 3
68 #define FADE_MAP  0
69 #define PLAIN_MAP 1
70 #define GLOW_MAP  2
71
72 typedef struct {
73            int glow    : 8;
74   unsigned int glyph   : 9;  /* note: 9 bit characters! */
75   unsigned int changed : 1;
76   unsigned int spinner : 1;
77 } m_cell;
78
79 typedef struct {
80   int remaining;
81   int throttle;
82   int y;
83 } m_feeder;
84
85 #define countof(x) (sizeof(x)/sizeof(*(x)))
86
87 static int matrix_encoding[] = { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
88                                  192, 193, 194, 195, 196, 197, 198, 199,
89                                  200, 201, 202, 203, 204, 205, 206, 207 };
90 static int decimal_encoding[]  = { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
91 static int hex_encoding[]      = { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
92                                    33, 34, 35, 36, 37, 38 };
93 static int binary_encoding[] = { 16, 17 };
94 static int dna_encoding[]    = { 33, 35, 39, 52 };
95 static unsigned char char_map[256] = {
96     3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  /*   0 */
97     3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  /*  16 */
98     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,  /*  32 */
99    16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,  /*  48 */
100    32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,  /*  64 */
101    48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,  /*  80 */
102    64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,  /*  96 */
103    80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,  /* 112 */
104     3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  /* 128 */
105     3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  /* 144 */
106    96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,  /* 160 */
107   112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,  /* 176 */
108   128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,  /* 192 */
109   144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,  /* 208 */
110   160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,  /* 224 */
111   176,177,178,195,180,181,182,183,184,185,186,187,188,189,190,191   /* 240 */
112 };
113
114 typedef enum { TRACE0, TRACE1, TRACE2,
115                KNOCK0, KNOCK1, KNOCK2, KNOCK3,
116                KNOCK4, KNOCK5, KNOCK6, KNOCK7,
117                MATRIX, DNA, BINARY, HEX } m_mode;
118
119 typedef struct {
120   Display *dpy;
121   Window window;
122   XWindowAttributes xgwa;
123   GC draw_gc, erase_gc;
124   int grid_width, grid_height;
125   int char_width, char_height;
126   m_cell *cells;
127   m_feeder *feeders;
128   int nspinners;
129   Bool small_p;
130   Bool insert_top_p, insert_bottom_p;
131   m_mode mode;
132   signed char *tracing;
133   int density;
134
135   Pixmap images[CHAR_MAPS];
136   int image_width, image_height;
137
138   int nglyphs;
139   int *glyph_map;
140
141 } m_state;
142
143
144 static void
145 load_images_1 (m_state *state, int which)
146 {
147 #ifdef HAVE_XPM
148   if (!get_boolean_resource ("mono", "Boolean") &&
149       state->xgwa.depth > 1)
150     {
151       
152
153       XpmAttributes xpmattrs;
154       int result;
155       xpmattrs.valuemask = 0;
156
157 # ifdef XpmCloseness
158       xpmattrs.valuemask |= XpmCloseness;
159       xpmattrs.closeness = 40000;
160 # endif
161 # ifdef XpmVisual
162       xpmattrs.valuemask |= XpmVisual;
163       xpmattrs.visual = state->xgwa.visual;
164 # endif
165 # ifdef XpmDepth
166       xpmattrs.valuemask |= XpmDepth;
167       xpmattrs.depth = state->xgwa.depth;
168 # endif
169 # ifdef XpmColormap
170       xpmattrs.valuemask |= XpmColormap;
171       xpmattrs.colormap = state->xgwa.colormap;
172 # endif
173
174       result = XpmCreatePixmapFromData (state->dpy, state->window,
175                 (which == 0 ? (state->small_p ? matrix0b_xpm : matrix0_xpm) :
176                  which == 1 ? (state->small_p ? matrix1b_xpm : matrix1_xpm) :
177                               (state->small_p ? matrix2b_xpm : matrix2_xpm)),
178                                         &state->images[which], 0 /* mask */,
179                                         &xpmattrs);
180       if (!state->images || (result != XpmSuccess && result != XpmColorError))
181         state->images[which] = 0;
182
183       state->image_width = xpmattrs.width;
184       state->image_height = xpmattrs.height;
185     }
186   else
187 #endif /* !HAVE_XPM */
188     {
189       unsigned long fg, bg;
190       state->image_width  = (state->small_p ? matrix0b_width :matrix0_width);
191       state->image_height = (state->small_p ? matrix0b_height:matrix0_height);
192       fg = get_pixel_resource("foreground", "Foreground",
193                               state->dpy, state->xgwa.colormap);
194       bg = get_pixel_resource("background", "Background",
195                               state->dpy, state->xgwa.colormap);
196       state->images[which] =
197         XCreatePixmapFromBitmapData (state->dpy, state->window, (char *)
198                 (which == 0 ? (state->small_p ? matrix0b_bits :matrix0_bits) :
199                  which == 1 ? (state->small_p ? matrix1b_bits :matrix1_bits) :
200                               (state->small_p ? matrix2b_bits :matrix2_bits)),
201                                      state->image_width, state->image_height,
202                                      bg, fg, state->xgwa.depth);
203     }
204 }
205
206
207 static void
208 load_images (m_state *state)
209 {
210   load_images_1 (state, 0);
211   load_images_1 (state, 1);
212   load_images_1 (state, 2);
213 }
214
215
216 static void
217 flip_images_1 (m_state *state, int which)
218 {
219   XImage *im = XGetImage (state->dpy, state->images[which], 0, 0,
220                           state->image_width, state->image_height,
221                           ~0L, (state->xgwa.depth > 1 ? ZPixmap : XYPixmap));
222   int x, y, xx;
223   int ww = state->char_width;
224   unsigned long *row = (unsigned long *) malloc (sizeof(*row) * ww);
225
226   for (y = 0; y < state->image_height; y++)
227     {
228       for (x = 0; x < CHAR_COLS; x++)
229         {
230           for (xx = 0; xx < ww; xx++)
231             row[xx] = XGetPixel (im, (x * ww) + xx, y);
232           for (xx = 0; xx < ww; xx++)
233             XPutPixel (im, (x * ww) + xx, y, row[ww - xx - 1]);
234         }
235     }
236
237   XPutImage (state->dpy, state->images[which], state->draw_gc, im, 0, 0, 0, 0,
238              state->image_width, state->image_height);
239   XDestroyImage (im);
240   free (row);
241 }
242
243 static void
244 flip_images (m_state *state)
245 {
246   flip_images_1 (state, 0);
247   flip_images_1 (state, 1);
248   flip_images_1 (state, 2);
249 }
250
251
252 static void
253 init_spinners (m_state *state)
254 {
255   int i = state->nspinners;
256   int x, y;
257   m_cell *cell;
258
259   for (y = 0; y < state->grid_height; y++)
260     for (x = 0; x < state->grid_width; x++)
261       {
262         cell = &state->cells[state->grid_width * y + x];
263         cell->spinner = 0;
264       }
265
266   while (--i > 0)
267     {
268       x = random() % state->grid_width;
269       y = random() % state->grid_height;
270       cell = &state->cells[state->grid_width * y + x];
271       cell->spinner = 1;
272     }
273 }
274
275
276 static void
277 init_trace (m_state *state)
278 {
279   char *s = get_string_resource ("tracePhone", "TracePhone");
280   char *s2, *s3;
281   int i;
282   if (!s)
283     goto FAIL;
284
285   state->tracing = (char *) malloc (strlen (s) + 1);
286   s3 = state->tracing;
287
288   for (s2 = s; *s2; s2++)
289     if (*s2 >= '0' && *s2 <= '9')
290       *s3++ = *s2;
291   *s3 = 0;
292
293   if (s3 == (char *) state->tracing)
294     goto FAIL;
295
296   for (i = 0; i < strlen(state->tracing); i++)
297     state->tracing[i] = -state->tracing[i];
298
299   state->glyph_map = decimal_encoding;
300   state->nglyphs = countof(decimal_encoding);
301
302   return;
303
304  FAIL:
305   fprintf (stderr, "%s: bad phone number: \"%s\".\n",
306            progname, s ? s : "(null)");
307
308   if (s) free (s);
309   if (state->tracing) free (state->tracing);
310   state->tracing = 0;
311   state->mode = MATRIX;
312 }
313
314
315 static m_state *
316 init_matrix (Display *dpy, Window window)
317 {
318   XGCValues gcv;
319   char *insert, *mode;
320   m_state *state = (m_state *) calloc (sizeof(*state), 1);
321
322   state->dpy = dpy;
323   state->window = window;
324
325   XGetWindowAttributes (dpy, window, &state->xgwa);
326
327   {
328     const char *s = get_string_resource ("small", "Boolean");
329     if (s && *s)
330       state->small_p = get_boolean_resource ("small", "Boolean");
331     else
332       state->small_p = (state->xgwa.width < 300);
333   }
334
335   load_images (state);
336
337   gcv.foreground = get_pixel_resource("foreground", "Foreground",
338                                       state->dpy, state->xgwa.colormap);
339   gcv.background = get_pixel_resource("background", "Background",
340                                       state->dpy, state->xgwa.colormap);
341   state->draw_gc = XCreateGC (state->dpy, state->window,
342                               GCForeground|GCBackground, &gcv);
343   gcv.foreground = gcv.background;
344   state->erase_gc = XCreateGC (state->dpy, state->window,
345                                GCForeground|GCBackground, &gcv);
346
347   state->char_width =  state->image_width  / CHAR_COLS;
348   state->char_height = state->image_height / CHAR_ROWS;
349
350   state->grid_width  = state->xgwa.width  / state->char_width;
351   state->grid_height = state->xgwa.height / state->char_height;
352   state->grid_width++;
353   state->grid_height++;
354
355   state->glyph_map = matrix_encoding;
356   state->nglyphs = countof(matrix_encoding);
357
358   state->cells = (m_cell *)
359     calloc (sizeof(m_cell), state->grid_width * state->grid_height);
360   state->feeders = (m_feeder *) calloc (sizeof(m_feeder), state->grid_width);
361
362   state->density = get_integer_resource ("density", "Integer");
363
364   insert = get_string_resource("insert", "Insert");
365   if (insert && !strcmp(insert, "top"))
366     {
367       state->insert_top_p = True;
368       state->insert_bottom_p = False;
369     }
370   else if (insert && !strcmp(insert, "bottom"))
371     {
372       state->insert_top_p = False;
373       state->insert_bottom_p = True;
374     }
375   else if (insert && !strcmp(insert, "both"))
376     {
377       state->insert_top_p = True;
378       state->insert_bottom_p = True;
379     }
380   else
381     {
382       if (insert && *insert)
383         fprintf (stderr,
384                  "%s: `insert' must be `top', `bottom', or `both', not `%s'\n",
385                  progname, insert);
386       state->insert_top_p = False;
387       state->insert_bottom_p = True;
388     }
389
390   state->nspinners = get_integer_resource ("spinners", "Integer");
391
392   if (insert)
393     free (insert);
394
395   mode = get_string_resource ("mode", "Mode");
396   if (mode && !strcasecmp(mode, "trace"))
397     state->mode = TRACE0;
398   else if (mode && !strcasecmp(mode, "dna"))
399     state->mode = DNA;
400   else if (mode && !strcasecmp(mode, "binary"))
401     state->mode = BINARY;
402   else if (mode && (!strcasecmp(mode, "hex") ||
403                     !strcasecmp(mode, "hexadecimal")))
404     state->mode = HEX;
405   else if (!mode || !*mode || !strcasecmp(mode, "matrix"))
406     state->mode = MATRIX;
407   else
408     {
409       fprintf (stderr,
410            "%s: `mode' must be matrix, trace, dna, binary, or hex: not `%s'\n",
411                progname, mode);
412       state->mode = MATRIX;
413     }
414
415   if (state->mode == DNA)
416     {
417       state->glyph_map = dna_encoding;
418       state->nglyphs = countof(dna_encoding);
419     }
420   else if (state->mode == BINARY)
421     {
422       state->glyph_map = binary_encoding;
423       state->nglyphs = countof(binary_encoding);
424     }
425   else if (state->mode == HEX)
426     {
427       state->glyph_map = hex_encoding;
428       state->nglyphs = countof(hex_encoding);
429     }
430   else if (state->mode == HEX)
431     {
432       state->glyph_map = hex_encoding;
433       state->nglyphs = countof(hex_encoding);
434     }
435   else if (state->mode == TRACE0)
436     init_trace (state);
437   else
438     {
439       flip_images (state);
440       init_spinners (state);
441     }
442
443   return state;
444 }
445
446
447 static void
448 insert_glyph (m_state *state, int glyph, int x, int y)
449 {
450   Bool bottom_feeder_p = (y >= 0);
451   m_cell *from, *to;
452
453   if (y >= state->grid_height)
454     return;
455
456   if (bottom_feeder_p)
457     {
458       to = &state->cells[state->grid_width * y + x];
459     }
460   else
461     {
462       for (y = state->grid_height-1; y > 0; y--)
463         {
464           from = &state->cells[state->grid_width * (y-1) + x];
465           to   = &state->cells[state->grid_width * y     + x];
466           to->glyph   = from->glyph;
467           to->glow    = from->glow;
468           to->changed = 1;
469         }
470       to = &state->cells[x];
471     }
472
473   to->glyph = glyph;
474   to->changed = 1;
475
476   if (!to->glyph)
477     ;
478   else if (bottom_feeder_p)
479     to->glow = 1 + (random() % 2);
480   else
481     to->glow = 0;
482 }
483
484
485 static void
486 feed_matrix (m_state *state)
487 {
488   int x;
489
490   switch (state->mode)
491     {
492     case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
493       break;
494     default:
495       return;
496     }
497
498   /* Update according to current feeders. */
499   for (x = 0; x < state->grid_width; x++)
500     {
501       m_feeder *f = &state->feeders[x];
502
503       if (f->throttle)          /* this is a delay tick, synced to frame. */
504         {
505           f->throttle--;
506         }
507       else if (f->remaining > 0)        /* how many items are in the pipe */
508         {
509           int g = state->glyph_map[(random() % state->nglyphs)] + 1;
510           insert_glyph (state, g, x, f->y);
511           f->remaining--;
512           if (f->y >= 0)  /* bottom_feeder_p */
513             f->y++;
514         }
515       else                              /* if pipe is empty, insert spaces */
516         {
517           insert_glyph (state, 0, x, f->y);
518           if (f->y >= 0)  /* bottom_feeder_p */
519             f->y++;
520         }
521
522       if ((random() % 10) == 0)         /* randomly change throttle speed */
523         {
524           f->throttle = ((random() % 5) + (random() % 5));
525         }
526     }
527 }
528
529 static int
530 densitizer (m_state *state)
531 {
532   /* Horrid kludge that converts percentages (density of screen coverage)
533      to the parameter that actually controls this.  I got this mapping
534      empirically, on a 1024x768 screen.  Sue me. */
535   if      (state->density < 10) return 85;
536   else if (state->density < 15) return 60;
537   else if (state->density < 20) return 45;
538   else if (state->density < 25) return 25;
539   else if (state->density < 30) return 20;
540   else if (state->density < 35) return 15;
541   else if (state->density < 45) return 10;
542   else if (state->density < 50) return 8;
543   else if (state->density < 55) return 7;
544   else if (state->density < 65) return 5;
545   else if (state->density < 80) return 3;
546   else if (state->density < 90) return 2;
547   else return 1;
548 }
549
550
551 static void
552 hack_text (m_state *state)
553 {
554   int i;
555   int x = 0;
556   const char *s;
557   switch (state->mode)
558     {
559     case TRACE0: s = "Call trans opt: received.\n"
560                      "2-19-98 13:24:18 REC:Log>_"; break;
561     case TRACE1: s = "Trace program: running_"; break;
562
563     case KNOCK0: s = "Wake up, Neo..."; break;
564     case KNOCK1: s = ""; break;
565     case KNOCK2: s = "The Matrix has you..."; break;
566     case KNOCK3: s = ""; break;
567     case KNOCK4: s = "Follow the white rabbit..."; break;
568     case KNOCK5: s = ""; break;
569     case KNOCK6: s = "Knock knock, Neo."; break;
570     case KNOCK7: s = ""; break;
571
572     default: abort(); break;
573     }
574
575   for (i = 0; i < state->grid_height * state->grid_width; i++)
576     {
577       m_cell *cell = &state->cells[i];
578       cell->changed = (cell->glyph != 0);
579       cell->glyph = 0;
580     }
581
582   if (state->mode == TRACE0 || state->mode == TRACE1)
583     i = 0;
584   else
585     {
586       int y;
587       x = ((int)state->grid_width - (int)strlen(s)) / 2;
588       y = (state->grid_height / 2) - 1;
589       if (y < 0) y = 0;
590       if (x < 0) x = 0;
591       i = (y * state->grid_width) + x;
592     }
593
594   while (*s)
595     {
596       if (*s == '\n')
597         {
598           i = ((i / state->grid_width) + 1) * state->grid_width;
599           x = 0;
600         }
601       else
602         {
603           m_cell *cell = &state->cells[i];
604           if (x < state->grid_width-1)
605             {
606               cell->glyph = char_map[(unsigned char) *s] + 1;
607               if (*s == ' ' || *s == '\t') cell->glyph = 0;
608               cell->changed = 1;
609               i++;
610             }
611           x++;
612         }
613       s++;
614     }
615 }
616
617 static void
618 roll_state (m_state *state)
619 {
620   int delay = 0;
621   switch (state->mode)
622     {
623     case TRACE0:
624       delay = 3;
625       state->mode = TRACE1;
626       break;
627
628     case TRACE1:
629       delay = 2;
630       state->mode = TRACE2;
631       break;
632
633     case TRACE2:
634       {
635         Bool any = False;
636         int i;
637         for (i = 0; i < strlen(state->tracing); i++)
638           if (state->tracing[i] < 0) any = True;
639
640         if (!any)
641           {
642             XSync (state->dpy, False);
643             sleep (3);
644             state->mode = MATRIX;
645             state->glyph_map = matrix_encoding;
646             state->nglyphs = countof(matrix_encoding);
647             flip_images (state);
648             free (state->tracing);
649             state->tracing = 0;
650           }
651         else if ((random() % 10) == 0)
652           {
653             int x = random() % strlen(state->tracing);
654             if (state->tracing[x] < 0)
655               state->tracing[x] = -state->tracing[x];
656           }
657         break;
658       }
659
660     case KNOCK0: delay = 1; state->mode++; break; /* wake */
661     case KNOCK1: delay = 4; state->mode++; break;
662     case KNOCK2: delay = 2; state->mode++; break; /* has */
663     case KNOCK3: delay = 4; state->mode++; break;
664     case KNOCK4: delay = 2; state->mode++; break; /* rabbit */
665     case KNOCK5: delay = 4; state->mode++; break;
666     case KNOCK6: delay = 4; state->mode++; break; /* knock */
667     case KNOCK7:
668       state->mode = MATRIX;
669       state->glyph_map = matrix_encoding;
670       state->nglyphs = countof(matrix_encoding);
671       flip_images (state);
672       break;
673
674     case MATRIX:
675       if (! (random() % 5000))
676         {
677           state->mode = KNOCK0;
678           flip_images (state);
679         }
680       break;
681
682     case DNA: case BINARY: case HEX:
683       break;
684
685     default:
686       abort();
687       break;
688     }
689
690   if (delay)
691     {
692       XSync (state->dpy, False);
693       sleep (delay);
694     }
695 }
696
697
698 static void
699 hack_matrix (m_state *state)
700 {
701   int x;
702
703   switch (state->mode)
704     {
705     case TRACE0: case TRACE1:
706     case KNOCK0: case KNOCK1: case KNOCK2: case KNOCK3:
707     case KNOCK4: case KNOCK5: case KNOCK6: case KNOCK7:
708       hack_text (state);
709       return;
710     case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
711       break;
712     default:
713       abort(); break;
714     }
715
716   /* Glow some characters. */
717   if (!state->insert_bottom_p)
718     {
719       int i = random() % (state->grid_width / 2);
720       while (--i > 0)
721         {
722           int x = random() % state->grid_width;
723           int y = random() % state->grid_height;
724           m_cell *cell = &state->cells[state->grid_width * y + x];
725           if (cell->glyph && cell->glow == 0)
726             {
727               cell->glow = random() % 10;
728               cell->changed = 1;
729             }
730         }
731     }
732
733   /* Change some of the feeders. */
734   for (x = 0; x < state->grid_width; x++)
735     {
736       m_feeder *f = &state->feeders[x];
737       Bool bottom_feeder_p;
738
739       if (f->remaining > 0)     /* never change if pipe isn't empty */
740         continue;
741
742       if ((random() % densitizer(state)) != 0) /* then change N% of the time */
743         continue;
744
745       f->remaining = 3 + (random() % state->grid_height);
746       f->throttle = ((random() % 5) + (random() % 5));
747
748       if ((random() % 4) != 0)
749         f->remaining = 0;
750
751       if (state->mode == TRACE2)
752         bottom_feeder_p = True;
753       if (state->insert_top_p && state->insert_bottom_p)
754         bottom_feeder_p = (random() & 1);
755       else
756         bottom_feeder_p = state->insert_bottom_p;
757
758       if (bottom_feeder_p)
759         f->y = random() % (state->grid_height / 2);
760       else
761         f->y = -1;
762     }
763
764   if (!state->mode == TRACE2 &&
765       ! (random() % 500))
766     init_spinners (state);
767 }
768
769
770 static void
771 draw_matrix (m_state *state)
772 {
773   int x, y;
774   int count = 0;
775
776   feed_matrix (state);
777   hack_matrix (state);
778
779   for (y = 0; y < state->grid_height; y++)
780     for (x = 0; x < state->grid_width; x++)
781       {
782         m_cell *cell = &state->cells[state->grid_width * y + x];
783
784         if (cell->glyph)
785           count++;
786
787         if (state->mode == TRACE2)
788           {
789             int xx = x % strlen(state->tracing);
790             Bool dead_p = state->tracing[xx] > 0;
791
792             if (y == 0 && x == xx)
793               cell->glyph = (dead_p
794                              ? state->glyph_map[state->tracing[xx]-'0'] + 1
795                              : 0);
796             else if (y == 0)
797               cell->glyph = 0;
798             else
799               cell->glyph = (dead_p ? 0 :
800                              (state->glyph_map[(random()%state->nglyphs)]
801                               + 1));
802
803             cell->changed = 1;
804           }
805
806         if (!cell->changed)
807           continue;
808
809         if (cell->glyph == 0)
810           XFillRectangle (state->dpy, state->window, state->erase_gc,
811                           x * state->char_width,
812                           y * state->char_height,
813                           state->char_width,
814                           state->char_height);
815         else
816           {
817             int cx = (cell->glyph - 1) % CHAR_COLS;
818             int cy = (cell->glyph - 1) / CHAR_COLS;
819             int map = ((cell->glow > 0 || cell->spinner) ? GLOW_MAP :
820                        (cell->glow == 0) ? PLAIN_MAP :
821                        GLOW_MAP);
822
823             XCopyArea (state->dpy, state->images[map],
824                        state->window, state->draw_gc,
825                        cx * state->char_width,
826                        cy * state->char_height,
827                        state->char_width,
828                        state->char_height,
829                        x * state->char_width,
830                        y * state->char_height);
831           }
832
833         cell->changed = 0;
834
835         if (cell->glow > 0)
836           {
837             cell->glow--;
838             cell->changed = 1;
839           }
840         else if (cell->glow < 0)
841           {
842             cell->glow++;
843             if (cell->glow == 0)
844               cell->glyph = 0;
845             cell->changed = 1;
846           }
847
848         if (cell->spinner)
849           {
850             cell->glyph = (state->glyph_map[(random()%state->nglyphs)] + 1);
851             cell->changed = 1;
852           }
853       }
854
855   roll_state (state);
856
857 #if 0
858   {
859     static int i = 0;
860     static int ndens = 0;
861     static int tdens = 0;
862     i++;
863     if (i > 50)
864       {
865         int dens = (100.0 *
866                     (((double)count) /
867                      ((double) (state->grid_width * state->grid_height))));
868         tdens += dens;
869         ndens++;
870         printf ("density: %d%% (%d%%)\n", dens, (tdens / ndens));
871         i = 0;
872       }
873   }
874 #endif
875
876 }
877
878 \f
879 char *progclass = "XMatrix";
880
881 char *defaults [] = {
882   ".background:            black",
883   ".foreground:            green",
884   "*small:                 ",
885   "*delay:                 10000",
886   "*insert:                both",
887   "*mode:                  Matrix",
888   "*tracePhone:            (212) 555-0690",
889   "*spinners:              5",
890   "*density:               75",
891   0
892 };
893
894 XrmOptionDescRec options [] = {
895   { "-small",           ".small",               XrmoptionNoArg, "True" },
896   { "-large",           ".small",               XrmoptionNoArg, "False" },
897   { "-delay",           ".delay",               XrmoptionSepArg, 0 },
898   { "-top",             ".insert",              XrmoptionNoArg, "top" },
899   { "-bottom",          ".insert",              XrmoptionNoArg, "bottom" },
900   { "-both",            ".insert",              XrmoptionNoArg, "both" },
901   { "-density",         ".density",             XrmoptionSepArg, 0 },
902   { "-trace",           ".mode",                XrmoptionNoArg, "trace" },
903   { "-phone",           ".tracePhone",          XrmoptionSepArg, 0 },
904   { "-dna",             ".mode",                XrmoptionNoArg, "DNA" },
905   { "-binary",          ".mode",                XrmoptionNoArg, "binary" },
906   { "-hexadecimal",     ".mode",                XrmoptionNoArg, "hexadecimal"},
907   { 0, 0, 0, 0 }
908 };
909
910
911 void
912 screenhack (Display *dpy, Window window)
913 {
914   m_state *state = init_matrix (dpy, window);
915   int delay = get_integer_resource ("delay", "Integer");
916   while (1)
917     {
918       draw_matrix (state);
919       XSync (dpy, False);
920       screenhack_handle_events (dpy);
921       if (delay) usleep (delay);
922     }
923 }