sdl

FORK: Simple Directmedia Layer
git clone https://git.neptards.moe/neptards/sdl.git
Log | Files | Refs

SDL_uikitwindow.m (17080B)


      1 /*
      2   Simple DirectMedia Layer
      3   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
      4 
      5   This software is provided 'as-is', without any express or implied
      6   warranty.  In no event will the authors be held liable for any damages
      7   arising from the use of this software.
      8 
      9   Permission is granted to anyone to use this software for any purpose,
     10   including commercial applications, and to alter it and redistribute it
     11   freely, subject to the following restrictions:
     12 
     13   1. The origin of this software must not be misrepresented; you must not
     14      claim that you wrote the original software. If you use this software
     15      in a product, an acknowledgment in the product documentation would be
     16      appreciated but is not required.
     17   2. Altered source versions must be plainly marked as such, and must not be
     18      misrepresented as being the original software.
     19   3. This notice may not be removed or altered from any source distribution.
     20 */
     21 #include "../../SDL_internal.h"
     22 
     23 #if SDL_VIDEO_DRIVER_UIKIT
     24 
     25 #include "SDL_syswm.h"
     26 #include "SDL_video.h"
     27 #include "SDL_mouse.h"
     28 #include "SDL_hints.h"
     29 #include "../SDL_sysvideo.h"
     30 #include "../SDL_pixels_c.h"
     31 #include "../../events/SDL_events_c.h"
     32 
     33 #include "SDL_uikitvideo.h"
     34 #include "SDL_uikitevents.h"
     35 #include "SDL_uikitmodes.h"
     36 #include "SDL_uikitwindow.h"
     37 #import "SDL_uikitappdelegate.h"
     38 
     39 #import "SDL_uikitview.h"
     40 #import "SDL_uikitopenglview.h"
     41 
     42 #include <Foundation/Foundation.h>
     43 
     44 @implementation SDL_WindowData
     45 
     46 @synthesize uiwindow;
     47 @synthesize viewcontroller;
     48 @synthesize views;
     49 
     50 - (instancetype)init
     51 {
     52     if ((self = [super init])) {
     53         views = [NSMutableArray new];
     54     }
     55 
     56     return self;
     57 }
     58 
     59 @end
     60 
     61 @interface SDL_uikitwindow : UIWindow
     62 
     63 - (void)layoutSubviews;
     64 
     65 @end
     66 
     67 @implementation SDL_uikitwindow
     68 
     69 - (void)layoutSubviews
     70 {
     71     /* Workaround to fix window orientation issues in iOS 8. */
     72     /* As of July 1 2019, I haven't been able to reproduce any orientation
     73      * issues with this disabled on iOS 12. The issue this is meant to fix might
     74      * only happen on iOS 8, or it might have been fixed another way with other
     75      * code... This code prevents split view (iOS 9+) from working on iPads, so
     76      * we want to avoid using it if possible. */
     77     if (!UIKit_IsSystemVersionAtLeast(9.0)) {
     78         self.frame = self.screen.bounds;
     79     }
     80     [super layoutSubviews];
     81 }
     82 
     83 @end
     84 
     85 
     86 static int
     87 SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
     88 {
     89     SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
     90     SDL_DisplayData *displaydata = (__bridge SDL_DisplayData *) display->driverdata;
     91     SDL_uikitview *view;
     92 
     93     CGRect frame = UIKit_ComputeViewFrame(window, displaydata.uiscreen);
     94     int width  = (int) frame.size.width;
     95     int height = (int) frame.size.height;
     96 
     97     SDL_WindowData *data = [[SDL_WindowData alloc] init];
     98     if (!data) {
     99         return SDL_OutOfMemory();
    100     }
    101 
    102     window->driverdata = (void *) CFBridgingRetain(data);
    103 
    104     data.uiwindow = uiwindow;
    105 
    106     /* only one window on iOS, always shown */
    107     window->flags &= ~SDL_WINDOW_HIDDEN;
    108 
    109     if (displaydata.uiscreen != [UIScreen mainScreen]) {
    110         window->flags &= ~SDL_WINDOW_RESIZABLE;  /* window is NEVER resizable */
    111         window->flags &= ~SDL_WINDOW_INPUT_FOCUS;  /* never has input focus */
    112         window->flags |= SDL_WINDOW_BORDERLESS;  /* never has a status bar. */
    113     }
    114 
    115 #if !TARGET_OS_TV
    116     if (displaydata.uiscreen == [UIScreen mainScreen]) {
    117         /* SDL_CreateWindow sets the window w&h to the display's bounds if the
    118          * fullscreen flag is set. But the display bounds orientation might not
    119          * match what we want, and GetSupportedOrientations call below uses the
    120          * window w&h. They're overridden below anyway, so we'll just set them
    121          * to the requested size for the purposes of determining orientation. */
    122         window->w = window->windowed.w;
    123         window->h = window->windowed.h;
    124 
    125         NSUInteger orients = UIKit_GetSupportedOrientations(window);
    126         BOOL supportsLandscape = (orients & UIInterfaceOrientationMaskLandscape) != 0;
    127         BOOL supportsPortrait = (orients & (UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown)) != 0;
    128 
    129         /* Make sure the width/height are oriented correctly */
    130         if ((width > height && !supportsLandscape) || (height > width && !supportsPortrait)) {
    131             int temp = width;
    132             width = height;
    133             height = temp;
    134         }
    135     }
    136 #endif /* !TARGET_OS_TV */
    137 
    138     window->x = 0;
    139     window->y = 0;
    140     window->w = width;
    141     window->h = height;
    142 
    143     /* The View Controller will handle rotating the view when the device
    144      * orientation changes. This will trigger resize events, if appropriate. */
    145     data.viewcontroller = [[SDL_uikitviewcontroller alloc] initWithSDLWindow:window];
    146 
    147     /* The window will initially contain a generic view so resizes, touch events,
    148      * etc. can be handled without an active OpenGL view/context. */
    149     view = [[SDL_uikitview alloc] initWithFrame:frame];
    150 
    151     /* Sets this view as the controller's view, and adds the view to the window
    152      * heirarchy. */
    153     [view setSDLWindow:window];
    154 
    155     return 0;
    156 }
    157 
    158 int
    159 UIKit_CreateWindow(_THIS, SDL_Window *window)
    160 {
    161     @autoreleasepool {
    162         SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
    163         SDL_DisplayData *data = (__bridge SDL_DisplayData *) display->driverdata;
    164 		SDL_Window *other;
    165 
    166         /* We currently only handle a single window per display on iOS */
    167 		for (other = _this->windows; other; other = other->next) {
    168 			if (other != window && SDL_GetDisplayForWindow(other) == display) {
    169 				return SDL_SetError("Only one window allowed per display.");
    170 			}
    171 		}
    172 
    173         /* If monitor has a resolution of 0x0 (hasn't been explicitly set by the
    174          * user, so it's in standby), try to force the display to a resolution
    175          * that most closely matches the desired window size. */
    176 #if !TARGET_OS_TV
    177         const CGSize origsize = data.uiscreen.currentMode.size;
    178         if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) {
    179             if (display->num_display_modes == 0) {
    180                 _this->GetDisplayModes(_this, display);
    181             }
    182 
    183             int i;
    184             const SDL_DisplayMode *bestmode = NULL;
    185             for (i = display->num_display_modes; i >= 0; i--) {
    186                 const SDL_DisplayMode *mode = &display->display_modes[i];
    187                 if ((mode->w >= window->w) && (mode->h >= window->h)) {
    188                     bestmode = mode;
    189                 }
    190             }
    191 
    192             if (bestmode) {
    193                 SDL_DisplayModeData *modedata = (__bridge SDL_DisplayModeData *)bestmode->driverdata;
    194                 [data.uiscreen setCurrentMode:modedata.uiscreenmode];
    195 
    196                 /* desktop_mode doesn't change here (the higher level will
    197                  * use it to set all the screens back to their defaults
    198                  * upon window destruction, SDL_Quit(), etc. */
    199                 display->current_mode = *bestmode;
    200             }
    201         }
    202 
    203         if (data.uiscreen == [UIScreen mainScreen]) {
    204             if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
    205                 [UIApplication sharedApplication].statusBarHidden = YES;
    206             } else {
    207                 [UIApplication sharedApplication].statusBarHidden = NO;
    208             }
    209         }
    210 #endif /* !TARGET_OS_TV */
    211 
    212         /* ignore the size user requested, and make a fullscreen window */
    213         /* !!! FIXME: can we have a smaller view? */
    214         UIWindow *uiwindow = [[SDL_uikitwindow alloc] initWithFrame:data.uiscreen.bounds];
    215 
    216         /* put the window on an external display if appropriate. */
    217         if (data.uiscreen != [UIScreen mainScreen]) {
    218             [uiwindow setScreen:data.uiscreen];
    219         }
    220 
    221         if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
    222             return -1;
    223         }
    224     }
    225 
    226     return 1;
    227 }
    228 
    229 void
    230 UIKit_SetWindowTitle(_THIS, SDL_Window * window)
    231 {
    232     @autoreleasepool {
    233         SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
    234         data.viewcontroller.title = @(window->title);
    235     }
    236 }
    237 
    238 void
    239 UIKit_ShowWindow(_THIS, SDL_Window * window)
    240 {
    241     @autoreleasepool {
    242         SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
    243         [data.uiwindow makeKeyAndVisible];
    244 
    245         /* Make this window the current mouse focus for touch input */
    246         SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
    247         SDL_DisplayData *displaydata = (__bridge SDL_DisplayData *) display->driverdata;
    248         if (displaydata.uiscreen == [UIScreen mainScreen]) {
    249             SDL_SetMouseFocus(window);
    250             SDL_SetKeyboardFocus(window);
    251         }
    252     }
    253 }
    254 
    255 void
    256 UIKit_HideWindow(_THIS, SDL_Window * window)
    257 {
    258     @autoreleasepool {
    259         SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
    260         data.uiwindow.hidden = YES;
    261     }
    262 }
    263 
    264 void
    265 UIKit_RaiseWindow(_THIS, SDL_Window * window)
    266 {
    267     /* We don't currently offer a concept of "raising" the SDL window, since
    268      * we only allow one per display, in the iOS fashion.
    269      * However, we use this entry point to rebind the context to the view
    270      * during OnWindowRestored processing. */
    271     _this->GL_MakeCurrent(_this, _this->current_glwin, _this->current_glctx);
    272 }
    273 
    274 static void
    275 UIKit_UpdateWindowBorder(_THIS, SDL_Window * window)
    276 {
    277     SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
    278     SDL_uikitviewcontroller *viewcontroller = data.viewcontroller;
    279 
    280 #if !TARGET_OS_TV
    281     if (data.uiwindow.screen == [UIScreen mainScreen]) {
    282         if (window->flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS)) {
    283             [UIApplication sharedApplication].statusBarHidden = YES;
    284         } else {
    285             [UIApplication sharedApplication].statusBarHidden = NO;
    286         }
    287 
    288         /* iOS 7+ won't update the status bar until we tell it to. */
    289         if ([viewcontroller respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
    290             [viewcontroller setNeedsStatusBarAppearanceUpdate];
    291         }
    292     }
    293 
    294     /* Update the view's frame to account for the status bar change. */
    295     viewcontroller.view.frame = UIKit_ComputeViewFrame(window, data.uiwindow.screen);
    296 #endif /* !TARGET_OS_TV */
    297 
    298 #ifdef SDL_IPHONE_KEYBOARD
    299     /* Make sure the view is offset correctly when the keyboard is visible. */
    300     [viewcontroller updateKeyboard];
    301 #endif
    302 
    303     [viewcontroller.view setNeedsLayout];
    304     [viewcontroller.view layoutIfNeeded];
    305 }
    306 
    307 void
    308 UIKit_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
    309 {
    310     @autoreleasepool {
    311         UIKit_UpdateWindowBorder(_this, window);
    312     }
    313 }
    314 
    315 void
    316 UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
    317 {
    318     @autoreleasepool {
    319         UIKit_UpdateWindowBorder(_this, window);
    320     }
    321 }
    322 
    323 void
    324 UIKit_DestroyWindow(_THIS, SDL_Window * window)
    325 {
    326     @autoreleasepool {
    327         if (window->driverdata != NULL) {
    328             SDL_WindowData *data = (SDL_WindowData *) CFBridgingRelease(window->driverdata);
    329             NSArray *views = nil;
    330 
    331             [data.viewcontroller stopAnimation];
    332 
    333             /* Detach all views from this window. We use a copy of the array
    334              * because setSDLWindow will remove the object from the original
    335              * array, which would be undesirable if we were iterating over it. */
    336             views = [data.views copy];
    337             for (SDL_uikitview *view in views) {
    338                 [view setSDLWindow:NULL];
    339             }
    340 
    341             /* iOS may still hold a reference to the window after we release it.
    342              * We want to make sure the SDL view controller isn't accessed in
    343              * that case, because it would contain an invalid pointer to the old
    344              * SDL window. */
    345             data.uiwindow.rootViewController = nil;
    346             data.uiwindow.hidden = YES;
    347         }
    348     }
    349     window->driverdata = NULL;
    350 }
    351 
    352 SDL_bool
    353 UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
    354 {
    355     @autoreleasepool {
    356         SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
    357 
    358         if (info->version.major <= SDL_MAJOR_VERSION) {
    359             int versionnum = SDL_VERSIONNUM(info->version.major, info->version.minor, info->version.patch);
    360 
    361             info->subsystem = SDL_SYSWM_UIKIT;
    362             info->info.uikit.window = data.uiwindow;
    363 
    364             /* These struct members were added in SDL 2.0.4. */
    365             if (versionnum >= SDL_VERSIONNUM(2,0,4)) {
    366 #if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
    367                 if ([data.viewcontroller.view isKindOfClass:[SDL_uikitopenglview class]]) {
    368                     SDL_uikitopenglview *glview = (SDL_uikitopenglview *)data.viewcontroller.view;
    369                     info->info.uikit.framebuffer = glview.drawableFramebuffer;
    370                     info->info.uikit.colorbuffer = glview.drawableRenderbuffer;
    371                     info->info.uikit.resolveFramebuffer = glview.msaaResolveFramebuffer;
    372                 } else {
    373 #else
    374                 {
    375 #endif
    376                     info->info.uikit.framebuffer = 0;
    377                     info->info.uikit.colorbuffer = 0;
    378                     info->info.uikit.resolveFramebuffer = 0;
    379                 }
    380             }
    381 
    382             return SDL_TRUE;
    383         } else {
    384             SDL_SetError("Application not compiled with SDL %d.%d",
    385                          SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
    386             return SDL_FALSE;
    387         }
    388     }
    389 }
    390 
    391 #if !TARGET_OS_TV
    392 NSUInteger
    393 UIKit_GetSupportedOrientations(SDL_Window * window)
    394 {
    395     const char *hint = SDL_GetHint(SDL_HINT_ORIENTATIONS);
    396     NSUInteger validOrientations = UIInterfaceOrientationMaskAll;
    397     NSUInteger orientationMask = 0;
    398 
    399     @autoreleasepool {
    400         SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
    401         UIApplication *app = [UIApplication sharedApplication];
    402 
    403         /* Get all possible valid orientations. If the app delegate doesn't tell
    404          * us, we get the orientations from Info.plist via UIApplication. */
    405         if ([app.delegate respondsToSelector:@selector(application:supportedInterfaceOrientationsForWindow:)]) {
    406             validOrientations = [app.delegate application:app supportedInterfaceOrientationsForWindow:data.uiwindow];
    407         } else if ([app respondsToSelector:@selector(supportedInterfaceOrientationsForWindow:)]) {
    408             validOrientations = [app supportedInterfaceOrientationsForWindow:data.uiwindow];
    409         }
    410 
    411         if (hint != NULL) {
    412             NSArray *orientations = [@(hint) componentsSeparatedByString:@" "];
    413 
    414             if ([orientations containsObject:@"LandscapeLeft"]) {
    415                 orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
    416             }
    417             if ([orientations containsObject:@"LandscapeRight"]) {
    418                 orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
    419             }
    420             if ([orientations containsObject:@"Portrait"]) {
    421                 orientationMask |= UIInterfaceOrientationMaskPortrait;
    422             }
    423             if ([orientations containsObject:@"PortraitUpsideDown"]) {
    424                 orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
    425             }
    426         }
    427 
    428         if (orientationMask == 0 && (window->flags & SDL_WINDOW_RESIZABLE)) {
    429             /* any orientation is okay. */
    430             orientationMask = UIInterfaceOrientationMaskAll;
    431         }
    432 
    433         if (orientationMask == 0) {
    434             if (window->w >= window->h) {
    435                 orientationMask |= UIInterfaceOrientationMaskLandscape;
    436             }
    437             if (window->h >= window->w) {
    438                 orientationMask |= (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
    439             }
    440         }
    441 
    442         /* Don't allow upside-down orientation on phones, so answering calls is in the natural orientation */
    443         if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
    444             orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
    445         }
    446 
    447         /* If none of the specified orientations are actually supported by the
    448          * app, we'll revert to what the app supports. An exception would be
    449          * thrown by the system otherwise. */
    450         if ((validOrientations & orientationMask) == 0) {
    451             orientationMask = validOrientations;
    452         }
    453     }
    454 
    455     return orientationMask;
    456 }
    457 #endif /* !TARGET_OS_TV */
    458 
    459 int
    460 SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam)
    461 {
    462     if (!window || !window->driverdata) {
    463         return SDL_SetError("Invalid window");
    464     }
    465 
    466     @autoreleasepool {
    467         SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
    468         [data.viewcontroller setAnimationCallback:interval
    469                                          callback:callback
    470                                     callbackParam:callbackParam];
    471     }
    472 
    473     return 0;
    474 }
    475 
    476 #endif /* SDL_VIDEO_DRIVER_UIKIT */
    477 
    478 /* vi: set ts=4 sw=4 expandtab: */