diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/cocoa/SDL_cocoavideo.m')
| -rw-r--r-- | contrib/SDL-3.2.8/src/video/cocoa/SDL_cocoavideo.m | 337 |
1 files changed, 337 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/cocoa/SDL_cocoavideo.m b/contrib/SDL-3.2.8/src/video/cocoa/SDL_cocoavideo.m new file mode 100644 index 0000000..81baf78 --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/cocoa/SDL_cocoavideo.m | |||
| @@ -0,0 +1,337 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 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 | #ifdef SDL_VIDEO_DRIVER_COCOA | ||
| 24 | |||
| 25 | #if !__has_feature(objc_arc) | ||
| 26 | #error SDL must be built with Objective-C ARC (automatic reference counting) enabled | ||
| 27 | #endif | ||
| 28 | |||
| 29 | #include "SDL_cocoavideo.h" | ||
| 30 | #include "SDL_cocoavulkan.h" | ||
| 31 | #include "SDL_cocoametalview.h" | ||
| 32 | #include "SDL_cocoaopengles.h" | ||
| 33 | #include "SDL_cocoamessagebox.h" | ||
| 34 | #include "SDL_cocoashape.h" | ||
| 35 | |||
| 36 | #include "../../events/SDL_keyboard_c.h" | ||
| 37 | #include "../../events/SDL_mouse_c.h" | ||
| 38 | |||
| 39 | @implementation SDL_CocoaVideoData | ||
| 40 | |||
| 41 | @end | ||
| 42 | |||
| 43 | // Initialization/Query functions | ||
| 44 | static bool Cocoa_VideoInit(SDL_VideoDevice *_this); | ||
| 45 | static void Cocoa_VideoQuit(SDL_VideoDevice *_this); | ||
| 46 | |||
| 47 | // Cocoa driver bootstrap functions | ||
| 48 | |||
| 49 | static void Cocoa_DeleteDevice(SDL_VideoDevice *device) | ||
| 50 | { | ||
| 51 | @autoreleasepool { | ||
| 52 | if (device->wakeup_lock) { | ||
| 53 | SDL_DestroyMutex(device->wakeup_lock); | ||
| 54 | } | ||
| 55 | CFBridgingRelease(device->internal); | ||
| 56 | SDL_free(device); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | static SDL_VideoDevice *Cocoa_CreateDevice(void) | ||
| 61 | { | ||
| 62 | @autoreleasepool { | ||
| 63 | SDL_VideoDevice *device; | ||
| 64 | SDL_CocoaVideoData *data; | ||
| 65 | |||
| 66 | if (![NSThread isMainThread]) { | ||
| 67 | return NULL; // this doesn't SDL_SetError() because SDL_VideoInit is just going to overwrite it. | ||
| 68 | } | ||
| 69 | |||
| 70 | Cocoa_RegisterApp(); | ||
| 71 | |||
| 72 | // Initialize all variables that we clean on shutdown | ||
| 73 | device = (SDL_VideoDevice *)SDL_calloc(1, sizeof(SDL_VideoDevice)); | ||
| 74 | if (device) { | ||
| 75 | data = [[SDL_CocoaVideoData alloc] init]; | ||
| 76 | } else { | ||
| 77 | data = nil; | ||
| 78 | } | ||
| 79 | if (!data) { | ||
| 80 | SDL_free(device); | ||
| 81 | return NULL; | ||
| 82 | } | ||
| 83 | device->internal = (SDL_VideoData *)CFBridgingRetain(data); | ||
| 84 | device->wakeup_lock = SDL_CreateMutex(); | ||
| 85 | device->system_theme = Cocoa_GetSystemTheme(); | ||
| 86 | |||
| 87 | // Set the function pointers | ||
| 88 | device->VideoInit = Cocoa_VideoInit; | ||
| 89 | device->VideoQuit = Cocoa_VideoQuit; | ||
| 90 | device->GetDisplayBounds = Cocoa_GetDisplayBounds; | ||
| 91 | device->GetDisplayUsableBounds = Cocoa_GetDisplayUsableBounds; | ||
| 92 | device->GetDisplayModes = Cocoa_GetDisplayModes; | ||
| 93 | device->SetDisplayMode = Cocoa_SetDisplayMode; | ||
| 94 | device->PumpEvents = Cocoa_PumpEvents; | ||
| 95 | device->WaitEventTimeout = Cocoa_WaitEventTimeout; | ||
| 96 | device->SendWakeupEvent = Cocoa_SendWakeupEvent; | ||
| 97 | device->SuspendScreenSaver = Cocoa_SuspendScreenSaver; | ||
| 98 | |||
| 99 | device->CreateSDLWindow = Cocoa_CreateWindow; | ||
| 100 | device->SetWindowTitle = Cocoa_SetWindowTitle; | ||
| 101 | device->SetWindowIcon = Cocoa_SetWindowIcon; | ||
| 102 | device->SetWindowPosition = Cocoa_SetWindowPosition; | ||
| 103 | device->SetWindowSize = Cocoa_SetWindowSize; | ||
| 104 | device->SetWindowMinimumSize = Cocoa_SetWindowMinimumSize; | ||
| 105 | device->SetWindowMaximumSize = Cocoa_SetWindowMaximumSize; | ||
| 106 | device->SetWindowAspectRatio = Cocoa_SetWindowAspectRatio; | ||
| 107 | device->SetWindowOpacity = Cocoa_SetWindowOpacity; | ||
| 108 | device->GetWindowSizeInPixels = Cocoa_GetWindowSizeInPixels; | ||
| 109 | device->ShowWindow = Cocoa_ShowWindow; | ||
| 110 | device->HideWindow = Cocoa_HideWindow; | ||
| 111 | device->RaiseWindow = Cocoa_RaiseWindow; | ||
| 112 | device->MaximizeWindow = Cocoa_MaximizeWindow; | ||
| 113 | device->MinimizeWindow = Cocoa_MinimizeWindow; | ||
| 114 | device->RestoreWindow = Cocoa_RestoreWindow; | ||
| 115 | device->SetWindowBordered = Cocoa_SetWindowBordered; | ||
| 116 | device->SetWindowResizable = Cocoa_SetWindowResizable; | ||
| 117 | device->SetWindowAlwaysOnTop = Cocoa_SetWindowAlwaysOnTop; | ||
| 118 | device->SetWindowFullscreen = Cocoa_SetWindowFullscreen; | ||
| 119 | device->GetWindowICCProfile = Cocoa_GetWindowICCProfile; | ||
| 120 | device->GetDisplayForWindow = Cocoa_GetDisplayForWindow; | ||
| 121 | device->SetWindowMouseRect = Cocoa_SetWindowMouseRect; | ||
| 122 | device->SetWindowMouseGrab = Cocoa_SetWindowMouseGrab; | ||
| 123 | device->SetWindowKeyboardGrab = Cocoa_SetWindowKeyboardGrab; | ||
| 124 | device->DestroyWindow = Cocoa_DestroyWindow; | ||
| 125 | device->SetWindowHitTest = Cocoa_SetWindowHitTest; | ||
| 126 | device->AcceptDragAndDrop = Cocoa_AcceptDragAndDrop; | ||
| 127 | device->UpdateWindowShape = Cocoa_UpdateWindowShape; | ||
| 128 | device->FlashWindow = Cocoa_FlashWindow; | ||
| 129 | device->SetWindowFocusable = Cocoa_SetWindowFocusable; | ||
| 130 | device->SetWindowParent = Cocoa_SetWindowParent; | ||
| 131 | device->SetWindowModal = Cocoa_SetWindowModal; | ||
| 132 | device->SyncWindow = Cocoa_SyncWindow; | ||
| 133 | |||
| 134 | #ifdef SDL_VIDEO_OPENGL_CGL | ||
| 135 | device->GL_LoadLibrary = Cocoa_GL_LoadLibrary; | ||
| 136 | device->GL_GetProcAddress = Cocoa_GL_GetProcAddress; | ||
| 137 | device->GL_UnloadLibrary = Cocoa_GL_UnloadLibrary; | ||
| 138 | device->GL_CreateContext = Cocoa_GL_CreateContext; | ||
| 139 | device->GL_MakeCurrent = Cocoa_GL_MakeCurrent; | ||
| 140 | device->GL_SetSwapInterval = Cocoa_GL_SetSwapInterval; | ||
| 141 | device->GL_GetSwapInterval = Cocoa_GL_GetSwapInterval; | ||
| 142 | device->GL_SwapWindow = Cocoa_GL_SwapWindow; | ||
| 143 | device->GL_DestroyContext = Cocoa_GL_DestroyContext; | ||
| 144 | device->GL_GetEGLSurface = NULL; | ||
| 145 | #endif | ||
| 146 | #ifdef SDL_VIDEO_OPENGL_EGL | ||
| 147 | #ifdef SDL_VIDEO_OPENGL_CGL | ||
| 148 | if (SDL_GetHintBoolean(SDL_HINT_VIDEO_FORCE_EGL, false)) { | ||
| 149 | #endif | ||
| 150 | device->GL_LoadLibrary = Cocoa_GLES_LoadLibrary; | ||
| 151 | device->GL_GetProcAddress = Cocoa_GLES_GetProcAddress; | ||
| 152 | device->GL_UnloadLibrary = Cocoa_GLES_UnloadLibrary; | ||
| 153 | device->GL_CreateContext = Cocoa_GLES_CreateContext; | ||
| 154 | device->GL_MakeCurrent = Cocoa_GLES_MakeCurrent; | ||
| 155 | device->GL_SetSwapInterval = Cocoa_GLES_SetSwapInterval; | ||
| 156 | device->GL_GetSwapInterval = Cocoa_GLES_GetSwapInterval; | ||
| 157 | device->GL_SwapWindow = Cocoa_GLES_SwapWindow; | ||
| 158 | device->GL_DestroyContext = Cocoa_GLES_DestroyContext; | ||
| 159 | device->GL_GetEGLSurface = Cocoa_GLES_GetEGLSurface; | ||
| 160 | #ifdef SDL_VIDEO_OPENGL_CGL | ||
| 161 | } | ||
| 162 | #endif | ||
| 163 | #endif | ||
| 164 | |||
| 165 | #ifdef SDL_VIDEO_VULKAN | ||
| 166 | device->Vulkan_LoadLibrary = Cocoa_Vulkan_LoadLibrary; | ||
| 167 | device->Vulkan_UnloadLibrary = Cocoa_Vulkan_UnloadLibrary; | ||
| 168 | device->Vulkan_GetInstanceExtensions = Cocoa_Vulkan_GetInstanceExtensions; | ||
| 169 | device->Vulkan_CreateSurface = Cocoa_Vulkan_CreateSurface; | ||
| 170 | device->Vulkan_DestroySurface = Cocoa_Vulkan_DestroySurface; | ||
| 171 | #endif | ||
| 172 | |||
| 173 | #ifdef SDL_VIDEO_METAL | ||
| 174 | device->Metal_CreateView = Cocoa_Metal_CreateView; | ||
| 175 | device->Metal_DestroyView = Cocoa_Metal_DestroyView; | ||
| 176 | device->Metal_GetLayer = Cocoa_Metal_GetLayer; | ||
| 177 | #endif | ||
| 178 | |||
| 179 | device->StartTextInput = Cocoa_StartTextInput; | ||
| 180 | device->StopTextInput = Cocoa_StopTextInput; | ||
| 181 | device->UpdateTextInputArea = Cocoa_UpdateTextInputArea; | ||
| 182 | |||
| 183 | device->SetClipboardData = Cocoa_SetClipboardData; | ||
| 184 | device->GetClipboardData = Cocoa_GetClipboardData; | ||
| 185 | device->HasClipboardData = Cocoa_HasClipboardData; | ||
| 186 | |||
| 187 | device->free = Cocoa_DeleteDevice; | ||
| 188 | |||
| 189 | device->device_caps = VIDEO_DEVICE_CAPS_HAS_POPUP_WINDOW_SUPPORT | | ||
| 190 | VIDEO_DEVICE_CAPS_SENDS_FULLSCREEN_DIMENSIONS; | ||
| 191 | return device; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | VideoBootStrap COCOA_bootstrap = { | ||
| 196 | "cocoa", "SDL Cocoa video driver", | ||
| 197 | Cocoa_CreateDevice, | ||
| 198 | Cocoa_ShowMessageBox, | ||
| 199 | false | ||
| 200 | }; | ||
| 201 | |||
| 202 | static bool Cocoa_VideoInit(SDL_VideoDevice *_this) | ||
| 203 | { | ||
| 204 | @autoreleasepool { | ||
| 205 | SDL_CocoaVideoData *data = (__bridge SDL_CocoaVideoData *)_this->internal; | ||
| 206 | |||
| 207 | Cocoa_InitModes(_this); | ||
| 208 | Cocoa_InitKeyboard(_this); | ||
| 209 | if (!Cocoa_InitMouse(_this)) { | ||
| 210 | return false; | ||
| 211 | } | ||
| 212 | if (!Cocoa_InitPen(_this)) { | ||
| 213 | return false; | ||
| 214 | } | ||
| 215 | |||
| 216 | // Assume we have a mouse and keyboard | ||
| 217 | // We could use GCMouse and GCKeyboard if we needed to, as is done in SDL_uikitevents.m | ||
| 218 | SDL_AddKeyboard(SDL_DEFAULT_KEYBOARD_ID, NULL, false); | ||
| 219 | SDL_AddMouse(SDL_DEFAULT_MOUSE_ID, NULL, false); | ||
| 220 | |||
| 221 | data.allow_spaces = SDL_GetHintBoolean(SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES, true); | ||
| 222 | data.trackpad_is_touch_only = SDL_GetHintBoolean(SDL_HINT_TRACKPAD_IS_TOUCH_ONLY, false); | ||
| 223 | SDL_AddHintCallback(SDL_HINT_VIDEO_MAC_FULLSCREEN_MENU_VISIBILITY, Cocoa_MenuVisibilityCallback, NULL); | ||
| 224 | |||
| 225 | data.swaplock = SDL_CreateMutex(); | ||
| 226 | if (!data.swaplock) { | ||
| 227 | return false; | ||
| 228 | } | ||
| 229 | |||
| 230 | return true; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | void Cocoa_VideoQuit(SDL_VideoDevice *_this) | ||
| 235 | { | ||
| 236 | @autoreleasepool { | ||
| 237 | SDL_CocoaVideoData *data = (__bridge SDL_CocoaVideoData *)_this->internal; | ||
| 238 | Cocoa_QuitModes(_this); | ||
| 239 | Cocoa_QuitKeyboard(_this); | ||
| 240 | Cocoa_QuitMouse(_this); | ||
| 241 | Cocoa_QuitPen(_this); | ||
| 242 | SDL_DestroyMutex(data.swaplock); | ||
| 243 | data.swaplock = NULL; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | // This function assumes that it's called from within an autorelease pool | ||
| 248 | SDL_SystemTheme Cocoa_GetSystemTheme(void) | ||
| 249 | { | ||
| 250 | if (@available(macOS 10.14, *)) { | ||
| 251 | NSAppearance* appearance = [[NSApplication sharedApplication] effectiveAppearance]; | ||
| 252 | |||
| 253 | if ([appearance.name containsString: @"Dark"]) { | ||
| 254 | return SDL_SYSTEM_THEME_DARK; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | return SDL_SYSTEM_THEME_LIGHT; | ||
| 258 | } | ||
| 259 | |||
| 260 | // This function assumes that it's called from within an autorelease pool | ||
| 261 | NSImage *Cocoa_CreateImage(SDL_Surface *surface) | ||
| 262 | { | ||
| 263 | NSImage *img; | ||
| 264 | |||
| 265 | img = [[NSImage alloc] initWithSize:NSMakeSize(surface->w, surface->h)]; | ||
| 266 | if (img == nil) { | ||
| 267 | return nil; | ||
| 268 | } | ||
| 269 | |||
| 270 | SDL_Surface **images = SDL_GetSurfaceImages(surface, NULL); | ||
| 271 | if (!images) { | ||
| 272 | return nil; | ||
| 273 | } | ||
| 274 | |||
| 275 | for (int i = 0; images[i]; ++i) { | ||
| 276 | SDL_Surface *converted = SDL_ConvertSurface(images[i], SDL_PIXELFORMAT_RGBA32); | ||
| 277 | if (!converted) { | ||
| 278 | SDL_free(images); | ||
| 279 | return nil; | ||
| 280 | } | ||
| 281 | |||
| 282 | // Premultiply the alpha channel | ||
| 283 | SDL_PremultiplySurfaceAlpha(converted, false); | ||
| 284 | |||
| 285 | NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL | ||
| 286 | pixelsWide:converted->w | ||
| 287 | pixelsHigh:converted->h | ||
| 288 | bitsPerSample:8 | ||
| 289 | samplesPerPixel:4 | ||
| 290 | hasAlpha:YES | ||
| 291 | isPlanar:NO | ||
| 292 | colorSpaceName:NSDeviceRGBColorSpace | ||
| 293 | bytesPerRow:converted->pitch | ||
| 294 | bitsPerPixel:SDL_BITSPERPIXEL(converted->format)]; | ||
| 295 | if (imgrep == nil) { | ||
| 296 | SDL_free(images); | ||
| 297 | SDL_DestroySurface(converted); | ||
| 298 | return nil; | ||
| 299 | } | ||
| 300 | |||
| 301 | // Copy the pixels | ||
| 302 | Uint8 *pixels = [imgrep bitmapData]; | ||
| 303 | SDL_memcpy(pixels, converted->pixels, (size_t)converted->h * converted->pitch); | ||
| 304 | SDL_DestroySurface(converted); | ||
| 305 | |||
| 306 | // Add the image representation | ||
| 307 | [img addRepresentation:imgrep]; | ||
| 308 | } | ||
| 309 | SDL_free(images); | ||
| 310 | |||
| 311 | return img; | ||
| 312 | } | ||
| 313 | |||
| 314 | /* | ||
| 315 | * macOS log support. | ||
| 316 | * | ||
| 317 | * This doesn't really have anything to do with the interfaces of the SDL video | ||
| 318 | * subsystem, but we need to stuff this into an Objective-C source code file. | ||
| 319 | * | ||
| 320 | * NOTE: This is copypasted in src/video/uikit/SDL_uikitvideo.m! Be sure both | ||
| 321 | * versions remain identical! | ||
| 322 | */ | ||
| 323 | |||
| 324 | void SDL_NSLog(const char *prefix, const char *text) | ||
| 325 | { | ||
| 326 | @autoreleasepool { | ||
| 327 | NSString *nsText = [NSString stringWithUTF8String:text]; | ||
| 328 | if (prefix && *prefix) { | ||
| 329 | NSString *nsPrefix = [NSString stringWithUTF8String:prefix]; | ||
| 330 | NSLog(@"%@%@", nsPrefix, nsText); | ||
| 331 | } else { | ||
| 332 | NSLog(@"%@", nsText); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | } | ||
| 336 | |||
| 337 | #endif // SDL_VIDEO_DRIVER_COCOA | ||
