2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* os_javascript.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2022-01-13 08:45:09 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "os_javascript.h"
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2020-01-19 10:44:19 +00:00
|
|
|
#include "core/io/json.h"
|
2018-10-01 19:35:55 +00:00
|
|
|
#include "drivers/gles2/rasterizer_gles2.h"
|
|
|
|
#include "drivers/gles3/rasterizer_gles3.h"
|
|
|
|
#include "drivers/unix/dir_access_unix.h"
|
|
|
|
#include "drivers/unix/file_access_unix.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "main/main.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "servers/visual/visual_server_raster.h"
|
2020-01-19 10:44:19 +00:00
|
|
|
#ifndef NO_THREADS
|
|
|
|
#include "servers/visual/visual_server_wrap_mt.h"
|
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-12-04 23:43:02 +00:00
|
|
|
#include <dlfcn.h>
|
2017-01-16 18:19:45 +00:00
|
|
|
#include <emscripten.h>
|
2018-09-16 17:37:36 +00:00
|
|
|
#include <png.h>
|
2017-03-05 15:44:50 +00:00
|
|
|
#include <stdlib.h>
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2022-01-31 14:28:12 +00:00
|
|
|
#include "api/javascript_singleton.h"
|
2018-07-08 00:23:19 +00:00
|
|
|
#include "dom_keys.inc"
|
2020-10-17 21:31:30 +00:00
|
|
|
#include "godot_js.h"
|
2018-07-08 00:23:19 +00:00
|
|
|
|
2017-04-24 19:41:39 +00:00
|
|
|
#define DOM_BUTTON_LEFT 0
|
|
|
|
#define DOM_BUTTON_MIDDLE 1
|
|
|
|
#define DOM_BUTTON_RIGHT 2
|
2018-07-05 14:50:30 +00:00
|
|
|
#define DOM_BUTTON_XBUTTON1 3
|
|
|
|
#define DOM_BUTTON_XBUTTON2 4
|
2017-04-21 03:01:31 +00:00
|
|
|
|
2020-10-17 21:31:30 +00:00
|
|
|
// Quit
|
|
|
|
void OS_JavaScript::request_quit_callback() {
|
|
|
|
OS_JavaScript *os = get_singleton();
|
|
|
|
if (os && os->get_main_loop()) {
|
|
|
|
os->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-05-23 12:02:12 +00:00
|
|
|
bool OS_JavaScript::tts_is_speaking() const {
|
|
|
|
return godot_js_tts_is_speaking();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OS_JavaScript::tts_is_paused() const {
|
|
|
|
return godot_js_tts_is_paused();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::update_voices_callback(int p_size, const char **p_voice) {
|
|
|
|
get_singleton()->voices.clear();
|
|
|
|
for (int i = 0; i < p_size; i++) {
|
|
|
|
Vector<String> tokens = String::utf8(p_voice[i]).split(";", true, 2);
|
|
|
|
if (tokens.size() == 2) {
|
|
|
|
Dictionary voice_d;
|
|
|
|
voice_d["name"] = tokens[1];
|
|
|
|
voice_d["id"] = tokens[1];
|
|
|
|
voice_d["language"] = tokens[0];
|
|
|
|
get_singleton()->voices.push_back(voice_d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Array OS_JavaScript::tts_get_voices() const {
|
|
|
|
godot_js_tts_get_voices(update_voices_callback);
|
|
|
|
return voices;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::tts_speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int p_utterance_id, bool p_interrupt) {
|
|
|
|
if (p_interrupt) {
|
|
|
|
tts_stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_text.empty()) {
|
|
|
|
tts_post_utterance_event(OS::TTS_UTTERANCE_CANCELED, p_utterance_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CharString string = p_text.utf8();
|
|
|
|
utterance_ids[p_utterance_id] = string;
|
|
|
|
|
|
|
|
godot_js_tts_speak(string.get_data(), p_voice.utf8().get_data(), CLAMP(p_volume, 0, 100), CLAMP(p_pitch, 0.f, 2.f), CLAMP(p_rate, 0.1f, 10.f), p_utterance_id, OS_JavaScript::_js_utterance_callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::tts_pause() {
|
|
|
|
godot_js_tts_pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::tts_resume() {
|
|
|
|
godot_js_tts_resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::tts_stop() {
|
|
|
|
for (Map<int, CharString>::Element *E = utterance_ids.front(); E; E = E->next()) {
|
|
|
|
tts_post_utterance_event(OS::TTS_UTTERANCE_CANCELED, E->key());
|
|
|
|
}
|
|
|
|
utterance_ids.clear();
|
|
|
|
godot_js_tts_stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::_js_utterance_callback(int p_event, int p_id, int p_pos) {
|
|
|
|
OS_JavaScript *ds = (OS_JavaScript *)OS::get_singleton();
|
|
|
|
if (ds->utterance_ids.has(p_id)) {
|
|
|
|
int pos = 0;
|
|
|
|
if ((TTSUtteranceEvent)p_event == OS::TTS_UTTERANCE_BOUNDARY) {
|
|
|
|
// Convert position from UTF-8 to UTF-32.
|
|
|
|
const CharString &string = ds->utterance_ids[p_id];
|
|
|
|
for (int i = 0; i < MIN(p_pos, string.length()); i++) {
|
|
|
|
uint8_t c = string[i];
|
|
|
|
if ((c & 0xe0) == 0xc0) {
|
|
|
|
i += 1;
|
|
|
|
} else if ((c & 0xf0) == 0xe0) {
|
|
|
|
i += 2;
|
|
|
|
} else if ((c & 0xf8) == 0xf0) {
|
|
|
|
i += 3;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
} else if ((TTSUtteranceEvent)p_event != OS::TTS_UTTERANCE_STARTED) {
|
|
|
|
ds->utterance_ids.erase(p_id);
|
|
|
|
}
|
|
|
|
ds->tts_post_utterance_event((TTSUtteranceEvent)p_event, p_id, pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 21:31:30 +00:00
|
|
|
// Files drop (implemented in JS for now).
|
|
|
|
void OS_JavaScript::drop_files_callback(char **p_filev, int p_filec) {
|
|
|
|
OS_JavaScript *os = get_singleton();
|
|
|
|
if (!os || !os->get_main_loop()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Vector<String> files;
|
|
|
|
for (int i = 0; i < p_filec; i++) {
|
|
|
|
files.push_back(String::utf8(p_filev[i]));
|
|
|
|
}
|
|
|
|
os->get_main_loop()->drop_files(files);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 21:31:30 +00:00
|
|
|
void OS_JavaScript::send_notification_callback(int p_notification) {
|
|
|
|
OS_JavaScript *os = get_singleton();
|
|
|
|
if (!os) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (p_notification == MainLoop::NOTIFICATION_WM_MOUSE_ENTER || p_notification == MainLoop::NOTIFICATION_WM_MOUSE_EXIT) {
|
|
|
|
os->cursor_inside_canvas = p_notification == MainLoop::NOTIFICATION_WM_MOUSE_ENTER;
|
|
|
|
}
|
|
|
|
MainLoop *loop = os->get_main_loop();
|
|
|
|
if (loop) {
|
|
|
|
loop->notification(p_notification);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 21:31:30 +00:00
|
|
|
// Window (canvas)
|
|
|
|
|
2020-06-29 16:54:20 +00:00
|
|
|
bool OS_JavaScript::check_size_force_redraw() {
|
2021-01-30 10:35:03 +00:00
|
|
|
return godot_js_display_size_update() != 0;
|
2020-06-15 10:19:07 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 10:19:33 +00:00
|
|
|
void OS_JavaScript::fullscreen_change_callback(int p_fullscreen) {
|
2018-07-08 00:23:19 +00:00
|
|
|
OS_JavaScript *os = get_singleton();
|
2021-09-12 10:19:33 +00:00
|
|
|
os->video_mode.fullscreen = p_fullscreen;
|
2016-11-26 12:13:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 11:10:47 +00:00
|
|
|
void OS_JavaScript::window_blur_callback() {
|
2021-09-18 06:45:02 +00:00
|
|
|
get_singleton()->input->release_pressed_events();
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
|
|
|
|
video_mode = p_video_mode;
|
2017-07-22 13:43:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
OS::VideoMode OS_JavaScript::get_video_mode(int p_screen) const {
|
|
|
|
return video_mode;
|
2017-07-22 13:43:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
Size2 OS_JavaScript::get_screen_size(int p_screen) const {
|
2021-01-30 10:35:03 +00:00
|
|
|
int size[2];
|
|
|
|
godot_js_display_screen_size_get(size, size + 1);
|
|
|
|
return Size2(size[0], size[1]);
|
2017-07-22 13:43:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::set_window_size(const Size2 p_size) {
|
2018-08-12 13:39:32 +00:00
|
|
|
if (video_mode.fullscreen) {
|
2018-07-08 00:23:19 +00:00
|
|
|
set_window_fullscreen(false);
|
2017-07-22 13:43:05 +00:00
|
|
|
}
|
2021-09-12 16:14:35 +00:00
|
|
|
godot_js_display_desired_size_set(p_size.x, p_size.y);
|
2017-04-21 03:01:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
Size2 OS_JavaScript::get_window_size() const {
|
2021-01-30 10:35:03 +00:00
|
|
|
int size[2];
|
|
|
|
godot_js_display_window_size_get(size, size + 1);
|
|
|
|
return Size2(size[0], size[1]);
|
2017-04-21 03:01:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::set_window_maximized(bool p_enabled) {
|
|
|
|
}
|
2017-04-21 03:01:31 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
bool OS_JavaScript::is_window_maximized() const {
|
2021-09-12 16:14:35 +00:00
|
|
|
return false;
|
2017-04-21 03:01:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::set_window_fullscreen(bool p_enabled) {
|
2018-08-12 13:39:32 +00:00
|
|
|
if (p_enabled == video_mode.fullscreen) {
|
2018-07-08 00:23:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-24 19:41:39 +00:00
|
|
|
|
2018-08-12 13:39:32 +00:00
|
|
|
// Just request changes here, if successful, logic continues in
|
|
|
|
// fullscreen_change_callback.
|
2018-07-08 00:23:19 +00:00
|
|
|
if (p_enabled) {
|
2021-01-30 10:35:03 +00:00
|
|
|
int result = godot_js_display_fullscreen_request();
|
|
|
|
ERR_FAIL_COND_MSG(result, "The request was denied. Remember that enabling fullscreen is only possible from an input callback for the HTML5 platform.");
|
2018-07-08 00:23:19 +00:00
|
|
|
} else {
|
2018-08-12 13:39:32 +00:00
|
|
|
// No logic allowed here, since exiting w/ ESC key won't use this function.
|
2021-01-30 10:35:03 +00:00
|
|
|
ERR_FAIL_COND(godot_js_display_fullscreen_exit());
|
2018-07-08 00:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-24 19:41:39 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
bool OS_JavaScript::is_window_fullscreen() const {
|
|
|
|
return video_mode.fullscreen;
|
2017-04-24 19:41:39 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
|
|
|
|
Size2 screen = get_screen_size();
|
|
|
|
p_list->push_back(OS::VideoMode(screen.width, screen.height, true));
|
|
|
|
}
|
2017-04-24 19:41:39 +00:00
|
|
|
|
2020-02-07 00:18:11 +00:00
|
|
|
bool OS_JavaScript::get_window_per_pixel_transparency_enabled() const {
|
|
|
|
if (!is_layered_allowed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return transparency_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::set_window_per_pixel_transparency_enabled(bool p_enabled) {
|
|
|
|
if (!is_layered_allowed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
transparency_enabled = p_enabled;
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
// Keys
|
2017-04-24 19:41:39 +00:00
|
|
|
|
2021-09-10 19:46:22 +00:00
|
|
|
static void dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod) {
|
|
|
|
ev->set_shift(p_mod & 1);
|
|
|
|
ev->set_alt(p_mod & 2);
|
|
|
|
ev->set_control(p_mod & 4);
|
|
|
|
ev->set_metakey(p_mod & 8);
|
2017-04-24 19:41:39 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 19:46:22 +00:00
|
|
|
void OS_JavaScript::key_callback(int p_pressed, int p_repeat, int p_modifiers) {
|
|
|
|
OS_JavaScript *os = get_singleton();
|
|
|
|
JSKeyEvent &key_event = os->key_event;
|
|
|
|
// Resume audio context after input in case autoplay was denied.
|
|
|
|
os->resume_audio();
|
|
|
|
|
2017-06-19 13:43:46 +00:00
|
|
|
Ref<InputEventKey> ev;
|
|
|
|
ev.instance();
|
2021-09-10 19:46:22 +00:00
|
|
|
ev->set_echo(p_repeat);
|
|
|
|
ev->set_scancode(dom_code2godot_scancode(key_event.code, key_event.key, false));
|
|
|
|
ev->set_physical_scancode(dom_code2godot_scancode(key_event.code, key_event.key, true));
|
|
|
|
ev->set_pressed(p_pressed);
|
|
|
|
dom2godot_mod(ev, p_modifiers);
|
2016-04-15 22:19:02 +00:00
|
|
|
|
2021-09-10 19:46:22 +00:00
|
|
|
String unicode = String::utf8(key_event.key);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (unicode.length() == 1) {
|
2017-06-19 13:43:46 +00:00
|
|
|
ev->set_unicode(unicode[0]);
|
2016-04-15 22:19:02 +00:00
|
|
|
}
|
2018-07-08 00:23:19 +00:00
|
|
|
os->input->parse_input_event(ev);
|
2021-09-12 16:36:25 +00:00
|
|
|
|
|
|
|
// Make sure to flush all events so we can call restricted APIs inside the event.
|
|
|
|
os->input->flush_buffered_events();
|
2016-04-15 22:19:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
// Mouse
|
|
|
|
|
|
|
|
Point2 OS_JavaScript::get_mouse_position() const {
|
|
|
|
return input->get_mouse_position();
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
int OS_JavaScript::get_mouse_button_state() const {
|
|
|
|
return input->get_mouse_button_mask();
|
2017-07-22 13:43:05 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 19:46:22 +00:00
|
|
|
int OS_JavaScript::mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers) {
|
2018-07-08 00:23:19 +00:00
|
|
|
OS_JavaScript *os = get_singleton();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
Ref<InputEventMouseButton> ev;
|
|
|
|
ev.instance();
|
2021-09-10 19:46:22 +00:00
|
|
|
ev->set_pressed(p_pressed);
|
|
|
|
ev->set_position(Point2(p_x, p_y));
|
2018-07-08 00:23:19 +00:00
|
|
|
ev->set_global_position(ev->get_position());
|
2021-09-10 19:46:22 +00:00
|
|
|
ev->set_pressed(p_pressed);
|
|
|
|
dom2godot_mod(ev, p_modifiers);
|
2020-01-17 13:10:50 +00:00
|
|
|
|
2021-09-10 19:46:22 +00:00
|
|
|
switch (p_button) {
|
2021-05-04 12:35:44 +00: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;
|
|
|
|
case DOM_BUTTON_XBUTTON1:
|
|
|
|
ev->set_button_index(BUTTON_XBUTTON1);
|
|
|
|
break;
|
|
|
|
case DOM_BUTTON_XBUTTON2:
|
|
|
|
ev->set_button_index(BUTTON_XBUTTON2);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
2018-03-07 18:53:13 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-09-10 19:46:22 +00:00
|
|
|
if (p_pressed) {
|
|
|
|
uint64_t diff = (OS::get_singleton()->get_ticks_usec() / 1000) - os->last_click_ms;
|
2018-07-20 16:16:09 +00:00
|
|
|
|
|
|
|
if (ev->get_button_index() == os->last_click_button_index) {
|
|
|
|
if (diff < 400 && Point2(os->last_click_pos).distance_to(ev->get_position()) < 5) {
|
|
|
|
os->last_click_ms = 0;
|
|
|
|
os->last_click_pos = Point2(-100, -100);
|
|
|
|
os->last_click_button_index = -1;
|
|
|
|
ev->set_doubleclick(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
os->last_click_button_index = ev->get_button_index();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ev->is_doubleclick()) {
|
|
|
|
os->last_click_ms += diff;
|
|
|
|
os->last_click_pos = ev->get_position();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
int mask = os->input->get_mouse_button_mask();
|
|
|
|
int button_flag = 1 << (ev->get_button_index() - 1);
|
|
|
|
if (ev->is_pressed()) {
|
|
|
|
mask |= button_flag;
|
|
|
|
} else if (mask & button_flag) {
|
|
|
|
mask &= ~button_flag;
|
2017-11-19 14:30:09 +00:00
|
|
|
} else {
|
2018-07-08 00:23:19 +00:00
|
|
|
// Received release event, but press was outside the canvas, so ignore.
|
|
|
|
return false;
|
2017-11-19 14:30:09 +00:00
|
|
|
}
|
2018-07-08 00:23:19 +00:00
|
|
|
ev->set_button_mask(mask);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
os->input->parse_input_event(ev);
|
2021-09-10 19:46:22 +00:00
|
|
|
// Resume audio context after input in case autoplay was denied.
|
|
|
|
os->resume_audio();
|
2021-09-12 16:36:25 +00:00
|
|
|
|
|
|
|
// Make sure to flush all events so we can call restricted APIs inside the event.
|
|
|
|
os->input->flush_buffered_events();
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
// Prevent multi-click text selection and wheel-click scrolling anchor.
|
|
|
|
// Context menu is prevented through contextmenu event.
|
|
|
|
return true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 19:46:22 +00:00
|
|
|
void OS_JavaScript::mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers) {
|
2018-07-08 00:23:19 +00:00
|
|
|
OS_JavaScript *os = get_singleton();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
int input_mask = os->input->get_mouse_button_mask();
|
|
|
|
// For motion outside the canvas, only read mouse movement if dragging
|
|
|
|
// started inside the canvas; imitating desktop app behaviour.
|
2020-10-17 21:31:30 +00:00
|
|
|
if (!os->cursor_inside_canvas && !input_mask)
|
2021-09-10 19:46:22 +00:00
|
|
|
return;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
Ref<InputEventMouseMotion> ev;
|
|
|
|
ev.instance();
|
2021-09-10 19:46:22 +00:00
|
|
|
dom2godot_mod(ev, p_modifiers);
|
2018-07-08 00:23:19 +00:00
|
|
|
ev->set_button_mask(input_mask);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-09-10 19:46:22 +00:00
|
|
|
ev->set_position(Point2(p_x, p_y));
|
2018-07-08 00:23:19 +00:00
|
|
|
ev->set_global_position(ev->get_position());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-09-10 19:46:22 +00:00
|
|
|
ev->set_relative(Vector2(p_rel_x, p_rel_y));
|
2018-07-08 00:23:19 +00:00
|
|
|
ev->set_speed(os->input->get_last_mouse_speed());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
os->input->parse_input_event(ev);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 19:35:31 +00:00
|
|
|
static const char *godot2dom_cursor(OS::CursorShape p_shape) {
|
|
|
|
switch (p_shape) {
|
|
|
|
case OS::CURSOR_ARROW:
|
|
|
|
default:
|
2022-07-01 18:46:58 +00:00
|
|
|
return "default";
|
2021-05-04 12:35:44 +00:00
|
|
|
case OS::CURSOR_IBEAM:
|
|
|
|
return "text";
|
|
|
|
case OS::CURSOR_POINTING_HAND:
|
|
|
|
return "pointer";
|
|
|
|
case OS::CURSOR_CROSS:
|
|
|
|
return "crosshair";
|
|
|
|
case OS::CURSOR_WAIT:
|
|
|
|
return "wait";
|
2022-05-14 02:24:25 +00:00
|
|
|
case OS::CURSOR_BUSY:
|
|
|
|
return "progress";
|
2021-05-04 12:35:44 +00:00
|
|
|
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 19:35:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::set_cursor_shape(CursorShape p_shape) {
|
|
|
|
ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
|
2020-10-17 21:31:30 +00:00
|
|
|
if (cursor_shape == p_shape) {
|
|
|
|
return;
|
2018-11-28 16:04:37 +00:00
|
|
|
}
|
2018-07-08 00:23:19 +00:00
|
|
|
cursor_shape = p_shape;
|
2020-10-17 21:31:30 +00:00
|
|
|
godot_js_display_cursor_set_shape(godot2dom_cursor(cursor_shape));
|
2018-07-08 00:23:19 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 10:49:02 +00:00
|
|
|
OS::CursorShape OS_JavaScript::get_cursor_shape() const {
|
|
|
|
return cursor_shape;
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
|
2018-11-28 16:04:37 +00:00
|
|
|
if (p_cursor.is_valid()) {
|
|
|
|
Ref<Texture> texture = p_cursor;
|
|
|
|
Ref<AtlasTexture> atlas_texture = p_cursor;
|
|
|
|
Ref<Image> image;
|
|
|
|
Size2 texture_size;
|
|
|
|
Rect2 atlas_rect;
|
|
|
|
|
|
|
|
if (texture.is_valid()) {
|
|
|
|
image = texture->get_data();
|
2019-01-27 16:39:16 +00:00
|
|
|
if (image.is_valid()) {
|
|
|
|
image->duplicate();
|
|
|
|
}
|
2018-11-28 16:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!image.is_valid() && atlas_texture.is_valid()) {
|
|
|
|
texture = atlas_texture->get_atlas();
|
|
|
|
|
|
|
|
atlas_rect.size.width = texture->get_width();
|
|
|
|
atlas_rect.size.height = texture->get_height();
|
|
|
|
atlas_rect.position.x = atlas_texture->get_region().position.x;
|
|
|
|
atlas_rect.position.y = atlas_texture->get_region().position.y;
|
|
|
|
|
|
|
|
texture_size.width = atlas_texture->get_region().size.x;
|
|
|
|
texture_size.height = atlas_texture->get_region().size.y;
|
|
|
|
} else if (image.is_valid()) {
|
|
|
|
texture_size.width = texture->get_width();
|
|
|
|
texture_size.height = texture->get_height();
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!texture.is_valid());
|
|
|
|
ERR_FAIL_COND(p_hotspot.x < 0 || p_hotspot.y < 0);
|
|
|
|
ERR_FAIL_COND(texture_size.width > 256 || texture_size.height > 256);
|
|
|
|
ERR_FAIL_COND(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height);
|
|
|
|
|
|
|
|
image = texture->get_data();
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!image.is_valid());
|
|
|
|
|
2019-01-27 16:39:16 +00:00
|
|
|
image = image->duplicate();
|
|
|
|
|
2018-11-28 16:04:37 +00:00
|
|
|
if (atlas_texture.is_valid())
|
|
|
|
image->crop_from_point(
|
|
|
|
atlas_rect.position.x,
|
|
|
|
atlas_rect.position.y,
|
|
|
|
texture_size.width,
|
|
|
|
texture_size.height);
|
|
|
|
|
|
|
|
if (image->get_format() != Image::FORMAT_RGBA8) {
|
|
|
|
image->convert(Image::FORMAT_RGBA8);
|
|
|
|
}
|
|
|
|
|
|
|
|
png_image png_meta;
|
|
|
|
memset(&png_meta, 0, sizeof png_meta);
|
|
|
|
png_meta.version = PNG_IMAGE_VERSION;
|
|
|
|
png_meta.width = texture_size.width;
|
|
|
|
png_meta.height = texture_size.height;
|
|
|
|
png_meta.format = PNG_FORMAT_RGBA;
|
|
|
|
|
|
|
|
PoolByteArray png;
|
|
|
|
size_t len;
|
|
|
|
PoolByteArray::Read r = image->get_data().read();
|
|
|
|
ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, r.ptr(), 0, NULL));
|
|
|
|
|
|
|
|
png.resize(len);
|
|
|
|
PoolByteArray::Write w = png.write();
|
|
|
|
ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, w.ptr(), &len, 0, r.ptr(), 0, NULL));
|
|
|
|
w = PoolByteArray::Write();
|
|
|
|
|
|
|
|
r = png.read();
|
2020-10-17 21:31:30 +00:00
|
|
|
godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), r.ptr(), len, p_hotspot.x, p_hotspot.y);
|
2018-11-28 16:04:37 +00:00
|
|
|
r = PoolByteArray::Read();
|
|
|
|
|
2020-10-17 21:31:30 +00:00
|
|
|
} else {
|
|
|
|
godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), NULL, 0, 0, 0);
|
2018-11-28 16:04:37 +00:00
|
|
|
}
|
2018-07-08 00:23:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 02:54:48 +00:00
|
|
|
void OS_JavaScript::set_mouse_mode(OS::MouseMode p_mode) {
|
2022-07-29 17:41:29 +00:00
|
|
|
ERR_FAIL_COND_MSG(p_mode == MOUSE_MODE_CONFINED || p_mode == MOUSE_MODE_CONFINED_HIDDEN, "MOUSE_MODE_CONFINED is not supported for the HTML5 platform.");
|
|
|
|
if (p_mode == get_mouse_mode()) {
|
2017-04-29 02:54:48 +00:00
|
|
|
return;
|
2022-07-29 17:41:29 +00:00
|
|
|
}
|
2017-04-29 02:54:48 +00:00
|
|
|
|
|
|
|
if (p_mode == MOUSE_MODE_VISIBLE) {
|
2020-10-17 21:31:30 +00:00
|
|
|
godot_js_display_cursor_set_visible(1);
|
2021-09-12 16:13:54 +00:00
|
|
|
godot_js_display_cursor_lock_set(false);
|
2017-04-29 02:54:48 +00:00
|
|
|
|
|
|
|
} else if (p_mode == MOUSE_MODE_HIDDEN) {
|
2020-10-17 21:31:30 +00:00
|
|
|
godot_js_display_cursor_set_visible(0);
|
2021-09-12 16:13:54 +00:00
|
|
|
godot_js_display_cursor_lock_set(false);
|
2017-04-29 02:54:48 +00:00
|
|
|
|
|
|
|
} else if (p_mode == MOUSE_MODE_CAPTURED) {
|
2020-10-17 21:31:30 +00:00
|
|
|
godot_js_display_cursor_set_visible(1);
|
2021-09-12 16:13:54 +00:00
|
|
|
godot_js_display_cursor_lock_set(true);
|
2017-04-29 02:54:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OS::MouseMode OS_JavaScript::get_mouse_mode() const {
|
2021-09-12 16:13:54 +00:00
|
|
|
if (godot_js_display_cursor_is_hidden()) {
|
2017-04-29 02:54:48 +00:00
|
|
|
return MOUSE_MODE_HIDDEN;
|
2021-09-12 16:13:54 +00:00
|
|
|
}
|
|
|
|
if (godot_js_display_cursor_is_locked()) {
|
|
|
|
return MOUSE_MODE_CAPTURED;
|
|
|
|
}
|
|
|
|
return MOUSE_MODE_VISIBLE;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-11-23 22:53:38 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
// Wheel
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-09-10 19:46:22 +00:00
|
|
|
int OS_JavaScript::mouse_wheel_callback(double p_delta_x, double p_delta_y) {
|
2020-10-17 21:31:30 +00:00
|
|
|
OS_JavaScript *os = get_singleton();
|
2021-09-10 19:46:22 +00:00
|
|
|
|
2020-10-17 21:31:30 +00:00
|
|
|
if (!godot_js_display_canvas_is_focused()) {
|
|
|
|
if (os->cursor_inside_canvas) {
|
|
|
|
godot_js_display_canvas_focus();
|
2018-07-08 00:23:19 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-17 21:31:30 +00:00
|
|
|
InputDefault *input = os->input;
|
2018-07-08 00:23:19 +00:00
|
|
|
Ref<InputEventMouseButton> ev;
|
|
|
|
ev.instance();
|
|
|
|
ev->set_position(input->get_mouse_position());
|
|
|
|
ev->set_global_position(ev->get_position());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
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));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-09-10 19:46:22 +00:00
|
|
|
if (p_delta_y < 0) {
|
2018-07-08 00:23:19 +00:00
|
|
|
ev->set_button_index(BUTTON_WHEEL_UP);
|
2021-09-10 19:46:22 +00:00
|
|
|
} else if (p_delta_y > 0) {
|
2018-07-08 00:23:19 +00:00
|
|
|
ev->set_button_index(BUTTON_WHEEL_DOWN);
|
2021-09-10 19:46:22 +00:00
|
|
|
} else if (p_delta_x > 0) {
|
2018-07-08 00:23:19 +00:00
|
|
|
ev->set_button_index(BUTTON_WHEEL_LEFT);
|
2021-09-10 19:46:22 +00:00
|
|
|
} else if (p_delta_x < 0) {
|
2018-07-08 00:23:19 +00:00
|
|
|
ev->set_button_index(BUTTON_WHEEL_RIGHT);
|
2021-09-10 19:46:22 +00:00
|
|
|
} else {
|
2018-07-08 00:23:19 +00:00
|
|
|
return false;
|
2021-09-10 19:46:22 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
// Different browsers give wildly different delta values, and we can't
|
|
|
|
// interpret deltaMode, so use default value for wheel events' factor.
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 13:12:13 +00:00
|
|
|
int button_flag = 1 << (ev->get_button_index() - 1);
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
ev->set_pressed(true);
|
2018-07-08 13:12:13 +00:00
|
|
|
ev->set_button_mask(input->get_mouse_button_mask() | button_flag);
|
2018-07-08 00:23:19 +00:00
|
|
|
input->parse_input_event(ev);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-09-15 14:57:09 +00:00
|
|
|
Ref<InputEventMouseButton> release = ev->duplicate();
|
|
|
|
release->set_pressed(false);
|
|
|
|
release->set_button_mask(input->get_mouse_button_mask() & ~button_flag);
|
|
|
|
input->parse_input_event(release);
|
2018-07-08 00:23:19 +00:00
|
|
|
|
|
|
|
return true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
// Touch
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
bool OS_JavaScript::has_touchscreen_ui_hint() const {
|
2020-10-17 21:31:30 +00:00
|
|
|
return godot_js_display_touchscreen_is_available();
|
2016-11-26 12:13:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 19:46:22 +00:00
|
|
|
void OS_JavaScript::touch_callback(int p_type, int p_count) {
|
2018-07-08 00:23:19 +00:00
|
|
|
OS_JavaScript *os = get_singleton();
|
2019-01-20 13:25:15 +00:00
|
|
|
// Resume audio context after input in case autoplay was denied.
|
2020-06-29 16:51:53 +00:00
|
|
|
os->resume_audio();
|
2016-11-26 12:13:16 +00:00
|
|
|
|
2021-09-10 19:46:22 +00:00
|
|
|
const JSTouchEvent &touch_event = os->touch_event;
|
|
|
|
for (int i = 0; i < p_count; i++) {
|
|
|
|
Point2 point(touch_event.coords[i * 2], touch_event.coords[i * 2 + 1]);
|
|
|
|
if (p_type == 2) {
|
|
|
|
// touchmove
|
|
|
|
Ref<InputEventScreenDrag> ev;
|
|
|
|
ev.instance();
|
|
|
|
ev->set_index(touch_event.identifier[i]);
|
|
|
|
ev->set_position(point);
|
|
|
|
|
|
|
|
Point2 &prev = os->touches[i];
|
|
|
|
ev->set_relative(ev->get_position() - prev);
|
|
|
|
prev = ev->get_position();
|
|
|
|
|
|
|
|
os->input->parse_input_event(ev);
|
|
|
|
} else {
|
|
|
|
// touchstart/touchend
|
|
|
|
Ref<InputEventScreenTouch> ev;
|
|
|
|
ev.instance();
|
|
|
|
ev->set_index(touch_event.identifier[i]);
|
|
|
|
ev->set_position(point);
|
|
|
|
ev->set_pressed(p_type == 0);
|
|
|
|
os->touches[i] = point;
|
|
|
|
|
|
|
|
os->input->parse_input_event(ev);
|
|
|
|
|
|
|
|
// Make sure to flush all events so we can call restricted APIs inside the event.
|
|
|
|
os->input->flush_buffered_events();
|
|
|
|
}
|
2017-07-25 18:55:44 +00:00
|
|
|
}
|
2018-07-08 00:23:19 +00:00
|
|
|
}
|
2017-07-25 18:55:44 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
// Gamepad
|
2020-12-17 12:37:50 +00:00
|
|
|
void OS_JavaScript::gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid) {
|
2018-07-08 00:23:19 +00:00
|
|
|
InputDefault *input = get_singleton()->input;
|
2020-12-17 12:37:50 +00:00
|
|
|
if (p_connected) {
|
|
|
|
input->joy_connection_changed(p_index, true, String::utf8(p_id), String::utf8(p_guid));
|
2018-07-08 00:23:19 +00:00
|
|
|
} else {
|
2020-12-17 12:37:50 +00:00
|
|
|
input->joy_connection_changed(p_index, false, "");
|
2016-11-26 12:13:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::process_joypads() {
|
2021-09-12 11:01:15 +00:00
|
|
|
int32_t pads = godot_js_input_gamepad_sample_count();
|
2020-12-17 12:37:50 +00:00
|
|
|
int32_t s_btns_num = 0;
|
|
|
|
int32_t s_axes_num = 0;
|
|
|
|
int32_t s_standard = 0;
|
|
|
|
float s_btns[16];
|
|
|
|
float s_axes[10];
|
|
|
|
for (int idx = 0; idx < pads; idx++) {
|
2021-09-12 11:01:15 +00:00
|
|
|
int err = godot_js_input_gamepad_sample_get(idx, s_btns, &s_btns_num, s_axes, &s_axes_num, &s_standard);
|
2020-12-17 12:37:50 +00:00
|
|
|
if (err) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int b = 0; b < s_btns_num; b++) {
|
|
|
|
// Buttons 6 and 7 in the standard mapping need to be
|
|
|
|
// axis to be handled as JOY_ANALOG by Godot.
|
|
|
|
if (s_standard && (b == 6 || b == 7)) {
|
2021-12-25 08:45:37 +00:00
|
|
|
input->joy_axis(idx, b, s_btns[b]);
|
2020-12-17 12:37:50 +00:00
|
|
|
} else {
|
2021-12-25 08:45:37 +00:00
|
|
|
input->joy_button(idx, b, s_btns[b]);
|
2018-07-08 00:23:19 +00:00
|
|
|
}
|
2016-11-26 12:13:16 +00:00
|
|
|
}
|
2020-12-17 12:37:50 +00:00
|
|
|
for (int a = 0; a < s_axes_num; a++) {
|
2021-12-25 08:45:37 +00:00
|
|
|
input->joy_axis(idx, a, s_axes[a]);
|
2020-12-17 12:37:50 +00:00
|
|
|
}
|
2016-11-26 12:13:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
bool OS_JavaScript::is_joy_known(int p_device) {
|
|
|
|
return input->is_joy_mapped(p_device);
|
2015-04-02 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
String OS_JavaScript::get_joy_guid(int p_device) const {
|
|
|
|
return input->get_joy_guid_remapped(p_device);
|
2017-07-25 18:55:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
// Video
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
int OS_JavaScript::get_video_driver_count() const {
|
|
|
|
return VIDEO_DRIVER_MAX;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
const char *OS_JavaScript::get_video_driver_name(int p_driver) const {
|
|
|
|
switch (p_driver) {
|
|
|
|
case VIDEO_DRIVER_GLES3:
|
|
|
|
return "GLES3";
|
|
|
|
case VIDEO_DRIVER_GLES2:
|
|
|
|
return "GLES2";
|
|
|
|
}
|
2019-08-09 04:49:33 +00:00
|
|
|
ERR_FAIL_V_MSG(NULL, "Invalid video driver index: " + itos(p_driver) + ".");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
// Audio
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
int OS_JavaScript::get_audio_driver_count() const {
|
2021-09-12 17:23:30 +00:00
|
|
|
return audio_drivers.size();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
const char *OS_JavaScript::get_audio_driver_name(int p_driver) const {
|
2021-09-12 17:23:30 +00:00
|
|
|
if (audio_drivers.size() <= p_driver) {
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
return audio_drivers[p_driver]->get_name();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 10:59:29 +00:00
|
|
|
// Clipboard
|
2020-10-17 21:31:30 +00:00
|
|
|
void OS_JavaScript::update_clipboard_callback(const char *p_text) {
|
2019-05-28 20:27:03 +00:00
|
|
|
// Only call set_clipboard from OS (sets local clipboard)
|
2022-01-06 09:34:10 +00:00
|
|
|
get_singleton()->OS::set_clipboard(String::utf8(p_text));
|
2019-05-28 20:27:03 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 10:59:29 +00:00
|
|
|
void OS_JavaScript::set_clipboard(const String &p_text) {
|
|
|
|
OS::set_clipboard(p_text);
|
2020-10-17 21:31:30 +00:00
|
|
|
int err = godot_js_display_clipboard_set(p_text.utf8().get_data());
|
2019-08-09 04:49:33 +00:00
|
|
|
ERR_FAIL_COND_MSG(err, "Clipboard API is not supported.");
|
2019-05-28 10:59:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 19:30:29 +00:00
|
|
|
String OS_JavaScript::get_clipboard() const {
|
2020-10-17 21:31:30 +00:00
|
|
|
godot_js_display_clipboard_get(update_clipboard_callback);
|
2019-05-29 19:30:29 +00:00
|
|
|
return this->OS::get_clipboard();
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
// Lifecycle
|
2018-07-19 21:58:15 +00:00
|
|
|
int OS_JavaScript::get_current_video_driver() const {
|
|
|
|
return video_driver_index;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::initialize_core() {
|
|
|
|
OS_Unix::initialize_core();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
Error OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
|
2021-01-30 10:35:03 +00:00
|
|
|
video_mode = p_desired;
|
|
|
|
// fullscreen_change_callback will correct this if the request is successful.
|
|
|
|
video_mode.fullscreen = false;
|
2021-03-08 18:31:52 +00:00
|
|
|
// Handle contextmenu, webglcontextlost, initial canvas setup.
|
|
|
|
godot_js_display_setup_canvas(video_mode.width, video_mode.height, video_mode.fullscreen, is_hidpi_allowed() ? 1 : 0);
|
2021-01-30 10:35:03 +00:00
|
|
|
|
2020-10-17 21:31:30 +00:00
|
|
|
swap_ok_cancel = godot_js_display_is_swap_ok_cancel() == 1;
|
2020-07-27 11:53:03 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
EmscriptenWebGLContextAttributes attributes;
|
|
|
|
emscripten_webgl_init_context_attributes(&attributes);
|
2020-02-07 00:18:11 +00:00
|
|
|
attributes.alpha = GLOBAL_GET("display/window/per_pixel_transparency/allowed");
|
2018-07-08 00:23:19 +00:00
|
|
|
attributes.antialias = false;
|
2020-01-19 10:44:19 +00:00
|
|
|
attributes.explicitSwapControl = true;
|
2018-07-08 00:23:19 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_video_driver, VIDEO_DRIVER_MAX, ERR_INVALID_PARAMETER);
|
2018-08-24 22:04:25 +00:00
|
|
|
|
2020-02-07 00:18:11 +00:00
|
|
|
if (p_desired.layered) {
|
|
|
|
set_window_per_pixel_transparency_enabled(true);
|
|
|
|
}
|
|
|
|
|
2018-08-24 22:04:25 +00:00
|
|
|
bool gles3 = true;
|
|
|
|
if (p_video_driver == VIDEO_DRIVER_GLES2) {
|
|
|
|
gles3 = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool gl_initialization_error = false;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (gles3) {
|
2021-04-06 10:46:33 +00:00
|
|
|
if (godot_js_display_has_webgl(2) && RasterizerGLES3::is_viable() == OK) {
|
2018-08-24 22:04:25 +00:00
|
|
|
attributes.majorVersion = 2;
|
|
|
|
RasterizerGLES3::register_config();
|
|
|
|
RasterizerGLES3::make_current();
|
|
|
|
break;
|
|
|
|
} else {
|
2019-03-05 12:46:51 +00:00
|
|
|
if (GLOBAL_GET("rendering/quality/driver/fallback_to_gles2")) {
|
2018-08-24 22:04:25 +00:00
|
|
|
p_video_driver = VIDEO_DRIVER_GLES2;
|
|
|
|
gles3 = false;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
gl_initialization_error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-04-06 10:46:33 +00:00
|
|
|
if (godot_js_display_has_webgl(1) && RasterizerGLES2::is_viable() == OK) {
|
2018-08-24 22:04:25 +00:00
|
|
|
attributes.majorVersion = 1;
|
|
|
|
RasterizerGLES2::register_config();
|
|
|
|
RasterizerGLES2::make_current();
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
gl_initialization_error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-07-08 00:23:19 +00:00
|
|
|
}
|
2018-07-19 21:58:15 +00:00
|
|
|
|
2020-10-17 21:31:30 +00:00
|
|
|
webgl_ctx = emscripten_webgl_create_context(canvas_id, &attributes);
|
2020-01-19 10:44:19 +00:00
|
|
|
if (emscripten_webgl_make_context_current(webgl_ctx) != EMSCRIPTEN_RESULT_SUCCESS) {
|
2018-08-24 22:04:25 +00:00
|
|
|
gl_initialization_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gl_initialization_error) {
|
|
|
|
OS::get_singleton()->alert("Your browser does not support any of the supported WebGL versions.\n"
|
|
|
|
"Please update your browser version.",
|
|
|
|
"Unable to initialize Video driver");
|
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
video_driver_index = p_video_driver;
|
2018-07-08 00:23:19 +00:00
|
|
|
|
|
|
|
AudioDriverManager::initialize(p_audio_driver);
|
2020-01-19 10:44:19 +00:00
|
|
|
visual_server = memnew(VisualServerRaster());
|
|
|
|
#ifndef NO_THREADS
|
|
|
|
visual_server = memnew(VisualServerWrapMT(visual_server, false));
|
|
|
|
#endif
|
2018-07-08 00:23:19 +00:00
|
|
|
input = memnew(InputDefault);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-09-12 11:01:15 +00:00
|
|
|
// JS Input interface (js/libs/library_godot_input.js)
|
|
|
|
godot_js_input_mouse_button_cb(&OS_JavaScript::mouse_button_callback);
|
|
|
|
godot_js_input_mouse_move_cb(&OS_JavaScript::mouse_move_callback);
|
|
|
|
godot_js_input_mouse_wheel_cb(&OS_JavaScript::mouse_wheel_callback);
|
|
|
|
godot_js_input_touch_cb(&OS_JavaScript::touch_callback, touch_event.identifier, touch_event.coords);
|
|
|
|
godot_js_input_key_cb(&OS_JavaScript::key_callback, key_event.code, key_event.key);
|
|
|
|
godot_js_input_gamepad_cb(&OS_JavaScript::gamepad_callback);
|
|
|
|
godot_js_input_paste_cb(&OS_JavaScript::update_clipboard_callback);
|
|
|
|
godot_js_input_drop_files_cb(&OS_JavaScript::drop_files_callback);
|
|
|
|
|
|
|
|
// JS Display interface (js/libs/library_godot_display.js)
|
2021-09-12 10:19:33 +00:00
|
|
|
godot_js_display_fullscreen_cb(&OS_JavaScript::fullscreen_change_callback);
|
2021-10-05 11:10:47 +00:00
|
|
|
godot_js_display_window_blur_cb(&window_blur_callback);
|
2020-10-17 21:31:30 +00:00
|
|
|
godot_js_display_notification_cb(&OS_JavaScript::send_notification_callback,
|
|
|
|
MainLoop::NOTIFICATION_WM_MOUSE_ENTER,
|
|
|
|
MainLoop::NOTIFICATION_WM_MOUSE_EXIT,
|
|
|
|
MainLoop::NOTIFICATION_WM_FOCUS_IN,
|
|
|
|
MainLoop::NOTIFICATION_WM_FOCUS_OUT);
|
2021-03-08 22:16:51 +00:00
|
|
|
godot_js_display_vk_cb(&input_text_callback);
|
2018-07-08 00:23:19 +00:00
|
|
|
|
|
|
|
visual_server->init();
|
|
|
|
|
|
|
|
return OK;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-09-02 23:44:00 +00:00
|
|
|
|
2021-03-08 22:16:51 +00:00
|
|
|
void OS_JavaScript::input_text_callback(const char *p_text, int p_cursor) {
|
|
|
|
OS_JavaScript *os = OS_JavaScript::get_singleton();
|
|
|
|
if (!os || !os->get_main_loop()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
os->get_main_loop()->input_text(String::utf8(p_text));
|
|
|
|
Ref<InputEventKey> k;
|
|
|
|
for (int i = 0; i < p_cursor; i++) {
|
|
|
|
k.instance();
|
|
|
|
k->set_pressed(true);
|
|
|
|
k->set_echo(false);
|
|
|
|
k->set_scancode(KEY_RIGHT);
|
|
|
|
os->input->parse_input_event(k);
|
|
|
|
k.instance();
|
|
|
|
k->set_pressed(false);
|
|
|
|
k->set_echo(false);
|
|
|
|
k->set_scancode(KEY_RIGHT);
|
|
|
|
os->input->parse_input_event(k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OS_JavaScript::has_virtual_keyboard() const {
|
|
|
|
return godot_js_display_vk_available() != 0;
|
|
|
|
}
|
|
|
|
|
2022-07-07 18:22:28 +00:00
|
|
|
void OS_JavaScript::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect, VirtualKeyboardType p_type, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
|
|
|
|
godot_js_display_vk_show(p_existing_text.utf8().get_data(), p_type, p_cursor_start, p_cursor_end);
|
2021-03-08 22:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::hide_virtual_keyboard() {
|
|
|
|
godot_js_display_vk_hide();
|
|
|
|
}
|
|
|
|
|
2020-07-27 11:53:03 +00:00
|
|
|
bool OS_JavaScript::get_swap_ok_cancel() {
|
|
|
|
return swap_ok_cancel;
|
|
|
|
}
|
|
|
|
|
2020-01-19 10:44:19 +00:00
|
|
|
void OS_JavaScript::swap_buffers() {
|
|
|
|
emscripten_webgl_commit_frame();
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::set_main_loop(MainLoop *p_main_loop) {
|
|
|
|
main_loop = p_main_loop;
|
|
|
|
input->set_main_loop(p_main_loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
MainLoop *OS_JavaScript::get_main_loop() const {
|
|
|
|
return main_loop;
|
|
|
|
}
|
|
|
|
|
2020-06-29 16:51:53 +00:00
|
|
|
void OS_JavaScript::resume_audio() {
|
2021-09-12 17:23:30 +00:00
|
|
|
AudioDriverJavaScript::resume();
|
2020-06-29 16:51:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 21:31:30 +00:00
|
|
|
void OS_JavaScript::fs_sync_callback() {
|
|
|
|
get_singleton()->idb_is_syncing = false;
|
2020-09-18 17:15:53 +00:00
|
|
|
}
|
2015-09-12 13:54:47 +00:00
|
|
|
|
2020-09-18 17:15:53 +00:00
|
|
|
bool OS_JavaScript::main_loop_iterate() {
|
|
|
|
if (is_userfs_persistent() && idb_needs_sync && !idb_is_syncing) {
|
|
|
|
idb_is_syncing = true;
|
|
|
|
idb_needs_sync = false;
|
2020-10-17 21:31:30 +00:00
|
|
|
godot_js_os_fs_sync(&OS_JavaScript::fs_sync_callback);
|
2015-09-12 13:54:47 +00:00
|
|
|
}
|
2018-08-12 13:39:32 +00:00
|
|
|
|
2021-09-12 16:36:25 +00:00
|
|
|
input->flush_buffered_events();
|
|
|
|
|
2021-09-12 11:01:15 +00:00
|
|
|
if (godot_js_input_gamepad_sample() == OK) {
|
2019-01-19 18:39:17 +00:00
|
|
|
process_joypads();
|
2021-09-12 11:01:15 +00:00
|
|
|
}
|
2017-07-25 18:55:44 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return Main::iteration();
|
|
|
|
}
|
|
|
|
|
2021-02-05 18:56:56 +00:00
|
|
|
int OS_JavaScript::get_screen_dpi(int p_screen) const {
|
|
|
|
return godot_js_display_screen_dpi_get();
|
|
|
|
}
|
|
|
|
|
|
|
|
float OS_JavaScript::get_screen_scale(int p_screen) const {
|
|
|
|
return godot_js_display_pixel_ratio_get();
|
|
|
|
}
|
|
|
|
|
|
|
|
float OS_JavaScript::get_screen_max_scale() const {
|
|
|
|
return get_screen_scale();
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::delete_main_loop() {
|
|
|
|
memdelete(main_loop);
|
2020-10-17 21:31:30 +00:00
|
|
|
main_loop = NULL;
|
2020-01-19 10:44:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::finalize() {
|
|
|
|
memdelete(input);
|
2020-01-19 10:44:19 +00:00
|
|
|
visual_server->finish();
|
|
|
|
emscripten_webgl_commit_frame();
|
|
|
|
memdelete(visual_server);
|
|
|
|
emscripten_webgl_destroy_context(webgl_ctx);
|
2021-09-12 17:23:30 +00:00
|
|
|
for (int i = 0; i < audio_drivers.size(); i++) {
|
|
|
|
memdelete(audio_drivers[i]);
|
2020-06-29 16:51:53 +00:00
|
|
|
}
|
2021-09-12 17:23:30 +00:00
|
|
|
audio_drivers.clear();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
// Miscellaneous
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-12-16 11:08:09 +00:00
|
|
|
Error OS_JavaScript::execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex, bool p_open_console) {
|
2020-01-19 10:44:19 +00:00
|
|
|
Array args;
|
|
|
|
for (const List<String>::Element *E = p_arguments.front(); E; E = E->next()) {
|
|
|
|
args.push_back(E->get());
|
|
|
|
}
|
|
|
|
String json_args = JSON::print(args);
|
2020-10-17 21:31:30 +00:00
|
|
|
int failed = godot_js_os_execute(json_args.utf8().get_data());
|
2020-01-19 10:44:19 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(failed, ERR_UNAVAILABLE, "OS::execute() must be implemented in Javascript via 'engine.setOnExecute' if required.");
|
|
|
|
return OK;
|
2018-10-29 20:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Error OS_JavaScript::kill(const ProcessID &p_pid) {
|
2019-08-09 04:49:33 +00:00
|
|
|
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "OS::kill() is not available on the HTML5 platform.");
|
2018-10-29 20:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int OS_JavaScript::get_process_id() const {
|
2019-08-09 04:49:33 +00:00
|
|
|
ERR_FAIL_V_MSG(0, "OS::get_process_id() is not available on the HTML5 platform.");
|
2018-10-29 20:15:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 03:36:23 +00:00
|
|
|
bool OS_JavaScript::is_process_running(const ProcessID &p_pid) const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-15 14:48:06 +00:00
|
|
|
int OS_JavaScript::get_processor_count() const {
|
|
|
|
return godot_js_os_hw_concurrency_get();
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
bool OS_JavaScript::_check_internal_feature_support(const String &p_feature) {
|
2020-12-03 16:15:14 +00:00
|
|
|
if (p_feature == "HTML5" || p_feature == "web") {
|
2018-07-08 00:23:19 +00:00
|
|
|
return true;
|
2020-12-03 16:15:14 +00:00
|
|
|
}
|
2018-07-08 00:23:19 +00:00
|
|
|
|
|
|
|
#ifdef JAVASCRIPT_EVAL_ENABLED
|
2020-12-03 16:15:14 +00:00
|
|
|
if (p_feature == "JavaScript") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifndef NO_THREADS
|
|
|
|
if (p_feature == "threads") {
|
2018-07-08 00:23:19 +00:00
|
|
|
return true;
|
2020-12-03 16:15:14 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if WASM_GDNATIVE
|
|
|
|
if (p_feature == "wasm32") {
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-08 00:23:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::alert(const String &p_alert, const String &p_title) {
|
2020-10-17 21:31:30 +00:00
|
|
|
godot_js_display_alert(p_alert.utf8().get_data());
|
2016-11-18 17:52:44 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::set_window_title(const String &p_title) {
|
2020-10-17 21:31:30 +00:00
|
|
|
godot_js_display_window_title_set(p_title.utf8().get_data());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-16 17:37:36 +00:00
|
|
|
void OS_JavaScript::set_icon(const Ref<Image> &p_icon) {
|
|
|
|
ERR_FAIL_COND(p_icon.is_null());
|
|
|
|
Ref<Image> icon = p_icon;
|
|
|
|
if (icon->is_compressed()) {
|
|
|
|
icon = icon->duplicate();
|
2019-06-11 12:49:34 +00:00
|
|
|
ERR_FAIL_COND(icon->decompress() != OK);
|
2018-09-16 17:37:36 +00:00
|
|
|
}
|
|
|
|
if (icon->get_format() != Image::FORMAT_RGBA8) {
|
|
|
|
if (icon == p_icon)
|
|
|
|
icon = icon->duplicate();
|
|
|
|
icon->convert(Image::FORMAT_RGBA8);
|
|
|
|
}
|
|
|
|
|
|
|
|
png_image png_meta;
|
|
|
|
memset(&png_meta, 0, sizeof png_meta);
|
|
|
|
png_meta.version = PNG_IMAGE_VERSION;
|
|
|
|
png_meta.width = icon->get_width();
|
|
|
|
png_meta.height = icon->get_height();
|
|
|
|
png_meta.format = PNG_FORMAT_RGBA;
|
|
|
|
|
|
|
|
PoolByteArray png;
|
|
|
|
size_t len;
|
|
|
|
PoolByteArray::Read r = icon->get_data().read();
|
|
|
|
ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, r.ptr(), 0, NULL));
|
|
|
|
|
|
|
|
png.resize(len);
|
|
|
|
PoolByteArray::Write w = png.write();
|
|
|
|
ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, w.ptr(), &len, 0, r.ptr(), 0, NULL));
|
|
|
|
w = PoolByteArray::Write();
|
|
|
|
|
|
|
|
r = png.read();
|
2020-10-17 21:31:30 +00:00
|
|
|
godot_js_display_window_icon_set(r.ptr(), len);
|
2018-09-16 17:37:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 17:52:44 +00:00
|
|
|
String OS_JavaScript::get_executable_path() const {
|
2017-03-28 01:21:21 +00:00
|
|
|
return OS::get_executable_path();
|
2016-11-18 17:52:44 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
Error OS_JavaScript::shell_open(String p_uri) {
|
|
|
|
// Open URI in a new tab, browser will deal with it by protocol.
|
2020-10-17 21:31:30 +00:00
|
|
|
godot_js_os_shell_open(p_uri.utf8().get_data());
|
2018-07-08 00:23:19 +00:00
|
|
|
return OK;
|
2015-09-12 13:54:47 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-05-20 17:36:24 +00:00
|
|
|
String OS_JavaScript::get_name() const {
|
2018-07-08 00:23:19 +00:00
|
|
|
return "HTML5";
|
|
|
|
}
|
2016-01-21 01:23:15 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
bool OS_JavaScript::can_draw() const {
|
|
|
|
return true; // Always?
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 10:51:40 +00:00
|
|
|
void OS_JavaScript::vibrate_handheld(int p_duration_ms) {
|
|
|
|
godot_js_input_vibrate_handheld(p_duration_ms);
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
String OS_JavaScript::get_user_data_dir() const {
|
|
|
|
return "/userfs";
|
|
|
|
};
|
2016-01-21 01:23:15 +00:00
|
|
|
|
2020-01-19 10:44:19 +00:00
|
|
|
String OS_JavaScript::get_cache_path() const {
|
|
|
|
return "/home/web_user/.cache";
|
|
|
|
}
|
|
|
|
|
|
|
|
String OS_JavaScript::get_config_path() const {
|
|
|
|
return "/home/web_user/.config";
|
|
|
|
}
|
|
|
|
|
|
|
|
String OS_JavaScript::get_data_path() const {
|
|
|
|
return "/home/web_user/.local/share";
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 19:09:06 +00:00
|
|
|
OS::PowerState OS_JavaScript::get_power_state() {
|
2020-01-19 10:44:19 +00:00
|
|
|
WARN_PRINT_ONCE("Power management is not supported for the HTML5 platform, defaulting to POWERSTATE_UNKNOWN");
|
2018-03-21 14:51:44 +00:00
|
|
|
return OS::POWERSTATE_UNKNOWN;
|
2016-07-23 11:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int OS_JavaScript::get_power_seconds_left() {
|
2020-01-19 10:44:19 +00:00
|
|
|
WARN_PRINT_ONCE("Power management is not supported for the HTML5 platform, defaulting to -1");
|
2018-03-21 14:51:44 +00:00
|
|
|
return -1;
|
2016-07-23 11:15:55 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-07-23 11:15:55 +00:00
|
|
|
int OS_JavaScript::get_power_percent_left() {
|
2020-01-19 10:44:19 +00:00
|
|
|
WARN_PRINT_ONCE("Power management is not supported for the HTML5 platform, defaulting to -1");
|
2018-03-21 14:51:44 +00:00
|
|
|
return -1;
|
2016-07-23 11:15:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
void OS_JavaScript::file_access_close_callback(const String &p_file, int p_flags) {
|
|
|
|
OS_JavaScript *os = get_singleton();
|
2020-09-18 17:15:53 +00:00
|
|
|
|
|
|
|
if (!(os->is_userfs_persistent() && p_flags & FileAccess::WRITE)) {
|
|
|
|
return; // FS persistence is not working or we are not writing.
|
|
|
|
}
|
|
|
|
bool is_file_persistent = p_file.begins_with("/userfs");
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
// Hack for editor persistence (can we track).
|
|
|
|
is_file_persistent = is_file_persistent || p_file.begins_with("/home/web_user/");
|
|
|
|
#endif
|
|
|
|
if (is_file_persistent) {
|
|
|
|
os->idb_needs_sync = true;
|
2018-07-08 00:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-11 23:15:21 +00:00
|
|
|
|
2022-01-31 14:28:12 +00:00
|
|
|
void OS_JavaScript::update_pwa_state_callback() {
|
|
|
|
if (OS_JavaScript::get_singleton()) {
|
|
|
|
OS_JavaScript::get_singleton()->pwa_is_waiting = true;
|
|
|
|
}
|
|
|
|
if (JavaScript::get_singleton()) {
|
|
|
|
JavaScript::get_singleton()->emit_signal("pwa_update_available");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Error OS_JavaScript::pwa_update() {
|
|
|
|
return godot_js_pwa_update() ? FAILED : OK;
|
|
|
|
}
|
|
|
|
|
2023-01-09 20:30:42 +00:00
|
|
|
void OS_JavaScript::force_fs_sync() {
|
|
|
|
if (is_userfs_persistent()) {
|
|
|
|
idb_needs_sync = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
bool OS_JavaScript::is_userfs_persistent() const {
|
|
|
|
return idb_available;
|
2017-10-02 14:09:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 23:43:02 +00:00
|
|
|
Error OS_JavaScript::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) {
|
|
|
|
String path = p_path.get_file();
|
|
|
|
p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
|
|
|
|
ERR_FAIL_COND_V_MSG(!p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + p_path + ". Error: " + dlerror());
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
OS_JavaScript *OS_JavaScript::get_singleton() {
|
|
|
|
return static_cast<OS_JavaScript *>(OS::get_singleton());
|
2017-10-02 14:09:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 21:31:30 +00:00
|
|
|
OS_JavaScript::OS_JavaScript() {
|
|
|
|
// Expose method for requesting quit.
|
|
|
|
godot_js_os_request_quit_cb(&request_quit_callback);
|
|
|
|
// Set canvas ID
|
|
|
|
godot_js_config_canvas_id_get(canvas_id, sizeof(canvas_id));
|
2018-07-08 00:23:19 +00:00
|
|
|
|
2020-10-17 21:31:30 +00:00
|
|
|
cursor_inside_canvas = true;
|
|
|
|
cursor_shape = OS::CURSOR_ARROW;
|
2018-01-06 23:04:09 +00:00
|
|
|
|
2018-07-20 16:16:09 +00:00
|
|
|
last_click_button_index = -1;
|
|
|
|
last_click_ms = 0;
|
|
|
|
last_click_pos = Point2(-100, -100);
|
|
|
|
|
2020-02-07 00:18:11 +00:00
|
|
|
transparency_enabled = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
main_loop = NULL;
|
2020-01-19 10:44:19 +00:00
|
|
|
visual_server = NULL;
|
2018-07-08 00:23:19 +00:00
|
|
|
|
2020-07-27 11:53:03 +00:00
|
|
|
swap_ok_cancel = false;
|
2020-10-17 21:31:30 +00:00
|
|
|
idb_available = godot_js_os_fs_is_persistent() != 0;
|
2020-09-18 17:15:53 +00:00
|
|
|
idb_needs_sync = false;
|
|
|
|
idb_is_syncing = false;
|
2022-01-31 14:28:12 +00:00
|
|
|
pwa_is_waiting = false;
|
|
|
|
godot_js_pwa_cb(&OS_JavaScript::update_pwa_state_callback);
|
2015-09-12 13:54:47 +00:00
|
|
|
|
2020-06-29 16:51:53 +00:00
|
|
|
if (AudioDriverJavaScript::is_available()) {
|
2021-09-12 17:23:30 +00:00
|
|
|
#ifdef NO_THREADS
|
|
|
|
audio_drivers.push_back(memnew(AudioDriverScriptProcessor));
|
|
|
|
#endif
|
|
|
|
audio_drivers.push_back(memnew(AudioDriverWorklet));
|
|
|
|
}
|
|
|
|
for (int i = 0; i < audio_drivers.size(); i++) {
|
|
|
|
AudioDriverManager::add_driver(audio_drivers[i]);
|
2020-06-29 16:51:53 +00:00
|
|
|
}
|
2018-01-06 23:04:09 +00:00
|
|
|
|
|
|
|
Vector<Logger *> loggers;
|
|
|
|
loggers.push_back(memnew(StdLogger));
|
|
|
|
_set_logger(memnew(CompositeLogger(loggers)));
|
2018-03-04 17:18:05 +00:00
|
|
|
|
2018-07-08 00:23:19 +00:00
|
|
|
FileAccessUnix::close_notification_func = file_access_close_callback;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|