1 /* xflame, Copyright (c) 1996-2018 Carsten Haitzler <raster@redhat.com>
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
12 /* Version history as near as I (jwz) can piece together:
14 * Carsten Haitzler <raster@redhat.com> wrote the first version in 1996.
16 * Rahul Jain <rahul@rice.edu> added support for TrueColor displays.
18 * Someone did a rough port of it to use the xscreensaver utility routines
19 instead of creating its own window by hand.
21 * Someone (probably Raster) came up with a subsequent version that had
22 a Red Hat logo hardcoded into it.
24 * Daniel Zahn <stumpy@religions.com> found that version in 1998, and
25 hacked it to be able to load a different logo from a PGM (P5) file,
26 with a single hardcoded pathname.
28 * Jamie Zawinski <jwz@jwz.org> found several versions of xflame in
29 March 1999, and pieced them together. Changes:
31 - Correct and fault-tolerant use of the Shared Memory extension;
32 previous versions of xflame did not work when $DISPLAY was remote.
34 - Replaced PGM-reading code with code that can read arbitrary XBM
35 and XPM files (color ones will be converted to grayscale.)
37 - Command-line options all around -- no hardcoded pathnames or
40 - General cleanup and portability tweaks.
42 * 4-Oct-99, jwz: added support for packed-24bpp (versus 32bpp.)
43 * 16-Jan-2002, jwz: added gdk_pixbuf support.
44 * 9-Oct-2016, Dave Odell <dmo2118@gmail.com>: Updated for new xshm.c.
48 /* portions by Daniel Zahn <stumpy@religions.com> */
51 #include "screenhack.h"
52 #include "ximage-loader.h"
56 #define countof(x) (sizeof((x))/sizeof((*x)))
60 # define MAX(A,B) ((A)>(B)?(A):(B))
61 # define MIN(A,B) ((A)<(B)?(A):(B))
65 #include "images/gen/bob_png.h"
80 XShmSegmentInfo shminfo;
105 GetXInfo(struct state *st)
107 XWindowAttributes xwa;
109 XGetWindowAttributes(st->dpy,st->window,&xwa);
111 st->colormap = xwa.colormap;
112 st->depth = xwa.depth;
113 st->visual = xwa.visual;
114 st->screen = xwa.screen;
115 st->width = xwa.width;
116 st->height = xwa.height;
125 MakeImage(struct state *st)
130 destroy_xshm_image (st->dpy, st->xim, &st->shminfo);
132 st->xim = create_xshm_image (st->dpy, st->visual, st->depth, ZPixmap,
133 &st->shminfo, st->width, st->height);
136 fprintf(stderr,"%s: out of memory.\n", progname);
141 st->gc = XCreateGC(st->dpy,st->window,0,&gcv);
146 InitColors(struct state *st)
148 int i = 0, j = 0, red = 0, green = 0, blue = 0;
151 /* Make it possible to set the color of the flames,
152 by Raymond Medeiros <ray@stommel.marine.usf.edu> and jwz.
154 fg.pixel = get_pixel_resource (st->dpy, st->colormap,
155 "foreground", "Foreground");
156 XQueryColor (st->dpy, st->colormap, &fg);
158 red = 255 - (fg.red >> 8);
159 green = 255 - (fg.green >> 8);
160 blue = 255 - (fg.blue >> 8);
163 for (i = 0; i < 256 * 2; i += 2)
166 int r = (i - red) * 3;
167 int g = (i - green) * 3;
168 int b = (i - blue) * 3;
171 if (r > 255) r = 255;
173 if (g > 255) g = 255;
175 if (b > 255) b = 255;
177 xcl.red = (unsigned short)((r << 8) | r);
178 xcl.green = (unsigned short)((g << 8) | g);
179 xcl.blue = (unsigned short)((b << 8) | b);
180 xcl.flags = DoRed | DoGreen | DoBlue;
182 XAllocColor(st->dpy,st->colormap,&xcl);
184 st->ctab[j++] = (int)xcl.pixel;
190 DisplayImage(struct state *st)
192 put_xshm_image(st->dpy, st->window, st->gc, st->xim, 0,(st->top - 1) << 1, 0,
193 (st->top - 1) << 1, st->width, st->height - ((st->top - 1) << 1),
199 InitFlame(struct state *st)
201 st->fwidth = st->width / 2;
202 st->fheight = st->height / 2;
204 if (st->flame) free (st->flame);
205 st->flame = (unsigned char *) malloc((st->fwidth + 2) * (st->fheight + 2)
206 * sizeof(unsigned char));
210 fprintf(stderr,"%s: out of memory\n", progname);
215 st->ihspread = get_integer_resource(st->dpy, "hspread", "Integer");
216 st->ivspread = get_integer_resource(st->dpy, "vspread", "Integer");
217 st->iresidual = get_integer_resource(st->dpy, "residual", "Integer");
218 st->variance = get_integer_resource(st->dpy, "variance", "Integer");
219 st->vartrend = get_integer_resource(st->dpy, "vartrend", "Integer");
220 st->bloom = get_boolean_resource(st->dpy, "bloom", "Boolean");
222 # define THROTTLE(VAR,NAME) \
223 if (VAR < 0 || VAR > 255) { \
224 fprintf(stderr, "%s: %s must be in the range 0-255 (not %d).\n", \
225 progname, NAME, VAR); \
227 THROTTLE (st->ihspread, "hspread");
228 THROTTLE (st->ivspread, "vspread");
229 THROTTLE (st->iresidual,"residual");
230 THROTTLE (st->variance, "variance");
231 THROTTLE (st->vartrend, "vartrend");
235 if (st->width > 2560) /* Retina displays */
237 /* #### One of these knobs must mean "make the fire be twice as tall"
238 but I can't figure out how. Changing any of the default values
239 of any of these seems to just make it all go crappy. */
240 st->ivspread = MAX (0, MIN (255, st->ivspread + 1));
244 st->hspread = st->ihspread;
245 st->vspread = st->ivspread;
246 st->residual = st->iresidual;
251 Flame2Image16(struct state *st)
258 ptr = (unsigned short *)st->xim->data;
259 ptr += (st->top << 1) * st->width;
260 ptr1 = st->flame + 1 + (st->top * (st->fwidth + 2));
262 for(y = st->top; y < st->fheight; y++)
264 for( x = 0; x < st->fwidth; x++)
267 v2 = (int)*(ptr1 + 1);
268 v3 = (int)*(ptr1 + st->fwidth + 2);
269 v4 = (int)*(ptr1 + st->fwidth + 2 + 1);
271 *ptr++ = (unsigned short)st->ctab[v1];
272 *ptr = (unsigned short)st->ctab[(v1 + v2) >> 1];
273 ptr += st->width - 1;
274 *ptr++ = (unsigned short)st->ctab[(v1 + v3) >> 1];
275 *ptr = (unsigned short)st->ctab[(v1 + v4) >> 1];
276 ptr -= st->width - 1;
284 Flame2Image32(struct state *st)
291 ptr = (unsigned int *)st->xim->data;
292 ptr += (st->top << 1) * st->width;
293 ptr1 = st->flame + 1 + (st->top * (st->fwidth + 2));
295 for( y = st->top; y < st->fheight; y++)
297 for( x = 0; x < st->fwidth; x++)
300 v2 = (int)*(ptr1 + 1);
301 v3 = (int)*(ptr1 + st->fwidth + 2);
302 v4 = (int)*(ptr1 + st->fwidth + 2 + 1);
304 *ptr++ = (unsigned int)st->ctab[v1];
305 *ptr = (unsigned int)st->ctab[(v1 + v2) >> 1];
306 ptr += st->width - 1;
307 *ptr++ = (unsigned int)st->ctab[(v1 + v3) >> 1];
308 *ptr = (unsigned int)st->ctab[(v1 + v4) >> 1];
309 ptr -= st->width - 1;
317 Flame2Image24(struct state *st)
324 ptr = (unsigned char *)st->xim->data;
325 ptr += (st->top << 1) * st->xim->bytes_per_line;
326 ptr1 = st->flame + 1 + (st->top * (st->fwidth + 2));
328 for( y = st->top; y < st->fheight; y++)
330 unsigned char *last_ptr = ptr;
331 for( x = 0; x < st->fwidth; x++)
334 v2 = (int)*(ptr1 + 1);
335 v3 = (int)*(ptr1 + st->fwidth + 2);
336 v4 = (int)*(ptr1 + st->fwidth + 2 + 1);
339 ptr[2] = ((unsigned int)st->ctab[v1] & 0x00FF0000) >> 16;
340 ptr[1] = ((unsigned int)st->ctab[v1] & 0x0000FF00) >> 8;
341 ptr[0] = ((unsigned int)st->ctab[v1] & 0x000000FF);
344 ptr[2] = ((unsigned int)st->ctab[(v1 + v2) >> 1] & 0x00FF0000) >> 16;
345 ptr[1] = ((unsigned int)st->ctab[(v1 + v2) >> 1] & 0x0000FF00) >> 8;
346 ptr[0] = ((unsigned int)st->ctab[(v1 + v2) >> 1] & 0x000000FF);
347 ptr += ((st->width - 1) * 3);
349 ptr[2] = ((unsigned int)st->ctab[(v1 + v3) >> 1] & 0x00FF0000) >> 16;
350 ptr[1] = ((unsigned int)st->ctab[(v1 + v3) >> 1] & 0x0000FF00) >> 8;
351 ptr[0] = ((unsigned int)st->ctab[(v1 + v3) >> 1] & 0x000000FF);
354 ptr[2] = ((unsigned int)st->ctab[(v1 + v4) >> 1] & 0x00FF0000) >> 16;
355 ptr[1] = ((unsigned int)st->ctab[(v1 + v4) >> 1] & 0x0000FF00) >> 8;
356 ptr[0] = ((unsigned int)st->ctab[(v1 + v4) >> 1] & 0x000000FF);
357 ptr -= ((st->width - 1) * 3);
360 ptr = last_ptr + (st->xim->bytes_per_line << 1);
366 Flame2Image8(struct state *st)
373 ptr = (unsigned char *)st->xim->data;
374 ptr += (st->top << 1) * st->width;
375 ptr1 = st->flame + 1 + (st->top * (st->fwidth + 2));
377 for(y=st->top;y<st->fheight;y++)
379 for(x=0;x<st->fwidth;x++)
382 v2 = (int)*(ptr1 + 1);
383 v3 = (int)*(ptr1 + st->fwidth + 2);
384 v4 = (int)*(ptr1 + st->fwidth + 2 + 1);
386 *ptr++ = (unsigned char)st->ctab[v1];
387 *ptr = (unsigned char)st->ctab[(v1 + v2) >> 1];
388 ptr += st->width - 1;
389 *ptr++ = (unsigned char)st->ctab[(v1 + v3) >> 1];
390 *ptr = (unsigned char)st->ctab[(v1 + v4) >> 1];
391 ptr -= st->width - 1;
399 Flame2Image1234567(struct state *st)
405 ptr1 = st->flame + 1 + (st->top * (st->fwidth + 2));
407 for( y = st->top; y < st->fheight; y++)
409 for( x = 0; x < st->fwidth; x++)
412 v2 = (int)*(ptr1 + 1);
413 v3 = (int)*(ptr1 + st->fwidth + 2);
414 v4 = (int)*(ptr1 + st->fwidth + 2 + 1);
416 XPutPixel(st->xim,(x << 1), (y << 1), st->ctab[v1]);
417 XPutPixel(st->xim,(x << 1) + 1,(y << 1), st->ctab[(v1 + v2) >> 1]);
418 XPutPixel(st->xim,(x << 1), (y << 1) + 1,st->ctab[(v1 + v3) >> 1]);
419 XPutPixel(st->xim,(x << 1) + 1,(y << 1) + 1,st->ctab[(v1 + v4) >> 1]);
425 Flame2Image(struct state *st)
427 switch (st->xim->bits_per_pixel)
429 case 32: Flame2Image32(st); break;
430 case 24: Flame2Image24(st); break;
431 case 16: Flame2Image16(st); break;
432 case 8: Flame2Image8(st); break;
434 if (st->xim->bits_per_pixel <= 7)
435 Flame2Image1234567(st);
444 FlameActive(struct state *st)
449 ptr1 = st->flame + ((st->fheight + 1) * (st->fwidth + 2));
451 for (x = 0; x < st->fwidth + 2; x++)
454 v1 += ((random() % st->variance) - st->vartrend);
460 v1= (random() % 100);
462 st->residual += (random()%10);
464 st->hspread += (random()%15);
466 st->vspread += (random()%20);
469 st->residual = ((st->iresidual* 10) + (st->residual *90)) / 100;
470 st->hspread = ((st->ihspread * 10) + (st->hspread *90)) / 100;
471 st->vspread = ((st->ivspread * 10) + (st->vspread *90)) / 100;
476 FlameAdvance(struct state *st)
480 int newtop = st->top;
482 for (y = st->fheight + 1; y >= st->top; y--)
485 unsigned char *ptr1 = st->flame + 1 + (y * (st->fwidth + 2));
486 for (x = 0; x < st->fwidth; x++)
493 ptr2 = ptr1 - st->fwidth - 2;
494 v3 = (v1 * st->vspread) >> 8;
500 *(ptr2) = (unsigned char)v2;
501 v3 = (v1 * st->hspread) >> 8;
502 v2 = (int)*(ptr2 + 1);
507 *(ptr2 + 1) = (unsigned char)v2;
508 v2 = (int)*(ptr2 - 1);
513 *(ptr2 - 1) = (unsigned char)v2;
515 if (y < st->fheight + 1)
517 v1 = (v1 * st->residual) >> 8;
518 *ptr1 = (unsigned char)v1;
526 /* clean up the right gutter */
529 v1 = (v1 * st->residual) >> 8;
530 *ptr1 = (unsigned char)v1;
534 st->top = newtop - 1;
542 FlameFill(struct state *st, int val)
545 for (y = 0; y < st->fheight + 1; y++)
547 unsigned char *ptr1 = st->flame + 1 + (y * (st->fwidth + 2));
548 for (x = 0; x < st->fwidth; x++)
558 FlamePasteData(struct state *st,
559 unsigned char *d, int xx, int yy, int w, int h)
561 unsigned char *ptr1,*ptr2;
569 (xx + w <= st->fwidth) &&
570 (yy + h <= st->fheight))
573 for (y = 0; y < h; y++)
575 ptr1 = st->flame + 1 + xx + ((yy + y) * (st->fwidth + 2));
576 for (x = 0; x < w; x++)
579 *ptr1 += random() % (*ptr2 / 24);
588 static Bool warned = False;
591 fprintf (stderr, "%s: st->window is %dx%d; image must be "
592 "smaller than %dx%d (not %dx%d).\n",
593 progname, st->width, st->height, st->fwidth, st->fheight, w, h);
601 double_ximage (Display *dpy, Visual *visual, XImage *image)
604 XImage *out = XCreateImage (dpy, visual, image->depth, ZPixmap, 0, 0,
605 image->width * 2, image->height * 2, 8, 0);
606 out->data = (char *) malloc (out->height * out->bytes_per_line);
607 for (y = 0; y < image->width; y++)
608 for (x = 0; x < image->height; x++)
610 unsigned long p = XGetPixel (image, x, y);
611 XPutPixel (out, x*2, y*2, p);
612 XPutPixel (out, x*2+1, y*2, p);
613 XPutPixel (out, x*2, y*2+1, p);
614 XPutPixel (out, x*2+1, y*2+1, p);
616 XDestroyImage (image);
621 static unsigned char *
622 gaussian_blur (unsigned char *in, int w, int h, double r)
624 unsigned char *out = malloc(w * h);
625 int rs = (int) ((r * 2.57) + 0.5);
628 for (i = 0; i < h; i ++)
629 for (j = 0; j < w; j++)
631 double val = 0, wsum = 0;
633 for (iy = i-rs; iy<i+rs+1; iy++)
634 for (ix = j-rs; ix<j+rs+1; ix++)
636 int x = MIN(w-1, MAX(0, ix));
637 int y = MIN(h-1, MAX(0, iy));
638 int dsq = (ix-j)*(ix-j)+(iy-i)*(iy-i);
639 double wght = exp (-dsq / (2*r*r)) / (M_PI*2*r*r);
640 val += in[y*w+x] * wght;
643 out[i*w+j] = val/wsum;
651 static unsigned char *
652 loadBitmap (struct state *st)
655 unsigned char *result, *o;
659 const char *bitmap_name = "(default)"; /* #### always use builtin */
661 char *bitmap_name = get_string_resource (st->dpy, "bitmap", "Bitmap");
666 !strcmp(bitmap_name, "none"))
668 else if (!strcmp(bitmap_name, "(default)")) /* use the builtin */
669 image = image_data_to_ximage (st->dpy, st->visual,
670 bob_png, sizeof(bob_png));
672 image = file_to_ximage (st->dpy, st->visual, bitmap_name);
674 if (! image) return 0;
676 while (image->width < st->width / 10 &&
677 image->height < st->height / 10)
679 image = double_ximage (st->dpy, st->visual, image);
683 result = (unsigned char *) malloc (image->width * image->height);
685 for (y = 0; y < image->height; y++)
686 for (x = 0; x < image->width; x++)
688 unsigned long agbr = XGetPixel (image, x, image->height - y - 1);
689 unsigned long a = (agbr >> 24) & 0xFF;
690 unsigned long gray = (a == 0
692 : ((((agbr >> 16) & 0xFF) +
693 ((agbr >> 8) & 0xFF) +
694 ((agbr >> 0) & 0xFF))
696 if (gray < 96) gray /= 2; /* a little more contrast */
700 /* If we enlarged the image, file off the sharp edges. */
702 result = gaussian_blur (result, image->width, image->height, blur * 1.7);
704 st->theimx = image->width;
705 st->theimy = image->height;
706 XDestroyImage (image);
712 xflame_init (Display *dpy, Window win)
714 struct state *st = (struct state *) calloc (1, sizeof(*st));
717 st->baseline = get_integer_resource (dpy, "bitmapBaseline", "Integer");
718 st->delay = get_integer_resource (dpy, "delay", "Integer");
725 st->theim = loadBitmap(st);
735 xflame_draw (Display *dpy, Window win, void *closure)
737 struct state *st = (struct state *) closure;
741 FlamePasteData(st, st->theim, (st->fwidth - st->theimx) / 2,
742 st->fheight - st->theimy - st->baseline, st->theimx, st->theimy);
752 xflame_reshape (Display *dpy, Window window, void *closure,
753 unsigned int w, unsigned int h)
755 struct state *st = (struct state *) closure;
760 XClearWindow (dpy, window);
764 xflame_event (Display *dpy, Window window, void *closure, XEvent *event)
770 xflame_free (Display *dpy, Window window, void *closure)
777 static const char *xflame_defaults [] = {
778 ".background: black",
779 ".foreground: #FFAF5F",
782 "*bitmap: (default)",
783 "*bitmapBaseline: 20",
792 #ifdef HAVE_XSHM_EXTENSION
793 "*useSHM: False", /* xshm turns out not to help. */
794 #endif /* HAVE_XSHM_EXTENSION */
798 static XrmOptionDescRec xflame_options [] = {
799 { "-foreground",".foreground", XrmoptionSepArg, 0 },
800 { "-fg", ".foreground", XrmoptionSepArg, 0 },
801 { "-delay", ".delay", XrmoptionSepArg, 0 },
802 { "-bitmap", ".bitmap", XrmoptionSepArg, 0 },
803 { "-baseline", ".bitmapBaseline", XrmoptionSepArg, 0 },
804 { "-hspread", ".hspread", XrmoptionSepArg, 0 },
805 { "-vspread", ".vspread", XrmoptionSepArg, 0 },
806 { "-residual", ".residual", XrmoptionSepArg, 0 },
807 { "-variance", ".variance", XrmoptionSepArg, 0 },
808 { "-vartrend", ".vartrend", XrmoptionSepArg, 0 },
809 { "-bloom", ".bloom", XrmoptionNoArg, "True" },
810 { "-no-bloom", ".bloom", XrmoptionNoArg, "False" },
811 #ifdef HAVE_XSHM_EXTENSION
812 { "-shm", ".useSHM", XrmoptionNoArg, "True" },
813 { "-no-shm", ".useSHM", XrmoptionNoArg, "False" },
814 #endif /* HAVE_XSHM_EXTENSION */
819 XSCREENSAVER_MODULE ("XFlame", xflame)