2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* os_web.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
#include "os_web.h"
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2023-06-08 12:51:32 +00:00
|
|
|
#include "api/javascript_bridge_singleton.h"
|
|
|
|
#include "display_server_web.h"
|
|
|
|
#include "godot_js.h"
|
|
|
|
|
2023-01-18 07:59:25 +00:00
|
|
|
#include "core/config/project_settings.h"
|
2020-03-16 16:02:14 +00:00
|
|
|
#include "core/debugger/engine_debugger.h"
|
2018-10-01 19:35:55 +00:00
|
|
|
#include "drivers/unix/dir_access_unix.h"
|
|
|
|
#include "drivers/unix/file_access_unix.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "main/main.h"
|
|
|
|
|
2021-11-12 12:42:58 +00:00
|
|
|
#include "modules/modules_enabled.gen.h" // For websocket.
|
2020-03-16 16:02:14 +00:00
|
|
|
|
2020-12-04 23:37:41 +00:00
|
|
|
#include <dlfcn.h>
|
2017-01-16 18:19:45 +00:00
|
|
|
#include <emscripten.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
void OS_Web::alert(const String &p_alert, const String &p_title) {
|
2021-07-22 16:23:48 +00:00
|
|
|
godot_js_display_alert(p_alert.utf8().get_data());
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
// Lifecycle
|
2022-08-28 18:27:45 +00:00
|
|
|
void OS_Web::initialize() {
|
2018-07-08 00:23:19 +00:00
|
|
|
OS_Unix::initialize_core();
|
2022-08-28 18:27:45 +00:00
|
|
|
DisplayServerWeb::register_web_driver();
|
2020-05-01 12:45:45 +00:00
|
|
|
}
|
2018-01-05 15:37:31 +00:00
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
void OS_Web::resume_audio() {
|
|
|
|
AudioDriverWeb::resume();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-09-02 23:44:00 +00:00
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
void OS_Web::set_main_loop(MainLoop *p_main_loop) {
|
2018-07-08 00:23:19 +00:00
|
|
|
main_loop = p_main_loop;
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
MainLoop *OS_Web::get_main_loop() const {
|
2018-07-08 00:23:19 +00:00
|
|
|
return main_loop;
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
void OS_Web::fs_sync_callback() {
|
2020-10-23 16:33:20 +00:00
|
|
|
get_singleton()->idb_is_syncing = false;
|
2018-07-08 00:23:19 +00:00
|
|
|
}
|
2015-09-12 13:54:47 +00:00
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
bool OS_Web::main_loop_iterate() {
|
2020-09-18 15:25:05 +00:00
|
|
|
if (is_userfs_persistent() && idb_needs_sync && !idb_is_syncing) {
|
|
|
|
idb_is_syncing = true;
|
|
|
|
idb_needs_sync = false;
|
2020-10-23 16:33:20 +00:00
|
|
|
godot_js_os_fs_sync(&fs_sync_callback);
|
2015-09-12 13:54:47 +00:00
|
|
|
}
|
2018-08-12 13:39:32 +00:00
|
|
|
|
2020-05-01 12:45:45 +00:00
|
|
|
DisplayServer::get_singleton()->process_events();
|
2018-08-12 13:39:32 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return Main::iteration();
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
void OS_Web::delete_main_loop() {
|
2020-05-01 12:45:45 +00:00
|
|
|
if (main_loop) {
|
|
|
|
memdelete(main_loop);
|
|
|
|
}
|
|
|
|
main_loop = nullptr;
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
void OS_Web::finalize() {
|
2020-05-01 12:45:45 +00:00
|
|
|
delete_main_loop();
|
2022-08-28 18:27:45 +00:00
|
|
|
for (AudioDriverWeb *driver : audio_drivers) {
|
2021-09-12 17:23:30 +00:00
|
|
|
memdelete(driver);
|
2020-06-29 16:51:53 +00:00
|
|
|
}
|
2021-09-12 17:23:30 +00:00
|
|
|
audio_drivers.clear();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
// Miscellaneous
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
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) {
|
2020-12-18 18:49:13 +00:00
|
|
|
return create_process(p_path, p_arguments);
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
Error OS_Web::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id, bool p_open_console) {
|
2020-05-01 12:45:45 +00:00
|
|
|
Array args;
|
2021-07-16 03:45:57 +00:00
|
|
|
for (const String &E : p_arguments) {
|
|
|
|
args.push_back(E);
|
2020-05-01 12:45:45 +00:00
|
|
|
}
|
2021-07-07 14:31:06 +00:00
|
|
|
String json_args = Variant(args).to_json_string();
|
2020-10-23 16:33:20 +00:00
|
|
|
int failed = godot_js_os_execute(json_args.utf8().get_data());
|
2022-08-28 18:27:45 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(failed, ERR_UNAVAILABLE, "OS::execute() or create_process() must be implemented in Web via 'engine.setOnExecute' if required.");
|
2020-05-01 12:45:45 +00:00
|
|
|
return OK;
|
2018-10-29 20:15:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
Error OS_Web::kill(const ProcessID &p_pid) {
|
|
|
|
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "OS::kill() is not available on the Web platform.");
|
2018-10-29 20:15:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
int OS_Web::get_process_id() const {
|
|
|
|
ERR_FAIL_V_MSG(0, "OS::get_process_id() is not available on the Web platform.");
|
2018-10-29 20:15:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
bool OS_Web::is_process_running(const ProcessID &p_pid) const {
|
2021-08-13 03:36:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
int OS_Web::get_processor_count() const {
|
2021-02-15 14:48:06 +00:00
|
|
|
return godot_js_os_hw_concurrency_get();
|
|
|
|
}
|
|
|
|
|
2023-09-27 13:17:02 +00:00
|
|
|
String OS_Web::get_unique_id() const {
|
|
|
|
ERR_FAIL_V_MSG("", "OS::get_unique_id() is not available on the Web platform.");
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
bool OS_Web::_check_internal_feature_support(const String &p_feature) {
|
|
|
|
if (p_feature == "web") {
|
2020-12-03 16:15:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-11-14 14:27:26 +00:00
|
|
|
if (godot_js_os_has_feature(p_feature.utf8().get_data())) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-08 00:23:19 +00:00
|
|
|
return false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
String OS_Web::get_executable_path() const {
|
2017-03-28 01:21:21 +00:00
|
|
|
return OS::get_executable_path();
|
2016-11-18 17:52:44 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
Error OS_Web::shell_open(String p_uri) {
|
2018-07-08 00:23:19 +00:00
|
|
|
// Open URI in a new tab, browser will deal with it by protocol.
|
2020-10-23 16:33:20 +00:00
|
|
|
godot_js_os_shell_open(p_uri.utf8().get_data());
|
2018-07-08 00:23:19 +00:00
|
|
|
return OK;
|
2015-09-12 13:54:47 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
String OS_Web::get_name() const {
|
|
|
|
return "Web";
|
2018-07-08 00:23:19 +00:00
|
|
|
}
|
2016-01-21 01:23:15 +00:00
|
|
|
|
2023-07-20 13:42:03 +00:00
|
|
|
void OS_Web::add_frame_delay(bool p_can_draw) {
|
|
|
|
#ifndef PROXY_TO_PTHREAD_ENABLED
|
|
|
|
OS::add_frame_delay(p_can_draw);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
void OS_Web::vibrate_handheld(int p_duration_ms) {
|
2022-07-27 12:35:01 +00:00
|
|
|
godot_js_input_vibrate_handheld(p_duration_ms);
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
String OS_Web::get_user_data_dir() const {
|
2023-01-18 07:59:25 +00:00
|
|
|
String userfs = "/userfs";
|
|
|
|
String appname = get_safe_dir_name(GLOBAL_GET("application/config/name"));
|
|
|
|
if (!appname.is_empty()) {
|
|
|
|
bool use_custom_dir = GLOBAL_GET("application/config/use_custom_user_dir");
|
|
|
|
if (use_custom_dir) {
|
|
|
|
String custom_dir = get_safe_dir_name(GLOBAL_GET("application/config/custom_user_dir_name"), true);
|
|
|
|
if (custom_dir.is_empty()) {
|
|
|
|
custom_dir = appname;
|
|
|
|
}
|
|
|
|
return userfs.path_join(custom_dir).replace("\\", "/");
|
|
|
|
} else {
|
|
|
|
return userfs.path_join(get_godot_dir_name()).path_join("app_userdata").path_join(appname).replace("\\", "/");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return userfs.path_join(get_godot_dir_name()).path_join("app_userdata").path_join("[unnamed project]");
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2016-01-21 01:23:15 +00:00
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
String OS_Web::get_cache_path() const {
|
2020-05-01 12:45:45 +00:00
|
|
|
return "/home/web_user/.cache";
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
String OS_Web::get_config_path() const {
|
2020-05-01 12:45:45 +00:00
|
|
|
return "/home/web_user/.config";
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
String OS_Web::get_data_path() const {
|
2020-05-01 12:45:45 +00:00
|
|
|
return "/home/web_user/.local/share";
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
void OS_Web::file_access_close_callback(const String &p_file, int p_flags) {
|
|
|
|
OS_Web *os = OS_Web::get_singleton();
|
2020-09-18 15:25:05 +00:00
|
|
|
if (!(os->is_userfs_persistent() && (p_flags & FileAccess::WRITE))) {
|
|
|
|
return; // FS persistence is not working or we are not writing.
|
|
|
|
}
|
|
|
|
bool is_file_persistent = p_file.begins_with("/userfs");
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
// Hack for editor persistence (can we track).
|
|
|
|
is_file_persistent = is_file_persistent || p_file.begins_with("/home/web_user/");
|
|
|
|
#endif
|
|
|
|
if (is_file_persistent) {
|
|
|
|
os->idb_needs_sync = true;
|
2018-07-08 00:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-11 23:15:21 +00:00
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
void OS_Web::update_pwa_state_callback() {
|
|
|
|
if (OS_Web::get_singleton()) {
|
|
|
|
OS_Web::get_singleton()->pwa_is_waiting = true;
|
2022-01-31 14:28:12 +00:00
|
|
|
}
|
2022-09-02 12:40:41 +00:00
|
|
|
if (JavaScriptBridge::get_singleton()) {
|
|
|
|
JavaScriptBridge::get_singleton()->emit_signal("pwa_update_available");
|
2022-01-31 14:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 11:17:42 +00:00
|
|
|
void OS_Web::force_fs_sync() {
|
|
|
|
if (is_userfs_persistent()) {
|
|
|
|
idb_needs_sync = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
Error OS_Web::pwa_update() {
|
2022-01-31 14:28:12 +00:00
|
|
|
return godot_js_pwa_update() ? FAILED : OK;
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
bool OS_Web::is_userfs_persistent() const {
|
2018-07-08 00:23:19 +00:00
|
|
|
return idb_available;
|
2017-10-02 14:09:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
Error OS_Web::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path, String *r_resolved_path) {
|
2020-12-04 23:37:41 +00:00
|
|
|
String path = p_path.get_file();
|
|
|
|
p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
|
2023-09-09 15:46:44 +00:00
|
|
|
ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, dlerror()));
|
2022-04-28 22:51:04 +00:00
|
|
|
|
|
|
|
if (r_resolved_path != nullptr) {
|
|
|
|
*r_resolved_path = path;
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:37:41 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
OS_Web *OS_Web::get_singleton() {
|
|
|
|
return static_cast<OS_Web *>(OS::get_singleton());
|
2017-10-02 14:09:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
void OS_Web::initialize_joypads() {
|
2020-05-01 12:45:45 +00:00
|
|
|
}
|
2018-07-08 00:23:19 +00:00
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
OS_Web::OS_Web() {
|
2020-10-23 16:33:20 +00:00
|
|
|
char locale_ptr[16];
|
|
|
|
godot_js_config_locale_get(locale_ptr, 16);
|
|
|
|
setenv("LANG", locale_ptr, true);
|
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
godot_js_pwa_cb(&OS_Web::update_pwa_state_callback);
|
2022-01-31 14:28:12 +00:00
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
if (AudioDriverWeb::is_available()) {
|
2021-09-12 17:23:30 +00:00
|
|
|
audio_drivers.push_back(memnew(AudioDriverWorklet));
|
|
|
|
}
|
|
|
|
for (int i = 0; i < audio_drivers.size(); i++) {
|
|
|
|
AudioDriverManager::add_driver(audio_drivers[i]);
|
2020-06-29 16:51:53 +00:00
|
|
|
}
|
2018-01-06 23:04:09 +00:00
|
|
|
|
2020-10-23 16:33:20 +00:00
|
|
|
idb_available = godot_js_os_fs_is_persistent();
|
|
|
|
|
2018-01-06 23:04:09 +00:00
|
|
|
Vector<Logger *> loggers;
|
|
|
|
loggers.push_back(memnew(StdLogger));
|
|
|
|
_set_logger(memnew(CompositeLogger(loggers)));
|
2018-03-04 17:18:05 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
FileAccessUnix::close_notification_func = file_access_close_callback;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|