1 /* xscreensaver, Copyright (c) 2002, 2004, 2006 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"
17 #ifdef HAVE_XSHM_EXTENSION
22 #define countof(x) (sizeof(x)/sizeof(*(x)))
39 XWindowAttributes xgwa;
40 GC draw_gc, erase_gc, text_gc;
41 XFontStruct *fonts[6];
44 enum { SEED_RAM, SEED_RANDOM, SEED_FILE } seed_mode;
45 enum { DRAW_COLOR, DRAW_MONO } draw_mode;
53 # ifdef HAVE_XSHM_EXTENSION
55 XShmSegmentInfo shm_info;
63 static void reshape_memscroller (state *st);
67 memscroller_init (Display *dpy, Window window)
71 state *st = (state *) calloc (1, sizeof (*st));
75 st->delay = get_integer_resource (dpy, "delay", "Integer");
77 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
79 /* Fill up the colormap with random colors.
80 We don't actually use these explicitly, but in 8-bit mode,
81 they will be used implicitly by the random image bits. */
85 make_random_colormap (st->dpy, st->xgwa.visual, st->xgwa.colormap,
86 colors, &ncolors, True, True, 0, False);
89 st->border = get_integer_resource (dpy, "borderSize", "BorderSize");
93 int nfonts = countof (st->fonts);
94 for (i = nfonts-1; i >= 0; i--)
98 sprintf (res, "font%d", i+1);
99 fontname = get_string_resource (dpy, res, "Font");
100 /* Each resource can be a comma-separated list of font names.
101 We use the first one that exists. */
102 if (fontname && *fontname)
104 char *f2 = strdup(fontname);
105 char *f, *token = f2;
106 while ((f = strtok(token, ",")) && !st->fonts[i])
109 while (*f == ' ' || *f == '\t') f++;
110 st->fonts[i] = XLoadQueryFont (dpy, f);
113 if (!st->fonts[i] && i < nfonts-1)
115 fprintf (stderr, "%s: unable to load font: \"%s\"\n",
117 st->fonts[i] = st->fonts[i+1];
123 st->fonts[0] = XLoadQueryFont (dpy, "fixed");
127 fprintf (stderr, "%s: unable to load any fonts!", progname);
132 gcv.line_width = st->border;
134 gcv.background = get_pixel_resource(st->dpy, st->xgwa.colormap,
135 "background", "Background");
136 gcv.foreground = get_pixel_resource(st->dpy, st->xgwa.colormap,
137 "textColor", "Foreground");
138 st->text_gc = XCreateGC (st->dpy, st->window,
139 GCForeground|GCBackground, &gcv);
141 gcv.foreground = get_pixel_resource(st->dpy, st->xgwa.colormap,
142 "foreground", "Foreground");
143 st->draw_gc = XCreateGC (st->dpy, st->window,
144 GCForeground|GCBackground|GCLineWidth,
146 gcv.foreground = gcv.background;
147 st->erase_gc = XCreateGC (st->dpy, st->window,
148 GCForeground|GCBackground, &gcv);
151 s = get_string_resource (dpy, "drawMode", "DrawMode");
152 if (!s || !*s || !strcasecmp (s, "color"))
153 st->draw_mode = DRAW_COLOR;
154 else if (!strcasecmp (s, "mono"))
155 st->draw_mode = DRAW_MONO;
158 fprintf (stderr, "%s: drawMode must be 'mono' or 'color', not '%s'\n",
166 st->filename = get_string_resource (dpy, "filename", "Filename");
170 !strcasecmp (st->filename, "(ram)") ||
171 !strcasecmp (st->filename, "(mem)") ||
172 !strcasecmp (st->filename, "(memory)"))
173 st->seed_mode = SEED_RAM;
174 else if (st->filename &&
175 (!strcasecmp (st->filename, "(rand)") ||
176 !strcasecmp (st->filename, "(random)")))
177 st->seed_mode = SEED_RANDOM;
179 st->seed_mode = SEED_FILE;
182 st->scrollers = (scroller *) calloc (st->nscrollers, sizeof(scroller));
184 for (i = 0; i < st->nscrollers; i++)
186 scroller *sc = &st->scrollers[i];
187 int max_height = 4096;
193 # ifdef HAVE_XSHM_EXTENSION
194 st->shm_p = get_boolean_resource (dpy, "useSHM", "Boolean");
197 sc->image = create_xshm_image (st->dpy, st->xgwa.visual,
199 ZPixmap, 0, &st->shm_info,
204 # endif /* HAVE_XSHM_EXTENSION */
207 sc->image = XCreateImage (st->dpy, st->xgwa.visual, st->xgwa.depth,
208 ZPixmap, 0, 0, 1, max_height, 8, 0);
210 if (sc->image && !sc->image->data)
211 sc->image->data = (char *)
212 malloc (sc->image->bytes_per_line * sc->image->height + 1);
214 if (!sc->image || !sc->image->data)
216 fprintf (stderr, "%s: out of memory (allocating 1x%d image)\n",
217 progname, sc->image->height);
222 reshape_memscroller (st);
228 reshape_memscroller (state *st)
232 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
234 for (i = 0; i < st->nscrollers; i++)
236 scroller *sc = &st->scrollers[i];
240 sc->rez = 6; /* #### */
242 sc->rect.width = (((int) (st->xgwa.width * 0.8)
243 / sc->rez) * sc->rez);
244 sc->rect.height = (((int) (st->xgwa.height * 0.3)
245 / sc->rez) * sc->rez);
247 sc->rect.x = (st->xgwa.width - sc->rect.width) / 2;
248 sc->rect.y = (st->xgwa.height - sc->rect.height) / 2;
252 scroller *sc0 = &st->scrollers[i-1];
253 sc->rez = sc0->rez * 1.8;
255 sc->rect.x = sc0->rect.x;
256 sc->rect.y = (sc0->rect.y + sc0->rect.height + st->border
257 + (st->border + 2) * 7);
258 sc->rect.width = sc0->rect.width;
259 sc->rect.height = (((int) (st->xgwa.height * 0.1)
260 / sc->rez) * sc->rez);
263 XDrawRectangle (st->dpy, st->window, st->draw_gc,
264 sc->rect.x - st->border*2,
265 sc->rect.y - st->border*2,
266 sc->rect.width + st->border*4,
267 sc->rect.height + st->border*4);
274 open_file (state *st)
282 st->in = fopen (st->filename, "r");
286 sprintf (buf, "%s: %s", progname, st->filename);
294 more_bits (state *st, scroller *sc)
296 static unsigned char *lomem = 0;
297 static unsigned char *himem = 0;
298 unsigned char r, g, b;
300 /* vv: Each incoming byte rolls through all 4 bytes of this (it is sc->value)
301 This is the number displayed at the top.
302 pv: the pixel color value. incoming bytes land in R,G,B, or maybe just G.
306 unsigned int rmsk = st->scrollers[0].image->red_mask;
307 unsigned int gmsk = st->scrollers[0].image->green_mask;
308 unsigned int bmsk = st->scrollers[0].image->blue_mask;
309 unsigned int amsk = ~(rmsk | gmsk | bmsk);
313 /* Pack RGB into a pixel according to the XImage component masks;
314 set the remaining bits to 1 for the benefit of HAVE_COCOA alpha.
317 # define PACK() ((((r << 24) | (r << 16) | (r << 8) | r) & rmsk) | \
318 (((g << 24) | (g << 16) | (g << 8) | g) & gmsk) | \
319 (((b << 24) | (b << 16) | (b << 8) | b) & bmsk) | \
322 switch (st->seed_mode)
327 lomem = (unsigned char *) progname; /* not first malloc, but early */
328 himem = (unsigned char *) /* not last malloc, but late */
329 st->scrollers[st->nscrollers-1].image->data;
332 if (sc->data < lomem)
335 # ifdef HAVE_SBRK /* re-get it each time through */
336 /* "The brk and sbrk functions are historical curiosities left over
337 from earlier days before the advent of virtual memory management."
338 -- sbrk(2) man page on MacOS
340 himem = ((unsigned char *) sbrk(0)) - (2 * sizeof(void *));
343 if (!lomem || !himem)
345 /* bad craziness! give up! */
346 st->seed_mode = SEED_RANDOM;
350 /* I don't understand what's going on there, but on MacOS X, we're
351 getting insane values for lomem and himem (both Xlib and HAVE_COCOA).
352 Does malloc() draw from more than one heap? */
353 if ((unsigned long) himem - (unsigned long) lomem > 0x0FFFFFFF) {
355 fprintf (stderr, "%s: wonky: 0x%08x - 0x%08x = 0x%08x\n", progname,
356 (unsigned int) himem, (unsigned int) lomem,
357 (unsigned int) himem - (unsigned int) lomem);
359 himem = lomem + 0xFFFF;
362 if (lomem >= himem) abort();
365 if (sc->data >= himem)
368 switch (st->draw_mode)
374 vv = (vv << 24) | (r << 16) | (g << 8) | b;
388 /* avoid having many seconds of blackness: truncate zeros at 24K.
394 if (sc->count_zero > 1024 * (st->draw_mode == DRAW_COLOR ? 24 : 8))
401 switch (st->draw_mode)
404 r = (vv >> 16) & 0xFF;
405 g = (vv >> 8) & 0xFF;
423 /* this one returns only bytes from the file */
426 i = fgetc (st->in); \
428 ? (open_file (st), 1) \
432 /* this one returns a null at EOF -- else we hang on zero-length files */
435 i = fgetc (st->in); \
436 if (i == EOF) { i = 0; open_file (st); } \
442 switch (st->draw_mode)
448 vv = (vv << 24) | (r << 16) | (g << 8) | b;
476 draw_string (state *st)
479 int direction, ascent, descent;
480 int bot = st->scrollers[0].rect.y;
481 const char *fmt = "%08X";
484 /* Draw the first font that fits.
486 for (i = 0; i < countof (st->fonts); i++)
491 if (! st->fonts[i]) continue;
493 sprintf (buf, fmt, 0);
494 XTextExtents (st->fonts[i], buf, strlen(buf),
495 &direction, &ascent, &descent, &overall);
496 sprintf (buf, "%08X", st->scrollers[0].value);
499 h = ascent + descent + 1;
500 x = (st->xgwa.width - w) / 2;
503 if (y + h + 10 <= bot && x > -10)
505 XSetFont (st->dpy, st->text_gc, st->fonts[i]->fid);
506 XFillRectangle (st->dpy, st->window, st->erase_gc,
508 XDrawString (st->dpy, st->window, st->text_gc,
509 x, y + ascent, buf, strlen(buf));
517 memscroller_draw (Display *dpy, Window window, void *closure)
519 state *st = (state *) closure;
523 for (i = 0; i < st->nscrollers; i++)
525 scroller *sc = &st->scrollers[i];
528 XCopyArea (st->dpy, st->window, st->window, st->draw_gc,
529 sc->rect.x + sc->speed, sc->rect.y,
530 sc->rect.width - sc->speed, sc->rect.height,
531 sc->rect.x, sc->rect.y);
533 if (sc->scroll_tick == 0)
535 int top = ((sc->image->bytes_per_line * sc->rect.height) /
537 unsigned int *out = (unsigned int *) sc->image->data;
538 for (j = 0; j < top; j++)
540 unsigned int v = more_bits(st, sc);
542 for (k = 0; k < sc->rez; k++)
548 if (sc->scroll_tick * sc->speed >= sc->rez)
551 for (j = 0; j < sc->speed; j++)
553 # ifdef HAVE_XSHM_EXTENSION
555 XShmPutImage (st->dpy, st->window, st->draw_gc, sc->image,
557 sc->rect.x + sc->rect.width - sc->image->width - j,
559 sc->rect.width, sc->rect.height,
562 # endif /* HAVE_XSHM_EXTENSION */
563 XPutImage (st->dpy, st->window, st->draw_gc, sc->image,
565 sc->rect.x + sc->rect.width - sc->image->width - j,
567 sc->rect.width, sc->rect.height);
576 memscroller_reshape (Display *dpy, Window window, void *closure,
577 unsigned int w, unsigned int h)
579 state *st = (state *) closure;
580 XClearWindow (st->dpy, st->window);
581 reshape_memscroller (st);
585 memscroller_event (Display *dpy, Window window, void *closure, XEvent *event)
592 memscroller_free (Display *dpy, Window window, void *closure)
597 static const char *memscroller_defaults [] = {
598 ".background: black",
603 ".textColor: #00FF00",
604 ".foreground: #00FF00",
608 ".font1: OCR A Std 192, Lucida Console 192",
609 ".font2: OCR A Std 144, Lucida Console 144",
610 ".font3: OCR A Std 128, Lucida Console 128",
611 ".font4: OCR A Std 96, Lucida Console 96",
612 ".font5: OCR A Std 48, Lucida Console 48",
613 ".font6: OCR A Std 24, Lucida Console 24",
614 #else /* !HAVE_COCOA */
615 ".font1: -*-courier-bold-r-*-*-*-1440-*-*-m-*-*-*",
616 ".font2: -*-courier-bold-r-*-*-*-960-*-*-m-*-*-*",
617 ".font3: -*-courier-bold-r-*-*-*-480-*-*-m-*-*-*",
618 ".font4: -*-courier-bold-r-*-*-*-320-*-*-m-*-*-*",
619 ".font5: -*-courier-bold-r-*-*-*-180-*-*-m-*-*-*",
621 #endif /* !HAVE_COCOA */
628 static XrmOptionDescRec memscroller_options [] = {
629 { "-delay", ".delay", XrmoptionSepArg, 0 },
630 { "-font", ".font", XrmoptionSepArg, 0 },
631 { "-filename", ".filename", XrmoptionSepArg, 0 },
632 { "-color", ".drawMode", XrmoptionNoArg, "color" },
633 { "-mono", ".drawMode", XrmoptionNoArg, "mono" },
634 { "-ram", ".filename", XrmoptionNoArg, "(RAM)" },
635 { "-random", ".filename", XrmoptionNoArg, "(RANDOM)" },
639 XSCREENSAVER_MODULE ("MemScroller", memscroller)