http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.01.tar.gz
[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 "xpm-pixmap.h"
46 #include <stdio.h>
47 #include <X11/Xutil.h>
48
49 #if defined(HAVE_GDK_PIXBUF) || defined(HAVE_XPM)
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 #if defined(HAVE_GDK_PIXBUF) || defined(HAVE_XPM)
148   if (!get_boolean_resource ("mono", "Boolean") &&
149       state->xgwa.depth > 1)
150     {
151       char **bits =
152         (which == 0 ? (state->small_p ? matrix0b_xpm : matrix0_xpm) :
153          which == 1 ? (state->small_p ? matrix1b_xpm : matrix1_xpm) :
154          (state->small_p ? matrix2b_xpm : matrix2_xpm));
155
156       state->images[which] =
157         xpm_data_to_pixmap (state->dpy, state->window, bits,
158                             &state->image_width, &state->image_height, 0);
159     }
160   else
161 #endif /* !HAVE_XPM && !HAVE_GDK_PIXBUF */
162     {
163       unsigned long fg, bg;
164       state->image_width  = (state->small_p ? matrix0b_width :matrix0_width);
165       state->image_height = (state->small_p ? matrix0b_height:matrix0_height);
166       fg = get_pixel_resource("foreground", "Foreground",
167                               state->dpy, state->xgwa.colormap);
168       bg = get_pixel_resource("background", "Background",
169                               state->dpy, state->xgwa.colormap);
170       state->images[which] =
171         XCreatePixmapFromBitmapData (state->dpy, state->window, (char *)
172                 (which == 0 ? (state->small_p ? matrix0b_bits :matrix0_bits) :
173                  which == 1 ? (state->small_p ? matrix1b_bits :matrix1_bits) :
174                               (state->small_p ? matrix2b_bits :matrix2_bits)),
175                                      state->image_width, state->image_height,
176                                      bg, fg, state->xgwa.depth);
177     }
178 }
179
180
181 static void
182 load_images (m_state *state)
183 {
184   load_images_1 (state, 0);
185   load_images_1 (state, 1);
186   load_images_1 (state, 2);
187 }
188
189
190 static void
191 flip_images_1 (m_state *state, int which)
192 {
193   XImage *im = XGetImage (state->dpy, state->images[which], 0, 0,
194                           state->image_width, state->image_height,
195                           ~0L, (state->xgwa.depth > 1 ? ZPixmap : XYPixmap));
196   int x, y, xx;
197   int ww = state->char_width;
198   unsigned long *row = (unsigned long *) malloc (sizeof(*row) * ww);
199
200   for (y = 0; y < state->image_height; y++)
201     {
202       for (x = 0; x < CHAR_COLS; x++)
203         {
204           for (xx = 0; xx < ww; xx++)
205             row[xx] = XGetPixel (im, (x * ww) + xx, y);
206           for (xx = 0; xx < ww; xx++)
207             XPutPixel (im, (x * ww) + xx, y, row[ww - xx - 1]);
208         }
209     }
210
211   XPutImage (state->dpy, state->images[which], state->draw_gc, im, 0, 0, 0, 0,
212              state->image_width, state->image_height);
213   XDestroyImage (im);
214   free (row);
215 }
216
217 static void
218 flip_images (m_state *state)
219 {
220   flip_images_1 (state, 0);
221   flip_images_1 (state, 1);
222   flip_images_1 (state, 2);
223 }
224
225
226 static void
227 init_spinners (m_state *state)
228 {
229   int i = state->nspinners;
230   int x, y;
231   m_cell *cell;
232
233   for (y = 0; y < state->grid_height; y++)
234     for (x = 0; x < state->grid_width; x++)
235       {
236         cell = &state->cells[state->grid_width * y + x];
237         cell->spinner = 0;
238       }
239
240   while (--i > 0)
241     {
242       x = random() % state->grid_width;
243       y = random() % state->grid_height;
244       cell = &state->cells[state->grid_width * y + x];
245       cell->spinner = 1;
246     }
247 }
248
249
250 static void
251 init_trace (m_state *state)
252 {
253   char *s = get_string_resource ("tracePhone", "TracePhone");
254   char *s2, *s3;
255   int i;
256   if (!s)
257     goto FAIL;
258
259   state->tracing = (char *) malloc (strlen (s) + 1);
260   s3 = state->tracing;
261
262   for (s2 = s; *s2; s2++)
263     if (*s2 >= '0' && *s2 <= '9')
264       *s3++ = *s2;
265   *s3 = 0;
266
267   if (s3 == (char *) state->tracing)
268     goto FAIL;
269
270   for (i = 0; i < strlen(state->tracing); i++)
271     state->tracing[i] = -state->tracing[i];
272
273   state->glyph_map = decimal_encoding;
274   state->nglyphs = countof(decimal_encoding);
275
276   return;
277
278  FAIL:
279   fprintf (stderr, "%s: bad phone number: \"%s\".\n",
280            progname, s ? s : "(null)");
281
282   if (s) free (s);
283   if (state->tracing) free (state->tracing);
284   state->tracing = 0;
285   state->mode = MATRIX;
286 }
287
288
289 static m_state *
290 init_matrix (Display *dpy, Window window)
291 {
292   XGCValues gcv;
293   char *insert, *mode;
294   m_state *state = (m_state *) calloc (sizeof(*state), 1);
295
296   state->dpy = dpy;
297   state->window = window;
298
299   XGetWindowAttributes (dpy, window, &state->xgwa);
300
301   {
302     const char *s = get_string_resource ("small", "Boolean");
303     if (s && *s)
304       state->small_p = get_boolean_resource ("small", "Boolean");
305     else
306       state->small_p = (state->xgwa.width < 300);
307   }
308
309   load_images (state);
310
311   gcv.foreground = get_pixel_resource("foreground", "Foreground",
312                                       state->dpy, state->xgwa.colormap);
313   gcv.background = get_pixel_resource("background", "Background",
314                                       state->dpy, state->xgwa.colormap);
315   state->draw_gc = XCreateGC (state->dpy, state->window,
316                               GCForeground|GCBackground, &gcv);
317   gcv.foreground = gcv.background;
318   state->erase_gc = XCreateGC (state->dpy, state->window,
319                                GCForeground|GCBackground, &gcv);
320
321   state->char_width =  state->image_width  / CHAR_COLS;
322   state->char_height = state->image_height / CHAR_ROWS;
323
324   state->grid_width  = state->xgwa.width  / state->char_width;
325   state->grid_height = state->xgwa.height / state->char_height;
326   state->grid_width++;
327   state->grid_height++;
328
329   state->glyph_map = matrix_encoding;
330   state->nglyphs = countof(matrix_encoding);
331
332   state->cells = (m_cell *)
333     calloc (sizeof(m_cell), state->grid_width * state->grid_height);
334   state->feeders = (m_feeder *) calloc (sizeof(m_feeder), state->grid_width);
335
336   state->density = get_integer_resource ("density", "Integer");
337
338   insert = get_string_resource("insert", "Insert");
339   if (insert && !strcmp(insert, "top"))
340     {
341       state->insert_top_p = True;
342       state->insert_bottom_p = False;
343     }
344   else if (insert && !strcmp(insert, "bottom"))
345     {
346       state->insert_top_p = False;
347       state->insert_bottom_p = True;
348     }
349   else if (insert && !strcmp(insert, "both"))
350     {
351       state->insert_top_p = True;
352       state->insert_bottom_p = True;
353     }
354   else
355     {
356       if (insert && *insert)
357         fprintf (stderr,
358                  "%s: `insert' must be `top', `bottom', or `both', not `%s'\n",
359                  progname, insert);
360       state->insert_top_p = False;
361       state->insert_bottom_p = True;
362     }
363
364   state->nspinners = get_integer_resource ("spinners", "Integer");
365
366   if (insert)
367     free (insert);
368
369   mode = get_string_resource ("mode", "Mode");
370   if (mode && !strcasecmp(mode, "trace"))
371     state->mode = TRACE0;
372   else if (mode && !strcasecmp(mode, "dna"))
373     state->mode = DNA;
374   else if (mode && !strcasecmp(mode, "binary"))
375     state->mode = BINARY;
376   else if (mode && (!strcasecmp(mode, "hex") ||
377                     !strcasecmp(mode, "hexadecimal")))
378     state->mode = HEX;
379   else if (!mode || !*mode || !strcasecmp(mode, "matrix"))
380     state->mode = MATRIX;
381   else
382     {
383       fprintf (stderr,
384            "%s: `mode' must be matrix, trace, dna, binary, or hex: not `%s'\n",
385                progname, mode);
386       state->mode = MATRIX;
387     }
388
389   if (state->mode == DNA)
390     {
391       state->glyph_map = dna_encoding;
392       state->nglyphs = countof(dna_encoding);
393     }
394   else if (state->mode == BINARY)
395     {
396       state->glyph_map = binary_encoding;
397       state->nglyphs = countof(binary_encoding);
398     }
399   else if (state->mode == HEX)
400     {
401       state->glyph_map = hex_encoding;
402       state->nglyphs = countof(hex_encoding);
403     }
404   else if (state->mode == HEX)
405     {
406       state->glyph_map = hex_encoding;
407       state->nglyphs = countof(hex_encoding);
408     }
409   else if (state->mode == TRACE0)
410     init_trace (state);
411   else
412     {
413       flip_images (state);
414       init_spinners (state);
415     }
416
417   return state;
418 }
419
420
421 static void
422 insert_glyph (m_state *state, int glyph, int x, int y)
423 {
424   Bool bottom_feeder_p = (y >= 0);
425   m_cell *from, *to;
426
427   if (y >= state->grid_height)
428     return;
429
430   if (bottom_feeder_p)
431     {
432       to = &state->cells[state->grid_width * y + x];
433     }
434   else
435     {
436       for (y = state->grid_height-1; y > 0; y--)
437         {
438           from = &state->cells[state->grid_width * (y-1) + x];
439           to   = &state->cells[state->grid_width * y     + x];
440           to->glyph   = from->glyph;
441           to->glow    = from->glow;
442           to->changed = 1;
443         }
444       to = &state->cells[x];
445     }
446
447   to->glyph = glyph;
448   to->changed = 1;
449
450   if (!to->glyph)
451     ;
452   else if (bottom_feeder_p)
453     to->glow = 1 + (random() % 2);
454   else
455     to->glow = 0;
456 }
457
458
459 static void
460 feed_matrix (m_state *state)
461 {
462   int x;
463
464   switch (state->mode)
465     {
466     case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
467       break;
468     default:
469       return;
470     }
471
472   /* Update according to current feeders. */
473   for (x = 0; x < state->grid_width; x++)
474     {
475       m_feeder *f = &state->feeders[x];
476
477       if (f->throttle)          /* this is a delay tick, synced to frame. */
478         {
479           f->throttle--;
480         }
481       else if (f->remaining > 0)        /* how many items are in the pipe */
482         {
483           int g = state->glyph_map[(random() % state->nglyphs)] + 1;
484           insert_glyph (state, g, x, f->y);
485           f->remaining--;
486           if (f->y >= 0)  /* bottom_feeder_p */
487             f->y++;
488         }
489       else                              /* if pipe is empty, insert spaces */
490         {
491           insert_glyph (state, 0, x, f->y);
492           if (f->y >= 0)  /* bottom_feeder_p */
493             f->y++;
494         }
495
496       if ((random() % 10) == 0)         /* randomly change throttle speed */
497         {
498           f->throttle = ((random() % 5) + (random() % 5));
499         }
500     }
501 }
502
503 static int
504 densitizer (m_state *state)
505 {
506   /* Horrid kludge that converts percentages (density of screen coverage)
507      to the parameter that actually controls this.  I got this mapping
508      empirically, on a 1024x768 screen.  Sue me. */
509   if      (state->density < 10) return 85;
510   else if (state->density < 15) return 60;
511   else if (state->density < 20) return 45;
512   else if (state->density < 25) return 25;
513   else if (state->density < 30) return 20;
514   else if (state->density < 35) return 15;
515   else if (state->density < 45) return 10;
516   else if (state->density < 50) return 8;
517   else if (state->density < 55) return 7;
518   else if (state->density < 65) return 5;
519   else if (state->density < 80) return 3;
520   else if (state->density < 90) return 2;
521   else return 1;
522 }
523
524
525 static void
526 hack_text (m_state *state)
527 {
528   int i;
529   int x = 0;
530   const char *s;
531   switch (state->mode)
532     {
533     case TRACE0: s = "Call trans opt: received.\n"
534                      "2-19-98 13:24:18 REC:Log>_"; break;
535     case TRACE1: s = "Trace program: running_"; break;
536
537     case KNOCK0: s = "Wake up, Neo..."; break;
538     case KNOCK1: s = ""; break;
539     case KNOCK2: s = "The Matrix has you..."; break;
540     case KNOCK3: s = ""; break;
541     case KNOCK4: s = "Follow the white rabbit..."; break;
542     case KNOCK5: s = ""; break;
543     case KNOCK6: s = "Knock knock, Neo."; break;
544     case KNOCK7: s = ""; break;
545
546     default: abort(); break;
547     }
548
549   for (i = 0; i < state->grid_height * state->grid_width; i++)
550     {
551       m_cell *cell = &state->cells[i];
552       cell->changed = (cell->glyph != 0);
553       cell->glyph = 0;
554     }
555
556   if (state->mode == TRACE0 || state->mode == TRACE1)
557     i = 0;
558   else
559     {
560       int y;
561       x = ((int)state->grid_width - (int)strlen(s)) / 2;
562       y = (state->grid_height / 2) - 1;
563       if (y < 0) y = 0;
564       if (x < 0) x = 0;
565       i = (y * state->grid_width) + x;
566     }
567
568   while (*s)
569     {
570       if (*s == '\n')
571         {
572           i = ((i / state->grid_width) + 1) * state->grid_width;
573           x = 0;
574         }
575       else
576         {
577           m_cell *cell = &state->cells[i];
578           if (x < state->grid_width-1)
579             {
580               cell->glyph = char_map[(unsigned char) *s] + 1;
581               if (*s == ' ' || *s == '\t') cell->glyph = 0;
582               cell->changed = 1;
583               i++;
584             }
585           x++;
586         }
587       s++;
588     }
589 }
590
591 static void
592 roll_state (m_state *state)
593 {
594   int delay = 0;
595   switch (state->mode)
596     {
597     case TRACE0:
598       delay = 3;
599       state->mode = TRACE1;
600       break;
601
602     case TRACE1:
603       delay = 2;
604       state->mode = TRACE2;
605       break;
606
607     case TRACE2:
608       {
609         Bool any = False;
610         int i;
611         for (i = 0; i < strlen(state->tracing); i++)
612           if (state->tracing[i] < 0) any = True;
613
614         if (!any)
615           {
616             XSync (state->dpy, False);
617             sleep (3);
618             state->mode = MATRIX;
619             state->glyph_map = matrix_encoding;
620             state->nglyphs = countof(matrix_encoding);
621             flip_images (state);
622             free (state->tracing);
623             state->tracing = 0;
624           }
625         else if ((random() % 10) == 0)
626           {
627             int x = random() % strlen(state->tracing);
628             if (state->tracing[x] < 0)
629               state->tracing[x] = -state->tracing[x];
630           }
631         break;
632       }
633
634     case KNOCK0: delay = 1; state->mode++; break; /* wake */
635     case KNOCK1: delay = 4; state->mode++; break;
636     case KNOCK2: delay = 2; state->mode++; break; /* has */
637     case KNOCK3: delay = 4; state->mode++; break;
638     case KNOCK4: delay = 2; state->mode++; break; /* rabbit */
639     case KNOCK5: delay = 4; state->mode++; break;
640     case KNOCK6: delay = 4; state->mode++; break; /* knock */
641     case KNOCK7:
642       state->mode = MATRIX;
643       state->glyph_map = matrix_encoding;
644       state->nglyphs = countof(matrix_encoding);
645       flip_images (state);
646       break;
647
648     case MATRIX:
649       if (! (random() % 5000))
650         {
651           state->mode = KNOCK0;
652           flip_images (state);
653         }
654       break;
655
656     case DNA: case BINARY: case HEX:
657       break;
658
659     default:
660       abort();
661       break;
662     }
663
664   if (delay)
665     {
666       XSync (state->dpy, False);
667       sleep (delay);
668     }
669 }
670
671
672 static void
673 hack_matrix (m_state *state)
674 {
675   int x;
676
677   switch (state->mode)
678     {
679     case TRACE0: case TRACE1:
680     case KNOCK0: case KNOCK1: case KNOCK2: case KNOCK3:
681     case KNOCK4: case KNOCK5: case KNOCK6: case KNOCK7:
682       hack_text (state);
683       return;
684     case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
685       break;
686     default:
687       abort(); break;
688     }
689
690   /* Glow some characters. */
691   if (!state->insert_bottom_p)
692     {
693       int i = random() % (state->grid_width / 2);
694       while (--i > 0)
695         {
696           int x = random() % state->grid_width;
697           int y = random() % state->grid_height;
698           m_cell *cell = &state->cells[state->grid_width * y + x];
699           if (cell->glyph && cell->glow == 0)
700             {
701               cell->glow = random() % 10;
702               cell->changed = 1;
703             }
704         }
705     }
706
707   /* Change some of the feeders. */
708   for (x = 0; x < state->grid_width; x++)
709     {
710       m_feeder *f = &state->feeders[x];
711       Bool bottom_feeder_p;
712
713       if (f->remaining > 0)     /* never change if pipe isn't empty */
714         continue;
715
716       if ((random() % densitizer(state)) != 0) /* then change N% of the time */
717         continue;
718
719       f->remaining = 3 + (random() % state->grid_height);
720       f->throttle = ((random() % 5) + (random() % 5));
721
722       if ((random() % 4) != 0)
723         f->remaining = 0;
724
725       if (state->mode == TRACE2)
726         bottom_feeder_p = True;
727       if (state->insert_top_p && state->insert_bottom_p)
728         bottom_feeder_p = (random() & 1);
729       else
730         bottom_feeder_p = state->insert_bottom_p;
731
732       if (bottom_feeder_p)
733         f->y = random() % (state->grid_height / 2);
734       else
735         f->y = -1;
736     }
737
738   if (!state->mode == TRACE2 &&
739       ! (random() % 500))
740     init_spinners (state);
741 }
742
743
744 static void
745 draw_matrix (m_state *state)
746 {
747   int x, y;
748   int count = 0;
749
750   feed_matrix (state);
751   hack_matrix (state);
752
753   for (y = 0; y < state->grid_height; y++)
754     for (x = 0; x < state->grid_width; x++)
755       {
756         m_cell *cell = &state->cells[state->grid_width * y + x];
757
758         if (cell->glyph)
759           count++;
760
761         if (state->mode == TRACE2)
762           {
763             int xx = x % strlen(state->tracing);
764             Bool dead_p = state->tracing[xx] > 0;
765
766             if (y == 0 && x == xx)
767               cell->glyph = (dead_p
768                              ? state->glyph_map[state->tracing[xx]-'0'] + 1
769                              : 0);
770             else if (y == 0)
771               cell->glyph = 0;
772             else
773               cell->glyph = (dead_p ? 0 :
774                              (state->glyph_map[(random()%state->nglyphs)]
775                               + 1));
776
777             cell->changed = 1;
778           }
779
780         if (!cell->changed)
781           continue;
782
783         if (cell->glyph == 0)
784           XFillRectangle (state->dpy, state->window, state->erase_gc,
785                           x * state->char_width,
786                           y * state->char_height,
787                           state->char_width,
788                           state->char_height);
789         else
790           {
791             int cx = (cell->glyph - 1) % CHAR_COLS;
792             int cy = (cell->glyph - 1) / CHAR_COLS;
793             int map = ((cell->glow > 0 || cell->spinner) ? GLOW_MAP :
794                        (cell->glow == 0) ? PLAIN_MAP :
795                        GLOW_MAP);
796
797             XCopyArea (state->dpy, state->images[map],
798                        state->window, state->draw_gc,
799                        cx * state->char_width,
800                        cy * state->char_height,
801                        state->char_width,
802                        state->char_height,
803                        x * state->char_width,
804                        y * state->char_height);
805           }
806
807         cell->changed = 0;
808
809         if (cell->glow > 0)
810           {
811             cell->glow--;
812             cell->changed = 1;
813           }
814         else if (cell->glow < 0)
815           {
816             cell->glow++;
817             if (cell->glow == 0)
818               cell->glyph = 0;
819             cell->changed = 1;
820           }
821
822         if (cell->spinner)
823           {
824             cell->glyph = (state->glyph_map[(random()%state->nglyphs)] + 1);
825             cell->changed = 1;
826           }
827       }
828
829   roll_state (state);
830
831 #if 0
832   {
833     static int i = 0;
834     static int ndens = 0;
835     static int tdens = 0;
836     i++;
837     if (i > 50)
838       {
839         int dens = (100.0 *
840                     (((double)count) /
841                      ((double) (state->grid_width * state->grid_height))));
842         tdens += dens;
843         ndens++;
844         printf ("density: %d%% (%d%%)\n", dens, (tdens / ndens));
845         i = 0;
846       }
847   }
848 #endif
849
850 }
851
852 \f
853 char *progclass = "XMatrix";
854
855 char *defaults [] = {
856   ".background:            black",
857   ".foreground:            green",
858   "*small:                 ",
859   "*delay:                 10000",
860   "*insert:                both",
861   "*mode:                  Matrix",
862   "*tracePhone:            (212) 555-0690",
863   "*spinners:              5",
864   "*density:               75",
865   0
866 };
867
868 XrmOptionDescRec options [] = {
869   { "-small",           ".small",               XrmoptionNoArg, "True" },
870   { "-large",           ".small",               XrmoptionNoArg, "False" },
871   { "-delay",           ".delay",               XrmoptionSepArg, 0 },
872   { "-top",             ".insert",              XrmoptionNoArg, "top" },
873   { "-bottom",          ".insert",              XrmoptionNoArg, "bottom" },
874   { "-both",            ".insert",              XrmoptionNoArg, "both" },
875   { "-density",         ".density",             XrmoptionSepArg, 0 },
876   { "-trace",           ".mode",                XrmoptionNoArg, "trace" },
877   { "-phone",           ".tracePhone",          XrmoptionSepArg, 0 },
878   { "-dna",             ".mode",                XrmoptionNoArg, "DNA" },
879   { "-binary",          ".mode",                XrmoptionNoArg, "binary" },
880   { "-hexadecimal",     ".mode",                XrmoptionNoArg, "hexadecimal"},
881   { 0, 0, 0, 0 }
882 };
883
884
885 void
886 screenhack (Display *dpy, Window window)
887 {
888   m_state *state = init_matrix (dpy, window);
889   int delay = get_integer_resource ("delay", "Integer");
890   while (1)
891     {
892       draw_matrix (state);
893       XSync (dpy, False);
894       screenhack_handle_events (dpy);
895       if (delay) usleep (delay);
896     }
897 }