os_javascript.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**************************************************************************/
  2. /* os_javascript.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 OS_JAVASCRIPT_H
  31. #define OS_JAVASCRIPT_H
  32. #include "audio_driver_javascript.h"
  33. #include "drivers/unix/os_unix.h"
  34. #include "main/input_default.h"
  35. #include "servers/audio_server.h"
  36. #include "servers/visual/rasterizer.h"
  37. #include <emscripten/html5.h>
  38. class OS_JavaScript : public OS_Unix {
  39. private:
  40. struct JSTouchEvent {
  41. uint32_t identifier[32] = { 0 };
  42. double coords[64] = { 0 };
  43. };
  44. JSTouchEvent touch_event;
  45. struct JSKeyEvent {
  46. char code[32] = { 0 };
  47. char key[32] = { 0 };
  48. uint8_t modifiers[4] = { 0 };
  49. };
  50. JSKeyEvent key_event;
  51. bool ime_active = false;
  52. bool ime_started = false;
  53. String ime_text;
  54. Vector2 ime_selection;
  55. struct KeyEvent {
  56. bool pressed = false;
  57. bool echo = false;
  58. bool raw = false;
  59. uint32_t keycode = 0;
  60. uint32_t physical_keycode = 0;
  61. uint32_t unicode = 0;
  62. int mod = 0;
  63. };
  64. Vector<KeyEvent> key_event_buffer;
  65. int key_event_pos = 0;
  66. VideoMode video_mode;
  67. bool transparency_enabled;
  68. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE webgl_ctx;
  69. InputDefault *input;
  70. CursorShape cursor_shape;
  71. Point2 touches[32];
  72. char canvas_id[256];
  73. bool cursor_inside_canvas;
  74. Point2i last_click_pos;
  75. uint64_t last_click_ms;
  76. int last_click_button_index;
  77. MainLoop *main_loop;
  78. int video_driver_index;
  79. List<AudioDriverJavaScript *> audio_drivers;
  80. VisualServer *visual_server;
  81. bool tts;
  82. bool swap_ok_cancel;
  83. bool idb_available;
  84. bool idb_needs_sync;
  85. bool idb_is_syncing;
  86. bool pwa_is_waiting;
  87. Map<int, CharString> utterance_ids;
  88. Array voices;
  89. static void fullscreen_change_callback(int p_fullscreen);
  90. static int mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers);
  91. static void mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers);
  92. static int mouse_wheel_callback(double p_delta_x, double p_delta_y);
  93. static void key_callback(int p_pressed, int p_repeat, int p_modifiers);
  94. static void touch_callback(int p_type, int p_count);
  95. static void gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid);
  96. static void input_text_callback(const char *p_text, int p_cursor);
  97. void process_joypads();
  98. void process_keys();
  99. static void file_access_close_callback(const String &p_file, int p_flags);
  100. static void request_quit_callback();
  101. static void window_blur_callback();
  102. static void drop_files_callback(char **p_filev, int p_filec);
  103. static void send_notification_callback(int p_notification);
  104. static void fs_sync_callback();
  105. static void update_clipboard_callback(const char *p_text);
  106. static void update_pwa_state_callback();
  107. static void _js_utterance_callback(int p_event, int p_id, int p_pos);
  108. static void ime_callback(int p_type, const char *p_text);
  109. static void update_voices_callback(int p_size, const char **p_voice);
  110. protected:
  111. void resume_audio();
  112. virtual int get_current_video_driver() const;
  113. virtual void initialize_core();
  114. virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver);
  115. virtual void set_main_loop(MainLoop *p_main_loop);
  116. virtual void delete_main_loop();
  117. virtual void finalize();
  118. virtual bool _check_internal_feature_support(const String &p_feature);
  119. public:
  120. bool check_size_force_redraw();
  121. bool pwa_needs_update() const { return pwa_is_waiting; }
  122. Error pwa_update();
  123. void force_fs_sync();
  124. // Override return type to make writing static callbacks less tedious.
  125. static OS_JavaScript *get_singleton();
  126. virtual bool tts_is_speaking() const;
  127. virtual bool tts_is_paused() const;
  128. virtual Array tts_get_voices() const;
  129. 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);
  130. virtual void tts_pause();
  131. virtual void tts_resume();
  132. virtual void tts_stop();
  133. virtual bool has_virtual_keyboard() const;
  134. virtual void show_virtual_keyboard(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);
  135. virtual void hide_virtual_keyboard();
  136. virtual bool get_swap_ok_cancel();
  137. virtual void swap_buffers();
  138. virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0);
  139. virtual VideoMode get_video_mode(int p_screen = 0) const;
  140. virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;
  141. virtual void set_window_size(const Size2);
  142. virtual Size2 get_window_size() const;
  143. virtual void set_window_maximized(bool p_enabled);
  144. virtual bool is_window_maximized() const;
  145. virtual void set_window_fullscreen(bool p_enabled);
  146. virtual bool is_window_fullscreen() const;
  147. virtual Size2 get_screen_size(int p_screen = -1) const;
  148. virtual int get_screen_dpi(int p_screen = -1) const;
  149. virtual float get_screen_scale(int p_screen = -1) const;
  150. virtual float get_screen_max_scale() const;
  151. virtual void set_ime_active(const bool p_active);
  152. virtual void set_ime_position(const Point2 &p_pos);
  153. virtual Point2 get_ime_selection() const;
  154. virtual String get_ime_text() const;
  155. virtual Point2 get_mouse_position() const;
  156. virtual int get_mouse_button_state() const;
  157. virtual void set_cursor_shape(CursorShape p_shape);
  158. virtual CursorShape get_cursor_shape() const;
  159. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
  160. virtual void set_mouse_mode(MouseMode p_mode);
  161. virtual MouseMode get_mouse_mode() const;
  162. virtual bool get_window_per_pixel_transparency_enabled() const;
  163. virtual void set_window_per_pixel_transparency_enabled(bool p_enabled);
  164. virtual bool has_touchscreen_ui_hint() const;
  165. virtual bool is_joy_known(int p_device);
  166. virtual String get_joy_guid(int p_device) const;
  167. virtual int get_video_driver_count() const;
  168. virtual const char *get_video_driver_name(int p_driver) const;
  169. virtual int get_audio_driver_count() const;
  170. virtual const char *get_audio_driver_name(int p_driver) const;
  171. virtual void set_clipboard(const String &p_text);
  172. virtual String get_clipboard() const;
  173. virtual MainLoop *get_main_loop() const;
  174. bool main_loop_iterate();
  175. virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking = true, ProcessID *r_child_id = NULL, String *r_pipe = NULL, int *r_exitcode = NULL, bool read_stderr = false, Mutex *p_pipe_mutex = NULL, bool p_open_console = false);
  176. virtual Error kill(const ProcessID &p_pid);
  177. virtual int get_process_id() const;
  178. bool is_process_running(const ProcessID &p_pid) const;
  179. int get_processor_count() const;
  180. String get_unique_id() const;
  181. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  182. virtual void set_window_title(const String &p_title);
  183. virtual void set_icon(const Ref<Image> &p_icon);
  184. String get_executable_path() const;
  185. virtual Error shell_open(String p_uri);
  186. virtual String get_name() const;
  187. virtual void add_frame_delay(bool p_can_draw) {}
  188. virtual bool can_draw() const;
  189. virtual void vibrate_handheld(int p_duration_ms);
  190. virtual String get_cache_path() const;
  191. virtual String get_config_path() const;
  192. virtual String get_data_path() const;
  193. virtual String get_user_data_dir() const;
  194. virtual OS::PowerState get_power_state();
  195. virtual int get_power_seconds_left();
  196. virtual int get_power_percent_left();
  197. virtual bool is_userfs_persistent() const;
  198. Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path);
  199. OS_JavaScript();
  200. };
  201. #endif // OS_JAVASCRIPT_H