[HTML5] Port JavaScript inline code to libraries.
The API is implemented in javascript, and generates C functions that can be called from godot. This allows much cleaner code replacing all `EM_ASM` calls in our C++ code with plain C function calls. This also gets rid of few hacks and comes with few optimizations (e.g. custom cursor shapes should be much faster now).
This commit is contained in:
parent
54cda5c3b8
commit
e2083871eb
33 changed files with 1995 additions and 1461 deletions
|
|
@ -18,21 +18,22 @@ if env["threads_enabled"]:
|
|||
|
||||
build = env.add_program(build_targets, javascript_files)
|
||||
|
||||
js_libraries = [
|
||||
"native/http_request.js",
|
||||
"native/library_godot_audio.js",
|
||||
]
|
||||
for lib in js_libraries:
|
||||
env.Append(LINKFLAGS=["--js-library", env.File(lib).path])
|
||||
env.Depends(build, js_libraries)
|
||||
env.AddJSLibraries(
|
||||
[
|
||||
"native/http_request.js",
|
||||
"native/library_godot_audio.js",
|
||||
"native/library_godot_display.js",
|
||||
"native/library_godot_os.js",
|
||||
]
|
||||
)
|
||||
|
||||
js_pre = [
|
||||
"native/id_handler.js",
|
||||
"native/utils.js",
|
||||
]
|
||||
for js in js_pre:
|
||||
env.Append(LINKFLAGS=["--pre-js", env.File(js).path])
|
||||
env.Depends(build, js_pre)
|
||||
if env["tools"]:
|
||||
env.AddJSLibraries(["native/library_godot_editor_tools.js"])
|
||||
if env["javascript_eval"]:
|
||||
env.AddJSLibraries(["native/library_godot_eval.js"])
|
||||
for lib in env["JS_LIBS"]:
|
||||
env.Append(LINKFLAGS=["--js-library", lib])
|
||||
env.Depends(build, env["JS_LIBS"])
|
||||
|
||||
engine = [
|
||||
"engine/preloader.js",
|
||||
|
|
|
|||
|
|
@ -39,6 +39,11 @@
|
|||
|
||||
#include <emscripten/emscripten.h>
|
||||
|
||||
// JavaScript functions defined in library_godot_editor_tools.js
|
||||
extern "C" {
|
||||
extern void godot_js_editor_download_file(const char *p_path, const char *p_name, const char *p_mime);
|
||||
}
|
||||
|
||||
static void _javascript_editor_init_callback() {
|
||||
EditorNode::get_singleton()->add_editor_plugin(memnew(JavaScriptToolsEditorPlugin(EditorNode::get_singleton())));
|
||||
}
|
||||
|
|
@ -65,25 +70,7 @@ void JavaScriptToolsEditorPlugin::_download_zip(Variant p_v) {
|
|||
String base_path = resource_path.substr(0, resource_path.rfind("/")) + "/";
|
||||
_zip_recursive(resource_path, base_path, zip);
|
||||
zipClose(zip, NULL);
|
||||
EM_ASM({
|
||||
const path = "/tmp/project.zip";
|
||||
const size = FS.stat(path)["size"];
|
||||
const buf = new Uint8Array(size);
|
||||
const fd = FS.open(path, "r");
|
||||
FS.read(fd, buf, 0, size);
|
||||
FS.close(fd);
|
||||
FS.unlink(path);
|
||||
const blob = new Blob([buf], { type: "application/zip" });
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = "project.zip";
|
||||
a.style.display = "none";
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
window.URL.revokeObjectURL(url);
|
||||
});
|
||||
godot_js_editor_download_file("/tmp/project.zip", "project.zip", "application/zip");
|
||||
}
|
||||
|
||||
void JavaScriptToolsEditorPlugin::_bind_methods() {
|
||||
|
|
|
|||
|
|
@ -55,33 +55,33 @@ void AudioDriverJavaScript::_audio_thread_func(void *p_data) {
|
|||
OS::get_singleton()->delay_usec(1000); // Give the browser some slack.
|
||||
continue;
|
||||
}
|
||||
obj->_js_driver_process();
|
||||
obj->_audio_driver_process();
|
||||
obj->needs_process = false;
|
||||
obj->unlock();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_process_start() {
|
||||
void AudioDriverJavaScript::_audio_driver_process_start() {
|
||||
#ifndef NO_THREADS
|
||||
AudioDriverJavaScript::singleton->lock();
|
||||
singleton->lock();
|
||||
#else
|
||||
AudioDriverJavaScript::singleton->_js_driver_process();
|
||||
singleton->_audio_driver_process();
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_process_end() {
|
||||
void AudioDriverJavaScript::_audio_driver_process_end() {
|
||||
#ifndef NO_THREADS
|
||||
AudioDriverJavaScript::singleton->needs_process = true;
|
||||
AudioDriverJavaScript::singleton->unlock();
|
||||
singleton->needs_process = true;
|
||||
singleton->unlock();
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_process_capture(float sample) {
|
||||
AudioDriverJavaScript::singleton->process_capture(sample);
|
||||
void AudioDriverJavaScript::_audio_driver_process_capture(float p_sample) {
|
||||
singleton->process_capture(p_sample);
|
||||
}
|
||||
|
||||
void AudioDriverJavaScript::_js_driver_process() {
|
||||
void AudioDriverJavaScript::_audio_driver_process() {
|
||||
int sample_count = memarr_len(internal_buffer) / channel_count;
|
||||
int32_t *stream_buffer = reinterpret_cast<int32_t *>(internal_buffer);
|
||||
audio_server_process(sample_count, stream_buffer);
|
||||
|
|
@ -122,7 +122,7 @@ void AudioDriverJavaScript::start() {
|
|||
#ifndef NO_THREADS
|
||||
thread = Thread::create(_audio_thread_func, this);
|
||||
#endif
|
||||
godot_audio_start(internal_buffer);
|
||||
godot_audio_start(internal_buffer, &_audio_driver_process_start, &_audio_driver_process_end, &_audio_driver_process_capture);
|
||||
}
|
||||
|
||||
void AudioDriverJavaScript::resume() {
|
||||
|
|
@ -153,18 +153,12 @@ void AudioDriverJavaScript::unlock() {
|
|||
#endif
|
||||
}
|
||||
|
||||
void AudioDriverJavaScript::finish_async() {
|
||||
#ifndef NO_THREADS
|
||||
quit = true; // Ask thread to quit.
|
||||
#endif
|
||||
godot_audio_finish_async();
|
||||
}
|
||||
|
||||
void AudioDriverJavaScript::finish() {
|
||||
#ifndef NO_THREADS
|
||||
quit = true; // Ask thread to quit.
|
||||
Thread::wait_to_finish(thread);
|
||||
memdelete(thread);
|
||||
thread = NULL;
|
||||
thread = nullptr;
|
||||
#endif
|
||||
if (internal_buffer) {
|
||||
memdelete_arr(internal_buffer);
|
||||
|
|
@ -173,13 +167,13 @@ void AudioDriverJavaScript::finish() {
|
|||
}
|
||||
|
||||
Error AudioDriverJavaScript::capture_start() {
|
||||
godot_audio_capture_stop();
|
||||
input_buffer_init(buffer_length);
|
||||
godot_audio_capture_start();
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error AudioDriverJavaScript::capture_stop() {
|
||||
godot_audio_capture_stop();
|
||||
input_buffer.clear();
|
||||
return OK;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,6 @@ private:
|
|||
int mix_rate = 0;
|
||||
int channel_count = 0;
|
||||
|
||||
public:
|
||||
#ifndef NO_THREADS
|
||||
Mutex mutex;
|
||||
Thread *thread = nullptr;
|
||||
|
|
@ -53,9 +52,12 @@ public:
|
|||
|
||||
static void _audio_thread_func(void *p_data);
|
||||
#endif
|
||||
static void _audio_driver_process_start();
|
||||
static void _audio_driver_process_end();
|
||||
static void _audio_driver_process_capture(float p_sample);
|
||||
void _audio_driver_process();
|
||||
|
||||
void _js_driver_process();
|
||||
|
||||
public:
|
||||
static bool is_available();
|
||||
void process_capture(float sample);
|
||||
|
||||
|
|
@ -72,7 +74,6 @@ public:
|
|||
void lock() override;
|
||||
void unlock() override;
|
||||
void finish() override;
|
||||
void finish_async();
|
||||
|
||||
Error capture_start() override;
|
||||
Error capture_stop() override;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import os
|
||||
|
||||
from emscripten_helpers import run_closure_compiler, create_engine_file
|
||||
from emscripten_helpers import run_closure_compiler, create_engine_file, add_js_libraries
|
||||
from SCons.Util import WhereIs
|
||||
|
||||
|
||||
|
|
@ -96,6 +96,9 @@ def configure(env):
|
|||
jscc = env.Builder(generator=run_closure_compiler, suffix=".cc.js", src_suffix=".js")
|
||||
env.Append(BUILDERS={"BuildJS": jscc})
|
||||
|
||||
# Add helper method for adding libraries.
|
||||
env.AddMethod(add_js_libraries, "AddJSLibraries")
|
||||
|
||||
# Add method that joins/compiles our Engine files.
|
||||
env.AddMethod(create_engine_file, "CreateEngineFile")
|
||||
|
||||
|
|
@ -166,6 +169,6 @@ def configure(env):
|
|||
env.Append(LINKFLAGS=["-s", "OFFSCREEN_FRAMEBUFFER=1"])
|
||||
|
||||
# callMain for manual start, FS for preloading, PATH and ERRNO_CODES for BrowserFS.
|
||||
env.Append(LINKFLAGS=["-s", "EXTRA_EXPORTED_RUNTIME_METHODS=['callMain', 'FS', 'PATH']"])
|
||||
env.Append(LINKFLAGS=["-s", "EXTRA_EXPORTED_RUNTIME_METHODS=['callMain']"])
|
||||
# Add code that allow exiting runtime.
|
||||
env.Append(LINKFLAGS=["-s", "EXIT_RUNTIME=1"])
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@
|
|||
#include <png.h>
|
||||
|
||||
#include "dom_keys.inc"
|
||||
#include "godot_js.h"
|
||||
|
||||
#define DOM_BUTTON_LEFT 0
|
||||
#define DOM_BUTTON_MIDDLE 1
|
||||
|
|
@ -44,28 +45,17 @@
|
|||
#define DOM_BUTTON_XBUTTON1 3
|
||||
#define DOM_BUTTON_XBUTTON2 4
|
||||
|
||||
char DisplayServerJavaScript::canvas_id[256] = { 0 };
|
||||
static bool cursor_inside_canvas = true;
|
||||
|
||||
DisplayServerJavaScript *DisplayServerJavaScript::get_singleton() {
|
||||
return static_cast<DisplayServerJavaScript *>(DisplayServer::get_singleton());
|
||||
}
|
||||
|
||||
// Window (canvas)
|
||||
void DisplayServerJavaScript::focus_canvas() {
|
||||
/* clang-format off */
|
||||
EM_ASM(
|
||||
Module['canvas'].focus();
|
||||
);
|
||||
/* clang-format on */
|
||||
godot_js_display_canvas_focus();
|
||||
}
|
||||
|
||||
bool DisplayServerJavaScript::is_canvas_focused() {
|
||||
/* clang-format off */
|
||||
return EM_ASM_INT_V(
|
||||
return document.activeElement == Module['canvas'];
|
||||
);
|
||||
/* clang-format on */
|
||||
return godot_js_display_canvas_is_focused() != 0;
|
||||
}
|
||||
|
||||
bool DisplayServerJavaScript::check_size_force_redraw() {
|
||||
|
|
@ -83,19 +73,17 @@ bool DisplayServerJavaScript::check_size_force_redraw() {
|
|||
}
|
||||
|
||||
Point2 DisplayServerJavaScript::compute_position_in_canvas(int p_x, int p_y) {
|
||||
int canvas_x = EM_ASM_INT({
|
||||
return Module['canvas'].getBoundingClientRect().x;
|
||||
});
|
||||
int canvas_y = EM_ASM_INT({
|
||||
return Module['canvas'].getBoundingClientRect().y;
|
||||
});
|
||||
DisplayServerJavaScript *display = get_singleton();
|
||||
int canvas_x;
|
||||
int canvas_y;
|
||||
godot_js_display_canvas_bounding_rect_position_get(&canvas_x, &canvas_y);
|
||||
int canvas_width;
|
||||
int canvas_height;
|
||||
emscripten_get_canvas_element_size(canvas_id, &canvas_width, &canvas_height);
|
||||
emscripten_get_canvas_element_size(display->canvas_id, &canvas_width, &canvas_height);
|
||||
|
||||
double element_width;
|
||||
double element_height;
|
||||
emscripten_get_element_css_size(canvas_id, &element_width, &element_height);
|
||||
emscripten_get_element_css_size(display->canvas_id, &element_width, &element_height);
|
||||
|
||||
return Point2((int)(canvas_width / element_width * (p_x - canvas_x)),
|
||||
(int)(canvas_height / element_height * (p_y - canvas_y)));
|
||||
|
|
@ -105,8 +93,7 @@ EM_BOOL DisplayServerJavaScript::fullscreen_change_callback(int p_event_type, co
|
|||
DisplayServerJavaScript *display = get_singleton();
|
||||
// Empty ID is canvas.
|
||||
String target_id = String::utf8(p_event->id);
|
||||
String canvas_str_id = String::utf8(canvas_id);
|
||||
if (target_id.empty() || target_id == canvas_str_id) {
|
||||
if (target_id.empty() || target_id == String::utf8(display->canvas_id)) {
|
||||
// This event property is the only reliable data on
|
||||
// browser fullscreen state.
|
||||
if (p_event->isFullscreen) {
|
||||
|
|
@ -118,14 +105,15 @@ EM_BOOL DisplayServerJavaScript::fullscreen_change_callback(int p_event_type, co
|
|||
return false;
|
||||
}
|
||||
|
||||
// Drag and drop callback (see native/utils.js).
|
||||
extern "C" EMSCRIPTEN_KEEPALIVE void _drop_files_callback(char *p_filev[], int p_filec) {
|
||||
DisplayServerJavaScript *ds = DisplayServerJavaScript::get_singleton();
|
||||
// Drag and drop callback.
|
||||
void DisplayServerJavaScript::drop_files_js_callback(char **p_filev, int p_filec) {
|
||||
DisplayServerJavaScript *ds = get_singleton();
|
||||
if (!ds) {
|
||||
ERR_FAIL_MSG("Unable to drop files because the DisplayServer is not active");
|
||||
}
|
||||
if (ds->drop_files_callback.is_null())
|
||||
if (ds->drop_files_callback.is_null()) {
|
||||
return;
|
||||
}
|
||||
Vector<String> files;
|
||||
for (int i = 0; i < p_filec; i++) {
|
||||
files.push_back(String::utf8(p_filev[i]));
|
||||
|
|
@ -137,6 +125,18 @@ extern "C" EMSCRIPTEN_KEEPALIVE void _drop_files_callback(char *p_filev[], int p
|
|||
ds->drop_files_callback.call((const Variant **)&vp, 1, ret, ce);
|
||||
}
|
||||
|
||||
// JavaScript quit request callback.
|
||||
void DisplayServerJavaScript::request_quit_callback() {
|
||||
DisplayServerJavaScript *ds = get_singleton();
|
||||
if (ds && !ds->window_event_callback.is_null()) {
|
||||
Variant event = int(DisplayServer::WINDOW_EVENT_CLOSE_REQUEST);
|
||||
Variant *eventp = &event;
|
||||
Variant ret;
|
||||
Callable::CallError ce;
|
||||
ds->window_event_callback.call((const Variant **)&eventp, 1, ret, ce);
|
||||
}
|
||||
}
|
||||
|
||||
// Keys
|
||||
|
||||
template <typename T>
|
||||
|
|
@ -272,12 +272,13 @@ EM_BOOL DisplayServerJavaScript::mouse_button_callback(int p_event_type, const E
|
|||
}
|
||||
|
||||
EM_BOOL DisplayServerJavaScript::mousemove_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data) {
|
||||
DisplayServerJavaScript *ds = get_singleton();
|
||||
Input *input = Input::get_singleton();
|
||||
int input_mask = input->get_mouse_button_mask();
|
||||
Point2 pos = compute_position_in_canvas(p_event->clientX, p_event->clientY);
|
||||
// For motion outside the canvas, only read mouse movement if dragging
|
||||
// started inside the canvas; imitating desktop app behaviour.
|
||||
if (!cursor_inside_canvas && !input_mask)
|
||||
if (!ds->cursor_inside_canvas && !input_mask)
|
||||
return false;
|
||||
|
||||
Ref<InputEventMouseMotion> ev;
|
||||
|
|
@ -339,35 +340,13 @@ const char *DisplayServerJavaScript::godot2dom_cursor(DisplayServer::CursorShape
|
|||
}
|
||||
}
|
||||
|
||||
void DisplayServerJavaScript::set_css_cursor(const char *p_cursor) {
|
||||
/* clang-format off */
|
||||
EM_ASM_({
|
||||
Module['canvas'].style.cursor = UTF8ToString($0);
|
||||
}, p_cursor);
|
||||
/* clang-format on */
|
||||
}
|
||||
|
||||
bool DisplayServerJavaScript::is_css_cursor_hidden() const {
|
||||
/* clang-format off */
|
||||
return EM_ASM_INT({
|
||||
return Module['canvas'].style.cursor === 'none';
|
||||
});
|
||||
/* clang-format on */
|
||||
}
|
||||
|
||||
void DisplayServerJavaScript::cursor_set_shape(CursorShape p_shape) {
|
||||
ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
|
||||
|
||||
if (mouse_get_mode() == MOUSE_MODE_VISIBLE) {
|
||||
if (cursors[p_shape] != "") {
|
||||
Vector<String> url = cursors[p_shape].split("?");
|
||||
set_css_cursor(("url(\"" + url[0] + "\") " + url[1] + ", auto").utf8());
|
||||
} else {
|
||||
set_css_cursor(godot2dom_cursor(p_shape));
|
||||
}
|
||||
if (cursor_shape == p_shape) {
|
||||
return;
|
||||
}
|
||||
|
||||
cursor_shape = p_shape;
|
||||
godot_js_display_cursor_set_shape(godot2dom_cursor(cursor_shape));
|
||||
}
|
||||
|
||||
DisplayServer::CursorShape DisplayServerJavaScript::cursor_get_shape() const {
|
||||
|
|
@ -376,17 +355,6 @@ DisplayServer::CursorShape DisplayServerJavaScript::cursor_get_shape() const {
|
|||
|
||||
void DisplayServerJavaScript::cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
|
||||
if (p_cursor.is_valid()) {
|
||||
Map<CursorShape, Vector<Variant>>::Element *cursor_c = cursors_cache.find(p_shape);
|
||||
|
||||
if (cursor_c) {
|
||||
if (cursor_c->get()[0] == p_cursor && cursor_c->get()[1] == p_hotspot) {
|
||||
cursor_set_shape(p_shape);
|
||||
return;
|
||||
}
|
||||
|
||||
cursors_cache.erase(p_shape);
|
||||
}
|
||||
|
||||
Ref<Texture2D> texture = p_cursor;
|
||||
Ref<AtlasTexture> atlas_texture = p_cursor;
|
||||
Ref<Image> image;
|
||||
|
|
@ -449,53 +417,10 @@ void DisplayServerJavaScript::cursor_set_custom_image(const RES &p_cursor, Curso
|
|||
png.resize(len);
|
||||
ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
|
||||
|
||||
char *object_url;
|
||||
/* clang-format off */
|
||||
EM_ASM({
|
||||
var PNG_PTR = $0;
|
||||
var PNG_LEN = $1;
|
||||
var PTR = $2;
|
||||
godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), png.ptr(), len, p_hotspot.x, p_hotspot.y);
|
||||
|
||||
var png = new Blob([HEAPU8.slice(PNG_PTR, PNG_PTR + PNG_LEN)], { type: 'image/png' });
|
||||
var url = URL.createObjectURL(png);
|
||||
var length_bytes = lengthBytesUTF8(url) + 1;
|
||||
var string_on_wasm_heap = _malloc(length_bytes);
|
||||
setValue(PTR, string_on_wasm_heap, '*');
|
||||
stringToUTF8(url, string_on_wasm_heap, length_bytes);
|
||||
}, png.ptr(), len, &object_url);
|
||||
/* clang-format on */
|
||||
|
||||
String url = String::utf8(object_url) + "?" + itos(p_hotspot.x) + " " + itos(p_hotspot.y);
|
||||
|
||||
/* clang-format off */
|
||||
EM_ASM({ _free($0); }, object_url);
|
||||
/* clang-format on */
|
||||
|
||||
if (cursors[p_shape] != "") {
|
||||
/* clang-format off */
|
||||
EM_ASM({
|
||||
URL.revokeObjectURL(UTF8ToString($0).split('?')[0]);
|
||||
}, cursors[p_shape].utf8().get_data());
|
||||
/* clang-format on */
|
||||
cursors[p_shape] = "";
|
||||
}
|
||||
|
||||
cursors[p_shape] = url;
|
||||
|
||||
Vector<Variant> params;
|
||||
params.push_back(p_cursor);
|
||||
params.push_back(p_hotspot);
|
||||
cursors_cache.insert(p_shape, params);
|
||||
|
||||
} else if (cursors[p_shape] != "") {
|
||||
/* clang-format off */
|
||||
EM_ASM({
|
||||
URL.revokeObjectURL(UTF8ToString($0).split('?')[0]);
|
||||
}, cursors[p_shape].utf8().get_data());
|
||||
/* clang-format on */
|
||||
cursors[p_shape] = "";
|
||||
|
||||
cursors_cache.erase(p_shape);
|
||||
} else {
|
||||
godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), NULL, 0, 0, 0);
|
||||
}
|
||||
|
||||
cursor_set_shape(cursor_shape);
|
||||
|
|
@ -508,40 +433,37 @@ void DisplayServerJavaScript::mouse_set_mode(MouseMode p_mode) {
|
|||
return;
|
||||
|
||||
if (p_mode == MOUSE_MODE_VISIBLE) {
|
||||
// set_css_cursor must be called before set_cursor_shape to make the cursor visible
|
||||
set_css_cursor(godot2dom_cursor(cursor_shape));
|
||||
cursor_set_shape(cursor_shape);
|
||||
godot_js_display_cursor_set_visible(1);
|
||||
emscripten_exit_pointerlock();
|
||||
|
||||
} else if (p_mode == MOUSE_MODE_HIDDEN) {
|
||||
set_css_cursor("none");
|
||||
godot_js_display_cursor_set_visible(0);
|
||||
emscripten_exit_pointerlock();
|
||||
|
||||
} else if (p_mode == MOUSE_MODE_CAPTURED) {
|
||||
EMSCRIPTEN_RESULT result = emscripten_request_pointerlock("canvas", false);
|
||||
godot_js_display_cursor_set_visible(1);
|
||||
EMSCRIPTEN_RESULT result = emscripten_request_pointerlock(canvas_id, false);
|
||||
ERR_FAIL_COND_MSG(result == EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED, "MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback.");
|
||||
ERR_FAIL_COND_MSG(result != EMSCRIPTEN_RESULT_SUCCESS, "MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback.");
|
||||
// set_css_cursor must be called before cursor_set_shape to make the cursor visible
|
||||
set_css_cursor(godot2dom_cursor(cursor_shape));
|
||||
cursor_set_shape(cursor_shape);
|
||||
}
|
||||
}
|
||||
|
||||
DisplayServer::MouseMode DisplayServerJavaScript::mouse_get_mode() const {
|
||||
if (is_css_cursor_hidden())
|
||||
if (godot_js_display_cursor_is_hidden()) {
|
||||
return MOUSE_MODE_HIDDEN;
|
||||
}
|
||||
|
||||
EmscriptenPointerlockChangeEvent ev;
|
||||
emscripten_get_pointerlock_status(&ev);
|
||||
return (ev.isActive && String::utf8(ev.id) == "canvas") ? MOUSE_MODE_CAPTURED : MOUSE_MODE_VISIBLE;
|
||||
return (ev.isActive && String::utf8(ev.id) == String::utf8(canvas_id)) ? MOUSE_MODE_CAPTURED : MOUSE_MODE_VISIBLE;
|
||||
}
|
||||
|
||||
// Wheel
|
||||
|
||||
EM_BOOL DisplayServerJavaScript::wheel_callback(int p_event_type, const EmscriptenWheelEvent *p_event, void *p_user_data) {
|
||||
ERR_FAIL_COND_V(p_event_type != EMSCRIPTEN_EVENT_WHEEL, false);
|
||||
DisplayServerJavaScript *ds = get_singleton();
|
||||
if (!is_canvas_focused()) {
|
||||
if (cursor_inside_canvas) {
|
||||
if (ds->cursor_inside_canvas) {
|
||||
focus_canvas();
|
||||
} else {
|
||||
return false;
|
||||
|
|
@ -632,7 +554,7 @@ EM_BOOL DisplayServerJavaScript::touchmove_callback(int p_event_type, const Emsc
|
|||
}
|
||||
|
||||
bool DisplayServerJavaScript::screen_is_touchscreen(int p_screen) const {
|
||||
return EM_ASM_INT({ return 'ontouchstart' in window; });
|
||||
return godot_js_display_touchscreen_is_available();
|
||||
}
|
||||
|
||||
// Gamepad
|
||||
|
|
@ -696,53 +618,30 @@ Vector<String> DisplayServerJavaScript::get_rendering_drivers_func() {
|
|||
}
|
||||
|
||||
// Clipboard
|
||||
extern "C" EMSCRIPTEN_KEEPALIVE void update_clipboard(const char *p_text) {
|
||||
// Only call set_clipboard from OS (sets local clipboard)
|
||||
DisplayServerJavaScript::get_singleton()->clipboard = p_text;
|
||||
void DisplayServerJavaScript::update_clipboard_callback(const char *p_text) {
|
||||
get_singleton()->clipboard = p_text;
|
||||
}
|
||||
|
||||
void DisplayServerJavaScript::clipboard_set(const String &p_text) {
|
||||
/* clang-format off */
|
||||
int err = EM_ASM_INT({
|
||||
var text = UTF8ToString($0);
|
||||
if (!navigator.clipboard || !navigator.clipboard.writeText)
|
||||
return 1;
|
||||
navigator.clipboard.writeText(text).catch(function(e) {
|
||||
// Setting OS clipboard is only possible from an input callback.
|
||||
console.error("Setting OS clipboard is only possible from an input callback for the HTML5 plafrom. Exception:", e);
|
||||
});
|
||||
return 0;
|
||||
}, p_text.utf8().get_data());
|
||||
/* clang-format on */
|
||||
clipboard = p_text;
|
||||
int err = godot_js_display_clipboard_set(p_text.utf8().get_data());
|
||||
ERR_FAIL_COND_MSG(err, "Clipboard API is not supported.");
|
||||
}
|
||||
|
||||
String DisplayServerJavaScript::clipboard_get() const {
|
||||
/* clang-format off */
|
||||
EM_ASM({
|
||||
try {
|
||||
navigator.clipboard.readText().then(function (result) {
|
||||
ccall('update_clipboard', 'void', ['string'], [result]);
|
||||
}).catch(function (e) {
|
||||
// Fail graciously.
|
||||
});
|
||||
} catch (e) {
|
||||
// Fail graciously.
|
||||
}
|
||||
});
|
||||
/* clang-format on */
|
||||
godot_js_display_clipboard_get(update_clipboard_callback);
|
||||
return clipboard;
|
||||
}
|
||||
|
||||
extern "C" EMSCRIPTEN_KEEPALIVE void send_window_event(int p_notification) {
|
||||
if (p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER || p_notification == DisplayServer::WINDOW_EVENT_MOUSE_EXIT) {
|
||||
cursor_inside_canvas = p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
|
||||
void DisplayServerJavaScript::send_window_event_callback(int p_notification) {
|
||||
DisplayServerJavaScript *ds = get_singleton();
|
||||
if (!ds) {
|
||||
return;
|
||||
}
|
||||
OS_JavaScript *os = OS_JavaScript::get_singleton();
|
||||
if (os->is_finalizing())
|
||||
return; // We don't want events anymore.
|
||||
DisplayServerJavaScript *ds = DisplayServerJavaScript::get_singleton();
|
||||
if (ds && !ds->window_event_callback.is_null()) {
|
||||
if (p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER || p_notification == DisplayServer::WINDOW_EVENT_MOUSE_EXIT) {
|
||||
ds->cursor_inside_canvas = p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
|
||||
}
|
||||
if (!ds->window_event_callback.is_null()) {
|
||||
Variant event = int(p_notification);
|
||||
Variant *eventp = &event;
|
||||
Variant ret;
|
||||
|
|
@ -752,11 +651,7 @@ extern "C" EMSCRIPTEN_KEEPALIVE void send_window_event(int p_notification) {
|
|||
}
|
||||
|
||||
void DisplayServerJavaScript::alert(const String &p_alert, const String &p_title) {
|
||||
/* clang-format off */
|
||||
EM_ASM_({
|
||||
window.alert(UTF8ToString($0));
|
||||
}, p_alert.utf8().get_data());
|
||||
/* clang-format on */
|
||||
godot_js_display_alert(p_alert.utf8().get_data());
|
||||
}
|
||||
|
||||
void DisplayServerJavaScript::set_icon(const Ref<Image> &p_icon) {
|
||||
|
|
@ -787,29 +682,11 @@ void DisplayServerJavaScript::set_icon(const Ref<Image> &p_icon) {
|
|||
png.resize(len);
|
||||
ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
|
||||
|
||||
/* clang-format off */
|
||||
EM_ASM({
|
||||
var PNG_PTR = $0;
|
||||
var PNG_LEN = $1;
|
||||
|
||||
var png = new Blob([HEAPU8.slice(PNG_PTR, PNG_PTR + PNG_LEN)], { type: "image/png" });
|
||||
var url = URL.createObjectURL(png);
|
||||
var link = document.getElementById('-gd-engine-icon');
|
||||
if (link === null) {
|
||||
link = document.createElement('link');
|
||||
link.rel = 'icon';
|
||||
link.id = '-gd-engine-icon';
|
||||
document.head.appendChild(link);
|
||||
}
|
||||
link.href = url;
|
||||
}, png.ptr(), len);
|
||||
/* clang-format on */
|
||||
godot_js_display_window_icon_set(png.ptr(), len);
|
||||
}
|
||||
|
||||
void DisplayServerJavaScript::_dispatch_input_event(const Ref<InputEvent> &p_event) {
|
||||
OS_JavaScript *os = OS_JavaScript::get_singleton();
|
||||
if (os->is_finalizing())
|
||||
return; // We don't want events anymore.
|
||||
|
||||
// Resume audio context after input in case autoplay was denied.
|
||||
os->resume_audio();
|
||||
|
|
@ -831,16 +708,14 @@ DisplayServer *DisplayServerJavaScript::create_func(const String &p_rendering_dr
|
|||
DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
|
||||
r_error = OK; // Always succeeds for now.
|
||||
|
||||
/* clang-format off */
|
||||
swap_cancel_ok = EM_ASM_INT({
|
||||
const win = (['Windows', 'Win64', 'Win32', 'WinCE']);
|
||||
const plat = navigator.platform || "";
|
||||
if (win.indexOf(plat) !== -1) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}) == 1;
|
||||
/* clang-format on */
|
||||
// Ensure the canvas ID.
|
||||
godot_js_config_canvas_id_get(canvas_id, 256);
|
||||
|
||||
// Check if it's windows.
|
||||
swap_cancel_ok = godot_js_display_is_swap_ok_cancel() == 1;
|
||||
|
||||
// Expose method for requesting quit.
|
||||
godot_js_os_request_quit_cb(request_quit_callback);
|
||||
|
||||
RasterizerDummy::make_current(); // TODO GLES2 in Godot 4.0... or webgpu?
|
||||
#if 0
|
||||
|
|
@ -878,10 +753,8 @@ DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_drive
|
|||
video_driver_index = p_video_driver;
|
||||
#endif
|
||||
|
||||
/* clang-format off */
|
||||
window_set_mode(p_mode);
|
||||
if (EM_ASM_INT_V({ return Module['resizeCanvasOnStart'] })) {
|
||||
/* clang-format on */
|
||||
if (godot_js_config_is_resize_on_start()) {
|
||||
window_set_size(p_resolution);
|
||||
}
|
||||
|
||||
|
|
@ -899,8 +772,7 @@ DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_drive
|
|||
result = emscripten_set_##ev##_callback(nullptr, true, &cb); \
|
||||
EM_CHECK(ev)
|
||||
// These callbacks from Emscripten's html5.h suffice to access most
|
||||
// JavaScript APIs. For APIs that are not (sufficiently) exposed, EM_ASM
|
||||
// is used below.
|
||||
// JavaScript APIs.
|
||||
SET_EM_CALLBACK(canvas_id, mousedown, mouse_button_callback)
|
||||
SET_EM_WINDOW_CALLBACK(mousemove, mousemove_callback)
|
||||
SET_EM_WINDOW_CALLBACK(mouseup, mouse_button_callback)
|
||||
|
|
@ -919,42 +791,20 @@ DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_drive
|
|||
#undef SET_EM_CALLBACK
|
||||
#undef EM_CHECK
|
||||
|
||||
/* clang-format off */
|
||||
EM_ASM_ARGS({
|
||||
// Bind native event listeners.
|
||||
// Module.listeners, and Module.drop_handler are defined in native/utils.js
|
||||
const canvas = Module['canvas'];
|
||||
const send_window_event = cwrap('send_window_event', null, ['number']);
|
||||
const notifications = arguments;
|
||||
(['mouseover', 'mouseleave', 'focus', 'blur']).forEach(function(event, index) {
|
||||
Module.listeners.add(canvas, event, send_window_event.bind(null, notifications[index]), true);
|
||||
});
|
||||
// Clipboard
|
||||
const update_clipboard = cwrap('update_clipboard', null, ['string']);
|
||||
Module.listeners.add(window, 'paste', function(evt) {
|
||||
update_clipboard(evt.clipboardData.getData('text'));
|
||||
}, false);
|
||||
// Drag an drop
|
||||
Module.listeners.add(canvas, 'dragover', function(ev) {
|
||||
// Prevent default behavior (which would try to open the file(s))
|
||||
ev.preventDefault();
|
||||
}, false);
|
||||
Module.listeners.add(canvas, 'drop', Module.drop_handler, false);
|
||||
},
|
||||
WINDOW_EVENT_MOUSE_ENTER,
|
||||
WINDOW_EVENT_MOUSE_EXIT,
|
||||
WINDOW_EVENT_FOCUS_IN,
|
||||
WINDOW_EVENT_FOCUS_OUT
|
||||
);
|
||||
/* clang-format on */
|
||||
// For APIs that are not (sufficiently) exposed, a
|
||||
// library is used below (implemented in library_godot_display.js).
|
||||
godot_js_display_notification_cb(&send_window_event_callback,
|
||||
WINDOW_EVENT_MOUSE_ENTER,
|
||||
WINDOW_EVENT_MOUSE_EXIT,
|
||||
WINDOW_EVENT_FOCUS_IN,
|
||||
WINDOW_EVENT_FOCUS_OUT);
|
||||
godot_js_display_paste_cb(update_clipboard_callback);
|
||||
godot_js_display_drop_files_cb(drop_files_js_callback);
|
||||
|
||||
Input::get_singleton()->set_event_dispatch_function(_dispatch_input_event);
|
||||
}
|
||||
|
||||
DisplayServerJavaScript::~DisplayServerJavaScript() {
|
||||
EM_ASM({
|
||||
Module.listeners.clear();
|
||||
});
|
||||
//emscripten_webgl_commit_frame();
|
||||
//emscripten_webgl_destroy_context(webgl_ctx);
|
||||
}
|
||||
|
|
@ -1057,11 +907,7 @@ void DisplayServerJavaScript::window_set_drop_files_callback(const Callable &p_c
|
|||
}
|
||||
|
||||
void DisplayServerJavaScript::window_set_title(const String &p_title, WindowID p_window) {
|
||||
/* clang-format off */
|
||||
EM_ASM_({
|
||||
document.title = UTF8ToString($0);
|
||||
}, p_title.utf8().get_data());
|
||||
/* clang-format on */
|
||||
godot_js_display_window_title_set(p_title.utf8().get_data());
|
||||
}
|
||||
|
||||
int DisplayServerJavaScript::window_get_current_screen(WindowID p_window) const {
|
||||
|
|
@ -1103,9 +949,7 @@ Size2i DisplayServerJavaScript::window_get_min_size(WindowID p_window) const {
|
|||
void DisplayServerJavaScript::window_set_size(const Size2i p_size, WindowID p_window) {
|
||||
last_width = p_size.x;
|
||||
last_height = p_size.y;
|
||||
double scale = EM_ASM_DOUBLE({
|
||||
return window.devicePixelRatio || 1;
|
||||
});
|
||||
double scale = godot_js_display_pixel_ratio_get();
|
||||
emscripten_set_canvas_element_size(canvas_id, p_size.x * scale, p_size.y * scale);
|
||||
emscripten_set_element_css_size(canvas_id, p_size.x, p_size.y);
|
||||
}
|
||||
|
|
@ -1130,7 +974,7 @@ void DisplayServerJavaScript::window_set_mode(WindowMode p_mode, WindowID p_wind
|
|||
emscripten_exit_fullscreen();
|
||||
}
|
||||
window_mode = WINDOW_MODE_WINDOWED;
|
||||
window_set_size(windowed_size);
|
||||
window_set_size(Size2i(last_width, last_height));
|
||||
} break;
|
||||
case WINDOW_MODE_FULLSCREEN: {
|
||||
EmscriptenFullscreenStrategy strategy;
|
||||
|
|
|
|||
|
|
@ -37,18 +37,22 @@
|
|||
#include <emscripten/html5.h>
|
||||
|
||||
class DisplayServerJavaScript : public DisplayServer {
|
||||
//int video_driver_index;
|
||||
|
||||
Vector2 windowed_size;
|
||||
|
||||
private:
|
||||
WindowMode window_mode = WINDOW_MODE_WINDOWED;
|
||||
ObjectID window_attached_instance_id = {};
|
||||
|
||||
Callable window_event_callback;
|
||||
Callable input_event_callback;
|
||||
Callable input_text_callback;
|
||||
Callable drop_files_callback;
|
||||
|
||||
String clipboard;
|
||||
Ref<InputEventKey> deferred_key_event;
|
||||
CursorShape cursor_shape = CURSOR_ARROW;
|
||||
String cursors[CURSOR_MAX];
|
||||
Map<CursorShape, Vector<Variant>> cursors_cache;
|
||||
Point2 touches[32];
|
||||
|
||||
char canvas_id[256] = { 0 };
|
||||
bool cursor_inside_canvas = true;
|
||||
CursorShape cursor_shape = CURSOR_ARROW;
|
||||
Point2i last_click_pos = Point2(-100, -100); // TODO check this again.
|
||||
double last_click_ms = 0;
|
||||
int last_click_button_index = -1;
|
||||
|
|
@ -66,8 +70,6 @@ class DisplayServerJavaScript : public DisplayServer {
|
|||
static void dom2godot_mod(T *emscripten_event_ptr, Ref<InputEventWithModifiers> godot_event);
|
||||
static Ref<InputEventKey> setup_key_event(const EmscriptenKeyboardEvent *emscripten_event);
|
||||
static const char *godot2dom_cursor(DisplayServer::CursorShape p_shape);
|
||||
static void set_css_cursor(const char *p_cursor);
|
||||
bool is_css_cursor_hidden() const;
|
||||
|
||||
// events
|
||||
static EM_BOOL fullscreen_change_callback(int p_event_type, const EmscriptenFullscreenChangeEvent *p_event, void *p_user_data);
|
||||
|
|
@ -92,22 +94,17 @@ class DisplayServerJavaScript : public DisplayServer {
|
|||
|
||||
static void _dispatch_input_event(const Ref<InputEvent> &p_event);
|
||||
|
||||
static void request_quit_callback();
|
||||
static void update_clipboard_callback(const char *p_text);
|
||||
static void send_window_event_callback(int p_notification);
|
||||
static void drop_files_js_callback(char **p_filev, int p_filec);
|
||||
|
||||
protected:
|
||||
int get_current_video_driver() const;
|
||||
|
||||
public:
|
||||
// Override return type to make writing static callbacks less tedious.
|
||||
static DisplayServerJavaScript *get_singleton();
|
||||
static char canvas_id[256];
|
||||
|
||||
WindowMode window_mode = WINDOW_MODE_WINDOWED;
|
||||
|
||||
String clipboard;
|
||||
|
||||
Callable window_event_callback;
|
||||
Callable input_event_callback;
|
||||
Callable input_text_callback;
|
||||
Callable drop_files_callback;
|
||||
|
||||
// utilities
|
||||
bool check_size_force_redraw();
|
||||
|
|
|
|||
|
|
@ -19,3 +19,9 @@ def create_engine_file(env, target, source, externs):
|
|||
if env["use_closure_compiler"]:
|
||||
return env.BuildJS(target, source, JSEXTERNS=externs)
|
||||
return env.Textfile(target, [env.File(s) for s in source])
|
||||
|
||||
|
||||
def add_js_libraries(env, libraries):
|
||||
if "JS_LIBS" not in env:
|
||||
env["JS_LIBS"] = []
|
||||
env.Append(JS_LIBS=env.File(libraries))
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ Function('return this')()['Engine'] = (function() {
|
|||
this.resizeCanvasOnStart = false;
|
||||
this.onExecute = null;
|
||||
this.onExit = null;
|
||||
this.persistentPaths = [];
|
||||
this.persistentPaths = ['/userfs'];
|
||||
};
|
||||
|
||||
Engine.prototype.init = /** @param {string=} basePath */ function(basePath) {
|
||||
|
|
@ -114,18 +114,30 @@ Function('return this')()['Engine'] = (function() {
|
|||
locale = navigator.languages ? navigator.languages[0] : navigator.language;
|
||||
locale = locale.split('.')[0];
|
||||
}
|
||||
me.rtenv['locale'] = locale;
|
||||
me.rtenv['canvas'] = me.canvas;
|
||||
// Emscripten configuration.
|
||||
me.rtenv['thisProgram'] = me.executableName;
|
||||
me.rtenv['resizeCanvasOnStart'] = me.resizeCanvasOnStart;
|
||||
me.rtenv['noExitRuntime'] = true;
|
||||
me.rtenv['onExecute'] = me.onExecute;
|
||||
me.rtenv['onExit'] = function(code) {
|
||||
me.rtenv['deinitFS']();
|
||||
if (me.onExit)
|
||||
me.onExit(code);
|
||||
me.rtenv = null;
|
||||
};
|
||||
// Godot configuration.
|
||||
me.rtenv['initConfig']({
|
||||
'resizeCanvasOnStart': me.resizeCanvasOnStart,
|
||||
'canvas': me.canvas,
|
||||
'locale': locale,
|
||||
'onExecute': function(p_args) {
|
||||
if (me.onExecute) {
|
||||
me.onExecute(p_args);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
},
|
||||
'onExit': function(p_code) {
|
||||
me.rtenv['deinitFS']();
|
||||
if (me.onExit) {
|
||||
me.onExit(p_code);
|
||||
}
|
||||
me.rtenv = null;
|
||||
},
|
||||
});
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
preloader.preloadedFiles.forEach(function(file) {
|
||||
me.rtenv['copyToFS'](file.path, file.buffer);
|
||||
|
|
@ -208,8 +220,6 @@ Function('return this')()['Engine'] = (function() {
|
|||
};
|
||||
|
||||
Engine.prototype.setOnExecute = function(onExecute) {
|
||||
if (this.rtenv)
|
||||
this.rtenv.onExecute = onExecute;
|
||||
this.onExecute = onExecute;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -42,9 +42,8 @@ extern int godot_audio_is_available();
|
|||
extern int godot_audio_init(int p_mix_rate, int p_latency);
|
||||
extern int godot_audio_create_processor(int p_buffer_length, int p_channel_count);
|
||||
|
||||
extern void godot_audio_start(float *r_buffer_ptr);
|
||||
extern void godot_audio_start(float *r_buffer_ptr, void (*p_start)(), void (*p_end)(), void (*p_input)(float p_sample));
|
||||
extern void godot_audio_resume();
|
||||
extern void godot_audio_finish_async();
|
||||
|
||||
extern float godot_audio_get_latency();
|
||||
|
||||
|
|
|
|||
87
platform/javascript/godot_js.h
Normal file
87
platform/javascript/godot_js.h
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/*************************************************************************/
|
||||
/* godot_js.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef GODOT_JS_H
|
||||
#define GODOT_JS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "stddef.h"
|
||||
|
||||
// Config
|
||||
extern void godot_js_config_locale_get(char *p_ptr, int p_ptr_max);
|
||||
extern void godot_js_config_canvas_id_get(char *p_ptr, int p_ptr_max);
|
||||
extern int godot_js_config_is_resize_on_start();
|
||||
|
||||
// OS
|
||||
extern void godot_js_os_finish_async(void (*p_callback)());
|
||||
extern void godot_js_os_request_quit_cb(void (*p_callback)());
|
||||
extern int godot_js_os_fs_is_persistent();
|
||||
extern void godot_js_os_fs_sync(void (*p_callback)());
|
||||
extern int godot_js_os_execute(const char *p_json);
|
||||
extern void godot_js_os_shell_open(const char *p_uri);
|
||||
|
||||
// Display
|
||||
extern double godot_js_display_pixel_ratio_get();
|
||||
extern void godot_js_display_alert(const char *p_text);
|
||||
extern int godot_js_display_touchscreen_is_available();
|
||||
extern int godot_js_display_is_swap_ok_cancel();
|
||||
|
||||
// Display canvas
|
||||
extern void godot_js_display_canvas_focus();
|
||||
extern int godot_js_display_canvas_is_focused();
|
||||
extern void godot_js_display_canvas_bounding_rect_position_get(int32_t *p_x, int32_t *p_y);
|
||||
|
||||
// Display window
|
||||
extern void godot_js_display_window_request_fullscreen();
|
||||
extern void godot_js_display_window_title_set(const char *p_text);
|
||||
extern void godot_js_display_window_icon_set(const uint8_t *p_ptr, int p_len);
|
||||
|
||||
// Display clipboard
|
||||
extern int godot_js_display_clipboard_set(const char *p_text);
|
||||
extern int godot_js_display_clipboard_get(void (*p_callback)(const char *p_text));
|
||||
|
||||
// Display cursor
|
||||
extern void godot_js_display_cursor_set_shape(const char *p_cursor);
|
||||
extern int godot_js_display_cursor_is_hidden();
|
||||
extern void godot_js_display_cursor_set_custom_shape(const char *p_shape, const uint8_t *p_ptr, int p_len, int p_hotspot_x, int p_hotspot_y);
|
||||
extern void godot_js_display_cursor_set_visible(int p_visible);
|
||||
|
||||
// Display listeners
|
||||
extern void godot_js_display_notification_cb(void (*p_callback)(int p_notification), int p_enter, int p_exit, int p_in, int p_out);
|
||||
extern void godot_js_display_paste_cb(void (*p_callback)(const char *p_text));
|
||||
extern void godot_js_display_drop_files_cb(void (*p_callback)(char **p_filev, int p_filec));
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* GODOT_JS_H */
|
||||
|
|
@ -33,95 +33,30 @@
|
|||
#include "api/javascript_eval.h"
|
||||
#include "emscripten.h"
|
||||
|
||||
extern "C" EMSCRIPTEN_KEEPALIVE uint8_t *resize_PackedByteArray_and_open_write(PackedByteArray *p_arr, VectorWriteProxy<uint8_t> *r_write, int p_len) {
|
||||
p_arr->resize(p_len);
|
||||
*r_write = p_arr->write;
|
||||
return p_arr->ptrw();
|
||||
extern "C" {
|
||||
union js_eval_ret {
|
||||
uint32_t b;
|
||||
double d;
|
||||
char *s;
|
||||
};
|
||||
|
||||
extern int godot_js_eval(const char *p_js, int p_use_global_ctx, union js_eval_ret *p_union_ptr, void *p_byte_arr, void *p_byte_arr_write, void *(*p_callback)(void *p_ptr, void *p_ptr2, int p_len));
|
||||
}
|
||||
|
||||
void *resize_PackedByteArray_and_open_write(void *p_arr, void *r_write, int p_len) {
|
||||
PackedByteArray *arr = (PackedByteArray *)p_arr;
|
||||
VectorWriteProxy<uint8_t> *write = (VectorWriteProxy<uint8_t> *)r_write;
|
||||
arr->resize(p_len);
|
||||
*write = arr->write;
|
||||
return arr->ptrw();
|
||||
}
|
||||
|
||||
Variant JavaScript::eval(const String &p_code, bool p_use_global_exec_context) {
|
||||
union {
|
||||
bool b;
|
||||
double d;
|
||||
char *s;
|
||||
} js_data;
|
||||
|
||||
union js_eval_ret js_data;
|
||||
PackedByteArray arr;
|
||||
VectorWriteProxy<uint8_t> arr_write;
|
||||
|
||||
/* clang-format off */
|
||||
Variant::Type return_type = static_cast<Variant::Type>(EM_ASM_INT({
|
||||
|
||||
const CODE = $0;
|
||||
const USE_GLOBAL_EXEC_CONTEXT = $1;
|
||||
const PTR = $2;
|
||||
const BYTEARRAY_PTR = $3;
|
||||
const BYTEARRAY_WRITE_PTR = $4;
|
||||
var eval_ret;
|
||||
try {
|
||||
if (USE_GLOBAL_EXEC_CONTEXT) {
|
||||
// indirect eval call grants global execution context
|
||||
var global_eval = eval;
|
||||
eval_ret = global_eval(UTF8ToString(CODE));
|
||||
} else {
|
||||
eval_ret = eval(UTF8ToString(CODE));
|
||||
}
|
||||
} catch (e) {
|
||||
err(e);
|
||||
eval_ret = null;
|
||||
}
|
||||
|
||||
switch (typeof eval_ret) {
|
||||
|
||||
case 'boolean':
|
||||
setValue(PTR, eval_ret, 'i32');
|
||||
return 1; // BOOL
|
||||
|
||||
case 'number':
|
||||
setValue(PTR, eval_ret, 'double');
|
||||
return 3; // FLOAT
|
||||
|
||||
case 'string':
|
||||
var array_len = lengthBytesUTF8(eval_ret)+1;
|
||||
var array_ptr = _malloc(array_len);
|
||||
try {
|
||||
if (array_ptr===0) {
|
||||
throw new Error('String allocation failed (probably out of memory)');
|
||||
}
|
||||
setValue(PTR, array_ptr , '*');
|
||||
stringToUTF8(eval_ret, array_ptr, array_len);
|
||||
return 4; // STRING
|
||||
} catch (e) {
|
||||
if (array_ptr!==0) {
|
||||
_free(array_ptr)
|
||||
}
|
||||
err(e);
|
||||
// fall through
|
||||
}
|
||||
break;
|
||||
|
||||
case 'object':
|
||||
if (eval_ret === null) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (ArrayBuffer.isView(eval_ret) && !(eval_ret instanceof Uint8Array)) {
|
||||
eval_ret = new Uint8Array(eval_ret.buffer);
|
||||
}
|
||||
else if (eval_ret instanceof ArrayBuffer) {
|
||||
eval_ret = new Uint8Array(eval_ret);
|
||||
}
|
||||
if (eval_ret instanceof Uint8Array) {
|
||||
var bytes_ptr = ccall('resize_PackedByteArray_and_open_write', 'number', ['number', 'number' ,'number'], [BYTEARRAY_PTR, BYTEARRAY_WRITE_PTR, eval_ret.length]);
|
||||
HEAPU8.set(eval_ret, bytes_ptr);
|
||||
return 20; // PACKED_BYTE_ARRAY
|
||||
}
|
||||
break;
|
||||
}
|
||||
return 0; // NIL
|
||||
|
||||
}, p_code.utf8().get_data(), p_use_global_exec_context, &js_data, &arr, &arr_write));
|
||||
/* clang-format on */
|
||||
Variant::Type return_type = static_cast<Variant::Type>(godot_js_eval(p_code.utf8().get_data(), p_use_global_exec_context, &js_data, &arr, &arr_write, resize_PackedByteArray_and_open_write));
|
||||
|
||||
switch (return_type) {
|
||||
case Variant::BOOL:
|
||||
|
|
@ -130,9 +65,7 @@ Variant JavaScript::eval(const String &p_code, bool p_use_global_exec_context) {
|
|||
return js_data.d;
|
||||
case Variant::STRING: {
|
||||
String str = String::utf8(js_data.s);
|
||||
/* clang-format off */
|
||||
EM_ASM_({ _free($0); }, js_data.s);
|
||||
/* clang-format on */
|
||||
free(js_data.s); // Must free the string allocated in JS.
|
||||
return str;
|
||||
}
|
||||
case Variant::PACKED_BYTE_ARRAY:
|
||||
|
|
|
|||
|
|
@ -36,20 +36,11 @@
|
|||
#include <emscripten/emscripten.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "godot_js.h"
|
||||
|
||||
static OS_JavaScript *os = nullptr;
|
||||
static uint64_t target_ticks = 0;
|
||||
|
||||
extern "C" EMSCRIPTEN_KEEPALIVE void _request_quit_callback(char *p_filev[], int p_filec) {
|
||||
DisplayServerJavaScript *ds = DisplayServerJavaScript::get_singleton();
|
||||
if (ds) {
|
||||
Variant event = int(DisplayServer::WINDOW_EVENT_CLOSE_REQUEST);
|
||||
Variant *eventp = &event;
|
||||
Variant ret;
|
||||
Callable::CallError ce;
|
||||
ds->window_event_callback.call((const Variant **)&eventp, 1, ret, ce);
|
||||
}
|
||||
}
|
||||
|
||||
void exit_callback() {
|
||||
emscripten_cancel_main_loop(); // After this, we can exit!
|
||||
Main::cleanup();
|
||||
|
|
@ -59,6 +50,10 @@ void exit_callback() {
|
|||
emscripten_force_exit(exit_code); // No matter that we call cancel_main_loop, regular "exit" will not work, forcing.
|
||||
}
|
||||
|
||||
void cleanup_after_sync() {
|
||||
emscripten_set_main_loop(exit_callback, -1, false);
|
||||
}
|
||||
|
||||
void main_loop_callback() {
|
||||
uint64_t current_ticks = os->get_ticks_usec();
|
||||
|
||||
|
|
@ -74,68 +69,14 @@ void main_loop_callback() {
|
|||
target_ticks += (uint64_t)(1000000 / target_fps);
|
||||
}
|
||||
if (os->main_loop_iterate()) {
|
||||
emscripten_cancel_main_loop(); // Cancel current loop and wait for finalize_async.
|
||||
/* clang-format off */
|
||||
EM_ASM({
|
||||
// This will contain the list of operations that need to complete before cleanup.
|
||||
Module.async_finish = [
|
||||
// Always contains at least one async promise, to avoid firing immediately if nothing is added.
|
||||
new Promise(function(accept, reject) {
|
||||
setTimeout(accept, 0);
|
||||
})
|
||||
];
|
||||
});
|
||||
/* clang-format on */
|
||||
os->get_main_loop()->finish();
|
||||
os->finalize_async(); // Will add all the async finish functions.
|
||||
/* clang-format off */
|
||||
EM_ASM({
|
||||
Promise.all(Module.async_finish).then(function() {
|
||||
Module.async_finish = [];
|
||||
return new Promise(function(accept, reject) {
|
||||
if (!Module.idbfs) {
|
||||
accept();
|
||||
return;
|
||||
}
|
||||
FS.syncfs(function(error) {
|
||||
if (error) {
|
||||
err('Failed to save IDB file system: ' + error.message);
|
||||
}
|
||||
accept();
|
||||
});
|
||||
});
|
||||
}).then(function() {
|
||||
ccall("cleanup_after_sync", null, []);
|
||||
});
|
||||
});
|
||||
/* clang-format on */
|
||||
emscripten_cancel_main_loop(); // Cancel current loop and wait for cleanup_after_sync.
|
||||
godot_js_os_finish_async(cleanup_after_sync);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" EMSCRIPTEN_KEEPALIVE void cleanup_after_sync() {
|
||||
emscripten_set_main_loop(exit_callback, -1, false);
|
||||
}
|
||||
|
||||
/// When calling main, it is assumed FS is setup and synced.
|
||||
int main(int argc, char *argv[]) {
|
||||
// Configure locale.
|
||||
char locale_ptr[16];
|
||||
/* clang-format off */
|
||||
EM_ASM({
|
||||
stringToUTF8(Module['locale'], $0, 16);
|
||||
}, locale_ptr);
|
||||
/* clang-format on */
|
||||
setenv("LANG", locale_ptr, true);
|
||||
|
||||
// Ensure the canvas ID.
|
||||
/* clang-format off */
|
||||
EM_ASM({
|
||||
stringToUTF8("#" + Module['canvas'].id, $0, 255);
|
||||
}, DisplayServerJavaScript::canvas_id);
|
||||
/* clang-format on */
|
||||
|
||||
os = new OS_JavaScript();
|
||||
os->set_idb_available((bool)EM_ASM_INT({ return Module.idbfs }));
|
||||
|
||||
// We must override main when testing is enabled
|
||||
TEST_MAIN_OVERRIDE
|
||||
|
|
@ -147,14 +88,6 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
Main::start();
|
||||
os->get_main_loop()->init();
|
||||
// Expose method for requesting quit.
|
||||
/* clang-format off */
|
||||
EM_ASM({
|
||||
Module['request_quit'] = function() {
|
||||
ccall("_request_quit_callback", null, []);
|
||||
};
|
||||
});
|
||||
/* clang-format on */
|
||||
emscripten_set_main_loop(main_loop_callback, -1, false);
|
||||
// Immediately run the first iteration.
|
||||
// We are inside an animation frame, we want to immediately draw on the newly setup canvas.
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@
|
|||
/*************************************************************************/
|
||||
var GodotAudio = {
|
||||
|
||||
$GodotAudio__deps: ['$GodotOS'],
|
||||
$GodotAudio: {
|
||||
|
||||
ctx: null,
|
||||
|
|
@ -49,6 +50,27 @@ var GodotAudio = {
|
|||
sampleRate: mix_rate,
|
||||
// latencyHint: latency / 1000 // Do not specify, leave 'interactive' for good performance.
|
||||
});
|
||||
GodotOS.atexit(function(accept, reject) {
|
||||
if (!GodotAudio.ctx) {
|
||||
accept();
|
||||
return;
|
||||
}
|
||||
if (GodotAudio.script) {
|
||||
GodotAudio.script.disconnect();
|
||||
GodotAudio.script.onaudioprocess = null;
|
||||
GodotAudio.script = null;
|
||||
}
|
||||
if (GodotAudio.input) {
|
||||
GodotAudio.input.disconnect();
|
||||
GodotAudio.input = null;
|
||||
}
|
||||
GodotAudio.ctx.close().then(function() {
|
||||
accept();
|
||||
}).catch(function(e) {
|
||||
accept();
|
||||
});
|
||||
GodotAudio.ctx = null;
|
||||
});
|
||||
return GodotAudio.ctx.destination.channelCount;
|
||||
},
|
||||
|
||||
|
|
@ -58,10 +80,10 @@ var GodotAudio = {
|
|||
return GodotAudio.script.bufferSize;
|
||||
},
|
||||
|
||||
godot_audio_start: function(buffer_ptr) {
|
||||
var audioDriverProcessStart = cwrap('audio_driver_process_start');
|
||||
var audioDriverProcessEnd = cwrap('audio_driver_process_end');
|
||||
var audioDriverProcessCapture = cwrap('audio_driver_process_capture', null, ['number']);
|
||||
godot_audio_start: function(buffer_ptr, p_process_start, p_process_end, p_process_capture) {
|
||||
const audioDriverProcessStart = GodotOS.get_func(p_process_start);
|
||||
const audioDriverProcessEnd = GodotOS.get_func(p_process_end);
|
||||
const audioDriverProcessCapture = GodotOS.get_func(p_process_capture);
|
||||
GodotAudio.script.onaudioprocess = function(audioProcessingEvent) {
|
||||
audioDriverProcessStart();
|
||||
|
||||
|
|
@ -96,29 +118,6 @@ var GodotAudio = {
|
|||
}
|
||||
},
|
||||
|
||||
godot_audio_finish_async: function() {
|
||||
Module.async_finish.push(new Promise(function(accept, reject) {
|
||||
if (!GodotAudio.ctx) {
|
||||
setTimeout(accept, 0);
|
||||
} else {
|
||||
if (GodotAudio.script) {
|
||||
GodotAudio.script.disconnect();
|
||||
GodotAudio.script = null;
|
||||
}
|
||||
if (GodotAudio.input) {
|
||||
GodotAudio.input.disconnect();
|
||||
GodotAudio.input = null;
|
||||
}
|
||||
GodotAudio.ctx.close().then(function() {
|
||||
accept();
|
||||
}).catch(function(e) {
|
||||
accept();
|
||||
});
|
||||
GodotAudio.ctx = null;
|
||||
}
|
||||
}));
|
||||
},
|
||||
|
||||
godot_audio_get_latency__proxy: 'sync',
|
||||
godot_audio_get_latency: function() {
|
||||
var latency = 0;
|
||||
|
|
@ -159,9 +158,9 @@ var GodotAudio = {
|
|||
godot_audio_capture_stop__proxy: 'sync',
|
||||
godot_audio_capture_stop: function() {
|
||||
if (GodotAudio.input) {
|
||||
const tracks = GodotAudio.input.mediaStream.getTracks();
|
||||
const tracks = GodotAudio.input['mediaStream']['getTracks']();
|
||||
for (var i = 0; i < tracks.length; i++) {
|
||||
tracks[i].stop();
|
||||
tracks[i]['stop']();
|
||||
}
|
||||
GodotAudio.input.disconnect();
|
||||
GodotAudio.input = null;
|
||||
|
|
|
|||
478
platform/javascript/native/library_godot_display.js
Normal file
478
platform/javascript/native/library_godot_display.js
Normal file
|
|
@ -0,0 +1,478 @@
|
|||
/*************************************************************************/
|
||||
/* library_godot_display.js */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
|
||||
/*
|
||||
* Display Server listeners.
|
||||
* Keeps track of registered event listeners so it can remove them on shutdown.
|
||||
*/
|
||||
const GodotDisplayListeners = {
|
||||
$GodotDisplayListeners__postset: 'GodotOS.atexit(function(resolve, reject) { GodotDisplayListeners.clear(); resolve(); });',
|
||||
$GodotDisplayListeners: {
|
||||
handlers: [],
|
||||
|
||||
has: function(target, event, method, capture) {
|
||||
return GodotDisplayListeners.handlers.findIndex(function(e) {
|
||||
return e.target === target && e.event === event && e.method === method && e.capture == capture;
|
||||
}) !== -1;
|
||||
},
|
||||
|
||||
add: function(target, event, method, capture) {
|
||||
if (GodotDisplayListeners.has(target, event, method, capture)) {
|
||||
return;
|
||||
}
|
||||
function Handler(target, event, method, capture) {
|
||||
this.target = target;
|
||||
this.event = event;
|
||||
this.method = method;
|
||||
this.capture = capture;
|
||||
};
|
||||
GodotDisplayListeners.handlers.push(new Handler(target, event, method, capture));
|
||||
target.addEventListener(event, method, capture);
|
||||
},
|
||||
|
||||
clear: function() {
|
||||
GodotDisplayListeners.handlers.forEach(function(h) {
|
||||
h.target.removeEventListener(h.event, h.method, h.capture);
|
||||
});
|
||||
GodotDisplayListeners.handlers.length = 0;
|
||||
},
|
||||
},
|
||||
};
|
||||
mergeInto(LibraryManager.library, GodotDisplayListeners);
|
||||
|
||||
/*
|
||||
* Drag and drop handler.
|
||||
* This is pretty big, but basically detect dropped files on GodotConfig.canvas,
|
||||
* process them one by one (recursively for directories), and copies them to
|
||||
* the temporary FS path '/tmp/drop-[random]/' so it can be emitted as a godot
|
||||
* event (that requires a string array of paths).
|
||||
*
|
||||
* NOTE: The temporary files are removed after the callback. This means that
|
||||
* deferred callbacks won't be able to access the files.
|
||||
*/
|
||||
const GodotDisplayDragDrop = {
|
||||
|
||||
$GodotDisplayDragDrop__deps: ['$FS', '$GodotFS'],
|
||||
$GodotDisplayDragDrop: {
|
||||
promises: [],
|
||||
pending_files: [],
|
||||
|
||||
add_entry: function(entry) {
|
||||
if (entry.isDirectory) {
|
||||
GodotDisplayDragDrop.add_dir(entry);
|
||||
} else if (entry.isFile) {
|
||||
GodotDisplayDragDrop.add_file(entry);
|
||||
} else {
|
||||
console.error("Unrecognized entry...", entry);
|
||||
}
|
||||
},
|
||||
|
||||
add_dir: function(entry) {
|
||||
GodotDisplayDragDrop.promises.push(new Promise(function(resolve, reject) {
|
||||
const reader = entry.createReader();
|
||||
reader.readEntries(function(entries) {
|
||||
for (let i = 0; i < entries.length; i++) {
|
||||
GodotDisplayDragDrop.add_entry(entries[i]);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
}));
|
||||
},
|
||||
|
||||
add_file: function(entry) {
|
||||
GodotDisplayDragDrop.promises.push(new Promise(function(resolve, reject) {
|
||||
entry.file(function(file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function() {
|
||||
const f = {
|
||||
"path": file.relativePath || file.webkitRelativePath,
|
||||
"name": file.name,
|
||||
"type": file.type,
|
||||
"size": file.size,
|
||||
"data": reader.result
|
||||
};
|
||||
if (!f['path']) {
|
||||
f['path'] = f['name'];
|
||||
}
|
||||
GodotDisplayDragDrop.pending_files.push(f);
|
||||
resolve()
|
||||
};
|
||||
reader.onerror = function() {
|
||||
console.log("Error reading file");
|
||||
reject();
|
||||
}
|
||||
reader.readAsArrayBuffer(file);
|
||||
}, function(err) {
|
||||
console.log("Error!");
|
||||
reject();
|
||||
});
|
||||
}));
|
||||
},
|
||||
|
||||
process: function(resolve, reject) {
|
||||
if (GodotDisplayDragDrop.promises.length == 0) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
GodotDisplayDragDrop.promises.pop().then(function() {
|
||||
setTimeout(function() {
|
||||
GodotDisplayDragDrop.process(resolve, reject);
|
||||
}, 0);
|
||||
});
|
||||
},
|
||||
|
||||
_process_event: function(ev, callback) {
|
||||
ev.preventDefault();
|
||||
if (ev.dataTransfer.items) {
|
||||
// Use DataTransferItemList interface to access the file(s)
|
||||
for (let i = 0; i < ev.dataTransfer.items.length; i++) {
|
||||
const item = ev.dataTransfer.items[i];
|
||||
let entry = null;
|
||||
if ("getAsEntry" in item) {
|
||||
entry = item.getAsEntry();
|
||||
} else if ("webkitGetAsEntry" in item) {
|
||||
entry = item.webkitGetAsEntry();
|
||||
}
|
||||
if (entry) {
|
||||
GodotDisplayDragDrop.add_entry(entry);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.error("File upload not supported");
|
||||
}
|
||||
new Promise(GodotDisplayDragDrop.process).then(function() {
|
||||
const DROP = "/tmp/drop-" + parseInt(Math.random() * Math.pow(2, 31)) + "/";
|
||||
const drops = [];
|
||||
const files = [];
|
||||
FS.mkdir(DROP);
|
||||
GodotDisplayDragDrop.pending_files.forEach((elem) => {
|
||||
const path = elem['path'];
|
||||
GodotFS.copy_to_fs(DROP + path, elem['data']);
|
||||
let idx = path.indexOf("/");
|
||||
if (idx == -1) {
|
||||
// Root file
|
||||
drops.push(DROP + path);
|
||||
} else {
|
||||
// Subdir
|
||||
const sub = path.substr(0, idx);
|
||||
idx = sub.indexOf("/");
|
||||
if (idx < 0 && drops.indexOf(DROP + sub) == -1) {
|
||||
drops.push(DROP + sub);
|
||||
}
|
||||
}
|
||||
files.push(DROP + path);
|
||||
});
|
||||
GodotDisplayDragDrop.promises = [];
|
||||
GodotDisplayDragDrop.pending_files = [];
|
||||
callback(drops);
|
||||
const dirs = [DROP.substr(0, DROP.length -1)];
|
||||
// Remove temporary files
|
||||
files.forEach(function (file) {
|
||||
FS.unlink(file);
|
||||
let dir = file.replace(DROP, "");
|
||||
let idx = dir.lastIndexOf("/");
|
||||
while (idx > 0) {
|
||||
dir = dir.substr(0, idx);
|
||||
if (dirs.indexOf(DROP + dir) == -1) {
|
||||
dirs.push(DROP + dir);
|
||||
}
|
||||
idx = dir.lastIndexOf("/");
|
||||
}
|
||||
});
|
||||
// Remove dirs.
|
||||
dirs.sort(function(a, b) {
|
||||
const al = (a.match(/\//g) || []).length;
|
||||
const bl = (b.match(/\//g) || []).length;
|
||||
if (al > bl)
|
||||
return -1;
|
||||
else if (al < bl)
|
||||
return 1;
|
||||
return 0;
|
||||
}).forEach(function(dir) {
|
||||
FS.rmdir(dir);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
handler: function(callback) {
|
||||
return function(ev) {
|
||||
GodotDisplayDragDrop._process_event(ev, callback);
|
||||
};
|
||||
},
|
||||
},
|
||||
};
|
||||
mergeInto(LibraryManager.library, GodotDisplayDragDrop);
|
||||
|
||||
/*
|
||||
* Display server cursor helper.
|
||||
* Keeps track of cursor status and custom shapes.
|
||||
*/
|
||||
const GodotDisplayCursor = {
|
||||
$GodotDisplayCursor__postset: 'GodotOS.atexit(function(resolve, reject) { GodotDisplayCursor.clear(); resolve(); });',
|
||||
$GodotDisplayCursor__deps: ['$GodotConfig', '$GodotOS'],
|
||||
$GodotDisplayCursor: {
|
||||
shape: 'auto',
|
||||
visible: true,
|
||||
cursors: {},
|
||||
set_style: function(style) {
|
||||
GodotConfig.canvas.style.cursor = style;
|
||||
},
|
||||
set_shape: function(shape) {
|
||||
GodotDisplayCursor.shape = shape;
|
||||
let css = shape;
|
||||
if (shape in GodotDisplayCursor.cursors) {
|
||||
const c = GodotDisplayCursor.cursors[shape];
|
||||
css = 'url("' + c.url + '") ' + c.x + ' ' + c.y + ', auto';
|
||||
}
|
||||
if (GodotDisplayCursor.visible) {
|
||||
GodotDisplayCursor.set_style(css);
|
||||
}
|
||||
},
|
||||
clear: function() {
|
||||
GodotDisplayCursor.set_style('');
|
||||
GodotDisplayCursor.shape = 'auto';
|
||||
GodotDisplayCursor.visible = true;
|
||||
Object.keys(GodotDisplayCursor.cursors).forEach(function(key) {
|
||||
URL.revokeObjectURL(GodotDisplayCursor.cursors[key]);
|
||||
delete GodotDisplayCursor.cursors[key];
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
mergeInto(LibraryManager.library, GodotDisplayCursor);
|
||||
|
||||
/**
|
||||
* Display server interface.
|
||||
*
|
||||
* Exposes all the functions needed by DisplayServer implementation.
|
||||
*/
|
||||
const GodotDisplay = {
|
||||
$GodotDisplay__deps: ['$GodotConfig', '$GodotOS', '$GodotDisplayCursor', '$GodotDisplayListeners', '$GodotDisplayDragDrop'],
|
||||
$GodotDisplay: {
|
||||
window_icon: '',
|
||||
},
|
||||
|
||||
godot_js_display_is_swap_ok_cancel: function() {
|
||||
const win = (['Windows', 'Win64', 'Win32', 'WinCE']);
|
||||
const plat = navigator.platform || "";
|
||||
if (win.indexOf(plat) !== -1) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
|
||||
godot_js_display_alert: function(p_text) {
|
||||
window.alert(UTF8ToString(p_text));
|
||||
},
|
||||
|
||||
godot_js_display_pixel_ratio_get: function() {
|
||||
return window.devicePixelRatio || 1;
|
||||
},
|
||||
|
||||
/*
|
||||
* Canvas
|
||||
*/
|
||||
godot_js_display_canvas_focus: function() {
|
||||
GodotConfig.canvas.focus();
|
||||
},
|
||||
|
||||
godot_js_display_canvas_is_focused: function() {
|
||||
return document.activeElement == GodotConfig.canvas;
|
||||
},
|
||||
|
||||
godot_js_display_canvas_bounding_rect_position_get: function(r_x, r_y) {
|
||||
const brect = GodotConfig.canvas.getBoundingClientRect();
|
||||
setValue(r_x, brect.x, 'i32');
|
||||
setValue(r_y, brect.y, 'i32');
|
||||
},
|
||||
|
||||
/*
|
||||
* Touchscreen
|
||||
*/
|
||||
godot_js_display_touchscreen_is_available: function() {
|
||||
return 'ontouchstart' in window;
|
||||
},
|
||||
|
||||
/*
|
||||
* Clipboard
|
||||
*/
|
||||
godot_js_display_clipboard_set: function(p_text) {
|
||||
const text = UTF8ToString(p_text);
|
||||
if (!navigator.clipboard || !navigator.clipboard.writeText) {
|
||||
return 1;
|
||||
}
|
||||
navigator.clipboard.writeText(text).catch(function(e) {
|
||||
// Setting OS clipboard is only possible from an input callback.
|
||||
console.error("Setting OS clipboard is only possible from an input callback for the HTML5 plafrom. Exception:", e);
|
||||
});
|
||||
return 0;
|
||||
},
|
||||
|
||||
godot_js_display_clipboard_get_deps: ['$GodotOS'],
|
||||
godot_js_display_clipboard_get: function(callback) {
|
||||
const func = GodotOS.get_func(callback);
|
||||
try {
|
||||
navigator.clipboard.readText().then(function (result) {
|
||||
const ptr = allocate(intArrayFromString(result), ALLOC_NORMAL);
|
||||
func(ptr);
|
||||
_free(ptr);
|
||||
}).catch(function (e) {
|
||||
// Fail graciously.
|
||||
});
|
||||
} catch (e) {
|
||||
// Fail graciously.
|
||||
}
|
||||
},
|
||||
|
||||
/*
|
||||
* Window
|
||||
*/
|
||||
godot_js_display_window_request_fullscreen: function() {
|
||||
const canvas = GodotConfig.canvas;
|
||||
(canvas.requestFullscreen || canvas.msRequestFullscreen ||
|
||||
canvas.mozRequestFullScreen || canvas.mozRequestFullscreen ||
|
||||
canvas.webkitRequestFullscreen
|
||||
).call(canvas);
|
||||
},
|
||||
|
||||
godot_js_display_window_title_set: function(p_data) {
|
||||
document.title = UTF8ToString(p_data);
|
||||
},
|
||||
|
||||
godot_js_display_window_icon_set: function(p_ptr, p_len) {
|
||||
let link = document.getElementById('-gd-engine-icon');
|
||||
if (link === null) {
|
||||
link = document.createElement('link');
|
||||
link.rel = 'icon';
|
||||
link.id = '-gd-engine-icon';
|
||||
document.head.appendChild(link);
|
||||
}
|
||||
const old_icon = GodotDisplay.window_icon;
|
||||
const png = new Blob([GodotOS.heapCopy(HEAPU8, p_ptr, p_len)], { type: "image/png" });
|
||||
GodotDisplay.window_icon = URL.createObjectURL(png);
|
||||
link.href = GodotDisplay.window_icon;
|
||||
if (old_icon) {
|
||||
URL.revokeObjectURL(old_icon);
|
||||
}
|
||||
},
|
||||
|
||||
/*
|
||||
* Cursor
|
||||
*/
|
||||
godot_js_display_cursor_set_visible: function(p_visible) {
|
||||
const visible = p_visible != 0;
|
||||
if (visible == GodotDisplayCursor.visible) {
|
||||
return;
|
||||
}
|
||||
GodotDisplayCursor.visible = visible;
|
||||
if (visible) {
|
||||
GodotDisplayCursor.set_shape(GodotDisplayCursor.shape);
|
||||
} else {
|
||||
GodotDisplayCursor.set_style('none');
|
||||
}
|
||||
},
|
||||
|
||||
godot_js_display_cursor_is_hidden: function() {
|
||||
return !GodotDisplayCursor.visible;
|
||||
},
|
||||
|
||||
godot_js_display_cursor_set_shape: function(p_string) {
|
||||
GodotDisplayCursor.set_shape(UTF8ToString(p_string));
|
||||
},
|
||||
|
||||
godot_js_display_cursor_set_custom_shape: function(p_shape, p_ptr, p_len, p_hotspot_x, p_hotspot_y) {
|
||||
const shape = UTF8ToString(p_shape);
|
||||
const old_shape = GodotDisplayCursor.cursors[shape];
|
||||
if (p_len > 0) {
|
||||
const png = new Blob([GodotOS.heapCopy(HEAPU8, p_ptr, p_len)], { type: 'image/png' });
|
||||
const url = URL.createObjectURL(png);
|
||||
GodotDisplayCursor.cursors[shape] = {
|
||||
url: url,
|
||||
x: p_hotspot_x,
|
||||
y: p_hotspot_y,
|
||||
};
|
||||
} else {
|
||||
delete GodotDisplayCursor.cursors[shape];
|
||||
}
|
||||
if (shape == GodotDisplayCursor.shape) {
|
||||
GodotDisplayCursor.set_shape(GodotDisplayCursor.shape);
|
||||
}
|
||||
if (old_shape) {
|
||||
URL.revokeObjectURL(old_shape.url);
|
||||
}
|
||||
},
|
||||
|
||||
/*
|
||||
* Listeners
|
||||
*/
|
||||
godot_js_display_notification_cb: function(callback, p_enter, p_exit, p_in, p_out) {
|
||||
const canvas = GodotConfig.canvas;
|
||||
const func = GodotOS.get_func(callback);
|
||||
const notif = [p_enter, p_exit, p_in, p_out];
|
||||
['mouseover', 'mouseleave', 'focus', 'blur'].forEach(function(evt_name, idx) {
|
||||
GodotDisplayListeners.add(canvas, evt_name, function() {
|
||||
func.bind(null, notif[idx]);
|
||||
}, true);
|
||||
});
|
||||
},
|
||||
|
||||
godot_js_display_paste_cb: function(callback) {
|
||||
const func = GodotOS.get_func(callback);
|
||||
GodotDisplayListeners.add(window, 'paste', function(evt) {
|
||||
const text = evt.clipboardData.getData('text');
|
||||
const ptr = allocate(intArrayFromString(text), ALLOC_NORMAL);
|
||||
func(ptr);
|
||||
_free(ptr);
|
||||
}, false);
|
||||
},
|
||||
|
||||
godot_js_display_drop_files_cb: function(callback) {
|
||||
const func = GodotOS.get_func(callback)
|
||||
const dropFiles = function(files) {
|
||||
const args = files || [];
|
||||
if (!args.length) {
|
||||
return;
|
||||
}
|
||||
const argc = args.length;
|
||||
const argv = GodotOS.allocStringArray(args);
|
||||
func(argv, argc);
|
||||
GodotOS.freeStringArray(argv, argc);
|
||||
};
|
||||
const canvas = GodotConfig.canvas;
|
||||
GodotDisplayListeners.add(canvas, 'dragover', function(ev) {
|
||||
// Prevent default behavior (which would try to open the file(s))
|
||||
ev.preventDefault();
|
||||
}, false);
|
||||
GodotDisplayListeners.add(canvas, 'drop', GodotDisplayDragDrop.handler(dropFiles));
|
||||
},
|
||||
};
|
||||
|
||||
autoAddDeps(GodotDisplay, '$GodotDisplay');
|
||||
mergeInto(LibraryManager.library, GodotDisplay);
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*************************************************************************/
|
||||
/* id_handler.js */
|
||||
/* library_godot_editor_tools.js */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
|
|
@ -28,36 +28,30 @@
|
|||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
var IDHandler = /** @constructor */ function() {
|
||||
const GodotEditorTools = {
|
||||
|
||||
var ids = {};
|
||||
var size = 0;
|
||||
|
||||
this.has = function(id) {
|
||||
return ids.hasOwnProperty(id);
|
||||
}
|
||||
|
||||
this.add = function(obj) {
|
||||
size += 1;
|
||||
var id = crypto.getRandomValues(new Int32Array(32))[0];
|
||||
ids[id] = obj;
|
||||
return id;
|
||||
}
|
||||
|
||||
this.get = function(id) {
|
||||
return ids[id];
|
||||
}
|
||||
|
||||
this.remove = function(id) {
|
||||
size -= 1;
|
||||
delete ids[id];
|
||||
}
|
||||
|
||||
this.size = function() {
|
||||
return size;
|
||||
}
|
||||
|
||||
this.ids = ids;
|
||||
godot_js_editor_download_file__deps: ['$FS'],
|
||||
godot_js_editor_download_file: function(p_path, p_name, p_mime) {
|
||||
const path = UTF8ToString(p_path);
|
||||
const name = UTF8ToString(p_name);
|
||||
const mime = UTF8ToString(p_mime);
|
||||
const size = FS.stat(path)['size'];
|
||||
const buf = new Uint8Array(size);
|
||||
const fd = FS.open(path, 'r');
|
||||
FS.read(fd, buf, 0, size);
|
||||
FS.close(fd);
|
||||
FS.unlink(path);
|
||||
const blob = new Blob([buf], { type: mime });
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = name;
|
||||
a.style.display = 'none';
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
window.URL.revokeObjectURL(url);
|
||||
},
|
||||
};
|
||||
|
||||
Module.IDHandler = new IDHandler;
|
||||
mergeInto(LibraryManager.library, GodotEditorTools);
|
||||
87
platform/javascript/native/library_godot_eval.js
Normal file
87
platform/javascript/native/library_godot_eval.js
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/*************************************************************************/
|
||||
/* library_godot_eval.js */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
|
||||
const GodotEval = {
|
||||
|
||||
godot_js_eval__deps: ['$GodotOS'],
|
||||
godot_js_eval: function(p_js, p_use_global_ctx, p_union_ptr, p_byte_arr, p_byte_arr_write, p_callback) {
|
||||
const js_code = UTF8ToString(p_js);
|
||||
let eval_ret = null;
|
||||
try {
|
||||
if (p_use_global_ctx) {
|
||||
// indirect eval call grants global execution context
|
||||
const global_eval = eval;
|
||||
eval_ret = global_eval(js_code);
|
||||
} else {
|
||||
eval_ret = eval(js_code);
|
||||
}
|
||||
} catch (e) {
|
||||
err(e);
|
||||
}
|
||||
|
||||
switch (typeof eval_ret) {
|
||||
|
||||
case 'boolean':
|
||||
setValue(p_union_ptr, eval_ret, 'i32');
|
||||
return 1; // BOOL
|
||||
|
||||
case 'number':
|
||||
setValue(p_union_ptr, eval_ret, 'double');
|
||||
return 3; // REAL
|
||||
|
||||
case 'string':
|
||||
let array_ptr = GodotOS.allocString(eval_ret);
|
||||
setValue(p_union_ptr, array_ptr , '*');
|
||||
return 4; // STRING
|
||||
|
||||
case 'object':
|
||||
if (eval_ret === null) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (ArrayBuffer.isView(eval_ret) && !(eval_ret instanceof Uint8Array)) {
|
||||
eval_ret = new Uint8Array(eval_ret.buffer);
|
||||
}
|
||||
else if (eval_ret instanceof ArrayBuffer) {
|
||||
eval_ret = new Uint8Array(eval_ret);
|
||||
}
|
||||
if (eval_ret instanceof Uint8Array) {
|
||||
const func = GodotOS.get_func(p_callback);
|
||||
const bytes_ptr = func(p_byte_arr, p_byte_arr_write, eval_ret.length);
|
||||
HEAPU8.set(eval_ret, bytes_ptr);
|
||||
return 20; // POOL_BYTE_ARRAY
|
||||
}
|
||||
break;
|
||||
}
|
||||
return 0; // NIL
|
||||
},
|
||||
}
|
||||
|
||||
mergeInto(LibraryManager.library, GodotEval);
|
||||
313
platform/javascript/native/library_godot_os.js
Normal file
313
platform/javascript/native/library_godot_os.js
Normal file
|
|
@ -0,0 +1,313 @@
|
|||
/*************************************************************************/
|
||||
/* library_godot_os.js */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
|
||||
const IDHandler = {
|
||||
$IDHandler: {
|
||||
_last_id: 0,
|
||||
_references: {},
|
||||
|
||||
get: function(p_id) {
|
||||
return IDHandler._references[p_id];
|
||||
},
|
||||
|
||||
add: function(p_data) {
|
||||
const id = ++IDHandler._last_id;
|
||||
IDHandler._references[id] = p_data;
|
||||
return id;
|
||||
},
|
||||
|
||||
remove: function(p_id) {
|
||||
delete IDHandler._references[p_id];
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
autoAddDeps(IDHandler, "$IDHandler");
|
||||
mergeInto(LibraryManager.library, IDHandler);
|
||||
|
||||
const GodotConfig = {
|
||||
|
||||
$GodotConfig__postset: 'Module["initConfig"] = GodotConfig.init_config;',
|
||||
$GodotConfig: {
|
||||
canvas: null,
|
||||
locale: "en",
|
||||
resize_on_start: false,
|
||||
on_execute: null,
|
||||
|
||||
init_config: function(p_opts) {
|
||||
GodotConfig.resize_on_start = p_opts['resizeCanvasOnStart'] ? true : false;
|
||||
GodotConfig.canvas = p_opts['canvas'];
|
||||
GodotConfig.locale = p_opts['locale'] || GodotConfig.locale;
|
||||
GodotConfig.on_execute = p_opts['onExecute'];
|
||||
// This is called by emscripten, even if undocumented.
|
||||
Module['onExit'] = p_opts['onExit'];
|
||||
},
|
||||
},
|
||||
|
||||
godot_js_config_canvas_id_get: function(p_ptr, p_ptr_max) {
|
||||
stringToUTF8('#' + GodotConfig.canvas.id, p_ptr, p_ptr_max);
|
||||
},
|
||||
|
||||
godot_js_config_locale_get: function(p_ptr, p_ptr_max) {
|
||||
stringToUTF8(GodotConfig.locale, p_ptr, p_ptr_max);
|
||||
},
|
||||
|
||||
godot_js_config_is_resize_on_start: function() {
|
||||
return GodotConfig.resize_on_start ? 1 : 0;
|
||||
},
|
||||
};
|
||||
|
||||
autoAddDeps(GodotConfig, '$GodotConfig');
|
||||
mergeInto(LibraryManager.library, GodotConfig);
|
||||
|
||||
const GodotFS = {
|
||||
$GodotFS__deps: ['$FS', '$IDBFS'],
|
||||
$GodotFS__postset: [
|
||||
'Module["initFS"] = GodotFS.init;',
|
||||
'Module["deinitFS"] = GodotFS.deinit;',
|
||||
'Module["copyToFS"] = GodotFS.copy_to_fs;',
|
||||
].join(''),
|
||||
$GodotFS: {
|
||||
_idbfs: false,
|
||||
_syncing: false,
|
||||
_mount_points: [],
|
||||
|
||||
is_persistent: function() {
|
||||
return GodotFS._idbfs ? 1 : 0;
|
||||
},
|
||||
|
||||
// Initialize godot file system, setting up persistent paths.
|
||||
// Returns a promise that resolves when the FS is ready.
|
||||
// We keep track of mount_points, so that we can properly close the IDBFS
|
||||
// since emscripten is not doing it by itself. (emscripten GH#12516).
|
||||
init: function(persistentPaths) {
|
||||
GodotFS._idbfs = false;
|
||||
if (!Array.isArray(persistentPaths)) {
|
||||
return Promise.reject(new Error('Persistent paths must be an array'));
|
||||
}
|
||||
if (!persistentPaths.length) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
GodotFS._mount_points = persistentPaths.slice();
|
||||
|
||||
function createRecursive(dir) {
|
||||
try {
|
||||
FS.stat(dir);
|
||||
} catch (e) {
|
||||
if (e.errno !== ERRNO_CODES.ENOENT) {
|
||||
throw e;
|
||||
}
|
||||
FS.mkdirTree(dir);
|
||||
}
|
||||
}
|
||||
|
||||
GodotFS._mount_points.forEach(function(path) {
|
||||
createRecursive(path);
|
||||
FS.mount(IDBFS, {}, path);
|
||||
});
|
||||
return new Promise(function(resolve, reject) {
|
||||
FS.syncfs(true, function(err) {
|
||||
if (err) {
|
||||
GodotFS._mount_points = [];
|
||||
GodotFS._idbfs = false;
|
||||
console.log("IndexedDB not available: " + err.message);
|
||||
} else {
|
||||
GodotFS._idbfs = true;
|
||||
}
|
||||
resolve(err);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
// Deinit godot file system, making sure to unmount file systems, and close IDBFS(s).
|
||||
deinit: function() {
|
||||
GodotFS._mount_points.forEach(function(path) {
|
||||
try {
|
||||
FS.unmount(path);
|
||||
} catch (e) {
|
||||
console.log("Already unmounted", e);
|
||||
}
|
||||
if (GodotFS._idbfs && IDBFS.dbs[path]) {
|
||||
IDBFS.dbs[path].close();
|
||||
delete IDBFS.dbs[path];
|
||||
}
|
||||
});
|
||||
GodotFS._mount_points = [];
|
||||
GodotFS._idbfs = false;
|
||||
GodotFS._syncing = false;
|
||||
},
|
||||
|
||||
sync: function() {
|
||||
if (GodotFS._syncing) {
|
||||
err('Already syncing!');
|
||||
return Promise.resolve();
|
||||
}
|
||||
GodotFS._syncing = true;
|
||||
return new Promise(function (resolve, reject) {
|
||||
FS.syncfs(false, function(error) {
|
||||
if (error) {
|
||||
err('Failed to save IDB file system: ' + error.message);
|
||||
}
|
||||
GodotFS._syncing = false;
|
||||
resolve(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
// Copies a buffer to the internal file system. Creating directories recursively.
|
||||
copy_to_fs: function(path, buffer) {
|
||||
const idx = path.lastIndexOf("/");
|
||||
let dir = "/";
|
||||
if (idx > 0) {
|
||||
dir = path.slice(0, idx);
|
||||
}
|
||||
try {
|
||||
FS.stat(dir);
|
||||
} catch (e) {
|
||||
if (e.errno !== ERRNO_CODES.ENOENT) {
|
||||
throw e;
|
||||
}
|
||||
FS.mkdirTree(dir);
|
||||
}
|
||||
FS.writeFile(path, new Uint8Array(buffer), {'flags': 'wx+'});
|
||||
},
|
||||
},
|
||||
};
|
||||
mergeInto(LibraryManager.library, GodotFS);
|
||||
|
||||
const GodotOS = {
|
||||
$GodotOS__deps: ['$GodotFS'],
|
||||
$GodotOS__postset: [
|
||||
'Module["request_quit"] = function() { GodotOS.request_quit() };',
|
||||
'GodotOS._fs_sync_promise = Promise.resolve();',
|
||||
].join(''),
|
||||
$GodotOS: {
|
||||
|
||||
request_quit: function() {},
|
||||
_async_cbs: [],
|
||||
_fs_sync_promise: null,
|
||||
|
||||
get_func: function(ptr) {
|
||||
return wasmTable.get(ptr);
|
||||
},
|
||||
|
||||
atexit: function(p_promise_cb) {
|
||||
GodotOS._async_cbs.push(p_promise_cb);
|
||||
},
|
||||
|
||||
finish_async: function(callback) {
|
||||
GodotOS._fs_sync_promise.then(function(err) {
|
||||
const promises = [];
|
||||
GodotOS._async_cbs.forEach(function(cb) {
|
||||
promises.push(new Promise(cb));
|
||||
});
|
||||
return Promise.all(promises);
|
||||
}).then(function() {
|
||||
return GodotFS.sync(); // Final FS sync.
|
||||
}).then(function(err) {
|
||||
// Always deferred.
|
||||
setTimeout(function() {
|
||||
callback();
|
||||
}, 0);
|
||||
});
|
||||
},
|
||||
|
||||
allocString: function(p_str) {
|
||||
const length = lengthBytesUTF8(p_str)+1;
|
||||
const c_str = _malloc(length);
|
||||
stringToUTF8(p_str, c_str, length);
|
||||
return c_str;
|
||||
},
|
||||
|
||||
allocStringArray: function(strings) {
|
||||
const size = strings.length;
|
||||
const c_ptr = _malloc(size * 4);
|
||||
for (let i = 0; i < size; i++) {
|
||||
HEAP32[(c_ptr >> 2) + i] = GodotOS.allocString(strings[i]);
|
||||
}
|
||||
return c_ptr;
|
||||
},
|
||||
|
||||
freeStringArray: function(c_ptr, size) {
|
||||
for (let i = 0; i < size; i++) {
|
||||
_free(HEAP32[(c_ptr >> 2) + i]);
|
||||
}
|
||||
_free(c_ptr);
|
||||
},
|
||||
|
||||
heapSub: function(heap, ptr, size) {
|
||||
const bytes = heap.BYTES_PER_ELEMENT;
|
||||
return heap.subarray(ptr / bytes, ptr / bytes + size);
|
||||
},
|
||||
|
||||
heapCopy: function(heap, ptr, size) {
|
||||
const bytes = heap.BYTES_PER_ELEMENT;
|
||||
return heap.slice(ptr / bytes, ptr / bytes + size);
|
||||
},
|
||||
},
|
||||
|
||||
godot_js_os_finish_async: function(p_callback) {
|
||||
const func = GodotOS.get_func(p_callback);
|
||||
GodotOS.finish_async(func);
|
||||
},
|
||||
|
||||
godot_js_os_request_quit_cb: function(p_callback) {
|
||||
GodotOS.request_quit = GodotOS.get_func(p_callback);
|
||||
},
|
||||
|
||||
godot_js_os_fs_is_persistent: function() {
|
||||
return GodotFS.is_persistent();
|
||||
},
|
||||
|
||||
godot_js_os_fs_sync: function(callback) {
|
||||
const func = GodotOS.get_func(callback);
|
||||
GodotOS._fs_sync_promise = GodotFS.sync();
|
||||
GodotOS._fs_sync_promise.then(function(err) {
|
||||
func();
|
||||
});
|
||||
},
|
||||
|
||||
godot_js_os_execute: function(p_json) {
|
||||
const json_args = UTF8ToString(p_json);
|
||||
const args = JSON.parse(json_args);
|
||||
if (GodotConfig.on_execute) {
|
||||
GodotConfig.on_execute(args);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
},
|
||||
|
||||
godot_js_os_shell_open: function(p_uri) {
|
||||
window.open(UTF8ToString(p_uri), '_blank');
|
||||
},
|
||||
};
|
||||
|
||||
autoAddDeps(GodotOS, '$GodotOS');
|
||||
mergeInto(LibraryManager.library, GodotOS);
|
||||
|
|
@ -1,292 +0,0 @@
|
|||
/*************************************************************************/
|
||||
/* utils.js */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
|
||||
Module['initFS'] = function(persistentPaths) {
|
||||
Module.mount_points = ['/userfs'].concat(persistentPaths);
|
||||
|
||||
function createRecursive(dir) {
|
||||
try {
|
||||
FS.stat(dir);
|
||||
} catch (e) {
|
||||
if (e.errno !== ERRNO_CODES.ENOENT) {
|
||||
throw e;
|
||||
}
|
||||
FS.mkdirTree(dir);
|
||||
}
|
||||
}
|
||||
|
||||
Module.mount_points.forEach(function(path) {
|
||||
createRecursive(path);
|
||||
FS.mount(IDBFS, {}, path);
|
||||
});
|
||||
return new Promise(function(resolve, reject) {
|
||||
FS.syncfs(true, function(err) {
|
||||
if (err) {
|
||||
Module.mount_points = [];
|
||||
Module.idbfs = false;
|
||||
console.log("IndexedDB not available: " + err.message);
|
||||
} else {
|
||||
Module.idbfs = true;
|
||||
}
|
||||
resolve(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Module['deinitFS'] = function() {
|
||||
Module.mount_points.forEach(function(path) {
|
||||
try {
|
||||
FS.unmount(path);
|
||||
} catch (e) {
|
||||
console.log("Already unmounted", e);
|
||||
}
|
||||
if (Module.idbfs && IDBFS.dbs[path]) {
|
||||
IDBFS.dbs[path].close();
|
||||
delete IDBFS.dbs[path];
|
||||
}
|
||||
});
|
||||
Module.mount_points = [];
|
||||
};
|
||||
|
||||
Module['copyToFS'] = function(path, buffer) {
|
||||
var p = path.lastIndexOf("/");
|
||||
var dir = "/";
|
||||
if (p > 0) {
|
||||
dir = path.slice(0, path.lastIndexOf("/"));
|
||||
}
|
||||
try {
|
||||
FS.stat(dir);
|
||||
} catch (e) {
|
||||
if (e.errno !== ERRNO_CODES.ENOENT) {
|
||||
throw e;
|
||||
}
|
||||
FS.mkdirTree(dir);
|
||||
}
|
||||
// With memory growth, canOwn should be false.
|
||||
FS.writeFile(path, new Uint8Array(buffer), {'flags': 'wx+'});
|
||||
}
|
||||
|
||||
Module.drop_handler = (function() {
|
||||
var upload = [];
|
||||
var uploadPromises = [];
|
||||
var uploadCallback = null;
|
||||
|
||||
function readFilePromise(entry, path) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
entry.file(function(file) {
|
||||
var reader = new FileReader();
|
||||
reader.onload = function() {
|
||||
var f = {
|
||||
"path": file.relativePath || file.webkitRelativePath,
|
||||
"name": file.name,
|
||||
"type": file.type,
|
||||
"size": file.size,
|
||||
"data": reader.result
|
||||
};
|
||||
if (!f['path'])
|
||||
f['path'] = f['name'];
|
||||
upload.push(f);
|
||||
resolve()
|
||||
};
|
||||
reader.onerror = function() {
|
||||
console.log("Error reading file");
|
||||
reject();
|
||||
}
|
||||
|
||||
reader.readAsArrayBuffer(file);
|
||||
|
||||
}, function(err) {
|
||||
console.log("Error!");
|
||||
reject();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function readDirectoryPromise(entry) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var reader = entry.createReader();
|
||||
reader.readEntries(function(entries) {
|
||||
for (var i = 0; i < entries.length; i++) {
|
||||
var ent = entries[i];
|
||||
if (ent.isDirectory) {
|
||||
uploadPromises.push(readDirectoryPromise(ent));
|
||||
} else if (ent.isFile) {
|
||||
uploadPromises.push(readFilePromise(ent));
|
||||
}
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function processUploadsPromises(resolve, reject) {
|
||||
if (uploadPromises.length == 0) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
uploadPromises.pop().then(function() {
|
||||
setTimeout(function() {
|
||||
processUploadsPromises(resolve, reject);
|
||||
//processUploadsPromises.bind(null, resolve, reject)
|
||||
}, 0);
|
||||
});
|
||||
}
|
||||
|
||||
function dropFiles(files) {
|
||||
var args = files || [];
|
||||
var argc = args.length;
|
||||
var argv = stackAlloc((argc + 1) * 4);
|
||||
for (var i = 0; i < argc; i++) {
|
||||
HEAP32[(argv >> 2) + i] = allocateUTF8OnStack(args[i]);
|
||||
}
|
||||
HEAP32[(argv >> 2) + argc] = 0;
|
||||
// Defined in display_server_javascript.cpp
|
||||
ccall('_drop_files_callback', 'void', ['number', 'number'], [argv, argc]);
|
||||
}
|
||||
|
||||
return function(ev) {
|
||||
ev.preventDefault();
|
||||
if (ev.dataTransfer.items) {
|
||||
// Use DataTransferItemList interface to access the file(s)
|
||||
for (var i = 0; i < ev.dataTransfer.items.length; i++) {
|
||||
const item = ev.dataTransfer.items[i];
|
||||
var entry = null;
|
||||
if ("getAsEntry" in item) {
|
||||
entry = item.getAsEntry();
|
||||
} else if ("webkitGetAsEntry" in item) {
|
||||
entry = item.webkitGetAsEntry();
|
||||
}
|
||||
if (!entry) {
|
||||
console.error("File upload not supported");
|
||||
} else if (entry.isDirectory) {
|
||||
uploadPromises.push(readDirectoryPromise(entry));
|
||||
} else if (entry.isFile) {
|
||||
uploadPromises.push(readFilePromise(entry));
|
||||
} else {
|
||||
console.error("Unrecognized entry...", entry);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.error("File upload not supported");
|
||||
}
|
||||
uploadCallback = new Promise(processUploadsPromises).then(function() {
|
||||
const DROP = "/tmp/drop-" + parseInt(Math.random() * Math.pow(2, 31)) + "/";
|
||||
var drops = [];
|
||||
var files = [];
|
||||
upload.forEach((elem) => {
|
||||
var path = elem['path'];
|
||||
Module['copyToFS'](DROP + path, elem['data']);
|
||||
var idx = path.indexOf("/");
|
||||
if (idx == -1) {
|
||||
// Root file
|
||||
drops.push(DROP + path);
|
||||
} else {
|
||||
// Subdir
|
||||
var sub = path.substr(0, idx);
|
||||
idx = sub.indexOf("/");
|
||||
if (idx < 0 && drops.indexOf(DROP + sub) == -1) {
|
||||
drops.push(DROP + sub);
|
||||
}
|
||||
}
|
||||
files.push(DROP + path);
|
||||
});
|
||||
uploadPromises = [];
|
||||
upload = [];
|
||||
dropFiles(drops);
|
||||
var dirs = [DROP.substr(0, DROP.length -1)];
|
||||
files.forEach(function (file) {
|
||||
FS.unlink(file);
|
||||
var dir = file.replace(DROP, "");
|
||||
var idx = dir.lastIndexOf("/");
|
||||
while (idx > 0) {
|
||||
dir = dir.substr(0, idx);
|
||||
if (dirs.indexOf(DROP + dir) == -1) {
|
||||
dirs.push(DROP + dir);
|
||||
}
|
||||
idx = dir.lastIndexOf("/");
|
||||
}
|
||||
});
|
||||
// Remove dirs.
|
||||
dirs = dirs.sort(function(a, b) {
|
||||
var al = (a.match(/\//g) || []).length;
|
||||
var bl = (b.match(/\//g) || []).length;
|
||||
if (al > bl)
|
||||
return -1;
|
||||
else if (al < bl)
|
||||
return 1;
|
||||
return 0;
|
||||
});
|
||||
dirs.forEach(function(dir) {
|
||||
FS.rmdir(dir);
|
||||
});
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
function EventHandlers() {
|
||||
function Handler(target, event, method, capture) {
|
||||
this.target = target;
|
||||
this.event = event;
|
||||
this.method = method;
|
||||
this.capture = capture;
|
||||
}
|
||||
|
||||
var listeners = [];
|
||||
|
||||
function has(target, event, method, capture) {
|
||||
return listeners.findIndex(function(e) {
|
||||
return e.target === target && e.event === event && e.method === method && e.capture == capture;
|
||||
}) !== -1;
|
||||
}
|
||||
|
||||
this.add = function(target, event, method, capture) {
|
||||
if (has(target, event, method, capture)) {
|
||||
return;
|
||||
}
|
||||
listeners.push(new Handler(target, event, method, capture));
|
||||
target.addEventListener(event, method, capture);
|
||||
};
|
||||
|
||||
this.remove = function(target, event, method, capture) {
|
||||
if (!has(target, event, method, capture)) {
|
||||
return;
|
||||
}
|
||||
target.removeEventListener(event, method, capture);
|
||||
};
|
||||
|
||||
this.clear = function() {
|
||||
listeners.forEach(function(h) {
|
||||
h.target.removeEventListener(h.event, h.method, h.capture);
|
||||
});
|
||||
listeners.length = 0;
|
||||
};
|
||||
}
|
||||
|
||||
Module.listeners = new EventHandlers();
|
||||
|
|
@ -46,6 +46,8 @@
|
|||
#include <emscripten.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "godot_js.h"
|
||||
|
||||
// Lifecycle
|
||||
void OS_JavaScript::initialize() {
|
||||
OS_Unix::initialize_core();
|
||||
|
|
@ -72,24 +74,15 @@ MainLoop *OS_JavaScript::get_main_loop() const {
|
|||
return main_loop;
|
||||
}
|
||||
|
||||
extern "C" EMSCRIPTEN_KEEPALIVE void _idb_synced() {
|
||||
OS_JavaScript::get_singleton()->idb_is_syncing = false;
|
||||
void OS_JavaScript::fs_sync_callback() {
|
||||
get_singleton()->idb_is_syncing = false;
|
||||
}
|
||||
|
||||
bool OS_JavaScript::main_loop_iterate() {
|
||||
if (is_userfs_persistent() && idb_needs_sync && !idb_is_syncing) {
|
||||
idb_is_syncing = true;
|
||||
idb_needs_sync = false;
|
||||
/* clang-format off */
|
||||
EM_ASM({
|
||||
FS.syncfs(function(error) {
|
||||
if (error) {
|
||||
err('Failed to save IDB file system: ' + error.message);
|
||||
}
|
||||
ccall("_idb_synced", 'void', [], []);
|
||||
});
|
||||
});
|
||||
/* clang-format on */
|
||||
godot_js_os_fs_sync(&fs_sync_callback);
|
||||
}
|
||||
|
||||
DisplayServer::get_singleton()->process_events();
|
||||
|
|
@ -104,13 +97,6 @@ void OS_JavaScript::delete_main_loop() {
|
|||
main_loop = nullptr;
|
||||
}
|
||||
|
||||
void OS_JavaScript::finalize_async() {
|
||||
finalizing = true;
|
||||
if (audio_driver_javascript) {
|
||||
audio_driver_javascript->finish_async();
|
||||
}
|
||||
}
|
||||
|
||||
void OS_JavaScript::finalize() {
|
||||
delete_main_loop();
|
||||
if (audio_driver_javascript) {
|
||||
|
|
@ -127,17 +113,7 @@ Error OS_JavaScript::execute(const String &p_path, const List<String> &p_argumen
|
|||
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 */
|
||||
int failed = godot_js_os_execute(json_args.utf8().get_data());
|
||||
ERR_FAIL_COND_V_MSG(failed, ERR_UNAVAILABLE, "OS::execute() must be implemented in JavaScript via 'engine.setOnExecute' if required.");
|
||||
return OK;
|
||||
}
|
||||
|
|
@ -168,11 +144,7 @@ String OS_JavaScript::get_executable_path() const {
|
|||
|
||||
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 */
|
||||
godot_js_os_shell_open(p_uri.utf8().get_data());
|
||||
return OK;
|
||||
}
|
||||
|
||||
|
|
@ -211,10 +183,6 @@ void OS_JavaScript::file_access_close_callback(const String &p_file, int p_flags
|
|||
}
|
||||
}
|
||||
|
||||
void OS_JavaScript::set_idb_available(bool p_idb_available) {
|
||||
idb_available = p_idb_available;
|
||||
}
|
||||
|
||||
bool OS_JavaScript::is_userfs_persistent() const {
|
||||
return idb_available;
|
||||
}
|
||||
|
|
@ -227,11 +195,17 @@ void OS_JavaScript::initialize_joypads() {
|
|||
}
|
||||
|
||||
OS_JavaScript::OS_JavaScript() {
|
||||
char locale_ptr[16];
|
||||
godot_js_config_locale_get(locale_ptr, 16);
|
||||
setenv("LANG", locale_ptr, true);
|
||||
|
||||
if (AudioDriverJavaScript::is_available()) {
|
||||
audio_driver_javascript = memnew(AudioDriverJavaScript);
|
||||
AudioDriverManager::add_driver(audio_driver_javascript);
|
||||
}
|
||||
|
||||
idb_available = godot_js_os_fs_is_persistent();
|
||||
|
||||
Vector<Logger *> loggers;
|
||||
loggers.push_back(memnew(StdLogger));
|
||||
_set_logger(memnew(CompositeLogger(loggers)));
|
||||
|
|
|
|||
|
|
@ -42,13 +42,14 @@ class OS_JavaScript : public OS_Unix {
|
|||
MainLoop *main_loop = nullptr;
|
||||
AudioDriverJavaScript *audio_driver_javascript = nullptr;
|
||||
|
||||
bool finalizing = false;
|
||||
bool idb_is_syncing = false;
|
||||
bool idb_available = false;
|
||||
bool idb_needs_sync = false;
|
||||
|
||||
static void main_loop_callback();
|
||||
|
||||
static void file_access_close_callback(const String &p_file, int p_flags);
|
||||
static void fs_sync_callback();
|
||||
|
||||
protected:
|
||||
void initialize() override;
|
||||
|
|
@ -61,15 +62,12 @@ protected:
|
|||
bool _check_internal_feature_support(const String &p_feature) override;
|
||||
|
||||
public:
|
||||
bool idb_is_syncing = false;
|
||||
|
||||
// Override return type to make writing static callbacks less tedious.
|
||||
static OS_JavaScript *get_singleton();
|
||||
|
||||
void initialize_joypads() override;
|
||||
|
||||
MainLoop *get_main_loop() const override;
|
||||
void finalize_async();
|
||||
bool main_loop_iterate();
|
||||
|
||||
Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking = true, ProcessID *r_child_id = nullptr, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr) override;
|
||||
|
|
@ -88,11 +86,9 @@ public:
|
|||
String get_data_path() const override;
|
||||
String get_user_data_dir() const override;
|
||||
|
||||
void set_idb_available(bool p_idb_available);
|
||||
bool is_userfs_persistent() const override;
|
||||
|
||||
void resume_audio();
|
||||
bool is_finalizing() { return finalizing; }
|
||||
|
||||
OS_JavaScript();
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue