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