Make sure mode width/height and status bar orientation match
authorSam Lantinga
Sat, 29 Sep 2012 18:52:00 -0700
changeset 65207a7726af31c1
parent 6519 fc926eedd78a
child 6521 f36bbe0a10c9
Make sure mode width/height and status bar orientation match
src/video/uikit/SDL_uikitmodes.h
src/video/uikit/SDL_uikitmodes.m
src/video/uikit/SDL_uikitwindow.m
     1.1 --- a/src/video/uikit/SDL_uikitmodes.h	Sat Sep 29 17:38:54 2012 -0700
     1.2 +++ b/src/video/uikit/SDL_uikitmodes.h	Sat Sep 29 18:52:00 2012 -0700
     1.3 @@ -39,6 +39,8 @@
     1.4  
     1.5  extern BOOL SDL_UIKit_supports_multiple_displays;
     1.6  
     1.7 +extern SDL_bool UIKit_IsDisplayLandscape(UIScreen *uiscreen);
     1.8 +
     1.9  extern int UIKit_InitModes(_THIS);
    1.10  extern void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display);
    1.11  extern int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
     2.1 --- a/src/video/uikit/SDL_uikitmodes.m	Sat Sep 29 17:38:54 2012 -0700
     2.2 +++ b/src/video/uikit/SDL_uikitmodes.m	Sat Sep 29 18:52:00 2012 -0700
     2.3 @@ -93,13 +93,13 @@
     2.4  
     2.5  static int
     2.6  UIKit_AddDisplayMode(SDL_VideoDisplay * display, int w, int h, CGFloat scale,
     2.7 -                     UIScreenMode * uiscreenmode, BOOL addRotated)
     2.8 +                     UIScreenMode * uiscreenmode, SDL_bool addRotation)
     2.9  {
    2.10      if (UIKit_AddSingleDisplayMode(display, w, h, uiscreenmode, scale) < 0) {
    2.11          return -1;
    2.12      }
    2.13      
    2.14 -    if (addRotated) {
    2.15 +    if (addRotation) {
    2.16          // Add the rotated version
    2.17          if (UIKit_AddSingleDisplayMode(display, h, w, uiscreenmode, scale) < 0) {
    2.18              return -1;
    2.19 @@ -114,6 +114,13 @@
    2.20  {
    2.21      CGSize size = [uiscreen bounds].size;
    2.22  
    2.23 +    // Make sure the width/height are oriented correctly
    2.24 +    if (UIKit_IsDisplayLandscape(uiscreen) != (size.width > size.height)) {
    2.25 +        CGFloat height = size.width;
    2.26 +        size.width = size.height;
    2.27 +        size.height = height;
    2.28 +    }
    2.29 +
    2.30      // When dealing with UIKit all coordinates are specified in terms of
    2.31      // what Apple refers to as points. On earlier devices without the
    2.32      // so called "Retina" display, there is a one to one mapping between
    2.33 @@ -127,7 +134,7 @@
    2.34      } else {
    2.35          scale = 1.0f; // iOS < 4.0
    2.36      }
    2.37 -	
    2.38 +
    2.39      SDL_VideoDisplay display;
    2.40      SDL_DisplayMode mode;
    2.41      SDL_zero(mode);
    2.42 @@ -168,6 +175,16 @@
    2.43      return 0;
    2.44  }
    2.45  
    2.46 +SDL_bool
    2.47 +UIKit_IsDisplayLandscape(UIScreen *uiscreen)
    2.48 +{
    2.49 +    if (uiscreen == [UIScreen mainScreen]) {
    2.50 +        return UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
    2.51 +    } else {
    2.52 +        CGSize size = [uiscreen bounds].size;
    2.53 +        return (size.width > size.height);
    2.54 +    }
    2.55 +}
    2.56  
    2.57  int
    2.58  UIKit_InitModes(_THIS)
    2.59 @@ -203,6 +220,9 @@
    2.60  {
    2.61      SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
    2.62  
    2.63 +    SDL_bool isLandscape = UIKit_IsDisplayLandscape(data->uiscreen);
    2.64 +    SDL_bool addRotation = (data->uiscreen == [UIScreen mainScreen]);
    2.65 +
    2.66      if (SDL_UIKit_supports_multiple_displays) {
    2.67          // availableModes showed up in 3.2 (the iPad and later). We should only
    2.68          //  land here for at least that version of the OS.
    2.69 @@ -210,10 +230,16 @@
    2.70              CGSize size = [uimode size];
    2.71              int w = (int)size.width;
    2.72              int h = (int)size.height;
    2.73 -            BOOL addRotated = (data->uiscreen == [UIScreen mainScreen]);
    2.74 -            
    2.75 + 
    2.76 +            // Make sure the width/height are oriented correctly
    2.77 +            if (isLandscape != (w > h)) {
    2.78 +                int tmp = w;
    2.79 +                w = h;
    2.80 +                h = tmp;
    2.81 +            }
    2.82 +
    2.83              // Add the native screen resolution.
    2.84 -            UIKit_AddDisplayMode(display, w, h, data->scale, uimode, addRotated);
    2.85 +            UIKit_AddDisplayMode(display, w, h, data->scale, uimode, addRotation);
    2.86  
    2.87              if (data->scale != 1.0f) {
    2.88                  // Add the native screen resolution divided by its scale.
    2.89 @@ -222,14 +248,22 @@
    2.90                  UIKit_AddDisplayMode(display,
    2.91                      (int)(size.width / data->scale),
    2.92                      (int)(size.height / data->scale),
    2.93 -                    1.0f, uimode, addRotated);
    2.94 +                    1.0f, uimode, addRotation);
    2.95              }
    2.96          }
    2.97      } else {
    2.98 -        const CGRect rect = [data->uiscreen bounds];
    2.99 -        UIKit_AddDisplayMode(display,
   2.100 -            (int)rect.size.width, (int)rect.size.height,
   2.101 -            1.0f, nil, YES);
   2.102 +        const CGSize size = [data->uiscreen bounds].size;
   2.103 +        int w = (int)size.width;
   2.104 +        int h = (int)size.height;
   2.105 +
   2.106 +        // Make sure the width/height are oriented correctly
   2.107 +        if (isLandscape != (w > h)) {
   2.108 +            int tmp = w;
   2.109 +            w = h;
   2.110 +            h = tmp;
   2.111 +        }
   2.112 +
   2.113 +        UIKit_AddDisplayMode(display, w, h, 1.0f, nil, addRotation);
   2.114      } 
   2.115  }
   2.116  
   2.117 @@ -237,6 +271,7 @@
   2.118  UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   2.119  {
   2.120      SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
   2.121 +
   2.122      if (!SDL_UIKit_supports_multiple_displays) {
   2.123          // Not on at least iPhoneOS 3.2 (versions prior to iPad).
   2.124          SDL_assert(mode->driverdata == NULL);
   2.125 @@ -244,15 +279,18 @@
   2.126          SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
   2.127          [data->uiscreen setCurrentMode:modedata->uiscreenmode];
   2.128  
   2.129 -        if (mode->w > mode->h) {
   2.130 -            if (!UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
   2.131 -                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
   2.132 -        } else if (mode->w < mode->h) {
   2.133 -            if (!UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
   2.134 -                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
   2.135 +        if (data->uiscreen == [UIScreen mainScreen]) {
   2.136 +            if (mode->w > mode->h) {
   2.137 +                if (!UIKit_IsDisplayLandscape(data->uiscreen)) {
   2.138 +                    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
   2.139 +                }
   2.140 +            } else if (mode->w < mode->h) {
   2.141 +                if (UIKit_IsDisplayLandscape(data->uiscreen)) {
   2.142 +                    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
   2.143 +                }
   2.144 +            }
   2.145          }
   2.146      }
   2.147 -
   2.148      return 0;
   2.149  }
   2.150  
     3.1 --- a/src/video/uikit/SDL_uikitwindow.m	Sat Sep 29 17:38:54 2012 -0700
     3.2 +++ b/src/video/uikit/SDL_uikitwindow.m	Sat Sep 29 18:52:00 2012 -0700
     3.3 @@ -76,32 +76,16 @@
     3.4          /* Get frame dimensions in pixels */
     3.5          int width = (int)(bounds.size.width * displaymodedata->scale);
     3.6          int height = (int)(bounds.size.height * displaymodedata->scale);
     3.7 -        
     3.8 -        if ([UIScreen mainScreen] == displaydata->uiscreen) {
     3.9 -            /* We can pick either width or height here and we'll rotate the
    3.10 -             screen to match, so we pick the closest to what we wanted.
    3.11 -             */
    3.12 -            if (window->w >= window->h) {
    3.13 -                if (width > height) {
    3.14 -                    window->w = width;
    3.15 -                    window->h = height;
    3.16 -                } else {
    3.17 -                    window->w = height;
    3.18 -                    window->h = width;
    3.19 -                }
    3.20 -            } else {
    3.21 -                if (width > height) {
    3.22 -                    window->w = height;
    3.23 -                    window->h = width;
    3.24 -                } else {
    3.25 -                    window->w = width;
    3.26 -                    window->h = height;
    3.27 -                }
    3.28 -            }
    3.29 -        } else {
    3.30 -            window->w = width;
    3.31 -            window->h = height;
    3.32 +
    3.33 +        // Make sure the width/height are oriented correctly
    3.34 +        if (UIKit_IsDisplayLandscape(displaydata->uiscreen) != (width > height)) {
    3.35 +            int temp = width;
    3.36 +            width = height;
    3.37 +            height = temp;
    3.38          }
    3.39 +
    3.40 +        window->w = width;
    3.41 +        window->h = height;
    3.42      }
    3.43  
    3.44      window->driverdata = data;
    3.45 @@ -112,13 +96,13 @@
    3.46      // SDL_WINDOW_BORDERLESS controls whether status bar is hidden.
    3.47      // This is only set if the window is on the main screen. Other screens
    3.48      //  just force the window to have the borderless flag.
    3.49 -    if ([UIScreen mainScreen] == displaydata->uiscreen) {
    3.50 +    if (displaydata->uiscreen == [UIScreen mainScreen]) {
    3.51          window->flags |= SDL_WINDOW_INPUT_FOCUS;  // always has input focus
    3.52          
    3.53 -        if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
    3.54 -            [UIApplication sharedApplication].statusBarHidden = YES;
    3.55 +        if ([UIApplication sharedApplication].statusBarHidden) {
    3.56 +            window->flags |= SDL_WINDOW_BORDERLESS;
    3.57          } else {
    3.58 -            [UIApplication sharedApplication].statusBarHidden = NO;
    3.59 +            window->flags &= ~SDL_WINDOW_BORDERLESS;
    3.60          }
    3.61      } else {
    3.62          window->flags &= ~SDL_WINDOW_RESIZABLE;  // window is NEVER resizeable
    3.63 @@ -182,6 +166,26 @@
    3.64              }
    3.65          }
    3.66      }
    3.67 +    
    3.68 +    if (data->uiscreen == [UIScreen mainScreen]) {
    3.69 +        if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
    3.70 +            [UIApplication sharedApplication].statusBarHidden = YES;
    3.71 +        } else {
    3.72 +            [UIApplication sharedApplication].statusBarHidden = NO;
    3.73 +        }
    3.74 +    }
    3.75 +    
    3.76 +    if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
    3.77 +        if (window->w > window->h) {
    3.78 +            if (!UIKit_IsDisplayLandscape(data->uiscreen)) {
    3.79 +                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
    3.80 +            }
    3.81 +        } else if (window->w < window->h) {
    3.82 +            if (UIKit_IsDisplayLandscape(data->uiscreen)) {
    3.83 +                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    3.84 +            }
    3.85 +        }
    3.86 +    }
    3.87  
    3.88      /* ignore the size user requested, and make a fullscreen window */
    3.89      // !!! FIXME: can we have a smaller view?