Debian Codegraph @

platform/macos/godot_main_macos.mm

raw
1 /**************************************************************************/
2 /* godot_main_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 "godot_application.h"
34
35 #include "core/profiling/profiling.h"
36 #include "main/main.h"
37
38 #if defined(SANITIZERS_ENABLED)
39 #include <sys/resource.h>
40 #endif
41
42 int main(int argc, char **argv) {
43 godot_init_profiler();
44
45 #if defined(VULKAN_ENABLED)
46 setenv("MVK_CONFIG_FULL_IMAGE_VIEW_SWIZZLE", "1", 1); // MoltenVK - enable full component swizzling support.
47 setenv("MVK_CONFIG_SWAPCHAIN_MIN_MAG_FILTER_USE_NEAREST", "0", 1); // MoltenVK - use linear surface scaling. TODO: remove when full DPI scaling is implemented.
48 #endif
49
50 #if defined(SANITIZERS_ENABLED)
51 // Note: Set stack size to be at least 30 MB (vs 8 MB default) to avoid overflow, address sanitizer can increase stack usage up to 3 times.
52 struct rlimit stack_lim = { 0x1E00000, 0x1E00000 };
53 setrlimit(RLIMIT_STACK, &stack_lim);
54 #endif
55
56 LocalVector<char *> args;
57 args.resize(argc);
58 uint32_t argsc = 0;
59
60 int wait_for_debugger = 0; // wait 5 second by default
61 bool is_embedded = false;
62 bool is_headless = false;
63
64 for (int i = 0; i < argc; i++) {
65 if (strcmp("-NSDocumentRevisionsDebugMode", argv[i]) == 0) {
66 // remove "-NSDocumentRevisionsDebugMode" and the next argument
67 continue;
68 }
69
70 if (strcmp("--os-debug", argv[i]) == 0) {
71 i++;
72 wait_for_debugger = 5000; // wait 5 seconds by default
73 if (i < argc && strncmp(argv[i], "--", 2) != 0) {
74 wait_for_debugger = atoi(argv[i]);
75 }
76 continue;
77 }
78
79 if (strcmp("--embedded", argv[i]) == 0) {
80 is_embedded = true;
81 }
82 for (size_t j = 0; j < std::size(OS_MacOS::headless_args); j++) {
83 if (strcmp(OS_MacOS::headless_args[j], argv[i]) == 0) {
84 is_headless = true;
85 break;
86 }
87 }
88
89 if (i < argc - 1 && strcmp("--display-driver", argv[i]) == 0 && strcmp("headless", argv[i + 1]) == 0) {
90 is_headless = true;
91 }
92
93 args.ptr()[argsc] = argv[i];
94 argsc++;
95 }
96
97 uint32_t remaining_args = argsc - 1;
98
99 OS_MacOS *os = nullptr;
100 if (is_embedded) {
101 #ifdef TOOLS_ENABLED
102 os = memnew(OS_MacOS_Embedded(args[0], remaining_args, remaining_args > 0 ? &args[1] : nullptr));
103 #else
104 WARN_PRINT("Embedded mode is not supported in release builds.");
105 return EXIT_FAILURE;
106 #endif
107 } else if (is_headless) {
108 os = memnew(OS_MacOS_Headless(args[0], remaining_args, remaining_args > 0 ? &args[1] : nullptr));
109 } else {
110 os = memnew(OS_MacOS_NSApp(args[0], remaining_args, remaining_args > 0 ? &args[1] : nullptr));
111 }
112
113 #ifdef TOOLS_ENABLED
114 if (wait_for_debugger > 0) {
115 os->wait_for_debugger(wait_for_debugger);
116 print_verbose("Continuing execution.");
117 }
118 #else
119 if (wait_for_debugger > 0) {
120 WARN_PRINT("--os-debug is not supported in release builds.");
121 }
122 #endif
123
124 if (is_embedded) {
125 // No dock icon for the embedded process, as it is hosted in the Godot editor.
126 ProcessSerialNumber psn = { 0, kCurrentProcess };
127 (void)TransformProcessType(&psn, kProcessTransformToBackgroundApplication);
128 }
129
130 // We must override main when testing is enabled.
131 TEST_MAIN_OVERRIDE
132
133 os->run();
134
135 // Note: `os->run()` will never return if `OS_MacOS_NSApp` is used. Use `OS_MacOS_NSApp::cleanup()` for cleanup.
136
137 int exit_code = os->get_exit_code();
138
139 memdelete(os);
140
141 godot_cleanup_profiler();
142 return exit_code;
143 }