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"
46 #include <X11/Xutil.h>
50 # include "images/matrix0.xpm"
51 # include "images/matrix1.xpm"
52 # include "images/matrix2.xpm"
53 # include "images/matrix0b.xpm"
54 # include "images/matrix1b.xpm"
55 # include "images/matrix2b.xpm"
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 typedef enum { TRACE0, TRACE1, TRACE2,
115 KNOCK0, KNOCK1, KNOCK2, KNOCK3,
116 KNOCK4, KNOCK5, KNOCK6, KNOCK7,
117 MATRIX, DNA, BINARY, HEX } m_mode;
122 XWindowAttributes xgwa;
123 GC draw_gc, erase_gc;
124 int grid_width, grid_height;
125 int char_width, char_height;
130 Bool insert_top_p, insert_bottom_p;
132 signed char *tracing;
135 Pixmap images[CHAR_MAPS];
136 int image_width, image_height;
145 load_images_1 (m_state *state, int which)
148 if (!get_boolean_resource ("mono", "Boolean") &&
149 state->xgwa.depth > 1)
153 XpmAttributes xpmattrs;
155 xpmattrs.valuemask = 0;
158 xpmattrs.valuemask |= XpmCloseness;
159 xpmattrs.closeness = 40000;
162 xpmattrs.valuemask |= XpmVisual;
163 xpmattrs.visual = state->xgwa.visual;
166 xpmattrs.valuemask |= XpmDepth;
167 xpmattrs.depth = state->xgwa.depth;
170 xpmattrs.valuemask |= XpmColormap;
171 xpmattrs.colormap = state->xgwa.colormap;
174 result = XpmCreatePixmapFromData (state->dpy, state->window,
175 (which == 0 ? (state->small_p ? matrix0b_xpm : matrix0_xpm) :
176 which == 1 ? (state->small_p ? matrix1b_xpm : matrix1_xpm) :
177 (state->small_p ? matrix2b_xpm : matrix2_xpm)),
178 &state->images[which], 0 /* mask */,
180 if (!state->images || (result != XpmSuccess && result != XpmColorError))
181 state->images[which] = 0;
183 state->image_width = xpmattrs.width;
184 state->image_height = xpmattrs.height;
187 #endif /* !HAVE_XPM */
189 unsigned long fg, bg;
190 state->image_width = (state->small_p ? matrix0b_width :matrix0_width);
191 state->image_height = (state->small_p ? matrix0b_height:matrix0_height);
192 fg = get_pixel_resource("foreground", "Foreground",
193 state->dpy, state->xgwa.colormap);
194 bg = get_pixel_resource("background", "Background",
195 state->dpy, state->xgwa.colormap);
196 state->images[which] =
197 XCreatePixmapFromBitmapData (state->dpy, state->window, (char *)
198 (which == 0 ? (state->small_p ? matrix0b_bits :matrix0_bits) :
199 which == 1 ? (state->small_p ? matrix1b_bits :matrix1_bits) :
200 (state->small_p ? matrix2b_bits :matrix2_bits)),
201 state->image_width, state->image_height,
202 bg, fg, state->xgwa.depth);
208 load_images (m_state *state)
210 load_images_1 (state, 0);
211 load_images_1 (state, 1);
212 load_images_1 (state, 2);
217 flip_images_1 (m_state *state, int which)
219 XImage *im = XGetImage (state->dpy, state->images[which], 0, 0,
220 state->image_width, state->image_height,
221 ~0L, (state->xgwa.depth > 1 ? ZPixmap : XYPixmap));
223 int ww = state->char_width;
224 unsigned long *row = (unsigned long *) malloc (sizeof(*row) * ww);
226 for (y = 0; y < state->image_height; y++)
228 for (x = 0; x < CHAR_COLS; x++)
230 for (xx = 0; xx < ww; xx++)
231 row[xx] = XGetPixel (im, (x * ww) + xx, y);
232 for (xx = 0; xx < ww; xx++)
233 XPutPixel (im, (x * ww) + xx, y, row[ww - xx - 1]);
237 XPutImage (state->dpy, state->images[which], state->draw_gc, im, 0, 0, 0, 0,
238 state->image_width, state->image_height);
244 flip_images (m_state *state)
246 flip_images_1 (state, 0);
247 flip_images_1 (state, 1);
248 flip_images_1 (state, 2);
253 init_spinners (m_state *state)
255 int i = state->nspinners;
259 for (y = 0; y < state->grid_height; y++)
260 for (x = 0; x < state->grid_width; x++)
262 cell = &state->cells[state->grid_width * y + x];
268 x = random() % state->grid_width;
269 y = random() % state->grid_height;
270 cell = &state->cells[state->grid_width * y + x];
277 init_trace (m_state *state)
279 char *s = get_string_resource ("tracePhone", "TracePhone");
285 state->tracing = (char *) malloc (strlen (s) + 1);
288 for (s2 = s; *s2; s2++)
289 if (*s2 >= '0' && *s2 <= '9')
293 if (s3 == (char *) state->tracing)
296 for (i = 0; i < strlen(state->tracing); i++)
297 state->tracing[i] = -state->tracing[i];
299 state->glyph_map = decimal_encoding;
300 state->nglyphs = countof(decimal_encoding);
305 fprintf (stderr, "%s: bad phone number: \"%s\".\n",
306 progname, s ? s : "(null)");
309 if (state->tracing) free (state->tracing);
311 state->mode = MATRIX;
316 init_matrix (Display *dpy, Window window)
320 m_state *state = (m_state *) calloc (sizeof(*state), 1);
323 state->window = window;
325 XGetWindowAttributes (dpy, window, &state->xgwa);
328 const char *s = get_string_resource ("small", "Boolean");
330 state->small_p = get_boolean_resource ("small", "Boolean");
332 state->small_p = (state->xgwa.width < 300);
337 gcv.foreground = get_pixel_resource("foreground", "Foreground",
338 state->dpy, state->xgwa.colormap);
339 gcv.background = get_pixel_resource("background", "Background",
340 state->dpy, state->xgwa.colormap);
341 state->draw_gc = XCreateGC (state->dpy, state->window,
342 GCForeground|GCBackground, &gcv);
343 gcv.foreground = gcv.background;
344 state->erase_gc = XCreateGC (state->dpy, state->window,
345 GCForeground|GCBackground, &gcv);
347 state->char_width = state->image_width / CHAR_COLS;
348 state->char_height = state->image_height / CHAR_ROWS;
350 state->grid_width = state->xgwa.width / state->char_width;
351 state->grid_height = state->xgwa.height / state->char_height;
353 state->grid_height++;
355 state->glyph_map = matrix_encoding;
356 state->nglyphs = countof(matrix_encoding);
358 state->cells = (m_cell *)
359 calloc (sizeof(m_cell), state->grid_width * state->grid_height);
360 state->feeders = (m_feeder *) calloc (sizeof(m_feeder), state->grid_width);
362 state->density = get_integer_resource ("density", "Integer");
364 insert = get_string_resource("insert", "Insert");
365 if (insert && !strcmp(insert, "top"))
367 state->insert_top_p = True;
368 state->insert_bottom_p = False;
370 else if (insert && !strcmp(insert, "bottom"))
372 state->insert_top_p = False;
373 state->insert_bottom_p = True;
375 else if (insert && !strcmp(insert, "both"))
377 state->insert_top_p = True;
378 state->insert_bottom_p = True;
382 if (insert && *insert)
384 "%s: `insert' must be `top', `bottom', or `both', not `%s'\n",
386 state->insert_top_p = False;
387 state->insert_bottom_p = True;
390 state->nspinners = get_integer_resource ("spinners", "Integer");
395 mode = get_string_resource ("mode", "Mode");
396 if (mode && !strcasecmp(mode, "trace"))
397 state->mode = TRACE0;
398 else if (mode && !strcasecmp(mode, "dna"))
400 else if (mode && !strcasecmp(mode, "binary"))
401 state->mode = BINARY;
402 else if (mode && (!strcasecmp(mode, "hex") ||
403 !strcasecmp(mode, "hexadecimal")))
405 else if (!mode || !*mode || !strcasecmp(mode, "matrix"))
406 state->mode = MATRIX;
410 "%s: `mode' must be matrix, trace, dna, binary, or hex: not `%s'\n",
412 state->mode = MATRIX;
415 if (state->mode == DNA)
417 state->glyph_map = dna_encoding;
418 state->nglyphs = countof(dna_encoding);
420 else if (state->mode == BINARY)
422 state->glyph_map = binary_encoding;
423 state->nglyphs = countof(binary_encoding);
425 else if (state->mode == HEX)
427 state->glyph_map = hex_encoding;
428 state->nglyphs = countof(hex_encoding);
430 else if (state->mode == HEX)
432 state->glyph_map = hex_encoding;
433 state->nglyphs = countof(hex_encoding);
435 else if (state->mode == TRACE0)
440 init_spinners (state);
448 insert_glyph (m_state *state, int glyph, int x, int y)
450 Bool bottom_feeder_p = (y >= 0);
453 if (y >= state->grid_height)
458 to = &state->cells[state->grid_width * y + x];
462 for (y = state->grid_height-1; y > 0; y--)
464 from = &state->cells[state->grid_width * (y-1) + x];
465 to = &state->cells[state->grid_width * y + x];
466 to->glyph = from->glyph;
467 to->glow = from->glow;
470 to = &state->cells[x];
478 else if (bottom_feeder_p)
479 to->glow = 1 + (random() % 2);
486 feed_matrix (m_state *state)
492 case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
498 /* Update according to current feeders. */
499 for (x = 0; x < state->grid_width; x++)
501 m_feeder *f = &state->feeders[x];
503 if (f->throttle) /* this is a delay tick, synced to frame. */
507 else if (f->remaining > 0) /* how many items are in the pipe */
509 int g = state->glyph_map[(random() % state->nglyphs)] + 1;
510 insert_glyph (state, g, x, f->y);
512 if (f->y >= 0) /* bottom_feeder_p */
515 else /* if pipe is empty, insert spaces */
517 insert_glyph (state, 0, x, f->y);
518 if (f->y >= 0) /* bottom_feeder_p */
522 if ((random() % 10) == 0) /* randomly change throttle speed */
524 f->throttle = ((random() % 5) + (random() % 5));
530 densitizer (m_state *state)
532 /* Horrid kludge that converts percentages (density of screen coverage)
533 to the parameter that actually controls this. I got this mapping
534 empirically, on a 1024x768 screen. Sue me. */
535 if (state->density < 10) return 85;
536 else if (state->density < 15) return 60;
537 else if (state->density < 20) return 45;
538 else if (state->density < 25) return 25;
539 else if (state->density < 30) return 20;
540 else if (state->density < 35) return 15;
541 else if (state->density < 45) return 10;
542 else if (state->density < 50) return 8;
543 else if (state->density < 55) return 7;
544 else if (state->density < 65) return 5;
545 else if (state->density < 80) return 3;
546 else if (state->density < 90) return 2;
552 hack_text (m_state *state)
559 case TRACE0: s = "Call trans opt: received.\n"
560 "2-19-98 13:24:18 REC:Log>_"; break;
561 case TRACE1: s = "Trace program: running_"; break;
563 case KNOCK0: s = "Wake up, Neo..."; break;
564 case KNOCK1: s = ""; break;
565 case KNOCK2: s = "The Matrix has you..."; break;
566 case KNOCK3: s = ""; break;
567 case KNOCK4: s = "Follow the white rabbit..."; break;
568 case KNOCK5: s = ""; break;
569 case KNOCK6: s = "Knock knock, Neo."; break;
570 case KNOCK7: s = ""; break;
572 default: abort(); break;
575 for (i = 0; i < state->grid_height * state->grid_width; i++)
577 m_cell *cell = &state->cells[i];
578 cell->changed = (cell->glyph != 0);
582 if (state->mode == TRACE0 || state->mode == TRACE1)
587 x = ((int)state->grid_width - (int)strlen(s)) / 2;
588 y = (state->grid_height / 2) - 1;
591 i = (y * state->grid_width) + x;
598 i = ((i / state->grid_width) + 1) * state->grid_width;
603 m_cell *cell = &state->cells[i];
604 if (x < state->grid_width-1)
606 cell->glyph = char_map[(unsigned char) *s] + 1;
607 if (*s == ' ' || *s == '\t') cell->glyph = 0;
618 roll_state (m_state *state)
625 state->mode = TRACE1;
630 state->mode = TRACE2;
637 for (i = 0; i < strlen(state->tracing); i++)
638 if (state->tracing[i] < 0) any = True;
642 XSync (state->dpy, False);
644 state->mode = MATRIX;
645 state->glyph_map = matrix_encoding;
646 state->nglyphs = countof(matrix_encoding);
648 free (state->tracing);
651 else if ((random() % 10) == 0)
653 int x = random() % strlen(state->tracing);
654 if (state->tracing[x] < 0)
655 state->tracing[x] = -state->tracing[x];
660 case KNOCK0: delay = 1; state->mode++; break; /* wake */
661 case KNOCK1: delay = 4; state->mode++; break;
662 case KNOCK2: delay = 2; state->mode++; break; /* has */
663 case KNOCK3: delay = 4; state->mode++; break;
664 case KNOCK4: delay = 2; state->mode++; break; /* rabbit */
665 case KNOCK5: delay = 4; state->mode++; break;
666 case KNOCK6: delay = 4; state->mode++; break; /* knock */
668 state->mode = MATRIX;
669 state->glyph_map = matrix_encoding;
670 state->nglyphs = countof(matrix_encoding);
675 if (! (random() % 5000))
677 state->mode = KNOCK0;
682 case DNA: case BINARY: case HEX:
692 XSync (state->dpy, False);
699 hack_matrix (m_state *state)
705 case TRACE0: case TRACE1:
706 case KNOCK0: case KNOCK1: case KNOCK2: case KNOCK3:
707 case KNOCK4: case KNOCK5: case KNOCK6: case KNOCK7:
710 case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
716 /* Glow some characters. */
717 if (!state->insert_bottom_p)
719 int i = random() % (state->grid_width / 2);
722 int x = random() % state->grid_width;
723 int y = random() % state->grid_height;
724 m_cell *cell = &state->cells[state->grid_width * y + x];
725 if (cell->glyph && cell->glow == 0)
727 cell->glow = random() % 10;
733 /* Change some of the feeders. */
734 for (x = 0; x < state->grid_width; x++)
736 m_feeder *f = &state->feeders[x];
737 Bool bottom_feeder_p;
739 if (f->remaining > 0) /* never change if pipe isn't empty */
742 if ((random() % densitizer(state)) != 0) /* then change N% of the time */
745 f->remaining = 3 + (random() % state->grid_height);
746 f->throttle = ((random() % 5) + (random() % 5));
748 if ((random() % 4) != 0)
751 if (state->mode == TRACE2)
752 bottom_feeder_p = True;
753 if (state->insert_top_p && state->insert_bottom_p)
754 bottom_feeder_p = (random() & 1);
756 bottom_feeder_p = state->insert_bottom_p;
759 f->y = random() % (state->grid_height / 2);
764 if (!state->mode == TRACE2 &&
766 init_spinners (state);
771 draw_matrix (m_state *state)
779 for (y = 0; y < state->grid_height; y++)
780 for (x = 0; x < state->grid_width; x++)
782 m_cell *cell = &state->cells[state->grid_width * y + x];
787 if (state->mode == TRACE2)
789 int xx = x % strlen(state->tracing);
790 Bool dead_p = state->tracing[xx] > 0;
792 if (y == 0 && x == xx)
793 cell->glyph = (dead_p
794 ? state->glyph_map[state->tracing[xx]-'0'] + 1
799 cell->glyph = (dead_p ? 0 :
800 (state->glyph_map[(random()%state->nglyphs)]
809 if (cell->glyph == 0)
810 XFillRectangle (state->dpy, state->window, state->erase_gc,
811 x * state->char_width,
812 y * state->char_height,
817 int cx = (cell->glyph - 1) % CHAR_COLS;
818 int cy = (cell->glyph - 1) / CHAR_COLS;
819 int map = ((cell->glow > 0 || cell->spinner) ? GLOW_MAP :
820 (cell->glow == 0) ? PLAIN_MAP :
823 XCopyArea (state->dpy, state->images[map],
824 state->window, state->draw_gc,
825 cx * state->char_width,
826 cy * state->char_height,
829 x * state->char_width,
830 y * state->char_height);
840 else if (cell->glow < 0)
850 cell->glyph = (state->glyph_map[(random()%state->nglyphs)] + 1);
860 static int ndens = 0;
861 static int tdens = 0;
867 ((double) (state->grid_width * state->grid_height))));
870 printf ("density: %d%% (%d%%)\n", dens, (tdens / ndens));
879 char *progclass = "XMatrix";
881 char *defaults [] = {
882 ".background: black",
883 ".foreground: green",
888 "*tracePhone: (212) 555-0690",
894 XrmOptionDescRec options [] = {
895 { "-small", ".small", XrmoptionNoArg, "True" },
896 { "-large", ".small", XrmoptionNoArg, "False" },
897 { "-delay", ".delay", XrmoptionSepArg, 0 },
898 { "-top", ".insert", XrmoptionNoArg, "top" },
899 { "-bottom", ".insert", XrmoptionNoArg, "bottom" },
900 { "-both", ".insert", XrmoptionNoArg, "both" },
901 { "-density", ".density", XrmoptionSepArg, 0 },
902 { "-trace", ".mode", XrmoptionNoArg, "trace" },
903 { "-phone", ".tracePhone", XrmoptionSepArg, 0 },
904 { "-dna", ".mode", XrmoptionNoArg, "DNA" },
905 { "-binary", ".mode", XrmoptionNoArg, "binary" },
906 { "-hexadecimal", ".mode", XrmoptionNoArg, "hexadecimal"},
912 screenhack (Display *dpy, Window window)
914 m_state *state = init_matrix (dpy, window);
915 int delay = get_integer_resource ("delay", "Integer");
920 screenhack_handle_events (dpy);
921 if (delay) usleep (delay);