Skip to content

Commit 4b8e025

Browse files
committed
Merge pull request #108696 from bruvzg/maco_full_headless
[macOS] Do not use NSApplication main loop for headless mode.
2 parents 24ad16c + 8b045ca commit 4b8e025

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

platform/macos/godot_main_macos.mm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ int main(int argc, char **argv) {
5656

5757
int wait_for_debugger = 0; // wait 5 second by default
5858
bool is_embedded = false;
59+
bool is_headless = false;
5960

6061
for (int i = 0; i < argc; i++) {
6162
if (strcmp("-NSDocumentRevisionsDebugMode", argv[i]) == 0) {
@@ -75,6 +76,12 @@ int main(int argc, char **argv) {
7576
if (strcmp("--embedded", argv[i]) == 0) {
7677
is_embedded = true;
7778
}
79+
if (strcmp("--headless", argv[i]) == 0 || strcmp("--doctool", argv[i]) == 0) {
80+
is_headless = true;
81+
}
82+
if (i < argc - 1 && strcmp("--display-driver", argv[i]) == 0 && strcmp("headless", argv[i + 1]) == 0) {
83+
is_headless = true;
84+
}
7885

7986
args.ptr()[argsc] = argv[i];
8087
argsc++;
@@ -90,6 +97,8 @@ int main(int argc, char **argv) {
9097
WARN_PRINT("Embedded mode is not supported in release builds.");
9198
return EXIT_FAILURE;
9299
#endif
100+
} else if (is_headless) {
101+
os = memnew(OS_MacOS_Headless(args[0], remaining_args, remaining_args > 0 ? &args[1] : nullptr));
93102
} else {
94103
os = memnew(OS_MacOS_NSApp(args[0], remaining_args, remaining_args > 0 ? &args[1] : nullptr));
95104
}

platform/macos/os_macos.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ class OS_MacOS_NSApp : public OS_MacOS {
171171
OS_MacOS_NSApp(const char *p_execpath, int p_argc, char **p_argv);
172172
};
173173

174+
class OS_MacOS_Headless : public OS_MacOS {
175+
public:
176+
virtual void run() override;
177+
178+
OS_MacOS_Headless(const char *p_execpath, int p_argc, char **p_argv);
179+
};
180+
174181
#ifdef DEBUG_ENABLED
175182

176183
class OS_MacOS_Embedded : public OS_MacOS {

platform/macos/os_macos.mm

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,59 @@ static void handle_interrupt(int sig) {
11711171
sigaction(SIGINT, &action, nullptr);
11721172
}
11731173

1174+
// MARK: - OS_MacOS_Headless
1175+
1176+
void OS_MacOS_Headless::run() {
1177+
CFRunLoopGetCurrent();
1178+
1179+
@autoreleasepool {
1180+
Error err = Main::setup(execpath, argc, argv);
1181+
if (err != OK) {
1182+
if (err == ERR_HELP) {
1183+
return set_exit_code(EXIT_SUCCESS);
1184+
}
1185+
return set_exit_code(EXIT_FAILURE);
1186+
}
1187+
}
1188+
1189+
int ret;
1190+
@autoreleasepool {
1191+
ret = Main::start();
1192+
}
1193+
1194+
if (ret == EXIT_SUCCESS && main_loop) {
1195+
@autoreleasepool {
1196+
main_loop->initialize();
1197+
}
1198+
1199+
while (true) {
1200+
@autoreleasepool {
1201+
@try {
1202+
if (Input::get_singleton()) {
1203+
Input::get_singleton()->flush_buffered_events();
1204+
}
1205+
1206+
if (Main::iteration()) {
1207+
break;
1208+
}
1209+
1210+
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, 0);
1211+
} @catch (NSException *exception) {
1212+
ERR_PRINT("NSException: " + String::utf8([exception reason].UTF8String));
1213+
}
1214+
}
1215+
}
1216+
1217+
main_loop->finalize();
1218+
}
1219+
1220+
Main::cleanup();
1221+
}
1222+
1223+
OS_MacOS_Headless::OS_MacOS_Headless(const char *p_execpath, int p_argc, char **p_argv) :
1224+
OS_MacOS(p_execpath, p_argc, p_argv) {
1225+
}
1226+
11741227
// MARK: - OS_MacOS_Embedded
11751228

11761229
#ifdef DEBUG_ENABLED

0 commit comments

Comments
 (0)