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