2020-05-01 14:45:45 +02:00
|
|
|
/*************************************************************************/
|
2022-08-28 20:27:45 +02:00
|
|
|
/* display_server_web.h */
|
2020-05-01 14:45:45 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2020-05-01 14:45:45 +02: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
#ifndef DISPLAY_SERVER_WEB_H
|
|
|
|
#define DISPLAY_SERVER_WEB_H
|
2020-05-01 14:45:45 +02:00
|
|
|
|
|
|
|
#include "servers/display_server.h"
|
|
|
|
|
|
|
|
#include <emscripten.h>
|
|
|
|
#include <emscripten/html5.h>
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
class DisplayServerWeb : public DisplayServer {
|
2020-10-23 18:33:20 +02:00
|
|
|
private:
|
2021-09-10 21:46:22 +02:00
|
|
|
struct JSTouchEvent {
|
|
|
|
uint32_t identifier[32] = { 0 };
|
|
|
|
double coords[64] = { 0 };
|
|
|
|
};
|
|
|
|
JSTouchEvent touch_event;
|
|
|
|
|
|
|
|
struct JSKeyEvent {
|
|
|
|
char code[32] = { 0 };
|
|
|
|
char key[32] = { 0 };
|
|
|
|
uint8_t modifiers[4] = { 0 };
|
|
|
|
};
|
|
|
|
JSKeyEvent key_event;
|
|
|
|
|
2021-10-25 19:16:40 +02:00
|
|
|
#ifdef GLES3_ENABLED
|
|
|
|
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE webgl_ctx = 0;
|
|
|
|
#endif
|
|
|
|
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<int, CharString> utterance_ids;
|
2021-11-04 13:33:37 +01:00
|
|
|
|
2020-10-23 18:33:20 +02:00
|
|
|
WindowMode window_mode = WINDOW_MODE_WINDOWED;
|
2020-05-01 14:45:45 +02:00
|
|
|
ObjectID window_attached_instance_id = {};
|
|
|
|
|
2020-10-23 18:33:20 +02:00
|
|
|
Callable window_event_callback;
|
|
|
|
Callable input_event_callback;
|
|
|
|
Callable input_text_callback;
|
|
|
|
Callable drop_files_callback;
|
|
|
|
|
|
|
|
String clipboard;
|
2020-05-01 14:45:45 +02:00
|
|
|
Point2 touches[32];
|
|
|
|
|
2021-11-04 13:33:37 +01:00
|
|
|
Array voices;
|
|
|
|
|
2020-10-23 18:33:20 +02:00
|
|
|
char canvas_id[256] = { 0 };
|
|
|
|
bool cursor_inside_canvas = true;
|
|
|
|
CursorShape cursor_shape = CURSOR_ARROW;
|
2020-05-01 14:45:45 +02:00
|
|
|
Point2i last_click_pos = Point2(-100, -100); // TODO check this again.
|
2021-09-10 21:46:22 +02:00
|
|
|
uint64_t last_click_ms = 0;
|
2021-08-13 23:31:57 +02:00
|
|
|
MouseButton last_click_button_index = MouseButton::NONE;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2020-07-27 13:53:03 +02:00
|
|
|
bool swap_cancel_ok = false;
|
|
|
|
|
2020-07-01 02:23:22 +02:00
|
|
|
// utilities
|
2021-09-10 21:46:22 +02:00
|
|
|
static void dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod);
|
2020-07-01 02:23:22 +02:00
|
|
|
static const char *godot2dom_cursor(DisplayServer::CursorShape p_shape);
|
|
|
|
|
|
|
|
// events
|
2021-09-12 12:19:33 +02:00
|
|
|
static void fullscreen_change_callback(int p_fullscreen);
|
2021-09-10 21:46:22 +02:00
|
|
|
static int mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers);
|
|
|
|
static void mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers);
|
|
|
|
static int mouse_wheel_callback(double p_delta_x, double p_delta_y);
|
|
|
|
static void touch_callback(int p_type, int p_count);
|
|
|
|
static void key_callback(int p_pressed, int p_repeat, int p_modifiers);
|
2021-03-08 23:16:51 +01:00
|
|
|
static void vk_input_text_callback(const char *p_text, int p_cursor);
|
2020-12-27 14:15:43 +01:00
|
|
|
static void gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid);
|
2020-05-01 14:45:45 +02:00
|
|
|
void process_joypads();
|
2021-11-04 13:33:37 +01:00
|
|
|
static void _js_utterance_callback(int p_event, int p_id, int p_pos);
|
2020-05-01 14:45:45 +02:00
|
|
|
|
|
|
|
static Vector<String> get_rendering_drivers_func();
|
2022-10-23 12:34:41 +02:00
|
|
|
static DisplayServer *create_func(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, Error &r_error);
|
2020-05-01 14:45:45 +02:00
|
|
|
|
|
|
|
static void _dispatch_input_event(const Ref<InputEvent> &p_event);
|
|
|
|
|
2020-10-23 18:33:20 +02:00
|
|
|
static void request_quit_callback();
|
2021-10-05 12:54:42 +02:00
|
|
|
static void window_blur_callback();
|
2021-11-04 13:33:37 +01:00
|
|
|
static void update_voices_callback(int p_size, const char **p_voice);
|
2020-10-23 18:33:20 +02:00
|
|
|
static void update_clipboard_callback(const char *p_text);
|
|
|
|
static void send_window_event_callback(int p_notification);
|
|
|
|
static void drop_files_js_callback(char **p_filev, int p_filec);
|
|
|
|
|
2020-05-01 14:45:45 +02:00
|
|
|
protected:
|
2020-08-06 16:20:05 +02:00
|
|
|
int get_current_video_driver() const;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Override return type to make writing static callbacks less tedious.
|
2022-08-28 20:27:45 +02:00
|
|
|
static DisplayServerWeb *get_singleton();
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2020-06-29 18:54:20 +02:00
|
|
|
// utilities
|
|
|
|
bool check_size_force_redraw();
|
|
|
|
|
2020-05-01 14:45:45 +02:00
|
|
|
// from DisplayServer
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual bool has_feature(Feature p_feature) const override;
|
|
|
|
virtual String get_name() const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-11-04 13:33:37 +01:00
|
|
|
// tts
|
|
|
|
virtual bool tts_is_speaking() const override;
|
|
|
|
virtual bool tts_is_paused() const override;
|
2022-08-05 03:41:48 +02:00
|
|
|
virtual TypedArray<Dictionary> tts_get_voices() const override;
|
2021-11-04 13:33:37 +01:00
|
|
|
|
|
|
|
virtual void tts_speak(const String &p_text, const String &p_voice, int p_volume = 50, float p_pitch = 1.f, float p_rate = 1.f, int p_utterance_id = 0, bool p_interrupt = false) override;
|
|
|
|
virtual void tts_pause() override;
|
|
|
|
virtual void tts_resume() override;
|
|
|
|
virtual void tts_stop() override;
|
|
|
|
|
2020-05-01 14:45:45 +02:00
|
|
|
// cursor
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void cursor_set_shape(CursorShape p_shape) override;
|
|
|
|
virtual CursorShape cursor_get_shape() const override;
|
2022-05-03 01:43:50 +02:00
|
|
|
virtual void cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape = CURSOR_ARROW, const Vector2 &p_hotspot = Vector2()) override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
|
|
|
// mouse
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void mouse_set_mode(MouseMode p_mode) override;
|
|
|
|
virtual MouseMode mouse_get_mode() const override;
|
2021-10-25 19:16:40 +02:00
|
|
|
virtual Point2i mouse_get_position() const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
|
|
|
// touch
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
|
|
|
// clipboard
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void clipboard_set(const String &p_text) override;
|
|
|
|
virtual String clipboard_get() const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
|
|
|
// screen
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual int get_screen_count() const override;
|
|
|
|
virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
|
|
|
|
virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
|
|
|
|
virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
|
|
|
|
virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
|
|
|
|
virtual float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
|
2022-01-27 20:46:57 +01:00
|
|
|
virtual float screen_get_refresh_rate(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2022-07-07 20:20:10 +02:00
|
|
|
virtual void virtual_keyboard_show(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2(), VirtualKeyboardType p_type = KEYBOARD_TYPE_DEFAULT, int p_max_input_length = -1, int p_cursor_start = -1, int p_cursor_end = -1) override;
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void virtual_keyboard_hide() override;
|
2021-03-08 23:16:51 +01:00
|
|
|
|
2020-05-01 14:45:45 +02:00
|
|
|
// windows
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual Vector<DisplayServer::WindowID> get_window_list() const override;
|
|
|
|
virtual WindowID get_window_at_screen_position(const Point2i &p_position) const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID) override;
|
|
|
|
virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
|
|
|
|
virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
|
|
|
|
virtual void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const override;
|
|
|
|
virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID) override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const override;
|
|
|
|
virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID) override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void window_set_transient(WindowID p_window, WindowID p_parent) override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
|
|
|
|
virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
|
|
|
|
virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
|
|
|
|
virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override;
|
|
|
|
virtual Size2i window_get_real_size(WindowID p_window = MAIN_WINDOW_ID) const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID) override;
|
|
|
|
virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID) override;
|
|
|
|
virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID) override;
|
|
|
|
virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID) override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual bool can_any_window_draw() const override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
|
|
|
// events
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void process_events() override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
|
|
|
// icon
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual void set_icon(const Ref<Image> &p_icon) override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
|
|
|
// others
|
2021-07-16 21:39:13 +02:00
|
|
|
virtual bool get_swap_cancel_ok() override;
|
|
|
|
virtual void swap_buffers() override;
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
static void register_web_driver();
|
2022-10-23 12:34:41 +02:00
|
|
|
DisplayServerWeb(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Point2i *p_position, const Size2i &p_resolution, Error &r_error);
|
2022-08-28 20:27:45 +02:00
|
|
|
~DisplayServerWeb();
|
2020-05-01 14:45:45 +02:00
|
|
|
};
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
#endif // DISPLAY_SERVER_WEB_H
|