| 1 | /**************************************************************************/ |
| 2 | /* display_server_web.cpp */ |
| 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 | |
| 31 | #include "display_server_web.h" |
| 32 | |
| 33 | #include "dom_keys.inc" |
| 34 | #include "godot_js.h" |
| 35 | #include "os_web.h" |
| 36 | |
| 37 | #include "core/config/project_settings.h" |
| 38 | #include "core/object/callable_method_pointer.h" |
| 39 | #include "core/os/main_loop.h" |
| 40 | #include "servers/rendering/dummy/rasterizer_dummy.h" |
| 41 | |
| 42 | #ifdef GLES3_ENABLED |
| 43 | #include "drivers/gles3/rasterizer_gles3.h" |
| 44 | #endif |
| 45 | |
| 46 | #include <emscripten.h> |
| 47 | #include <png.h> |
| 48 | |
| 49 | #define DOM_BUTTON_LEFT 0 |
| 50 | #define DOM_BUTTON_MIDDLE 1 |
| 51 | #define DOM_BUTTON_RIGHT 2 |
| 52 | #define DOM_BUTTON_XBUTTON1 3 |
| 53 | #define DOM_BUTTON_XBUTTON2 4 |
| 54 | |
| 55 | DisplayServerWeb *DisplayServerWeb::get_singleton() { |
| 56 | return static_cast<DisplayServerWeb *>(DisplayServer::get_singleton()); |
| 57 | } |
| 58 | |
| 59 | // Window (canvas) |
| 60 | bool DisplayServerWeb::check_size_force_redraw() { |
| 61 | bool size_changed = godot_js_display_size_update() != 0; |
| 62 | if (size_changed && rect_changed_callback.is_valid()) { |
| 63 | Size2i window_size = window_get_size(); |
| 64 | Variant size = Rect2i(Point2i(), window_size); // TODO use window_get_position if implemented. |
| 65 | rect_changed_callback.call(size); |
| 66 | emscripten_set_canvas_element_size(canvas_id, window_size.x, window_size.y); |
| 67 | } |
| 68 | return size_changed; |
| 69 | } |
| 70 | |
| 71 | void DisplayServerWeb::fullscreen_change_callback(int p_fullscreen) { |
| 72 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 73 | if (!Thread::is_main_thread()) { |
| 74 | callable_mp_static(DisplayServerWeb::_fullscreen_change_callback).call_deferred(p_fullscreen); |
| 75 | return; |
| 76 | } |
| 77 | #endif |
| 78 | |
| 79 | _fullscreen_change_callback(p_fullscreen); |
| 80 | } |
| 81 | |
| 82 | void DisplayServerWeb::_fullscreen_change_callback(int p_fullscreen) { |
| 83 | DisplayServerWeb *display = get_singleton(); |
| 84 | if (p_fullscreen) { |
| 85 | display->window_mode = WINDOW_MODE_FULLSCREEN; |
| 86 | } else { |
| 87 | display->window_mode = WINDOW_MODE_WINDOWED; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Drag and drop callback. |
| 92 | void DisplayServerWeb::drop_files_js_callback(const char **p_filev, int p_filec) { |
| 93 | Vector<String> files; |
| 94 | for (int i = 0; i < p_filec; i++) { |
| 95 | files.push_back(String::utf8(p_filev[i])); |
| 96 | } |
| 97 | |
| 98 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 99 | if (!Thread::is_main_thread()) { |
| 100 | callable_mp_static(DisplayServerWeb::_drop_files_js_callback).call_deferred(files); |
| 101 | return; |
| 102 | } |
| 103 | #endif |
| 104 | |
| 105 | _drop_files_js_callback(files); |
| 106 | } |
| 107 | |
| 108 | void DisplayServerWeb::_drop_files_js_callback(const Vector<String> &p_files) { |
| 109 | DisplayServerWeb *ds = get_singleton(); |
| 110 | if (!ds) { |
| 111 | ERR_FAIL_MSG("Unable to drop files because the DisplayServer is not active"); |
| 112 | } |
| 113 | if (!ds->drop_files_callback.is_valid()) { |
| 114 | return; |
| 115 | } |
| 116 | Variant v_files = p_files; |
| 117 | const Variant *v_args[1] = { &v_files }; |
| 118 | Variant ret; |
| 119 | Callable::CallError ce; |
| 120 | ds->drop_files_callback.callp((const Variant **)&v_args, 1, ret, ce); |
| 121 | if (ce.error != Callable::CallError::CALL_OK) { |
| 122 | ERR_PRINT(vformat("Failed to execute drop files callback: %s.", Variant::get_callable_error_text(ds->drop_files_callback, v_args, 1, ce))); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Web quit request callback. |
| 127 | void DisplayServerWeb::request_quit_callback() { |
| 128 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 129 | if (!Thread::is_main_thread()) { |
| 130 | callable_mp_static(DisplayServerWeb::_request_quit_callback).call_deferred(); |
| 131 | return; |
| 132 | } |
| 133 | #endif |
| 134 | |
| 135 | _request_quit_callback(); |
| 136 | } |
| 137 | |
| 138 | void DisplayServerWeb::_request_quit_callback() { |
| 139 | DisplayServerWeb *ds = get_singleton(); |
| 140 | if (ds && ds->window_event_callback.is_valid()) { |
| 141 | Variant event = int(DisplayServer::WINDOW_EVENT_CLOSE_REQUEST); |
| 142 | ds->window_event_callback.call(event); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Keys |
| 147 | |
| 148 | void DisplayServerWeb::dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod, Key p_keycode) { |
| 149 | if (p_keycode != Key::SHIFT) { |
| 150 | ev->set_shift_pressed(p_mod & 1); |
| 151 | } |
| 152 | if (p_keycode != Key::ALT) { |
| 153 | ev->set_alt_pressed(p_mod & 2); |
| 154 | } |
| 155 | if (p_keycode != Key::CTRL) { |
| 156 | ev->set_ctrl_pressed(p_mod & 4); |
| 157 | } |
| 158 | if (p_keycode != Key::META) { |
| 159 | ev->set_meta_pressed(p_mod & 8); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void DisplayServerWeb::key_callback(int p_pressed, int p_repeat, int p_modifiers) { |
| 164 | DisplayServerWeb *ds = get_singleton(); |
| 165 | JSKeyEvent &key_event = ds->key_event; |
| 166 | |
| 167 | const String code = String::utf8(key_event.code); |
| 168 | const String key = String::utf8(key_event.key); |
| 169 | |
| 170 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 171 | if (!Thread::is_main_thread()) { |
| 172 | callable_mp_static(DisplayServerWeb::_key_callback).call_deferred(code, key, p_pressed, p_repeat, p_modifiers); |
| 173 | return; |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | _key_callback(code, key, p_pressed, p_repeat, p_modifiers); |
| 178 | } |
| 179 | |
| 180 | void DisplayServerWeb::_key_callback(const String &p_key_event_code, const String &p_key_event_key, int p_pressed, int p_repeat, int p_modifiers) { |
| 181 | // Resume audio context after input in case autoplay was denied. |
| 182 | OS_Web::get_singleton()->resume_audio(); |
| 183 | |
| 184 | DisplayServerWeb *ds = get_singleton(); |
| 185 | if (ds->ime_started) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | char32_t c = 0x00; |
| 190 | String unicode = p_key_event_key; |
| 191 | if (unicode.length() == 1) { |
| 192 | c = unicode[0]; |
| 193 | } |
| 194 | |
| 195 | Key keycode = dom_code2godot_scancode(p_key_event_code.utf8().get_data(), p_key_event_key.utf8().get_data(), false); |
| 196 | Key scancode = dom_code2godot_scancode(p_key_event_code.utf8().get_data(), p_key_event_key.utf8().get_data(), true); |
| 197 | KeyLocation location = dom_code2godot_key_location(p_key_event_code.utf8().get_data()); |
| 198 | |
| 199 | DisplayServerWeb::KeyEvent ke; |
| 200 | |
| 201 | ke.pressed = p_pressed; |
| 202 | ke.echo = p_repeat; |
| 203 | ke.raw = true; |
| 204 | ke.keycode = fix_keycode(c, keycode); |
| 205 | ke.physical_keycode = scancode; |
| 206 | ke.key_label = fix_key_label(c, keycode); |
| 207 | ke.unicode = fix_unicode(c); |
| 208 | ke.location = location; |
| 209 | ke.mod = p_modifiers; |
| 210 | |
| 211 | if (ds->key_event_pos >= ds->key_event_buffer.size()) { |
| 212 | ds->key_event_buffer.resize(1 + ds->key_event_pos); |
| 213 | } |
| 214 | ds->key_event_buffer.write[ds->key_event_pos++] = ke; |
| 215 | |
| 216 | // Make sure to flush all events so we can call restricted APIs inside the event. |
| 217 | Input::get_singleton()->flush_buffered_events(); |
| 218 | } |
| 219 | |
| 220 | // Mouse |
| 221 | |
| 222 | int DisplayServerWeb::mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers) { |
| 223 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 224 | if (!Thread::is_main_thread()) { |
| 225 | callable_mp_static(DisplayServerWeb::_mouse_button_callback).call_deferred(p_pressed, p_button, p_x, p_y, p_modifiers); |
| 226 | return true; |
| 227 | } |
| 228 | #endif |
| 229 | |
| 230 | return _mouse_button_callback(p_pressed, p_button, p_x, p_y, p_modifiers); |
| 231 | } |
| 232 | |
| 233 | int DisplayServerWeb::_mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers) { |
| 234 | DisplayServerWeb *ds = get_singleton(); |
| 235 | |
| 236 | Point2 pos(p_x, p_y); |
| 237 | Ref<InputEventMouseButton> ev; |
| 238 | ev.instantiate(); |
| 239 | ev->set_position(pos); |
| 240 | ev->set_global_position(pos); |
| 241 | ev->set_pressed(p_pressed); |
| 242 | dom2godot_mod(ev, p_modifiers, Key::NONE); |
| 243 | |
| 244 | switch (p_button) { |
| 245 | case DOM_BUTTON_LEFT: |
| 246 | ev->set_button_index(MouseButton::LEFT); |
| 247 | break; |
| 248 | case DOM_BUTTON_MIDDLE: |
| 249 | ev->set_button_index(MouseButton::MIDDLE); |
| 250 | break; |
| 251 | case DOM_BUTTON_RIGHT: |
| 252 | ev->set_button_index(MouseButton::RIGHT); |
| 253 | break; |
| 254 | case DOM_BUTTON_XBUTTON1: |
| 255 | ev->set_button_index(MouseButton::MB_XBUTTON1); |
| 256 | break; |
| 257 | case DOM_BUTTON_XBUTTON2: |
| 258 | ev->set_button_index(MouseButton::MB_XBUTTON2); |
| 259 | break; |
| 260 | default: |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | if (p_pressed) { |
| 265 | uint64_t diff = (OS::get_singleton()->get_ticks_usec() / 1000) - ds->last_click_ms; |
| 266 | |
| 267 | if (ev->get_button_index() == ds->last_click_button_index) { |
| 268 | if (diff < 400 && Point2(ds->last_click_pos).distance_to(ev->get_position()) < 5) { |
| 269 | ds->last_click_ms = 0; |
| 270 | ds->last_click_pos = Point2(-100, -100); |
| 271 | ds->last_click_button_index = MouseButton::NONE; |
| 272 | ev->set_double_click(true); |
| 273 | } |
| 274 | |
| 275 | } else { |
| 276 | ds->last_click_button_index = ev->get_button_index(); |
| 277 | } |
| 278 | |
| 279 | if (!ev->is_double_click()) { |
| 280 | ds->last_click_ms += diff; |
| 281 | ds->last_click_pos = ev->get_position(); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | BitField<MouseButtonMask> mask = Input::get_singleton()->get_mouse_button_mask(); |
| 286 | MouseButtonMask button_flag = mouse_button_to_mask(ev->get_button_index()); |
| 287 | if (ev->is_pressed()) { |
| 288 | mask.set_flag(button_flag); |
| 289 | } else if (mask.has_flag(button_flag)) { |
| 290 | mask.clear_flag(button_flag); |
| 291 | } else { |
| 292 | // Received release event, but press was outside the canvas, so ignore. |
| 293 | return false; |
| 294 | } |
| 295 | ev->set_button_mask(mask); |
| 296 | |
| 297 | Input::get_singleton()->parse_input_event(ev); |
| 298 | // Resume audio context after input in case autoplay was denied. |
| 299 | OS_Web::get_singleton()->resume_audio(); |
| 300 | |
| 301 | // Make sure to flush all events so we can call restricted APIs inside the event. |
| 302 | Input::get_singleton()->flush_buffered_events(); |
| 303 | |
| 304 | // Prevent multi-click text selection and wheel-click scrolling anchor. |
| 305 | // Context menu is prevented through contextmenu event. |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | void DisplayServerWeb::mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers, double p_pressure) { |
| 310 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 311 | if (!Thread::is_main_thread()) { |
| 312 | callable_mp_static(DisplayServerWeb::_mouse_move_callback).call_deferred(p_x, p_y, p_rel_x, p_rel_y, p_modifiers, p_pressure); |
| 313 | return; |
| 314 | } |
| 315 | #endif |
| 316 | |
| 317 | _mouse_move_callback(p_x, p_y, p_rel_x, p_rel_y, p_modifiers, p_pressure); |
| 318 | } |
| 319 | |
| 320 | void DisplayServerWeb::_mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers, double p_pressure) { |
| 321 | BitField<MouseButtonMask> input_mask = Input::get_singleton()->get_mouse_button_mask(); |
| 322 | // For motion outside the canvas, only read mouse movement if dragging |
| 323 | // started inside the canvas; imitating desktop app behavior. |
| 324 | if (!get_singleton()->cursor_inside_canvas && input_mask.is_empty()) { |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | Point2 pos(p_x, p_y); |
| 329 | Ref<InputEventMouseMotion> ev; |
| 330 | ev.instantiate(); |
| 331 | dom2godot_mod(ev, p_modifiers, Key::NONE); |
| 332 | ev->set_button_mask(input_mask); |
| 333 | |
| 334 | ev->set_position(pos); |
| 335 | ev->set_global_position(pos); |
| 336 | ev->set_pressure((float)p_pressure); |
| 337 | |
| 338 | ev->set_relative(Vector2(p_rel_x, p_rel_y)); |
| 339 | ev->set_relative_screen_position(ev->get_relative()); |
| 340 | ev->set_velocity(Input::get_singleton()->get_last_mouse_velocity()); |
| 341 | ev->set_screen_velocity(ev->get_velocity()); |
| 342 | |
| 343 | Input::get_singleton()->parse_input_event(ev); |
| 344 | } |
| 345 | |
| 346 | // Cursor |
| 347 | const char *DisplayServerWeb::godot2dom_cursor(DisplayServer::CursorShape p_shape) { |
| 348 | switch (p_shape) { |
| 349 | case DisplayServer::CURSOR_ARROW: |
| 350 | return "default"; |
| 351 | case DisplayServer::CURSOR_IBEAM: |
| 352 | return "text"; |
| 353 | case DisplayServer::CURSOR_POINTING_HAND: |
| 354 | return "pointer"; |
| 355 | case DisplayServer::CURSOR_CROSS: |
| 356 | return "crosshair"; |
| 357 | case DisplayServer::CURSOR_WAIT: |
| 358 | return "wait"; |
| 359 | case DisplayServer::CURSOR_BUSY: |
| 360 | return "progress"; |
| 361 | case DisplayServer::CURSOR_DRAG: |
| 362 | return "grab"; |
| 363 | case DisplayServer::CURSOR_CAN_DROP: |
| 364 | return "grabbing"; |
| 365 | case DisplayServer::CURSOR_FORBIDDEN: |
| 366 | return "no-drop"; |
| 367 | case DisplayServer::CURSOR_VSIZE: |
| 368 | return "ns-resize"; |
| 369 | case DisplayServer::CURSOR_HSIZE: |
| 370 | return "ew-resize"; |
| 371 | case DisplayServer::CURSOR_BDIAGSIZE: |
| 372 | return "nesw-resize"; |
| 373 | case DisplayServer::CURSOR_FDIAGSIZE: |
| 374 | return "nwse-resize"; |
| 375 | case DisplayServer::CURSOR_MOVE: |
| 376 | return "move"; |
| 377 | case DisplayServer::CURSOR_VSPLIT: |
| 378 | return "row-resize"; |
| 379 | case DisplayServer::CURSOR_HSPLIT: |
| 380 | return "col-resize"; |
| 381 | case DisplayServer::CURSOR_HELP: |
| 382 | return "help"; |
| 383 | default: |
| 384 | return "default"; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | bool DisplayServerWeb::tts_is_speaking() const { |
| 389 | return godot_js_tts_is_speaking(); |
| 390 | } |
| 391 | |
| 392 | bool DisplayServerWeb::tts_is_paused() const { |
| 393 | return godot_js_tts_is_paused(); |
| 394 | } |
| 395 | |
| 396 | void DisplayServerWeb::update_voices_callback(int p_size, const char **p_voice) { |
| 397 | Vector<String> voices; |
| 398 | for (int i = 0; i < p_size; i++) { |
| 399 | voices.append(String::utf8(p_voice[i])); |
| 400 | } |
| 401 | |
| 402 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 403 | if (!Thread::is_main_thread()) { |
| 404 | callable_mp_static(DisplayServerWeb::_update_voices_callback).call_deferred(voices); |
| 405 | return; |
| 406 | } |
| 407 | #endif |
| 408 | |
| 409 | _update_voices_callback(voices); |
| 410 | } |
| 411 | |
| 412 | void DisplayServerWeb::_update_voices_callback(const Vector<String> &p_voices) { |
| 413 | get_singleton()->voices.clear(); |
| 414 | for (int i = 0; i < p_voices.size(); i++) { |
| 415 | Vector<String> tokens = p_voices[i].split(";", true, 2); |
| 416 | if (tokens.size() == 2) { |
| 417 | Dictionary voice_d; |
| 418 | voice_d["name"] = tokens[1]; |
| 419 | voice_d["id"] = tokens[1]; |
| 420 | voice_d["language"] = tokens[0]; |
| 421 | get_singleton()->voices.push_back(voice_d); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | TypedArray<Dictionary> DisplayServerWeb::tts_get_voices() const { |
| 427 | godot_js_tts_get_voices(update_voices_callback); |
| 428 | return voices; |
| 429 | } |
| 430 | |
| 431 | void DisplayServerWeb::tts_speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int64_t p_utterance_id, bool p_interrupt) { |
| 432 | if (p_interrupt) { |
| 433 | tts_stop(); |
| 434 | } |
| 435 | |
| 436 | if (p_text.is_empty()) { |
| 437 | tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, p_utterance_id); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | CharString string = p_text.utf8(); |
| 442 | utterance_ids[p_utterance_id] = string; |
| 443 | |
| 444 | godot_js_tts_speak(string.get_data(), p_voice.utf8().get_data(), CLAMP(p_volume, 0, 100), CLAMP(p_pitch, 0.f, 2.f), CLAMP(p_rate, 0.1f, 10.f), p_utterance_id, DisplayServerWeb::js_utterance_callback); |
| 445 | } |
| 446 | |
| 447 | void DisplayServerWeb::tts_pause() { |
| 448 | godot_js_tts_pause(); |
| 449 | } |
| 450 | |
| 451 | void DisplayServerWeb::tts_resume() { |
| 452 | godot_js_tts_resume(); |
| 453 | } |
| 454 | |
| 455 | void DisplayServerWeb::tts_stop() { |
| 456 | for (const KeyValue<int64_t, CharString> &E : utterance_ids) { |
| 457 | tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, E.key); |
| 458 | } |
| 459 | utterance_ids.clear(); |
| 460 | godot_js_tts_stop(); |
| 461 | } |
| 462 | |
| 463 | void DisplayServerWeb::js_utterance_callback(int p_event, int64_t p_id, int p_pos) { |
| 464 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 465 | if (!Thread::is_main_thread()) { |
| 466 | callable_mp_static(DisplayServerWeb::_js_utterance_callback).call_deferred(p_event, p_id, p_pos); |
| 467 | return; |
| 468 | } |
| 469 | #endif |
| 470 | |
| 471 | _js_utterance_callback(p_event, p_id, p_pos); |
| 472 | } |
| 473 | |
| 474 | void DisplayServerWeb::_js_utterance_callback(int p_event, int64_t p_id, int p_pos) { |
| 475 | DisplayServerWeb *ds = (DisplayServerWeb *)DisplayServer::get_singleton(); |
| 476 | if (ds->utterance_ids.has(p_id)) { |
| 477 | int pos = 0; |
| 478 | if ((TTSUtteranceEvent)p_event == DisplayServer::TTS_UTTERANCE_BOUNDARY) { |
| 479 | // Convert position from UTF-8 to UTF-32. |
| 480 | const CharString &string = ds->utterance_ids[p_id]; |
| 481 | for (int i = 0; i < MIN(p_pos, string.length()); i++) { |
| 482 | uint8_t c = string[i]; |
| 483 | if ((c & 0xe0) == 0xc0) { |
| 484 | i += 1; |
| 485 | } else if ((c & 0xf0) == 0xe0) { |
| 486 | i += 2; |
| 487 | } else if ((c & 0xf8) == 0xf0) { |
| 488 | i += 3; |
| 489 | } |
| 490 | pos++; |
| 491 | } |
| 492 | } else if ((TTSUtteranceEvent)p_event != DisplayServer::TTS_UTTERANCE_STARTED) { |
| 493 | ds->utterance_ids.erase(p_id); |
| 494 | } |
| 495 | ds->tts_post_utterance_event((TTSUtteranceEvent)p_event, p_id, pos); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | void DisplayServerWeb::cursor_set_shape(CursorShape p_shape) { |
| 500 | ERR_FAIL_INDEX(p_shape, CURSOR_MAX); |
| 501 | if (cursor_shape == p_shape) { |
| 502 | return; |
| 503 | } |
| 504 | cursor_shape = p_shape; |
| 505 | godot_js_display_cursor_set_shape(godot2dom_cursor(cursor_shape)); |
| 506 | } |
| 507 | |
| 508 | DisplayServer::CursorShape DisplayServerWeb::cursor_get_shape() const { |
| 509 | return cursor_shape; |
| 510 | } |
| 511 | |
| 512 | void DisplayServerWeb::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) { |
| 513 | ERR_FAIL_INDEX(p_shape, CURSOR_MAX); |
| 514 | if (p_cursor.is_valid()) { |
| 515 | Ref<Image> image = _get_cursor_image_from_resource(p_cursor, p_hotspot); |
| 516 | ERR_FAIL_COND(image.is_null()); |
| 517 | Vector2i texture_size = image->get_size(); |
| 518 | |
| 519 | if (image->get_format() != Image::FORMAT_RGBA8) { |
| 520 | image->convert(Image::FORMAT_RGBA8); |
| 521 | } |
| 522 | |
| 523 | png_image png_meta; |
| 524 | memset(&png_meta, 0, sizeof png_meta); |
| 525 | png_meta.version = PNG_IMAGE_VERSION; |
| 526 | png_meta.width = texture_size.width; |
| 527 | png_meta.height = texture_size.height; |
| 528 | png_meta.format = PNG_FORMAT_RGBA; |
| 529 | |
| 530 | PackedByteArray png; |
| 531 | size_t len; |
| 532 | PackedByteArray data = image->get_data(); |
| 533 | ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr)); |
| 534 | |
| 535 | png.resize(len); |
| 536 | ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr)); |
| 537 | |
| 538 | godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), png.ptr(), len, p_hotspot.x, p_hotspot.y); |
| 539 | |
| 540 | } else { |
| 541 | godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), nullptr, 0, 0, 0); |
| 542 | } |
| 543 | |
| 544 | cursor_set_shape(cursor_shape); |
| 545 | } |
| 546 | |
| 547 | // Mouse mode |
| 548 | void DisplayServerWeb::_mouse_update_mode() { |
| 549 | MouseMode wanted_mouse_mode = mouse_mode_override_enabled |
| 550 | ? mouse_mode_override |
| 551 | : mouse_mode_base; |
| 552 | |
| 553 | ERR_FAIL_COND_MSG(wanted_mouse_mode == MOUSE_MODE_CONFINED || wanted_mouse_mode == MOUSE_MODE_CONFINED_HIDDEN, "MOUSE_MODE_CONFINED is not supported for the Web platform."); |
| 554 | if (wanted_mouse_mode == mouse_get_mode()) { |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | if (wanted_mouse_mode == MOUSE_MODE_VISIBLE) { |
| 559 | godot_js_display_cursor_set_visible(1); |
| 560 | godot_js_display_cursor_lock_set(0); |
| 561 | |
| 562 | } else if (wanted_mouse_mode == MOUSE_MODE_HIDDEN) { |
| 563 | godot_js_display_cursor_set_visible(0); |
| 564 | godot_js_display_cursor_lock_set(0); |
| 565 | |
| 566 | } else if (wanted_mouse_mode == MOUSE_MODE_CAPTURED) { |
| 567 | godot_js_display_cursor_set_visible(1); |
| 568 | godot_js_display_cursor_lock_set(1); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | void DisplayServerWeb::mouse_set_mode(MouseMode p_mode) { |
| 573 | ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX); |
| 574 | |
| 575 | if (mouse_mode_override_enabled) { |
| 576 | mouse_mode_base = p_mode; |
| 577 | // No need to update, as override is enabled. |
| 578 | return; |
| 579 | } |
| 580 | if (p_mode == mouse_mode_base && p_mode == mouse_get_mode()) { |
| 581 | // No need to update, as it is currently set as the correct mode. |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | mouse_mode_base = p_mode; |
| 586 | _mouse_update_mode(); |
| 587 | } |
| 588 | |
| 589 | DisplayServer::MouseMode DisplayServerWeb::mouse_get_mode() const { |
| 590 | if (godot_js_display_cursor_is_hidden()) { |
| 591 | return MOUSE_MODE_HIDDEN; |
| 592 | } |
| 593 | |
| 594 | if (godot_js_display_cursor_is_locked()) { |
| 595 | return MOUSE_MODE_CAPTURED; |
| 596 | } |
| 597 | return MOUSE_MODE_VISIBLE; |
| 598 | } |
| 599 | |
| 600 | void DisplayServerWeb::mouse_set_mode_override(MouseMode p_mode) { |
| 601 | ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX); |
| 602 | |
| 603 | if (!mouse_mode_override_enabled) { |
| 604 | mouse_mode_override = p_mode; |
| 605 | // No need to update, as override is not enabled. |
| 606 | return; |
| 607 | } |
| 608 | if (p_mode == mouse_mode_override && p_mode == mouse_get_mode()) { |
| 609 | // No need to update, as it is currently set as the correct mode. |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | mouse_mode_override = p_mode; |
| 614 | _mouse_update_mode(); |
| 615 | } |
| 616 | |
| 617 | DisplayServer::MouseMode DisplayServerWeb::mouse_get_mode_override() const { |
| 618 | return mouse_mode_override; |
| 619 | } |
| 620 | |
| 621 | void DisplayServerWeb::mouse_set_mode_override_enabled(bool p_override_enabled) { |
| 622 | if (p_override_enabled == mouse_mode_override_enabled) { |
| 623 | return; |
| 624 | } |
| 625 | mouse_mode_override_enabled = p_override_enabled; |
| 626 | _mouse_update_mode(); |
| 627 | } |
| 628 | |
| 629 | bool DisplayServerWeb::mouse_is_mode_override_enabled() const { |
| 630 | return mouse_mode_override_enabled; |
| 631 | } |
| 632 | |
| 633 | Point2i DisplayServerWeb::mouse_get_position() const { |
| 634 | return Input::get_singleton()->get_mouse_position(); |
| 635 | } |
| 636 | |
| 637 | // Wheel |
| 638 | int DisplayServerWeb::mouse_wheel_callback(int p_delta_mode, double p_delta_x, double p_delta_y) { |
| 639 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 640 | if (!Thread::is_main_thread()) { |
| 641 | callable_mp_static(DisplayServerWeb::_mouse_wheel_callback).call_deferred(p_delta_mode, p_delta_x, p_delta_y); |
| 642 | return true; |
| 643 | } |
| 644 | #endif |
| 645 | |
| 646 | return _mouse_wheel_callback(p_delta_mode, p_delta_x, p_delta_y); |
| 647 | } |
| 648 | |
| 649 | int DisplayServerWeb::_mouse_wheel_callback(int p_delta_mode, double p_delta_x, double p_delta_y) { |
| 650 | if (!godot_js_display_canvas_is_focused() && !godot_js_is_ime_focused()) { |
| 651 | if (get_singleton()->cursor_inside_canvas) { |
| 652 | godot_js_display_canvas_focus(); |
| 653 | } else { |
| 654 | return false; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | Input *input = Input::get_singleton(); |
| 659 | Ref<InputEventMouseButton> ev; |
| 660 | ev.instantiate(); |
| 661 | ev->set_position(input->get_mouse_position()); |
| 662 | ev->set_global_position(ev->get_position()); |
| 663 | |
| 664 | ev->set_shift_pressed(input->is_key_pressed(Key::SHIFT)); |
| 665 | ev->set_alt_pressed(input->is_key_pressed(Key::ALT)); |
| 666 | ev->set_ctrl_pressed(input->is_key_pressed(Key::CTRL)); |
| 667 | ev->set_meta_pressed(input->is_key_pressed(Key::META)); |
| 668 | |
| 669 | enum DeltaMode { |
| 670 | DELTA_MODE_PIXEL = 0, |
| 671 | DELTA_MODE_LINE = 1, |
| 672 | DELTA_MODE_PAGE = 2, |
| 673 | }; |
| 674 | const float MOUSE_WHEEL_PIXEL_FACTOR = 0.03f; |
| 675 | const float MOUSE_WHEEL_LINE_FACTOR = 0.3f; |
| 676 | const float MOUSE_WHEEL_PAGE_FACTOR = 1.0f; |
| 677 | float mouse_wheel_factor; |
| 678 | |
| 679 | switch (p_delta_mode) { |
| 680 | case DELTA_MODE_PIXEL: { |
| 681 | mouse_wheel_factor = MOUSE_WHEEL_PIXEL_FACTOR; |
| 682 | } break; |
| 683 | case DELTA_MODE_LINE: { |
| 684 | mouse_wheel_factor = MOUSE_WHEEL_LINE_FACTOR; |
| 685 | } break; |
| 686 | case DELTA_MODE_PAGE: { |
| 687 | mouse_wheel_factor = MOUSE_WHEEL_PAGE_FACTOR; |
| 688 | } break; |
| 689 | } |
| 690 | |
| 691 | if (p_delta_y < 0) { |
| 692 | ev->set_button_index(MouseButton::WHEEL_UP); |
| 693 | ev->set_factor(-p_delta_y * mouse_wheel_factor); |
| 694 | } else if (p_delta_y > 0) { |
| 695 | ev->set_button_index(MouseButton::WHEEL_DOWN); |
| 696 | ev->set_factor(p_delta_y * mouse_wheel_factor); |
| 697 | } else if (p_delta_x > 0) { |
| 698 | ev->set_button_index(MouseButton::WHEEL_LEFT); |
| 699 | ev->set_factor(p_delta_x * mouse_wheel_factor); |
| 700 | } else if (p_delta_x < 0) { |
| 701 | ev->set_button_index(MouseButton::WHEEL_RIGHT); |
| 702 | ev->set_factor(-p_delta_x * mouse_wheel_factor); |
| 703 | } else { |
| 704 | return false; |
| 705 | } |
| 706 | |
| 707 | MouseButtonMask button_flag = mouse_button_to_mask(ev->get_button_index()); |
| 708 | BitField<MouseButtonMask> button_mask = input->get_mouse_button_mask(); |
| 709 | button_mask.set_flag(button_flag); |
| 710 | |
| 711 | ev->set_pressed(true); |
| 712 | ev->set_button_mask(button_mask); |
| 713 | input->parse_input_event(ev); |
| 714 | |
| 715 | Ref<InputEventMouseButton> release = ev->duplicate(); |
| 716 | release->set_pressed(false); |
| 717 | release->set_button_mask(input->get_mouse_button_mask()); |
| 718 | input->parse_input_event(release); |
| 719 | |
| 720 | return true; |
| 721 | } |
| 722 | |
| 723 | // Touch |
| 724 | void DisplayServerWeb::touch_callback(int p_type, int p_count) { |
| 725 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 726 | if (!Thread::is_main_thread()) { |
| 727 | callable_mp_static(DisplayServerWeb::_touch_callback).call_deferred(p_type, p_count); |
| 728 | return; |
| 729 | } |
| 730 | #endif |
| 731 | |
| 732 | _touch_callback(p_type, p_count); |
| 733 | } |
| 734 | |
| 735 | void DisplayServerWeb::_touch_callback(int p_type, int p_count) { |
| 736 | DisplayServerWeb *ds = get_singleton(); |
| 737 | |
| 738 | const JSTouchEvent &touch_event = ds->touch_event; |
| 739 | for (int i = 0; i < p_count; i++) { |
| 740 | Point2 point(touch_event.coords[i * 2], touch_event.coords[i * 2 + 1]); |
| 741 | if (p_type == 2) { |
| 742 | // touchmove |
| 743 | Ref<InputEventScreenDrag> ev; |
| 744 | ev.instantiate(); |
| 745 | ev->set_index(touch_event.identifier[i]); |
| 746 | ev->set_position(point); |
| 747 | |
| 748 | Point2 &prev = ds->touches[i]; |
| 749 | ev->set_relative(ev->get_position() - prev); |
| 750 | ev->set_relative_screen_position(ev->get_relative()); |
| 751 | prev = ev->get_position(); |
| 752 | |
| 753 | Input::get_singleton()->parse_input_event(ev); |
| 754 | } else { |
| 755 | // touchstart/touchend |
| 756 | Ref<InputEventScreenTouch> ev; |
| 757 | |
| 758 | // Resume audio context after input in case autoplay was denied. |
| 759 | OS_Web::get_singleton()->resume_audio(); |
| 760 | |
| 761 | ev.instantiate(); |
| 762 | ev->set_index(touch_event.identifier[i]); |
| 763 | ev->set_position(point); |
| 764 | ev->set_pressed(p_type == 0); |
| 765 | ds->touches[i] = point; |
| 766 | |
| 767 | Input::get_singleton()->parse_input_event(ev); |
| 768 | |
| 769 | // Make sure to flush all events so we can call restricted APIs inside the event. |
| 770 | Input::get_singleton()->flush_buffered_events(); |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | bool DisplayServerWeb::is_touchscreen_available() const { |
| 776 | return godot_js_display_touchscreen_is_available() || (Input::get_singleton() && Input::get_singleton()->is_emulating_touch_from_mouse()); |
| 777 | } |
| 778 | |
| 779 | // Virtual Keyboard |
| 780 | void DisplayServerWeb::vk_input_text_callback(const char *p_text, int p_cursor) { |
| 781 | String text = String::utf8(p_text); |
| 782 | |
| 783 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 784 | if (!Thread::is_main_thread()) { |
| 785 | callable_mp_static(DisplayServerWeb::_vk_input_text_callback).call_deferred(text, p_cursor); |
| 786 | return; |
| 787 | } |
| 788 | #endif |
| 789 | |
| 790 | _vk_input_text_callback(text, p_cursor); |
| 791 | } |
| 792 | |
| 793 | void DisplayServerWeb::_vk_input_text_callback(const String &p_text, int p_cursor) { |
| 794 | DisplayServerWeb *ds = DisplayServerWeb::get_singleton(); |
| 795 | if (!ds || !ds->input_text_callback.is_valid()) { |
| 796 | return; |
| 797 | } |
| 798 | // Call input_text |
| 799 | ds->input_text_callback.call(p_text, true); |
| 800 | // Insert key right to reach position. |
| 801 | Input *input = Input::get_singleton(); |
| 802 | Ref<InputEventKey> k; |
| 803 | for (int i = 0; i < p_cursor; i++) { |
| 804 | k.instantiate(); |
| 805 | k->set_pressed(true); |
| 806 | k->set_echo(false); |
| 807 | k->set_keycode(Key::RIGHT); |
| 808 | input->parse_input_event(k); |
| 809 | k.instantiate(); |
| 810 | k->set_pressed(false); |
| 811 | k->set_echo(false); |
| 812 | k->set_keycode(Key::RIGHT); |
| 813 | input->parse_input_event(k); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | void DisplayServerWeb::virtual_keyboard_show(const String &p_existing_text, const Rect2 &p_screen_rect, VirtualKeyboardType p_type, int p_max_input_length, int p_cursor_start, int p_cursor_end) { |
| 818 | godot_js_display_vk_show(p_existing_text.utf8().get_data(), p_type, p_cursor_start, p_cursor_end); |
| 819 | } |
| 820 | |
| 821 | void DisplayServerWeb::virtual_keyboard_hide() { |
| 822 | godot_js_display_vk_hide(); |
| 823 | } |
| 824 | |
| 825 | void DisplayServerWeb::window_blur_callback() { |
| 826 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 827 | if (!Thread::is_main_thread()) { |
| 828 | callable_mp_static(DisplayServerWeb::_window_blur_callback).call_deferred(); |
| 829 | return; |
| 830 | } |
| 831 | #endif |
| 832 | |
| 833 | _window_blur_callback(); |
| 834 | } |
| 835 | |
| 836 | void DisplayServerWeb::_window_blur_callback() { |
| 837 | Input::get_singleton()->release_pressed_events(); |
| 838 | } |
| 839 | |
| 840 | // Gamepad |
| 841 | void DisplayServerWeb::gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid) { |
| 842 | String id = p_id; |
| 843 | String guid = p_guid; |
| 844 | |
| 845 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 846 | if (!Thread::is_main_thread()) { |
| 847 | callable_mp_static(DisplayServerWeb::_gamepad_callback).call_deferred(p_index, p_connected, id, guid); |
| 848 | return; |
| 849 | } |
| 850 | #endif |
| 851 | |
| 852 | _gamepad_callback(p_index, p_connected, id, guid); |
| 853 | } |
| 854 | |
| 855 | void DisplayServerWeb::_gamepad_callback(int p_index, int p_connected, const String &p_id, const String &p_guid) { |
| 856 | if (p_connected) { |
| 857 | DisplayServerWeb::get_singleton()->gamepad_count += 1; |
| 858 | } else { |
| 859 | DisplayServerWeb::get_singleton()->gamepad_count -= 1; |
| 860 | } |
| 861 | |
| 862 | Input *input = Input::get_singleton(); |
| 863 | if (p_connected) { |
| 864 | input->joy_connection_changed(p_index, true, p_id, p_guid); |
| 865 | } else { |
| 866 | input->joy_connection_changed(p_index, false, ""); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | // IME. |
| 871 | void DisplayServerWeb::ime_callback(int p_type, const char *p_text) { |
| 872 | String text = String::utf8(p_text); |
| 873 | |
| 874 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 875 | if (!Thread::is_main_thread()) { |
| 876 | callable_mp_static(DisplayServerWeb::_ime_callback).call_deferred(p_type, text); |
| 877 | return; |
| 878 | } |
| 879 | #endif |
| 880 | |
| 881 | _ime_callback(p_type, text); |
| 882 | } |
| 883 | |
| 884 | void DisplayServerWeb::_ime_callback(int p_type, const String &p_text) { |
| 885 | DisplayServerWeb *ds = get_singleton(); |
| 886 | // Resume audio context after input in case autoplay was denied. |
| 887 | OS_Web::get_singleton()->resume_audio(); |
| 888 | |
| 889 | switch (p_type) { |
| 890 | case 0: { |
| 891 | // IME start. |
| 892 | ds->ime_text = String(); |
| 893 | ds->ime_selection = Vector2i(); |
| 894 | for (int i = ds->key_event_pos - 1; i >= 0; i--) { |
| 895 | // Delete last raw keydown event from query. |
| 896 | if (ds->key_event_buffer[i].pressed && ds->key_event_buffer[i].raw) { |
| 897 | ds->key_event_buffer.remove_at(i); |
| 898 | ds->key_event_pos--; |
| 899 | break; |
| 900 | } |
| 901 | } |
| 902 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE); |
| 903 | ds->ime_started = true; |
| 904 | } break; |
| 905 | case 1: { |
| 906 | // IME update. |
| 907 | if (ds->ime_active && ds->ime_started) { |
| 908 | ds->ime_text = p_text; |
| 909 | ds->ime_selection = Vector2i(ds->ime_text.length(), ds->ime_text.length()); |
| 910 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE); |
| 911 | } |
| 912 | } break; |
| 913 | case 2: { |
| 914 | // IME commit. |
| 915 | if (ds->ime_active && ds->ime_started) { |
| 916 | ds->ime_started = false; |
| 917 | |
| 918 | ds->ime_text = String(); |
| 919 | ds->ime_selection = Vector2i(); |
| 920 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE); |
| 921 | |
| 922 | String text = p_text; |
| 923 | for (int i = 0; i < text.length(); i++) { |
| 924 | DisplayServerWeb::KeyEvent ke; |
| 925 | |
| 926 | ke.pressed = true; |
| 927 | ke.echo = false; |
| 928 | ke.raw = false; |
| 929 | ke.keycode = Key::NONE; |
| 930 | ke.physical_keycode = Key::NONE; |
| 931 | ke.key_label = Key::NONE; |
| 932 | ke.unicode = text[i]; |
| 933 | ke.mod = 0; |
| 934 | |
| 935 | if (ds->key_event_pos >= ds->key_event_buffer.size()) { |
| 936 | ds->key_event_buffer.resize(1 + ds->key_event_pos); |
| 937 | } |
| 938 | ds->key_event_buffer.write[ds->key_event_pos++] = ke; |
| 939 | } |
| 940 | } |
| 941 | } break; |
| 942 | default: |
| 943 | break; |
| 944 | } |
| 945 | |
| 946 | ds->process_keys(); |
| 947 | Input::get_singleton()->flush_buffered_events(); |
| 948 | } |
| 949 | |
| 950 | void DisplayServerWeb::window_set_ime_active(const bool p_active, WindowID p_window) { |
| 951 | ime_active = p_active; |
| 952 | godot_js_set_ime_active(p_active); |
| 953 | } |
| 954 | |
| 955 | void DisplayServerWeb::window_set_ime_position(const Point2i &p_pos, WindowID p_window) { |
| 956 | godot_js_set_ime_position(p_pos.x, p_pos.y); |
| 957 | } |
| 958 | |
| 959 | Point2i DisplayServerWeb::ime_get_selection() const { |
| 960 | return ime_selection; |
| 961 | } |
| 962 | |
| 963 | String DisplayServerWeb::ime_get_text() const { |
| 964 | return ime_text; |
| 965 | } |
| 966 | |
| 967 | void DisplayServerWeb::process_joypads() { |
| 968 | Input *input = Input::get_singleton(); |
| 969 | int32_t pads = godot_js_input_gamepad_sample_count(); |
| 970 | int32_t s_btns_num = 0; |
| 971 | int32_t s_axes_num = 0; |
| 972 | int32_t s_standard = 0; |
| 973 | float s_btns[16]; |
| 974 | float s_axes[10]; |
| 975 | for (int idx = 0; idx < pads; idx++) { |
| 976 | int err = godot_js_input_gamepad_sample_get(idx, s_btns, &s_btns_num, s_axes, &s_axes_num, &s_standard); |
| 977 | if (err) { |
| 978 | continue; |
| 979 | } |
| 980 | for (int b = 0; b < s_btns_num; b++) { |
| 981 | // Buttons 6 and 7 in the standard mapping need to be |
| 982 | // axis to be handled as JoyAxis::TRIGGER by Godot. |
| 983 | if (s_standard && (b == 6)) { |
| 984 | input->joy_axis(idx, JoyAxis::TRIGGER_LEFT, s_btns[b]); |
| 985 | } else if (s_standard && (b == 7)) { |
| 986 | input->joy_axis(idx, JoyAxis::TRIGGER_RIGHT, s_btns[b]); |
| 987 | } else { |
| 988 | input->joy_button(idx, (JoyButton)b, s_btns[b]); |
| 989 | } |
| 990 | } |
| 991 | for (int a = 0; a < s_axes_num; a++) { |
| 992 | input->joy_axis(idx, (JoyAxis)a, s_axes[a]); |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | Vector<String> DisplayServerWeb::get_rendering_drivers_func() { |
| 998 | Vector<String> drivers; |
| 999 | #ifdef GLES3_ENABLED |
| 1000 | drivers.push_back("opengl3"); |
| 1001 | #endif |
| 1002 | return drivers; |
| 1003 | } |
| 1004 | |
| 1005 | // Clipboard |
| 1006 | void DisplayServerWeb::update_clipboard_callback(const char *p_text) { |
| 1007 | String text = String::utf8(p_text); |
| 1008 | |
| 1009 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 1010 | if (!Thread::is_main_thread()) { |
| 1011 | callable_mp_static(DisplayServerWeb::_update_clipboard_callback).call_deferred(text); |
| 1012 | return; |
| 1013 | } |
| 1014 | #endif |
| 1015 | |
| 1016 | _update_clipboard_callback(text); |
| 1017 | } |
| 1018 | |
| 1019 | void DisplayServerWeb::_update_clipboard_callback(const String &p_text) { |
| 1020 | get_singleton()->clipboard = p_text; |
| 1021 | } |
| 1022 | |
| 1023 | void DisplayServerWeb::clipboard_set(const String &p_text) { |
| 1024 | clipboard = p_text; |
| 1025 | int err = godot_js_display_clipboard_set(p_text.utf8().get_data()); |
| 1026 | ERR_FAIL_COND_MSG(err, "Clipboard API is not supported."); |
| 1027 | } |
| 1028 | |
| 1029 | String DisplayServerWeb::clipboard_get() const { |
| 1030 | godot_js_display_clipboard_get(update_clipboard_callback); |
| 1031 | return clipboard; |
| 1032 | } |
| 1033 | |
| 1034 | void DisplayServerWeb::send_window_event_callback(int p_notification) { |
| 1035 | #ifdef PROXY_TO_PTHREAD_ENABLED |
| 1036 | if (!Thread::is_main_thread()) { |
| 1037 | callable_mp_static(DisplayServerWeb::_send_window_event_callback).call_deferred(p_notification); |
| 1038 | return; |
| 1039 | } |
| 1040 | #endif |
| 1041 | |
| 1042 | _send_window_event_callback(p_notification); |
| 1043 | } |
| 1044 | |
| 1045 | void DisplayServerWeb::_send_window_event_callback(int p_notification) { |
| 1046 | DisplayServerWeb *ds = get_singleton(); |
| 1047 | if (!ds) { |
| 1048 | return; |
| 1049 | } |
| 1050 | if (p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER || p_notification == DisplayServer::WINDOW_EVENT_MOUSE_EXIT) { |
| 1051 | ds->cursor_inside_canvas = p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER; |
| 1052 | } |
| 1053 | if (godot_js_is_ime_focused() && (p_notification == DisplayServer::WINDOW_EVENT_FOCUS_IN || p_notification == DisplayServer::WINDOW_EVENT_FOCUS_OUT)) { |
| 1054 | return; |
| 1055 | } |
| 1056 | if (ds->window_event_callback.is_valid()) { |
| 1057 | Variant event = int(p_notification); |
| 1058 | ds->window_event_callback.call(event); |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | void DisplayServerWeb::set_icon(const Ref<Image> &p_icon) { |
| 1063 | if (p_icon.is_valid()) { |
| 1064 | ERR_FAIL_COND(p_icon->get_width() <= 0 || p_icon->get_height() <= 0); |
| 1065 | |
| 1066 | Ref<Image> icon = p_icon; |
| 1067 | if (icon->is_compressed()) { |
| 1068 | icon = icon->duplicate(); |
| 1069 | ERR_FAIL_COND(icon->decompress() != OK); |
| 1070 | } |
| 1071 | if (icon->get_format() != Image::FORMAT_RGBA8) { |
| 1072 | if (icon == p_icon) { |
| 1073 | icon = icon->duplicate(); |
| 1074 | } |
| 1075 | icon->convert(Image::FORMAT_RGBA8); |
| 1076 | } |
| 1077 | |
| 1078 | png_image png_meta; |
| 1079 | memset(&png_meta, 0, sizeof png_meta); |
| 1080 | png_meta.version = PNG_IMAGE_VERSION; |
| 1081 | png_meta.width = icon->get_width(); |
| 1082 | png_meta.height = icon->get_height(); |
| 1083 | png_meta.format = PNG_FORMAT_RGBA; |
| 1084 | |
| 1085 | PackedByteArray png; |
| 1086 | size_t len; |
| 1087 | PackedByteArray data = icon->get_data(); |
| 1088 | ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr)); |
| 1089 | |
| 1090 | png.resize(len); |
| 1091 | ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr)); |
| 1092 | |
| 1093 | godot_js_display_window_icon_set(png.ptr(), len); |
| 1094 | } else { |
| 1095 | godot_js_display_window_icon_set(nullptr, 0); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | void DisplayServerWeb::_dispatch_input_event(const Ref<InputEvent> &p_event) { |
| 1100 | Callable cb = get_singleton()->input_event_callback; |
| 1101 | if (cb.is_valid()) { |
| 1102 | cb.call(p_event); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | DisplayServer *DisplayServerWeb::create_func(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Point2i *p_position, const Size2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) { |
| 1107 | return memnew(DisplayServerWeb(p_rendering_driver, p_window_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, p_context, p_parent_window, r_error)); |
| 1108 | } |
| 1109 | |
| 1110 | DisplayServerWeb::DisplayServerWeb(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Point2i *p_position, const Size2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) { |
| 1111 | r_error = OK; // Always succeeds for now. |
| 1112 | |
| 1113 | native_menu = memnew(NativeMenu); // Dummy native menu. |
| 1114 | |
| 1115 | // Ensure the canvas ID. |
| 1116 | godot_js_config_canvas_id_get(canvas_id, 256); |
| 1117 | |
| 1118 | // Handle contextmenu, webglcontextlost |
| 1119 | godot_js_display_setup_canvas(p_resolution.x, p_resolution.y, (p_window_mode == WINDOW_MODE_FULLSCREEN || p_window_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN), OS::get_singleton()->is_hidpi_allowed() ? 1 : 0); |
| 1120 | |
| 1121 | // Check if it's windows. |
| 1122 | swap_cancel_ok = godot_js_display_is_swap_ok_cancel() == 1; |
| 1123 | |
| 1124 | // Expose method for requesting quit. |
| 1125 | godot_js_os_request_quit_cb(request_quit_callback); |
| 1126 | |
| 1127 | #ifdef GLES3_ENABLED |
| 1128 | bool webgl2_inited = false; |
| 1129 | if (godot_js_display_has_webgl(2)) { |
| 1130 | EmscriptenWebGLContextAttributes attributes; |
| 1131 | emscripten_webgl_init_context_attributes(&attributes); |
| 1132 | attributes.alpha = OS::get_singleton()->is_layered_allowed(); |
| 1133 | attributes.antialias = false; |
| 1134 | attributes.majorVersion = 2; |
| 1135 | attributes.explicitSwapControl = true; |
| 1136 | |
| 1137 | webgl_ctx = emscripten_webgl_create_context(canvas_id, &attributes); |
| 1138 | webgl2_inited = webgl_ctx && emscripten_webgl_make_context_current(webgl_ctx) == EMSCRIPTEN_RESULT_SUCCESS; |
| 1139 | } |
| 1140 | if (webgl2_inited) { |
| 1141 | if (!emscripten_webgl_enable_extension(webgl_ctx, "OVR_multiview2")) { |
| 1142 | print_verbose("Failed to enable WebXR extension."); |
| 1143 | } |
| 1144 | RasterizerGLES3::make_current(false); |
| 1145 | |
| 1146 | } else { |
| 1147 | OS::get_singleton()->alert( |
| 1148 | "Your browser seems not to support WebGL 2.\n\n" |
| 1149 | "If possible, consider updating your browser version and video card drivers.", |
| 1150 | "Unable to initialize WebGL 2 video driver"); |
| 1151 | RasterizerDummy::make_current(); |
| 1152 | } |
| 1153 | #else |
| 1154 | RasterizerDummy::make_current(); |
| 1155 | #endif |
| 1156 | |
| 1157 | // JS Input interface (js/libs/library_godot_input.js) |
| 1158 | godot_js_input_mouse_button_cb(&DisplayServerWeb::mouse_button_callback); |
| 1159 | godot_js_input_mouse_move_cb(&DisplayServerWeb::mouse_move_callback); |
| 1160 | godot_js_input_mouse_wheel_cb(&DisplayServerWeb::mouse_wheel_callback); |
| 1161 | godot_js_input_touch_cb(&DisplayServerWeb::touch_callback, touch_event.identifier, touch_event.coords); |
| 1162 | godot_js_input_key_cb(&DisplayServerWeb::key_callback, key_event.code, key_event.key); |
| 1163 | godot_js_input_paste_cb(&DisplayServerWeb::update_clipboard_callback); |
| 1164 | godot_js_input_drop_files_cb(&DisplayServerWeb::drop_files_js_callback); |
| 1165 | godot_js_input_gamepad_cb(&DisplayServerWeb::gamepad_callback); |
| 1166 | godot_js_set_ime_cb(&DisplayServerWeb::ime_callback, &DisplayServerWeb::key_callback, key_event.code, key_event.key); |
| 1167 | |
| 1168 | // JS Display interface (js/libs/library_godot_display.js) |
| 1169 | godot_js_display_fullscreen_cb(&DisplayServerWeb::fullscreen_change_callback); |
| 1170 | godot_js_display_window_blur_cb(&DisplayServerWeb::window_blur_callback); |
| 1171 | godot_js_display_notification_cb(&DisplayServerWeb::send_window_event_callback, |
| 1172 | WINDOW_EVENT_MOUSE_ENTER, |
| 1173 | WINDOW_EVENT_MOUSE_EXIT, |
| 1174 | WINDOW_EVENT_FOCUS_IN, |
| 1175 | WINDOW_EVENT_FOCUS_OUT); |
| 1176 | godot_js_display_vk_cb(&DisplayServerWeb::vk_input_text_callback); |
| 1177 | |
| 1178 | Input::get_singleton()->set_event_dispatch_function(_dispatch_input_event); |
| 1179 | } |
| 1180 | |
| 1181 | DisplayServerWeb::~DisplayServerWeb() { |
| 1182 | if (native_menu) { |
| 1183 | memdelete(native_menu); |
| 1184 | native_menu = nullptr; |
| 1185 | } |
| 1186 | #ifdef GLES3_ENABLED |
| 1187 | if (webgl_ctx) { |
| 1188 | emscripten_webgl_commit_frame(); |
| 1189 | emscripten_webgl_destroy_context(webgl_ctx); |
| 1190 | } |
| 1191 | #endif |
| 1192 | } |
| 1193 | |
| 1194 | bool DisplayServerWeb::has_feature(Feature p_feature) const { |
| 1195 | switch (p_feature) { |
| 1196 | #ifndef DISABLE_DEPRECATED |
| 1197 | case FEATURE_GLOBAL_MENU: { |
| 1198 | return (native_menu && native_menu->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)); |
| 1199 | } break; |
| 1200 | #endif |
| 1201 | //case FEATURE_HIDPI: |
| 1202 | case FEATURE_ICON: |
| 1203 | case FEATURE_CLIPBOARD: |
| 1204 | case FEATURE_CURSOR_SHAPE: |
| 1205 | case FEATURE_CUSTOM_CURSOR_SHAPE: |
| 1206 | case FEATURE_MOUSE: |
| 1207 | case FEATURE_TOUCHSCREEN: |
| 1208 | return true; |
| 1209 | //case FEATURE_MOUSE_WARP: |
| 1210 | //case FEATURE_NATIVE_DIALOG: |
| 1211 | //case FEATURE_NATIVE_DIALOG_INPUT: |
| 1212 | //case FEATURE_NATIVE_DIALOG_FILE: |
| 1213 | //case FEATURE_NATIVE_DIALOG_FILE_EXTRA: |
| 1214 | //case FEATURE_NATIVE_DIALOG_FILE_MIME: |
| 1215 | //case FEATURE_NATIVE_ICON: |
| 1216 | //case FEATURE_WINDOW_TRANSPARENCY: |
| 1217 | //case FEATURE_KEEP_SCREEN_ON: |
| 1218 | //case FEATURE_ORIENTATION: |
| 1219 | case FEATURE_IME: |
| 1220 | // IME does not work with experimental VK support. |
| 1221 | return godot_js_display_vk_available() == 0; |
| 1222 | case FEATURE_VIRTUAL_KEYBOARD: |
| 1223 | return godot_js_display_vk_available() != 0; |
| 1224 | case FEATURE_TEXT_TO_SPEECH: |
| 1225 | return godot_js_display_tts_available() != 0; |
| 1226 | default: |
| 1227 | return false; |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | void DisplayServerWeb::register_web_driver() { |
| 1232 | register_create_function("web", create_func, get_rendering_drivers_func); |
| 1233 | } |
| 1234 | |
| 1235 | String DisplayServerWeb::get_name() const { |
| 1236 | return "web"; |
| 1237 | } |
| 1238 | |
| 1239 | int DisplayServerWeb::get_screen_count() const { |
| 1240 | return 1; |
| 1241 | } |
| 1242 | |
| 1243 | int DisplayServerWeb::get_primary_screen() const { |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | Point2i DisplayServerWeb::screen_get_position(int p_screen) const { |
| 1248 | p_screen = _get_screen_index(p_screen); |
| 1249 | int screen_count = get_screen_count(); |
| 1250 | ERR_FAIL_INDEX_V(p_screen, screen_count, Point2i()); |
| 1251 | |
| 1252 | return Point2i(0, 0); // TODO offsetX/Y? |
| 1253 | } |
| 1254 | |
| 1255 | Size2i DisplayServerWeb::screen_get_size(int p_screen) const { |
| 1256 | p_screen = _get_screen_index(p_screen); |
| 1257 | int screen_count = get_screen_count(); |
| 1258 | ERR_FAIL_INDEX_V(p_screen, screen_count, Size2i()); |
| 1259 | |
| 1260 | int size[2]; |
| 1261 | godot_js_display_screen_size_get(size, size + 1); |
| 1262 | return Size2(size[0], size[1]); |
| 1263 | } |
| 1264 | |
| 1265 | Rect2i DisplayServerWeb::screen_get_usable_rect(int p_screen) const { |
| 1266 | p_screen = _get_screen_index(p_screen); |
| 1267 | int screen_count = get_screen_count(); |
| 1268 | ERR_FAIL_INDEX_V(p_screen, screen_count, Rect2i()); |
| 1269 | |
| 1270 | int size[2]; |
| 1271 | godot_js_display_window_size_get(size, size + 1); |
| 1272 | return Rect2i(0, 0, size[0], size[1]); |
| 1273 | } |
| 1274 | |
| 1275 | int DisplayServerWeb::screen_get_dpi(int p_screen) const { |
| 1276 | p_screen = _get_screen_index(p_screen); |
| 1277 | int screen_count = get_screen_count(); |
| 1278 | ERR_FAIL_INDEX_V(p_screen, screen_count, 72); |
| 1279 | |
| 1280 | return godot_js_display_screen_dpi_get(); |
| 1281 | } |
| 1282 | |
| 1283 | float DisplayServerWeb::screen_get_scale(int p_screen) const { |
| 1284 | p_screen = _get_screen_index(p_screen); |
| 1285 | int screen_count = get_screen_count(); |
| 1286 | ERR_FAIL_INDEX_V(p_screen, screen_count, 1.0f); |
| 1287 | |
| 1288 | return godot_js_display_pixel_ratio_get(); |
| 1289 | } |
| 1290 | |
| 1291 | float DisplayServerWeb::screen_get_refresh_rate(int p_screen) const { |
| 1292 | p_screen = _get_screen_index(p_screen); |
| 1293 | int screen_count = get_screen_count(); |
| 1294 | ERR_FAIL_INDEX_V(p_screen, screen_count, SCREEN_REFRESH_RATE_FALLBACK); |
| 1295 | |
| 1296 | return SCREEN_REFRESH_RATE_FALLBACK; // Web doesn't have much of a need for the screen refresh rate, and there's no native way to do so. |
| 1297 | } |
| 1298 | |
| 1299 | Vector<DisplayServer::WindowID> DisplayServerWeb::get_window_list() const { |
| 1300 | Vector<WindowID> ret; |
| 1301 | ret.push_back(MAIN_WINDOW_ID); |
| 1302 | return ret; |
| 1303 | } |
| 1304 | |
| 1305 | DisplayServerWeb::WindowID DisplayServerWeb::get_window_at_screen_position(const Point2i &p_position) const { |
| 1306 | return MAIN_WINDOW_ID; |
| 1307 | } |
| 1308 | |
| 1309 | void DisplayServerWeb::window_attach_instance_id(ObjectID p_instance, WindowID p_window) { |
| 1310 | window_attached_instance_id = p_instance; |
| 1311 | } |
| 1312 | |
| 1313 | ObjectID DisplayServerWeb::window_get_attached_instance_id(WindowID p_window) const { |
| 1314 | return window_attached_instance_id; |
| 1315 | } |
| 1316 | |
| 1317 | void DisplayServerWeb::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) { |
| 1318 | rect_changed_callback = p_callable; |
| 1319 | } |
| 1320 | |
| 1321 | void DisplayServerWeb::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) { |
| 1322 | window_event_callback = p_callable; |
| 1323 | } |
| 1324 | |
| 1325 | void DisplayServerWeb::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) { |
| 1326 | input_event_callback = p_callable; |
| 1327 | } |
| 1328 | |
| 1329 | void DisplayServerWeb::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) { |
| 1330 | input_text_callback = p_callable; |
| 1331 | } |
| 1332 | |
| 1333 | void DisplayServerWeb::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) { |
| 1334 | drop_files_callback = p_callable; |
| 1335 | } |
| 1336 | |
| 1337 | void DisplayServerWeb::window_set_title(const String &p_title, WindowID p_window) { |
| 1338 | godot_js_display_window_title_set(p_title.utf8().get_data()); |
| 1339 | } |
| 1340 | |
| 1341 | int DisplayServerWeb::window_get_current_screen(WindowID p_window) const { |
| 1342 | ERR_FAIL_COND_V(p_window != MAIN_WINDOW_ID, INVALID_SCREEN); |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | void DisplayServerWeb::window_set_current_screen(int p_screen, WindowID p_window) { |
| 1347 | // Not implemented. |
| 1348 | } |
| 1349 | |
| 1350 | Point2i DisplayServerWeb::window_get_position(WindowID p_window) const { |
| 1351 | return Point2i(); |
| 1352 | } |
| 1353 | |
| 1354 | Point2i DisplayServerWeb::window_get_position_with_decorations(WindowID p_window) const { |
| 1355 | return Point2i(); |
| 1356 | } |
| 1357 | |
| 1358 | void DisplayServerWeb::window_set_position(const Point2i &p_position, WindowID p_window) { |
| 1359 | // Not supported. |
| 1360 | } |
| 1361 | |
| 1362 | void DisplayServerWeb::window_set_transient(WindowID p_window, WindowID p_parent) { |
| 1363 | // Not supported. |
| 1364 | } |
| 1365 | |
| 1366 | void DisplayServerWeb::window_set_max_size(const Size2i p_size, WindowID p_window) { |
| 1367 | // Not supported. |
| 1368 | } |
| 1369 | |
| 1370 | Size2i DisplayServerWeb::window_get_max_size(WindowID p_window) const { |
| 1371 | return Size2i(); |
| 1372 | } |
| 1373 | |
| 1374 | void DisplayServerWeb::window_set_min_size(const Size2i p_size, WindowID p_window) { |
| 1375 | // Not supported. |
| 1376 | } |
| 1377 | |
| 1378 | Size2i DisplayServerWeb::window_get_min_size(WindowID p_window) const { |
| 1379 | return Size2i(); |
| 1380 | } |
| 1381 | |
| 1382 | void DisplayServerWeb::window_set_size(const Size2i p_size, WindowID p_window) { |
| 1383 | godot_js_display_desired_size_set(p_size.x, p_size.y); |
| 1384 | } |
| 1385 | |
| 1386 | Size2i DisplayServerWeb::window_get_size(WindowID p_window) const { |
| 1387 | int size[2]; |
| 1388 | godot_js_display_window_size_get(size, size + 1); |
| 1389 | return Size2i(size[0], size[1]); |
| 1390 | } |
| 1391 | |
| 1392 | Size2i DisplayServerWeb::window_get_size_with_decorations(WindowID p_window) const { |
| 1393 | return window_get_size(p_window); |
| 1394 | } |
| 1395 | |
| 1396 | void DisplayServerWeb::window_set_mode(WindowMode p_mode, WindowID p_window) { |
| 1397 | if (window_mode == p_mode) { |
| 1398 | return; |
| 1399 | } |
| 1400 | |
| 1401 | switch (p_mode) { |
| 1402 | case WINDOW_MODE_WINDOWED: { |
| 1403 | if (window_mode == WINDOW_MODE_FULLSCREEN) { |
| 1404 | godot_js_display_fullscreen_exit(); |
| 1405 | } |
| 1406 | window_mode = WINDOW_MODE_WINDOWED; |
| 1407 | } break; |
| 1408 | case WINDOW_MODE_EXCLUSIVE_FULLSCREEN: |
| 1409 | case WINDOW_MODE_FULLSCREEN: { |
| 1410 | int result = godot_js_display_fullscreen_request(); |
| 1411 | ERR_FAIL_COND_MSG(result, "The request was denied. Remember that enabling fullscreen is only possible from an input callback for the Web platform."); |
| 1412 | } break; |
| 1413 | case WINDOW_MODE_MAXIMIZED: |
| 1414 | case WINDOW_MODE_MINIMIZED: |
| 1415 | // WindowMode MAXIMIZED and MINIMIZED are not supported in Web platform. |
| 1416 | break; |
| 1417 | default: |
| 1418 | break; |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | DisplayServerWeb::WindowMode DisplayServerWeb::window_get_mode(WindowID p_window) const { |
| 1423 | return window_mode; |
| 1424 | } |
| 1425 | |
| 1426 | bool DisplayServerWeb::window_is_maximize_allowed(WindowID p_window) const { |
| 1427 | return false; |
| 1428 | } |
| 1429 | |
| 1430 | void DisplayServerWeb::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) { |
| 1431 | // Not supported. |
| 1432 | } |
| 1433 | |
| 1434 | bool DisplayServerWeb::window_get_flag(WindowFlags p_flag, WindowID p_window) const { |
| 1435 | return false; |
| 1436 | } |
| 1437 | |
| 1438 | void DisplayServerWeb::window_request_attention(WindowID p_window) { |
| 1439 | // Not supported. |
| 1440 | } |
| 1441 | |
| 1442 | void DisplayServerWeb::window_move_to_foreground(WindowID p_window) { |
| 1443 | // Not supported. |
| 1444 | } |
| 1445 | |
| 1446 | bool DisplayServerWeb::window_is_focused(WindowID p_window) const { |
| 1447 | return true; |
| 1448 | } |
| 1449 | |
| 1450 | bool DisplayServerWeb::window_can_draw(WindowID p_window) const { |
| 1451 | return true; |
| 1452 | } |
| 1453 | |
| 1454 | bool DisplayServerWeb::can_any_window_draw() const { |
| 1455 | return true; |
| 1456 | } |
| 1457 | |
| 1458 | DisplayServer::VSyncMode DisplayServerWeb::window_get_vsync_mode(WindowID p_vsync_mode) const { |
| 1459 | return DisplayServer::VSYNC_ENABLED; |
| 1460 | } |
| 1461 | |
| 1462 | void DisplayServerWeb::process_events() { |
| 1463 | process_keys(); |
| 1464 | Input::get_singleton()->flush_buffered_events(); |
| 1465 | |
| 1466 | if (gamepad_count > 0) { |
| 1467 | if (godot_js_input_gamepad_sample() == OK) { |
| 1468 | process_joypads(); |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | void DisplayServerWeb::process_keys() { |
| 1474 | for (int i = 0; i < key_event_pos; i++) { |
| 1475 | const DisplayServerWeb::KeyEvent &ke = key_event_buffer[i]; |
| 1476 | |
| 1477 | Ref<InputEventKey> ev; |
| 1478 | ev.instantiate(); |
| 1479 | ev->set_pressed(ke.pressed); |
| 1480 | ev->set_echo(ke.echo); |
| 1481 | ev->set_keycode(ke.keycode); |
| 1482 | ev->set_physical_keycode(ke.physical_keycode); |
| 1483 | ev->set_key_label(ke.key_label); |
| 1484 | ev->set_unicode(ke.unicode); |
| 1485 | ev->set_location(ke.location); |
| 1486 | if (ke.raw) { |
| 1487 | dom2godot_mod(ev, ke.mod, ke.keycode); |
| 1488 | } |
| 1489 | |
| 1490 | Input::get_singleton()->parse_input_event(ev); |
| 1491 | } |
| 1492 | key_event_pos = 0; |
| 1493 | } |
| 1494 | |
| 1495 | int DisplayServerWeb::get_current_video_driver() const { |
| 1496 | return 1; |
| 1497 | } |
| 1498 | |
| 1499 | bool DisplayServerWeb::get_swap_cancel_ok() { |
| 1500 | return swap_cancel_ok; |
| 1501 | } |
| 1502 | |
| 1503 | void DisplayServerWeb::swap_buffers() { |
| 1504 | #ifdef GLES3_ENABLED |
| 1505 | if (webgl_ctx) { |
| 1506 | emscripten_webgl_commit_frame(); |
| 1507 | } |
| 1508 | #endif |
| 1509 | } |