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 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)
147 #if defined(HAVE_GDK_PIXBUF) || defined(HAVE_XPM)
148 if (!get_boolean_resource ("mono", "Boolean") &&
149 state->xgwa.depth > 1)
152 (which == 0 ? (state->small_p ? matrix0b_xpm : matrix0_xpm) :
153 which == 1 ? (state->small_p ? matrix1b_xpm : matrix1_xpm) :
154 (state->small_p ? matrix2b_xpm : matrix2_xpm));
156 state->images[which] =
157 xpm_data_to_pixmap (state->dpy, state->window, bits,
158 &state->image_width, &state->image_height, 0);
161 #endif /* !HAVE_XPM && !HAVE_GDK_PIXBUF */
163 unsigned long fg, bg;
164 state->image_width = (state->small_p ? matrix0b_width :matrix0_width);
165 state->image_height = (state->small_p ? matrix0b_height:matrix0_height);
166 fg = get_pixel_resource("foreground", "Foreground",
167 state->dpy, state->xgwa.colormap);
168 bg = get_pixel_resource("background", "Background",
169 state->dpy, state->xgwa.colormap);
170 state->images[which] =
171 XCreatePixmapFromBitmapData (state->dpy, state->window, (char *)
172 (which == 0 ? (state->small_p ? matrix0b_bits :matrix0_bits) :
173 which == 1 ? (state->small_p ? matrix1b_bits :matrix1_bits) :
174 (state->small_p ? matrix2b_bits :matrix2_bits)),
175 state->image_width, state->image_height,
176 bg, fg, state->xgwa.depth);
182 load_images (m_state *state)
184 load_images_1 (state, 0);
185 load_images_1 (state, 1);
186 load_images_1 (state, 2);
191 flip_images_1 (m_state *state, int which)
193 XImage *im = XGetImage (state->dpy, state->images[which], 0, 0,
194 state->image_width, state->image_height,
195 ~0L, (state->xgwa.depth > 1 ? ZPixmap : XYPixmap));
197 int ww = state->char_width;
198 unsigned long *row = (unsigned long *) malloc (sizeof(*row) * ww);
200 for (y = 0; y < state->image_height; y++)
202 for (x = 0; x < CHAR_COLS; x++)
204 for (xx = 0; xx < ww; xx++)
205 row[xx] = XGetPixel (im, (x * ww) + xx, y);
206 for (xx = 0; xx < ww; xx++)
207 XPutPixel (im, (x * ww) + xx, y, row[ww - xx - 1]);
211 XPutImage (state->dpy, state->images[which], state->draw_gc, im, 0, 0, 0, 0,
212 state->image_width, state->image_height);
218 flip_images (m_state *state)
220 flip_images_1 (state, 0);
221 flip_images_1 (state, 1);
222 flip_images_1 (state, 2);
227 init_spinners (m_state *state)
229 int i = state->nspinners;
233 for (y = 0; y < state->grid_height; y++)
234 for (x = 0; x < state->grid_width; x++)
236 cell = &state->cells[state->grid_width * y + x];
242 x = random() % state->grid_width;
243 y = random() % state->grid_height;
244 cell = &state->cells[state->grid_width * y + x];
251 init_trace (m_state *state)
253 char *s = get_string_resource ("tracePhone", "TracePhone");
259 state->tracing = (char *) malloc (strlen (s) + 1);
262 for (s2 = s; *s2; s2++)
263 if (*s2 >= '0' && *s2 <= '9')
267 if (s3 == (char *) state->tracing)
270 for (i = 0; i < strlen(state->tracing); i++)
271 state->tracing[i] = -state->tracing[i];
273 state->glyph_map = decimal_encoding;
274 state->nglyphs = countof(decimal_encoding);
279 fprintf (stderr, "%s: bad phone number: \"%s\".\n",
280 progname, s ? s : "(null)");
283 if (state->tracing) free (state->tracing);
285 state->mode = MATRIX;
290 init_matrix (Display *dpy, Window window)
294 m_state *state = (m_state *) calloc (sizeof(*state), 1);
297 state->window = window;
299 XGetWindowAttributes (dpy, window, &state->xgwa);
302 const char *s = get_string_resource ("small", "Boolean");
304 state->small_p = get_boolean_resource ("small", "Boolean");
306 state->small_p = (state->xgwa.width < 300);
311 gcv.foreground = get_pixel_resource("foreground", "Foreground",
312 state->dpy, state->xgwa.colormap);
313 gcv.background = get_pixel_resource("background", "Background",
314 state->dpy, state->xgwa.colormap);
315 state->draw_gc = XCreateGC (state->dpy, state->window,
316 GCForeground|GCBackground, &gcv);
317 gcv.foreground = gcv.background;
318 state->erase_gc = XCreateGC (state->dpy, state->window,
319 GCForeground|GCBackground, &gcv);
321 state->char_width = state->image_width / CHAR_COLS;
322 state->char_height = state->image_height / CHAR_ROWS;
324 state->grid_width = state->xgwa.width / state->char_width;
325 state->grid_height = state->xgwa.height / state->char_height;
327 state->grid_height++;
329 state->glyph_map = matrix_encoding;
330 state->nglyphs = countof(matrix_encoding);
332 state->cells = (m_cell *)
333 calloc (sizeof(m_cell), state->grid_width * state->grid_height);
334 state->feeders = (m_feeder *) calloc (sizeof(m_feeder), state->grid_width);
336 state->density = get_integer_resource ("density", "Integer");
338 insert = get_string_resource("insert", "Insert");
339 if (insert && !strcmp(insert, "top"))
341 state->insert_top_p = True;
342 state->insert_bottom_p = False;
344 else if (insert && !strcmp(insert, "bottom"))
346 state->insert_top_p = False;
347 state->insert_bottom_p = True;
349 else if (insert && !strcmp(insert, "both"))
351 state->insert_top_p = True;
352 state->insert_bottom_p = True;
356 if (insert && *insert)
358 "%s: `insert' must be `top', `bottom', or `both', not `%s'\n",
360 state->insert_top_p = False;
361 state->insert_bottom_p = True;
364 state->nspinners = get_integer_resource ("spinners", "Integer");
369 mode = get_string_resource ("mode", "Mode");
370 if (mode && !strcasecmp(mode, "trace"))
371 state->mode = TRACE0;
372 else if (mode && !strcasecmp(mode, "dna"))
374 else if (mode && !strcasecmp(mode, "binary"))
375 state->mode = BINARY;
376 else if (mode && (!strcasecmp(mode, "hex") ||
377 !strcasecmp(mode, "hexadecimal")))
379 else if (!mode || !*mode || !strcasecmp(mode, "matrix"))
380 state->mode = MATRIX;
384 "%s: `mode' must be matrix, trace, dna, binary, or hex: not `%s'\n",
386 state->mode = MATRIX;
389 if (state->mode == DNA)
391 state->glyph_map = dna_encoding;
392 state->nglyphs = countof(dna_encoding);
394 else if (state->mode == BINARY)
396 state->glyph_map = binary_encoding;
397 state->nglyphs = countof(binary_encoding);
399 else if (state->mode == HEX)
401 state->glyph_map = hex_encoding;
402 state->nglyphs = countof(hex_encoding);
404 else if (state->mode == HEX)
406 state->glyph_map = hex_encoding;
407 state->nglyphs = countof(hex_encoding);
409 else if (state->mode == TRACE0)
414 init_spinners (state);
422 insert_glyph (m_state *state, int glyph, int x, int y)
424 Bool bottom_feeder_p = (y >= 0);
427 if (y >= state->grid_height)
432 to = &state->cells[state->grid_width * y + x];
436 for (y = state->grid_height-1; y > 0; y--)
438 from = &state->cells[state->grid_width * (y-1) + x];
439 to = &state->cells[state->grid_width * y + x];
440 to->glyph = from->glyph;
441 to->glow = from->glow;
444 to = &state->cells[x];
452 else if (bottom_feeder_p)
453 to->glow = 1 + (random() % 2);
460 feed_matrix (m_state *state)
466 case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
472 /* Update according to current feeders. */
473 for (x = 0; x < state->grid_width; x++)
475 m_feeder *f = &state->feeders[x];
477 if (f->throttle) /* this is a delay tick, synced to frame. */
481 else if (f->remaining > 0) /* how many items are in the pipe */
483 int g = state->glyph_map[(random() % state->nglyphs)] + 1;
484 insert_glyph (state, g, x, f->y);
486 if (f->y >= 0) /* bottom_feeder_p */
489 else /* if pipe is empty, insert spaces */
491 insert_glyph (state, 0, x, f->y);
492 if (f->y >= 0) /* bottom_feeder_p */
496 if ((random() % 10) == 0) /* randomly change throttle speed */
498 f->throttle = ((random() % 5) + (random() % 5));
504 densitizer (m_state *state)
506 /* Horrid kludge that converts percentages (density of screen coverage)
507 to the parameter that actually controls this. I got this mapping
508 empirically, on a 1024x768 screen. Sue me. */
509 if (state->density < 10) return 85;
510 else if (state->density < 15) return 60;
511 else if (state->density < 20) return 45;
512 else if (state->density < 25) return 25;
513 else if (state->density < 30) return 20;
514 else if (state->density < 35) return 15;
515 else if (state->density < 45) return 10;
516 else if (state->density < 50) return 8;
517 else if (state->density < 55) return 7;
518 else if (state->density < 65) return 5;
519 else if (state->density < 80) return 3;
520 else if (state->density < 90) return 2;
526 hack_text (m_state *state)
533 case TRACE0: s = "Call trans opt: received.\n"
534 "2-19-98 13:24:18 REC:Log>_"; break;
535 case TRACE1: s = "Trace program: running_"; break;
537 case KNOCK0: s = "Wake up, Neo..."; break;
538 case KNOCK1: s = ""; break;
539 case KNOCK2: s = "The Matrix has you..."; break;
540 case KNOCK3: s = ""; break;
541 case KNOCK4: s = "Follow the white rabbit..."; break;
542 case KNOCK5: s = ""; break;
543 case KNOCK6: s = "Knock knock, Neo."; break;
544 case KNOCK7: s = ""; break;
546 default: abort(); break;
549 for (i = 0; i < state->grid_height * state->grid_width; i++)
551 m_cell *cell = &state->cells[i];
552 cell->changed = (cell->glyph != 0);
556 if (state->mode == TRACE0 || state->mode == TRACE1)
561 x = ((int)state->grid_width - (int)strlen(s)) / 2;
562 y = (state->grid_height / 2) - 1;
565 i = (y * state->grid_width) + x;
572 i = ((i / state->grid_width) + 1) * state->grid_width;
577 m_cell *cell = &state->cells[i];
578 if (x < state->grid_width-1)
580 cell->glyph = char_map[(unsigned char) *s] + 1;
581 if (*s == ' ' || *s == '\t') cell->glyph = 0;
592 roll_state (m_state *state)
599 state->mode = TRACE1;
604 state->mode = TRACE2;
611 for (i = 0; i < strlen(state->tracing); i++)
612 if (state->tracing[i] < 0) any = True;
616 XSync (state->dpy, False);
618 state->mode = MATRIX;
619 state->glyph_map = matrix_encoding;
620 state->nglyphs = countof(matrix_encoding);
622 free (state->tracing);
625 else if ((random() % 10) == 0)
627 int x = random() % strlen(state->tracing);
628 if (state->tracing[x] < 0)
629 state->tracing[x] = -state->tracing[x];
634 case KNOCK0: delay = 1; state->mode++; break; /* wake */
635 case KNOCK1: delay = 4; state->mode++; break;
636 case KNOCK2: delay = 2; state->mode++; break; /* has */
637 case KNOCK3: delay = 4; state->mode++; break;
638 case KNOCK4: delay = 2; state->mode++; break; /* rabbit */
639 case KNOCK5: delay = 4; state->mode++; break;
640 case KNOCK6: delay = 4; state->mode++; break; /* knock */
642 state->mode = MATRIX;
643 state->glyph_map = matrix_encoding;
644 state->nglyphs = countof(matrix_encoding);
649 if (! (random() % 5000))
651 state->mode = KNOCK0;
656 case DNA: case BINARY: case HEX:
666 XSync (state->dpy, False);
673 hack_matrix (m_state *state)
679 case TRACE0: case TRACE1:
680 case KNOCK0: case KNOCK1: case KNOCK2: case KNOCK3:
681 case KNOCK4: case KNOCK5: case KNOCK6: case KNOCK7:
684 case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
690 /* Glow some characters. */
691 if (!state->insert_bottom_p)
693 int i = random() % (state->grid_width / 2);
696 int x = random() % state->grid_width;
697 int y = random() % state->grid_height;
698 m_cell *cell = &state->cells[state->grid_width * y + x];
699 if (cell->glyph && cell->glow == 0)
701 cell->glow = random() % 10;
707 /* Change some of the feeders. */
708 for (x = 0; x < state->grid_width; x++)
710 m_feeder *f = &state->feeders[x];
711 Bool bottom_feeder_p;
713 if (f->remaining > 0) /* never change if pipe isn't empty */
716 if ((random() % densitizer(state)) != 0) /* then change N% of the time */
719 f->remaining = 3 + (random() % state->grid_height);
720 f->throttle = ((random() % 5) + (random() % 5));
722 if ((random() % 4) != 0)
725 if (state->mode == TRACE2)
726 bottom_feeder_p = True;
727 if (state->insert_top_p && state->insert_bottom_p)
728 bottom_feeder_p = (random() & 1);
730 bottom_feeder_p = state->insert_bottom_p;
733 f->y = random() % (state->grid_height / 2);
738 if (!state->mode == TRACE2 &&
740 init_spinners (state);
745 draw_matrix (m_state *state)
753 for (y = 0; y < state->grid_height; y++)
754 for (x = 0; x < state->grid_width; x++)
756 m_cell *cell = &state->cells[state->grid_width * y + x];
761 if (state->mode == TRACE2)
763 int xx = x % strlen(state->tracing);
764 Bool dead_p = state->tracing[xx] > 0;
766 if (y == 0 && x == xx)
767 cell->glyph = (dead_p
768 ? state->glyph_map[state->tracing[xx]-'0'] + 1
773 cell->glyph = (dead_p ? 0 :
774 (state->glyph_map[(random()%state->nglyphs)]
783 if (cell->glyph == 0)
784 XFillRectangle (state->dpy, state->window, state->erase_gc,
785 x * state->char_width,
786 y * state->char_height,
791 int cx = (cell->glyph - 1) % CHAR_COLS;
792 int cy = (cell->glyph - 1) / CHAR_COLS;
793 int map = ((cell->glow > 0 || cell->spinner) ? GLOW_MAP :
794 (cell->glow == 0) ? PLAIN_MAP :
797 XCopyArea (state->dpy, state->images[map],
798 state->window, state->draw_gc,
799 cx * state->char_width,
800 cy * state->char_height,
803 x * state->char_width,
804 y * state->char_height);
814 else if (cell->glow < 0)
824 cell->glyph = (state->glyph_map[(random()%state->nglyphs)] + 1);
834 static int ndens = 0;
835 static int tdens = 0;
841 ((double) (state->grid_width * state->grid_height))));
844 printf ("density: %d%% (%d%%)\n", dens, (tdens / ndens));
853 char *progclass = "XMatrix";
855 char *defaults [] = {
856 ".background: black",
857 ".foreground: green",
862 "*tracePhone: (212) 555-0690",
868 XrmOptionDescRec options [] = {
869 { "-small", ".small", XrmoptionNoArg, "True" },
870 { "-large", ".small", XrmoptionNoArg, "False" },
871 { "-delay", ".delay", XrmoptionSepArg, 0 },
872 { "-top", ".insert", XrmoptionNoArg, "top" },
873 { "-bottom", ".insert", XrmoptionNoArg, "bottom" },
874 { "-both", ".insert", XrmoptionNoArg, "both" },
875 { "-density", ".density", XrmoptionSepArg, 0 },
876 { "-trace", ".mode", XrmoptionNoArg, "trace" },
877 { "-phone", ".tracePhone", XrmoptionSepArg, 0 },
878 { "-dna", ".mode", XrmoptionNoArg, "DNA" },
879 { "-binary", ".mode", XrmoptionNoArg, "binary" },
880 { "-hexadecimal", ".mode", XrmoptionNoArg, "hexadecimal"},
886 screenhack (Display *dpy, Window window)
888 m_state *state = init_matrix (dpy, window);
889 int delay = get_integer_resource ("delay", "Integer");
894 screenhack_handle_events (dpy);
895 if (delay) usleep (delay);