1 /* xscreensaver, Copyright (c) 2002-2015 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)))
43 XWindowAttributes xgwa;
44 GC draw_gc, erase_gc, text_gc;
45 XFontStruct *fonts[6];
48 enum { SEED_RAM, SEED_RANDOM, SEED_FILE } seed_mode;
49 enum { DRAW_COLOR, DRAW_MONO } draw_mode;
57 # ifdef HAVE_XSHM_EXTENSION
59 XShmSegmentInfo shm_info;
67 static void reshape_memscroller (state *st);
71 memscroller_init (Display *dpy, Window window)
75 state *st = (state *) calloc (1, sizeof (*st));
79 st->delay = get_integer_resource (dpy, "delay", "Integer");
81 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
83 /* Fill up the colormap with random colors.
84 We don't actually use these explicitly, but in 8-bit mode,
85 they will be used implicitly by the random image bits. */
89 make_random_colormap (st->xgwa.screen, st->xgwa.visual, st->xgwa.colormap,
90 colors, &ncolors, True, True, 0, False);
93 st->border = get_integer_resource (dpy, "borderSize", "BorderSize");
97 int nfonts = countof (st->fonts);
98 for (i = nfonts-1; i >= 0; i--)
102 sprintf (res, "font%d", i+1);
103 fontname = get_string_resource (dpy, res, "Font");
104 /* Each resource can be a comma-separated list of font names.
105 We use the first one that exists. */
106 if (fontname && *fontname)
108 char *f2 = strdup(fontname);
109 char *f, *token = f2;
110 while ((f = strtok(token, ",")) && !st->fonts[i])
113 while (*f == ' ' || *f == '\t') f++;
114 st->fonts[i] = XLoadQueryFont (dpy, f);
117 if (!st->fonts[i] && i < nfonts-1)
119 fprintf (stderr, "%s: unable to load font: \"%s\"\n",
121 st->fonts[i] = st->fonts[i+1];
127 st->fonts[0] = XLoadQueryFont (dpy, "fixed");
131 fprintf (stderr, "%s: unable to load any fonts!", progname);
136 gcv.line_width = st->border;
138 gcv.background = get_pixel_resource(st->dpy, st->xgwa.colormap,
139 "background", "Background");
140 gcv.foreground = get_pixel_resource(st->dpy, st->xgwa.colormap,
141 "textColor", "Foreground");
142 st->text_gc = XCreateGC (st->dpy, st->window,
143 GCForeground|GCBackground, &gcv);
145 gcv.foreground = get_pixel_resource(st->dpy, st->xgwa.colormap,
146 "foreground", "Foreground");
147 st->draw_gc = XCreateGC (st->dpy, st->window,
148 GCForeground|GCBackground|GCLineWidth,
150 gcv.foreground = gcv.background;
151 st->erase_gc = XCreateGC (st->dpy, st->window,
152 GCForeground|GCBackground, &gcv);
155 s = get_string_resource (dpy, "drawMode", "DrawMode");
156 if (!s || !*s || !strcasecmp (s, "color"))
157 st->draw_mode = DRAW_COLOR;
158 else if (!strcasecmp (s, "mono"))
159 st->draw_mode = DRAW_MONO;
162 fprintf (stderr, "%s: drawMode must be 'mono' or 'color', not '%s'\n",
171 st->filename = get_string_resource (dpy, "filename", "Filename");
176 !strcasecmp (st->filename, "(ram)") ||
177 !strcasecmp (st->filename, "(mem)") ||
178 !strcasecmp (st->filename, "(memory)"))
179 st->seed_mode = SEED_RAM;
181 else if (st->filename &&
182 (!strcasecmp (st->filename, "(rand)") ||
183 !strcasecmp (st->filename, "(random)")))
184 st->seed_mode = SEED_RANDOM;
186 st->seed_mode = SEED_FILE;
188 st->seed_mode = SEED_RANDOM;
192 st->scrollers = (scroller *) calloc (st->nscrollers, sizeof(scroller));
194 for (i = 0; i < st->nscrollers; i++)
196 scroller *sc = &st->scrollers[i];
197 int max_height = 4096;
203 # ifdef HAVE_XSHM_EXTENSION
204 st->shm_p = get_boolean_resource (dpy, "useSHM", "Boolean");
207 sc->image = create_xshm_image (st->dpy, st->xgwa.visual,
209 ZPixmap, 0, &st->shm_info,
214 # endif /* HAVE_XSHM_EXTENSION */
217 sc->image = XCreateImage (st->dpy, st->xgwa.visual, st->xgwa.depth,
218 ZPixmap, 0, 0, 1, max_height, 8, 0);
220 if (sc->image && !sc->image->data)
221 sc->image->data = (char *)
222 malloc (sc->image->bytes_per_line * sc->image->height + 1);
224 if (!sc->image || !sc->image->data)
226 fprintf (stderr, "%s: out of memory (allocating 1x%d image)\n",
227 progname, sc->image->height);
232 reshape_memscroller (st);
238 reshape_memscroller (state *st)
242 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
244 for (i = 0; i < st->nscrollers; i++)
246 scroller *sc = &st->scrollers[i];
250 sc->rez = 6; /* #### */
252 sc->rect.width = (((int) (st->xgwa.width * 0.8)
253 / sc->rez) * sc->rez);
254 sc->rect.height = (((int) (st->xgwa.height * 0.3)
255 / sc->rez) * sc->rez);
257 sc->rect.x = (st->xgwa.width - sc->rect.width) / 2;
258 sc->rect.y = (st->xgwa.height - sc->rect.height) / 2;
262 scroller *sc0 = &st->scrollers[i-1];
263 sc->rez = sc0->rez * 1.8;
265 sc->rect.x = sc0->rect.x;
266 sc->rect.y = (sc0->rect.y + sc0->rect.height + st->border
267 + (st->border + 2) * 7);
268 sc->rect.width = sc0->rect.width;
269 sc->rect.height = (((int) (st->xgwa.height * 0.1)
270 / sc->rez) * sc->rez);
273 XDrawRectangle (st->dpy, st->window, st->draw_gc,
274 sc->rect.x - st->border*2,
275 sc->rect.y - st->border*2,
276 sc->rect.width + st->border*4,
277 sc->rect.height + st->border*4);
285 open_file (state *st)
293 st->in = fopen (st->filename, "r");
297 sprintf (buf, "%s: %s", progname, st->filename);
305 /* "The brk and sbrk functions are historical curiosities left over
306 from earlier days before the advent of virtual memory management."
307 -- sbrk(2) man page on BSD systems, as of 1995 or so.
310 # if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) /* gcc >= 4.2 */
311 /* Don't print "warning: 'sbrk' is deprecated". */
312 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
318 more_bits (state *st, scroller *sc)
320 static unsigned char *lomem = 0;
321 static unsigned char *himem = 0;
322 unsigned char r, g, b;
324 /* vv: Each incoming byte rolls through all 4 bytes of this (it is sc->value)
325 This is the number displayed at the top.
326 pv: the pixel color value. incoming bytes land in R,G,B, or maybe just G.
330 unsigned int rmsk = st->scrollers[0].image->red_mask;
331 unsigned int gmsk = st->scrollers[0].image->green_mask;
332 unsigned int bmsk = st->scrollers[0].image->blue_mask;
333 unsigned int amsk = ~(rmsk | gmsk | bmsk);
337 /* Pack RGB into a pixel according to the XImage component masks;
338 set the remaining bits to 1 for the benefit of HAVE_JWXYZ alpha.
341 # define PACK() ((((r << 24) | (r << 16) | (r << 8) | r) & rmsk) | \
342 (((g << 24) | (g << 16) | (g << 8) | g) & gmsk) | \
343 (((b << 24) | (b << 16) | (b << 8) | b) & bmsk) | \
346 switch (st->seed_mode)
351 lomem = (unsigned char *) progname; /* not first malloc, but early */
352 himem = (unsigned char *) /* not last malloc, but late */
353 st->scrollers[st->nscrollers-1].image->data;
356 if (sc->data < lomem)
359 # ifdef HAVE_SBRK /* re-get it each time through */
360 himem = ((unsigned char *) sbrk(0)) - (2 * sizeof(void *));
363 if (!lomem || !himem)
365 /* bad craziness! give up! */
366 st->seed_mode = SEED_RANDOM;
370 /* I don't understand what's going on there, but on MacOS X, we're
371 getting insane values for lomem and himem (both Xlib and HAVE_JWXYZ).
372 Does malloc() draw from more than one heap? */
373 if ((unsigned long) himem - (unsigned long) lomem > 0x0FFFFFFF) {
375 fprintf (stderr, "%s: wonky: 0x%08x - 0x%08x = 0x%08x\n", progname,
376 (unsigned int) himem, (unsigned int) lomem,
377 (unsigned int) himem - (unsigned int) lomem);
379 himem = lomem + 0xFFFF;
382 if (lomem >= himem) abort();
385 if (sc->data >= himem)
388 switch (st->draw_mode)
394 vv = (vv << 24) | (r << 16) | (g << 8) | b;
408 /* avoid having many seconds of blackness: truncate zeros at 24K.
414 if (sc->count_zero > 1024 * (st->draw_mode == DRAW_COLOR ? 24 : 8))
421 switch (st->draw_mode)
424 r = (vv >> 16) & 0xFF;
425 g = (vv >> 8) & 0xFF;
444 /* this one returns only bytes from the file */
447 i = fgetc (st->in); \
449 ? (open_file (st), 1) \
453 /* this one returns a null at EOF -- else we hang on zero-length files */
456 i = fgetc (st->in); \
457 if (i == EOF) { i = 0; open_file (st); } \
463 switch (st->draw_mode)
469 vv = (vv << 24) | (r << 16) | (g << 8) | b;
484 # endif /* READ_FILES */
498 draw_string (state *st)
501 int direction, ascent, descent;
502 int bot = st->scrollers[0].rect.y;
503 const char *fmt = "%08X";
506 /* Draw the first font that fits.
508 for (i = 0; i < countof (st->fonts); i++)
513 if (! st->fonts[i]) continue;
515 sprintf (buf, fmt, 0);
516 XTextExtents (st->fonts[i], buf, strlen(buf),
517 &direction, &ascent, &descent, &overall);
518 sprintf (buf, "%08X", st->scrollers[0].value);
521 h = ascent + descent + 1;
522 x = (st->xgwa.width - w) / 2;
525 if (y + h + 10 <= bot && x > -10)
527 XSetFont (st->dpy, st->text_gc, st->fonts[i]->fid);
528 XFillRectangle (st->dpy, st->window, st->erase_gc,
530 XDrawString (st->dpy, st->window, st->text_gc,
531 x, y + ascent, buf, strlen(buf));
539 memscroller_draw (Display *dpy, Window window, void *closure)
541 state *st = (state *) closure;
545 for (i = 0; i < st->nscrollers; i++)
547 scroller *sc = &st->scrollers[i];
550 XCopyArea (st->dpy, st->window, st->window, st->draw_gc,
551 sc->rect.x + sc->speed, sc->rect.y,
552 sc->rect.width - sc->speed, sc->rect.height,
553 sc->rect.x, sc->rect.y);
555 if (sc->scroll_tick == 0)
557 int top = ((sc->image->bytes_per_line * sc->rect.height) /
559 unsigned int *out = (unsigned int *) sc->image->data;
560 for (j = 0; j < top; j++)
562 unsigned int v = more_bits(st, sc);
564 for (k = 0; k < sc->rez; k++)
570 if (sc->scroll_tick * sc->speed >= sc->rez)
573 for (j = 0; j < sc->speed; j++)
575 # ifdef HAVE_XSHM_EXTENSION
577 XShmPutImage (st->dpy, st->window, st->draw_gc, sc->image,
579 sc->rect.x + sc->rect.width - sc->image->width - j,
581 sc->rect.width, sc->rect.height,
584 # endif /* HAVE_XSHM_EXTENSION */
585 XPutImage (st->dpy, st->window, st->draw_gc, sc->image,
587 sc->rect.x + sc->rect.width - sc->image->width - j,
589 sc->rect.width, sc->rect.height);
598 memscroller_reshape (Display *dpy, Window window, void *closure,
599 unsigned int w, unsigned int h)
601 state *st = (state *) closure;
602 XClearWindow (st->dpy, st->window);
603 reshape_memscroller (st);
607 memscroller_event (Display *dpy, Window window, void *closure, XEvent *event)
614 memscroller_free (Display *dpy, Window window, void *closure)
619 static const char *memscroller_defaults [] = {
620 ".background: black",
625 ".textColor: #00FF00",
626 ".foreground: #00FF00",
629 #if defined(HAVE_COCOA) || defined(HAVE_ANDROID)
630 ".font1: OCR A Std 192, Lucida Console 192, Monaco 192",
631 ".font2: OCR A Std 144, Lucida Console 144, Monaco 144",
632 ".font3: OCR A Std 128, Lucida Console 128, Monaco 128",
633 ".font4: OCR A Std 96, Lucida Console 96, Monaco 96",
634 ".font5: OCR A Std 48, Lucida Console 48, Monaco 48",
635 ".font6: OCR A Std 24, Lucida Console 24, Monaco 24",
637 ".font1: -*-courier-bold-r-*-*-*-1440-*-*-m-*-*-*",
638 ".font2: -*-courier-bold-r-*-*-*-960-*-*-m-*-*-*",
639 ".font3: -*-courier-bold-r-*-*-*-480-*-*-m-*-*-*",
640 ".font4: -*-courier-bold-r-*-*-*-320-*-*-m-*-*-*",
641 ".font5: -*-courier-bold-r-*-*-*-180-*-*-m-*-*-*",
643 #endif /* real X11 */
650 static XrmOptionDescRec memscroller_options [] = {
651 { "-delay", ".delay", XrmoptionSepArg, 0 },
652 { "-font", ".font", XrmoptionSepArg, 0 },
653 { "-filename", ".filename", XrmoptionSepArg, 0 },
654 { "-color", ".drawMode", XrmoptionNoArg, "color" },
655 { "-mono", ".drawMode", XrmoptionNoArg, "mono" },
656 { "-ram", ".filename", XrmoptionNoArg, "(RAM)" },
657 { "-random", ".filename", XrmoptionNoArg, "(RANDOM)" },
661 XSCREENSAVER_MODULE ("MemScroller", memscroller)