+ char *cmd = malloc (strlen(dir) * 2 + 100);
+ char *s;
+ strcpy (cmd, "xscreensaver-getimage-file --name ");
+ s = cmd + strlen (cmd);
+ while (*dir) {
+ char c = *dir++;
+ /* put a backslash in front of any character that might confuse sh. */
+ if (! ((c >= 'a' && c <= 'z') ||
+ (c >= 'A' && c <= 'Z') ||
+ (c >= '0' && c <= '9') ||
+ c == '.' || c == '_' || c == '-' || c == '+' || c == '/'))
+ *s++ = '\\';
+ *s++ = c;
+ }
+ *s = 0;
+
+ FILE *pipe = popen (cmd, "r");
+ free (cmd);
+ return pipe;
+}
+
+
+struct pipe_closure {
+ FILE *pipe;
+ XtInputId id;
+ Screen *screen;
+ Window xwindow;
+ Drawable drawable;
+ void (*callback) (Screen *, Window, Drawable,
+ const char *name, XRectangle *geom,
+ void *closure);
+ void *closure;
+};
+
+
+static void
+pipe_cb (XtPointer closure, int *source, XtInputId *id)
+{
+ /* This is not called from a signal handler, so doing stuff here is fine.
+ */
+ struct pipe_closure *clo2 = (struct pipe_closure *) closure;
+ char buf[10240];
+ fgets (buf, sizeof(buf)-1, clo2->pipe);
+ pclose (clo2->pipe);
+ clo2->pipe = 0;
+ XtRemoveInput (clo2->id);
+ clo2->id = 0;
+
+ /* strip trailing newline */
+ int L = strlen(buf);
+ while (L > 0 && (buf[L-1] == '\r' || buf[L-1] == '\n'))
+ buf[--L] = 0;
+
+ Display *dpy = DisplayOfScreen (clo2->screen);
+ XRectangle geom;
+
+ if (! osx_load_image_file (clo2->screen, clo2->xwindow, clo2->drawable,
+ buf, &geom)) {
+ /* unable to load image - draw colorbars
+ */
+ XWindowAttributes xgwa;
+ XGetWindowAttributes (dpy, clo2->xwindow, &xgwa);
+ Window r;
+ int x, y;
+ unsigned int w, h, bbw, d;
+ XGetGeometry (dpy, clo2->drawable, &r, &x, &y, &w, &h, &bbw, &d);
+ draw_colorbars (clo2->screen, xgwa.visual, clo2->drawable, xgwa.colormap,
+ 0, 0, w, h);
+ geom.x = geom.y = 0;
+ geom.width = w;
+ geom.height = h;
+ }
+
+ clo2->callback (clo2->screen, clo2->xwindow, clo2->drawable, buf, &geom,
+ clo2->closure);
+ clo2->callback = 0;
+ free (clo2);