display_server_javascript.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*************************************************************************/
  2. /* display_server_javascript.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_JAVASCRIPT_H
  31. #define DISPLAY_SERVER_JAVASCRIPT_H
  32. #include "servers/display_server.h"
  33. #include <emscripten.h>
  34. #include <emscripten/html5.h>
  35. class DisplayServerJavaScript : public DisplayServer {
  36. private:
  37. struct JSTouchEvent {
  38. uint32_t identifier[32] = { 0 };
  39. double coords[64] = { 0 };
  40. };
  41. JSTouchEvent touch_event;
  42. struct JSKeyEvent {
  43. char code[32] = { 0 };
  44. char key[32] = { 0 };
  45. uint8_t modifiers[4] = { 0 };
  46. };
  47. JSKeyEvent key_event;
  48. #ifdef GLES3_ENABLED
  49. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE webgl_ctx = 0;
  50. #endif
  51. HashMap<int, CharString> utterance_ids;
  52. WindowMode window_mode = WINDOW_MODE_WINDOWED;
  53. ObjectID window_attached_instance_id = {};
  54. Callable window_event_callback;
  55. Callable input_event_callback;
  56. Callable input_text_callback;
  57. Callable drop_files_callback;
  58. String clipboard;
  59. Point2 touches[32];
  60. Array voices;
  61. char canvas_id[256] = { 0 };
  62. bool cursor_inside_canvas = true;
  63. CursorShape cursor_shape = CURSOR_ARROW;
  64. Point2i last_click_pos = Point2(-100, -100); // TODO check this again.
  65. uint64_t last_click_ms = 0;
  66. MouseButton last_click_button_index = MouseButton::NONE;
  67. bool swap_cancel_ok = false;
  68. // utilities
  69. static void dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod);
  70. static const char *godot2dom_cursor(DisplayServer::CursorShape p_shape);
  71. // events
  72. static void fullscreen_change_callback(int p_fullscreen);
  73. static int mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers);
  74. static void mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers);
  75. static int mouse_wheel_callback(double p_delta_x, double p_delta_y);
  76. static void touch_callback(int p_type, int p_count);
  77. static void key_callback(int p_pressed, int p_repeat, int p_modifiers);
  78. static void vk_input_text_callback(const char *p_text, int p_cursor);
  79. static void gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid);
  80. void process_joypads();
  81. static void _js_utterance_callback(int p_event, int p_id, int p_pos);
  82. static Vector<String> get_rendering_drivers_func();
  83. static DisplayServer *create_func(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error);
  84. static void _dispatch_input_event(const Ref<InputEvent> &p_event);
  85. static void request_quit_callback();
  86. static void window_blur_callback();
  87. static void update_voices_callback(int p_size, const char **p_voice);
  88. static void update_clipboard_callback(const char *p_text);
  89. static void send_window_event_callback(int p_notification);
  90. static void drop_files_js_callback(char **p_filev, int p_filec);
  91. protected:
  92. int get_current_video_driver() const;
  93. public:
  94. // Override return type to make writing static callbacks less tedious.
  95. static DisplayServerJavaScript *get_singleton();
  96. // utilities
  97. bool check_size_force_redraw();
  98. // from DisplayServer
  99. virtual bool has_feature(Feature p_feature) const override;
  100. virtual String get_name() const override;
  101. // tts
  102. virtual bool tts_is_speaking() const override;
  103. virtual bool tts_is_paused() const override;
  104. virtual TypedArray<Dictionary> tts_get_voices() const override;
  105. 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;
  106. virtual void tts_pause() override;
  107. virtual void tts_resume() override;
  108. virtual void tts_stop() override;
  109. // cursor
  110. virtual void cursor_set_shape(CursorShape p_shape) override;
  111. virtual CursorShape cursor_get_shape() const override;
  112. virtual void cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape = CURSOR_ARROW, const Vector2 &p_hotspot = Vector2()) override;
  113. // mouse
  114. virtual void mouse_set_mode(MouseMode p_mode) override;
  115. virtual MouseMode mouse_get_mode() const override;
  116. virtual Point2i mouse_get_position() const override;
  117. // touch
  118. virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  119. // clipboard
  120. virtual void clipboard_set(const String &p_text) override;
  121. virtual String clipboard_get() const override;
  122. // screen
  123. virtual int get_screen_count() const override;
  124. virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  125. virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  126. virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  127. virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  128. virtual float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  129. virtual float screen_get_refresh_rate(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  130. 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;
  131. virtual void virtual_keyboard_hide() override;
  132. // windows
  133. virtual Vector<DisplayServer::WindowID> get_window_list() const override;
  134. virtual WindowID get_window_at_screen_position(const Point2i &p_position) const override;
  135. virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID) override;
  136. virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const override;
  137. virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  138. virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  139. virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  140. virtual void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  141. virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  142. virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) override;
  143. virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const override;
  144. virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID) override;
  145. virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const override;
  146. virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID) override;
  147. virtual void window_set_transient(WindowID p_window, WindowID p_parent) override;
  148. virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  149. virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  150. virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  151. virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  152. virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  153. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  154. virtual Size2i window_get_real_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  155. virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID) override;
  156. virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const override;
  157. virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const override;
  158. virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID) override;
  159. virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const override;
  160. virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID) override;
  161. virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID) override;
  162. virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const override;
  163. virtual bool can_any_window_draw() const override;
  164. // events
  165. virtual void process_events() override;
  166. // icon
  167. virtual void set_icon(const Ref<Image> &p_icon) override;
  168. // others
  169. virtual bool get_swap_cancel_ok() override;
  170. virtual void swap_buffers() override;
  171. static void register_javascript_driver();
  172. DisplayServerJavaScript(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Size2i &p_resolution, Error &r_error);
  173. ~DisplayServerJavaScript();
  174. };
  175. #endif // DISPLAY_SERVER_JAVASCRIPT_H