Added support for SDL_SetWindowBordered on iOS. Worked around a bug with rotating the device on iOS 8. iOS-improvements
authorAlex Szpakowski <slime73@gmail.com>
Tue, 29 Jul 2014 00:05:48 -0300
branchiOS-improvements
changeset 9505 6fc615dfc93f
parent 9504 1f3b6fc94fae
child 9506 18e3f94bd860
Added support for SDL_SetWindowBordered on iOS. Worked around a bug with rotating the device on iOS 8.
include/SDL_hints.h
src/video/uikit/SDL_uikitappdelegate.m
src/video/uikit/SDL_uikitmessagebox.m
src/video/uikit/SDL_uikitvideo.m
src/video/uikit/SDL_uikitview.m
src/video/uikit/SDL_uikitviewcontroller.m
src/video/uikit/SDL_uikitwindow.h
src/video/uikit/SDL_uikitwindow.m
--- a/include/SDL_hints.h	Thu Jul 24 22:35:25 2014 -0300
+++ b/include/SDL_hints.h	Tue Jul 29 00:05:48 2014 -0300
@@ -346,7 +346,7 @@
 
 
 /**
- *  \brief If set to 1, then do not allow high-DPI windows. ("Retina" on Mac)
+ *  \brief If set to 1, then do not allow high-DPI windows. ("Retina" on Mac and iOS)
  */
 #define SDL_HINT_VIDEO_HIGHDPI_DISABLED "SDL_VIDEO_HIGHDPI_DISABLED"
 
