Atlas - SDL_uikitwindow.m

Home / ext / SDL / src / video / uikit Lines: 1 | Size: 17765 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 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#ifdef SDL_VIDEO_DRIVER_UIKIT 24 25#include "../SDL_sysvideo.h" 26#include "../SDL_pixels_c.h" 27#include "../../events/SDL_events_c.h" 28 29#include "SDL_uikitvideo.h" 30#include "SDL_uikitevents.h" 31#include "SDL_uikitmodes.h" 32#include "SDL_uikitwindow.h" 33#include "SDL_uikitappdelegate.h" 34#include "SDL_uikitview.h" 35#include "SDL_uikitopenglview.h" 36 37#include <Foundation/Foundation.h> 38 39@implementation SDL_UIKitWindowData 40 41@synthesize uiwindow; 42@synthesize viewcontroller; 43@synthesize views; 44 45- (instancetype)init 46{ 47 if ((self = [super init])) { 48 views = [NSMutableArray new]; 49 } 50 51 return self; 52} 53 54@end 55 56static bool SetupWindowData(SDL_VideoDevice *_this, SDL_Window *window, UIWindow *uiwindow, bool created) 57{ 58 SDL_VideoDisplay *display = SDL_GetVideoDisplayForWindow(window); 59 SDL_UIKitDisplayData *displaydata = (__bridge SDL_UIKitDisplayData *)display->internal; 60 SDL_uikitview *view; 61 62#ifdef SDL_PLATFORM_VISIONOS 63 CGRect frame = UIKit_ComputeViewFrame(window); 64#else 65 CGRect frame = UIKit_ComputeViewFrame(window, displaydata.uiscreen); 66#endif 67 68 int width = (int)frame.size.width; 69 int height = (int)frame.size.height; 70 71 SDL_UIKitWindowData *data = [[SDL_UIKitWindowData alloc] init]; 72 if (!data) { 73 return SDL_OutOfMemory(); 74 } 75 76 window->internal = (SDL_WindowData *)CFBridgingRetain(data); 77 78 data.uiwindow = uiwindow; 79 80#ifndef SDL_PLATFORM_VISIONOS 81 if (displaydata.uiscreen != [UIScreen mainScreen]) { 82 window->flags &= ~SDL_WINDOW_RESIZABLE; // window is NEVER resizable 83 window->flags &= ~SDL_WINDOW_INPUT_FOCUS; // never has input focus 84 window->flags |= SDL_WINDOW_BORDERLESS; // never has a status bar. 85 } 86#endif 87 88#if !defined(SDL_PLATFORM_TVOS) && !defined(SDL_PLATFORM_VISIONOS) 89 if (displaydata.uiscreen == [UIScreen mainScreen]) { 90 NSUInteger orients = UIKit_GetSupportedOrientations(window); 91 BOOL supportsLandscape = (orients & UIInterfaceOrientationMaskLandscape) != 0; 92 BOOL supportsPortrait = (orients & (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown)) != 0; 93 94 // Make sure the width/height are oriented correctly 95 if ((width > height && !supportsLandscape) || (height > width && !supportsPortrait)) { 96 int temp = width; 97 width = height; 98 height = temp; 99 } 100 } 101#endif // !SDL_PLATFORM_TVOS 102 103#if 0 // Don't set the x/y position, it's already placed on a display 104 window->x = 0; 105 window->y = 0; 106#endif 107 window->w = width; 108 window->h = height; 109 110 /* The View Controller will handle rotating the view when the device 111 * orientation changes. This will trigger resize events, if appropriate. */ 112 data.viewcontroller = [[SDL_uikitviewcontroller alloc] initWithSDLWindow:window]; 113 114 /* The window will initially contain a generic view so resizes, touch events, 115 * etc. can be handled without an active OpenGL view/context. */ 116 view = [[SDL_uikitview alloc] initWithFrame:frame]; 117 118 /* Sets this view as the controller's view, and adds the view to the window 119 * hierarchy. */ 120 [view setSDLWindow:window]; 121 122 SDL_PropertiesID props = SDL_GetWindowProperties(window); 123 SDL_SetPointerProperty(props, SDL_PROP_WINDOW_UIKIT_WINDOW_POINTER, (__bridge void *)data.uiwindow); 124 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_UIKIT_METAL_VIEW_TAG_NUMBER, SDL_METALVIEW_TAG); 125 126 return true; 127} 128 129bool UIKit_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props) 130{ 131 @autoreleasepool { 132 SDL_VideoDisplay *display = SDL_GetVideoDisplayForWindow(window); 133 SDL_UIKitDisplayData *data = (__bridge SDL_UIKitDisplayData *)display->internal; 134 SDL_Window *other; 135 136 // We currently only handle a single window per display on iOS 137 for (other = _this->windows; other; other = other->next) { 138 if (other != window && SDL_GetVideoDisplayForWindow(other) == display) { 139 return SDL_SetError("Only one window allowed per display."); 140 } 141 } 142 143 /* If monitor has a resolution of 0x0 (hasn't been explicitly set by the 144 * user, so it's in standby), try to force the display to a resolution 145 * that most closely matches the desired window size. */ 146#if !defined(SDL_PLATFORM_TVOS) && !defined(SDL_PLATFORM_VISIONOS) 147 const CGSize origsize = data.uiscreen.currentMode.size; 148 if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) { 149 SDL_DisplayMode bestmode; 150 bool include_high_density_modes = false; 151 if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) { 152 include_high_density_modes = true; 153 } 154 if (SDL_GetClosestFullscreenDisplayMode(display->id, window->w, window->h, 0.0f, include_high_density_modes, &bestmode)) { 155 SDL_UIKitDisplayModeData *modedata = (__bridge SDL_UIKitDisplayModeData *)bestmode.internal; 156 [data.uiscreen setCurrentMode:modedata.uiscreenmode]; 157 158 /* desktop_mode doesn't change here (the higher level will 159 * use it to set all the screens back to their defaults 160 * upon window destruction, SDL_Quit(), etc. */ 161 SDL_SetCurrentDisplayMode(display, &bestmode); 162 } 163 } 164 165 if (data.uiscreen == [UIScreen mainScreen]) { 166 if (window->flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS)) { 167 [UIApplication sharedApplication].statusBarHidden = YES; 168 } else { 169 [UIApplication sharedApplication].statusBarHidden = NO; 170 } 171 } 172#endif // !SDL_PLATFORM_TVOS 173 174 UIWindow *uiwindow = nil; 175 if (@available(iOS 13.0, tvOS 13.0, *)) { 176 UIWindowScene *scene = (__bridge UIWindowScene *)SDL_GetPointerProperty(create_props, SDL_PROP_WINDOW_CREATE_WINDOWSCENE_POINTER, NULL); 177 if (!scene) { 178 scene = UIKit_GetActiveWindowScene(); 179 } 180 if (scene) { 181 uiwindow = [[UIWindow alloc] initWithWindowScene:scene]; 182 } 183 } 184 if (!uiwindow) { 185 // ignore the size user requested, and make a fullscreen window 186#ifdef SDL_PLATFORM_VISIONOS 187 uiwindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, SDL_XR_SCREENWIDTH, SDL_XR_SCREENHEIGHT)]; 188#else 189 uiwindow = [[UIWindow alloc] initWithFrame:data.uiscreen.bounds]; 190#endif 191 } 192 193 // put the window on an external display if appropriate. 194#ifndef SDL_PLATFORM_VISIONOS 195 if (data.uiscreen != [UIScreen mainScreen]) { 196 [uiwindow setScreen:data.uiscreen]; 197 } 198#endif 199 200 if (!SetupWindowData(_this, window, uiwindow, true)) { 201 return false; 202 } 203 204#ifdef SDL_PLATFORM_VISIONOS 205 SDL_SetWindowSize(window, window->w, window->h); 206#endif 207 } 208 209 return true; 210} 211 212void UIKit_SetWindowTitle(SDL_VideoDevice *_this, SDL_Window *window) 213{ 214 @autoreleasepool { 215 SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)window->internal; 216 data.viewcontroller.title = @(window->title); 217 } 218} 219 220void UIKit_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window) 221{ 222#ifdef SDL_PLATFORM_VISIONOS 223 @autoreleasepool { 224 SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)window->internal; 225 UIWindowScene *scene = data.uiwindow.windowScene; 226 CGSize size = { window->pending.w, window->pending.h }; 227 UIWindowSceneGeometryPreferences *preferences = [[UIWindowSceneGeometryPreferencesVision alloc] initWithSize:size]; 228 [scene requestGeometryUpdateWithPreferences:preferences errorHandler:^(NSError * _Nonnull error) { 229 // Request failed, no worries 230 }]; 231 } 232#endif 233} 234 235void UIKit_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window) 236{ 237 @autoreleasepool { 238 SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)window->internal; 239 [data.uiwindow makeKeyAndVisible]; 240 241 // Make this window the current mouse focus for touch input 242 SDL_VideoDisplay *display = SDL_GetVideoDisplayForWindow(window); 243 SDL_UIKitDisplayData *displaydata = (__bridge SDL_UIKitDisplayData *)display->internal; 244#ifndef SDL_PLATFORM_VISIONOS 245 if (displaydata.uiscreen == [UIScreen mainScreen]) 246#endif 247 { 248 SDL_SetMouseFocus(window); 249 SDL_SetKeyboardFocus(window); 250 } 251 } 252} 253 254void UIKit_HideWindow(SDL_VideoDevice *_this, SDL_Window *window) 255{ 256 @autoreleasepool { 257 SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)window->internal; 258 data.uiwindow.hidden = YES; 259 } 260} 261 262void UIKit_RaiseWindow(SDL_VideoDevice *_this, SDL_Window *window) 263{ 264#if defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2) 265 /* We don't currently offer a concept of "raising" the SDL window, since 266 * we only allow one per display, in the iOS fashion. 267 * However, we use this entry point to rebind the context to the view 268 * during OnWindowRestored processing. */ 269 _this->GL_MakeCurrent(_this, _this->current_glwin, _this->current_glctx); 270#endif 271} 272 273static void UIKit_UpdateWindowBorder(SDL_VideoDevice *_this, SDL_Window *window) 274{ 275 SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)window->internal; 276 SDL_uikitviewcontroller *viewcontroller = data.viewcontroller; 277 278#if !defined(SDL_PLATFORM_TVOS) && !defined(SDL_PLATFORM_VISIONOS) 279 if (data.uiwindow.screen == [UIScreen mainScreen]) { 280 if (window->flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS)) { 281 [UIApplication sharedApplication].statusBarHidden = YES; 282 } else { 283 [UIApplication sharedApplication].statusBarHidden = NO; 284 } 285 286 [viewcontroller setNeedsStatusBarAppearanceUpdate]; 287 } 288 289 // Update the view's frame to account for the status bar change. 290 viewcontroller.view.frame = UIKit_ComputeViewFrame(window, data.uiwindow.screen); 291#endif // !SDL_PLATFORM_TVOS 292 293#ifdef SDL_IPHONE_KEYBOARD 294 // Make sure the view is offset correctly when the keyboard is visible. 295 [viewcontroller updateKeyboard]; 296#endif 297 298 [viewcontroller.view setNeedsLayout]; 299 [viewcontroller.view layoutIfNeeded]; 300} 301 302void UIKit_SetWindowBordered(SDL_VideoDevice *_this, SDL_Window *window, bool bordered) 303{ 304 @autoreleasepool { 305 if (bordered) { 306 window->flags &= ~SDL_WINDOW_BORDERLESS; 307 } else { 308 window->flags |= SDL_WINDOW_BORDERLESS; 309 } 310 UIKit_UpdateWindowBorder(_this, window); 311 } 312} 313 314SDL_FullscreenResult UIKit_SetWindowFullscreen(SDL_VideoDevice *_this, SDL_Window *window, SDL_VideoDisplay *display, SDL_FullscreenOp fullscreen) 315{ 316 @autoreleasepool { 317 SDL_SendWindowEvent(window, fullscreen ? SDL_EVENT_WINDOW_ENTER_FULLSCREEN : SDL_EVENT_WINDOW_LEAVE_FULLSCREEN, 0, 0); 318 UIKit_UpdateWindowBorder(_this, window); 319 } 320 return SDL_FULLSCREEN_SUCCEEDED; 321} 322 323void UIKit_UpdatePointerLock(SDL_VideoDevice *_this, SDL_Window *window) 324{ 325#ifndef SDL_PLATFORM_TVOS 326 @autoreleasepool { 327 SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)window->internal; 328 SDL_uikitviewcontroller *viewcontroller = data.viewcontroller; 329 if (@available(iOS 14.0, *)) { 330 [viewcontroller setNeedsUpdateOfPrefersPointerLocked]; 331 } 332 } 333#endif // !SDL_PLATFORM_TVOS 334} 335 336void UIKit_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window) 337{ 338 @autoreleasepool { 339 if (window->internal != NULL) { 340 SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)window->internal; 341 NSArray *views = nil; 342 343 [data.viewcontroller stopAnimation]; 344 345 /* Detach all views from this window. We use a copy of the array 346 * because setSDLWindow will remove the object from the original 347 * array, which would be undesirable if we were iterating over it. */ 348 views = [data.views copy]; 349 for (SDL_uikitview *view in views) { 350 [view setSDLWindow:NULL]; 351 } 352 353 /* iOS may still hold a reference to the window after we release it. 354 * We want to make sure the SDL view controller isn't accessed in 355 * that case, because it would contain an invalid pointer to the old 356 * SDL window. */ 357 data.uiwindow.rootViewController = nil; 358 data.uiwindow.hidden = YES; 359 360 CFRelease(window->internal); 361 window->internal = NULL; 362 } 363 } 364} 365 366void UIKit_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window *window, int *w, int *h) 367{ 368 @autoreleasepool { 369 SDL_UIKitWindowData *windata = (__bridge SDL_UIKitWindowData *)window->internal; 370 UIView *view = windata.viewcontroller.view; 371 CGSize size = view.bounds.size; 372 CGFloat scale = 1.0; 373 374 375 if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) { 376#ifndef SDL_PLATFORM_VISIONOS 377 scale = windata.uiwindow.screen.nativeScale; 378#else 379 scale = 2.0; 380#endif 381 } 382 383 384 /* Integer truncation of fractional values matches SDL_uikitmetalview and 385 * SDL_uikitopenglview. */ 386 *w = (int)(size.width * scale); 387 *h = (int)(size.height * scale); 388 } 389} 390 391#ifndef SDL_PLATFORM_TVOS 392NSUInteger 393UIKit_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_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)window->internal; 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 { 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->floating.w >= window->floating.h) { 435 orientationMask |= UIInterfaceOrientationMaskLandscape; 436 } 437 if (window->floating.h >= window->floating.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 // !SDL_PLATFORM_TVOS 458 459bool SDL_SetiOSAnimationCallback(SDL_Window *window, int interval, SDL_iOSAnimationCallback callback, void *callbackParam) 460{ 461 if (!window || !window->internal) { 462 return SDL_SetError("Invalid window"); 463 } 464 465 @autoreleasepool { 466 SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)window->internal; 467 [data.viewcontroller setAnimationCallback:interval 468 callback:callback 469 callbackParam:callbackParam]; 470 } 471 472 return true; 473} 474 475#endif // SDL_VIDEO_DRIVER_UIKIT 476
[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.