iOS: Report both landscape and portrait orientation as display modes.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 27 Mar 2011 01:35:19 -0400
changeset 5518 f84dd424d514
parent 5517 aa339c0dca2d
child 5519 c425e4e8fc75
iOS: Report both landscape and portrait orientation as display modes.
src/video/uikit/SDL_uikitvideo.m
src/video/uikit/SDL_uikitwindow.m
--- a/src/video/uikit/SDL_uikitvideo.m	Sat Mar 26 21:28:17 2011 -0700
+++ b/src/video/uikit/SDL_uikitvideo.m	Sun Mar 27 01:35:19 2011 -0400
@@ -119,6 +119,32 @@
 
 */
 
+static CGSize
+UIKit_ForcePortrait(const CGSize size)
+{
+    CGSize retval;
+    if (size.width < size.height) { // portrait
+        retval = size;
+    } else {  // landscape
+        retval.width = size.height;
+        retval.height = size.width;
+    }
+    return retval;
+}
+
+static CGSize
+UIKit_ForceLandscape(const CGSize size)
+{
+    CGSize retval;
+    if (size.width > size.height) { // landscape
+        retval = size;
+    } else {  // portrait
+        retval.width = size.height;
+        retval.height = size.width;
+    }
+    return retval;
+}
+
 static void
 UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
 {
@@ -136,22 +162,42 @@
         mode.refresh_rate = 0;
         mode.driverdata = NULL;
         SDL_AddDisplayMode(display, &mode);
+        mode.w = (int) rect.size.height;  // swap the orientation, add again.
+        mode.h = (int) rect.size.width;
+        SDL_AddDisplayMode(display, &mode);
         return;
     }
 
+    const int ismain = (uiscreen == [UIScreen mainScreen]);
     const NSArray *modes = [uiscreen availableModes];
     const NSUInteger mode_count = [modes count];
     NSUInteger i;
     for (i = 0; i < mode_count; i++) {
         UIScreenMode *uimode = (UIScreenMode *) [modes objectAtIndex:i];
-        const CGSize size = [uimode size];
+        CGSize size = [uimode size];
         mode.format = SDL_PIXELFORMAT_ABGR8888;
+        mode.refresh_rate = 0;
+        mode.driverdata = uimode;
         mode.w = (int) size.width;
         mode.h = (int) size.height;
-        mode.refresh_rate = 0;
-        mode.driverdata = uimode;
-        [uimode retain];
-        SDL_AddDisplayMode(display, &mode);
+        if (SDL_AddDisplayMode(display, &mode))
+            [uimode retain];
+
+        if (ismain) {
+            // Add the mode twice, flipped to portrait and landscape.
+            //  SDL_AddDisplayMode() will ignore duplicates.
+            size = UIKit_ForcePortrait([uimode size]);
+            mode.w = (int) size.width;
+            mode.h = (int) size.height;
+            if (SDL_AddDisplayMode(display, &mode))
+                [uimode retain];
+
+            size = UIKit_ForceLandscape(size);
+            mode.w = (int) size.width;
+            mode.h = (int) size.height;
+            if (SDL_AddDisplayMode(display, &mode))
+                [uimode retain];
+        }
     }
 }
 
--- a/src/video/uikit/SDL_uikitwindow.m	Sat Mar 26 21:28:17 2011 -0700
+++ b/src/video/uikit/SDL_uikitwindow.m	Sun Mar 27 01:35:19 2011 -0400
@@ -63,22 +63,37 @@
     
     window->driverdata = data;
     
-    window->flags &= ~SDL_WINDOW_RESIZABLE;        /* window is NEVER resizeable */
     window->flags |= SDL_WINDOW_FULLSCREEN;        /* window is always fullscreen */
     window->flags |= SDL_WINDOW_SHOWN;            /* only one window on iPod touch, always shown */
-    window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */    
 
     // SDL_WINDOW_BORDERLESS controls whether status bar is hidden.
     // This is only set if the window is on the main screen. Other screens
     //  just force the window to have the borderless flag.
-    if ([UIScreen mainScreen] == uiscreen) {
+    if ([UIScreen mainScreen] != uiscreen) {
+        window->flags &= ~SDL_WINDOW_RESIZABLE;  // window is NEVER resizeable
+        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;  // never has input focus
+    } else {
+        window->flags |= SDL_WINDOW_INPUT_FOCUS;  // always has input focus
+
         if (window->flags & SDL_WINDOW_BORDERLESS) {
             [UIApplication sharedApplication].statusBarHidden = YES;
         } else {
             [UIApplication sharedApplication].statusBarHidden = NO;
         }
+
+        // Rotate the view if we have to, but only on the main screen
+        //  (presumably, an external display doesn't report orientation).
+        const CGSize uisize = [[uiscreen currentMode] size];
+        if ( ((window->w > window->h) && (uisize.width < uisize.height)) ||
+             ((window->w < window->h) && (uisize.width > uisize.height)) ) {
+            // !!! FIXME: flip orientation.
+        }
+
+        if (window->flags & SDL_WINDOW_RESIZABLE) {
+            // !!! FIXME: register for orientation change alerts.
+        }
     }
-    
+
     return 0;
     
 }