--- a/src/video/uikit/SDL_uikitappdelegate.m	Thu Jul 24 22:35:25 2014 -0300
+++ b/src/video/uikit/SDL_uikitappdelegate.m	Tue Jul 29 00:05:48 2014 -0300
@@ -244,22 +244,29 @@
 
 - (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation
 {
-    UIInterfaceOrientation orientation = application.statusBarOrientation;
+    BOOL isLandscape = UIInterfaceOrientationIsLandscape(application.statusBarOrientation);
     SDL_VideoDevice *_this = SDL_GetVideoDevice();
 
     if (_this && _this->num_displays > 0) {
-        SDL_VideoDisplay *display = &_this->displays[0]; /* Main screen. */
-        SDL_DisplayMode *mode = &display->desktop_mode;
+        SDL_DisplayMode *desktopmode = &_this->displays[0].desktop_mode;
+        SDL_DisplayMode *currentmode = &_this->displays[0].current_mode;
 
         /* The desktop display mode should be kept in sync with the screen
          * orientation so that updating a window's fullscreen state to
          * SDL_WINDOW_FULLSCREEN_DESKTOP keeps the window dimensions in the
          * correct orientation.
          */
-        if (UIInterfaceOrientationIsLandscape(orientation) != (mode->w > mode->h)) {
-            int height = mode->w;
-            mode->w = mode->h;
-            mode->h = height;
+        if (isLandscape != (desktopmode->w > desktopmode->h)) {
+            int height = desktopmode->w;
+            desktopmode->w = desktopmode->h;
+            desktopmode->h = height;
+        }
+
+        /* Same deal with the current mode + SDL_GetCurrentDisplayMode. */
+        if (isLandscape != (currentmode->w > currentmode->h)) {
+            int height = currentmode->w;
+            currentmode->w = currentmode->h;
+            currentmode->h = height;
         }
     }
 }
--- a/src/video/uikit/SDL_uikitmessagebox.m	Thu Jul 24 22:35:25 2014 -0300
+++ b/src/video/uikit/SDL_uikitmessagebox.m	Tue Jul 29 00:05:48 2014 -0300
@@ -32,7 +32,7 @@
 
 @interface UIKit_UIAlertViewDelegate : NSObject <UIAlertViewDelegate>
 
-- (id)initWithButtonIndex:(int *)_buttonIndex;
+- (id)initWithButtonIndex:(int *)buttonIndex;
 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
 
 @end
--- a/src/video/uikit/SDL_uikitvideo.m	Thu Jul 24 22:35:25 2014 -0300
+++ b/src/video/uikit/SDL_uikitvideo.m	Tue Jul 29 00:05:48 2014 -0300
@@ -78,12 +78,11 @@
     device->ShowWindow = UIKit_ShowWindow;
     device->HideWindow = UIKit_HideWindow;
     device->RaiseWindow = UIKit_RaiseWindow;
+    device->SetWindowBordered = UIKit_SetWindowBordered;
     device->SetWindowFullscreen = UIKit_SetWindowFullscreen;
     device->DestroyWindow = UIKit_DestroyWindow;
     device->GetWindowWMInfo = UIKit_GetWindowWMInfo;
 
-    /* !!! FIXME: implement SetWindowBordered */
-
 #if SDL_IPHONE_KEYBOARD
     device->HasScreenKeyboardSupport = UIKit_HasScreenKeyboardSupport;
     device->ShowScreenKeyboard = UIKit_ShowScreenKeyboard;
--- a/src/video/uikit/SDL_uikitview.m	Thu Jul 24 22:35:25 2014 -0300
+++ b/src/video/uikit/SDL_uikitview.m	Tue Jul 29 00:05:48 2014 -0300
@@ -51,23 +51,18 @@
 
 }
 
-- (void)dealloc
-{
-    [super dealloc];
-}
-
 - (id)initWithFrame:(CGRect)frame
 {
-    self = [super initWithFrame: frame];
-
+    if (self = [super initWithFrame: frame]) {
 #if SDL_IPHONE_KEYBOARD
-    [self initializeKeyboard];
+        [self initializeKeyboard];
 #endif
 
-    self.multipleTouchEnabled = YES;
+        self.multipleTouchEnabled = YES;
 
-    touchId = 1;
-    SDL_AddTouch(touchId, "");
+        touchId = 1;
+        SDL_AddTouch(touchId, "");
+    }
 
     return self;
 
--- a/src/video/uikit/SDL_uikitviewcontroller.m	Thu Jul 24 22:35:25 2014 -0300
+++ b/src/video/uikit/SDL_uikitviewcontroller.m	Tue Jul 29 00:05:48 2014 -0300
@@ -40,12 +40,9 @@
 
 - (id)initWithSDLWindow:(SDL_Window *)_window
 {
-    self = [self init];
-    if (self == nil) {
-        return nil;
+    if (self = [super initWithNibName:nil bundle:nil]) {
+        self.window = _window;
     }
-    self.window = _window;
-
     return self;
 }
 
@@ -56,8 +53,7 @@
 
 - (void)viewDidLayoutSubviews
 {
-    SDL_WindowData *data = window->driverdata;
-    const CGSize size = data->view.bounds.size;
+    const CGSize size = self.view.bounds.size;
     int w = (int) size.width;
     int h = (int) size.height;
 
--- a/src/video/uikit/SDL_uikitwindow.h	Thu Jul 24 22:35:25 2014 -0300
+++ b/src/video/uikit/SDL_uikitwindow.h	Tue Jul 29 00:05:48 2014 -0300
@@ -32,6 +32,7 @@
 extern void UIKit_ShowWindow(_THIS, SDL_Window * window);
 extern void UIKit_HideWindow(_THIS, SDL_Window * window);
 extern void UIKit_RaiseWindow(_THIS, SDL_Window * window);
+extern void UIKit_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered);
 extern void UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
 extern void UIKit_DestroyWindow(_THIS, SDL_Window * window);
 extern SDL_bool UIKit_GetWindowWMInfo(_THIS, SDL_Window * window,
@@ -39,9 +40,13 @@
 
 @class UIWindow;
 
+@interface SDL_uikitwindow : UIWindow
+
+@end
+
 struct SDL_WindowData
 {
-    UIWindow *uiwindow;
+    SDL_uikitwindow *uiwindow;
     SDL_uikitopenglview *view;
     SDL_uikitviewcontroller *viewcontroller;
 };
--- a/src/video/uikit/SDL_uikitwindow.m	Thu Jul 24 22:35:25 2014 -0300
+++ b/src/video/uikit/SDL_uikitwindow.m	Tue Jul 29 00:05:48 2014 -0300
@@ -41,8 +41,23 @@
 
 #include <Foundation/Foundation.h>
 
+@implementation SDL_uikitwindow
 
-static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
+- (void)layoutSubviews
+{
+    [super layoutSubviews];
+
+    /* This seems to be needed on iOS 8, otherwise the window's frame is put in
+     * an unexpected position when the screen or device is rotated.
+     * FIXME: is there a better solution to that problem than this ugly hack?
+     */
+    self.frame = self.screen.bounds;
+}
+
+@end
+
+
+static int SetupWindowData(_THIS, SDL_Window *window, SDL_uikitwindow *uiwindow, SDL_bool created)
 {
     SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
     SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
@@ -181,7 +196,7 @@
 
     /* ignore the size user requested, and make a fullscreen window */
     /* !!! FIXME: can we have a smaller view? */
-    UIWindow *uiwindow = [[UIWindow alloc] initWithFrame:[data->uiscreen bounds]];
+    SDL_uikitwindow *uiwindow = [[SDL_uikitwindow alloc] initWithFrame:data->uiscreen.bounds];
 
     /* put the window on an external display if appropriate. This implicitly
      * does [uiwindow setframe:[uiscreen bounds]], so don't do it on the
@@ -198,7 +213,6 @@
     }
 
     return 1;
-
 }
 
 void
@@ -228,48 +242,60 @@
     _this->GL_MakeCurrent(_this, _this->current_glwin, _this->current_glctx);
 }
 
-void
-UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
+static void
+UIKit_UpdateWindowBorder(_THIS, SDL_Window * window)
 {
-    SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
     SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
     SDL_uikitviewcontroller *viewcontroller = windowdata->viewcontroller;
     CGRect frame;
 
-    if (fullscreen || (window->flags & SDL_WINDOW_BORDERLESS)) {
-        [UIApplication sharedApplication].statusBarHidden = YES;
-    } else {
-        [UIApplication sharedApplication].statusBarHidden = NO;
-    }
+    if (windowdata->uiwindow.screen == [UIScreen mainScreen]) {
+        if (window->flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS)) {
+            [UIApplication sharedApplication].statusBarHidden = YES;
+        } else {
+            [UIApplication sharedApplication].statusBarHidden = NO;
+        }
 
-    /* iOS 7+ won't update the status bar until we tell it to. */
-    if ([viewcontroller respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
-        [viewcontroller setNeedsStatusBarAppearanceUpdate];
+        /* iOS 7+ won't update the status bar until we tell it to. */
+        if ([viewcontroller respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
+            [viewcontroller setNeedsStatusBarAppearanceUpdate];
+        }
     }
 
     /* Update the view's frame to account for the status bar change. */
-    frame = UIKit_ComputeViewFrame(window, displaydata->uiscreen);
+    frame = UIKit_ComputeViewFrame(window, windowdata->uiwindow.screen);
+
     windowdata->view.frame = frame;
     [windowdata->view setNeedsLayout];
     [windowdata->view layoutIfNeeded];
 
     /* Get frame dimensions */
-    int width = (int) frame.size.width;
+    int width  = (int) frame.size.width;
     int height = (int) frame.size.height;
 
     /* We can pick either width or height here and we'll rotate the
-       screen to match, so we pick the closest to what we wanted.
+     screen to match, so we pick the closest to what we wanted.
      */
     if (window->w >= window->h) {
-        window->w = SDL_max(width, height);
-        window->h = SDL_min(width, height);
+        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, SDL_max(width, height), SDL_min(width, height));
     } else {
-        window->w = SDL_min(width, height);
-        window->h = SDL_max(width, height);
+        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, SDL_min(width, height), SDL_max(width, height));
     }
 }
 
 void
+UIKit_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
+{
+    UIKit_UpdateWindowBorder(_this, window);
+}
+
+void
+UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
+{
+    UIKit_UpdateWindowBorder(_this, window);
+}
+
+void
 UIKit_DestroyWindow(_THIS, SDL_Window * window)
 {
     SDL_WindowData *data = (SDL_WindowData *)window->driverdata;