Debian Codegraph @

platform/web/web_main.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* web_main.cpp */
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 #include "display_server_web.h"
32 #include "godot_js.h"
33 #include "os_web.h"
34
35 #include "core/config/engine.h"
36 #include "core/io/file_access.h"
37 #include "core/io/resource_loader.h"
38 #include "core/profiling/profiling.h"
39 #include "main/main.h"
40 #include "scene/main/scene_tree.h"
41 #include "scene/main/window.h" // SceneTree only forward declares it.
42
43 #ifdef TOOLS_ENABLED
44 #include "editor/web_tools_editor_plugin.h"
45 #endif
46
47 #include <emscripten/emscripten.h>
48 #include <cstdlib>
49
50 static OS_Web *os = nullptr;
51 #ifndef PROXY_TO_PTHREAD_ENABLED
52 static uint64_t target_ticks = 0;
53 #endif
54
55 static bool main_started = false;
56 static bool shutdown_complete = false;
57
58 void exit_callback() {
59 if (!shutdown_complete) {
60 return; // Still waiting.
61 }
62 if (main_started) {
63 Main::cleanup();
64 main_started = false;
65 }
66 int exit_code = OS_Web::get_singleton()->get_exit_code();
67 memdelete(os);
68 os = nullptr;
69 godot_cleanup_profiler();
70 emscripten_force_exit(exit_code); // Exit runtime.
71 }
72
73 void cleanup_after_sync() {
74 shutdown_complete = true;
75 }
76
77 void main_loop_callback() {
78 #ifndef PROXY_TO_PTHREAD_ENABLED
79 uint64_t current_ticks = os->get_ticks_usec();
80 #endif
81
82 bool force_draw = DisplayServerWeb::get_singleton()->check_size_force_redraw();
83 if (force_draw) {
84 Main::force_redraw();
85 #ifndef PROXY_TO_PTHREAD_ENABLED
86 } else if (current_ticks < target_ticks) {
87 return; // Skip frame.
88 #endif
89 }
90
91 #ifndef PROXY_TO_PTHREAD_ENABLED
92 int max_fps = Engine::get_singleton()->get_max_fps();
93 if (max_fps > 0) {
94 if (current_ticks - target_ticks > 1000000) {
95 // When the window loses focus, we stop getting updates and accumulate delay.
96 // For this reason, if the difference is too big, we reset target ticks to the current ticks.
97 target_ticks = current_ticks;
98 }
99 target_ticks += (uint64_t)(1000000 / max_fps);
100 }
101 #endif
102
103 if (os->main_loop_iterate()) {
104 emscripten_cancel_main_loop(); // Cancel current loop and set the cleanup one.
105 emscripten_set_main_loop(exit_callback, -1, false);
106 godot_js_os_finish_async(cleanup_after_sync);
107 }
108 }
109
110 void print_web_header() {
111 // Emscripten.
112 char *emscripten_version_char = godot_js_emscripten_get_version();
113 String emscripten_version = vformat("Emscripten %s", emscripten_version_char);
114 // `free()` is used here because it's not memory that was allocated by Godot.
115 free(emscripten_version_char);
116
117 // Build features.
118 String thread_support = OS::get_singleton()->has_feature("threads")
119 ? "multi-threaded"
120 : "single-threaded";
121 String extensions_support = OS::get_singleton()->has_feature("web_extensions")
122 ? "GDExtension support"
123 : "no GDExtension support";
124
125 Vector<String> build_configuration = { emscripten_version, thread_support, extensions_support };
126 print_line(vformat("Build configuration: %s.", String(", ").join(build_configuration)));
127 }
128
129 /// When calling main, it is assumed FS is setup and synced.
130 extern EMSCRIPTEN_KEEPALIVE int godot_web_main(int argc, char *argv[]) {
131 godot_init_profiler();
132
133 os = new OS_Web();
134
135 #ifdef TOOLS_ENABLED
136 WebToolsEditorPlugin::initialize();
137 #endif
138
139 // We must override main when testing is enabled
140 TEST_MAIN_OVERRIDE
141
142 Error err = Main::setup(argv[0], argc - 1, &argv[1]);
143
144 // Proper shutdown in case of setup failure.
145 if (err != OK) {
146 // Will only exit after sync.
147 emscripten_set_main_loop(exit_callback, -1, false);
148 godot_js_os_finish_async(cleanup_after_sync);
149 if (err == ERR_HELP) { // Returned by --help and --version, so success.
150 return EXIT_SUCCESS;
151 }
152 return EXIT_FAILURE;
153 }
154
155 print_web_header();
156
157 main_started = true;
158
159 // Ease up compatibility.
160 ResourceLoader::set_abort_on_missing_resources(false);
161
162 int ret = Main::start();
163 os->set_exit_code(ret);
164 os->get_main_loop()->initialize();
165 #ifdef TOOLS_ENABLED
166 if (Engine::get_singleton()->is_project_manager_hint() && FileAccess::exists("/tmp/preload.zip")) {
167 PackedStringArray ps;
168 ps.push_back("/tmp/preload.zip");
169 SceneTree::get_singleton()->get_root()->emit_signal(SNAME("files_dropped"), ps);
170 }
171 #endif
172 emscripten_set_main_loop(main_loop_callback, -1, false);
173 // Immediately run the first iteration.
174 // We are inside an animation frame, we want to immediately draw on the newly setup canvas.
175 main_loop_callback();
176
177 return os->get_exit_code();
178 }