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;
133 Bool insert_top_p, insert_bottom_p;
135 signed char *tracing;
138 Pixmap images[CHAR_MAPS];
139 int image_width, image_height;
148 load_images_1 (m_state *state, int which)
150 #if defined(HAVE_GDK_PIXBUF) || defined(HAVE_XPM)
151 if (!get_boolean_resource ("mono", "Boolean") &&
152 state->xgwa.depth > 1)
155 (which == 0 ? (state->small_p ? matrix0b_xpm : matrix0_xpm) :
156 which == 1 ? (state->small_p ? matrix1b_xpm : matrix1_xpm) :
157 (state->small_p ? matrix2b_xpm : matrix2_xpm));
159 state->images[which] =
160 xpm_data_to_pixmap (state->dpy, state->window, bits,
161 &state->image_width, &state->image_height, 0);
164 #endif /* !HAVE_XPM && !HAVE_GDK_PIXBUF */
166 unsigned long fg, bg;
167 state->image_width = (state->small_p ? matrix0b_width :matrix0_width);
168 state->image_height = (state->small_p ? matrix0b_height:matrix0_height);
169 fg = get_pixel_resource("foreground", "Foreground",
170 state->dpy, state->xgwa.colormap);
171 bg = get_pixel_resource("background", "Background",
172 state->dpy, state->xgwa.colormap);
173 state->images[which] =
174 XCreatePixmapFromBitmapData (state->dpy, state->window, (char *)
175 (which == 0 ? (state->small_p ? matrix0b_bits :matrix0_bits) :
176 which == 1 ? (state->small_p ? matrix1b_bits :matrix1_bits) :
177 (state->small_p ? matrix2b_bits :matrix2_bits)),
178 state->image_width, state->image_height,
179 bg, fg, state->xgwa.depth);
185 load_images (m_state *state)
187 load_images_1 (state, 0);
188 load_images_1 (state, 1);
189 load_images_1 (state, 2);
194 flip_images_1 (m_state *state, int which)
196 XImage *im = XGetImage (state->dpy, state->images[which], 0, 0,
197 state->image_width, state->image_height,
198 ~0L, (state->xgwa.depth > 1 ? ZPixmap : XYPixmap));
200 int ww = state->char_width;
201 unsigned long *row = (unsigned long *) malloc (sizeof(*row) * ww);
203 for (y = 0; y < state->image_height; y++)
205 for (x = 0; x < CHAR_COLS; x++)
207 for (xx = 0; xx < ww; xx++)
208 row[xx] = XGetPixel (im, (x * ww) + xx, y);
209 for (xx = 0; xx < ww; xx++)
210 XPutPixel (im, (x * ww) + xx, y, row[ww - xx - 1]);
214 XPutImage (state->dpy, state->images[which], state->draw_gc, im, 0, 0, 0, 0,
215 state->image_width, state->image_height);
221 flip_images (m_state *state)
223 flip_images_1 (state, 0);
224 flip_images_1 (state, 1);
225 flip_images_1 (state, 2);
230 init_spinners (m_state *state)
232 int i = state->nspinners;
236 for (y = 0; y < state->grid_height; y++)
237 for (x = 0; x < state->grid_width; x++)
239 cell = &state->cells[state->grid_width * y + x];
245 x = random() % state->grid_width;
246 y = random() % state->grid_height;
247 cell = &state->cells[state->grid_width * y + x];
254 init_trace (m_state *state)
256 char *s = get_string_resource ("tracePhone", "TracePhone");
262 state->tracing = (char *) malloc (strlen (s) + 1);
265 for (s2 = s; *s2; s2++)
266 if (*s2 >= '0' && *s2 <= '9')
270 if (s3 == (char *) state->tracing)
273 for (i = 0; i < strlen(state->tracing); i++)
274 state->tracing[i] = -state->tracing[i];
276 state->glyph_map = decimal_encoding;
277 state->nglyphs = countof(decimal_encoding);
282 fprintf (stderr, "%s: bad phone number: \"%s\".\n",
283 progname, s ? s : "(null)");
286 if (state->tracing) free (state->tracing);
288 state->mode = MATRIX;
293 init_matrix (Display *dpy, Window window)
297 m_state *state = (m_state *) calloc (sizeof(*state), 1);
300 state->window = window;
302 XGetWindowAttributes (dpy, window, &state->xgwa);
305 const char *s = get_string_resource ("small", "Boolean");
307 state->small_p = get_boolean_resource ("small", "Boolean");
309 state->small_p = (state->xgwa.width < 300);
314 gcv.foreground = get_pixel_resource("foreground", "Foreground",
315 state->dpy, state->xgwa.colormap);
316 gcv.background = get_pixel_resource("background", "Background",
317 state->dpy, state->xgwa.colormap);
318 state->draw_gc = XCreateGC (state->dpy, state->window,
319 GCForeground|GCBackground, &gcv);
320 gcv.foreground = gcv.background;
321 state->erase_gc = XCreateGC (state->dpy, state->window,
322 GCForeground|GCBackground, &gcv);
324 state->char_width = state->image_width / CHAR_COLS;
325 state->char_height = state->image_height / CHAR_ROWS;
327 state->grid_width = state->xgwa.width / state->char_width;
328 state->grid_height = state->xgwa.height / state->char_height;
330 state->grid_height++;
332 state->glyph_map = matrix_encoding;
333 state->nglyphs = countof(matrix_encoding);
335 state->cells = (m_cell *)
336 calloc (sizeof(m_cell), state->grid_width * state->grid_height);
337 state->cursor = NULL;
338 state->feeders = (m_feeder *) calloc (sizeof(m_feeder), state->grid_width);
340 state->density = get_integer_resource ("density", "Integer");
342 insert = get_string_resource("insert", "Insert");
343 if (insert && !strcmp(insert, "top"))
345 state->insert_top_p = True;
346 state->insert_bottom_p = False;
348 else if (insert && !strcmp(insert, "bottom"))
350 state->insert_top_p = False;
351 state->insert_bottom_p = True;
353 else if (insert && !strcmp(insert, "both"))
355 state->insert_top_p = True;
356 state->insert_bottom_p = True;
360 if (insert && *insert)
362 "%s: `insert' must be `top', `bottom', or `both', not `%s'\n",
364 state->insert_top_p = False;
365 state->insert_bottom_p = True;
368 state->nspinners = get_integer_resource ("spinners", "Integer");
373 mode = get_string_resource ("mode", "Mode");
374 if (mode && !strcasecmp(mode, "trace"))
375 state->mode = TRACE0;
376 else if (mode && !strcasecmp(mode, "dna"))
378 else if (mode && !strcasecmp(mode, "binary"))
379 state->mode = BINARY;
380 else if (mode && (!strcasecmp(mode, "hex") ||
381 !strcasecmp(mode, "hexadecimal")))
383 else if (!mode || !*mode || !strcasecmp(mode, "matrix"))
384 state->mode = MATRIX;
388 "%s: `mode' must be matrix, trace, dna, binary, or hex: not `%s'\n",
390 state->mode = MATRIX;
393 if (state->mode == DNA)
395 state->glyph_map = dna_encoding;
396 state->nglyphs = countof(dna_encoding);
398 else if (state->mode == BINARY)
400 state->glyph_map = binary_encoding;
401 state->nglyphs = countof(binary_encoding);
403 else if (state->mode == HEX)
405 state->glyph_map = hex_encoding;
406 state->nglyphs = countof(hex_encoding);
408 else if (state->mode == HEX)
410 state->glyph_map = hex_encoding;
411 state->nglyphs = countof(hex_encoding);
413 else if (state->mode == TRACE0)
418 init_spinners (state);
426 insert_glyph (m_state *state, int glyph, int x, int y)
428 Bool bottom_feeder_p = (y >= 0);
431 if (y >= state->grid_height)
436 to = &state->cells[state->grid_width * y + x];
440 for (y = state->grid_height-1; y > 0; y--)
442 from = &state->cells[state->grid_width * (y-1) + x];
443 to = &state->cells[state->grid_width * y + x];
444 to->glyph = from->glyph;
445 to->glow = from->glow;
448 to = &state->cells[x];
456 else if (bottom_feeder_p)
457 to->glow = 1 + (random() % 2);
464 feed_matrix (m_state *state)
470 case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
476 /* Update according to current feeders. */
477 for (x = 0; x < state->grid_width; x++)
479 m_feeder *f = &state->feeders[x];
481 if (f->throttle) /* this is a delay tick, synced to frame. */
485 else if (f->remaining > 0) /* how many items are in the pipe */
487 int g = state->glyph_map[(random() % state->nglyphs)] + 1;
488 insert_glyph (state, g, x, f->y);
490 if (f->y >= 0) /* bottom_feeder_p */
493 else /* if pipe is empty, insert spaces */
495 insert_glyph (state, 0, x, f->y);
496 if (f->y >= 0) /* bottom_feeder_p */
500 if ((random() % 10) == 0) /* randomly change throttle speed */
502 f->throttle = ((random() % 5) + (random() % 5));
509 redraw_cells (m_state *state, Bool active)
514 for (y = 0; y < state->grid_height; y++)
515 for (x = 0; x < state->grid_width; x++)
517 m_cell *cell = &state->cells[state->grid_width * y + x];
522 if (state->mode == TRACE2 && active)
524 int xx = x % strlen(state->tracing);
525 Bool dead_p = state->tracing[xx] > 0;
527 if (y == 0 && x == xx)
528 cell->glyph = (dead_p
529 ? state->glyph_map[state->tracing[xx]-'0'] + 1
534 cell->glyph = (dead_p ? 0 :
535 (state->glyph_map[(random()%state->nglyphs)]
544 if (cell->glyph == 0 && cell != state->cursor)
545 XFillRectangle (state->dpy, state->window, state->erase_gc,
546 x * state->char_width,
547 y * state->char_height,
552 int g = (cell == state->cursor ? CURSOR_GLYPH : cell->glyph);
553 int cx = (g - 1) % CHAR_COLS;
554 int cy = (g - 1) / CHAR_COLS;
555 int map = ((cell->glow > 0 || cell->spinner) ? GLOW_MAP :
556 (cell->glow < 0) ? FADE_MAP :
559 XCopyArea (state->dpy, state->images[map],
560 state->window, state->draw_gc,
561 cx * state->char_width,
562 cy * state->char_height,
565 x * state->char_width,
566 y * state->char_height);
576 else if (cell->glow < 0)
584 if (cell->spinner && active)
586 cell->glyph = (state->glyph_map[(random()%state->nglyphs)] + 1);
593 state->cursor->changed = 1;
599 densitizer (m_state *state)
601 /* Horrid kludge that converts percentages (density of screen coverage)
602 to the parameter that actually controls this. I got this mapping
603 empirically, on a 1024x768 screen. Sue me. */
604 if (state->density < 10) return 85;
605 else if (state->density < 15) return 60;
606 else if (state->density < 20) return 45;
607 else if (state->density < 25) return 25;
608 else if (state->density < 30) return 20;
609 else if (state->density < 35) return 15;
610 else if (state->density < 45) return 10;
611 else if (state->density < 50) return 8;
612 else if (state->density < 55) return 7;
613 else if (state->density < 65) return 5;
614 else if (state->density < 80) return 3;
615 else if (state->density < 90) return 2;
621 hack_text (m_state *state)
626 Bool typing_delay = False;
627 Bool transmit_delay = False;
628 Bool visible_cursor = False;
631 case TRACE0: if (state->grid_width >= 52)
632 s = "Call trans opt: received. 2-19-98 13:24:18 REC:Log>";
634 s = "Call trans opt: received.\n2-19-98 13:24:18 REC:Log>";
635 transmit_delay = True;
636 visible_cursor = True;
638 case TRACE1: s = "Trace program: running";
639 transmit_delay = True;
640 visible_cursor = True;
642 case KNOCK0: s = "Wake up, Neo..."; break;
643 case KNOCK1: s = ""; break;
644 case KNOCK2: s = "The Matrix has you..."; typing_delay = True; break;
645 case KNOCK3: s = ""; break;
646 case KNOCK4: s = "Follow the white rabbit."; typing_delay = True; break;
647 case KNOCK5: s = ""; break;
648 case KNOCK6: s = "Knock, knock, Neo."; break;
649 case KNOCK7: s = ""; break;
651 default: abort(); break;
654 for (i = 0; i < state->grid_height * state->grid_width; i++)
656 m_cell *cell = &state->cells[i];
657 cell->changed = (cell->glyph != 0);
661 if (state->mode == TRACE0 || state->mode == TRACE1)
666 x = ((int)state->grid_width - (int)strlen(s)) / 2;
667 y = (state->grid_height / 2) - 1;
670 i = (y * state->grid_width) + x;
677 i = ((i / state->grid_width) + 1) * state->grid_width;
682 m_cell *cell = &state->cells[i];
683 if (x < state->grid_width-1)
685 cell->glyph = char_map[(unsigned char) *s] + 1;
686 if (*s == ' ' || *s == '\t') cell->glyph = 0;
690 m_cell *next = &state->cells[i + 1];
692 state->cursor = next;
699 if (typing_delay || transmit_delay)
701 redraw_cells (state, False);
702 XSync (state->dpy, False);
703 screenhack_handle_events (state->dpy);
707 if (typing_delay && 0 == random() % 3)
708 usleep (0xFFFFFF & ((random() % 250000) + 1));
717 roll_state (m_state *state)
724 state->mode = TRACE1;
729 state->mode = TRACE2;
736 for (i = 0; i < strlen(state->tracing); i++)
737 if (state->tracing[i] < 0) any = True;
741 XSync (state->dpy, False);
743 state->mode = MATRIX;
744 state->glyph_map = matrix_encoding;
745 state->nglyphs = countof(matrix_encoding);
747 free (state->tracing);
750 else if ((random() % 10) == 0)
752 int x = random() % strlen(state->tracing);
753 if (state->tracing[x] < 0)
754 state->tracing[x] = -state->tracing[x];
759 case KNOCK0: delay = 1; state->mode++; break; /* wake */
760 case KNOCK1: delay = 4; state->mode++; break;
761 case KNOCK2: delay = 2; state->mode++; break; /* has */
762 case KNOCK3: delay = 4; state->mode++; break;
763 case KNOCK4: delay = 2; state->mode++; break; /* rabbit */
764 case KNOCK5: delay = 4; state->mode++; break;
765 case KNOCK6: delay = 4; state->mode++; break; /* knock */
767 state->mode = MATRIX;
768 state->glyph_map = matrix_encoding;
769 state->nglyphs = countof(matrix_encoding);
774 if (! (random() % 5000))
776 state->mode = KNOCK0;
781 case DNA: case BINARY: case HEX:
793 int blink_delay = 333000;
795 m_cell *cursor = state->cursor;
796 while (tot_delay < delay * 1000000)
800 usleep (blink_delay * 2);
801 tot_delay += (2 * blink_delay);
802 state->cursor = NULL;
806 usleep (blink_delay);
807 tot_delay += blink_delay;
808 state->cursor = cursor;
811 redraw_cells (state, False);
812 XSync (state->dpy, False);
813 screenhack_handle_events (state->dpy);
818 XSync (state->dpy, False);
822 state->cursor = NULL;
827 hack_matrix (m_state *state)
833 case TRACE0: case TRACE1:
834 case KNOCK0: case KNOCK1: case KNOCK2: case KNOCK3:
835 case KNOCK4: case KNOCK5: case KNOCK6: case KNOCK7:
838 case TRACE2: case MATRIX: case DNA: case BINARY: case HEX:
844 /* Glow some characters. */
845 if (!state->insert_bottom_p)
847 int i = random() % (state->grid_width / 2);
850 int x = random() % state->grid_width;
851 int y = random() % state->grid_height;
852 m_cell *cell = &state->cells[state->grid_width * y + x];
853 if (cell->glyph && cell->glow == 0)
855 cell->glow = random() % 10;
861 /* Change some of the feeders. */
862 for (x = 0; x < state->grid_width; x++)
864 m_feeder *f = &state->feeders[x];
865 Bool bottom_feeder_p;
867 if (f->remaining > 0) /* never change if pipe isn't empty */
870 if ((random() % densitizer(state)) != 0) /* then change N% of the time */
873 f->remaining = 3 + (random() % state->grid_height);
874 f->throttle = ((random() % 5) + (random() % 5));
876 if ((random() % 4) != 0)
879 if (state->mode == TRACE2)
880 bottom_feeder_p = True;
881 if (state->insert_top_p && state->insert_bottom_p)
882 bottom_feeder_p = (random() & 1);
884 bottom_feeder_p = state->insert_bottom_p;
887 f->y = random() % (state->grid_height / 2);
892 if (!state->mode == TRACE2 &&
894 init_spinners (state);
899 draw_matrix (m_state *state)
903 redraw_cells (state, True);
909 static int ndens = 0;
910 static int tdens = 0;
916 ((double) (state->grid_width * state->grid_height))));
919 printf ("density: %d%% (%d%%)\n", dens, (tdens / ndens));
928 char *progclass = "XMatrix";
930 char *defaults [] = {
931 ".background: black",
932 ".foreground: #00AA00",
937 "*tracePhone: (212) 555-0690",
943 XrmOptionDescRec options [] = {
944 { "-small", ".small", XrmoptionNoArg, "True" },
945 { "-large", ".small", XrmoptionNoArg, "False" },
946 { "-delay", ".delay", XrmoptionSepArg, 0 },
947 { "-top", ".insert", XrmoptionNoArg, "top" },
948 { "-bottom", ".insert", XrmoptionNoArg, "bottom" },
949 { "-both", ".insert", XrmoptionNoArg, "both" },
950 { "-density", ".density", XrmoptionSepArg, 0 },
951 { "-trace", ".mode", XrmoptionNoArg, "trace" },
952 { "-phone", ".tracePhone", XrmoptionSepArg, 0 },
953 { "-dna", ".mode", XrmoptionNoArg, "DNA" },
954 { "-binary", ".mode", XrmoptionNoArg, "binary" },
955 { "-hexadecimal", ".mode", XrmoptionNoArg, "hexadecimal"},
961 screenhack (Display *dpy, Window window)
963 m_state *state = init_matrix (dpy, window);
964 int delay = get_integer_resource ("delay", "Integer");
969 screenhack_handle_events (dpy);
970 if (delay) usleep (delay);