+ return True;
+}
+
+
+#elif defined(USE_IPHONE)
+
+ /* What a hack!
+
+ On iOS, our application delegate, SaverRunner, grabs an image
+ of itself as a UIImage before mapping the XScreenSaverView.
+ In this code, we ask SaverRunner for that UIImage, then copy
+ it to the root window.
+ */
+
+Bool
+osx_grab_desktop_image (Screen *screen, Window xwindow, Drawable drawable,
+ XRectangle *geom_ret)
+{
+ SaverRunner *s =
+ (SaverRunner *) [[UIApplication sharedApplication] delegate];
+ if (! s)
+ return False;
+ if (! [s isKindOfClass:[SaverRunner class]])
+ return False;
+ UIImage *img = [s screenshot];
+ if (! img)
+ return False;
+ jwxyz_draw_NSImage_or_CGImage (DisplayOfScreen (screen), drawable,
+ True, img, geom_ret, 0);
+ return True;
+}
+
+
+#else /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
+
+ 10.5+ code.
+
+ This version of the code is simpler and more reliable, but
+ uses an API that only exist on 10.5 and newer, so we can only
+ use it if when being compiled against the 10.5 SDK or later.
+ */
+
+/* Loads an image into the Drawable, returning once the image is loaded.
+ */
+Bool
+osx_grab_desktop_image (Screen *screen, Window xwindow, Drawable drawable,
+ XRectangle *geom_ret)
+{
+ Display *dpy = DisplayOfScreen (screen);
+ NSView *nsview = jwxyz_window_view (xwindow);
+ XWindowAttributes xgwa;
+ int window_x, window_y;
+ Window unused;
+
+ // Figure out where this window is on the screen.
+ //
+ XGetWindowAttributes (dpy, xwindow, &xgwa);
+ XTranslateCoordinates (dpy, xwindow, RootWindowOfScreen (screen), 0, 0,
+ &window_x, &window_y, &unused);
+
+ // Grab only the rectangle of the screen underlying this window.
+ //
+ CGRect cgrect;
+ cgrect.origin.x = window_x;
+ cgrect.origin.y = window_y;
+ cgrect.size.width = xgwa.width;
+ cgrect.size.height = xgwa.height;
+
+ /* If a password is required to unlock the screen, a large black
+ window will be on top of all of the desktop windows by the time
+ we reach here, making the screen-grab rather uninteresting. If
+ we move ourselves temporarily below the login-window windows
+ before capturing the image, we capture the real desktop as
+ intended.
+ */
+
+ // save our current level so we can restore it later
+ int oldLevel = [[nsview window] level];
+
+ [[nsview window] setLevel:CGWindowLevelForKey(kCGPopUpMenuWindowLevelKey)];
+
+ // Grab a screen shot of those windows below this one
+ // (hey, X11 can't do that!)
+ //
+ CGImageRef img =
+ CGWindowListCreateImage (cgrect,
+ kCGWindowListOptionOnScreenBelowWindow,
+ [[nsview window] windowNumber],
+ kCGWindowImageDefault);
+
+ // put us back above the login windows so the screensaver is visible.
+ [[nsview window] setLevel:oldLevel];
+
+ if (! img) return False;
+
+ // Render the grabbed CGImage into the Drawable.
+ jwxyz_draw_NSImage_or_CGImage (DisplayOfScreen (screen), drawable,
+ False, img, geom_ret, 0);
+ CGImageRelease (img);
+ return True;