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