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,
119 MATRIX, DNA, BINARY, HEX } m_mode;
124 XWindowAttributes xgwa;
125 GC draw_gc, erase_gc;
126 int grid_width, grid_height;
127 int char_width, char_height;
134 Bool insert_top_p, insert_bottom_p;
136 signed char *tracing;
139 Pixmap images[CHAR_MAPS];
140 int image_width, image_height;
149 load_images_1 (m_state *state, int which)
151 #if defined(HAVE_GDK_PIXBUF) || defined(HAVE_XPM)
152 if (!get_boolean_resource ("mono", "Boolean") &&
153 state->xgwa.depth > 1)
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));
160 state->images[which] =
161 xpm_data_to_pixmap (state->dpy, state->window, bits,
162 &state->image_width, &state->image_height, 0);
165 #endif /* !HAVE_XPM && !HAVE_GDK_PIXBUF */
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);
186 load_images (m_state *state)
188 load_images_1 (state, 0);
189 load_images_1 (state, 1);
190 load_images_1 (state, 2);
195 flip_images_1 (m_state *state, int which)
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));
201 int ww = state->char_width;
202 unsigned long *row = (unsigned long *) malloc (sizeof(*row) * ww);
204 for (y = 0; y < state->image_height; y++)
206 for (x = 0; x < CHAR_COLS; x++)
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]);
215 XPutImage (state->dpy, state->images[which], state->draw_gc, im, 0, 0, 0, 0,
216 state->image_width, state->image_height);
222 flip_images (m_state *state)
224 flip_images_1 (state, 0);
225 flip_images_1 (state, 1);
226 flip_images_1 (state, 2);
231 init_spinners (m_state *state)
233 int i = state->nspinners;
237 for (y = 0; y < state->grid_height; y++)
238 for (x = 0; x < state->grid_width; x++)
240 cell = &state->cells[state->grid_width * y + x];
246 x = random() % state->grid_width;
247 y = random() % state->grid_height;
248 cell = &state->cells[state->grid_width * y + x];
255 init_trace (m_state *state)
257 char *s = get_string_resource ("tracePhone", "TracePhone");
263 state->tracing = (char *) malloc (strlen (s) + 1);
266 for (s2 = s; *s2; s2++)
267 if (*s2 >= '0' && *s2 <= '9')
271 if (s3 == (char *) state->tracing)
274 for (i = 0; i < strlen(state->tracing); i++)
275 state->tracing[i] = -state->tracing[i];
277 state->glyph_map = decimal_encoding;
278 state->nglyphs = countof(decimal_encoding);
283 fprintf (stderr, "%s: bad phone number: \"%s\".\n",
284 progname, s ? s : "(null)");
287 if (state->tracing) free (state->tracing);
289 state->mode = MATRIX;
294 init_matrix (Display *dpy, Window window)
298 m_state *state = (m_state *) calloc (sizeof(*state), 1);
301 state->window = window;
303 XGetWindowAttributes (dpy, window, &state->xgwa);
306 const char *s = get_string_resource ("small", "Boolean");
308 state->small_p = get_boolean_resource ("small", "Boolean");
310 state->small_p = (state->xgwa.width < 300);
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);
325 state->char_width = state->image_width / CHAR_COLS;
326 state->char_height = state->image_height / CHAR_ROWS;
328 state->grid_width = state->xgwa.width / state->char_width;
329 state->grid_height = state->xgwa.height / state->char_height;
331 state->grid_height++;
333 state->glyph_map = matrix_encoding;
334 state->nglyphs = countof(matrix_encoding);
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);
341 state->density = get_integer_resource ("density", "Integer");
343 insert = get_string_resource("insert", "Insert");
344 if (insert && !strcmp(insert, "top"))
346 state->insert_top_p = True;
347 state->insert_bottom_p = False;
349 else if (insert && !strcmp(insert, "bottom"))
351 state->insert_top_p = False;
352 state->insert_bottom_p = True;
354 else if (insert && !strcmp(insert, "both"))
356 state->insert_top_p = True;
357 state->insert_bottom_p = True;
361 if (insert && *insert)
363 "%s: `insert' must be `top', `bottom', or `both', not `%s'\n",
365 state->insert_top_p = False;
366 state->insert_bottom_p = True;
369 state->nspinners = get_integer_resource ("spinners", "Integer");
374 state->knock_knock_p = get_boolean_resource ("knockKnock", "KnockKnock");
376 mode = get_string_resource ("mode", "Mode");
377 if (mode && !strcasecmp(mode, "trace"))
378 state->mode = TRACE0;
379 else if (mode && !strcasecmp(mode, "dna"))
381 else if (mode && !strcasecmp(mode, "binary"))
382 state->mode = BINARY;
383 else if (mode && (!strcasecmp(mode, "hex") ||
384 !strcasecmp(mode, "hexadecimal")))
386 else if (!mode || !*mode || !strcasecmp(mode, "matrix"))
387 state->mode = MATRIX;
391 "%s: `mode' must be matrix, trace, dna, binary, or hex: not `%s'\n",
393 state->mode = MATRIX;
396 if (state->mode == DNA)
398 state->glyph_map = dna_encoding;
399 state->nglyphs = countof(dna_encoding);
401 else if (state->mode == BINARY)
403 state->glyph_map = binary_encoding;
404 state->nglyphs = countof(binary_encoding);
406 else if (state->mode == HEX)
408 state->glyph_map = hex_encoding;
409 state->nglyphs = countof(hex_encoding);
411 else if (state->mode == HEX)
413 state->glyph_map = hex_encoding;
414 state->nglyphs = countof(hex_encoding);
416 else if (state->mode == TRACE0)
421 init_spinners (state);
429 insert_glyph (m_state *state, int glyph, int x, int y)
431 Bool bottom_feeder_p = (y >= 0);
434 if (y >= state->grid_height)
439 to = &state->cells[state->grid_width * y + x];
443 for (y = state->grid_height-1; y > 0; y--)
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;
451 to = &state->cells[x];
459 else if (bottom_feeder_p)
460 to->glow = 1 + (random() % 2);
467 feed_matrix (m_state *state)
473 case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
479 /* Update according to current feeders. */
480 for (x = 0; x < state->grid_width; x++)
482 m_feeder *f = &state->feeders[x];
484 if (f->throttle) /* this is a delay tick, synced to frame. */
488 else if (f->remaining > 0) /* how many items are in the pipe */
490 int g = state->glyph_map[(random() % state->nglyphs)] + 1;
491 insert_glyph (state, g, x, f->y);
493 if (f->y >= 0) /* bottom_feeder_p */
496 else /* if pipe is empty, insert spaces */
498 insert_glyph (state, 0, x, f->y);
499 if (f->y >= 0) /* bottom_feeder_p */
503 if ((random() % 10) == 0) /* randomly change throttle speed */
505 f->throttle = ((random() % 5) + (random() % 5));
512 redraw_cells (m_state *state, Bool active)
517 for (y = 0; y < state->grid_height; y++)
518 for (x = 0; x < state->grid_width; x++)
520 m_cell *cell = &state->cells[state->grid_width * y + x];
525 if (state->mode == TRACE2 && active)
527 int xx = x % strlen(state->tracing);
528 Bool dead_p = state->tracing[xx] > 0;
530 if (y == 0 && x == xx)
531 cell->glyph = (dead_p
532 ? state->glyph_map[state->tracing[xx]-'0'] + 1
537 cell->glyph = (dead_p ? 0 :
538 (state->glyph_map[(random()%state->nglyphs)]
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,
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 :
562 XCopyArea (state->dpy, state->images[map],
563 state->window, state->draw_gc,
564 cx * state->char_width,
565 cy * state->char_height,
568 x * state->char_width,
569 y * state->char_height);
579 else if (cell->glow < 0)
587 if (cell->spinner && active)
589 cell->glyph = (state->glyph_map[(random()%state->nglyphs)] + 1);
596 state->cursor->changed = 1;
602 densitizer (m_state *state)
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;
624 hack_text (m_state *state)
629 Bool typing_delay = False;
630 Bool transmit_delay = False;
631 Bool visible_cursor = False;
634 case TRACE0: if (state->grid_width >= 52)
635 s = "Call trans opt: received. 2-19-98 13:24:18 REC:Log>";
637 s = "Call trans opt: received.\n2-19-98 13:24:18 REC:Log>";
638 transmit_delay = True;
639 visible_cursor = True;
641 case TRACE1: s = "Trace program: running";
642 transmit_delay = True;
643 visible_cursor = True;
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;
654 default: abort(); break;
657 for (i = 0; i < state->grid_height * state->grid_width; i++)
659 m_cell *cell = &state->cells[i];
660 cell->changed = (cell->glyph != 0);
664 if (state->mode == TRACE0 || state->mode == TRACE1)
669 x = ((int)state->grid_width - (int)strlen(s)) / 2;
670 y = (state->grid_height / 2) - 1;
673 i = (y * state->grid_width) + x;
680 i = ((i / state->grid_width) + 1) * state->grid_width;
685 m_cell *cell = &state->cells[i];
686 if (x < state->grid_width-1)
688 cell->glyph = char_map[(unsigned char) *s] + 1;
689 if (*s == ' ' || *s == '\t') cell->glyph = 0;
693 m_cell *next = &state->cells[i + 1];
695 state->cursor = next;
702 if (typing_delay || transmit_delay)
704 redraw_cells (state, False);
705 XSync (state->dpy, False);
706 screenhack_handle_events (state->dpy);
710 if (typing_delay && 0 == random() % 3)
711 usleep (0xFFFFFF & ((random() % 250000) + 1));
720 roll_state (m_state *state)
727 state->mode = TRACE1;
732 state->mode = TRACE2;
739 for (i = 0; i < strlen(state->tracing); i++)
740 if (state->tracing[i] < 0) any = True;
744 XSync (state->dpy, False);
746 state->mode = MATRIX;
747 state->glyph_map = matrix_encoding;
748 state->nglyphs = countof(matrix_encoding);
750 free (state->tracing);
753 else if ((random() % 10) == 0)
755 int x = random() % strlen(state->tracing);
756 if (state->tracing[x] < 0)
757 state->tracing[x] = -state->tracing[x];
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 */
770 state->mode = MATRIX;
771 state->glyph_map = matrix_encoding;
772 state->nglyphs = countof(matrix_encoding);
777 if (state->knock_knock_p && (! (random() % 5000)))
779 state->mode = KNOCK0;
784 case DNA: case BINARY: case HEX:
796 int blink_delay = 333000;
798 m_cell *cursor = state->cursor;
799 while (tot_delay < delay * 1000000)
803 usleep (blink_delay * 2);
804 tot_delay += (2 * blink_delay);
805 state->cursor = NULL;
809 usleep (blink_delay);
810 tot_delay += blink_delay;
811 state->cursor = cursor;
814 redraw_cells (state, False);
815 XSync (state->dpy, False);
816 screenhack_handle_events (state->dpy);
821 XSync (state->dpy, False);
825 state->cursor = NULL;
830 hack_matrix (m_state *state)
836 case TRACE0: case TRACE1:
837 case KNOCK0: case KNOCK1: case KNOCK2: case KNOCK3:
838 case KNOCK4: case KNOCK5: case KNOCK6: case KNOCK7:
841 case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
847 /* Glow some characters. */
848 if (!state->insert_bottom_p)
850 int i = random() % (state->grid_width / 2);
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)
858 cell->glow = random() % 10;
864 /* Change some of the feeders. */
865 for (x = 0; x < state->grid_width; x++)
867 m_feeder *f = &state->feeders[x];
868 Bool bottom_feeder_p;
870 if (f->remaining > 0) /* never change if pipe isn't empty */
873 if ((random() % densitizer(state)) != 0) /* then change N% of the time */
876 f->remaining = 3 + (random() % state->grid_height);
877 f->throttle = ((random() % 5) + (random() % 5));
879 if ((random() % 4) != 0)
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);
887 bottom_feeder_p = state->insert_bottom_p;
890 f->y = random() % (state->grid_height / 2);
895 if (!state->mode == TRACE2 &&
897 init_spinners (state);
902 draw_matrix (m_state *state)
906 redraw_cells (state, True);
912 static int ndens = 0;
913 static int tdens = 0;
919 ((double) (state->grid_width * state->grid_height))));
922 printf ("density: %d%% (%d%%)\n", dens, (tdens / ndens));
931 char *progclass = "XMatrix";
933 char *defaults [] = {
934 ".background: black",
935 ".foreground: #00AA00",
940 "*tracePhone: (212) 555-0690",
943 "*knockKnock: False",
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" },
966 screenhack (Display *dpy, Window window)
968 m_state *state = init_matrix (dpy, window);
969 int delay = get_integer_resource ("delay", "Integer");
974 screenhack_handle_events (dpy);
975 if (delay) usleep (delay);