2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* os_javascript.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* 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-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "os_javascript.h"
|
2017-01-16 19:19:45 +01:00
|
|
|
|
2020-03-16 17:02:14 +01:00
|
|
|
#include "core/debugger/engine_debugger.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/io/file_access_buffered_fa.h"
|
2020-05-01 14:45:45 +02:00
|
|
|
#include "core/io/json.h"
|
2018-10-01 21:35:55 +02:00
|
|
|
#include "drivers/unix/dir_access_unix.h"
|
|
|
|
#include "drivers/unix/file_access_unix.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "main/main.h"
|
2020-03-16 17:02:14 +01:00
|
|
|
#include "modules/modules_enabled.gen.h"
|
2020-05-01 14:45:45 +02:00
|
|
|
#include "platform/javascript/display_server_javascript.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-16 17:02:14 +01:00
|
|
|
#ifdef MODULE_WEBSOCKET_ENABLED
|
|
|
|
#include "modules/websocket/remote_debugger_peer_websocket.h"
|
|
|
|
#endif
|
|
|
|
|
2017-01-16 19:19:45 +01:00
|
|
|
#include <emscripten.h>
|
2017-03-05 16:44:50 +01:00
|
|
|
#include <stdlib.h>
|
2017-01-16 19:19:45 +01:00
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
bool OS_JavaScript::has_touchscreen_ui_hint() const {
|
|
|
|
/* clang-format off */
|
|
|
|
return EM_ASM_INT_V(
|
|
|
|
return 'ontouchstart' in window;
|
|
|
|
);
|
|
|
|
/* clang-format on */
|
2016-11-26 13:13:16 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
// Audio
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
int OS_JavaScript::get_audio_driver_count() const {
|
|
|
|
return 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
const char *OS_JavaScript::get_audio_driver_name(int p_driver) const {
|
|
|
|
return "JavaScript";
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
// Lifecycle
|
2020-05-01 14:45:45 +02:00
|
|
|
void OS_JavaScript::initialize() {
|
2018-07-08 02:23:19 +02:00
|
|
|
OS_Unix::initialize_core();
|
2020-03-17 07:33:00 +01:00
|
|
|
FileAccess::make_default<FileAccessBufferedFA<FileAccessUnix>>(FileAccess::ACCESS_RESOURCES);
|
2020-05-01 14:45:45 +02:00
|
|
|
DisplayServerJavaScript::register_javascript_driver();
|
2018-07-08 02:23:19 +02:00
|
|
|
|
2020-03-16 17:02:14 +01:00
|
|
|
#ifdef MODULE_WEBSOCKET_ENABLED
|
|
|
|
EngineDebugger::register_uri_handler("ws://", RemoteDebuggerPeerWebSocket::create);
|
|
|
|
EngineDebugger::register_uri_handler("wss://", RemoteDebuggerPeerWebSocket::create);
|
|
|
|
#endif
|
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
char locale_ptr[16];
|
|
|
|
/* clang-format off */
|
2020-05-01 14:45:45 +02:00
|
|
|
EM_ASM({
|
2020-05-08 16:55:01 +02:00
|
|
|
stringToUTF8(Module['locale'], $0, 16);
|
2018-07-08 02:23:19 +02:00
|
|
|
}, locale_ptr);
|
|
|
|
/* clang-format on */
|
|
|
|
setenv("LANG", locale_ptr, true);
|
2020-05-01 14:45:45 +02:00
|
|
|
}
|
2018-01-05 16:37:31 +01:00
|
|
|
|
2020-05-01 14:45:45 +02:00
|
|
|
void OS_JavaScript::resume_audio() {
|
|
|
|
audio_driver_javascript.resume();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-09-03 01:44:00 +02:00
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
void OS_JavaScript::set_main_loop(MainLoop *p_main_loop) {
|
|
|
|
main_loop = p_main_loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
MainLoop *OS_JavaScript::get_main_loop() const {
|
|
|
|
return main_loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::main_loop_callback() {
|
|
|
|
get_singleton()->main_loop_iterate();
|
|
|
|
}
|
2015-09-12 15:54:47 +02:00
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
bool OS_JavaScript::main_loop_iterate() {
|
|
|
|
if (is_userfs_persistent() && sync_wait_time >= 0) {
|
|
|
|
int64_t current_time = get_ticks_msec();
|
|
|
|
int64_t elapsed_time = current_time - last_sync_check_time;
|
|
|
|
last_sync_check_time = current_time;
|
2015-09-12 15:54:47 +02:00
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
sync_wait_time -= elapsed_time;
|
2015-09-12 15:54:47 +02:00
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
if (sync_wait_time < 0) {
|
2017-01-15 20:00:04 +01:00
|
|
|
/* clang-format off */
|
2015-09-12 15:54:47 +02:00
|
|
|
EM_ASM(
|
2019-02-24 04:40:06 +01:00
|
|
|
FS.syncfs(function(error) {
|
|
|
|
if (error) { err('Failed to save IDB file system: ' + error.message); }
|
2017-01-12 14:14:40 +01:00
|
|
|
});
|
2015-09-12 15:54:47 +02:00
|
|
|
);
|
2017-01-15 20:00:04 +01:00
|
|
|
/* clang-format on */
|
2015-09-12 15:54:47 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-12 15:39:32 +02:00
|
|
|
|
2020-05-01 14:45:45 +02:00
|
|
|
DisplayServer::get_singleton()->process_events();
|
2018-08-12 15:39:32 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return Main::iteration();
|
|
|
|
}
|
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
void OS_JavaScript::delete_main_loop() {
|
2020-05-01 14:45:45 +02:00
|
|
|
if (main_loop) {
|
|
|
|
memdelete(main_loop);
|
|
|
|
}
|
|
|
|
main_loop = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::finalize_async() {
|
|
|
|
finalizing = true;
|
|
|
|
audio_driver_javascript.finish_async();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
void OS_JavaScript::finalize() {
|
2020-05-01 14:45:45 +02:00
|
|
|
delete_main_loop();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
// Miscellaneous
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-05-14 10:28:27 +02:00
|
|
|
Error OS_JavaScript::execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex) {
|
2020-05-01 14:45:45 +02:00
|
|
|
Array args;
|
|
|
|
for (const List<String>::Element *E = p_arguments.front(); E; E = E->next()) {
|
|
|
|
args.push_back(E->get());
|
|
|
|
}
|
|
|
|
String json_args = JSON::print(args);
|
|
|
|
/* clang-format off */
|
|
|
|
int failed = EM_ASM_INT({
|
|
|
|
const json_args = UTF8ToString($0);
|
|
|
|
const args = JSON.parse(json_args);
|
|
|
|
if (Module["onExecute"]) {
|
|
|
|
Module["onExecute"](args);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}, json_args.utf8().get_data());
|
|
|
|
/* clang-format on */
|
|
|
|
ERR_FAIL_COND_V_MSG(failed, ERR_UNAVAILABLE, "OS::execute() must be implemented in JavaScript via 'engine.setOnExecute' if required.");
|
|
|
|
return OK;
|
2018-10-29 21:15:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Error OS_JavaScript::kill(const ProcessID &p_pid) {
|
2019-08-09 06:49:33 +02:00
|
|
|
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "OS::kill() is not available on the HTML5 platform.");
|
2018-10-29 21:15:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int OS_JavaScript::get_process_id() const {
|
2019-08-09 06:49:33 +02:00
|
|
|
ERR_FAIL_V_MSG(0, "OS::get_process_id() is not available on the HTML5 platform.");
|
2018-10-29 21:15:15 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
bool OS_JavaScript::_check_internal_feature_support(const String &p_feature) {
|
|
|
|
if (p_feature == "HTML5" || p_feature == "web")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
#ifdef JAVASCRIPT_EVAL_ENABLED
|
|
|
|
if (p_feature == "JavaScript")
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2016-11-18 18:52:44 +01:00
|
|
|
String OS_JavaScript::get_executable_path() const {
|
2017-03-28 03:21:21 +02:00
|
|
|
return OS::get_executable_path();
|
2016-11-18 18:52:44 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
Error OS_JavaScript::shell_open(String p_uri) {
|
|
|
|
// Open URI in a new tab, browser will deal with it by protocol.
|
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_({
|
|
|
|
window.open(UTF8ToString($0), '_blank');
|
|
|
|
}, p_uri.utf8().get_data());
|
|
|
|
/* clang-format on */
|
|
|
|
return OK;
|
2015-09-12 15:54:47 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-05-20 19:36:24 +02:00
|
|
|
String OS_JavaScript::get_name() const {
|
2018-07-08 02:23:19 +02:00
|
|
|
return "HTML5";
|
|
|
|
}
|
2016-01-21 02:23:15 +01:00
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
bool OS_JavaScript::can_draw() const {
|
|
|
|
return true; // Always?
|
2016-01-21 02:23:15 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
String OS_JavaScript::get_user_data_dir() const {
|
|
|
|
return "/userfs";
|
|
|
|
};
|
2016-01-21 02:23:15 +01:00
|
|
|
|
2020-05-01 14:45:45 +02:00
|
|
|
String OS_JavaScript::get_cache_path() const {
|
|
|
|
return "/home/web_user/.cache";
|
|
|
|
}
|
|
|
|
|
|
|
|
String OS_JavaScript::get_config_path() const {
|
|
|
|
return "/home/web_user/.config";
|
|
|
|
}
|
|
|
|
|
|
|
|
String OS_JavaScript::get_data_path() const {
|
|
|
|
return "/home/web_user/.local/share";
|
2016-01-21 02:23:15 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
void OS_JavaScript::file_access_close_callback(const String &p_file, int p_flags) {
|
|
|
|
OS_JavaScript *os = get_singleton();
|
|
|
|
if (os->is_userfs_persistent() && p_file.begins_with("/userfs") && p_flags & FileAccess::WRITE) {
|
|
|
|
os->last_sync_check_time = OS::get_singleton()->get_ticks_msec();
|
|
|
|
// Wait five seconds in case more files are about to be closed.
|
|
|
|
os->sync_wait_time = 5000;
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 00:15:21 +01:00
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
void OS_JavaScript::set_idb_available(bool p_idb_available) {
|
|
|
|
idb_available = p_idb_available;
|
2017-07-19 22:00:46 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
bool OS_JavaScript::is_userfs_persistent() const {
|
|
|
|
return idb_available;
|
2017-10-02 16:09:24 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
OS_JavaScript *OS_JavaScript::get_singleton() {
|
|
|
|
return static_cast<OS_JavaScript *>(OS::get_singleton());
|
2017-10-02 16:09:24 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 14:45:45 +02:00
|
|
|
void OS_JavaScript::initialize_joypads() {
|
|
|
|
}
|
2018-07-08 02:23:19 +02:00
|
|
|
|
2020-05-01 14:45:45 +02:00
|
|
|
OS_JavaScript::OS_JavaScript() {
|
2018-07-08 02:23:19 +02:00
|
|
|
AudioDriverManager::add_driver(&audio_driver_javascript);
|
2018-01-07 00:04:09 +01:00
|
|
|
|
|
|
|
Vector<Logger *> loggers;
|
|
|
|
loggers.push_back(memnew(StdLogger));
|
|
|
|
_set_logger(memnew(CompositeLogger(loggers)));
|
2018-03-04 18:18:05 +01:00
|
|
|
|
2018-07-08 02:23:19 +02:00
|
|
|
FileAccessUnix::close_notification_func = file_access_close_callback;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|