Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/web/os_web.cpp
20844 views
1
/**************************************************************************/
2
/* os_web.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 "os_web.h"
32
33
#include "api/javascript_bridge_singleton.h"
34
#include "display_server_web.h"
35
#include "godot_js.h"
36
#include "ip_web.h"
37
#include "net_socket_web.h"
38
39
#include "core/config/project_settings.h"
40
#include "core/debugger/engine_debugger.h"
41
#include "core/io/file_access.h"
42
#include "core/os/main_loop.h"
43
#include "core/profiling/profiling.h"
44
#include "drivers/unix/dir_access_unix.h"
45
#include "drivers/unix/file_access_unix.h"
46
#include "main/main.h"
47
48
#include "modules/modules_enabled.gen.h" // For websocket.
49
50
#include <dlfcn.h>
51
#include <emscripten.h>
52
#include <cstdlib>
53
54
void OS_Web::alert(const String &p_alert, const String &p_title) {
55
godot_js_display_alert(p_alert.utf8().get_data());
56
}
57
58
// Lifecycle
59
void OS_Web::initialize() {
60
OS_Unix::initialize_core();
61
IPWeb::make_default();
62
NetSocketWeb::make_default();
63
DisplayServerWeb::register_web_driver();
64
}
65
66
void OS_Web::resume_audio() {
67
AudioDriverWeb::resume();
68
}
69
70
void OS_Web::set_main_loop(MainLoop *p_main_loop) {
71
main_loop = p_main_loop;
72
}
73
74
MainLoop *OS_Web::get_main_loop() const {
75
return main_loop;
76
}
77
78
void OS_Web::fs_sync_callback() {
79
get_singleton()->idb_is_syncing = false;
80
}
81
82
bool OS_Web::main_loop_iterate() {
83
GodotProfileFrameMark;
84
GodotProfileZone("OS_Web::main_loop_iterate");
85
if (is_userfs_persistent() && idb_needs_sync && !idb_is_syncing) {
86
idb_is_syncing = true;
87
idb_needs_sync = false;
88
godot_js_os_fs_sync(&fs_sync_callback);
89
}
90
91
DisplayServer::get_singleton()->process_events();
92
93
return Main::iteration();
94
}
95
96
void OS_Web::delete_main_loop() {
97
if (main_loop) {
98
memdelete(main_loop);
99
}
100
main_loop = nullptr;
101
}
102
103
void OS_Web::finalize() {
104
delete_main_loop();
105
for (AudioDriverWeb *driver : audio_drivers) {
106
memdelete(driver);
107
}
108
audio_drivers.clear();
109
}
110
111
// Miscellaneous
112
113
Error OS_Web::execute(const String &p_path, const List<String> &p_arguments, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex, bool p_open_console) {
114
return create_process(p_path, p_arguments);
115
}
116
117
Dictionary OS_Web::execute_with_pipe(const String &p_path, const List<String> &p_arguments, bool p_blocking) {
118
ERR_FAIL_V_MSG(Dictionary(), "OS::execute_with_pipe is not available on the Web platform.");
119
}
120
121
Error OS_Web::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id, bool p_open_console) {
122
Array args;
123
for (const String &E : p_arguments) {
124
args.push_back(E);
125
}
126
String json_args = Variant(args).to_json_string();
127
int failed = godot_js_os_execute(json_args.utf8().get_data());
128
ERR_FAIL_COND_V_MSG(failed, ERR_UNAVAILABLE, "OS::execute() or create_process() must be implemented in Web via 'engine.setOnExecute' if required.");
129
return OK;
130
}
131
132
Error OS_Web::kill(const ProcessID &p_pid) {
133
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "OS::kill() is not available on the Web platform.");
134
}
135
136
int OS_Web::get_process_id() const {
137
return 0;
138
}
139
140
bool OS_Web::is_process_running(const ProcessID &p_pid) const {
141
return false;
142
}
143
144
int OS_Web::get_process_exit_code(const ProcessID &p_pid) const {
145
return -1;
146
}
147
148
int OS_Web::get_processor_count() const {
149
return godot_js_os_hw_concurrency_get();
150
}
151
152
String OS_Web::get_unique_id() const {
153
ERR_FAIL_V_MSG("", "OS::get_unique_id() is not available on the Web platform.");
154
}
155
156
int OS_Web::get_default_thread_pool_size() const {
157
#ifdef THREADS_ENABLED
158
return godot_js_os_thread_pool_size_get();
159
#else // No threads.
160
return 1;
161
#endif
162
}
163
164
bool OS_Web::_check_internal_feature_support(const String &p_feature) {
165
if (p_feature == "web") {
166
return true;
167
}
168
169
if (p_feature == "web_extensions") {
170
#ifdef WEB_DLINK_ENABLED
171
return true;
172
#else
173
return false;
174
#endif
175
}
176
if (p_feature == "web_noextensions") {
177
#ifdef WEB_DLINK_ENABLED
178
return false;
179
#else
180
return true;
181
#endif
182
}
183
184
if (godot_js_os_has_feature(p_feature.utf8().get_data())) {
185
return true;
186
}
187
return false;
188
}
189
190
String OS_Web::get_executable_path() const {
191
return OS::get_executable_path();
192
}
193
194
Error OS_Web::shell_open(const String &p_uri) {
195
// Open URI in a new tab, browser will deal with it by protocol.
196
godot_js_os_shell_open(p_uri.utf8().get_data());
197
return OK;
198
}
199
200
String OS_Web::get_name() const {
201
return "Web";
202
}
203
204
void OS_Web::add_frame_delay(bool p_can_draw, bool p_wake_for_events) {
205
#ifndef PROXY_TO_PTHREAD_ENABLED
206
OS::add_frame_delay(p_can_draw, p_wake_for_events);
207
#endif
208
}
209
210
void OS_Web::vibrate_handheld(int p_duration_ms, float p_amplitude) {
211
godot_js_input_vibrate_handheld(p_duration_ms);
212
}
213
214
String OS_Web::get_user_data_dir(const String &p_user_dir) const {
215
String userfs = "/userfs";
216
return userfs.path_join(p_user_dir).replace_char('\\', '/');
217
}
218
219
String OS_Web::get_cache_path() const {
220
return "/home/web_user/.cache";
221
}
222
223
String OS_Web::get_config_path() const {
224
return "/home/web_user/.config";
225
}
226
227
String OS_Web::get_data_path() const {
228
return "/home/web_user/.local/share";
229
}
230
231
void OS_Web::file_access_close_callback(const String &p_file, int p_flags) {
232
OS_Web *os = OS_Web::get_singleton();
233
if (!(os->is_userfs_persistent() && (p_flags & FileAccess::WRITE))) {
234
return; // FS persistence is not working or we are not writing.
235
}
236
bool is_file_persistent = p_file.begins_with("/userfs");
237
#ifdef TOOLS_ENABLED
238
// Hack for editor persistence (can we track).
239
is_file_persistent = is_file_persistent || p_file.begins_with("/home/web_user/");
240
#endif
241
if (is_file_persistent) {
242
os->idb_needs_sync = true;
243
}
244
}
245
246
void OS_Web::dir_access_remove_callback(const String &p_file) {
247
OS_Web *os = OS_Web::get_singleton();
248
bool is_file_persistent = p_file.begins_with("/userfs");
249
#ifdef TOOLS_ENABLED
250
// Hack for editor persistence (can we track).
251
is_file_persistent = is_file_persistent || p_file.begins_with("/home/web_user/");
252
#endif
253
if (is_file_persistent) {
254
os->idb_needs_sync = true;
255
}
256
}
257
258
void OS_Web::update_pwa_state_callback() {
259
if (OS_Web::get_singleton()) {
260
OS_Web::get_singleton()->pwa_is_waiting = true;
261
}
262
if (JavaScriptBridge::get_singleton()) {
263
JavaScriptBridge::get_singleton()->emit_signal("pwa_update_available");
264
}
265
}
266
267
void OS_Web::force_fs_sync() {
268
if (is_userfs_persistent()) {
269
idb_needs_sync = true;
270
}
271
}
272
273
Error OS_Web::pwa_update() {
274
return godot_js_pwa_update() ? FAILED : OK;
275
}
276
277
bool OS_Web::is_userfs_persistent() const {
278
return idb_available;
279
}
280
281
Error OS_Web::open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data) {
282
String path = p_path.get_file();
283
p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
284
ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, dlerror()));
285
286
if (p_data != nullptr && p_data->r_resolved_path != nullptr) {
287
*p_data->r_resolved_path = path;
288
}
289
290
return OK;
291
}
292
293
OS_Web *OS_Web::get_singleton() {
294
return static_cast<OS_Web *>(OS::get_singleton());
295
}
296
297
void OS_Web::initialize_joypads() {
298
}
299
300
OS_Web::OS_Web() {
301
char locale_ptr[16];
302
godot_js_config_locale_get(locale_ptr, 16);
303
setenv("LANG", locale_ptr, true);
304
305
godot_js_pwa_cb(&OS_Web::update_pwa_state_callback);
306
307
if (AudioDriverWeb::is_available()) {
308
audio_drivers.push_back(memnew(AudioDriverWorklet));
309
audio_drivers.push_back(memnew(AudioDriverScriptProcessor));
310
}
311
for (AudioDriverWeb *audio_driver : audio_drivers) {
312
AudioDriverManager::add_driver(audio_driver);
313
}
314
315
idb_available = godot_js_os_fs_is_persistent();
316
317
Vector<Logger *> loggers;
318
loggers.push_back(memnew(StdLogger));
319
_set_logger(memnew(CompositeLogger(loggers)));
320
321
FileAccessUnix::close_notification_func = file_access_close_callback;
322
DirAccessUnix::remove_notification_func = dir_access_remove_callback;
323
}
324
325