1 /*************************************************************************/
2 /*  os_javascript.h                                                      */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #ifndef OS_JAVASCRIPT_H
32 #define OS_JAVASCRIPT_H
33 
34 #include "audio_driver_javascript.h"
35 #include "drivers/unix/os_unix.h"
36 #include "main/input_default.h"
37 #include "servers/audio_server.h"
38 #include "servers/visual/rasterizer.h"
39 
40 #include <emscripten/html5.h>
41 
42 class OS_JavaScript : public OS_Unix {
43 
44 	VideoMode video_mode;
45 	Vector2 windowed_size;
46 	bool window_maximized;
47 	bool entering_fullscreen;
48 	bool just_exited_fullscreen;
49 	bool transparency_enabled;
50 
51 	EMSCRIPTEN_WEBGL_CONTEXT_HANDLE webgl_ctx;
52 
53 	InputDefault *input;
54 	Ref<InputEventKey> deferred_key_event;
55 	CursorShape cursor_shape;
56 	String cursors[CURSOR_MAX];
57 	Map<CursorShape, Vector<Variant> > cursors_cache;
58 	Point2 touches[32];
59 
60 	Point2i last_click_pos;
61 	double last_click_ms;
62 	int last_click_button_index;
63 
64 	int last_width;
65 	int last_height;
66 
67 	MainLoop *main_loop;
68 	int video_driver_index;
69 	AudioDriverJavaScript *audio_driver_javascript;
70 	VisualServer *visual_server;
71 
72 	bool swap_ok_cancel;
73 	bool idb_available;
74 	int64_t sync_wait_time;
75 	int64_t last_sync_check_time;
76 
77 	static EM_BOOL fullscreen_change_callback(int p_event_type, const EmscriptenFullscreenChangeEvent *p_event, void *p_user_data);
78 
79 	static EM_BOOL keydown_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
80 	static EM_BOOL keypress_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
81 	static EM_BOOL keyup_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
82 
83 	static EM_BOOL mousemove_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data);
84 	static EM_BOOL mouse_button_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data);
85 
86 	static EM_BOOL wheel_callback(int p_event_type, const EmscriptenWheelEvent *p_event, void *p_user_data);
87 
88 	static EM_BOOL touch_press_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data);
89 	static EM_BOOL touchmove_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data);
90 
91 	static EM_BOOL gamepad_change_callback(int p_event_type, const EmscriptenGamepadEvent *p_event, void *p_user_data);
92 	void process_joypads();
93 
94 	static void file_access_close_callback(const String &p_file, int p_flags);
95 
96 protected:
97 	void resume_audio();
98 
99 	virtual int get_current_video_driver() const;
100 
101 	virtual void initialize_core();
102 	virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver);
103 
104 	virtual void set_main_loop(MainLoop *p_main_loop);
105 	virtual void delete_main_loop();
106 
107 	virtual void finalize();
108 
109 	virtual bool _check_internal_feature_support(const String &p_feature);
110 
111 public:
112 	String canvas_id;
113 	void finalize_async();
114 	bool check_size_force_redraw();
115 
116 	// Override return type to make writing static callbacks less tedious.
117 	static OS_JavaScript *get_singleton();
118 
119 	virtual bool get_swap_ok_cancel();
120 	virtual void swap_buffers();
121 	virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0);
122 	virtual VideoMode get_video_mode(int p_screen = 0) const;
123 	virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;
124 
125 	virtual void set_window_size(const Size2);
126 	virtual Size2 get_window_size() const;
127 	virtual void set_window_maximized(bool p_enabled);
128 	virtual bool is_window_maximized() const;
129 	virtual void set_window_fullscreen(bool p_enabled);
130 	virtual bool is_window_fullscreen() const;
131 	virtual Size2 get_screen_size(int p_screen = -1) const;
132 
133 	virtual Point2 get_mouse_position() const;
134 	virtual int get_mouse_button_state() const;
135 	virtual void set_cursor_shape(CursorShape p_shape);
136 	virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
137 	virtual void set_mouse_mode(MouseMode p_mode);
138 	virtual MouseMode get_mouse_mode() const;
139 
140 	virtual bool get_window_per_pixel_transparency_enabled() const;
141 	virtual void set_window_per_pixel_transparency_enabled(bool p_enabled);
142 
143 	virtual bool has_touchscreen_ui_hint() const;
144 
145 	virtual bool is_joy_known(int p_device);
146 	virtual String get_joy_guid(int p_device) const;
147 
148 	virtual int get_video_driver_count() const;
149 	virtual const char *get_video_driver_name(int p_driver) const;
150 
151 	virtual int get_audio_driver_count() const;
152 	virtual const char *get_audio_driver_name(int p_driver) const;
153 
154 	virtual void set_clipboard(const String &p_text);
155 	virtual String get_clipboard() const;
156 
157 	virtual MainLoop *get_main_loop() const;
158 	bool main_loop_iterate();
159 
160 	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);
161 	virtual Error kill(const ProcessID &p_pid);
162 	virtual int get_process_id() const;
163 
164 	virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
165 	virtual void set_window_title(const String &p_title);
166 	virtual void set_icon(const Ref<Image> &p_icon);
167 	String get_executable_path() const;
168 	virtual Error shell_open(String p_uri);
169 	virtual String get_name() const;
add_frame_delay(bool p_can_draw)170 	virtual void add_frame_delay(bool p_can_draw) {}
171 	virtual bool can_draw() const;
172 
173 	virtual String get_cache_path() const;
174 	virtual String get_config_path() const;
175 	virtual String get_data_path() const;
176 	virtual String get_user_data_dir() const;
177 
178 	virtual OS::PowerState get_power_state();
179 	virtual int get_power_seconds_left();
180 	virtual int get_power_percent_left();
181 
182 	void set_idb_available(bool p_idb_available);
183 	virtual bool is_userfs_persistent() const;
184 
185 	OS_JavaScript(int p_argc, char *p_argv[]);
186 };
187 
188 #endif
189