+
+static inline unsigned char addbs(unsigned char c, unsigned int i)
+{
+ i += c;
+ return(i > 255 ? 255 : i);
+}
+
+static void chromo_2x2_light(struct state *st)
+{
+ unsigned int n, x, y, v = 0;
+ unsigned int nl = st->width * 4;
+ unsigned char *mem = st->palaka2;
+ float r, g, b;
+ float rgb[SHELLCOUNT*4];
+ fireshell *fs = st->fireshell_array;
+
+ for (n = 0, x = 0; n < SHELLCOUNT; n++, x += 4, fs++)
+ {
+ rgb[x ] = fs->flash_r;
+ rgb[x+1] = fs->flash_g;
+ rgb[x+2] = fs->flash_b;
+ }
+
+ for (y = st->height/2; y; y--)
+ {
+ for (x = st->width/2; x; x--, v += 4)
+ {
+ r = rgb[0] * st->light_map[v] + rgb[4] * st->light_map[v+1]
+ + rgb[ 8] * st->light_map[v+2] + rgb[12] * st->light_map[v+3];
+ g = rgb[1] * st->light_map[v] + rgb[5] * st->light_map[v+1]
+ + rgb[ 9] * st->light_map[v+2] + rgb[13] * st->light_map[v+3];
+ b = rgb[2] * st->light_map[v] + rgb[6] * st->light_map[v+1]
+ + rgb[10] * st->light_map[v+2] + rgb[14] * st->light_map[v+3];
+
+ mem[0] = addbs(mem[0], b);
+ mem[1] = addbs(mem[1], g);
+ mem[2] = addbs(mem[2], r);
+ mem[4] = addbs(mem[4], b);
+ mem[5] = addbs(mem[5], g);
+ mem[6] = addbs(mem[6], r);
+
+ mem += nl;
+
+ mem[0] = addbs(mem[0], b);
+ mem[1] = addbs(mem[1], g);
+ mem[2] = addbs(mem[2], r);
+ mem[4] = addbs(mem[4], b);
+ mem[5] = addbs(mem[5], g);
+ mem[6] = addbs(mem[6], r);
+
+ mem -= nl - 8;
+ }
+ mem += nl;
+ }
+}
+
+#endif
+
+static void resize(struct state *st)
+{
+ unsigned int n;
+ fireshell *fs = st->fireshell_array;
+ XWindowAttributes xwa;
+ XGetWindowAttributes (st->dpy, st->window, &xwa);
+ xwa.width -= xwa.width % 4;
+ xwa.height -= xwa.height % 2;
+ st->width = xwa.width;
+ st->height = xwa.height;
+ if (st->verbose)
+ {
+ printf("resolution: %d x %d \n",st->width,st->height);
+ }
+ XSync(st->dpy, 0);
+ if (st->xim)
+ {
+ if (st->xim->data == (char *)st->palaka2) st->xim->data = NULL;
+ XDestroyImage(st->xim);
+ XSync(st->dpy, 0);
+ free(st->mem2);
+ free(st->mem1);
+ }
+ st->xim = XCreateImage(st->dpy, xwa.visual, xwa.depth, ZPixmap, 0, 0,
+ st->width, st->height, 8, 0);
+ if (!st->xim) return;
+
+#ifdef __SSE2___ABANDONED /* causes __ERROR_use_memset_not_bzero_in_xscreensaver__ */
+ st->mem1 = _mm_malloc(((st->height + 2) * st->width + 8)*4, 16);
+ bzero(st->mem1, ((st->height + 2) * st->width + 8)*4);
+ st->mem2 = _mm_malloc(((st->height + 2) * st->width + 8)*4, 16);
+ bzero(st->mem2, ((st->height + 2) * st->width + 8)*4);
+#else
+ st->mem1 = calloc((st->height + 2) * st->width + 8, 4);
+ st->mem2 = calloc((st->height + 2) * st->width + 8, 4);
+#endif
+ st->palaka1 = (unsigned char *) st->mem1 + (st->width * 4 + 16);
+ st->palaka2 = (unsigned char *) st->mem2 + (st->width * 4 + 16);
+
+ if (xwa.depth >= 24)
+ {
+ st->xim->data = (char *)st->palaka2;
+ }
+ else
+ {
+ st->xim->data = calloc(st->height, st->xim->bytes_per_line);
+ }
+
+ if (st->light_map) free(st->light_map);
+ st->light_map = calloc((st->width * st->height * SHELLCOUNT)/4, sizeof(float));
+ for (n = 0; n < SHELLCOUNT; n++, fs++)
+ {
+ render_light_map(st, fs);
+ }
+}
+
+static void put_image(struct state *st)
+{
+ int x,y,i,j;
+ unsigned char r, g, b;
+ if (!st->xim) return;
+ i = 0;
+ j = 0;
+ if (st->depth==16)
+ {
+ if(st->bigendian)
+ for (y=0; y<st->xim->height; y++)
+ for (x=0; x<st->xim->width; x++)
+ {
+ r = st->palaka2[j++];
+ g = st->palaka2[j++];
+ b = st->palaka2[j++];
+ j++;
+ st->xim->data[i++] = (g&224)>>5 | (r&248);
+ st->xim->data[i++] = (b&248)>>3 | (g&28)<<3;
+ }
+ else
+ for (y=0; y<st->xim->height; y++)
+ for (x=0; x<st->xim->width; x++)
+ {
+ r = st->palaka2[j++];
+ g = st->palaka2[j++];
+ b = st->palaka2[j++];
+ j++;
+ st->xim->data[i++] = (b&248)>>3 | (g&28)<<3;
+ st->xim->data[i++] = (g&224)>>5 | (r&248);
+ }
+ }
+ if (st->depth==15)
+ {
+ if(st->bigendian)
+ for (y=0; y<st->xim->height; y++)
+ for (x=0; x<st->xim->width; x++)
+ {
+ r = st->palaka2[j++];
+ g = st->palaka2[j++];
+ b = st->palaka2[j++];
+ j++;
+ st->xim->data[i++] = (g&192)>>6 | (r&248)>>1;
+ st->xim->data[i++] = (b&248)>>3 | (g&56)<<2;
+ }
+ else
+ for (y=0; y<st->xim->height; y++)
+ for (x=0; x<st->xim->width; x++)
+ {
+ r = st->palaka2[j++];
+ g = st->palaka2[j++];
+ b = st->palaka2[j++];
+ j++;
+ st->xim->data[i++] = (b&248)>>3 | (g&56)<<2;
+ st->xim->data[i++] = (g&192)>>6 | (r&248)>>1;
+ }
+ }
+ if (st->depth==8)
+ {
+ for (y=0; y<st->xim->height; y++)
+ for (x=0; x<st->xim->width; x++)
+ {
+ r = st->palaka2[j++];
+ g = st->palaka2[j++];
+ b = st->palaka2[j++];
+ j++;
+ st->xim->data[i++] = (((7*g)/256)*36)+(((6*r)/256)*6)+((6*b)/256);
+ }
+ }
+ XPutImage(st->dpy,st->window,st->gc,st->xim,0,0,0,0,st->xim->width,st->xim->height);
+}
+
+static void *
+fireworkx_init (Display *dpy, Window win)
+{
+ struct state *st = (struct state *) calloc (1, sizeof(*st));
+ unsigned int n;
+ Visual *vi;
+ Colormap cmap;
+ Bool writable;
+ XWindowAttributes xwa;
+ XGCValues gcv;
+ firepix *fp;
+ fireshell *fs;
+
+ st->dpy = dpy;
+ st->window = win;
+ st->xim = NULL;
+ st->flash_on = 1;
+ st->shoot = 0;
+ st->width = 0;
+ st->height = 0;
+ st->max_shell_life = SHELL_LIFE_DEFAULT;
+ st->flash_fade = 0.995;
+ st->light_map = NULL;
+ st->palaka1 = NULL;
+ st->palaka2 = NULL;
+
+ st->flash_on = get_boolean_resource(st->dpy, "flash" , "Boolean");
+ st->shoot = get_boolean_resource(st->dpy, "shoot" , "Boolean");
+ st->verbose = get_boolean_resource(st->dpy, "verbose" , "Boolean");
+ st->max_shell_life = get_integer_resource(st->dpy, "maxlife" , "Integer");
+ /* transition from xscreensaver <= 5.20 */
+ if (st->max_shell_life > 100) st->max_shell_life = 100;
+
+ st->delay = get_integer_resource(st->dpy, "delay" , "Integer");
+
+ st->max_shell_life = pow(10.0,(st->max_shell_life/50.0)+2.7);
+ if(st->max_shell_life < 1000) st->flash_fade = 0.998;
+
+ if(st->verbose)
+ {
+ printf("Fireworkx %s - Pyrotechnics explosions simulation \n", FWXVERSION);
+ printf("Copyright (GPL) 1999-2013 Rony B Chandran <ronybc@gmail.com> \n\n");
+ printf("url: http://www.ronybc.com \n\n");
+ printf("Life = %u\n", st->max_shell_life);
+#ifdef __SSE2__
+ printf("Using SSE2 optimization.\n");
+#endif
+ }
+
+ XGetWindowAttributes(st->dpy,win,&xwa);
+ st->depth = xwa.depth;
+ vi = xwa.visual;
+ cmap = xwa.colormap;
+ st->bigendian = (ImageByteOrder(st->dpy) == MSBFirst);
+
+ if(st->depth==8)
+ {
+ st->colors = (XColor *) calloc(sizeof(XColor),st->ncolors+1);
+ writable = False;
+ make_smooth_colormap(xwa.screen, vi, cmap,
+ st->colors, &st->ncolors,
+ False, &writable, True);
+ }
+ st->gc = XCreateGC(st->dpy, win, 0, &gcv);
+
+ fs = calloc(SHELLCOUNT, sizeof(fireshell));
+ fp = calloc(PIXCOUNT * SHELLCOUNT, sizeof(firepix));
+ st->fireshell_array = fs;
+
+ XGetWindowAttributes (st->dpy, st->window, &xwa);
+ st->depth = xwa.depth;
+
+ resize(st); /* initialize palakas */
+
+ for (n = 0; n < SHELLCOUNT; n++, fs++)
+ {
+ fs->seq_number = n;
+ fs->fpix = fp;
+ recycle (st, fs, rnd(st->width), rnd(st->height));
+ fp += PIXCOUNT;
+ }
+
+ return st;
+}
+
+static unsigned long
+fireworkx_draw (Display *dpy, Window win, void *closure)
+{
+ struct state *st = (struct state *) closure;
+ fireshell *fs;
+ unsigned int n, q;
+ for (q = FTWEAK; q; q--)
+ {
+ fs = st->fireshell_array;
+ for (n = 0; n < SHELLCOUNT; n++, fs++)
+ {
+ if (!explode(st, fs))
+ {
+ recycle(st, fs, rnd(st->width), rnd(st->height));
+ }
+ }
+ }
+
+ glow_blur(st);
+
+ if (st->flash_on)
+ {
+ chromo_2x2_light(st);
+ }
+
+ put_image(st);
+ return st->delay;
+}
+
+static void
+fireworkx_reshape (Display *dpy, Window window, void *closure,
+ unsigned int w, unsigned int h)
+{
+ struct state *st = (struct state *) closure;
+ st->width = w;
+ st->height = h;
+ resize(st);
+}
+
+static Bool
+fireworkx_event (Display *dpy, Window window, void *closure, XEvent *event)
+{
+ struct state *st = (struct state *) closure;
+ if (event->type == ButtonPress)
+ {
+ recycle_oldest(st, event->xbutton.x, event->xbutton.y);
+ return True;
+ }
+ return False;
+}
+
+static void
+fireworkx_free (Display *dpy, Window window, void *closure)
+{
+ struct state *st = (struct state *) closure;
+ free(st->mem2);
+ free(st->mem1);
+ free(st->fireshell_array->fpix);
+ free(st->fireshell_array);
+}
+
+static const char *fireworkx_defaults [] =
+{
+ ".background: black",
+ ".foreground: white",
+ "*delay: 10000", /* never default to zero! */
+ "*maxlife: 32",
+ "*flash: True",
+ "*shoot: False",
+ "*verbose: False",
+ 0
+};
+
+static XrmOptionDescRec fireworkx_options [] =
+{
+ { "-delay", ".delay", XrmoptionSepArg, 0 },
+ { "-maxlife", ".maxlife", XrmoptionSepArg, 0 },
+ { "-no-flash", ".flash", XrmoptionNoArg, "False" },
+ { "-shoot", ".shoot", XrmoptionNoArg, "True" },
+ { "-verbose", ".verbose", XrmoptionNoArg, "True" },
+ { 0, 0, 0, 0 }
+};
+
+XSCREENSAVER_MODULE ("Fireworkx", fireworkx)