os_javascript.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*************************************************************************/
  2. /* os_javascript.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 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. VideoMode video_mode;
  40. Vector2 windowed_size;
  41. bool window_maximized;
  42. bool entering_fullscreen;
  43. bool just_exited_fullscreen;
  44. InputDefault *input;
  45. Ref<InputEventKey> deferred_key_event;
  46. CursorShape cursor_shape;
  47. String cursors[CURSOR_MAX];
  48. Point2 touches[32];
  49. Point2i last_click_pos;
  50. uint64_t last_click_ms;
  51. int last_click_button_index;
  52. MainLoop *main_loop;
  53. AudioDriverJavaScript audio_driver_javascript;
  54. bool idb_available;
  55. int64_t sync_wait_time;
  56. int64_t last_sync_check_time;
  57. static EM_BOOL fullscreen_change_callback(int p_event_type, const EmscriptenFullscreenChangeEvent *p_event, void *p_user_data);
  58. static EM_BOOL keydown_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
  59. static EM_BOOL keypress_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
  60. static EM_BOOL keyup_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
  61. static EM_BOOL mousemove_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data);
  62. static EM_BOOL mouse_button_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data);
  63. static EM_BOOL wheel_callback(int p_event_type, const EmscriptenWheelEvent *p_event, void *p_user_data);
  64. static EM_BOOL touch_press_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data);
  65. static EM_BOOL touchmove_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data);
  66. static EM_BOOL gamepad_change_callback(int p_event_type, const EmscriptenGamepadEvent *p_event, void *p_user_data);
  67. void process_joypads();
  68. static void main_loop_callback();
  69. static void file_access_close_callback(const String &p_file, int p_flags);
  70. int video_driver_index;
  71. protected:
  72. virtual int get_current_video_driver() const;
  73. virtual void initialize_core();
  74. virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver);
  75. virtual void set_main_loop(MainLoop *p_main_loop);
  76. virtual void delete_main_loop();
  77. virtual void finalize();
  78. virtual bool _check_internal_feature_support(const String &p_feature);
  79. public:
  80. // Override return type to make writing static callbacks less tedious.
  81. static OS_JavaScript *get_singleton();
  82. virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0);
  83. virtual VideoMode get_video_mode(int p_screen = 0) const;
  84. virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;
  85. virtual void set_window_size(const Size2);
  86. virtual Size2 get_window_size() const;
  87. virtual void set_window_maximized(bool p_enabled);
  88. virtual bool is_window_maximized() const;
  89. virtual void set_window_fullscreen(bool p_enabled);
  90. virtual bool is_window_fullscreen() const;
  91. virtual Size2 get_screen_size(int p_screen = -1) const;
  92. virtual Point2 get_mouse_position() const;
  93. virtual int get_mouse_button_state() const;
  94. virtual void set_cursor_shape(CursorShape p_shape);
  95. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
  96. virtual void set_mouse_mode(MouseMode p_mode);
  97. virtual MouseMode get_mouse_mode() const;
  98. virtual bool has_touchscreen_ui_hint() const;
  99. virtual bool is_joy_known(int p_device);
  100. virtual String get_joy_guid(int p_device) const;
  101. virtual int get_video_driver_count() const;
  102. virtual const char *get_video_driver_name(int p_driver) const;
  103. virtual int get_audio_driver_count() const;
  104. virtual const char *get_audio_driver_name(int p_driver) const;
  105. virtual MainLoop *get_main_loop() const;
  106. void run_async();
  107. bool main_loop_iterate();
  108. virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id = NULL, String *r_pipe = NULL, int *r_exitcode = NULL, bool read_stderr = false);
  109. virtual Error kill(const ProcessID &p_pid);
  110. virtual int get_process_id() const;
  111. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  112. virtual void set_window_title(const String &p_title);
  113. virtual void set_icon(const Ref<Image> &p_icon);
  114. String get_executable_path() const;
  115. virtual Error shell_open(String p_uri);
  116. virtual String get_name();
  117. virtual bool can_draw() const;
  118. virtual String get_resource_dir() const;
  119. virtual String get_user_data_dir() const;
  120. virtual OS::PowerState get_power_state();
  121. virtual int get_power_seconds_left();
  122. virtual int get_power_percent_left();
  123. void set_idb_available(bool p_idb_available);
  124. virtual bool is_userfs_persistent() const;
  125. OS_JavaScript(int p_argc, char *p_argv[]);
  126. };
  127. #endif