+ struct state *st = (struct state *) calloc (1, sizeof(*st));
+ XWindowAttributes xgwa;
+ Visual *visual;
+ XGCValues gcv;
+ long gcflags;
+
+ st->dpy = dpy;
+ st->window = window;
+ XGetWindowAttributes (st->dpy, st->window, &xgwa);
+ st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
+ st->window, 0, 0);
+ st->start_time = time ((time_t) 0);
+
+ visual = xgwa.visual;
+ st->max_width = xgwa.width;
+ st->max_height = xgwa.height;
+
+ st->delay = get_integer_resource (st->dpy, "delay", "Integer");
+ st->delay2 = get_integer_resource (st->dpy, "delay2", "Integer");
+ st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
+ st->grid_size = get_integer_resource (st->dpy, "gridSize", "Integer");
+ st->pix_inc = get_integer_resource (st->dpy, "pixelIncrement", "Integer");
+
+ /* Don't let the grid be smaller than 5x5 */
+ while (st->grid_size > xgwa.width / 5)
+ st->grid_size /= 2;
+ while (st->grid_size > xgwa.height / 5)
+ st->grid_size /= 2;
+
+ if (st->delay < 0) st->delay = 0;
+ if (st->delay2 < 0) st->delay2 = 0;
+ if (st->duration < 1) st->duration = 1;
+ if (st->pix_inc < 1) st->pix_inc = 1;
+ if (st->grid_size < 1) st->grid_size = 1;
+
+
+ {
+ XColor fgc, bgc;
+ char *fgs = get_string_resource(st->dpy, "background", "Background");
+ char *bgs = get_string_resource(st->dpy, "foreground", "Foreground");
+ Bool fg_ok, bg_ok;
+ if (!XParseColor (st->dpy, xgwa.colormap, fgs, &fgc))
+ XParseColor (st->dpy, xgwa.colormap, "black", &bgc);
+ if (!XParseColor (st->dpy, xgwa.colormap, bgs, &bgc))
+ XParseColor (st->dpy, xgwa.colormap, "gray", &fgc);
+
+ fg_ok = XAllocColor (st->dpy, xgwa.colormap, &fgc);
+ bg_ok = XAllocColor (st->dpy, xgwa.colormap, &bgc);
+
+ /* If we weren't able to allocate the two colors we want from the
+ colormap (which is likely if the screen has been grabbed on an
+ 8-bit SGI visual -- don't ask) then just go through the map
+ and find the closest color to the ones we wanted, and use those
+ pixels without actually allocating them.
+ */
+ if (fg_ok)
+ st->fg = fgc.pixel;
+ else
+ st->fg = 0;
+
+ if (bg_ok)
+ st->bg = bgc.pixel;
+ else
+ st->bg = 1;
+
+#ifndef HAVE_COCOA
+ if (!fg_ok || bg_ok)
+ {
+ int i;
+ unsigned long fgd = ~0;
+ unsigned long bgd = ~0;
+ int max = visual_cells (xgwa.screen, xgwa.visual);
+ XColor *all = (XColor *) calloc(sizeof (*all), max);
+ for (i = 0; i < max; i++)
+ {
+ all[i].flags = DoRed|DoGreen|DoBlue;
+ all[i].pixel = i;
+ }
+ XQueryColors (st->dpy, xgwa.colormap, all, max);
+ for(i = 0; i < max; i++)
+ {
+ long rd, gd, bd;
+ unsigned long dd;
+ if (!fg_ok)
+ {
+ rd = (all[i].red >> 8) - (fgc.red >> 8);
+ gd = (all[i].green >> 8) - (fgc.green >> 8);
+ bd = (all[i].blue >> 8) - (fgc.blue >> 8);
+ if (rd < 0) rd = -rd;
+ if (gd < 0) gd = -gd;
+ if (bd < 0) bd = -bd;
+ dd = (rd << 1) + (gd << 2) + bd;
+ if (dd < fgd)
+ {
+ fgd = dd;
+ st->fg = all[i].pixel;
+ if (dd == 0)
+ fg_ok = True;
+ }
+ }
+
+ if (!bg_ok)
+ {
+ rd = (all[i].red >> 8) - (bgc.red >> 8);
+ gd = (all[i].green >> 8) - (bgc.green >> 8);
+ bd = (all[i].blue >> 8) - (bgc.blue >> 8);
+ if (rd < 0) rd = -rd;
+ if (gd < 0) gd = -gd;
+ if (bd < 0) bd = -bd;
+ dd = (rd << 1) + (gd << 2) + bd;
+ if (dd < bgd)
+ {
+ bgd = dd;
+ st->bg = all[i].pixel;
+ if (dd == 0)
+ bg_ok = True;
+ }
+ }
+
+ if (fg_ok && bg_ok)
+ break;
+ }
+ XFree(all);
+ }
+#endif /* !HAVE_COCOA */
+ }
+
+ gcv.foreground = st->fg;
+ gcv.function = GXcopy;
+ gcv.subwindow_mode = IncludeInferiors;
+ gcflags = GCForeground |GCFunction;
+ if (use_subwindow_mode_p(xgwa.screen, st->window)) /* see grabscreen.c */
+ gcflags |= GCSubwindowMode;
+ st->gc = XCreateGC (st->dpy, st->window, gcflags, &gcv);
+
+ return st;