1 /* xscreensaver, Copyright (c) 1999 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 * ==========================================================
42 * One thing I would like to add (to "-trace" mode) is an intro like at the
43 * beginning of the movie, where it printed
45 * Call trans opt: received. 2-19-98 13:24:18 REC:Log>_
49 * Trace program: running_
53 * I was also thinking of sometimes making the screen go blank and say
56 * However, the problem with both of these ideas is, I made the number images
57 * by tooling around in GIMP until I got something that looked good (blurring
58 * and unblurring and enlarging and shrinking and blurring some more...) and I
59 * couldn't reproduce it if my life depended on it. And to add anything other
60 * than roman digits/katakana, I'd need to matrixify a whole font...
63 #include "screenhack.h"
65 #include <X11/Xutil.h>
69 # include "images/matrix.xpm"
70 # include "images/matrix2.xpm"
73 #include "images/matrix.xbm"
74 #include "images/matrix2.xbm"
83 unsigned int glyph : 8;
85 unsigned int changed : 1;
86 unsigned int spinner : 1;
98 XWindowAttributes xgwa;
100 int grid_width, grid_height;
101 int char_width, char_height;
106 Bool insert_top_p, insert_bottom_p;
108 signed char *tracing;
112 int image_width, image_height;
119 load_images (m_state *state)
122 if (!get_boolean_resource ("mono", "Boolean") &&
123 state->xgwa.depth > 1)
125 XpmAttributes xpmattrs;
127 xpmattrs.valuemask = 0;
130 xpmattrs.valuemask |= XpmCloseness;
131 xpmattrs.closeness = 40000;
134 xpmattrs.valuemask |= XpmVisual;
135 xpmattrs.visual = state->xgwa.visual;
138 xpmattrs.valuemask |= XpmDepth;
139 xpmattrs.depth = state->xgwa.depth;
142 xpmattrs.valuemask |= XpmColormap;
143 xpmattrs.colormap = state->xgwa.colormap;
146 result = XpmCreatePixmapFromData (state->dpy, state->window,
150 &state->images, 0 /* mask */,
152 if (!state->images || (result != XpmSuccess && result != XpmColorError))
155 state->image_width = xpmattrs.width;
156 state->image_height = xpmattrs.height;
157 state->nglyphs = CHAR_ROWS;
160 #endif /* !HAVE_XPM */
162 unsigned long fg, bg;
163 state->image_width = (state->small_p ? matrix2_width : matrix_width);
164 state->image_height = (state->small_p ? matrix2_height : matrix_height);
165 state->nglyphs = CHAR_ROWS;
167 fg = get_pixel_resource("foreground", "Foreground",
168 state->dpy, state->xgwa.colormap);
169 bg = get_pixel_resource("background", "Background",
170 state->dpy, state->xgwa.colormap);
172 XCreatePixmapFromBitmapData (state->dpy, state->window,
174 ? (char *) matrix2_bits
175 : (char *) matrix_bits),
176 state->image_width, state->image_height,
177 bg, fg, state->xgwa.depth);
183 flip_images (m_state *state)
185 XImage *im = XGetImage (state->dpy, state->images, 0, 0,
186 state->image_width, state->image_height,
187 ~0L, (state->xgwa.depth > 1 ? ZPixmap : XYPixmap));
189 int w = state->image_width / CHAR_COLS;
190 unsigned long *row = (unsigned long *) malloc (sizeof(*row) * w);
192 for (y = 0; y < state->image_height; y++)
193 for (i = 0; i < CHAR_COLS; i++)
195 for (x = 0; x < w; x++)
196 row[x] = XGetPixel (im, (i * w) + x, y);
197 for (x = 0; x < w; x++)
198 XPutPixel (im, (i * w) + x, y, row[w - x - 1]);
201 XPutImage (state->dpy, state->images, state->draw_gc, im, 0, 0, 0, 0,
202 state->image_width, state->image_height);
209 init_spinners (m_state *state)
211 int i = state->nspinners;
215 for (y = 0; y < state->grid_height; y++)
216 for (x = 0; x < state->grid_width; x++)
218 cell = &state->cells[state->grid_width * y + x];
224 x = random() % state->grid_width;
225 y = random() % state->grid_height;
226 cell = &state->cells[state->grid_width * y + x];
233 init_trace (m_state *state)
235 char *s = get_string_resource ("tracePhone", "TracePhone");
241 state->tracing = (char *) malloc (strlen (s) + 1);
244 for (s2 = s; *s2; s2++)
245 if (*s2 >= '0' && *s2 <= '9')
249 if (s3 == (char *) state->tracing)
252 for (i = 0; i < strlen(state->tracing); i++)
253 state->tracing[i] = -state->tracing[i];
260 fprintf (stderr, "%s: bad phone number: \"%s\".\n",
261 progname, s ? s : "(null)");
264 if (state->tracing) free (state->tracing);
266 state->trace_p = False;
271 init_matrix (Display *dpy, Window window)
275 m_state *state = (m_state *) calloc (sizeof(*state), 1);
277 state->window = window;
279 XGetWindowAttributes (dpy, window, &state->xgwa);
281 state->small_p = get_boolean_resource ("small", "Boolean");
284 gcv.foreground = get_pixel_resource("foreground", "Foreground",
285 state->dpy, state->xgwa.colormap);
286 gcv.background = get_pixel_resource("background", "Background",
287 state->dpy, state->xgwa.colormap);
288 state->draw_gc = XCreateGC (state->dpy, state->window,
289 GCForeground|GCBackground, &gcv);
290 gcv.foreground = gcv.background;
291 state->erase_gc = XCreateGC (state->dpy, state->window,
292 GCForeground|GCBackground, &gcv);
294 state->char_width = state->image_width / CHAR_COLS;
295 state->char_height = state->image_height / CHAR_ROWS;
297 state->grid_width = state->xgwa.width / state->char_width;
298 state->grid_height = state->xgwa.height / state->char_height;
300 state->grid_height++;
302 state->cells = (m_cell *)
303 calloc (sizeof(m_cell), state->grid_width * state->grid_height);
304 state->feeders = (m_feeder *) calloc (sizeof(m_feeder), state->grid_width);
306 state->density = get_integer_resource ("density", "Integer");
308 insert = get_string_resource("insert", "Insert");
309 if (insert && !strcmp(insert, "top"))
311 state->insert_top_p = True;
312 state->insert_bottom_p = False;
314 else if (insert && !strcmp(insert, "bottom"))
316 state->insert_top_p = False;
317 state->insert_bottom_p = True;
319 else if (insert && !strcmp(insert, "both"))
321 state->insert_top_p = True;
322 state->insert_bottom_p = True;
326 if (insert && *insert)
328 "%s: `insert' must be `top', `bottom', or `both', not `%s'\n",
330 state->insert_top_p = False;
331 state->insert_bottom_p = True;
334 state->nspinners = get_integer_resource ("spinners", "Integer");
339 state->trace_p = get_boolean_resource ("trace", "Trace");
343 init_spinners (state);
350 insert_glyph (m_state *state, int glyph, int x, int y)
352 Bool bottom_feeder_p = (y >= 0);
355 if (y >= state->grid_height)
360 to = &state->cells[state->grid_width * y + x];
364 for (y = state->grid_height-1; y > 0; y--)
366 from = &state->cells[state->grid_width * (y-1) + x];
367 to = &state->cells[state->grid_width * y + x];
368 to->glyph = from->glyph;
369 to->glow = from->glow;
372 to = &state->cells[x];
380 else if (bottom_feeder_p)
381 to->glow = 1 + (random() % 2);
388 feed_matrix (m_state *state)
392 /* Update according to current feeders. */
393 for (x = 0; x < state->grid_width; x++)
395 m_feeder *f = &state->feeders[x];
397 if (f->throttle) /* this is a delay tick, synced to frame. */
401 else if (f->remaining > 0) /* how many items are in the pipe */
403 int g = (random() % state->nglyphs) + 1;
404 insert_glyph (state, g, x, f->y);
406 if (f->y >= 0) /* bottom_feeder_p */
409 else /* if pipe is empty, insert spaces */
411 insert_glyph (state, 0, x, f->y);
412 if (f->y >= 0) /* bottom_feeder_p */
416 if ((random() % 10) == 0) /* randomly change throttle speed */
418 f->throttle = ((random() % 5) + (random() % 5));
424 densitizer (m_state *state)
426 /* Horrid kludge that converts percentages (density of screen coverage)
427 to the parameter that actually controls this. I got this mapping
428 empirically, on a 1024x768 screen. Sue me. */
429 if (state->density < 10) return 85;
430 else if (state->density < 15) return 60;
431 else if (state->density < 20) return 45;
432 else if (state->density < 25) return 25;
433 else if (state->density < 30) return 20;
434 else if (state->density < 35) return 15;
435 else if (state->density < 45) return 10;
436 else if (state->density < 50) return 8;
437 else if (state->density < 55) return 7;
438 else if (state->density < 65) return 5;
439 else if (state->density < 80) return 3;
440 else if (state->density < 90) return 2;
446 hack_matrix (m_state *state)
450 /* Glow some characters. */
451 if (!state->insert_bottom_p)
453 int i = random() % (state->grid_width / 2);
456 int x = random() % state->grid_width;
457 int y = random() % state->grid_height;
458 m_cell *cell = &state->cells[state->grid_width * y + x];
459 if (cell->glyph && cell->glow == 0)
461 cell->glow = random() % 10;
467 /* Change some of the feeders. */
468 for (x = 0; x < state->grid_width; x++)
470 m_feeder *f = &state->feeders[x];
471 Bool bottom_feeder_p;
473 if (f->remaining > 0) /* never change if pipe isn't empty */
476 if ((random() % densitizer(state)) != 0) /* then change N% of the time */
479 f->remaining = 3 + (random() % state->grid_height);
480 f->throttle = ((random() % 5) + (random() % 5));
482 if ((random() % 4) != 0)
486 bottom_feeder_p = True;
487 if (state->insert_top_p && state->insert_bottom_p)
488 bottom_feeder_p = (random() & 1);
490 bottom_feeder_p = state->insert_bottom_p;
493 f->y = random() % (state->grid_height / 2);
498 if (!state->trace_p &&
500 init_spinners (state);
505 draw_matrix (m_state *state)
513 for (y = 0; y < state->grid_height; y++)
514 for (x = 0; x < state->grid_width; x++)
516 m_cell *cell = &state->cells[state->grid_width * y + x];
523 int xx = x % strlen(state->tracing);
524 Bool dead_p = state->tracing[xx] > 0;
526 if (y == 0 && x == xx)
527 cell->glyph = (dead_p ? (state->tracing[xx]-'0'+1) : 0);
531 cell->glyph = (dead_p ? 0 : (random() % state->nglyphs) + 1);
539 if (cell->glyph == 0)
540 XFillRectangle (state->dpy, state->window, state->erase_gc,
541 x * state->char_width,
542 y * state->char_height,
546 XCopyArea (state->dpy, state->images, state->window, state->draw_gc,
547 ((cell->glow > 0 || cell->spinner)
548 ? (state->char_width * GLOW_COL)
550 ? (state->char_width * PLAIN_COL)
551 : (state->char_width * FADE_COL))),
552 (cell->glyph - 1) * state->char_height,
553 state->char_width, state->char_height,
554 x * state->char_width,
555 y * state->char_height);
564 else if (cell->glow < 0)
574 cell->glyph = random() % CHAR_ROWS;
583 for (i = 0; i < strlen(state->tracing); i++)
584 if (state->tracing[i] < 0) any = True;
588 XSync (state->dpy, False);
590 state->trace_p = False;
591 state->nglyphs = CHAR_ROWS;
593 free (state->tracing);
596 else if ((random() % 10) == 0)
598 int x = random() % strlen(state->tracing);
599 if (state->tracing[x] < 0)
600 state->tracing[x] = -state->tracing[x];
607 static int ndens = 0;
608 static int tdens = 0;
614 ((double) (state->grid_width * state->grid_height))));
617 printf ("density: %d%% (%d%%)\n", dens, (tdens / ndens));
626 char *progclass = "XMatrix";
628 char *defaults [] = {
629 ".background: black",
630 ".foreground: green",
635 "*tracePhone: (212) 555-0690",
641 XrmOptionDescRec options [] = {
642 { "-small", ".small", XrmoptionNoArg, "True" },
643 { "-large", ".small", XrmoptionNoArg, "False" },
644 { "-delay", ".delay", XrmoptionSepArg, 0 },
645 { "-top", ".insert", XrmoptionNoArg, "top" },
646 { "-bottom", ".insert", XrmoptionNoArg, "bottom" },
647 { "-both", ".insert", XrmoptionNoArg, "both" },
648 { "-density", ".density", XrmoptionSepArg, 0 },
649 { "-trace", ".trace", XrmoptionNoArg, "True" },
650 { "-phone", ".tracePhone", XrmoptionSepArg, 0 },
656 screenhack (Display *dpy, Window window)
658 m_state *state = init_matrix (dpy, window);
659 int delay = get_integer_resource ("delay", "Integer");
664 screenhack_handle_events (dpy);
665 if (delay) usleep (delay);