display_server_web.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**************************************************************************/
  2. /* display_server_web.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef DISPLAY_SERVER_WEB_H
  31. #define DISPLAY_SERVER_WEB_H
  32. #include "servers/display_server.h"
  33. #include "godot_js.h"
  34. #include <emscripten.h>
  35. #include <emscripten/html5.h>
  36. class DisplayServerWeb : public DisplayServer {
  37. // No need to register with GDCLASS, it's platform-specific and nothing is added.
  38. private:
  39. struct JSTouchEvent {
  40. uint32_t identifier[32] = { 0 };
  41. double coords[64] = { 0 };
  42. };
  43. JSTouchEvent touch_event;
  44. struct JSKeyEvent {
  45. char code[32] = { 0 };
  46. char key[32] = { 0 };
  47. uint8_t modifiers[4] = { 0 };
  48. };
  49. JSKeyEvent key_event;
  50. #ifdef GLES3_ENABLED
  51. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE webgl_ctx = 0;
  52. #endif
  53. HashMap<int, CharString> utterance_ids;
  54. WindowMode window_mode = WINDOW_MODE_WINDOWED;
  55. ObjectID window_attached_instance_id = {};
  56. Callable rect_changed_callback;
  57. Callable window_event_callback;
  58. Callable input_event_callback;
  59. Callable input_text_callback;
  60. Callable drop_files_callback;
  61. String clipboard;
  62. Point2 touches[32];
  63. Array voices;
  64. char canvas_id[256] = { 0 };
  65. bool cursor_inside_canvas = true;
  66. CursorShape cursor_shape = CURSOR_ARROW;
  67. Point2i last_click_pos = Point2(-100, -100); // TODO check this again.
  68. uint64_t last_click_ms = 0;
  69. MouseButton last_click_button_index = MouseButton::NONE;
  70. bool ime_active = false;
  71. bool ime_started = false;
  72. String ime_text;
  73. Vector2i ime_selection;
  74. struct KeyEvent {
  75. bool pressed = false;
  76. bool echo = false;
  77. bool raw = false;
  78. Key keycode = Key::NONE;
  79. Key physical_keycode = Key::NONE;
  80. Key key_label = Key::NONE;
  81. uint32_t unicode = 0;
  82. KeyLocation location = KeyLocation::UNSPECIFIED;
  83. int mod = 0;
  84. };
  85. Vector<KeyEvent> key_event_buffer;
  86. int key_event_pos = 0;
  87. bool swap_cancel_ok = false;
  88. bool tts = false;
  89. NativeMenu *native_menu = nullptr;
  90. // utilities
  91. static void dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod, Key p_keycode);
  92. static const char *godot2dom_cursor(DisplayServer::CursorShape p_shape);
  93. // events
  94. WASM_EXPORT static void fullscreen_change_callback(int p_fullscreen);
  95. static void _fullscreen_change_callback(int p_fullscreen);
  96. WASM_EXPORT static int mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers);
  97. static int _mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers);
  98. WASM_EXPORT static void mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers);
  99. static void _mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers);
  100. WASM_EXPORT static int mouse_wheel_callback(double p_delta_x, double p_delta_y);
  101. static int _mouse_wheel_callback(double p_delta_x, double p_delta_y);
  102. WASM_EXPORT static void touch_callback(int p_type, int p_count);
  103. static void _touch_callback(int p_type, int p_count);
  104. WASM_EXPORT static void key_callback(int p_pressed, int p_repeat, int p_modifiers);
  105. static void _key_callback(const String &p_key_event_code, const String &p_key_event_key, int p_pressed, int p_repeat, int p_modifiers);
  106. WASM_EXPORT static void vk_input_text_callback(const char *p_text, int p_cursor);
  107. static void _vk_input_text_callback(const String &p_text, int p_cursor);
  108. WASM_EXPORT static void gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid);
  109. static void _gamepad_callback(int p_index, int p_connected, const String &p_id, const String &p_guid);
  110. WASM_EXPORT static void js_utterance_callback(int p_event, int p_id, int p_pos);
  111. static void _js_utterance_callback(int p_event, int p_id, int p_pos);
  112. WASM_EXPORT static void ime_callback(int p_type, const char *p_text);
  113. static void _ime_callback(int p_type, const String &p_text);
  114. WASM_EXPORT static void request_quit_callback();
  115. static void _request_quit_callback();
  116. WASM_EXPORT static void window_blur_callback();
  117. static void _window_blur_callback();
  118. WASM_EXPORT static void update_voices_callback(int p_size, const char **p_voice);
  119. static void _update_voices_callback(const Vector<String> &p_voices);
  120. WASM_EXPORT static void update_clipboard_callback(const char *p_text);
  121. static void _update_clipboard_callback(const String &p_text);
  122. WASM_EXPORT static void send_window_event_callback(int p_notification);
  123. static void _send_window_event_callback(int p_notification);
  124. WASM_EXPORT static void drop_files_js_callback(const char **p_filev, int p_filec);
  125. static void _drop_files_js_callback(const Vector<String> &p_files);
  126. void process_joypads();
  127. void process_keys();
  128. static Vector<String> get_rendering_drivers_func();
  129. 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, int p_screen, Context p_context, Error &r_error);
  130. static void _dispatch_input_event(const Ref<InputEvent> &p_event);
  131. protected:
  132. int get_current_video_driver() const;
  133. public:
  134. // Override return type to make writing static callbacks less tedious.
  135. static DisplayServerWeb *get_singleton();
  136. // utilities
  137. bool check_size_force_redraw();
  138. // from DisplayServer
  139. virtual bool has_feature(Feature p_feature) const override;
  140. virtual String get_name() const override;
  141. // tts
  142. virtual bool tts_is_speaking() const override;
  143. virtual bool tts_is_paused() const override;
  144. virtual TypedArray<Dictionary> tts_get_voices() const override;
  145. 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;
  146. virtual void tts_pause() override;
  147. virtual void tts_resume() override;
  148. virtual void tts_stop() override;
  149. // cursor
  150. virtual void cursor_set_shape(CursorShape p_shape) override;
  151. virtual CursorShape cursor_get_shape() const override;
  152. virtual void cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape = CURSOR_ARROW, const Vector2 &p_hotspot = Vector2()) override;
  153. // mouse
  154. virtual void mouse_set_mode(MouseMode p_mode) override;
  155. virtual MouseMode mouse_get_mode() const override;
  156. virtual Point2i mouse_get_position() const override;
  157. // ime
  158. virtual void window_set_ime_active(const bool p_active, WindowID p_window = MAIN_WINDOW_ID) override;
  159. virtual void window_set_ime_position(const Point2i &p_pos, WindowID p_window = MAIN_WINDOW_ID) override;
  160. virtual Point2i ime_get_selection() const override;
  161. virtual String ime_get_text() const override;
  162. // touch
  163. virtual bool is_touchscreen_available() const override;
  164. // clipboard
  165. virtual void clipboard_set(const String &p_text) override;
  166. virtual String clipboard_get() const override;
  167. // screen
  168. virtual int get_screen_count() const override;
  169. virtual int get_primary_screen() const override;
  170. virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  171. virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  172. virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  173. virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  174. virtual float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  175. virtual float screen_get_refresh_rate(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  176. virtual void screen_set_keep_on(bool p_enable) override {}
  177. 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;
  178. virtual void virtual_keyboard_hide() override;
  179. // windows
  180. virtual Vector<DisplayServer::WindowID> get_window_list() const override;
  181. virtual WindowID get_window_at_screen_position(const Point2i &p_position) const override;
  182. virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID) override;
  183. virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const override;
  184. virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  185. virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  186. virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  187. virtual void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  188. virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  189. virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) override;
  190. virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const override;
  191. virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID) override;
  192. virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const override;
  193. virtual Point2i window_get_position_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
  194. virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID) override;
  195. virtual void window_set_transient(WindowID p_window, WindowID p_parent) override;
  196. virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  197. virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  198. virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  199. virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  200. virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  201. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  202. virtual Size2i window_get_size_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
  203. virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID) override;
  204. virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const override;
  205. virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const override;
  206. virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID) override;
  207. virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const override;
  208. virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID) override;
  209. virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID) override;
  210. virtual bool window_is_focused(WindowID p_window = MAIN_WINDOW_ID) const override;
  211. virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const override;
  212. virtual bool can_any_window_draw() const override;
  213. virtual void window_set_vsync_mode(VSyncMode p_vsync_mode, WindowID p_window = MAIN_WINDOW_ID) override {}
  214. virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override;
  215. // events
  216. virtual void process_events() override;
  217. // icon
  218. virtual void set_icon(const Ref<Image> &p_icon) override;
  219. // others
  220. virtual bool get_swap_cancel_ok() override;
  221. virtual void swap_buffers() override;
  222. static void register_web_driver();
  223. 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, int p_screen, Context p_context, Error &r_error);
  224. ~DisplayServerWeb();
  225. };
  226. #endif // DISPLAY_SERVER_WEB_H