+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);
+}
+
+
+static void
+osx_load_image_file_async (Screen *screen, Window xwindow, Drawable drawable,
+ const char *dir,
+ void (*callback) (Screen *, Window, Drawable,
+ const char *name,
+ XRectangle *geom,
+ void *closure),
+ void *closure)
+{
+#if 0 /* do it synchronously */
+
+ FILE *pipe = open_image_name_pipe (dir);
+ char buf[10240];
+ *buf = 0;
+ fgets (buf, sizeof(buf)-1, pipe);
+ pclose (pipe);
+
+ /* strip trailing newline */
+ int L = strlen(buf);
+ while (L > 0 && (buf[L-1] == '\r' || buf[L-1] == '\n'))
+ buf[--L] = 0;
+
+ XRectangle geom;
+ if (! osx_load_image_file (screen, xwindow, drawable, buf, &geom)) {
+ /* draw colorbars */
+ abort();
+ }
+ callback (screen, xwindow, drawable, buf, &geom, closure);
+
+#else /* do it asynchronously */
+
+ Display *dpy = DisplayOfScreen (screen);
+ struct pipe_closure *clo2 = (struct pipe_closure *) calloc (1, sizeof(*clo2));
+ clo2->pipe = open_image_name_pipe (dir);
+ clo2->id = XtAppAddInput (XtDisplayToApplicationContext (dpy),
+ fileno (clo2->pipe),
+ (XtPointer) (XtInputReadMask | XtInputExceptMask),
+ pipe_cb, (XtPointer) clo2);
+ clo2->screen = screen;
+ clo2->xwindow = xwindow;
+ clo2->drawable = drawable;
+ clo2->callback = callback;
+ clo2->closure = closure;
+#endif
+}
+