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