1 /* xscreensaver, Copyright (c) 1999, 2001 Jamie Zawinski <jwz@jwz.org>
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
11 * Matrix -- simulate the text scrolls from the movie "The Matrix".
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.
20 * ==========================================================
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.
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...
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.
40 * ==========================================================
44 #include "screenhack.h"
45 #include "xpm-pixmap.h"
47 #include <X11/Xutil.h>
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"
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"
74 unsigned int glyph : 9; /* note: 9 bit characters! */
75 unsigned int changed : 1;
76 unsigned int spinner : 1;
85 #define countof(x) (sizeof(x)/sizeof(*(x)))
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 */
114 #define CURSOR_GLYPH 97
116 typedef enum { TRACE0, TRACE1, TRACE2,
117 KNOCK0, KNOCK1, KNOCK2, KNOCK3,
118 KNOCK4, KNOCK5, KNOCK6, KNOCK7,
120 MATRIX, DNA, BINARY, HEX } m_mode;
125 XWindowAttributes xgwa;
126 GC draw_gc, erase_gc;
127 int grid_width, grid_height;
128 int char_width, char_height;
135 Bool insert_top_p, insert_bottom_p;
137 signed char *tracing;
140 Pixmap images[CHAR_MAPS];
141 int image_width, image_height;
150 load_images_1 (m_state *state, int which)
152 #if defined(HAVE_GDK_PIXBUF) || defined(HAVE_XPM)
153 if (!get_boolean_resource ("mono", "Boolean") &&
154 state->xgwa.depth > 1)
157 (which == 0 ? (state->small_p ? matrix0b_xpm : matrix0_xpm) :
158 which == 1 ? (state->small_p ? matrix1b_xpm : matrix1_xpm) :
159 (state->small_p ? matrix2b_xpm : matrix2_xpm));
161 state->images[which] =
162 xpm_data_to_pixmap (state->dpy, state->window, bits,
163 &state->image_width, &state->image_height, 0);
166 #endif /* !HAVE_XPM && !HAVE_GDK_PIXBUF */
168 unsigned long fg, bg;
169 state->image_width = (state->small_p ? matrix0b_width :matrix0_width);
170 state->image_height = (state->small_p ? matrix0b_height:matrix0_height);
171 fg = get_pixel_resource("foreground", "Foreground",
172 state->dpy, state->xgwa.colormap);
173 bg = get_pixel_resource("background", "Background",
174 state->dpy, state->xgwa.colormap);
175 state->images[which] =
176 XCreatePixmapFromBitmapData (state->dpy, state->window, (char *)
177 (which == 0 ? (state->small_p ? matrix0b_bits :matrix0_bits) :
178 which == 1 ? (state->small_p ? matrix1b_bits :matrix1_bits) :
179 (state->small_p ? matrix2b_bits :matrix2_bits)),
180 state->image_width, state->image_height,
181 bg, fg, state->xgwa.depth);
187 load_images (m_state *state)
189 load_images_1 (state, 0);
190 load_images_1 (state, 1);
191 load_images_1 (state, 2);
196 flip_images_1 (m_state *state, int which)
198 XImage *im = XGetImage (state->dpy, state->images[which], 0, 0,
199 state->image_width, state->image_height,
200 ~0L, (state->xgwa.depth > 1 ? ZPixmap : XYPixmap));
202 int ww = state->char_width;
203 unsigned long *row = (unsigned long *) malloc (sizeof(*row) * ww);
205 for (y = 0; y < state->image_height; y++)
207 for (x = 0; x < CHAR_COLS; x++)
209 for (xx = 0; xx < ww; xx++)
210 row[xx] = XGetPixel (im, (x * ww) + xx, y);
211 for (xx = 0; xx < ww; xx++)
212 XPutPixel (im, (x * ww) + xx, y, row[ww - xx - 1]);
216 XPutImage (state->dpy, state->images[which], state->draw_gc, im, 0, 0, 0, 0,
217 state->image_width, state->image_height);
223 flip_images (m_state *state)
225 flip_images_1 (state, 0);
226 flip_images_1 (state, 1);
227 flip_images_1 (state, 2);
232 init_spinners (m_state *state)
234 int i = state->nspinners;
238 for (y = 0; y < state->grid_height; y++)
239 for (x = 0; x < state->grid_width; x++)
241 cell = &state->cells[state->grid_width * y + x];
247 x = random() % state->grid_width;
248 y = random() % state->grid_height;
249 cell = &state->cells[state->grid_width * y + x];
256 init_trace (m_state *state)
258 char *s = get_string_resource ("tracePhone", "TracePhone");
264 state->tracing = (char *) malloc (strlen (s) + 1);
267 for (s2 = s; *s2; s2++)
268 if (*s2 >= '0' && *s2 <= '9')
272 if (s3 == (char *) state->tracing)
275 for (i = 0; i < strlen(state->tracing); i++)
276 state->tracing[i] = -state->tracing[i];
278 state->glyph_map = decimal_encoding;
279 state->nglyphs = countof(decimal_encoding);
284 fprintf (stderr, "%s: bad phone number: \"%s\".\n",
285 progname, s ? s : "(null)");
288 if (state->tracing) free (state->tracing);
290 state->mode = MATRIX;
295 init_matrix (Display *dpy, Window window)
299 m_state *state = (m_state *) calloc (sizeof(*state), 1);
302 state->window = window;
304 XGetWindowAttributes (dpy, window, &state->xgwa);
307 const char *s = get_string_resource ("small", "Boolean");
309 state->small_p = get_boolean_resource ("small", "Boolean");
311 state->small_p = (state->xgwa.width < 300);
316 gcv.foreground = get_pixel_resource("foreground", "Foreground",
317 state->dpy, state->xgwa.colormap);
318 gcv.background = get_pixel_resource("background", "Background",
319 state->dpy, state->xgwa.colormap);
320 state->draw_gc = XCreateGC (state->dpy, state->window,
321 GCForeground|GCBackground, &gcv);
322 gcv.foreground = gcv.background;
323 state->erase_gc = XCreateGC (state->dpy, state->window,
324 GCForeground|GCBackground, &gcv);
326 state->char_width = state->image_width / CHAR_COLS;
327 state->char_height = state->image_height / CHAR_ROWS;
329 state->grid_width = state->xgwa.width / state->char_width;
330 state->grid_height = state->xgwa.height / state->char_height;
332 state->grid_height++;
333 if (state->grid_width < 5) state->grid_width = 5;
334 if (state->grid_height < 5) state->grid_height = 5;
336 state->glyph_map = matrix_encoding;
337 state->nglyphs = countof(matrix_encoding);
339 state->cells = (m_cell *)
340 calloc (sizeof(m_cell), state->grid_width * state->grid_height);
341 state->cursor = NULL;
342 state->feeders = (m_feeder *) calloc (sizeof(m_feeder), state->grid_width);
344 state->density = get_integer_resource ("density", "Integer");
346 insert = get_string_resource("insert", "Insert");
347 if (insert && !strcmp(insert, "top"))
349 state->insert_top_p = True;
350 state->insert_bottom_p = False;
352 else if (insert && !strcmp(insert, "bottom"))
354 state->insert_top_p = False;
355 state->insert_bottom_p = True;
357 else if (insert && !strcmp(insert, "both"))
359 state->insert_top_p = True;
360 state->insert_bottom_p = True;
364 if (insert && *insert)
366 "%s: `insert' must be `top', `bottom', or `both', not `%s'\n",
368 state->insert_top_p = False;
369 state->insert_bottom_p = True;
372 state->nspinners = get_integer_resource ("spinners", "Integer");
377 state->knock_knock_p = get_boolean_resource ("knockKnock", "KnockKnock");
379 mode = get_string_resource ("mode", "Mode");
380 if (mode && !strcasecmp(mode, "trace"))
381 state->mode = TRACE0;
382 else if (mode && !strcasecmp(mode, "crack"))
384 else if (mode && !strcasecmp(mode, "dna"))
386 else if (mode && !strcasecmp(mode, "binary"))
387 state->mode = BINARY;
388 else if (mode && (!strcasecmp(mode, "hex") ||
389 !strcasecmp(mode, "hexadecimal")))
391 else if (!mode || !*mode || !strcasecmp(mode, "matrix"))
392 state->mode = MATRIX;
396 "%s: `mode' must be matrix, trace, dna, binary, or hex: not `%s'\n",
398 state->mode = MATRIX;
401 if (state->mode == DNA)
403 state->glyph_map = dna_encoding;
404 state->nglyphs = countof(dna_encoding);
406 else if (state->mode == BINARY)
408 state->glyph_map = binary_encoding;
409 state->nglyphs = countof(binary_encoding);
411 else if (state->mode == HEX)
413 state->glyph_map = hex_encoding;
414 state->nglyphs = countof(hex_encoding);
416 else if (state->mode == HEX)
418 state->glyph_map = hex_encoding;
419 state->nglyphs = countof(hex_encoding);
421 else if (state->mode == TRACE0)
423 else if (state->mode == NMAP0)
428 init_spinners (state);
436 insert_glyph (m_state *state, int glyph, int x, int y)
438 Bool bottom_feeder_p = (y >= 0);
441 if (y >= state->grid_height)
446 to = &state->cells[state->grid_width * y + x];
450 for (y = state->grid_height-1; y > 0; y--)
452 from = &state->cells[state->grid_width * (y-1) + x];
453 to = &state->cells[state->grid_width * y + x];
454 to->glyph = from->glyph;
455 to->glow = from->glow;
458 to = &state->cells[x];
466 else if (bottom_feeder_p)
467 to->glow = 1 + (random() % 2);
474 feed_matrix (m_state *state)
480 case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
486 /* Update according to current feeders. */
487 for (x = 0; x < state->grid_width; x++)
489 m_feeder *f = &state->feeders[x];
491 if (f->throttle) /* this is a delay tick, synced to frame. */
495 else if (f->remaining > 0) /* how many items are in the pipe */
497 int g = state->glyph_map[(random() % state->nglyphs)] + 1;
498 insert_glyph (state, g, x, f->y);
500 if (f->y >= 0) /* bottom_feeder_p */
503 else /* if pipe is empty, insert spaces */
505 insert_glyph (state, 0, x, f->y);
506 if (f->y >= 0) /* bottom_feeder_p */
510 if ((random() % 10) == 0) /* randomly change throttle speed */
512 f->throttle = ((random() % 5) + (random() % 5));
519 redraw_cells (m_state *state, Bool active)
524 for (y = 0; y < state->grid_height; y++)
525 for (x = 0; x < state->grid_width; x++)
527 m_cell *cell = &state->cells[state->grid_width * y + x];
532 if (state->mode == TRACE2 && active)
534 int xx = x % strlen(state->tracing);
535 Bool dead_p = state->tracing[xx] > 0;
537 if (y == 0 && x == xx)
538 cell->glyph = (dead_p
539 ? state->glyph_map[state->tracing[xx]-'0'] + 1
544 cell->glyph = (dead_p ? 0 :
545 (state->glyph_map[(random()%state->nglyphs)]
554 if (cell->glyph == 0 && cell != state->cursor)
555 XFillRectangle (state->dpy, state->window, state->erase_gc,
556 x * state->char_width,
557 y * state->char_height,
562 int g = (cell == state->cursor ? CURSOR_GLYPH : cell->glyph);
563 int cx = (g - 1) % CHAR_COLS;
564 int cy = (g - 1) / CHAR_COLS;
565 int map = ((cell->glow > 0 || cell->spinner) ? GLOW_MAP :
566 (cell->glow < 0) ? FADE_MAP :
569 XCopyArea (state->dpy, state->images[map],
570 state->window, state->draw_gc,
571 cx * state->char_width,
572 cy * state->char_height,
575 x * state->char_width,
576 y * state->char_height);
586 else if (cell->glow < 0)
594 if (cell->spinner && active)
596 cell->glyph = (state->glyph_map[(random()%state->nglyphs)] + 1);
603 state->cursor->changed = 1;
609 densitizer (m_state *state)
611 /* Horrid kludge that converts percentages (density of screen coverage)
612 to the parameter that actually controls this. I got this mapping
613 empirically, on a 1024x768 screen. Sue me. */
614 if (state->density < 10) return 85;
615 else if (state->density < 15) return 60;
616 else if (state->density < 20) return 45;
617 else if (state->density < 25) return 25;
618 else if (state->density < 30) return 20;
619 else if (state->density < 35) return 15;
620 else if (state->density < 45) return 10;
621 else if (state->density < 50) return 8;
622 else if (state->density < 55) return 7;
623 else if (state->density < 65) return 5;
624 else if (state->density < 80) return 3;
625 else if (state->density < 90) return 2;
630 static void drain_matrix (m_state *);
633 handle_events (m_state *state)
635 XSync (state->dpy, False);
636 while (XPending (state->dpy))
639 XNextEvent (state->dpy, &event);
641 if (event.xany.type == ConfigureNotify)
643 int ow = state->grid_width;
644 int oh = state->grid_height;
645 XGetWindowAttributes (state->dpy, state->window, &state->xgwa);
646 state->grid_width = state->xgwa.width / state->char_width;
647 state->grid_height = state->xgwa.height / state->char_height;
649 state->grid_height++;
650 if (state->grid_width < 5) state->grid_width = 5;
651 if (state->grid_height < 5) state->grid_height = 5;
653 if (ow != state->grid_width ||
654 oh != state->grid_height)
656 m_cell *ncells = (m_cell *)
657 calloc (sizeof(m_cell),
658 state->grid_width * state->grid_height);
659 m_feeder *nfeeders = (m_feeder *)
660 calloc (sizeof(m_feeder), state->grid_width);
663 /* fprintf(stderr, "resize: %d x %d ==> %d x %d\n",
664 ow, oh, state->grid_width, state->grid_height); */
666 for (y = 0; y < oh; y++)
667 for (x = 0; x < ow; x++)
668 if (x < ow && x < state->grid_width &&
669 y < oh && y < state->grid_height)
670 ncells[y * state->grid_width + x] =
671 state->cells[y * ow + x];
673 state->cells = ncells;
675 x = (ow < state->grid_width ? ow : state->grid_width);
676 for (i = 0; i < x; i++)
677 nfeeders[i] = state->feeders[i];
678 free (state->feeders);
679 state->feeders = nfeeders;
682 else if (event.xany.type == KeyPress)
686 XLookupString (&event.xkey, &c, 1, &keysym, 0);
689 drain_matrix (state);
692 else if (c == '+' || c == '=' || c == '>' || c == '.')
694 state->density += 10;
695 if (state->density > 100)
696 state->density = 100;
700 else if (c == '-' || c == '_' || c == '<' || c == ',')
702 state->density -= 10;
703 if (state->density < 0)
710 screenhack_handle_event (state->dpy, &event);
716 matrix_usleep (m_state *state, unsigned long delay)
722 int blink_delay = 333000;
724 m_cell *cursor = state->cursor;
725 while (tot_delay < delay)
729 usleep (blink_delay * 2);
730 tot_delay += (2 * blink_delay);
731 state->cursor = NULL;
735 usleep (blink_delay);
736 tot_delay += blink_delay;
737 state->cursor = cursor;
740 redraw_cells (state, False);
741 XSync (state->dpy, False);
742 handle_events (state);
747 XSync (state->dpy, False);
748 handle_events (state);
755 hack_text_1 (m_state *state,
766 int i = state->grid_width * y + x;
769 if (y >= state->grid_height-1) return;
774 Bool done_p = s[1] == '\000';
782 i = state->grid_width * y + x;
787 for (yy = 0; yy < state->grid_height-1; yy++)
788 for (xx = 0; xx < state->grid_width; xx++)
790 int ii = yy * state->grid_width + xx;
791 int jj = (yy+1) * state->grid_width + xx;
792 state->cells[ii] = state->cells[jj];
793 state->cells[ii].changed = 1;
795 /* clear bottom row */
796 for (xx = 0; xx < state->grid_width; xx++)
798 int ii = yy * state->grid_width + xx;
799 state->cells[ii].glyph = 0;
800 state->cells[ii].changed = 1;
802 y--; /* move it back */
803 i = state->grid_width * y + x;
806 if (y >= state->grid_height) return;
808 cell = &state->cells[i];
812 state->cursor = cell;
815 else if (*s == '\010')
817 else if (*s == '\002')
821 cell = &state->cells[i];
822 if (x < state->grid_width-1)
824 cell->glyph = char_map[(unsigned char) *s] + 1;
825 if (*s == ' ' || *s == '\t') cell->glyph = 0;
827 cell->glow = (glow_p ? 8 : 0);
830 m_cell *next = &state->cells[i + 1];
832 state->cursor = next;
839 if (typing_delay || transmit_delay || long_delay)
841 redraw_cells (state, False);
842 XSync (state->dpy, False);
843 handle_events (state);
847 if (typing_delay && 0 == random() % 3)
848 usleep (0xFFFFFF & ((random() % 250000) + 1));
852 matrix_usleep (state, 1000000);
864 hack_text (m_state *state)
870 Bool typing_delay = False;
871 Bool transmit_delay = False;
872 Bool long_delay = False;
873 Bool visible_cursor = False;
876 case TRACE0: if (state->grid_width >= 52)
877 s = "Call trans opt: received. 2-19-98 13:24:18 REC:Log>";
879 s = "Call trans opt: received.\n2-19-98 13:24:18 REC:Log>";
880 transmit_delay = True;
881 visible_cursor = True;
883 case TRACE1: s = "Trace program: running";
884 transmit_delay = True;
885 visible_cursor = True;
887 case KNOCK0: s = "Wake up, Neo..."; break;
888 case KNOCK1: s = ""; break;
889 case KNOCK2: s = "The Matrix has you..."; typing_delay = True; break;
890 case KNOCK3: s = ""; break;
891 case KNOCK4: s = "Follow the white rabbit."; typing_delay = True; break;
892 case KNOCK5: s = ""; break;
893 case KNOCK6: s = "Knock, knock, Neo."; break;
896 case NMAP1: s = ""; break;
898 default: abort(); break;
901 for (i = 0; i < state->grid_height * state->grid_width; i++)
903 m_cell *cell = &state->cells[i];
904 cell->changed = (cell->glyph != 0);
908 if (state->mode == NMAP0)
910 /* Note that what Trinity is using here is moderately accurate:
911 She runs nmap (http://www.insecure.org/nmap/) then breaks in
912 with a (hypothetical) program called "sshnuke" that exploits
913 the (very real) SSHv1 CRC32 compensation attack detector bug
914 (http://staff.washington.edu/dittrich/misc/ssh-analysis.txt).
916 The command syntax of the power grid control software looks a
917 lot like Cisco IOS to me. (IOS is a descendant of VMS.)
919 const char *blocks[] = {
922 "\001nmap 10.2.2.2\n",
923 "Starting nmap V. 2.54BETA25\n"
925 "\010", "\010", "\010",
927 "Insufficient responses for TCP sequencing (3), OS detection "
930 "Interesting ports on 10.2.2.2:\n"
931 "(The 1538 ports scanned but not shown below are in state: filtered)\n"
932 "Port state service\n"
935 "No exact OS matches for host\n"
937 "Nmap run completed -- 1 IP address (1 host up) scanned\n"
940 "\001sshnuke 10.2.2.2 -rootpw=\"Z1ON0101\"\n",
942 "Connecting to 10.2.2.2:ssh ... ",
945 "Attempting to exploit SSHv1 CRC32 ... ",
948 "Resetting root password to \"Z1ON0101\".\n",
950 "System open: Access Level <9>\n"
953 "\001ssh 10.2.2.2 -l root\n",
955 "root@10.2.2.2's password: ",
957 "\001\010\010\010\010\010\010\010\010\n",
962 "\001disable grid nodes 21 - 40\n",
964 "Warning: Disabling nodes 21-40 will disconnect sector 11 (27 nodes)\n"
966 "\002 ARE YOU SURE? (y/n) ",
972 int nblocks = countof(blocks);
973 int y = state->grid_height - 2;
976 visible_cursor = True;
978 for (j = 0; j < nblocks; j++)
980 const char *s = blocks[j];
981 typing_delay = (*s == '\001');
982 if (typing_delay) s++;
985 hack_text_1 (state, &x, &y, s,
986 typing_delay, transmit_delay, long_delay,
987 visible_cursor, True);
990 typing_delay = False;
992 for (j = 21; j <= 40; j++)
995 sprintf (buf, "Grid Node %d offline...\n", j);
996 hack_text_1 (state, &x, &y, buf,
997 typing_delay, transmit_delay, long_delay,
998 visible_cursor, True);
1001 hack_text_1 (state, &x, &y, "\nFFF-CONTROL> ",
1002 typing_delay, transmit_delay, long_delay,
1003 visible_cursor, True);
1008 if (state->mode == TRACE0 || state->mode == TRACE1)
1012 x = ((int)state->grid_width - (int)strlen(s)) / 2;
1013 y = (state->grid_height / 2) - 1;
1018 hack_text_1 (state, &x, &y, s,
1019 typing_delay, transmit_delay, long_delay,
1020 visible_cursor, False);
1026 drain_matrix (m_state *state)
1028 int delay = get_integer_resource ("delay", "Integer");
1031 /* Fill the top row with empty top-feeders, to clear the screen. */
1032 for (i = 0; i < state->grid_width; i++)
1034 m_feeder *f = &state->feeders[i];
1040 /* Turn off all the spinners, else they never go away. */
1041 for (i = 0; i < state->grid_width * state->grid_height; i++)
1042 if (state->cells[i].spinner)
1044 state->cells[i].spinner = 0;
1045 state->cells[i].changed = 1;
1048 /* Run the machine until there are no live cells left. */
1051 Bool any_cells_p = False;
1052 for (i = 0; i < state->grid_width * state->grid_height; i++)
1053 if (state->cells[i].glyph)
1062 feed_matrix (state);
1063 redraw_cells (state, True);
1064 XSync (state->dpy, False);
1065 handle_events (state);
1066 if (delay) usleep (delay);
1072 roll_state (m_state *state)
1075 switch (state->mode)
1079 state->mode = TRACE1;
1084 state->mode = TRACE2;
1091 for (i = 0; i < strlen(state->tracing); i++)
1092 if (state->tracing[i] < 0) any = True;
1096 XSync (state->dpy, False);
1097 matrix_usleep (state, 3000000);
1098 state->mode = MATRIX;
1099 state->glyph_map = matrix_encoding;
1100 state->nglyphs = countof(matrix_encoding);
1101 flip_images (state);
1102 free (state->tracing);
1105 else if ((random() % 10) == 0)
1107 int x = random() % strlen(state->tracing);
1108 if (state->tracing[x] < 0)
1109 state->tracing[x] = -state->tracing[x];
1114 case KNOCK0: delay = 1; state->mode++; break; /* wake */
1115 case KNOCK1: delay = 4; state->mode++; break;
1116 case KNOCK2: delay = 2; state->mode++; break; /* has */
1117 case KNOCK3: delay = 4; state->mode++; break;
1118 case KNOCK4: delay = 2; state->mode++; break; /* rabbit */
1119 case KNOCK5: delay = 4; state->mode++; break;
1120 case KNOCK6: delay = 4; state->mode++; break; /* knock */
1122 case NMAP0: delay = 4; state->mode++; break; /* knock */
1127 state->mode = MATRIX;
1128 state->glyph_map = matrix_encoding;
1129 state->nglyphs = countof(matrix_encoding);
1130 flip_images (state);
1134 if (state->knock_knock_p && (! (random() % 3500)))
1136 drain_matrix (state);
1137 if (! (random() % 5))
1138 state->mode = NMAP0;
1140 state->mode = KNOCK0;
1142 flip_images (state);
1146 case DNA: case BINARY: case HEX:
1154 matrix_usleep (state, delay * 1000000);
1155 state->cursor = NULL;
1160 hack_matrix (m_state *state)
1164 switch (state->mode)
1166 case TRACE0: case TRACE1:
1167 case KNOCK0: case KNOCK1: case KNOCK2: case KNOCK3:
1168 case KNOCK4: case KNOCK5: case KNOCK6: case KNOCK7:
1169 case NMAP0: case NMAP1:
1172 case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
1178 /* Glow some characters. */
1179 if (!state->insert_bottom_p)
1181 int i = random() % (state->grid_width / 2);
1184 int x = random() % state->grid_width;
1185 int y = random() % state->grid_height;
1186 m_cell *cell = &state->cells[state->grid_width * y + x];
1187 if (cell->glyph && cell->glow == 0)
1189 cell->glow = random() % 10;
1195 /* Change some of the feeders. */
1196 for (x = 0; x < state->grid_width; x++)
1198 m_feeder *f = &state->feeders[x];
1199 Bool bottom_feeder_p;
1201 if (f->remaining > 0) /* never change if pipe isn't empty */
1204 if ((random() % densitizer(state)) != 0) /* then change N% of the time */
1207 f->remaining = 3 + (random() % state->grid_height);
1208 f->throttle = ((random() % 5) + (random() % 5));
1210 if ((random() % 4) != 0)
1213 if (state->mode == TRACE2)
1214 bottom_feeder_p = True;
1215 if (state->insert_top_p && state->insert_bottom_p)
1216 bottom_feeder_p = (random() & 1);
1218 bottom_feeder_p = state->insert_bottom_p;
1220 if (bottom_feeder_p)
1221 f->y = random() % (state->grid_height / 2);
1226 if (!state->mode == TRACE2 &&
1228 init_spinners (state);
1233 draw_matrix (m_state *state)
1235 feed_matrix (state);
1236 hack_matrix (state);
1237 redraw_cells (state, True);
1243 static int ndens = 0;
1244 static int tdens = 0;
1250 ((double) (state->grid_width * state->grid_height))));
1253 printf ("density: %d%% (%d%%)\n", dens, (tdens / ndens));
1262 char *progclass = "XMatrix";
1264 char *defaults [] = {
1265 ".background: black",
1266 ".foreground: #00AA00",
1271 "*tracePhone: (212) 555-0690",
1274 "*knockKnock: False",
1275 "*geometry: 800x600",
1279 XrmOptionDescRec options [] = {
1280 { "-small", ".small", XrmoptionNoArg, "True" },
1281 { "-large", ".small", XrmoptionNoArg, "False" },
1282 { "-delay", ".delay", XrmoptionSepArg, 0 },
1283 { "-top", ".insert", XrmoptionNoArg, "top" },
1284 { "-bottom", ".insert", XrmoptionNoArg, "bottom" },
1285 { "-both", ".insert", XrmoptionNoArg, "both" },
1286 { "-density", ".density", XrmoptionSepArg, 0 },
1287 { "-trace", ".mode", XrmoptionNoArg, "trace" },
1288 { "-crack", ".mode", XrmoptionNoArg, "crack"},
1289 { "-phone", ".tracePhone", XrmoptionSepArg, 0 },
1290 { "-dna", ".mode", XrmoptionNoArg, "DNA" },
1291 { "-binary", ".mode", XrmoptionNoArg, "binary" },
1292 { "-hexadecimal", ".mode", XrmoptionNoArg, "hexadecimal"},
1293 { "-knock-knock", ".knockKnock", XrmoptionNoArg, "True" },
1299 screenhack (Display *dpy, Window window)
1301 m_state *state = init_matrix (dpy, window);
1302 int delay = get_integer_resource ("delay", "Integer");
1305 draw_matrix (state);
1307 handle_events (state);
1308 if (delay) usleep (delay);