2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* os_javascript.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2018-01-01 14:40:08 +01:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "os_javascript.h"
|
2017-01-16 19:19:45 +01:00
|
|
|
|
2017-11-13 21:46:57 +01:00
|
|
|
#include "core/engine.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "core/io/file_access_buffered_fa.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "dom_keys.h"
|
|
|
|
#include "drivers/gles3/rasterizer_gles3.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "drivers/unix/dir_access_unix.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "drivers/unix/file_access_unix.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "main/main.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "servers/visual/visual_server_raster.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-16 19:19:45 +01:00
|
|
|
#include <emscripten.h>
|
2017-03-05 16:44:50 +01:00
|
|
|
#include <stdlib.h>
|
2017-01-16 19:19:45 +01:00
|
|
|
|
2017-04-24 21:41:39 +02:00
|
|
|
#define DOM_BUTTON_LEFT 0
|
|
|
|
#define DOM_BUTTON_MIDDLE 1
|
|
|
|
#define DOM_BUTTON_RIGHT 2
|
2017-04-21 05:01:31 +02:00
|
|
|
|
|
|
|
template <typename T>
|
2017-06-19 15:43:46 +02:00
|
|
|
static void dom2godot_mod(T emscripten_event_ptr, Ref<InputEventWithModifiers> godot_event) {
|
|
|
|
|
|
|
|
godot_event->set_shift(emscripten_event_ptr->shiftKey);
|
|
|
|
godot_event->set_alt(emscripten_event_ptr->altKey);
|
|
|
|
godot_event->set_control(emscripten_event_ptr->ctrlKey);
|
|
|
|
godot_event->set_metakey(emscripten_event_ptr->metaKey);
|
2017-04-21 05:01:31 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
int OS_JavaScript::get_video_driver_count() const {
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2017-01-16 19:19:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const char *OS_JavaScript::get_video_driver_name(int p_driver) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-31 03:46:30 +01:00
|
|
|
return "GLES3";
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int OS_JavaScript::get_audio_driver_count() const {
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const char *OS_JavaScript::get_audio_driver_name(int p_driver) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return "JavaScript";
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::initialize_core() {
|
|
|
|
|
|
|
|
OS_Unix::initialize_core();
|
|
|
|
FileAccess::make_default<FileAccessBufferedFA<FileAccessUnix> >(FileAccess::ACCESS_RESOURCES);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void OS_JavaScript::set_opengl_extensions(const char *p_gl_extensions) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!p_gl_extensions);
|
2017-03-05 16:44:50 +01:00
|
|
|
gl_extensions = p_gl_extensions;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2016-11-26 13:13:16 +01:00
|
|
|
static EM_BOOL _browser_resize_callback(int event_type, const EmscriptenUiEvent *ui_event, void *user_data) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_RESIZE, false);
|
2016-11-26 13:13:16 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
OS_JavaScript *os = static_cast<OS_JavaScript *>(user_data);
|
2017-07-25 20:55:44 +02:00
|
|
|
// The order of the fullscreen change event and the window size change
|
|
|
|
// event varies, even within just one browser, so defer handling
|
|
|
|
os->request_canvas_size_adjustment();
|
2016-11-26 13:13:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static EM_BOOL _fullscreen_change_callback(int event_type, const EmscriptenFullscreenChangeEvent *event, void *user_data) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_FULLSCREENCHANGE, false);
|
2016-11-26 13:13:16 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
OS_JavaScript *os = static_cast<OS_JavaScript *>(user_data);
|
2016-11-26 13:13:16 +01:00
|
|
|
String id = String::utf8(event->id);
|
|
|
|
// empty id is canvas
|
2017-03-05 16:44:50 +01:00
|
|
|
if (id.empty() || id == "canvas") {
|
2016-11-26 13:13:16 +01:00
|
|
|
|
|
|
|
OS::VideoMode vm = os->get_video_mode();
|
|
|
|
// this event property is the only reliable information on
|
|
|
|
// browser fullscreen state
|
|
|
|
vm.fullscreen = event->isFullscreen;
|
2017-07-25 20:55:44 +02:00
|
|
|
os->set_video_mode(vm);
|
|
|
|
os->request_canvas_size_adjustment();
|
2016-11-26 13:13:16 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:41:39 +02:00
|
|
|
static InputDefault *_input;
|
2017-04-21 05:01:31 +02:00
|
|
|
|
2017-07-22 15:43:05 +02:00
|
|
|
static bool is_canvas_focused() {
|
|
|
|
|
|
|
|
/* clang-format off */
|
|
|
|
return EM_ASM_INT_V(
|
|
|
|
return document.activeElement == Module.canvas;
|
|
|
|
);
|
|
|
|
/* clang-format on */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void focus_canvas() {
|
|
|
|
|
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM(
|
|
|
|
Module.canvas.focus();
|
|
|
|
);
|
|
|
|
/* clang-format on */
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool _cursor_inside_canvas = true;
|
|
|
|
|
|
|
|
static bool is_cursor_inside_canvas() {
|
|
|
|
|
|
|
|
return _cursor_inside_canvas;
|
|
|
|
}
|
|
|
|
|
2017-04-21 05:01:31 +02:00
|
|
|
static EM_BOOL _mousebutton_callback(int event_type, const EmscriptenMouseEvent *mouse_event, void *user_data) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_MOUSEDOWN && event_type != EMSCRIPTEN_EVENT_MOUSEUP, false);
|
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
Ref<InputEventMouseButton> ev;
|
|
|
|
ev.instance();
|
|
|
|
ev->set_pressed(event_type == EMSCRIPTEN_EVENT_MOUSEDOWN);
|
|
|
|
ev->set_position(Point2(mouse_event->canvasX, mouse_event->canvasY));
|
|
|
|
ev->set_global_position(ev->get_position());
|
|
|
|
dom2godot_mod(mouse_event, ev);
|
2017-04-21 05:01:31 +02:00
|
|
|
|
|
|
|
switch (mouse_event->button) {
|
2017-06-19 15:43:46 +02:00
|
|
|
case DOM_BUTTON_LEFT: ev->set_button_index(BUTTON_LEFT); break;
|
|
|
|
case DOM_BUTTON_MIDDLE: ev->set_button_index(BUTTON_MIDDLE); break;
|
|
|
|
case DOM_BUTTON_RIGHT: ev->set_button_index(BUTTON_RIGHT); break;
|
2017-04-21 05:01:31 +02:00
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
int mask = _input->get_mouse_button_mask();
|
2017-11-13 16:14:08 +01:00
|
|
|
int button_flag = 1 << (ev->get_button_index() - 1);
|
2017-07-22 15:43:05 +02:00
|
|
|
if (ev->is_pressed()) {
|
|
|
|
// since the event is consumed, focus manually
|
|
|
|
if (!is_canvas_focused()) {
|
|
|
|
focus_canvas();
|
|
|
|
}
|
2017-11-13 16:14:08 +01:00
|
|
|
mask |= button_flag;
|
|
|
|
} else if (mask & button_flag) {
|
|
|
|
mask &= ~button_flag;
|
2017-07-22 15:43:05 +02:00
|
|
|
} else {
|
|
|
|
// release event, but press was outside the canvas, so ignore
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-19 19:15:51 +02:00
|
|
|
ev->set_button_mask(mask);
|
2017-04-21 05:01:31 +02:00
|
|
|
|
|
|
|
_input->parse_input_event(ev);
|
2017-07-22 15:43:05 +02:00
|
|
|
// prevent selection dragging
|
2017-04-21 05:01:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static EM_BOOL _mousemove_callback(int event_type, const EmscriptenMouseEvent *mouse_event, void *user_data) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_MOUSEMOVE, false);
|
2017-07-22 15:43:05 +02:00
|
|
|
OS_JavaScript *os = static_cast<OS_JavaScript *>(user_data);
|
|
|
|
int input_mask = _input->get_mouse_button_mask();
|
|
|
|
Point2 pos = Point2(mouse_event->canvasX, mouse_event->canvasY);
|
|
|
|
// outside the canvas, only read mouse movement if dragging started inside
|
|
|
|
// the canvas; imitating desktop app behaviour
|
|
|
|
if (!is_cursor_inside_canvas() && !input_mask)
|
|
|
|
return false;
|
2017-04-21 05:01:31 +02:00
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
Ref<InputEventMouseMotion> ev;
|
|
|
|
ev.instance();
|
|
|
|
dom2godot_mod(mouse_event, ev);
|
2017-09-19 19:15:51 +02:00
|
|
|
ev->set_button_mask(input_mask);
|
2017-04-21 05:01:31 +02:00
|
|
|
|
2017-07-22 15:43:05 +02:00
|
|
|
ev->set_position(pos);
|
2017-06-19 15:43:46 +02:00
|
|
|
ev->set_global_position(ev->get_position());
|
2017-04-21 05:01:31 +02:00
|
|
|
|
2017-11-28 23:30:57 +01:00
|
|
|
ev->set_relative(ev->get_position() - _input->get_mouse_position());
|
2017-06-19 15:43:46 +02:00
|
|
|
_input->set_mouse_position(ev->get_position());
|
|
|
|
ev->set_speed(_input->get_last_mouse_speed());
|
2017-04-21 05:01:31 +02:00
|
|
|
|
|
|
|
_input->parse_input_event(ev);
|
2017-07-22 15:43:05 +02:00
|
|
|
// don't suppress mouseover/leave events
|
|
|
|
return false;
|
2017-04-21 05:01:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static EM_BOOL _wheel_callback(int event_type, const EmscriptenWheelEvent *wheel_event, void *user_data) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_WHEEL, false);
|
2017-07-22 15:43:05 +02:00
|
|
|
if (!is_canvas_focused()) {
|
|
|
|
if (is_cursor_inside_canvas()) {
|
|
|
|
focus_canvas();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-04-21 05:01:31 +02:00
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
Ref<InputEventMouseButton> ev;
|
|
|
|
ev.instance();
|
2017-09-19 19:15:51 +02:00
|
|
|
ev->set_button_mask(_input->get_mouse_button_mask());
|
2017-06-19 15:43:46 +02:00
|
|
|
ev->set_position(_input->get_mouse_position());
|
|
|
|
ev->set_global_position(ev->get_position());
|
|
|
|
|
|
|
|
ev->set_shift(_input->is_key_pressed(KEY_SHIFT));
|
|
|
|
ev->set_alt(_input->is_key_pressed(KEY_ALT));
|
|
|
|
ev->set_control(_input->is_key_pressed(KEY_CONTROL));
|
|
|
|
ev->set_metakey(_input->is_key_pressed(KEY_META));
|
2017-04-21 05:01:31 +02:00
|
|
|
|
|
|
|
if (wheel_event->deltaY < 0)
|
2017-06-19 15:43:46 +02:00
|
|
|
ev->set_button_index(BUTTON_WHEEL_UP);
|
2017-04-21 05:01:31 +02:00
|
|
|
else if (wheel_event->deltaY > 0)
|
2017-06-19 15:43:46 +02:00
|
|
|
ev->set_button_index(BUTTON_WHEEL_DOWN);
|
2017-04-21 05:01:31 +02:00
|
|
|
else if (wheel_event->deltaX > 0)
|
2017-06-19 15:43:46 +02:00
|
|
|
ev->set_button_index(BUTTON_WHEEL_LEFT);
|
2017-04-21 05:01:31 +02:00
|
|
|
else if (wheel_event->deltaX < 0)
|
2017-06-19 15:43:46 +02:00
|
|
|
ev->set_button_index(BUTTON_WHEEL_RIGHT);
|
2017-04-21 05:01:31 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
// Different browsers give wildly different delta values, and we can't
|
|
|
|
// interpret deltaMode, so use default value for wheel events' factor
|
|
|
|
|
|
|
|
ev->set_pressed(true);
|
2017-04-21 05:01:31 +02:00
|
|
|
_input->parse_input_event(ev);
|
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
ev->set_pressed(false);
|
2017-04-21 05:01:31 +02:00
|
|
|
_input->parse_input_event(ev);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:41:39 +02:00
|
|
|
static Point2 _prev_touches[32];
|
|
|
|
|
|
|
|
static EM_BOOL _touchpress_callback(int event_type, const EmscriptenTouchEvent *touch_event, void *user_data) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(
|
|
|
|
event_type != EMSCRIPTEN_EVENT_TOUCHSTART &&
|
|
|
|
event_type != EMSCRIPTEN_EVENT_TOUCHEND &&
|
|
|
|
event_type != EMSCRIPTEN_EVENT_TOUCHCANCEL,
|
|
|
|
false);
|
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
Ref<InputEventScreenTouch> ev;
|
|
|
|
ev.instance();
|
2017-04-24 21:41:39 +02:00
|
|
|
int lowest_id_index = -1;
|
|
|
|
for (int i = 0; i < touch_event->numTouches; ++i) {
|
|
|
|
|
|
|
|
const EmscriptenTouchPoint &touch = touch_event->touches[i];
|
|
|
|
if (lowest_id_index == -1 || touch.identifier < touch_event->touches[lowest_id_index].identifier)
|
|
|
|
lowest_id_index = i;
|
|
|
|
if (!touch.isChanged)
|
|
|
|
continue;
|
2017-06-19 15:43:46 +02:00
|
|
|
ev->set_index(touch.identifier);
|
|
|
|
ev->set_position(Point2(touch.canvasX, touch.canvasY));
|
|
|
|
_prev_touches[i] = ev->get_position();
|
|
|
|
ev->set_pressed(event_type == EMSCRIPTEN_EVENT_TOUCHSTART);
|
2017-04-24 21:41:39 +02:00
|
|
|
|
|
|
|
_input->parse_input_event(ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (touch_event->touches[lowest_id_index].isChanged) {
|
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
Ref<InputEventMouseButton> ev_mouse;
|
|
|
|
ev_mouse.instance();
|
2017-09-19 19:15:51 +02:00
|
|
|
ev_mouse->set_button_mask(_input->get_mouse_button_mask());
|
2017-06-19 15:43:46 +02:00
|
|
|
dom2godot_mod(touch_event, ev_mouse);
|
2017-04-24 21:41:39 +02:00
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
const EmscriptenTouchPoint &first_touch = touch_event->touches[lowest_id_index];
|
|
|
|
ev_mouse->set_position(Point2(first_touch.canvasX, first_touch.canvasY));
|
|
|
|
ev_mouse->set_global_position(ev_mouse->get_position());
|
|
|
|
|
|
|
|
ev_mouse->set_button_index(BUTTON_LEFT);
|
|
|
|
ev_mouse->set_pressed(event_type == EMSCRIPTEN_EVENT_TOUCHSTART);
|
|
|
|
|
|
|
|
_input->parse_input_event(ev_mouse);
|
2017-04-24 21:41:39 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static EM_BOOL _touchmove_callback(int event_type, const EmscriptenTouchEvent *touch_event, void *user_data) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_TOUCHMOVE, false);
|
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
Ref<InputEventScreenDrag> ev;
|
|
|
|
ev.instance();
|
2017-04-24 21:41:39 +02:00
|
|
|
int lowest_id_index = -1;
|
|
|
|
for (int i = 0; i < touch_event->numTouches; ++i) {
|
|
|
|
|
|
|
|
const EmscriptenTouchPoint &touch = touch_event->touches[i];
|
|
|
|
if (lowest_id_index == -1 || touch.identifier < touch_event->touches[lowest_id_index].identifier)
|
|
|
|
lowest_id_index = i;
|
|
|
|
if (!touch.isChanged)
|
|
|
|
continue;
|
2017-06-19 15:43:46 +02:00
|
|
|
ev->set_index(touch.identifier);
|
|
|
|
ev->set_position(Point2(touch.canvasX, touch.canvasY));
|
2017-04-24 21:41:39 +02:00
|
|
|
Point2 &prev = _prev_touches[i];
|
2017-06-19 15:43:46 +02:00
|
|
|
ev->set_relative(ev->get_position() - prev);
|
|
|
|
prev = ev->get_position();
|
2017-04-24 21:41:39 +02:00
|
|
|
|
|
|
|
_input->parse_input_event(ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (touch_event->touches[lowest_id_index].isChanged) {
|
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
Ref<InputEventMouseMotion> ev_mouse;
|
|
|
|
ev_mouse.instance();
|
|
|
|
dom2godot_mod(touch_event, ev_mouse);
|
2017-09-19 19:15:51 +02:00
|
|
|
ev_mouse->set_button_mask(_input->get_mouse_button_mask());
|
2017-04-24 21:41:39 +02:00
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
const EmscriptenTouchPoint &first_touch = touch_event->touches[lowest_id_index];
|
|
|
|
ev_mouse->set_position(Point2(first_touch.canvasX, first_touch.canvasY));
|
|
|
|
ev_mouse->set_global_position(ev_mouse->get_position());
|
2017-04-24 21:41:39 +02:00
|
|
|
|
2017-11-28 23:30:57 +01:00
|
|
|
ev_mouse->set_relative(ev_mouse->get_position() - _input->get_mouse_position());
|
2017-06-19 15:43:46 +02:00
|
|
|
_input->set_mouse_position(ev_mouse->get_position());
|
|
|
|
ev_mouse->set_speed(_input->get_last_mouse_speed());
|
|
|
|
|
|
|
|
_input->parse_input_event(ev_mouse);
|
2017-04-24 21:41:39 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
static Ref<InputEventKey> _setup_key_event(const EmscriptenKeyboardEvent *emscripten_event) {
|
2016-04-16 00:19:02 +02:00
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
Ref<InputEventKey> ev;
|
|
|
|
ev.instance();
|
|
|
|
ev->set_echo(emscripten_event->repeat);
|
|
|
|
dom2godot_mod(emscripten_event, ev);
|
|
|
|
ev->set_scancode(dom2godot_scancode(emscripten_event->keyCode));
|
2016-04-16 00:19:02 +02:00
|
|
|
|
|
|
|
String unicode = String::utf8(emscripten_event->key);
|
2016-11-18 18:52:44 +01:00
|
|
|
// check if empty or multi-character (e.g. `CapsLock`)
|
2017-03-05 16:44:50 +01:00
|
|
|
if (unicode.length() != 1) {
|
2016-11-18 18:52:44 +01:00
|
|
|
// might be empty as well, but better than nonsense
|
2016-04-16 00:19:02 +02:00
|
|
|
unicode = String::utf8(emscripten_event->charValue);
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if (unicode.length() == 1) {
|
2017-06-19 15:43:46 +02:00
|
|
|
ev->set_unicode(unicode[0]);
|
2016-04-16 00:19:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ev;
|
|
|
|
}
|
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
static Ref<InputEventKey> deferred_key_event;
|
2016-04-19 14:15:20 +02:00
|
|
|
|
2016-04-16 00:19:02 +02:00
|
|
|
static EM_BOOL _keydown_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYDOWN, false);
|
2016-04-16 00:19:02 +02:00
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
Ref<InputEventKey> ev = _setup_key_event(key_event);
|
|
|
|
ev->set_pressed(true);
|
|
|
|
if (ev->get_unicode() == 0 && keycode_has_unicode(ev->get_scancode())) {
|
2016-04-19 14:15:20 +02:00
|
|
|
// defer to keypress event for legacy unicode retrieval
|
|
|
|
deferred_key_event = ev;
|
|
|
|
return false; // do not suppress keypress event
|
|
|
|
}
|
2017-04-21 05:01:31 +02:00
|
|
|
_input->parse_input_event(ev);
|
2016-04-19 14:15:20 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static EM_BOOL _keypress_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYPRESS, false);
|
2016-04-19 14:15:20 +02:00
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
deferred_key_event->set_unicode(key_event->charCode);
|
2017-04-21 05:01:31 +02:00
|
|
|
_input->parse_input_event(deferred_key_event);
|
2016-04-19 14:15:20 +02:00
|
|
|
return true;
|
2016-04-16 00:19:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static EM_BOOL _keyup_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYUP, false);
|
2016-04-16 00:19:02 +02:00
|
|
|
|
2017-06-19 15:43:46 +02:00
|
|
|
Ref<InputEventKey> ev = _setup_key_event(key_event);
|
|
|
|
ev->set_pressed(false);
|
2017-04-21 05:01:31 +02:00
|
|
|
_input->parse_input_event(ev);
|
2017-05-20 17:38:03 +02:00
|
|
|
return ev->get_scancode() != KEY_UNKNOWN && ev->get_scancode() != 0;
|
2016-04-16 00:19:02 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 02:23:15 +01:00
|
|
|
static EM_BOOL joy_callback_func(int p_type, const EmscriptenGamepadEvent *p_event, void *p_user) {
|
2017-03-05 16:44:50 +01:00
|
|
|
OS_JavaScript *os = (OS_JavaScript *)OS::get_singleton();
|
2016-01-21 02:23:15 +01:00
|
|
|
if (os) {
|
|
|
|
return os->joy_connection_changed(p_type, p_event);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-22 15:43:05 +02:00
|
|
|
extern "C" {
|
|
|
|
void send_notification(int notif) {
|
|
|
|
if (notif == MainLoop::NOTIFICATION_WM_MOUSE_ENTER || notif == MainLoop::NOTIFICATION_WM_MOUSE_EXIT) {
|
|
|
|
_cursor_inside_canvas = notif == MainLoop::NOTIFICATION_WM_MOUSE_ENTER;
|
|
|
|
}
|
|
|
|
OS_JavaScript::get_singleton()->get_main_loop()->notification(notif);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 18:26:44 +01:00
|
|
|
Error OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
print_line("Init OS");
|
|
|
|
|
2017-04-27 15:34:46 +02:00
|
|
|
EmscriptenWebGLContextAttributes attributes;
|
|
|
|
emscripten_webgl_init_context_attributes(&attributes);
|
|
|
|
attributes.alpha = false;
|
|
|
|
attributes.antialias = false;
|
|
|
|
attributes.majorVersion = 2;
|
|
|
|
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context(NULL, &attributes);
|
2018-01-04 17:32:33 +01:00
|
|
|
ERR_FAIL_COND_V(emscripten_webgl_make_context_current(ctx) != EMSCRIPTEN_RESULT_SUCCESS, ERR_UNAVAILABLE);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
video_mode = p_desired;
|
2017-04-27 15:34:46 +02:00
|
|
|
// can't fulfil fullscreen request due to browser security
|
2017-03-05 16:44:50 +01:00
|
|
|
video_mode.fullscreen = false;
|
2017-11-19 15:30:09 +01:00
|
|
|
/* clang-format off */
|
|
|
|
bool resize_canvas_on_start = EM_ASM_INT_V(
|
|
|
|
return Module.resizeCanvasOnStart;
|
|
|
|
);
|
|
|
|
/* clang-format on */
|
|
|
|
if (resize_canvas_on_start) {
|
|
|
|
set_window_size(Size2(video_mode.width, video_mode.height));
|
|
|
|
} else {
|
|
|
|
Size2 canvas_size = get_window_size();
|
|
|
|
video_mode.width = canvas_size.width;
|
|
|
|
video_mode.height = canvas_size.height;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-11-18 18:52:44 +01:00
|
|
|
char locale_ptr[16];
|
2017-01-15 20:00:04 +01:00
|
|
|
/* clang-format off */
|
2017-11-19 15:30:09 +01:00
|
|
|
EM_ASM_ARGS({
|
|
|
|
stringToUTF8(Module.locale, $0, 16);
|
2016-11-18 18:52:44 +01:00
|
|
|
}, locale_ptr);
|
2017-01-15 20:00:04 +01:00
|
|
|
/* clang-format on */
|
2016-11-18 18:52:44 +01:00
|
|
|
setenv("LANG", locale_ptr, true);
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
print_line("Init Audio");
|
|
|
|
|
2017-01-15 20:06:14 +01:00
|
|
|
AudioDriverManager::add_driver(&audio_driver_javascript);
|
2017-09-13 18:34:22 +02:00
|
|
|
AudioDriverManager::initialize(p_audio_driver);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-31 03:46:30 +01:00
|
|
|
RasterizerGLES3::register_config();
|
|
|
|
RasterizerGLES3::make_current();
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
print_line("Init VS");
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
visual_server = memnew(VisualServerRaster());
|
2017-08-08 13:51:10 +02:00
|
|
|
// visual_server->cursor_set_visible(false, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
print_line("Init Physicsserver");
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
input = memnew(InputDefault);
|
2017-04-21 05:01:31 +02:00
|
|
|
_input = input;
|
2017-03-05 16:44:50 +01:00
|
|
|
|
|
|
|
power_manager = memnew(PowerJavascript);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define EM_CHECK(ev) \
|
|
|
|
if (result != EMSCRIPTEN_RESULT_SUCCESS) \
|
2016-11-26 13:13:16 +01:00
|
|
|
ERR_PRINTS("Error while setting " #ev " callback: Code " + itos(result))
|
2017-04-24 21:41:39 +02:00
|
|
|
#define SET_EM_CALLBACK(target, ev, cb) \
|
|
|
|
result = emscripten_set_##ev##_callback(target, this, true, &cb); \
|
2017-03-05 16:44:50 +01:00
|
|
|
EM_CHECK(ev)
|
|
|
|
#define SET_EM_CALLBACK_NODATA(ev, cb) \
|
|
|
|
result = emscripten_set_##ev##_callback(NULL, true, &cb); \
|
|
|
|
EM_CHECK(ev)
|
2016-11-26 13:13:16 +01:00
|
|
|
|
|
|
|
EMSCRIPTEN_RESULT result;
|
2017-07-22 15:43:05 +02:00
|
|
|
SET_EM_CALLBACK("#window", mousemove, _mousemove_callback)
|
2017-04-24 21:41:39 +02:00
|
|
|
SET_EM_CALLBACK("#canvas", mousedown, _mousebutton_callback)
|
2017-07-22 15:43:05 +02:00
|
|
|
SET_EM_CALLBACK("#window", mouseup, _mousebutton_callback)
|
|
|
|
SET_EM_CALLBACK("#window", wheel, _wheel_callback)
|
|
|
|
SET_EM_CALLBACK("#window", touchstart, _touchpress_callback)
|
|
|
|
SET_EM_CALLBACK("#window", touchmove, _touchmove_callback)
|
|
|
|
SET_EM_CALLBACK("#window", touchend, _touchpress_callback)
|
|
|
|
SET_EM_CALLBACK("#window", touchcancel, _touchpress_callback)
|
|
|
|
SET_EM_CALLBACK("#canvas", keydown, _keydown_callback)
|
|
|
|
SET_EM_CALLBACK("#canvas", keypress, _keypress_callback)
|
|
|
|
SET_EM_CALLBACK("#canvas", keyup, _keyup_callback)
|
2017-04-24 21:41:39 +02:00
|
|
|
SET_EM_CALLBACK(NULL, resize, _browser_resize_callback)
|
|
|
|
SET_EM_CALLBACK(NULL, fullscreenchange, _fullscreen_change_callback)
|
2016-11-26 13:13:16 +01:00
|
|
|
SET_EM_CALLBACK_NODATA(gamepadconnected, joy_callback_func)
|
|
|
|
SET_EM_CALLBACK_NODATA(gamepaddisconnected, joy_callback_func)
|
|
|
|
|
|
|
|
#undef SET_EM_CALLBACK_NODATA
|
|
|
|
#undef SET_EM_CALLBACK
|
|
|
|
#undef EM_CHECK
|
2016-04-11 23:22:14 +02:00
|
|
|
|
2017-02-22 23:14:21 +01:00
|
|
|
visual_server->init();
|
2018-01-03 18:26:44 +01:00
|
|
|
|
|
|
|
return OK;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void OS_JavaScript::set_main_loop(MainLoop *p_main_loop) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
main_loop = p_main_loop;
|
2014-02-10 02:10:30 +01:00
|
|
|
input->set_main_loop(p_main_loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::delete_main_loop() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
memdelete(main_loop);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::finalize() {
|
|
|
|
|
|
|
|
memdelete(input);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void OS_JavaScript::alert(const String &p_alert, const String &p_title) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-15 20:00:04 +01:00
|
|
|
/* clang-format off */
|
2016-11-18 18:52:44 +01:00
|
|
|
EM_ASM_({
|
|
|
|
window.alert(UTF8ToString($0));
|
|
|
|
}, p_alert.utf8().get_data());
|
2017-01-15 20:00:04 +01:00
|
|
|
/* clang-format on */
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-04-29 21:35:31 +02:00
|
|
|
static const char *godot2dom_cursor(OS::CursorShape p_shape) {
|
|
|
|
|
|
|
|
switch (p_shape) {
|
|
|
|
case OS::CURSOR_ARROW:
|
|
|
|
default:
|
|
|
|
return "auto";
|
|
|
|
case OS::CURSOR_IBEAM: return "text";
|
|
|
|
case OS::CURSOR_POINTING_HAND: return "pointer";
|
|
|
|
case OS::CURSOR_CROSS: return "crosshair";
|
|
|
|
case OS::CURSOR_WAIT: return "progress";
|
|
|
|
case OS::CURSOR_BUSY: return "wait";
|
|
|
|
case OS::CURSOR_DRAG: return "grab";
|
|
|
|
case OS::CURSOR_CAN_DROP: return "grabbing";
|
|
|
|
case OS::CURSOR_FORBIDDEN: return "no-drop";
|
|
|
|
case OS::CURSOR_VSIZE: return "ns-resize";
|
|
|
|
case OS::CURSOR_HSIZE: return "ew-resize";
|
|
|
|
case OS::CURSOR_BDIAGSIZE: return "nesw-resize";
|
|
|
|
case OS::CURSOR_FDIAGSIZE: return "nwse-resize";
|
|
|
|
case OS::CURSOR_MOVE: return "move";
|
|
|
|
case OS::CURSOR_VSPLIT: return "row-resize";
|
|
|
|
case OS::CURSOR_HSPLIT: return "col-resize";
|
|
|
|
case OS::CURSOR_HELP: return "help";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-29 04:54:48 +02:00
|
|
|
void OS_JavaScript::set_css_cursor(const char *p_cursor) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-04-29 04:54:48 +02:00
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_({
|
|
|
|
Module.canvas.style.cursor = Module.UTF8ToString($0);
|
|
|
|
}, p_cursor);
|
|
|
|
/* clang-format on */
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-04-29 04:54:48 +02:00
|
|
|
const char *OS_JavaScript::get_css_cursor() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-04-29 04:54:48 +02:00
|
|
|
char cursor[16];
|
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_INT({
|
|
|
|
Module.stringToUTF8(Module.canvas.style.cursor ? Module.canvas.style.cursor : 'auto', $0, 16);
|
|
|
|
}, cursor);
|
|
|
|
/* clang-format on */
|
|
|
|
return cursor;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-04-29 04:54:48 +02:00
|
|
|
void OS_JavaScript::set_mouse_mode(OS::MouseMode p_mode) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-04-29 04:54:48 +02:00
|
|
|
ERR_FAIL_INDEX(p_mode, MOUSE_MODE_CONFINED + 1);
|
|
|
|
ERR_EXPLAIN("MOUSE_MODE_CONFINED is not supported for the HTML5 platform");
|
|
|
|
ERR_FAIL_COND(p_mode == MOUSE_MODE_CONFINED);
|
|
|
|
if (p_mode == get_mouse_mode())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (p_mode == MOUSE_MODE_VISIBLE) {
|
|
|
|
|
2017-04-29 21:35:31 +02:00
|
|
|
set_css_cursor(godot2dom_cursor(cursor_shape));
|
2017-04-29 04:54:48 +02:00
|
|
|
emscripten_exit_pointerlock();
|
|
|
|
|
|
|
|
} else if (p_mode == MOUSE_MODE_HIDDEN) {
|
|
|
|
|
|
|
|
set_css_cursor("none");
|
|
|
|
emscripten_exit_pointerlock();
|
|
|
|
|
|
|
|
} else if (p_mode == MOUSE_MODE_CAPTURED) {
|
|
|
|
|
|
|
|
EMSCRIPTEN_RESULT result = emscripten_request_pointerlock("canvas", false);
|
|
|
|
ERR_EXPLAIN("MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback");
|
|
|
|
ERR_FAIL_COND(result == EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED);
|
|
|
|
ERR_FAIL_COND(result != EMSCRIPTEN_RESULT_SUCCESS);
|
2017-04-29 21:35:31 +02:00
|
|
|
set_css_cursor(godot2dom_cursor(cursor_shape));
|
2017-04-29 04:54:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OS::MouseMode OS_JavaScript::get_mouse_mode() const {
|
|
|
|
|
|
|
|
if (!strcmp(get_css_cursor(), "none"))
|
|
|
|
return MOUSE_MODE_HIDDEN;
|
|
|
|
|
|
|
|
EmscriptenPointerlockChangeEvent ev;
|
|
|
|
emscripten_get_pointerlock_status(&ev);
|
|
|
|
return ev.isActive && (strcmp(ev.id, "canvas") == 0) ? MOUSE_MODE_CAPTURED : MOUSE_MODE_VISIBLE;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-11-23 23:53:38 +01:00
|
|
|
|
2017-03-29 17:29:38 +02:00
|
|
|
Point2 OS_JavaScript::get_mouse_position() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-29 17:29:38 +02:00
|
|
|
return input->get_mouse_position();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-11-23 23:53:38 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
int OS_JavaScript::get_mouse_button_state() const {
|
|
|
|
|
2017-04-21 05:01:31 +02:00
|
|
|
return input->get_mouse_button_mask();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void OS_JavaScript::set_window_title(const String &p_title) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-15 20:00:04 +01:00
|
|
|
/* clang-format off */
|
2016-11-18 18:52:44 +01:00
|
|
|
EM_ASM_({
|
|
|
|
document.title = UTF8ToString($0);
|
|
|
|
}, p_title.utf8().get_data());
|
2017-01-15 20:00:04 +01:00
|
|
|
/* clang-format on */
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//interesting byt not yet
|
|
|
|
//void set_clipboard(const String& p_text);
|
|
|
|
//String get_clipboard() const;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void OS_JavaScript::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-11-26 13:13:16 +01:00
|
|
|
video_mode = p_video_mode;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OS::VideoMode OS_JavaScript::get_video_mode(int p_screen) const {
|
|
|
|
|
2016-11-26 13:13:16 +01:00
|
|
|
return video_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 OS_JavaScript::get_screen_size(int p_screen) const {
|
|
|
|
|
|
|
|
EmscriptenFullscreenChangeEvent ev;
|
|
|
|
EMSCRIPTEN_RESULT result = emscripten_get_fullscreen_status(&ev);
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(result != EMSCRIPTEN_RESULT_SUCCESS, Size2());
|
2016-11-26 13:13:16 +01:00
|
|
|
return Size2(ev.screenWidth, ev.screenHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::set_window_size(const Size2 p_size) {
|
|
|
|
|
2017-07-25 20:55:44 +02:00
|
|
|
windowed_size = p_size;
|
2016-11-26 13:13:16 +01:00
|
|
|
if (is_window_fullscreen()) {
|
2017-07-25 20:55:44 +02:00
|
|
|
window_maximized = false;
|
2016-11-26 13:13:16 +01:00
|
|
|
set_window_fullscreen(false);
|
2017-07-25 20:55:44 +02:00
|
|
|
} else if (is_window_maximized()) {
|
|
|
|
set_window_maximized(false);
|
|
|
|
} else {
|
|
|
|
video_mode.width = p_size.x;
|
|
|
|
video_mode.height = p_size.y;
|
|
|
|
emscripten_set_canvas_size(p_size.x, p_size.y);
|
2016-11-26 13:13:16 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2015-04-02 19:54:36 +02:00
|
|
|
|
2015-04-02 20:03:14 +02:00
|
|
|
Size2 OS_JavaScript::get_window_size() const {
|
2015-04-02 19:54:36 +02:00
|
|
|
|
2016-11-26 13:13:16 +01:00
|
|
|
int canvas[3];
|
2017-03-05 16:44:50 +01:00
|
|
|
emscripten_get_canvas_size(canvas, canvas + 1, canvas + 2);
|
2016-11-26 13:13:16 +01:00
|
|
|
return Size2(canvas[0], canvas[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::set_window_maximized(bool p_enabled) {
|
|
|
|
|
|
|
|
window_maximized = p_enabled;
|
2017-07-25 20:55:44 +02:00
|
|
|
if (is_window_fullscreen()) {
|
|
|
|
set_window_fullscreen(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Calling emscripten_enter_soft_fullscreen mutltiple times hides all
|
|
|
|
// page elements except the canvas permanently, so track state
|
|
|
|
if (p_enabled && !soft_fs_enabled) {
|
|
|
|
|
|
|
|
EmscriptenFullscreenStrategy strategy;
|
|
|
|
strategy.scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH;
|
|
|
|
strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF;
|
|
|
|
strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT;
|
|
|
|
strategy.canvasResizedCallback = NULL;
|
|
|
|
emscripten_enter_soft_fullscreen(NULL, &strategy);
|
|
|
|
soft_fs_enabled = true;
|
|
|
|
video_mode.width = get_window_size().width;
|
|
|
|
video_mode.height = get_window_size().height;
|
|
|
|
} else if (!p_enabled) {
|
|
|
|
|
|
|
|
emscripten_exit_soft_fullscreen();
|
|
|
|
soft_fs_enabled = false;
|
|
|
|
video_mode.width = windowed_size.width;
|
|
|
|
video_mode.height = windowed_size.height;
|
|
|
|
emscripten_set_canvas_size(video_mode.width, video_mode.height);
|
2016-11-26 13:13:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::set_window_fullscreen(bool p_enable) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_enable == is_window_fullscreen()) {
|
2016-11-26 13:13:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only requesting changes here, if successful, canvas is resized in
|
|
|
|
// _browser_resize_callback or _fullscreen_change_callback
|
|
|
|
EMSCRIPTEN_RESULT result;
|
|
|
|
if (p_enable) {
|
2017-07-25 20:55:44 +02:00
|
|
|
if (window_maximized) {
|
|
|
|
// soft fs during real fs can cause issues
|
|
|
|
set_window_maximized(false);
|
|
|
|
window_maximized = true;
|
|
|
|
}
|
|
|
|
EmscriptenFullscreenStrategy strategy;
|
|
|
|
strategy.scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH;
|
|
|
|
strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF;
|
|
|
|
strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT;
|
|
|
|
strategy.canvasResizedCallback = NULL;
|
|
|
|
emscripten_request_fullscreen_strategy(NULL, false, &strategy);
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2016-11-26 13:13:16 +01:00
|
|
|
result = emscripten_exit_fullscreen();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (result != EMSCRIPTEN_RESULT_SUCCESS) {
|
2016-11-26 13:13:16 +01:00
|
|
|
ERR_PRINTS("Failed to exit fullscreen: Code " + itos(result));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OS_JavaScript::is_window_fullscreen() const {
|
|
|
|
|
|
|
|
return video_mode.fullscreen;
|
2015-04-02 19:54:36 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 20:55:44 +02:00
|
|
|
void OS_JavaScript::request_canvas_size_adjustment() {
|
|
|
|
|
|
|
|
canvas_size_adjustment_requested = true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void OS_JavaScript::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-11-26 13:13:16 +01:00
|
|
|
Size2 screen = get_screen_size();
|
|
|
|
p_list->push_back(OS::VideoMode(screen.width, screen.height, true));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String OS_JavaScript::get_name() {
|
|
|
|
|
|
|
|
return "HTML5";
|
|
|
|
}
|
|
|
|
|
|
|
|
MainLoop *OS_JavaScript::get_main_loop() const {
|
|
|
|
|
|
|
|
return main_loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OS_JavaScript::can_draw() const {
|
|
|
|
|
|
|
|
return true; //always?
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::set_cursor_shape(CursorShape p_shape) {
|
|
|
|
|
2017-04-29 21:35:31 +02:00
|
|
|
ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
|
|
|
|
|
|
|
|
cursor_shape = p_shape;
|
|
|
|
if (get_mouse_mode() != MOUSE_MODE_HIDDEN)
|
|
|
|
set_css_cursor(godot2dom_cursor(cursor_shape));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::main_loop_begin() {
|
|
|
|
|
|
|
|
if (main_loop)
|
|
|
|
main_loop->init();
|
2017-09-03 01:44:00 +02:00
|
|
|
|
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_ARGS({
|
|
|
|
const send_notification = Module.cwrap('send_notification', null, ['number']);
|
|
|
|
const notifs = arguments;
|
|
|
|
(['mouseover', 'mouseleave', 'focus', 'blur']).forEach(function(event, i) {
|
|
|
|
Module.canvas.addEventListener(event, send_notification.bind(null, notifs[i]));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
MainLoop::NOTIFICATION_WM_MOUSE_ENTER,
|
|
|
|
MainLoop::NOTIFICATION_WM_MOUSE_EXIT,
|
|
|
|
MainLoop::NOTIFICATION_WM_FOCUS_IN,
|
|
|
|
MainLoop::NOTIFICATION_WM_FOCUS_OUT
|
|
|
|
);
|
|
|
|
/* clang-format on */
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-09-03 01:44:00 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
bool OS_JavaScript::main_loop_iterate() {
|
|
|
|
|
|
|
|
if (!main_loop)
|
|
|
|
return false;
|
2015-09-12 15:54:47 +02:00
|
|
|
|
2017-10-02 16:09:24 +02:00
|
|
|
if (idbfs_available && time_to_save_sync >= 0) {
|
2015-09-12 15:54:47 +02:00
|
|
|
int64_t newtime = get_ticks_msec();
|
|
|
|
int64_t elapsed = newtime - last_sync_time;
|
2017-03-05 16:44:50 +01:00
|
|
|
last_sync_time = newtime;
|
2015-09-12 15:54:47 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
time_to_save_sync -= elapsed;
|
2015-09-12 15:54:47 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (time_to_save_sync < 0) {
|
2015-09-12 15:54:47 +02:00
|
|
|
//time to sync, for real
|
2017-01-15 20:00:04 +01:00
|
|
|
/* clang-format off */
|
2015-09-12 15:54:47 +02:00
|
|
|
EM_ASM(
|
2017-01-12 14:14:40 +01:00
|
|
|
FS.syncfs(function(err) {
|
|
|
|
if (err) { Module.printErr('Failed to save IDB file system: ' + err.message); }
|
|
|
|
});
|
2015-09-12 15:54:47 +02:00
|
|
|
);
|
2017-01-15 20:00:04 +01:00
|
|
|
/* clang-format on */
|
2015-09-12 15:54:47 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-08 21:05:51 +01:00
|
|
|
process_joypads();
|
2017-07-25 20:55:44 +02:00
|
|
|
if (canvas_size_adjustment_requested) {
|
|
|
|
|
|
|
|
if (video_mode.fullscreen || window_maximized) {
|
|
|
|
video_mode.width = get_window_size().width;
|
|
|
|
video_mode.height = get_window_size().height;
|
|
|
|
}
|
|
|
|
if (!video_mode.fullscreen) {
|
|
|
|
set_window_maximized(window_maximized);
|
|
|
|
}
|
|
|
|
canvas_size_adjustment_requested = false;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
return Main::iteration();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::main_loop_end() {
|
|
|
|
|
|
|
|
if (main_loop)
|
|
|
|
main_loop->finish();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void OS_JavaScript::process_accelerometer(const Vector3 &p_accelerometer) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
input->set_accelerometer(p_accelerometer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OS_JavaScript::has_touchscreen_ui_hint() const {
|
|
|
|
|
2017-07-28 05:39:25 +02:00
|
|
|
/* clang-format off */
|
|
|
|
return EM_ASM_INT_V(
|
|
|
|
return 'ontouchstart' in window;
|
|
|
|
);
|
|
|
|
/* clang-format on */
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::main_loop_request_quit() {
|
|
|
|
|
|
|
|
if (main_loop)
|
|
|
|
main_loop->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error OS_JavaScript::shell_open(String p_uri) {
|
2017-01-15 20:00:04 +01:00
|
|
|
/* clang-format off */
|
2016-11-18 18:52:44 +01:00
|
|
|
EM_ASM_({
|
|
|
|
window.open(UTF8ToString($0), '_blank');
|
|
|
|
}, p_uri.utf8().get_data());
|
2017-01-15 20:00:04 +01:00
|
|
|
/* clang-format on */
|
2016-11-18 18:52:44 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String OS_JavaScript::get_resource_dir() const {
|
|
|
|
|
|
|
|
return "/"; //javascript has it's own filesystem for resources inside the APK
|
|
|
|
}
|
|
|
|
|
2017-11-17 15:25:22 +01:00
|
|
|
String OS_JavaScript::get_user_data_dir() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
2017-11-17 15:25:22 +01:00
|
|
|
if (get_user_data_dir_func)
|
|
|
|
return get_user_data_dir_func();
|
2017-01-14 12:26:56 +01:00
|
|
|
*/
|
2015-09-12 15:54:47 +02:00
|
|
|
return "/userfs";
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2016-11-18 18:52:44 +01:00
|
|
|
String OS_JavaScript::get_executable_path() const {
|
|
|
|
|
2017-03-28 03:21:21 +02:00
|
|
|
return OS::get_executable_path();
|
2016-11-18 18:52:44 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void OS_JavaScript::_close_notification_funcs(const String &p_file, int p_flags) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-10-02 16:09:24 +02:00
|
|
|
OS_JavaScript *os = static_cast<OS_JavaScript *>(get_singleton());
|
|
|
|
if (os->idbfs_available && p_file.begins_with("/userfs") && p_flags & FileAccess::WRITE) {
|
|
|
|
os->last_sync_time = OS::get_singleton()->get_ticks_msec();
|
|
|
|
os->time_to_save_sync = 5000; //five seconds since last save
|
2015-09-12 15:54:47 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
void OS_JavaScript::process_joypads() {
|
2016-01-21 02:23:15 +01:00
|
|
|
|
|
|
|
int joy_count = emscripten_get_num_gamepads();
|
|
|
|
for (int i = 0; i < joy_count; i++) {
|
|
|
|
EmscriptenGamepadEvent state;
|
|
|
|
emscripten_get_gamepad_status(i, &state);
|
|
|
|
if (state.connected) {
|
|
|
|
|
2016-01-22 22:56:05 +01:00
|
|
|
int num_buttons = MIN(state.numButtons, 18);
|
2016-01-21 02:23:15 +01:00
|
|
|
int num_axes = MIN(state.numAxes, 8);
|
|
|
|
for (int j = 0; j < num_buttons; j++) {
|
|
|
|
|
|
|
|
float value = state.analogButton[j];
|
|
|
|
if (String(state.mapping) == "standard" && (j == 6 || j == 7)) {
|
|
|
|
InputDefault::JoyAxis jx;
|
|
|
|
jx.min = 0;
|
|
|
|
jx.value = value;
|
2017-03-26 15:59:13 +02:00
|
|
|
input->joy_axis(i, j, jx);
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2017-03-26 15:59:13 +02:00
|
|
|
input->joy_button(i, j, value);
|
2016-01-21 02:23:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int j = 0; j < num_axes; j++) {
|
|
|
|
|
|
|
|
InputDefault::JoyAxis jx;
|
|
|
|
jx.min = -1;
|
|
|
|
jx.value = state.axis[j];
|
2017-03-26 15:59:13 +02:00
|
|
|
input->joy_axis(i, j, jx);
|
2016-01-21 02:23:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OS_JavaScript::joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event) {
|
|
|
|
if (p_type == EMSCRIPTEN_EVENT_GAMEPADCONNECTED) {
|
|
|
|
|
|
|
|
String guid = "";
|
|
|
|
if (String(p_event->mapping) == "standard")
|
|
|
|
guid = "Default HTML5 Gamepad";
|
|
|
|
input->joy_connection_changed(p_event->index, true, String(p_event->id), guid);
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2016-01-21 02:23:15 +01:00
|
|
|
input->joy_connection_changed(p_event->index, false, "");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OS_JavaScript::is_joy_known(int p_device) {
|
|
|
|
return input->is_joy_mapped(p_device);
|
|
|
|
}
|
|
|
|
|
|
|
|
String OS_JavaScript::get_joy_guid(int p_device) const {
|
|
|
|
return input->get_joy_guid_remapped(p_device);
|
|
|
|
}
|
|
|
|
|
2017-09-12 21:09:06 +02:00
|
|
|
OS::PowerState OS_JavaScript::get_power_state() {
|
2016-07-23 13:15:55 +02:00
|
|
|
return power_manager->get_power_state();
|
|
|
|
}
|
|
|
|
|
|
|
|
int OS_JavaScript::get_power_seconds_left() {
|
|
|
|
return power_manager->get_power_seconds_left();
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-07-23 13:15:55 +02:00
|
|
|
int OS_JavaScript::get_power_percent_left() {
|
|
|
|
return power_manager->get_power_percent_left();
|
|
|
|
}
|
|
|
|
|
2017-07-19 22:00:46 +02:00
|
|
|
bool OS_JavaScript::_check_internal_feature_support(const String &p_feature) {
|
|
|
|
|
|
|
|
return p_feature == "web" || p_feature == "s3tc"; // TODO check for these features really being available
|
|
|
|
}
|
|
|
|
|
2017-10-02 16:09:24 +02:00
|
|
|
void OS_JavaScript::set_idbfs_available(bool p_idbfs_available) {
|
|
|
|
|
|
|
|
idbfs_available = p_idbfs_available;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OS_JavaScript::is_userfs_persistent() const {
|
|
|
|
|
|
|
|
return idbfs_available;
|
|
|
|
}
|
|
|
|
|
2017-11-17 15:25:22 +01:00
|
|
|
OS_JavaScript::OS_JavaScript(const char *p_execpath, GetUserDataDirFunc p_get_user_data_dir_func) {
|
2017-03-28 03:21:21 +02:00
|
|
|
set_cmdline(p_execpath, get_cmdline_args());
|
2017-03-05 16:44:50 +01:00
|
|
|
main_loop = NULL;
|
|
|
|
gl_extensions = NULL;
|
|
|
|
window_maximized = false;
|
2017-07-25 20:55:44 +02:00
|
|
|
soft_fs_enabled = false;
|
|
|
|
canvas_size_adjustment_requested = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-11-17 15:25:22 +01:00
|
|
|
get_user_data_dir_func = p_get_user_data_dir_func;
|
2017-03-05 16:44:50 +01:00
|
|
|
FileAccessUnix::close_notification_func = _close_notification_funcs;
|
2015-09-12 15:54:47 +02:00
|
|
|
|
2017-10-02 16:09:24 +02:00
|
|
|
idbfs_available = false;
|
2017-03-05 16:44:50 +01:00
|
|
|
time_to_save_sync = -1;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OS_JavaScript::~OS_JavaScript() {
|
|
|
|
}
|