From http://www.jwz.org/xscreensaver/xscreensaver-5.35.tar.gz
[xscreensaver] / OSX / XScreenSaverGLView.m
index 41b99f31b24ebfc7fcd65fc922f52fc3a352dc41..989f8eb9258f36a3a7193bc902f629b4b25e93b2 100644 (file)
@@ -1,4 +1,4 @@
-/* xscreensaver, Copyright (c) 2006-2015 Jamie Zawinski <jwz@jwz.org>
+/* xscreensaver, Copyright (c) 2006-2016 Jamie Zawinski <jwz@jwz.org>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -17,6 +17,8 @@
 
 #import "XScreenSaverGLView.h"
 #import "XScreenSaverConfigSheet.h"
+#import "jwxyz-cocoa.h"
+#import "jwxyzI.h"
 #import "screenhackI.h"
 #import "xlockmoreI.h"
 
@@ -39,48 +41,50 @@ extern void check_gl_error (const char *type);
 @implementation XScreenSaverGLView
 
 
-- (NSOpenGLContext *) oglContext
-{
-  return ogl_ctx;
-}
-
-
 #ifdef USE_IPHONE
 /* With GL programs, drawing at full resolution isn't a problem.
  */
 - (CGFloat) hackedContentScaleFactor
 {
-  NSSize ssize = [[[UIScreen mainScreen] currentMode] size];
-  NSSize bsize = [self bounds].size;
+  return [self contentScaleFactor];
+}
 
-  // Ratio of screen size in pixels to view size in points.
-  GLfloat s = ((ssize.width > ssize.height ? ssize.width : ssize.height) /
-               (bsize.width > bsize.height ? bsize.width : bsize.height));
-  return s;
+- (BOOL)ignoreRotation
+{
+  return FALSE;                // Allow xwindow and the glViewport to change shape
+}
+
+- (BOOL) suppressRotationAnimation
+{
+  return _suppressRotationAnimation;  // per-hack setting, default FALSE
+}
+
+- (BOOL) rotateTouches
+{
+  return TRUE;         // We need the XY axes swapped in our events
 }
-#endif // USE_IPHONE
 
 
-#ifdef USE_IPHONE
 - (void) swapBuffers
 {
+#  ifdef JWXYZ_GL
+  GLint gl_renderbuffer = xwindow->gl_renderbuffer;
+#  endif // JWXYZ_GL
   glBindRenderbufferOES (GL_RENDERBUFFER_OES, gl_renderbuffer);
   [ogl_ctx presentRenderbuffer:GL_RENDERBUFFER_OES];
 }
 #endif // USE_IPHONE
 
 
-#ifdef USE_BACKBUFFER
-
 - (void) animateOneFrame
 {
-# ifdef USE_IPHONE
+# if defined USE_IPHONE && defined JWXYZ_QUARTZ
   UIGraphicsPushContext (backbuffer);
 # endif
 
   [self render_x11];
 
-# ifdef USE_IPHONE
+# if defined USE_IPHONE && defined JWXYZ_QUARTZ
   UIGraphicsPopContext();
 # endif
 }
@@ -92,6 +96,42 @@ extern void check_gl_error (const char *type);
 }
 
 
+/* GL screenhacks set their own viewport and matrices. */
+- (void) setViewport
+{
+}
+
+
+#ifdef USE_IPHONE
+
+/* Keep the GL scene oriented into a portrait-mode View, regardless of
+   what the physical device orientation is.
+ */
+- (void) reshape_x11
+{
+  [super reshape_x11];
+
+  glMatrixMode(GL_PROJECTION);
+  glRotatef (-current_device_rotation(), 0, 0, 1);
+  glMatrixMode(GL_MODELVIEW);
+}
+
+- (void) render_x11
+{
+  BOOL was_initted_p = initted_p;
+  [super render_x11];
+
+  if (! was_initted_p)
+    _suppressRotationAnimation =
+      get_boolean_resource (xdpy,
+                            "suppressRotationAnimation",
+                            "SuppressRotationAnimation");
+}
+
+#endif // USE_IPHONE
+
+
+
 /* The backbuffer isn't actually used for GL programs, but it needs to
    be there for X11 calls to not error out.  However, nothing done with
    X11 calls will ever show up!  It all gets written into the backbuffer
@@ -100,11 +140,10 @@ extern void check_gl_error (const char *type);
  */
 - (void) createBackbuffer:(CGSize)new_size
 {
+#ifdef JWXYZ_QUARTZ
   NSAssert (! backbuffer_texture,
                        @"backbuffer_texture shouldn't be used for GL hacks");
 
-  backbuffer_size = new_size;
-
   if (! backbuffer) {
     CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
     int w = 8;
@@ -115,6 +154,7 @@ extern void check_gl_error (const char *type);
                                          kCGImageAlphaNoneSkipLast));
     CGColorSpaceRelease (cs);
   }
+#endif // JWXYZ_QUARTZ
 }
 
 
@@ -125,24 +165,11 @@ extern void check_gl_error (const char *type);
 
 
 /* Likewise. GL screenhacks control display with glXSwapBuffers(). */
-- (void) flushBackbufer
+- (void) flushBackbuffer
 {
 }
 
 
-# endif // USE_BACKBUFFER
-
-
-/* When changing the device orientation, leave the X11 Window and glViewport
-   in portrait configuration.  OpenGL hacks examine current_device_rotation()
-   within the scene as needed.
- */
-- (BOOL)reshapeRotatedWindow
-{
-  return NO;
-}
-
-
 #ifndef USE_IPHONE
 
 - (NSOpenGLPixelFormat *) getGLPixelFormat
@@ -177,18 +204,23 @@ extern void check_gl_error (const char *type);
     // attrs[i++] = NSOpenGLPFANoRecovery;
   }
 
+  attrs[i++] = NSOpenGLPFAWindow;
+# ifdef JWXYZ_GL
+  attrs[i++] = NSOpenGLPFAPixelBuffer;
+# endif
+
   attrs[i] = 0;
 
-  NSOpenGLPixelFormat *pixfmt = [[NSOpenGLPixelFormat alloc]
+  NSOpenGLPixelFormat *result = [[NSOpenGLPixelFormat alloc]
                                  initWithAttributes:attrs];
 
-  if (ms_p && !pixfmt) {   // Retry without multisampling.
+  if (ms_p && !result) {   // Retry without multisampling.
     i -= 2;
     attrs[i] = 0;
-    pixfmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
+    result = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
   }
 
-  return [pixfmt autorelease];
+  return [result autorelease];
 }
 
 #else // !USE_IPHONE
@@ -225,6 +257,11 @@ extern void check_gl_error (const char *type);
                                 GL_RENDERBUFFER_OES, gl_depthbuffer);
 }
 
+- (NSString *)getCAGravity
+{
+  return kCAGravityCenter;
+}
+
 #endif // !USE_IPHONE
 
 
@@ -298,6 +335,11 @@ init_GL (ModeInfo *mi)
 void
 glXSwapBuffers (Display *dpy, Window window)
 {
+  // This all is very much like what's in -[XScreenSaverView flushBackbuffer].
+#ifdef JWXYZ_GL
+  jwxyz_bind_drawable (window, window);
+#endif // JWXYZ_GL
+
   XScreenSaverGLView *view = (XScreenSaverGLView *) jwxyz_window_view (window);
   NSAssert1 ([view isKindOfClass:[XScreenSaverGLView class]],
              @"wrong view class: %@", view);