display_server_javascript.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. WindowMode window_mode = WINDOW_MODE_WINDOWED;
  38. ObjectID window_attached_instance_id = {};
  39. Callable window_event_callback;
  40. Callable input_event_callback;
  41. Callable input_text_callback;
  42. Callable drop_files_callback;
  43. String clipboard;
  44. Ref<InputEventKey> deferred_key_event;
  45. Point2 touches[32];
  46. char canvas_id[256] = { 0 };
  47. bool cursor_inside_canvas = true;
  48. CursorShape cursor_shape = CURSOR_ARROW;
  49. Point2i last_click_pos = Point2(-100, -100); // TODO check this again.
  50. double last_click_ms = 0;
  51. int last_click_button_index = -1;
  52. bool swap_cancel_ok = false;
  53. // utilities
  54. static Point2 compute_position_in_canvas(int p_x, int p_y);
  55. static void focus_canvas();
  56. static bool is_canvas_focused();
  57. template <typename T>
  58. static void dom2godot_mod(T *emscripten_event_ptr, Ref<InputEventWithModifiers> godot_event);
  59. static Ref<InputEventKey> setup_key_event(const EmscriptenKeyboardEvent *emscripten_event);
  60. static const char *godot2dom_cursor(DisplayServer::CursorShape p_shape);
  61. // events
  62. static EM_BOOL fullscreen_change_callback(int p_event_type, const EmscriptenFullscreenChangeEvent *p_event, void *p_user_data);
  63. static EM_BOOL keydown_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
  64. static EM_BOOL keypress_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
  65. static EM_BOOL keyup_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
  66. static void vk_input_text_callback(const char *p_text, int p_cursor);
  67. static EM_BOOL mousemove_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data);
  68. static EM_BOOL mouse_button_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data);
  69. static EM_BOOL wheel_callback(int p_event_type, const EmscriptenWheelEvent *p_event, void *p_user_data);
  70. static EM_BOOL touch_press_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data);
  71. static EM_BOOL touchmove_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data);
  72. static void gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid);
  73. void process_joypads();
  74. static Vector<String> get_rendering_drivers_func();
  75. static DisplayServer *create_func(const String &p_rendering_driver, DisplayServer::WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error);
  76. static void _dispatch_input_event(const Ref<InputEvent> &p_event);
  77. static void request_quit_callback();
  78. static void update_clipboard_callback(const char *p_text);
  79. static void send_window_event_callback(int p_notification);
  80. static void drop_files_js_callback(char **p_filev, int p_filec);
  81. protected:
  82. int get_current_video_driver() const;
  83. public:
  84. // Override return type to make writing static callbacks less tedious.
  85. static DisplayServerJavaScript *get_singleton();
  86. // utilities
  87. bool check_size_force_redraw();
  88. // from DisplayServer
  89. void alert(const String &p_alert, const String &p_title = "ALERT!") override;
  90. bool has_feature(Feature p_feature) const override;
  91. String get_name() const override;
  92. // cursor
  93. void cursor_set_shape(CursorShape p_shape) override;
  94. CursorShape cursor_get_shape() const override;
  95. void cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape = CURSOR_ARROW, const Vector2 &p_hotspot = Vector2()) override;
  96. // mouse
  97. void mouse_set_mode(MouseMode p_mode) override;
  98. MouseMode mouse_get_mode() const override;
  99. // touch
  100. bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  101. // clipboard
  102. void clipboard_set(const String &p_text) override;
  103. String clipboard_get() const override;
  104. // screen
  105. int get_screen_count() const override;
  106. Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  107. Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  108. Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  109. int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  110. float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  111. void virtual_keyboard_show(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2(), bool p_multiline = false, int p_max_input_length = -1, int p_cursor_start = -1, int p_cursor_end = -1) override;
  112. void virtual_keyboard_hide() override;
  113. // windows
  114. Vector<DisplayServer::WindowID> get_window_list() const override;
  115. WindowID get_window_at_screen_position(const Point2i &p_position) const override;
  116. void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID) override;
  117. ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const override;
  118. void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  119. void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  120. void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  121. void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  122. void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  123. void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) override;
  124. int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const override;
  125. void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID) override;
  126. Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const override;
  127. void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID) override;
  128. void window_set_transient(WindowID p_window, WindowID p_parent) override;
  129. void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  130. Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  131. void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  132. Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  133. void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  134. Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  135. Size2i window_get_real_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  136. void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID) override;
  137. WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const override;
  138. bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const override;
  139. void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID) override;
  140. bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const override;
  141. void window_request_attention(WindowID p_window = MAIN_WINDOW_ID) override;
  142. void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID) override;
  143. bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const override;
  144. bool can_any_window_draw() const override;
  145. // events
  146. void process_events() override;
  147. // icon
  148. void set_icon(const Ref<Image> &p_icon) override;
  149. // others
  150. bool get_swap_cancel_ok() override;
  151. void swap_buffers() override;
  152. static void register_javascript_driver();
  153. DisplayServerJavaScript(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error);
  154. ~DisplayServerJavaScript();
  155. };
  156. #endif // DISPLAY_SERVER_JAVASCRIPT_H