| 1 |
/**************************************************************************/ |
| 2 |
/* os_macos.mm */ |
| 3 |
/**************************************************************************/ |
| 4 |
/* This file is part of: */ |
| 5 |
/* GODOT ENGINE */ |
| 6 |
/* https://godotengine.org */ |
| 7 |
/**************************************************************************/ |
| 8 |
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 |
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 |
/* */ |
| 11 |
/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 |
/* a copy of this software and associated documentation files (the */ |
| 13 |
/* "Software"), to deal in the Software without restriction, including */ |
| 14 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 |
/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 |
/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 |
/* the following conditions: */ |
| 18 |
/* */ |
| 19 |
/* The above copyright notice and this permission notice shall be */ |
| 20 |
/* included in all copies or substantial portions of the Software. */ |
| 21 |
/* */ |
| 22 |
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 |
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 |
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 |
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 |
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 |
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 |
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 |
/**************************************************************************/ |
| 30 |
|
| 31 |
#import "os_macos.h" |
| 32 |
|
| 33 |
#import "dir_access_macos.h" |
| 34 |
#ifdef TOOLS_ENABLED |
| 35 |
#import "display_server_embedded.h" |
| 36 |
#endif |
| 37 |
#import "display_server_macos.h" |
| 38 |
#import "godot_application.h" |
| 39 |
#import "godot_application_delegate.h" |
| 40 |
|
| 41 |
#include "core/crypto/crypto_core.h" |
| 42 |
#include "core/io/file_access.h" |
| 43 |
#include "core/os/main_loop.h" |
| 44 |
#include "core/profiling/profiling.h" |
| 45 |
#include "core/version_generated.gen.h" |
| 46 |
#include "drivers/apple/os_log_logger.h" |
| 47 |
#include "main/main.h" |
| 48 |
|
| 49 |
#ifdef SDL_ENABLED |
| 50 |
#include "drivers/sdl/joypad_sdl.h" |
| 51 |
#endif |
| 52 |
|
| 53 |
#include <dlfcn.h> |
| 54 |
#include <libproc.h> |
| 55 |
#import <mach-o/dyld.h> |
| 56 |
#include <os/log.h> |
| 57 |
#include <sys/sysctl.h> |
| 58 |
|
| 59 |
void OS_MacOS::add_frame_delay(bool p_can_draw, bool p_wake_for_events) { |
| 60 |
bool wake_for_events = p_wake_for_events; |
| 61 |
if (!wake_for_events) { |
| 62 |
NSArray<NSRunningApplication *> *proc_array = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.crowdcafe.windowmagnet"]; |
| 63 |
if (proc_array && proc_array.count > 0) { |
| 64 |
wake_for_events = true; |
| 65 |
} |
| 66 |
} |
| 67 |
if (wake_for_events) { |
| 68 |
uint64_t delay = get_frame_delay(p_can_draw); |
| 69 |
if (delay == 0) { |
| 70 |
return; |
| 71 |
} |
| 72 |
if (wait_timer) { |
| 73 |
CFRunLoopTimerInvalidate(wait_timer); |
| 74 |
CFRelease(wait_timer); |
| 75 |
} |
| 76 |
wait_timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + (double(delay) / 1000000.0), 0, 0, 0, |
| 77 |
^(CFRunLoopTimerRef timer) { |
| 78 |
CFRunLoopTimerInvalidate(wait_timer); |
| 79 |
CFRelease(wait_timer); |
| 80 |
wait_timer = nil; |
| 81 |
}); |
| 82 |
CFRunLoopAddTimer(CFRunLoopGetCurrent(), wait_timer, kCFRunLoopCommonModes); |
| 83 |
return; |
| 84 |
} |
| 85 |
OS_Unix::add_frame_delay(p_can_draw, wake_for_events); |
| 86 |
} |
| 87 |
|
| 88 |
void OS_MacOS::initialize() { |
| 89 |
crash_handler.initialize(); |
| 90 |
|
| 91 |
initialize_core(); |
| 92 |
} |
| 93 |
|
| 94 |
String OS_MacOS::get_model_name() const { |
| 95 |
char buffer[256]; |
| 96 |
size_t buffer_len = 256; |
| 97 |
if (sysctlbyname("hw.model", &buffer, &buffer_len, nullptr, 0) == 0 && buffer_len != 0) { |
| 98 |
return String::utf8(buffer, buffer_len); |
| 99 |
} |
| 100 |
return OS_Unix::get_model_name(); |
| 101 |
} |
| 102 |
|
| 103 |
String OS_MacOS::get_processor_name() const { |
| 104 |
char buffer[256]; |
| 105 |
size_t buffer_len = 256; |
| 106 |
if (sysctlbyname("machdep.cpu.brand_string", &buffer, &buffer_len, nullptr, 0) == 0) { |
| 107 |
return String::utf8(buffer, buffer_len); |
| 108 |
} |
| 109 |
ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name. Returning an empty string.")); |
| 110 |
} |
| 111 |
|
| 112 |
bool OS_MacOS::is_sandboxed() const { |
| 113 |
return has_environment("APP_SANDBOX_CONTAINER_ID"); |
| 114 |
} |
| 115 |
|
| 116 |
bool OS_MacOS::request_permission(const String &p_name) { |
| 117 |
if (@available(macOS 11.0, *)) { |
| 118 |
if (p_name == "macos.permission.RECORD_SCREEN") { |
| 119 |
if (CGPreflightScreenCaptureAccess()) { |
| 120 |
return true; |
| 121 |
} else { |
| 122 |
CGRequestScreenCaptureAccess(); |
| 123 |
return false; |
| 124 |
} |
| 125 |
} |
| 126 |
} else { |
| 127 |
if (p_name == "macos.permission.RECORD_SCREEN") { |
| 128 |
return true; |
| 129 |
} |
| 130 |
} |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
Vector<String> OS_MacOS::get_granted_permissions() const { |
| 135 |
Vector<String> ret; |
| 136 |
|
| 137 |
if (@available(macOS 11.0, *)) { |
| 138 |
if (CGPreflightScreenCaptureAccess()) { |
| 139 |
ret.push_back("macos.permission.RECORD_SCREEN"); |
| 140 |
} |
| 141 |
} else { |
| 142 |
ret.push_back("macos.permission.RECORD_SCREEN"); |
| 143 |
} |
| 144 |
|
| 145 |
if (is_sandboxed()) { |
| 146 |
NSArray *bookmarks = [[NSUserDefaults standardUserDefaults] arrayForKey:@"sec_bookmarks"]; |
| 147 |
for (id bookmark in bookmarks) { |
| 148 |
NSError *error = nil; |
| 149 |
BOOL isStale = NO; |
| 150 |
NSURL *url = [NSURL URLByResolvingBookmarkData:bookmark options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:&isStale error:&error]; |
| 151 |
if (!error && !isStale) { |
| 152 |
String url_string = String::utf8([[url path] UTF8String]); |
| 153 |
ret.push_back(url_string); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return ret; |
| 159 |
} |
| 160 |
|
| 161 |
void OS_MacOS::revoke_granted_permissions() { |
| 162 |
if (is_sandboxed()) { |
| 163 |
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"sec_bookmarks"]; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
#if TOOLS_ENABLED |
| 168 |
|
| 169 |
// Function to check if a debugger is attached to the current process |
| 170 |
bool OS_MacOS::is_debugger_attached() { |
| 171 |
int mib[4]; |
| 172 |
struct kinfo_proc info{}; |
| 173 |
size_t size = sizeof(info); |
| 174 |
|
| 175 |
// Initialize the flags so that, if sysctl fails, info.kp_proc.p_flag will be 0. |
| 176 |
info.kp_proc.p_flag = 0; |
| 177 |
|
| 178 |
// Initialize mib, which tells sysctl the info we want, in this case we're looking for information |
| 179 |
// about a specific process ID. |
| 180 |
mib[0] = CTL_KERN; |
| 181 |
mib[1] = KERN_PROC; |
| 182 |
mib[2] = KERN_PROC_PID; |
| 183 |
mib[3] = getpid(); |
| 184 |
|
| 185 |
if (sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0) != 0) { |
| 186 |
perror("sysctl"); |
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
return (info.kp_proc.p_flag & P_TRACED) != 0; |
| 191 |
} |
| 192 |
|
| 193 |
void OS_MacOS::wait_for_debugger(uint32_t p_msec) { |
| 194 |
if (p_msec == 0) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent(); |
| 199 |
CFTimeInterval wait_time = p_msec / 1000.0; |
| 200 |
|
| 201 |
NSTimer *timer = [NSTimer timerWithTimeInterval:0.100 |
| 202 |
repeats:YES |
| 203 |
block:^(NSTimer *t) { |
| 204 |
if (is_debugger_attached() || CFAbsoluteTimeGetCurrent() > start + wait_time) { |
| 205 |
[NSApp stopModalWithCode:NSModalResponseContinue]; |
| 206 |
[t invalidate]; |
| 207 |
} |
| 208 |
}]; |
| 209 |
|
| 210 |
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSModalPanelRunLoopMode]; |
| 211 |
|
| 212 |
pid_t pid = getpid(); |
| 213 |
alert(vformat("Attach debugger to pid: %d", pid)); |
| 214 |
|
| 215 |
print("continue..."); |
| 216 |
} |
| 217 |
|
| 218 |
#endif |
| 219 |
|
| 220 |
void OS_MacOS::initialize_core() { |
| 221 |
OS_Unix::initialize_core(); |
| 222 |
|
| 223 |
DirAccess::make_default<DirAccessMacOS>(DirAccess::ACCESS_RESOURCES); |
| 224 |
DirAccess::make_default<DirAccessMacOS>(DirAccess::ACCESS_USERDATA); |
| 225 |
DirAccess::make_default<DirAccessMacOS>(DirAccess::ACCESS_FILESYSTEM); |
| 226 |
} |
| 227 |
|
| 228 |
void OS_MacOS::finalize() { |
| 229 |
if (is_sandboxed()) { |
| 230 |
NSArray *bookmarks = [[NSUserDefaults standardUserDefaults] arrayForKey:@"sec_bookmarks"]; |
| 231 |
for (id bookmark in bookmarks) { |
| 232 |
NSError *error = nil; |
| 233 |
BOOL isStale = NO; |
| 234 |
NSURL *url = [NSURL URLByResolvingBookmarkData:bookmark options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:&isStale error:&error]; |
| 235 |
if (!error && !isStale) { |
| 236 |
[url stopAccessingSecurityScopedResource]; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
#ifdef COREMIDI_ENABLED |
| 242 |
midi_driver.close(); |
| 243 |
#endif |
| 244 |
|
| 245 |
delete_main_loop(); |
| 246 |
|
| 247 |
#ifdef SDL_ENABLED |
| 248 |
if (joypad_sdl) { |
| 249 |
memdelete(joypad_sdl); |
| 250 |
} |
| 251 |
#endif |
| 252 |
} |
| 253 |
|
| 254 |
void OS_MacOS::initialize_joypads() { |
| 255 |
#ifdef SDL_ENABLED |
| 256 |
joypad_sdl = memnew(JoypadSDL()); |
| 257 |
if (joypad_sdl->initialize() != OK) { |
| 258 |
ERR_PRINT("Couldn't initialize SDL joypad input driver."); |
| 259 |
memdelete(joypad_sdl); |
| 260 |
joypad_sdl = nullptr; |
| 261 |
} |
| 262 |
#endif |
| 263 |
} |
| 264 |
|
| 265 |
void OS_MacOS::set_main_loop(MainLoop *p_main_loop) { |
| 266 |
main_loop = p_main_loop; |
| 267 |
} |
| 268 |
|
| 269 |
void OS_MacOS::delete_main_loop() { |
| 270 |
if (!main_loop) { |
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
memdelete(main_loop); |
| 275 |
main_loop = nullptr; |
| 276 |
} |
| 277 |
|
| 278 |
void OS_MacOS::set_cmdline_platform_args(const List<String> &p_args) { |
| 279 |
launch_service_args = p_args; |
| 280 |
} |
| 281 |
|
| 282 |
List<String> OS_MacOS::get_cmdline_platform_args() const { |
| 283 |
return launch_service_args; |
| 284 |
} |
| 285 |
|
| 286 |
void OS_MacOS::load_shell_environment() const { |
| 287 |
static bool shell_env_loaded = false; |
| 288 |
if (unlikely(!shell_env_loaded)) { |
| 289 |
shell_env_loaded = true; |
| 290 |
if (OS::get_singleton()->has_environment("TERM") || OS::get_singleton()->has_environment("__GODOT_SHELL_ENV_SET")) { |
| 291 |
return; // Already started from terminal, or other the instance with the shell environment, do nothing. |
| 292 |
} |
| 293 |
String pipe; |
| 294 |
List<String> args; |
| 295 |
args.push_back("-c"); |
| 296 |
args.push_back(". /etc/zshrc;. /etc/zprofile;. ~/.zshenv;. ~/.zshrc;. ~/.zprofile;env"); |
| 297 |
Error err = OS::get_singleton()->execute("zsh", args, &pipe); |
| 298 |
if (err == OK) { |
| 299 |
Vector<String> env_vars = pipe.split("\n"); |
| 300 |
for (const String &E : env_vars) { |
| 301 |
Vector<String> tags = E.split("=", 2); |
| 302 |
if (tags.size() != 2 || tags[0] == "SHELL" || tags[0] == "USER" || tags[0] == "COMMAND_MODE" || tags[0] == "TMPDIR" || tags[0] == "TERM_SESSION_ID" || tags[0] == "PWD" || tags[0] == "OLDPWD" || tags[0] == "SHLVL" || tags[0] == "HOME" || tags[0] == "DISPLAY" || tags[0] == "LOGNAME" || tags[0] == "TERM" || tags[0] == "COLORTERM" || tags[0] == "_" || tags[0].begins_with("__CF") || tags[0].begins_with("XPC_") || tags[0].begins_with("__GODOT")) { |
| 303 |
continue; |
| 304 |
} |
| 305 |
OS::get_singleton()->set_environment(tags[0], tags[1]); |
| 306 |
} |
| 307 |
} |
| 308 |
OS::get_singleton()->set_environment("__GODOT_SHELL_ENV_SET", "1"); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
String OS_MacOS::get_name() const { |
| 313 |
return "macOS"; |
| 314 |
} |
| 315 |
|
| 316 |
String OS_MacOS::get_distribution_name() const { |
| 317 |
return get_name(); |
| 318 |
} |
| 319 |
|
| 320 |
String OS_MacOS::get_version() const { |
| 321 |
NSOperatingSystemVersion ver = [NSProcessInfo processInfo].operatingSystemVersion; |
| 322 |
return vformat("%d.%d.%d", (int64_t)ver.majorVersion, (int64_t)ver.minorVersion, (int64_t)ver.patchVersion); |
| 323 |
} |
| 324 |
|
| 325 |
String OS_MacOS::get_version_alias() const { |
| 326 |
NSOperatingSystemVersion ver = [NSProcessInfo processInfo].operatingSystemVersion; |
| 327 |
String macos_string; |
| 328 |
if (ver.majorVersion == 26) { |
| 329 |
macos_string += "Tahoe"; |
| 330 |
} else if (ver.majorVersion == 15) { |
| 331 |
macos_string += "Sequoia"; |
| 332 |
} else if (ver.majorVersion == 14) { |
| 333 |
macos_string += "Sonoma"; |
| 334 |
} else if (ver.majorVersion == 13) { |
| 335 |
macos_string += "Ventura"; |
| 336 |
} else if (ver.majorVersion == 12) { |
| 337 |
macos_string += "Monterey"; |
| 338 |
} else if (ver.majorVersion == 11 || (ver.majorVersion == 10 && ver.minorVersion == 16)) { |
| 339 |
// Big Sur was 10.16 during beta, but it became 11 for the stable version. |
| 340 |
macos_string += "Big Sur"; |
| 341 |
} else if (ver.majorVersion == 10 && ver.minorVersion == 15) { |
| 342 |
macos_string += "Catalina"; |
| 343 |
} else if (ver.majorVersion == 10 && ver.minorVersion == 14) { |
| 344 |
macos_string += "Mojave"; |
| 345 |
} else if (ver.majorVersion == 10 && ver.minorVersion == 13) { |
| 346 |
macos_string += "High Sierra"; |
| 347 |
} else { |
| 348 |
macos_string += "Unknown"; |
| 349 |
} |
| 350 |
// macOS versions older than 10.13 cannot run Godot. |
| 351 |
return vformat("%s (%s)", macos_string, get_version()); |
| 352 |
} |
| 353 |
|
| 354 |
void OS_MacOS::alert(const String &p_alert, const String &p_title) { |
| 355 |
NSAlert *window = [[NSAlert alloc] init]; |
| 356 |
NSString *ns_title = [NSString stringWithUTF8String:p_title.utf8().get_data()]; |
| 357 |
NSString *ns_alert = [NSString stringWithUTF8String:p_alert.utf8().get_data()]; |
| 358 |
|
| 359 |
NSTextField *text_field = [NSTextField labelWithString:ns_alert]; |
| 360 |
[text_field setAlignment:NSTextAlignmentCenter]; |
| 361 |
[window addButtonWithTitle:@"OK"]; |
| 362 |
[window setMessageText:ns_title]; |
| 363 |
[window setAccessoryView:text_field]; |
| 364 |
[window setAlertStyle:NSAlertStyleWarning]; |
| 365 |
|
| 366 |
id key_window = [[NSApplication sharedApplication] keyWindow]; |
| 367 |
[window runModal]; |
| 368 |
if (key_window) { |
| 369 |
[key_window makeKeyAndOrderFront:nil]; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
_FORCE_INLINE_ String OS_MacOS::get_framework_executable(const String &p_path) { |
| 374 |
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 375 |
|
| 376 |
// Read framework bundle to get executable name. |
| 377 |
NSURL *url = [NSURL fileURLWithPath:@(p_path.utf8().get_data())]; |
| 378 |
if (url) { |
| 379 |
NSBundle *bundle = [NSBundle bundleWithURL:url]; |
| 380 |
if (bundle) { |
| 381 |
String exe_path = String::utf8([[bundle executablePath] UTF8String]); |
| 382 |
if (da->file_exists(exe_path)) { |
| 383 |
return exe_path; |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
// Try default executable name (invalid framework). |
| 389 |
if (da->dir_exists(p_path) && da->file_exists(p_path.path_join(p_path.get_file().get_basename()))) { |
| 390 |
return p_path.path_join(p_path.get_file().get_basename()); |
| 391 |
} |
| 392 |
|
| 393 |
// Not a framework, try loading as .dylib. |
| 394 |
return p_path; |
| 395 |
} |
| 396 |
|
| 397 |
Error OS_MacOS::open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data) { |
| 398 |
String path = get_framework_executable(p_path); |
| 399 |
|
| 400 |
if (!FileAccess::exists(path)) { |
| 401 |
// Load .dylib or framework from within the executable path. |
| 402 |
path = get_framework_executable(get_executable_path().get_base_dir().path_join(p_path.get_file())); |
| 403 |
} |
| 404 |
|
| 405 |
if (!FileAccess::exists(path)) { |
| 406 |
// Load .dylib or framework from a standard macOS location. |
| 407 |
path = get_framework_executable(get_executable_path().get_base_dir().path_join("../Frameworks").path_join(p_path.get_file())); |
| 408 |
} |
| 409 |
|
| 410 |
if (!FileAccess::exists(path)) { |
| 411 |
// Try using path as is. macOS system libraries with `/usr/lib/*` path do not exist as physical files and are loaded from shared cache. |
| 412 |
path = p_path; |
| 413 |
} |
| 414 |
|
| 415 |
p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW); |
| 416 |
ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, dlerror())); |
| 417 |
|
| 418 |
if (p_data != nullptr && p_data->r_resolved_path != nullptr) { |
| 419 |
*p_data->r_resolved_path = path; |
| 420 |
} |
| 421 |
|
| 422 |
return OK; |
| 423 |
} |
| 424 |
|
| 425 |
MainLoop *OS_MacOS::get_main_loop() const { |
| 426 |
return main_loop; |
| 427 |
} |
| 428 |
|
| 429 |
String OS_MacOS::get_config_path() const { |
| 430 |
if (has_environment("HOME")) { |
| 431 |
return get_environment("HOME").path_join("Library/Application Support"); |
| 432 |
} |
| 433 |
return "."; |
| 434 |
} |
| 435 |
|
| 436 |
String OS_MacOS::get_data_path() const { |
| 437 |
return get_config_path(); |
| 438 |
} |
| 439 |
|
| 440 |
String OS_MacOS::get_cache_path() const { |
| 441 |
if (has_environment("HOME")) { |
| 442 |
return get_environment("HOME").path_join("Library/Caches"); |
| 443 |
} |
| 444 |
return get_config_path(); |
| 445 |
} |
| 446 |
|
| 447 |
String OS_MacOS::get_temp_path() const { |
| 448 |
static String ret; |
| 449 |
if (ret.is_empty()) { |
| 450 |
NSURL *url = [NSURL fileURLWithPath:NSTemporaryDirectory() |
| 451 |
isDirectory:YES]; |
| 452 |
if (url) { |
| 453 |
ret = String::utf8([url.path UTF8String]); |
| 454 |
ret = ret.trim_prefix("file://"); |
| 455 |
} |
| 456 |
} |
| 457 |
return ret; |
| 458 |
} |
| 459 |
|
| 460 |
String OS_MacOS::get_bundle_resource_dir() const { |
| 461 |
String ret; |
| 462 |
|
| 463 |
NSBundle *main = [NSBundle mainBundle]; |
| 464 |
if (main) { |
| 465 |
NSString *resource_path = [main resourcePath]; |
| 466 |
ret.append_utf8([resource_path UTF8String]); |
| 467 |
} |
| 468 |
return ret; |
| 469 |
} |
| 470 |
|
| 471 |
String OS_MacOS::get_bundle_icon_path() const { |
| 472 |
String ret; |
| 473 |
|
| 474 |
NSBundle *main = [NSBundle mainBundle]; |
| 475 |
if (main) { |
| 476 |
NSString *icon_path = [[main infoDictionary] objectForKey:@"CFBundleIconFile"]; |
| 477 |
if (icon_path) { |
| 478 |
ret.append_utf8([icon_path UTF8String]); |
| 479 |
} |
| 480 |
} |
| 481 |
return ret; |
| 482 |
} |
| 483 |
|
| 484 |
String OS_MacOS::get_bundle_icon_name() const { |
| 485 |
String ret; |
| 486 |
|
| 487 |
NSBundle *main = [NSBundle mainBundle]; |
| 488 |
if (main) { |
| 489 |
NSString *icon_name = [[main infoDictionary] objectForKey:@"CFBundleIconName"]; |
| 490 |
if (icon_name) { |
| 491 |
ret.append_utf8([icon_name UTF8String]); |
| 492 |
} |
| 493 |
} |
| 494 |
return ret; |
| 495 |
} |
| 496 |
|
| 497 |
// Get properly capitalized engine name for system paths |
| 498 |
String OS_MacOS::get_godot_dir_name() const { |
| 499 |
return String(GODOT_VERSION_SHORT_NAME).capitalize(); |
| 500 |
} |
| 501 |
|
| 502 |
String OS_MacOS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const { |
| 503 |
NSSearchPathDirectory id; |
| 504 |
bool found = true; |
| 505 |
|
| 506 |
switch (p_dir) { |
| 507 |
case SYSTEM_DIR_DESKTOP: { |
| 508 |
id = NSDesktopDirectory; |
| 509 |
} break; |
| 510 |
case SYSTEM_DIR_DOCUMENTS: { |
| 511 |
id = NSDocumentDirectory; |
| 512 |
} break; |
| 513 |
case SYSTEM_DIR_DOWNLOADS: { |
| 514 |
id = NSDownloadsDirectory; |
| 515 |
} break; |
| 516 |
case SYSTEM_DIR_MOVIES: { |
| 517 |
id = NSMoviesDirectory; |
| 518 |
} break; |
| 519 |
case SYSTEM_DIR_MUSIC: { |
| 520 |
id = NSMusicDirectory; |
| 521 |
} break; |
| 522 |
case SYSTEM_DIR_PICTURES: { |
| 523 |
id = NSPicturesDirectory; |
| 524 |
} break; |
| 525 |
default: { |
| 526 |
found = false; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
String ret; |
| 531 |
if (found) { |
| 532 |
NSArray *paths = NSSearchPathForDirectoriesInDomains(id, NSUserDomainMask, YES); |
| 533 |
if (paths && [paths count] >= 1) { |
| 534 |
ret.append_utf8([[paths firstObject] UTF8String]); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
return ret; |
| 539 |
} |
| 540 |
|
| 541 |
Error OS_MacOS::shell_show_in_file_manager(String p_path, bool p_open_folder) { |
| 542 |
bool open_folder = false; |
| 543 |
if (DirAccess::dir_exists_absolute(p_path) && p_open_folder) { |
| 544 |
open_folder = true; |
| 545 |
} |
| 546 |
|
| 547 |
if (!p_path.begins_with("file://")) { |
| 548 |
p_path = String("file://") + p_path; |
| 549 |
} |
| 550 |
|
| 551 |
NSString *string = [NSString stringWithUTF8String:p_path.utf8().get_data()]; |
| 552 |
NSURL *uri = [[NSURL alloc] initWithString:[string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]]; |
| 553 |
|
| 554 |
if (open_folder) { |
| 555 |
[[NSWorkspace sharedWorkspace] openURL:uri]; |
| 556 |
} else { |
| 557 |
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:@[ uri ]]; |
| 558 |
} |
| 559 |
return OK; |
| 560 |
} |
| 561 |
|
| 562 |
Error OS_MacOS::shell_open(const String &p_uri) { |
| 563 |
NSString *string = [NSString stringWithUTF8String:p_uri.utf8().get_data()]; |
| 564 |
NSURL *uri = [[NSURL alloc] initWithString:string]; |
| 565 |
if (!uri || !uri.scheme || [uri.scheme isEqual:@"file"]) { |
| 566 |
// No scheme set, assume "file://" and escape special characters. |
| 567 |
if (!p_uri.begins_with("file://")) { |
| 568 |
string = [NSString stringWithUTF8String:("file://" + p_uri).utf8().get_data()]; |
| 569 |
} |
| 570 |
uri = [[NSURL alloc] initWithString:[string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]]; |
| 571 |
} |
| 572 |
[[NSWorkspace sharedWorkspace] openURL:uri]; |
| 573 |
return OK; |
| 574 |
} |
| 575 |
|
| 576 |
String OS_MacOS::get_locale() const { |
| 577 |
NSString *locale_code = [[NSLocale preferredLanguages] objectAtIndex:0]; |
| 578 |
return String([locale_code UTF8String]).replace_char('-', '_'); |
| 579 |
} |
| 580 |
|
| 581 |
Vector<String> OS_MacOS::get_system_fonts() const { |
| 582 |
HashSet<String> font_names; |
| 583 |
CFArrayRef fonts = CTFontManagerCopyAvailableFontFamilyNames(); |
| 584 |
if (fonts) { |
| 585 |
for (CFIndex i = 0; i < CFArrayGetCount(fonts); i++) { |
| 586 |
CFStringRef cf_name = (CFStringRef)CFArrayGetValueAtIndex(fonts, i); |
| 587 |
if (cf_name && (CFStringGetLength(cf_name) > 0) && (CFStringCompare(cf_name, CFSTR("LastResort"), kCFCompareCaseInsensitive) != kCFCompareEqualTo) && (CFStringGetCharacterAtIndex(cf_name, 0) != '.')) { |
| 588 |
NSString *ns_name = (__bridge NSString *)cf_name; |
| 589 |
font_names.insert(String::utf8([ns_name UTF8String])); |
| 590 |
} |
| 591 |
} |
| 592 |
CFRelease(fonts); |
| 593 |
} |
| 594 |
|
| 595 |
Vector<String> ret; |
| 596 |
for (const String &E : font_names) { |
| 597 |
ret.push_back(E); |
| 598 |
} |
| 599 |
return ret; |
| 600 |
} |
| 601 |
|
| 602 |
String OS_MacOS::_get_default_fontname(const String &p_font_name) const { |
| 603 |
String font_name = p_font_name; |
| 604 |
if (font_name.to_lower() == "sans-serif") { |
| 605 |
font_name = "Helvetica"; |
| 606 |
} else if (font_name.to_lower() == "serif") { |
| 607 |
font_name = "Times"; |
| 608 |
} else if (font_name.to_lower() == "monospace") { |
| 609 |
font_name = "Courier"; |
| 610 |
} else if (font_name.to_lower() == "fantasy") { |
| 611 |
font_name = "Papyrus"; |
| 612 |
} else if (font_name.to_lower() == "cursive") { |
| 613 |
font_name = "Apple Chancery"; |
| 614 |
}; |
| 615 |
return font_name; |
| 616 |
} |
| 617 |
|
| 618 |
CGFloat OS_MacOS::_weight_to_ct(int p_weight) const { |
| 619 |
if (p_weight < 150) { |
| 620 |
return -0.80; |
| 621 |
} else if (p_weight < 250) { |
| 622 |
return -0.60; |
| 623 |
} else if (p_weight < 350) { |
| 624 |
return -0.40; |
| 625 |
} else if (p_weight < 450) { |
| 626 |
return 0.0; |
| 627 |
} else if (p_weight < 550) { |
| 628 |
return 0.23; |
| 629 |
} else if (p_weight < 650) { |
| 630 |
return 0.30; |
| 631 |
} else if (p_weight < 750) { |
| 632 |
return 0.40; |
| 633 |
} else if (p_weight < 850) { |
| 634 |
return 0.56; |
| 635 |
} else if (p_weight < 925) { |
| 636 |
return 0.62; |
| 637 |
} else { |
| 638 |
return 1.00; |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
CGFloat OS_MacOS::_stretch_to_ct(int p_stretch) const { |
| 643 |
if (p_stretch < 56) { |
| 644 |
return -0.5; |
| 645 |
} else if (p_stretch < 69) { |
| 646 |
return -0.37; |
| 647 |
} else if (p_stretch < 81) { |
| 648 |
return -0.25; |
| 649 |
} else if (p_stretch < 93) { |
| 650 |
return -0.13; |
| 651 |
} else if (p_stretch < 106) { |
| 652 |
return 0.0; |
| 653 |
} else if (p_stretch < 137) { |
| 654 |
return 0.13; |
| 655 |
} else if (p_stretch < 144) { |
| 656 |
return 0.25; |
| 657 |
} else if (p_stretch < 162) { |
| 658 |
return 0.37; |
| 659 |
} else { |
| 660 |
return 0.5; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
Vector<String> OS_MacOS::get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale, const String &p_script, int p_weight, int p_stretch, bool p_italic) const { |
| 665 |
Vector<String> ret; |
| 666 |
String font_name = _get_default_fontname(p_font_name); |
| 667 |
|
| 668 |
CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, font_name.utf8().get_data(), kCFStringEncodingUTF8); |
| 669 |
CTFontSymbolicTraits traits = 0; |
| 670 |
if (p_weight >= 700) { |
| 671 |
traits |= kCTFontBoldTrait; |
| 672 |
} |
| 673 |
if (p_italic) { |
| 674 |
traits |= kCTFontItalicTrait; |
| 675 |
} |
| 676 |
if (p_stretch < 100) { |
| 677 |
traits |= kCTFontCondensedTrait; |
| 678 |
} else if (p_stretch > 100) { |
| 679 |
traits |= kCTFontExpandedTrait; |
| 680 |
} |
| 681 |
|
| 682 |
CFNumberRef sym_traits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &traits); |
| 683 |
CFMutableDictionaryRef traits_dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr); |
| 684 |
CFDictionaryAddValue(traits_dict, kCTFontSymbolicTrait, sym_traits); |
| 685 |
|
| 686 |
CGFloat weight = _weight_to_ct(p_weight); |
| 687 |
CFNumberRef font_weight = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &weight); |
| 688 |
CFDictionaryAddValue(traits_dict, kCTFontWeightTrait, font_weight); |
| 689 |
|
| 690 |
CGFloat stretch = _stretch_to_ct(p_stretch); |
| 691 |
CFNumberRef font_stretch = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &stretch); |
| 692 |
CFDictionaryAddValue(traits_dict, kCTFontWidthTrait, font_stretch); |
| 693 |
|
| 694 |
CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr); |
| 695 |
CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, name); |
| 696 |
CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits_dict); |
| 697 |
|
| 698 |
CTFontDescriptorRef font = CTFontDescriptorCreateWithAttributes(attributes); |
| 699 |
if (font) { |
| 700 |
CTFontRef family = CTFontCreateWithFontDescriptor(font, 0, nullptr); |
| 701 |
if (family) { |
| 702 |
CFStringRef string = CFStringCreateWithCString(kCFAllocatorDefault, p_text.utf8().get_data(), kCFStringEncodingUTF8); |
| 703 |
CFRange range = CFRangeMake(0, CFStringGetLength(string)); |
| 704 |
CTFontRef fallback_family = CTFontCreateForString(family, string, range); |
| 705 |
if (fallback_family) { |
| 706 |
CTFontDescriptorRef fallback_font = CTFontCopyFontDescriptor(fallback_family); |
| 707 |
if (fallback_font) { |
| 708 |
CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute(fallback_font, kCTFontURLAttribute); |
| 709 |
if (url) { |
| 710 |
NSString *font_path = [NSString stringWithString:[(__bridge NSURL *)url path]]; |
| 711 |
ret.push_back(String::utf8([font_path UTF8String])); |
| 712 |
CFRelease(url); |
| 713 |
} |
| 714 |
CFRelease(fallback_font); |
| 715 |
} |
| 716 |
CFRelease(fallback_family); |
| 717 |
} |
| 718 |
CFRelease(string); |
| 719 |
CFRelease(family); |
| 720 |
} |
| 721 |
CFRelease(font); |
| 722 |
} |
| 723 |
|
| 724 |
CFRelease(attributes); |
| 725 |
CFRelease(traits_dict); |
| 726 |
CFRelease(sym_traits); |
| 727 |
CFRelease(font_stretch); |
| 728 |
CFRelease(font_weight); |
| 729 |
CFRelease(name); |
| 730 |
|
| 731 |
return ret; |
| 732 |
} |
| 733 |
|
| 734 |
String OS_MacOS::get_system_font_path(const String &p_font_name, int p_weight, int p_stretch, bool p_italic) const { |
| 735 |
String ret; |
| 736 |
String font_name = _get_default_fontname(p_font_name); |
| 737 |
|
| 738 |
CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, font_name.utf8().get_data(), kCFStringEncodingUTF8); |
| 739 |
|
| 740 |
CTFontSymbolicTraits traits = 0; |
| 741 |
if (p_weight > 700) { |
| 742 |
traits |= kCTFontBoldTrait; |
| 743 |
} |
| 744 |
if (p_italic) { |
| 745 |
traits |= kCTFontItalicTrait; |
| 746 |
} |
| 747 |
if (p_stretch < 100) { |
| 748 |
traits |= kCTFontCondensedTrait; |
| 749 |
} else if (p_stretch > 100) { |
| 750 |
traits |= kCTFontExpandedTrait; |
| 751 |
} |
| 752 |
|
| 753 |
CFNumberRef sym_traits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &traits); |
| 754 |
CFMutableDictionaryRef traits_dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr); |
| 755 |
CFDictionaryAddValue(traits_dict, kCTFontSymbolicTrait, sym_traits); |
| 756 |
|
| 757 |
CGFloat weight = _weight_to_ct(p_weight); |
| 758 |
CFNumberRef font_weight = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &weight); |
| 759 |
CFDictionaryAddValue(traits_dict, kCTFontWeightTrait, font_weight); |
| 760 |
|
| 761 |
CGFloat stretch = _stretch_to_ct(p_stretch); |
| 762 |
CFNumberRef font_stretch = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &stretch); |
| 763 |
CFDictionaryAddValue(traits_dict, kCTFontWidthTrait, font_stretch); |
| 764 |
|
| 765 |
CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr); |
| 766 |
CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, name); |
| 767 |
CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits_dict); |
| 768 |
|
| 769 |
CTFontDescriptorRef font = CTFontDescriptorCreateWithAttributes(attributes); |
| 770 |
if (font) { |
| 771 |
CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute(font, kCTFontURLAttribute); |
| 772 |
if (url) { |
| 773 |
NSString *font_path = [NSString stringWithString:[(__bridge NSURL *)url path]]; |
| 774 |
ret = String::utf8([font_path UTF8String]); |
| 775 |
CFRelease(url); |
| 776 |
} |
| 777 |
CFRelease(font); |
| 778 |
} |
| 779 |
|
| 780 |
CFRelease(attributes); |
| 781 |
CFRelease(traits_dict); |
| 782 |
CFRelease(sym_traits); |
| 783 |
CFRelease(font_stretch); |
| 784 |
CFRelease(font_weight); |
| 785 |
CFRelease(name); |
| 786 |
|
| 787 |
return ret; |
| 788 |
} |
| 789 |
|
| 790 |
String OS_MacOS::get_executable_path() const { |
| 791 |
char pathbuf[PROC_PIDPATHINFO_MAXSIZE]; |
| 792 |
int pid = getpid(); |
| 793 |
pid_t ret = proc_pidpath(pid, pathbuf, sizeof(pathbuf)); |
| 794 |
if (ret <= 0) { |
| 795 |
return OS::get_executable_path(); |
| 796 |
} else { |
| 797 |
return String::utf8(pathbuf); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
Error OS_MacOS::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id, bool p_open_console) { |
| 802 |
// Use NSWorkspace if path is an .app bundle. |
| 803 |
NSURL *url = [NSURL fileURLWithPath:@(p_path.utf8().get_data())]; |
| 804 |
NSBundle *bundle = [NSBundle bundleWithURL:url]; |
| 805 |
if (bundle) { |
| 806 |
NSMutableArray *arguments = [[NSMutableArray alloc] init]; |
| 807 |
for (const String &arg : p_arguments) { |
| 808 |
[arguments addObject:[NSString stringWithUTF8String:arg.utf8().get_data()]]; |
| 809 |
} |
| 810 |
#if defined(__x86_64__) |
| 811 |
if (@available(macOS 10.15, *)) { |
| 812 |
#endif |
| 813 |
NSWorkspaceOpenConfiguration *configuration = [[NSWorkspaceOpenConfiguration alloc] init]; |
| 814 |
[configuration setArguments:arguments]; |
| 815 |
[configuration setCreatesNewApplicationInstance:YES]; |
| 816 |
[configuration setEnvironment:[[NSProcessInfo processInfo] environment]]; |
| 817 |
__block dispatch_semaphore_t lock = dispatch_semaphore_create(0); |
| 818 |
__block Error err = ERR_TIMEOUT; |
| 819 |
__block pid_t pid = 0; |
| 820 |
|
| 821 |
[[NSWorkspace sharedWorkspace] openApplicationAtURL:url |
| 822 |
configuration:configuration |
| 823 |
completionHandler:^(NSRunningApplication *app, NSError *error) { |
| 824 |
if (error) { |
| 825 |
err = ERR_CANT_FORK; |
| 826 |
NSLog(@"Failed to execute: %@", error.localizedDescription); |
| 827 |
} else { |
| 828 |
pid = [app processIdentifier]; |
| 829 |
err = OK; |
| 830 |
} |
| 831 |
dispatch_semaphore_signal(lock); |
| 832 |
}]; |
| 833 |
dispatch_semaphore_wait(lock, dispatch_time(DISPATCH_TIME_NOW, 20000000000)); // 20 sec timeout, wait for app to launch. |
| 834 |
|
| 835 |
if (err == OK) { |
| 836 |
if (r_child_id) { |
| 837 |
*r_child_id = (ProcessID)pid; |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
return err; |
| 842 |
#if defined(__x86_64__) |
| 843 |
} else { |
| 844 |
Error err = ERR_TIMEOUT; |
| 845 |
NSError *error = nullptr; |
| 846 |
NSDictionary *config = @{ |
| 847 |
NSWorkspaceLaunchConfigurationArguments : arguments, |
| 848 |
NSWorkspaceLaunchConfigurationEnvironment : [[NSProcessInfo processInfo] environment] |
| 849 |
}; |
| 850 |
NSRunningApplication *app = [[NSWorkspace sharedWorkspace] launchApplicationAtURL:url options:NSWorkspaceLaunchNewInstance configuration:config error:&error]; |
| 851 |
if (error) { |
| 852 |
err = ERR_CANT_FORK; |
| 853 |
NSLog(@"Failed to execute: %@", error.localizedDescription); |
| 854 |
} else { |
| 855 |
if (r_child_id) { |
| 856 |
*r_child_id = (ProcessID)[app processIdentifier]; |
| 857 |
} |
| 858 |
err = OK; |
| 859 |
} |
| 860 |
return err; |
| 861 |
} |
| 862 |
#endif |
| 863 |
} else { |
| 864 |
return OS_Unix::create_process(p_path, p_arguments, r_child_id, p_open_console); |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
Error OS_MacOS::create_instance(const List<String> &p_arguments, ProcessID *r_child_id) { |
| 869 |
// Do not run headless instance as app bundle, since it will never send `applicationDidFinishLaunching` and register as failed start after timeout. |
| 870 |
for (size_t i = 0; i < std::size(OS_MacOS::headless_args); i++) { |
| 871 |
if (p_arguments.find(String(OS_MacOS::headless_args[i]))) { |
| 872 |
return OS_Unix::create_process(get_executable_path(), p_arguments, r_child_id, false); |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
// If executable is bundled, always execute editor instances as an app bundle to ensure app window is registered and activated correctly. |
| 877 |
NSString *nsappname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]; |
| 878 |
if (nsappname != nil) { |
| 879 |
String path = String::utf8([[[NSBundle mainBundle] bundlePath] UTF8String]); |
| 880 |
#ifdef TOOLS_ENABLED |
| 881 |
if (Engine::get_singleton() && !Engine::get_singleton()->is_project_manager_hint() && !Engine::get_singleton()->is_editor_hint()) { |
| 882 |
// Project started from the editor, inject "path" argument to set instance working directory. |
| 883 |
char cwd[PATH_MAX]; |
| 884 |
if (::getcwd(cwd, sizeof(cwd)) != nullptr) { |
| 885 |
List<String> arguments = p_arguments; |
| 886 |
arguments.push_back("--path"); |
| 887 |
arguments.push_back(String::utf8(cwd)); |
| 888 |
return create_process(path, arguments, r_child_id, false); |
| 889 |
} |
| 890 |
} |
| 891 |
#endif |
| 892 |
return create_process(path, p_arguments, r_child_id, false); |
| 893 |
} else { |
| 894 |
return create_process(get_executable_path(), p_arguments, r_child_id, false); |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
Error OS_MacOS::open_with_program(const String &p_program_path, const List<String> &p_paths) { |
| 899 |
NSURL *app_url = [NSURL fileURLWithPath:@(p_program_path.utf8().get_data())]; |
| 900 |
if (!app_url) { |
| 901 |
return ERR_INVALID_PARAMETER; |
| 902 |
} |
| 903 |
|
| 904 |
NSBundle *bundle = [NSBundle bundleWithURL:app_url]; |
| 905 |
if (!bundle) { |
| 906 |
return OS_Unix::create_process(p_program_path, p_paths); |
| 907 |
} |
| 908 |
|
| 909 |
NSMutableArray *urls_to_open = [[NSMutableArray alloc] init]; |
| 910 |
for (const String &path : p_paths) { |
| 911 |
NSURL *file_url = [NSURL fileURLWithPath:@(path.utf8().get_data())]; |
| 912 |
if (file_url) { |
| 913 |
[urls_to_open addObject:file_url]; |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
if ([urls_to_open count] == 0) { |
| 918 |
return ERR_INVALID_PARAMETER; |
| 919 |
} |
| 920 |
|
| 921 |
#if defined(__x86_64__) |
| 922 |
if (@available(macOS 10.15, *)) { |
| 923 |
#endif |
| 924 |
NSWorkspaceOpenConfiguration *configuration = [[NSWorkspaceOpenConfiguration alloc] init]; |
| 925 |
[configuration setCreatesNewApplicationInstance:NO]; |
| 926 |
__block dispatch_semaphore_t lock = dispatch_semaphore_create(0); |
| 927 |
__block Error err = ERR_TIMEOUT; |
| 928 |
|
| 929 |
[[NSWorkspace sharedWorkspace] openURLs:urls_to_open |
| 930 |
withApplicationAtURL:app_url |
| 931 |
configuration:configuration |
| 932 |
completionHandler:^(NSRunningApplication *app, NSError *error) { |
| 933 |
if (error) { |
| 934 |
err = ERR_CANT_FORK; |
| 935 |
NSLog(@"Failed to open paths: %@", error.localizedDescription); |
| 936 |
} else { |
| 937 |
err = OK; |
| 938 |
} |
| 939 |
dispatch_semaphore_signal(lock); |
| 940 |
}]; |
| 941 |
dispatch_semaphore_wait(lock, dispatch_time(DISPATCH_TIME_NOW, 20000000000)); // 20 sec timeout, wait for app to launch. |
| 942 |
|
| 943 |
return err; |
| 944 |
#if defined(__x86_64__) |
| 945 |
} else { |
| 946 |
NSError *error = nullptr; |
| 947 |
[[NSWorkspace sharedWorkspace] openURLs:urls_to_open withApplicationAtURL:app_url options:NSWorkspaceLaunchDefault configuration:@{} error:&error]; |
| 948 |
if (error) { |
| 949 |
return ERR_CANT_FORK; |
| 950 |
} |
| 951 |
return OK; |
| 952 |
} |
| 953 |
#endif |
| 954 |
} |
| 955 |
|
| 956 |
bool OS_MacOS::is_process_running(const ProcessID &p_pid) const { |
| 957 |
NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:(pid_t)p_pid]; |
| 958 |
if (!app) { |
| 959 |
return OS_Unix::is_process_running(p_pid); |
| 960 |
} |
| 961 |
|
| 962 |
return ![app isTerminated]; |
| 963 |
} |
| 964 |
|
| 965 |
String OS_MacOS::get_unique_id() const { |
| 966 |
static String serial_number; |
| 967 |
|
| 968 |
if (serial_number.is_empty()) { |
| 969 |
io_service_t platform_expert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice")); |
| 970 |
CFStringRef serial_number_cf_string = nullptr; |
| 971 |
if (platform_expert) { |
| 972 |
serial_number_cf_string = (CFStringRef)IORegistryEntryCreateCFProperty(platform_expert, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, 0); |
| 973 |
IOObjectRelease(platform_expert); |
| 974 |
} |
| 975 |
|
| 976 |
NSString *serial_number_ns_string = nil; |
| 977 |
if (serial_number_cf_string) { |
| 978 |
serial_number_ns_string = [NSString stringWithString:(__bridge NSString *)serial_number_cf_string]; |
| 979 |
CFRelease(serial_number_cf_string); |
| 980 |
} |
| 981 |
|
| 982 |
if (serial_number_ns_string) { |
| 983 |
serial_number.append_utf8([serial_number_ns_string UTF8String]); |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
return serial_number; |
| 988 |
} |
| 989 |
|
| 990 |
bool OS_MacOS::_check_internal_feature_support(const String &p_feature) { |
| 991 |
if (p_feature == "system_fonts") { |
| 992 |
return true; |
| 993 |
} |
| 994 |
if (p_feature == "pc") { |
| 995 |
return true; |
| 996 |
} |
| 997 |
|
| 998 |
return false; |
| 999 |
} |
| 1000 |
|
| 1001 |
void OS_MacOS::disable_crash_handler() { |
| 1002 |
crash_handler.disable(); |
| 1003 |
} |
| 1004 |
|
| 1005 |
bool OS_MacOS::is_disable_crash_handler() const { |
| 1006 |
return crash_handler.is_disabled(); |
| 1007 |
} |
| 1008 |
|
| 1009 |
Error OS_MacOS::move_to_trash(const String &p_path) { |
| 1010 |
NSFileManager *fm = [NSFileManager defaultManager]; |
| 1011 |
NSURL *url = [NSURL fileURLWithPath:@(p_path.utf8().get_data())]; |
| 1012 |
NSError *err; |
| 1013 |
|
| 1014 |
if (![fm trashItemAtURL:url resultingItemURL:nil error:&err]) { |
| 1015 |
ERR_PRINT("trashItemAtURL error: " + String::utf8(err.localizedDescription.UTF8String)); |
| 1016 |
return FAILED; |
| 1017 |
} |
| 1018 |
|
| 1019 |
return OK; |
| 1020 |
} |
| 1021 |
|
| 1022 |
String OS_MacOS::get_system_ca_certificates() { |
| 1023 |
CFArrayRef result; |
| 1024 |
SecCertificateRef item; |
| 1025 |
CFDataRef der; |
| 1026 |
|
| 1027 |
OSStatus ret = SecTrustCopyAnchorCertificates(&result); |
| 1028 |
ERR_FAIL_COND_V(ret != noErr, ""); |
| 1029 |
|
| 1030 |
CFIndex l = CFArrayGetCount(result); |
| 1031 |
String certs; |
| 1032 |
PackedByteArray pba; |
| 1033 |
for (CFIndex i = 0; i < l; i++) { |
| 1034 |
item = (SecCertificateRef)CFArrayGetValueAtIndex(result, i); |
| 1035 |
der = SecCertificateCopyData(item); |
| 1036 |
int derlen = CFDataGetLength(der); |
| 1037 |
if (pba.size() < derlen * 3) { |
| 1038 |
pba.resize(derlen * 3); |
| 1039 |
} |
| 1040 |
size_t b64len = 0; |
| 1041 |
Error err = CryptoCore::b64_encode(pba.ptrw(), pba.size(), &b64len, (unsigned char *)CFDataGetBytePtr(der), derlen); |
| 1042 |
CFRelease(der); |
| 1043 |
ERR_CONTINUE(err != OK); |
| 1044 |
// Certificate is bas64 encoded, aka ascii. |
| 1045 |
certs += "-----BEGIN CERTIFICATE-----\n" + String::ascii(Span((char *)pba.ptr(), b64len)) + "\n-----END CERTIFICATE-----\n"; |
| 1046 |
} |
| 1047 |
CFRelease(result); |
| 1048 |
return certs; |
| 1049 |
} |
| 1050 |
|
| 1051 |
OS::PreferredTextureFormat OS_MacOS::get_preferred_texture_format() const { |
| 1052 |
// macOS supports both formats on ARM. Prefer S3TC/BPTC |
| 1053 |
// for better compatibility with x86 platforms. |
| 1054 |
return PREFERRED_TEXTURE_FORMAT_S3TC_BPTC; |
| 1055 |
} |
| 1056 |
|
| 1057 |
OS_MacOS::OS_MacOS(const char *p_execpath, int p_argc, char **p_argv) { |
| 1058 |
execpath = p_execpath; |
| 1059 |
argc = p_argc; |
| 1060 |
argv = p_argv; |
| 1061 |
|
| 1062 |
if (is_sandboxed()) { |
| 1063 |
// Load security-scoped bookmarks, request access, remove stale or invalid bookmarks. |
| 1064 |
NSArray *bookmarks = [[NSUserDefaults standardUserDefaults] arrayForKey:@"sec_bookmarks"]; |
| 1065 |
NSMutableArray *new_bookmarks = [[NSMutableArray alloc] init]; |
| 1066 |
for (id bookmark in bookmarks) { |
| 1067 |
NSError *error = nil; |
| 1068 |
BOOL isStale = NO; |
| 1069 |
NSURL *url = [NSURL URLByResolvingBookmarkData:bookmark options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:&isStale error:&error]; |
| 1070 |
if (!error && !isStale) { |
| 1071 |
if ([url startAccessingSecurityScopedResource]) { |
| 1072 |
[new_bookmarks addObject:bookmark]; |
| 1073 |
} |
| 1074 |
} |
| 1075 |
} |
| 1076 |
[[NSUserDefaults standardUserDefaults] setObject:new_bookmarks forKey:@"sec_bookmarks"]; |
| 1077 |
} |
| 1078 |
Vector<Logger *> loggers; |
| 1079 |
loggers.push_back(memnew(OsLogLogger(NSBundle.mainBundle.bundleIdentifier.UTF8String))); |
| 1080 |
loggers.push_back(memnew(UnixTerminalLogger)); |
| 1081 |
_set_logger(memnew(CompositeLogger(loggers))); |
| 1082 |
|
| 1083 |
#ifdef COREAUDIO_ENABLED |
| 1084 |
AudioDriverManager::add_driver(&audio_driver); |
| 1085 |
#endif |
| 1086 |
|
| 1087 |
DisplayServerMacOS::register_macos_driver(); |
| 1088 |
} |
| 1089 |
|
| 1090 |
// MARK: - OS_MacOS_NSApp |
| 1091 |
|
| 1092 |
void OS_MacOS_NSApp::run() { |
| 1093 |
[NSApp run]; // Note: this call will never return. Use `OS_MacOS_NSApp::cleanup()` for cleanup. |
| 1094 |
} |
| 1095 |
|
| 1096 |
static bool sig_received = false; |
| 1097 |
|
| 1098 |
static void handle_interrupt(int sig) { |
| 1099 |
if (sig == SIGINT) { |
| 1100 |
sig_received = true; |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
void OS_MacOS_NSApp::start_main() { |
| 1105 |
Error err; |
| 1106 |
@autoreleasepool { |
| 1107 |
err = Main::setup(execpath, argc, argv); |
| 1108 |
} |
| 1109 |
|
| 1110 |
if (err == OK) { |
| 1111 |
main_started = true; |
| 1112 |
|
| 1113 |
int ret; |
| 1114 |
@autoreleasepool { |
| 1115 |
ret = Main::start(); |
| 1116 |
} |
| 1117 |
if (ret == EXIT_SUCCESS) { |
| 1118 |
if (main_loop) { |
| 1119 |
@autoreleasepool { |
| 1120 |
main_loop->initialize(); |
| 1121 |
} |
| 1122 |
DisplayServer *ds = DisplayServer::get_singleton(); |
| 1123 |
DisplayServerMacOS *ds_mac = Object::cast_to<DisplayServerMacOS>(ds); |
| 1124 |
|
| 1125 |
pre_wait_observer = CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, kCFRunLoopBeforeWaiting, true, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) { |
| 1126 |
@autoreleasepool { |
| 1127 |
@try { |
| 1128 |
GodotProfileFrameMark; |
| 1129 |
GodotProfileZone("macOS main loop"); |
| 1130 |
|
| 1131 |
if (ds_mac) { |
| 1132 |
ds_mac->_process_events(false); |
| 1133 |
} else if (ds) { |
| 1134 |
ds->process_events(); |
| 1135 |
} |
| 1136 |
#ifdef SDL_ENABLED |
| 1137 |
if (joypad_sdl) { |
| 1138 |
joypad_sdl->process_events(); |
| 1139 |
} |
| 1140 |
#endif |
| 1141 |
|
| 1142 |
if (Main::iteration() || sig_received) { |
| 1143 |
terminate(); |
| 1144 |
} |
| 1145 |
} @catch (NSException *exception) { |
| 1146 |
ERR_PRINT("NSException: " + String::utf8([exception reason].UTF8String)); |
| 1147 |
} |
| 1148 |
} |
| 1149 |
if (wait_timer == nil) { |
| 1150 |
CFRunLoopWakeUp(CFRunLoopGetCurrent()); // Prevent main loop from sleeping. |
| 1151 |
} |
| 1152 |
}); |
| 1153 |
CFRunLoopAddObserver(CFRunLoopGetCurrent(), pre_wait_observer, kCFRunLoopCommonModes); |
| 1154 |
return; |
| 1155 |
} |
| 1156 |
} else { |
| 1157 |
set_exit_code(EXIT_FAILURE); |
| 1158 |
} |
| 1159 |
} else if (err == ERR_HELP) { // Returned by --help and --version, so success. |
| 1160 |
set_exit_code(EXIT_SUCCESS); |
| 1161 |
} else { |
| 1162 |
set_exit_code(EXIT_FAILURE); |
| 1163 |
} |
| 1164 |
|
| 1165 |
terminate(); |
| 1166 |
} |
| 1167 |
|
| 1168 |
void OS_MacOS_NSApp::terminate() { |
| 1169 |
// Note: This method only sends app termination request. Use `OS_MacOS_NSApp::cleanup()` for cleanup. |
| 1170 |
if (pre_wait_observer) { |
| 1171 |
CFRunLoopRemoveObserver(CFRunLoopGetCurrent(), pre_wait_observer, kCFRunLoopCommonModes); |
| 1172 |
CFRelease(pre_wait_observer); |
| 1173 |
pre_wait_observer = nil; |
| 1174 |
} |
| 1175 |
|
| 1176 |
should_terminate = true; |
| 1177 |
[NSApp terminate:nil]; |
| 1178 |
} |
| 1179 |
|
| 1180 |
void OS_MacOS_NSApp::cleanup() { |
| 1181 |
if (main_loop) { |
| 1182 |
main_loop->finalize(); |
| 1183 |
} |
| 1184 |
if (main_started) { |
| 1185 |
@autoreleasepool { |
| 1186 |
Main::cleanup(); |
| 1187 |
} |
| 1188 |
} |
| 1189 |
godot_cleanup_profiler(); |
| 1190 |
} |
| 1191 |
|
| 1192 |
OS_MacOS_NSApp::OS_MacOS_NSApp(const char *p_execpath, int p_argc, char **p_argv) : |
| 1193 |
OS_MacOS(p_execpath, p_argc, p_argv) { |
| 1194 |
// Implicitly create shared NSApplication instance. |
| 1195 |
[GodotApplication sharedApplication]; |
| 1196 |
|
| 1197 |
// In case we are unbundled, make us a proper UI application. |
| 1198 |
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; |
| 1199 |
|
| 1200 |
// Menu bar setup must go between sharedApplication above and |
| 1201 |
// finishLaunching below, in order to properly emulate the behavior |
| 1202 |
// of NSApplicationMain. |
| 1203 |
|
| 1204 |
NSMenu *main_menu = [[NSMenu alloc] initWithTitle:@""]; |
| 1205 |
[NSApp setMainMenu:main_menu]; |
| 1206 |
|
| 1207 |
delegate = [[GodotApplicationDelegate alloc] initWithOS:this]; |
| 1208 |
ERR_FAIL_NULL(delegate); |
| 1209 |
[NSApp setDelegate:delegate]; |
| 1210 |
[NSApp registerUserInterfaceItemSearchHandler:delegate]; |
| 1211 |
|
| 1212 |
struct sigaction action; |
| 1213 |
memset(&action, 0, sizeof(action)); |
| 1214 |
action.sa_handler = handle_interrupt; |
| 1215 |
sigaction(SIGINT, &action, nullptr); |
| 1216 |
} |
| 1217 |
|
| 1218 |
// MARK: - OS_MacOS_Headless |
| 1219 |
|
| 1220 |
void OS_MacOS_Headless::run() { |
| 1221 |
CFRunLoopGetCurrent(); |
| 1222 |
|
| 1223 |
@autoreleasepool { |
| 1224 |
Error err = Main::setup(execpath, argc, argv); |
| 1225 |
if (err != OK) { |
| 1226 |
if (err == ERR_HELP) { |
| 1227 |
return set_exit_code(EXIT_SUCCESS); |
| 1228 |
} |
| 1229 |
return set_exit_code(EXIT_FAILURE); |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
int ret; |
| 1234 |
@autoreleasepool { |
| 1235 |
ret = Main::start(); |
| 1236 |
} |
| 1237 |
|
| 1238 |
if (ret == EXIT_SUCCESS && main_loop) { |
| 1239 |
@autoreleasepool { |
| 1240 |
main_loop->initialize(); |
| 1241 |
} |
| 1242 |
|
| 1243 |
while (true) { |
| 1244 |
@autoreleasepool { |
| 1245 |
@try { |
| 1246 |
if (Input::get_singleton()) { |
| 1247 |
Input::get_singleton()->flush_buffered_events(); |
| 1248 |
} |
| 1249 |
|
| 1250 |
if (Main::iteration()) { |
| 1251 |
break; |
| 1252 |
} |
| 1253 |
|
| 1254 |
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, 0); |
| 1255 |
} @catch (NSException *exception) { |
| 1256 |
ERR_PRINT("NSException: " + String::utf8([exception reason].UTF8String)); |
| 1257 |
} |
| 1258 |
} |
| 1259 |
} |
| 1260 |
|
| 1261 |
main_loop->finalize(); |
| 1262 |
} |
| 1263 |
|
| 1264 |
Main::cleanup(); |
| 1265 |
} |
| 1266 |
|
| 1267 |
OS_MacOS_Headless::OS_MacOS_Headless(const char *p_execpath, int p_argc, char **p_argv) : |
| 1268 |
OS_MacOS(p_execpath, p_argc, p_argv) { |
| 1269 |
} |
| 1270 |
|
| 1271 |
// MARK: - OS_MacOS_Embedded |
| 1272 |
|
| 1273 |
#ifdef TOOLS_ENABLED |
| 1274 |
|
| 1275 |
void OS_MacOS_Embedded::run() { |
| 1276 |
CFRunLoopGetCurrent(); |
| 1277 |
|
| 1278 |
@autoreleasepool { |
| 1279 |
Error err = Main::setup(execpath, argc, argv); |
| 1280 |
if (err != OK) { |
| 1281 |
if (err == ERR_HELP) { |
| 1282 |
return set_exit_code(EXIT_SUCCESS); |
| 1283 |
} |
| 1284 |
return set_exit_code(EXIT_FAILURE); |
| 1285 |
} |
| 1286 |
} |
| 1287 |
|
| 1288 |
int ret; |
| 1289 |
@autoreleasepool { |
| 1290 |
ret = Main::start(); |
| 1291 |
} |
| 1292 |
|
| 1293 |
DisplayServerEmbedded *ds = Object::cast_to<DisplayServerEmbedded>(DisplayServer::get_singleton()); |
| 1294 |
if (!ds) { |
| 1295 |
ERR_FAIL_MSG("DisplayServerEmbedded is not initialized."); |
| 1296 |
} |
| 1297 |
|
| 1298 |
if (ds && ret == EXIT_SUCCESS && main_loop) { |
| 1299 |
@autoreleasepool { |
| 1300 |
main_loop->initialize(); |
| 1301 |
} |
| 1302 |
|
| 1303 |
while (true) { |
| 1304 |
@autoreleasepool { |
| 1305 |
@try { |
| 1306 |
GodotProfileFrameMark; |
| 1307 |
GodotProfileZone("macOS embedded main loop"); |
| 1308 |
|
| 1309 |
ds->process_events(); |
| 1310 |
|
| 1311 |
#ifdef SDL_ENABLED |
| 1312 |
if (joypad_sdl) { |
| 1313 |
joypad_sdl->process_events(); |
| 1314 |
} |
| 1315 |
#endif |
| 1316 |
if (Main::iteration()) { |
| 1317 |
break; |
| 1318 |
} |
| 1319 |
|
| 1320 |
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, 0); |
| 1321 |
} @catch (NSException *exception) { |
| 1322 |
ERR_PRINT("NSException: " + String::utf8([exception reason].UTF8String)); |
| 1323 |
} |
| 1324 |
} |
| 1325 |
} |
| 1326 |
|
| 1327 |
main_loop->finalize(); |
| 1328 |
} |
| 1329 |
|
| 1330 |
Main::cleanup(); |
| 1331 |
} |
| 1332 |
|
| 1333 |
OS_MacOS_Embedded::OS_MacOS_Embedded(const char *p_execpath, int p_argc, char **p_argv) : |
| 1334 |
OS_MacOS(p_execpath, p_argc, p_argv) { |
| 1335 |
DisplayServerEmbedded::register_embedded_driver(); |
| 1336 |
} |
| 1337 |
|
| 1338 |
#endif |