Atlas - SDL_uikitwindow.m

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