1// 2// cocoa_sdl_helpers.m 3// Warzone 4// 5// Copyright © 2017 pastdue (https://github.com/past-due/) 6// 7// Permission is hereby granted, free of charge, to any person obtaining a copy 8// of this software and associated documentation files (the "Software"), to deal 9// in the Software without restriction, including without limitation the rights 10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11// copies of the Software, and to permit persons to whom the Software is 12// furnished to do so, subject to the following conditions: 13// 14// The above copyright notice and this permission notice shall be included in all 15// copies or substantial portions of the Software. 16// 17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23// SOFTWARE. 24// 25 26#import <Foundation/Foundation.h> 27#import <Cocoa/Cocoa.h> 28#import <Availability.h> 29#import "cocoa_sdl_helpers.h" 30#include "SDL_syswm.h" 31#include <mach-o/dyld.h> 32 33bool cocoaIsNSWindowFullscreened(NSWindow __unsafe_unretained *window) 34{ 35// note use of 101200 instead of __MAC_10_12 (to support earlier SDKs) 36#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101200 37 return [window styleMask] & NSFullScreenWindowMask; 38#else 39 return [window styleMask] & NSWindowStyleMaskFullScreen; 40#endif 41} 42 43bool cocoaIsSDLWindowFullscreened(SDL_Window *window) 44{ 45 if (window == nil) return false; 46 SDL_SysWMinfo info; 47 SDL_VERSION(&info.version); /* initialize info structure with SDL version info */ 48 if(SDL_GetWindowWMInfo(window, &info) == SDL_FALSE) { 49 NSLog(@"Unable to get SDL_SysWMinfo, with error: %s", SDL_GetError()); 50 return false; 51 } 52 if (info.subsystem != SDL_SYSWM_COCOA) { 53 NSLog(@"Unexpected SDL subsystem: %d", info.subsystem); 54 return false; 55 } 56 assert(info.info.cocoa.window != nil); 57 return cocoaIsNSWindowFullscreened(info.info.cocoa.window); 58} 59 60std::string cocoaGetCurrentExecutablePath() 61{ 62 std::string result; 63 char path[MAXPATHLEN] = {}; 64 uint32_t size = MAXPATHLEN; 65 int ret = _NSGetExecutablePath(path, &size); 66 if (ret == 0) 67 { 68 result = path; 69 } 70 else 71 { 72 // allocate larger buffer 73 char* larger_path = (char*)malloc(size); 74 ret =_NSGetExecutablePath(larger_path, &size); 75 if (ret == 0) 76 { 77 result = larger_path; 78 } 79 free(larger_path); 80 } 81 82 return result; 83} 84 85std::string cocoaGetFrameworksPath(const char* resourceName) 86{ 87 std::string path = cocoaGetCurrentExecutablePath(); 88 if (path.empty()) 89 { 90 return ""; 91 } 92 // Executable path will end with "/MacOS/<executable name>" 93 // Remove last two path components 94 for (size_t i = 0; i < 2; i++) 95 { 96 size_t lastSlashPos = path.rfind("/"); 97 if (lastSlashPos == std::string::npos) 98 { 99 break; 100 } 101 path = path.substr(0, lastSlashPos); 102 } 103 if (path.empty()) 104 { 105 return ""; 106 } 107 // Append "Frameworks" folder name 108 path += "/Frameworks/"; 109 // Append resourceName (if provided) 110 if (resourceName != nullptr) 111 { 112 path += resourceName; 113 } 114 return path; 115} 116