1 /* xscreensaver, Copyright (c) 2002, 2004 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 * Memscroller -- scrolls a dump of its own RAM across the screen.
14 #include "screenhack.h"
16 #include <X11/Xutil.h>
18 #ifdef HAVE_XSHM_EXTENSION
36 XWindowAttributes xgwa;
37 GC draw_gc, erase_gc, text_gc;
41 enum { SEED_RAM, SEED_RANDOM } seed_mode;
42 enum { DRAW_COLOR, DRAW_MONO } draw_mode;
47 # ifdef HAVE_XSHM_EXTENSION
49 XShmSegmentInfo shm_info;
56 init_memscroller (Display *dpy, Window window)
60 state *st = (state *) calloc (1, sizeof (*st));
64 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
65 XSelectInput(st->dpy, st->window, ExposureMask);
67 /* Fill up the colormap with random colors.
68 We don't actually use these explicitly, but in 8-bit mode,
69 they will be used implicitly by the random image bits. */
73 make_random_colormap (st->dpy, st->xgwa.visual, st->xgwa.colormap,
74 colors, &ncolors, True, True, 0, False);
77 st->border = get_integer_resource ("borderSize", "BorderSize");
80 char *fontname = get_string_resource ("font", "Font");
81 st->font = XLoadQueryFont (dpy, fontname);
85 static const char *fonts[] = {
86 "-*-courier-medium-r-*-*-*-1400-*-*-p-*-*-*",
87 "-*-courier-medium-r-*-*-*-600-*-*-p-*-*-*",
88 "-*-utopia-*-r-*-*-*-1400-*-*-p-*-*-*",
89 "-*-utopia-*-r-*-*-*-600-*-*-p-*-*-*",
90 "-*-utopia-*-r-*-*-*-240-*-*-p-*-*-*",
91 "-*-helvetica-*-r-*-*-*-240-*-*-p-*-*-*",
92 "-*-*-*-r-*-*-*-240-*-*-m-*-*-*",
97 st->font = XLoadQueryFont (dpy, fonts[i]);
102 fprintf (stderr, "%s: couldn't load font \"%s\", using \"%s\"\n",
103 progname, fontname, fonts[i]);
106 fprintf(stderr, "%s: couldn't load any font\n", progname);
112 gcv.line_width = st->border;
114 gcv.background = get_pixel_resource("background", "Background",
115 st->dpy, st->xgwa.colormap);
116 gcv.foreground = get_pixel_resource("textColor", "Foreground",
117 st->dpy, st->xgwa.colormap);
118 gcv.font = st->font->fid;
119 st->text_gc = XCreateGC (st->dpy, st->window,
120 GCForeground|GCBackground|GCFont, &gcv);
122 gcv.foreground = get_pixel_resource("foreground", "Foreground",
123 st->dpy, st->xgwa.colormap);
124 st->draw_gc = XCreateGC (st->dpy, st->window,
125 GCForeground|GCBackground|GCLineWidth,
127 gcv.foreground = gcv.background;
128 st->erase_gc = XCreateGC (st->dpy, st->window,
129 GCForeground|GCBackground, &gcv);
132 s = get_string_resource ("drawMode", "DrawMode");
133 if (!s || !*s || !strcasecmp (s, "color"))
134 st->draw_mode = DRAW_COLOR;
135 else if (!strcasecmp (s, "mono"))
136 st->draw_mode = DRAW_MONO;
139 fprintf (stderr, "%s: drawMode must be 'mono' or 'color', not '%s'\n",
146 s = get_string_resource ("seedMode", "SeedMode");
148 !strcasecmp (s, "ram") ||
149 !strcasecmp (s, "mem") ||
150 !strcasecmp (s, "memory"))
151 st->seed_mode = SEED_RAM;
152 else if (!strcasecmp (s, "rand") ||
153 !strcasecmp (s, "random"))
154 st->seed_mode = SEED_RANDOM;
157 fprintf (stderr, "%s: seedMode must be 'RAM' or 'random', not '%s'\n",
166 st->scrollers = (scroller *) calloc (st->nscrollers, sizeof(scroller));
168 for (i = 0; i < st->nscrollers; i++)
170 scroller *sc = &st->scrollers[i];
171 int max_height = 4096;
177 # ifdef HAVE_XSHM_EXTENSION
178 st->shm_p = get_boolean_resource ("useSHM", "Boolean");
181 sc->image = create_xshm_image (st->dpy, st->xgwa.visual,
183 ZPixmap, 0, &st->shm_info,
188 # endif /* HAVE_XSHM_EXTENSION */
191 sc->image = XCreateImage (st->dpy, st->xgwa.visual, st->xgwa.depth,
192 ZPixmap, 0, 0, 1, max_height, 8, 0);
194 if (sc->image && !sc->image->data)
195 sc->image->data = (char *)
196 malloc (sc->image->bytes_per_line * sc->image->height + 1);
198 if (!sc->image || !sc->image->data)
200 fprintf (stderr, "%s: out of memory (allocating 1x%d image)\n",
201 progname, sc->image->height);
211 reshape_memscroller (state *st)
215 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
217 for (i = 0; i < st->nscrollers; i++)
219 scroller *sc = &st->scrollers[i];
223 sc->rez = 6; /* #### */
225 sc->rect.width = (((int) (st->xgwa.width * 0.8)
226 / sc->rez) * sc->rez);
227 sc->rect.height = (((int) (st->xgwa.height * 0.3)
228 / sc->rez) * sc->rez);
230 sc->rect.x = (st->xgwa.width - sc->rect.width) / 2;
231 sc->rect.y = (st->xgwa.height - sc->rect.height) / 2;
235 scroller *sc0 = &st->scrollers[i-1];
236 sc->rez = sc0->rez * 1.8;
238 sc->rect.x = sc0->rect.x;
239 sc->rect.y = (sc0->rect.y + sc0->rect.height + st->border
240 + (st->border + 2) * 7);
241 sc->rect.width = sc0->rect.width;
242 sc->rect.height = (((int) (st->xgwa.height * 0.1)
243 / sc->rez) * sc->rez);
246 XDrawRectangle (st->dpy, st->window, st->draw_gc,
247 sc->rect.x - st->border*2,
248 sc->rect.y - st->border*2,
249 sc->rect.width + st->border*4,
250 sc->rect.height + st->border*4);
257 more_bits (state *st, scroller *sc)
259 static unsigned char *lomem = 0;
260 static unsigned char *himem = 0;
261 unsigned char r, g, b;
264 /* Pack bytes as RGBR so that we don't have to worry about actually
265 figuring out the color channel order.
268 # define PACK(r,g,b) ((r) | ((g) << 8) | ((b) << 16) | ((r) << 24))
270 switch (st->seed_mode)
275 lomem = (unsigned char *) progname; /* not first malloc, but early */
276 himem = (unsigned char *) /* not last malloc, but late */
277 st->scrollers[st->nscrollers-1].image->data;
280 if (sc->data < lomem)
283 # ifdef HAVE_SBRK /* re-get it each time through */
284 himem = ((unsigned char *) sbrk(0)) - (2 * sizeof(void *));
287 if (lomem >= himem) abort();
290 if (sc->data >= himem)
293 switch (st->draw_mode)
311 /* avoid having many seconds of blackness: truncate zeros at 24K.
317 if (sc->count_zero > 1024 * (st->draw_mode == DRAW_COLOR ? 24 : 8))
324 switch (st->draw_mode)
327 r = (v >> 16) & 0xFF;
352 draw_string (state *st, unsigned int n)
355 int direction, ascent, descent;
358 int bot = st->scrollers[0].rect.y;
359 const char *fmt = "%08X";
361 sprintf (buf, fmt, 0);
362 XTextExtents (st->font, buf, strlen(buf),
363 &direction, &ascent, &descent, &overall);
364 sprintf (buf, "%08X", n);
367 h = st->font->ascent + st->font->descent + 1;
368 x = (st->xgwa.width - w) / 2;
371 if (y + h + 10 > bot) return;
373 XFillRectangle (st->dpy, st->window, st->erase_gc,
375 XDrawString (st->dpy, st->window, st->text_gc,
376 x, y + st->font->ascent, buf, strlen(buf));
381 draw_memscroller (state *st)
385 draw_string (st, *((unsigned int *) st->scrollers[0].image->data));
387 for (i = 0; i < st->nscrollers; i++)
389 scroller *sc = &st->scrollers[i];
392 XCopyArea (st->dpy, st->window, st->window, st->draw_gc,
393 sc->rect.x + sc->speed, sc->rect.y,
394 sc->rect.width - sc->speed, sc->rect.height,
395 sc->rect.x, sc->rect.y);
397 if (sc->scroll_tick == 0)
399 int top = ((sc->image->bytes_per_line * sc->rect.height) /
401 unsigned int *out = (unsigned int *) sc->image->data;
402 for (j = 0; j < top; j++)
404 unsigned int v = more_bits(st, sc);
406 for (k = 0; k < sc->rez; k++)
412 if (sc->scroll_tick * sc->speed >= sc->rez)
415 for (j = 0; j < sc->speed; j++)
417 # ifdef HAVE_XSHM_EXTENSION
419 XShmPutImage (st->dpy, st->window, st->draw_gc, sc->image,
421 sc->rect.x + sc->rect.width - sc->image->width - j,
423 sc->rect.width, sc->rect.height,
426 # endif /* HAVE_XSHM_EXTENSION */
427 XPutImage (st->dpy, st->window, st->draw_gc, sc->image,
429 sc->rect.x + sc->rect.width - sc->image->width - j,
431 sc->rect.width, sc->rect.height);
437 handle_events (state *st)
439 XSync (st->dpy, False);
440 while (XPending (st->dpy))
443 XNextEvent (st->dpy, &event);
444 if (event.xany.type == ConfigureNotify ||
445 event.xany.type == Expose)
447 XClearWindow (st->dpy, st->window);
448 reshape_memscroller (st);
451 screenhack_handle_event (st->dpy, &event);
457 char *progclass = "MemScroller";
459 char *defaults [] = {
460 ".background: black",
463 ".textColor: #00FF00",
464 ".foreground: #00FF00",
466 ".font: -*-courier-medium-r-*-*-*-1400-*-*-m-*-*-*",
472 XrmOptionDescRec options [] = {
473 { "-delay", ".delay", XrmoptionSepArg, 0 },
474 { "-font", ".font", XrmoptionSepArg, 0 },
475 { "-color", ".drawMode", XrmoptionNoArg, "color" },
476 { "-mono", ".drawMode", XrmoptionNoArg, "mono" },
477 { "-ram", ".seedMode", XrmoptionNoArg, "ram" },
478 { "-random", ".seedMode", XrmoptionNoArg, "random" },
484 screenhack (Display *dpy, Window window)
486 state *st = init_memscroller (dpy, window);
487 int delay = get_integer_resource ("delay", "Integer");
488 reshape_memscroller (st);
491 draw_memscroller (st);
494 if (delay) usleep (delay);