+ // Create a graphics context with the target size
+ // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to
+ // take the scale into consideration
+ // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
+
+ if (UIGraphicsBeginImageContextWithOptions)
+ UIGraphicsBeginImageContextWithOptions (size, NO, 0);
+ else
+ UIGraphicsBeginImageContext (size);
+
+ CGContextRef ctx = UIGraphicsGetCurrentContext();
+
+
+ // Rotate the graphics context to match current hardware rotation.
+ //
+ switch (orient) {
+ case UIInterfaceOrientationPortraitUpsideDown:
+ CGContextTranslateCTM (ctx, [window center].x, [window center].y);
+ CGContextRotateCTM (ctx, M_PI);
+ CGContextTranslateCTM (ctx, -[window center].x, -[window center].y);
+ break;
+ case UIInterfaceOrientationLandscapeLeft:
+ case UIInterfaceOrientationLandscapeRight:
+ CGContextTranslateCTM (ctx,
+ ([window frame].size.height -
+ [window frame].size.width) / 2,
+ ([window frame].size.width -
+ [window frame].size.height) / 2);
+ CGContextTranslateCTM (ctx, [window center].x, [window center].y);
+ CGContextRotateCTM (ctx,
+ (orient == UIInterfaceOrientationLandscapeLeft
+ ? M_PI/2
+ : -M_PI/2));
+ CGContextTranslateCTM (ctx, -[window center].x, -[window center].y);
+ break;
+ default:
+ break;
+ }
+
+ // Iterate over every window from back to front
+ //
+ for (UIWindow *win in [[UIApplication sharedApplication] windows]) {
+ if (![win respondsToSelector:@selector(screen)] ||
+ [win screen] == [UIScreen mainScreen]) {
+
+ // -renderInContext: renders in the coordinate space of the layer,
+ // so we must first apply the layer's geometry to the graphics context
+ CGContextSaveGState (ctx);
+
+ // Center the context around the window's anchor point
+ CGContextTranslateCTM (ctx, [win center].x, [win center].y);
+
+ // Apply the window's transform about the anchor point
+ CGContextConcatCTM (ctx, [win transform]);
+
+ // Offset by the portion of the bounds left of and above anchor point
+ CGContextTranslateCTM (ctx,
+ -[win bounds].size.width * [[win layer] anchorPoint].x,
+ -[win bounds].size.height * [[win layer] anchorPoint].y);
+
+ // Render the layer hierarchy to the current context
+ [[win layer] renderInContext:ctx];
+
+ // Restore the context
+ CGContextRestoreGState (ctx);
+ }
+ }
+
+ if (saved_screenshot)
+ [saved_screenshot release];
+ saved_screenshot = [UIGraphicsGetImageFromCurrentImageContext() retain];
+
+ UIGraphicsEndImageContext();
+}
+
+
+- (void) openPreferences: (NSString *) saver
+{
+ [self loadSaver:saver launch:NO];
+ if (! saverView) return;
+
+ NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
+ [prefs setObject:saver forKey:@"selectedSaverName"];
+ [prefs synchronize];
+
+ [rootViewController pushViewController: [saverView configureView]
+ animated:YES];
+}
+
+
+#endif // USE_IPHONE
+
+
+
+- (void)loadSaver:(NSString *)name launch:(BOOL)launch
+{
+ // NSLog (@"selecting saver \"%@\"", name);
+
+# ifndef USE_IPHONE
+
+ if (saverName && [saverName isEqualToString: name]) {
+ if (launch)
+ for (NSWindow *win in windows) {
+ ScreenSaverView *sv = find_saverView ([win contentView]);
+ if (![sv isAnimating])
+ [sv startAnimation];
+ }
+ return;
+ }
+
+ saverName = name;
+
+ for (NSWindow *win in windows) {
+ NSView *cv = [win contentView];
+ NSString *old_title = [win title];