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