+}
+
+#elif defined (HAVE_COCOA) /* OSX or iOS */
+
+# ifndef USE_IPHONE /* HAVE_COCOA && !USE_IPHONE -- desktop OSX */
+
+# define BACKSLASH(c) \
+ (! ((c >= 'a' && c <= 'z') || \
+ (c >= 'A' && c <= 'Z') || \
+ (c >= '0' && c <= '9') || \
+ c == '.' || c == '_' || c == '-' || c == '+' || c == '/'))
+
+/* Gets the name of an image file to load by running xscreensaver-getimage-file
+ at the end of a pipe. This can be very slow!
+ */
+static FILE *
+open_image_name_pipe (const char *dir)
+{
+ char *s;
+
+ /* /bin/sh on OS X 10.10 wipes out the PATH. */
+ const char *path = getenv("PATH");
+ char *cmd = s = malloc ((strlen(dir) + strlen(path)) * 2 + 100);
+ strcpy (s, "/bin/sh -c 'export PATH=");
+ s += strlen (s);
+ while (*path) {
+ char c = *path++;
+ if (BACKSLASH(c)) *s++ = '\\';
+ *s++ = c;
+ }
+ strcpy (s, "; ");
+ s += strlen (s);
+
+ strcpy (s, "xscreensaver-getimage-file --name ");
+ s += strlen (s);
+ while (*dir) {
+ char c = *dir++;
+ if (BACKSLASH(c)) *s++ = '\\';
+ *s++ = c;
+ }
+
+ strcpy (s, "'");
+ s += strlen (s);
+
+ *s = 0;
+
+ FILE *pipe = popen (cmd, "r");
+ free (cmd);
+ return pipe;
+}
+
+
+static void
+xscreensaver_getimage_file_cb (XtPointer closure, int *source, XtInputId *id)
+{
+ /* This is not called from a signal handler, so doing stuff here is fine.
+ */
+ xscreensaver_getimage_data *clo2 = (xscreensaver_getimage_data *) closure;
+ char buf[10240];
+ const char *dir = clo2->directory;
+ char *absfile = 0;
+ *buf = 0;
+ fgets (buf, sizeof(buf)-1, clo2->pipe);
+ pclose (clo2->pipe);
+ clo2->pipe = 0;
+ XtRemoveInput (clo2->pipe_id);
+ clo2->pipe_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 (*buf && *buf != '/') /* pathname is relative to dir. */
+ {
+ absfile = malloc (strlen(dir) + strlen(buf) + 10);
+ strcpy (absfile, dir);
+ if (dir[strlen(dir)-1] != '/')
+ strcat (absfile, "/");
+ strcat (absfile, buf);
+ }
+
+ if (! osx_load_image_file (clo2->screen, clo2->window, clo2->drawable,
+ (absfile ? absfile : buf), &geom)) {
+ /* unable to load image - draw colorbars
+ */
+ XWindowAttributes xgwa;
+ XGetWindowAttributes (dpy, clo2->window, &xgwa);
+ Window r;
+ int x, y;
+ unsigned int w, h, bbw, d;
+ struct stat st;
+
+ /* Log something to syslog so we can tell the difference between
+ corrupted images and broken symlinks. */
+ if (!*buf)
+ fprintf (stderr, "%s: no image filename found\n", progname);
+ else if (! stat (buf, &st))
+ fprintf (stderr, "%s: %s: unparsable\n", progname, buf);
+ else
+ {
+ char buf2[2048];
+ sprintf (buf2, "%.255s: %.1024s", progname, buf);
+ perror (buf2);
+ }
+
+ 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, 0, 0); /* #### logo missing */
+ geom.x = geom.y = 0;
+ geom.width = w;
+ geom.height = h;
+ }
+
+ /* Take the extension off of the file name. */
+ /* Duplicated in driver/xscreensaver-getimage.c. */
+ if (*buf)
+ {
+ char *slash = strrchr (buf, '/');
+ char *dot = strrchr ((slash ? slash : buf), '.');
+ if (dot) *dot = 0;
+ /* Replace slashes with newlines */
+ /* while (dot = strchr(buf, '/')) *dot = '\n'; */
+ /* Replace slashes with spaces */
+ /* while ((dot = strchr(buf, '/'))) *dot = ' '; */
+ }
+
+ if (absfile) free (absfile);
+ clo2->callback (clo2->screen, clo2->window, clo2->drawable, buf, &geom,
+ clo2->closure);
+ clo2->callback = 0;
+ free (clo2->directory);
+ free (clo2);
+}
+
+
+# else /* HAVE_COCOA && USE_IPHONE -- iOS */
+
+/* Callback for ios_load_random_image(), called after we have loaded an
+ image from the iOS device's Photo Library. See grabclient-ios.m.
+ */
+static void
+ios_load_random_image_cb (void *uiimage, const char *filename,
+ int width, int height, void *closure)
+{
+ xscreensaver_getimage_data *clo2 = (xscreensaver_getimage_data *) closure;
+ Display *dpy = DisplayOfScreen (clo2->screen);
+ XRectangle geom;
+ XWindowAttributes xgwa;
+ Window r;
+ int x, y;
+ unsigned int w, h, bbw, d;
+ int rot = 0;
+
+ XGetWindowAttributes (dpy, clo2->window, &xgwa);
+ XGetGeometry (dpy, clo2->drawable, &r, &x, &y, &w, &h, &bbw, &d);
+
+ /* If the image is portrait and the window is landscape, or vice versa,
+ rotate the image. The idea is to fill up as many pixels as possible,
+ and assume the user will just rotate their phone until it looks right.
+ This makes "decayscreen", etc. much more easily viewable.
+ */
+ if (get_boolean_resource (dpy, "rotateImages", "RotateImages")) {
+ if ((width > height) != (w > h))
+ rot = 5;
+ }
+
+ if (uiimage)
+ {
+ jwxyz_draw_NSImage_or_CGImage (DisplayOfScreen (clo2->screen),
+ clo2->drawable,
+ True, uiimage, &geom,
+ rot);
+ }
+ else /* Probably means no images in the gallery. */
+ {
+ draw_colorbars (clo2->screen, xgwa.visual, clo2->drawable, xgwa.colormap,
+ 0, 0, w, h, 0, 0); /* #### logo missing */
+ geom.x = geom.y = 0;
+ geom.width = w;
+ geom.height = h;
+ filename = 0;
+ }
+
+ clo2->callback (clo2->screen, clo2->window, clo2->drawable,
+ filename, &geom, clo2->closure);
+ clo2->callback = 0;
+ free (clo2);
+}
+
+# endif /* HAVE_COCOA && USE_IPHONE */
+
+
+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)
+{
+ xscreensaver_getimage_data *clo2 =
+ (xscreensaver_getimage_data *) calloc (1, sizeof(*clo2));
+
+ clo2->screen = screen;
+ clo2->window = xwindow;
+ clo2->drawable = drawable;
+ clo2->callback = callback;
+ clo2->closure = closure;
+
+# ifndef USE_IPHONE /* Desktop OSX */
+ clo2->directory = strdup (dir);
+ clo2->pipe = open_image_name_pipe (dir);
+ clo2->pipe_id = XtAppAddInput (XtDisplayToApplicationContext (
+ DisplayOfScreen (screen)),
+ fileno (clo2->pipe),
+ (XtPointer) (XtInputReadMask | XtInputExceptMask),
+ xscreensaver_getimage_file_cb, (XtPointer) clo2);
+# else /* USE_IPHONE */
+ {
+ XWindowAttributes xgwa;
+ XGetWindowAttributes (DisplayOfScreen (screen), xwindow, &xgwa);
+ ios_load_random_image (ios_load_random_image_cb, clo2,
+ xgwa.width, xgwa.height);
+ }
+# endif /* USE_IPHONE */
+}
+
+
+/* Loads an image into the Drawable, returning once the image is loaded.
+ */
+static void
+load_random_image_cocoa (Screen *screen, Window window, Drawable drawable,
+ void (*callback) (Screen *, Window, Drawable,
+ const char *name, XRectangle *geom,
+ void *closure),
+ void *closure)
+{
+ Display *dpy = DisplayOfScreen (screen);
+ XWindowAttributes xgwa;
+ Bool deskp = get_boolean_resource (dpy, "grabDesktopImages", "Boolean");
+ Bool filep = get_boolean_resource (dpy, "chooseRandomImages", "Boolean");
+ const char *dir = 0;
+ Bool done = False;
+ XRectangle geom;
+ char *name = 0;
+
+ if (!drawable) abort();
+
+ XGetWindowAttributes (dpy, window, &xgwa);
+ {
+ Window r;
+ int x, y;
+ unsigned int w, h, bbw, d;
+ XGetGeometry (dpy, drawable, &r, &x, &y, &w, &h, &bbw, &d);
+ xgwa.width = w;
+ xgwa.height = h;
+ }
+
+ geom.x = 0;
+ geom.y = 0;
+ geom.width = xgwa.width;
+ geom.height = xgwa.height;
+
+# ifndef USE_IPHONE
+ if (filep)
+ dir = get_string_resource (dpy, "imageDirectory", "ImageDirectory");
+
+ if (!dir || !*dir)
+ filep = False;
+# endif /* ! USE_IPHONE */