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 5518f84dd424d514
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
     1.1 --- a/src/video/uikit/SDL_uikitvideo.m	Sat Mar 26 21:28:17 2011 -0700
     1.2 +++ b/src/video/uikit/SDL_uikitvideo.m	Sun Mar 27 01:35:19 2011 -0400
     1.3 @@ -119,6 +119,32 @@
     1.4  
     1.5  */
     1.6  
     1.7 +static CGSize
     1.8 +UIKit_ForcePortrait(const CGSize size)
     1.9 +{
    1.10 +    CGSize retval;
    1.11 +    if (size.width < size.height) { // portrait
    1.12 +        retval = size;
    1.13 +    } else {  // landscape
    1.14 +        retval.width = size.height;
    1.15 +        retval.height = size.width;
    1.16 +    }
    1.17 +    return retval;
    1.18 +}
    1.19 +
    1.20 +static CGSize
    1.21 +UIKit_ForceLandscape(const CGSize size)
    1.22 +{
    1.23 +    CGSize retval;
    1.24 +    if (size.width > size.height) { // landscape
    1.25 +        retval = size;
    1.26 +    } else {  // portrait
    1.27 +        retval.width = size.height;
    1.28 +        retval.height = size.width;
    1.29 +    }
    1.30 +    return retval;
    1.31 +}
    1.32 +
    1.33  static void
    1.34  UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
    1.35  {
    1.36 @@ -136,22 +162,42 @@
    1.37          mode.refresh_rate = 0;
    1.38          mode.driverdata = NULL;
    1.39          SDL_AddDisplayMode(display, &mode);
    1.40 +        mode.w = (int) rect.size.height;  // swap the orientation, add again.
    1.41 +        mode.h = (int) rect.size.width;
    1.42 +        SDL_AddDisplayMode(display, &mode);
    1.43          return;
    1.44      }
    1.45  
    1.46 +    const int ismain = (uiscreen == [UIScreen mainScreen]);
    1.47      const NSArray *modes = [uiscreen availableModes];
    1.48      const NSUInteger mode_count = [modes count];
    1.49      NSUInteger i;
    1.50      for (i = 0; i < mode_count; i++) {
    1.51          UIScreenMode *uimode = (UIScreenMode *) [modes objectAtIndex:i];
    1.52 -        const CGSize size = [uimode size];
    1.53 +        CGSize size = [uimode size];
    1.54          mode.format = SDL_PIXELFORMAT_ABGR8888;
    1.55 +        mode.refresh_rate = 0;
    1.56 +        mode.driverdata = uimode;
    1.57          mode.w = (int) size.width;
    1.58          mode.h = (int) size.height;
    1.59 -        mode.refresh_rate = 0;
    1.60 -        mode.driverdata = uimode;
    1.61 -        [uimode retain];
    1.62 -        SDL_AddDisplayMode(display, &mode);
    1.63 +        if (SDL_AddDisplayMode(display, &mode))
    1.64 +            [uimode retain];
    1.65 +
    1.66 +        if (ismain) {
    1.67 +            // Add the mode twice, flipped to portrait and landscape.
    1.68 +            //  SDL_AddDisplayMode() will ignore duplicates.
    1.69 +            size = UIKit_ForcePortrait([uimode size]);
    1.70 +            mode.w = (int) size.width;
    1.71 +            mode.h = (int) size.height;
    1.72 +            if (SDL_AddDisplayMode(display, &mode))
    1.73 +                [uimode retain];
    1.74 +
    1.75 +            size = UIKit_ForceLandscape(size);
    1.76 +            mode.w = (int) size.width;
    1.77 +            mode.h = (int) size.height;
    1.78 +            if (SDL_AddDisplayMode(display, &mode))
    1.79 +                [uimode retain];
    1.80 +        }
    1.81      }
    1.82  }
    1.83  
     2.1 --- a/src/video/uikit/SDL_uikitwindow.m	Sat Mar 26 21:28:17 2011 -0700
     2.2 +++ b/src/video/uikit/SDL_uikitwindow.m	Sun Mar 27 01:35:19 2011 -0400
     2.3 @@ -63,22 +63,37 @@
     2.4      
     2.5      window->driverdata = data;
     2.6      
     2.7 -    window->flags &= ~SDL_WINDOW_RESIZABLE;        /* window is NEVER resizeable */
     2.8      window->flags |= SDL_WINDOW_FULLSCREEN;        /* window is always fullscreen */
     2.9      window->flags |= SDL_WINDOW_SHOWN;            /* only one window on iPod touch, always shown */
    2.10 -    window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */    
    2.11  
    2.12      // SDL_WINDOW_BORDERLESS controls whether status bar is hidden.
    2.13      // This is only set if the window is on the main screen. Other screens
    2.14      //  just force the window to have the borderless flag.
    2.15 -    if ([UIScreen mainScreen] == uiscreen) {
    2.16 +    if ([UIScreen mainScreen] != uiscreen) {
    2.17 +        window->flags &= ~SDL_WINDOW_RESIZABLE;  // window is NEVER resizeable
    2.18 +        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;  // never has input focus
    2.19 +    } else {
    2.20 +        window->flags |= SDL_WINDOW_INPUT_FOCUS;  // always has input focus
    2.21 +
    2.22          if (window->flags & SDL_WINDOW_BORDERLESS) {
    2.23              [UIApplication sharedApplication].statusBarHidden = YES;
    2.24          } else {
    2.25              [UIApplication sharedApplication].statusBarHidden = NO;
    2.26          }
    2.27 +
    2.28 +        // Rotate the view if we have to, but only on the main screen
    2.29 +        //  (presumably, an external display doesn't report orientation).
    2.30 +        const CGSize uisize = [[uiscreen currentMode] size];
    2.31 +        if ( ((window->w > window->h) && (uisize.width < uisize.height)) ||
    2.32 +             ((window->w < window->h) && (uisize.width > uisize.height)) ) {
    2.33 +            // !!! FIXME: flip orientation.
    2.34 +        }
    2.35 +
    2.36 +        if (window->flags & SDL_WINDOW_RESIZABLE) {
    2.37 +            // !!! FIXME: register for orientation change alerts.
    2.38 +        }
    2.39      }
    2.40 -    
    2.41 +
    2.42      return 0;
    2.43      
    2.44  }