display_server_web.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. #include "display_server_web.h"
  31. #include "dom_keys.inc"
  32. #include "godot_js.h"
  33. #include "os_web.h"
  34. #include "core/config/project_settings.h"
  35. #include "servers/rendering/dummy/rasterizer_dummy.h"
  36. #ifdef GLES3_ENABLED
  37. #include "drivers/gles3/rasterizer_gles3.h"
  38. #endif
  39. #include <emscripten.h>
  40. #include <png.h>
  41. #define DOM_BUTTON_LEFT 0
  42. #define DOM_BUTTON_MIDDLE 1
  43. #define DOM_BUTTON_RIGHT 2
  44. #define DOM_BUTTON_XBUTTON1 3
  45. #define DOM_BUTTON_XBUTTON2 4
  46. DisplayServerWeb *DisplayServerWeb::get_singleton() {
  47. return static_cast<DisplayServerWeb *>(DisplayServer::get_singleton());
  48. }
  49. // Window (canvas)
  50. bool DisplayServerWeb::check_size_force_redraw() {
  51. bool size_changed = godot_js_display_size_update() != 0;
  52. if (size_changed && !rect_changed_callback.is_null()) {
  53. Variant size = Rect2i(Point2i(), window_get_size()); // TODO use window_get_position if implemented.
  54. Variant *vp = &size;
  55. Variant ret;
  56. Callable::CallError ce;
  57. rect_changed_callback.callp((const Variant **)&vp, 1, ret, ce);
  58. }
  59. return size_changed;
  60. }
  61. void DisplayServerWeb::fullscreen_change_callback(int p_fullscreen) {
  62. DisplayServerWeb *display = get_singleton();
  63. if (p_fullscreen) {
  64. display->window_mode = WINDOW_MODE_FULLSCREEN;
  65. } else {
  66. display->window_mode = WINDOW_MODE_WINDOWED;
  67. }
  68. }
  69. // Drag and drop callback.
  70. void DisplayServerWeb::drop_files_js_callback(char **p_filev, int p_filec) {
  71. DisplayServerWeb *ds = get_singleton();
  72. if (!ds) {
  73. ERR_FAIL_MSG("Unable to drop files because the DisplayServer is not active");
  74. }
  75. if (ds->drop_files_callback.is_null()) {
  76. return;
  77. }
  78. Vector<String> files;
  79. for (int i = 0; i < p_filec; i++) {
  80. files.push_back(String::utf8(p_filev[i]));
  81. }
  82. Variant v = files;
  83. Variant *vp = &v;
  84. Variant ret;
  85. Callable::CallError ce;
  86. ds->drop_files_callback.callp((const Variant **)&vp, 1, ret, ce);
  87. }
  88. // Web quit request callback.
  89. void DisplayServerWeb::request_quit_callback() {
  90. DisplayServerWeb *ds = get_singleton();
  91. if (ds && !ds->window_event_callback.is_null()) {
  92. Variant event = int(DisplayServer::WINDOW_EVENT_CLOSE_REQUEST);
  93. Variant *eventp = &event;
  94. Variant ret;
  95. Callable::CallError ce;
  96. ds->window_event_callback.callp((const Variant **)&eventp, 1, ret, ce);
  97. }
  98. }
  99. // Keys
  100. void DisplayServerWeb::dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod, Key p_keycode) {
  101. if (p_keycode != Key::SHIFT) {
  102. ev->set_shift_pressed(p_mod & 1);
  103. }
  104. if (p_keycode != Key::ALT) {
  105. ev->set_alt_pressed(p_mod & 2);
  106. }
  107. if (p_keycode != Key::CTRL) {
  108. ev->set_ctrl_pressed(p_mod & 4);
  109. }
  110. if (p_keycode != Key::META) {
  111. ev->set_meta_pressed(p_mod & 8);
  112. }
  113. }
  114. void DisplayServerWeb::key_callback(int p_pressed, int p_repeat, int p_modifiers) {
  115. DisplayServerWeb *ds = get_singleton();
  116. JSKeyEvent &key_event = ds->key_event;
  117. // Resume audio context after input in case autoplay was denied.
  118. OS_Web::get_singleton()->resume_audio();
  119. char32_t c = 0x00;
  120. String unicode = String::utf8(key_event.key);
  121. if (unicode.length() == 1) {
  122. c = unicode[0];
  123. }
  124. Key keycode = dom_code2godot_scancode(key_event.code, key_event.key, false);
  125. Key scancode = dom_code2godot_scancode(key_event.code, key_event.key, true);
  126. Ref<InputEventKey> ev;
  127. ev.instantiate();
  128. ev->set_echo(p_repeat);
  129. ev->set_keycode(fix_keycode(c, keycode));
  130. ev->set_physical_keycode(scancode);
  131. ev->set_key_label(fix_key_label(c, keycode));
  132. ev->set_unicode(fix_unicode(c));
  133. ev->set_pressed(p_pressed);
  134. dom2godot_mod(ev, p_modifiers, fix_keycode(c, keycode));
  135. Input::get_singleton()->parse_input_event(ev);
  136. // Make sure to flush all events so we can call restricted APIs inside the event.
  137. Input::get_singleton()->flush_buffered_events();
  138. }
  139. // Mouse
  140. int DisplayServerWeb::mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers) {
  141. DisplayServerWeb *ds = get_singleton();
  142. Point2 pos(p_x, p_y);
  143. Ref<InputEventMouseButton> ev;
  144. ev.instantiate();
  145. ev->set_position(pos);
  146. ev->set_global_position(pos);
  147. ev->set_pressed(p_pressed);
  148. dom2godot_mod(ev, p_modifiers, Key::NONE);
  149. switch (p_button) {
  150. case DOM_BUTTON_LEFT:
  151. ev->set_button_index(MouseButton::LEFT);
  152. break;
  153. case DOM_BUTTON_MIDDLE:
  154. ev->set_button_index(MouseButton::MIDDLE);
  155. break;
  156. case DOM_BUTTON_RIGHT:
  157. ev->set_button_index(MouseButton::RIGHT);
  158. break;
  159. case DOM_BUTTON_XBUTTON1:
  160. ev->set_button_index(MouseButton::MB_XBUTTON1);
  161. break;
  162. case DOM_BUTTON_XBUTTON2:
  163. ev->set_button_index(MouseButton::MB_XBUTTON2);
  164. break;
  165. default:
  166. return false;
  167. }
  168. if (p_pressed) {
  169. uint64_t diff = (OS::get_singleton()->get_ticks_usec() / 1000) - ds->last_click_ms;
  170. if (ev->get_button_index() == ds->last_click_button_index) {
  171. if (diff < 400 && Point2(ds->last_click_pos).distance_to(ev->get_position()) < 5) {
  172. ds->last_click_ms = 0;
  173. ds->last_click_pos = Point2(-100, -100);
  174. ds->last_click_button_index = MouseButton::NONE;
  175. ev->set_double_click(true);
  176. }
  177. } else {
  178. ds->last_click_button_index = ev->get_button_index();
  179. }
  180. if (!ev->is_double_click()) {
  181. ds->last_click_ms += diff;
  182. ds->last_click_pos = ev->get_position();
  183. }
  184. }
  185. BitField<MouseButtonMask> mask = Input::get_singleton()->get_mouse_button_mask();
  186. MouseButtonMask button_flag = mouse_button_to_mask(ev->get_button_index());
  187. if (ev->is_pressed()) {
  188. mask.set_flag(button_flag);
  189. } else if (mask.has_flag(button_flag)) {
  190. mask.clear_flag(button_flag);
  191. } else {
  192. // Received release event, but press was outside the canvas, so ignore.
  193. return false;
  194. }
  195. ev->set_button_mask(mask);
  196. Input::get_singleton()->parse_input_event(ev);
  197. // Resume audio context after input in case autoplay was denied.
  198. OS_Web::get_singleton()->resume_audio();
  199. // Make sure to flush all events so we can call restricted APIs inside the event.
  200. Input::get_singleton()->flush_buffered_events();
  201. // Prevent multi-click text selection and wheel-click scrolling anchor.
  202. // Context menu is prevented through contextmenu event.
  203. return true;
  204. }
  205. void DisplayServerWeb::mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers) {
  206. BitField<MouseButtonMask> input_mask = Input::get_singleton()->get_mouse_button_mask();
  207. // For motion outside the canvas, only read mouse movement if dragging
  208. // started inside the canvas; imitating desktop app behavior.
  209. if (!get_singleton()->cursor_inside_canvas && input_mask.is_empty()) {
  210. return;
  211. }
  212. Point2 pos(p_x, p_y);
  213. Ref<InputEventMouseMotion> ev;
  214. ev.instantiate();
  215. dom2godot_mod(ev, p_modifiers, Key::NONE);
  216. ev->set_button_mask(input_mask);
  217. ev->set_position(pos);
  218. ev->set_global_position(pos);
  219. ev->set_relative(Vector2(p_rel_x, p_rel_y));
  220. ev->set_velocity(Input::get_singleton()->get_last_mouse_velocity());
  221. Input::get_singleton()->parse_input_event(ev);
  222. }
  223. // Cursor
  224. const char *DisplayServerWeb::godot2dom_cursor(DisplayServer::CursorShape p_shape) {
  225. switch (p_shape) {
  226. case DisplayServer::CURSOR_ARROW:
  227. return "default";
  228. case DisplayServer::CURSOR_IBEAM:
  229. return "text";
  230. case DisplayServer::CURSOR_POINTING_HAND:
  231. return "pointer";
  232. case DisplayServer::CURSOR_CROSS:
  233. return "crosshair";
  234. case DisplayServer::CURSOR_WAIT:
  235. return "wait";
  236. case DisplayServer::CURSOR_BUSY:
  237. return "progress";
  238. case DisplayServer::CURSOR_DRAG:
  239. return "grab";
  240. case DisplayServer::CURSOR_CAN_DROP:
  241. return "grabbing";
  242. case DisplayServer::CURSOR_FORBIDDEN:
  243. return "no-drop";
  244. case DisplayServer::CURSOR_VSIZE:
  245. return "ns-resize";
  246. case DisplayServer::CURSOR_HSIZE:
  247. return "ew-resize";
  248. case DisplayServer::CURSOR_BDIAGSIZE:
  249. return "nesw-resize";
  250. case DisplayServer::CURSOR_FDIAGSIZE:
  251. return "nwse-resize";
  252. case DisplayServer::CURSOR_MOVE:
  253. return "move";
  254. case DisplayServer::CURSOR_VSPLIT:
  255. return "row-resize";
  256. case DisplayServer::CURSOR_HSPLIT:
  257. return "col-resize";
  258. case DisplayServer::CURSOR_HELP:
  259. return "help";
  260. default:
  261. return "default";
  262. }
  263. }
  264. bool DisplayServerWeb::tts_is_speaking() const {
  265. ERR_FAIL_COND_V_MSG(!tts, false, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  266. return godot_js_tts_is_speaking();
  267. }
  268. bool DisplayServerWeb::tts_is_paused() const {
  269. ERR_FAIL_COND_V_MSG(!tts, false, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  270. return godot_js_tts_is_paused();
  271. }
  272. void DisplayServerWeb::update_voices_callback(int p_size, const char **p_voice) {
  273. get_singleton()->voices.clear();
  274. for (int i = 0; i < p_size; i++) {
  275. Vector<String> tokens = String::utf8(p_voice[i]).split(";", true, 2);
  276. if (tokens.size() == 2) {
  277. Dictionary voice_d;
  278. voice_d["name"] = tokens[1];
  279. voice_d["id"] = tokens[1];
  280. voice_d["language"] = tokens[0];
  281. get_singleton()->voices.push_back(voice_d);
  282. }
  283. }
  284. }
  285. TypedArray<Dictionary> DisplayServerWeb::tts_get_voices() const {
  286. ERR_FAIL_COND_V_MSG(!tts, TypedArray<Dictionary>(), "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  287. godot_js_tts_get_voices(update_voices_callback);
  288. return voices;
  289. }
  290. void DisplayServerWeb::tts_speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int p_utterance_id, bool p_interrupt) {
  291. ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  292. if (p_interrupt) {
  293. tts_stop();
  294. }
  295. if (p_text.is_empty()) {
  296. tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, p_utterance_id);
  297. return;
  298. }
  299. CharString string = p_text.utf8();
  300. utterance_ids[p_utterance_id] = string;
  301. 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);
  302. }
  303. void DisplayServerWeb::tts_pause() {
  304. ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  305. godot_js_tts_pause();
  306. }
  307. void DisplayServerWeb::tts_resume() {
  308. ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  309. godot_js_tts_resume();
  310. }
  311. void DisplayServerWeb::tts_stop() {
  312. ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  313. for (const KeyValue<int, CharString> &E : utterance_ids) {
  314. tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, E.key);
  315. }
  316. utterance_ids.clear();
  317. godot_js_tts_stop();
  318. }
  319. void DisplayServerWeb::_js_utterance_callback(int p_event, int p_id, int p_pos) {
  320. DisplayServerWeb *ds = (DisplayServerWeb *)DisplayServer::get_singleton();
  321. if (ds->utterance_ids.has(p_id)) {
  322. int pos = 0;
  323. if ((TTSUtteranceEvent)p_event == DisplayServer::TTS_UTTERANCE_BOUNDARY) {
  324. // Convert position from UTF-8 to UTF-32.
  325. const CharString &string = ds->utterance_ids[p_id];
  326. for (int i = 0; i < MIN(p_pos, string.length()); i++) {
  327. uint8_t c = string[i];
  328. if ((c & 0xe0) == 0xc0) {
  329. i += 1;
  330. } else if ((c & 0xf0) == 0xe0) {
  331. i += 2;
  332. } else if ((c & 0xf8) == 0xf0) {
  333. i += 3;
  334. }
  335. pos++;
  336. }
  337. } else if ((TTSUtteranceEvent)p_event != DisplayServer::TTS_UTTERANCE_STARTED) {
  338. ds->utterance_ids.erase(p_id);
  339. }
  340. ds->tts_post_utterance_event((TTSUtteranceEvent)p_event, p_id, pos);
  341. }
  342. }
  343. void DisplayServerWeb::cursor_set_shape(CursorShape p_shape) {
  344. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  345. if (cursor_shape == p_shape) {
  346. return;
  347. }
  348. cursor_shape = p_shape;
  349. godot_js_display_cursor_set_shape(godot2dom_cursor(cursor_shape));
  350. }
  351. DisplayServer::CursorShape DisplayServerWeb::cursor_get_shape() const {
  352. return cursor_shape;
  353. }
  354. void DisplayServerWeb::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  355. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  356. if (p_cursor.is_valid()) {
  357. Ref<Texture2D> texture = p_cursor;
  358. ERR_FAIL_COND(!texture.is_valid());
  359. Ref<AtlasTexture> atlas_texture = p_cursor;
  360. Size2 texture_size;
  361. Rect2 atlas_rect;
  362. if (atlas_texture.is_valid()) {
  363. texture = atlas_texture->get_atlas();
  364. atlas_rect.size.width = texture->get_width();
  365. atlas_rect.size.height = texture->get_height();
  366. atlas_rect.position.x = atlas_texture->get_region().position.x;
  367. atlas_rect.position.y = atlas_texture->get_region().position.y;
  368. texture_size.width = atlas_texture->get_region().size.x;
  369. texture_size.height = atlas_texture->get_region().size.y;
  370. } else {
  371. texture_size.width = texture->get_width();
  372. texture_size.height = texture->get_height();
  373. }
  374. ERR_FAIL_COND(p_hotspot.x < 0 || p_hotspot.y < 0);
  375. ERR_FAIL_COND(texture_size.width > 256 || texture_size.height > 256);
  376. ERR_FAIL_COND(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height);
  377. Ref<Image> image = texture->get_image();
  378. ERR_FAIL_COND(!image.is_valid());
  379. image = image->duplicate(true);
  380. if (image->is_compressed()) {
  381. Error err = image->decompress();
  382. ERR_FAIL_COND_MSG(err != OK, "Couldn't decompress VRAM-compressed custom mouse cursor image. Switch to a lossless compression mode in the Import dock.");
  383. }
  384. if (atlas_texture.is_valid()) {
  385. image->crop_from_point(
  386. atlas_rect.position.x,
  387. atlas_rect.position.y,
  388. texture_size.width,
  389. texture_size.height);
  390. }
  391. if (image->get_format() != Image::FORMAT_RGBA8) {
  392. image->convert(Image::FORMAT_RGBA8);
  393. }
  394. png_image png_meta;
  395. memset(&png_meta, 0, sizeof png_meta);
  396. png_meta.version = PNG_IMAGE_VERSION;
  397. png_meta.width = texture_size.width;
  398. png_meta.height = texture_size.height;
  399. png_meta.format = PNG_FORMAT_RGBA;
  400. PackedByteArray png;
  401. size_t len;
  402. PackedByteArray data = image->get_data();
  403. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  404. png.resize(len);
  405. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  406. godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), png.ptr(), len, p_hotspot.x, p_hotspot.y);
  407. } else {
  408. godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), nullptr, 0, 0, 0);
  409. }
  410. cursor_set_shape(cursor_shape);
  411. }
  412. // Mouse mode
  413. void DisplayServerWeb::mouse_set_mode(MouseMode p_mode) {
  414. ERR_FAIL_COND_MSG(p_mode == MOUSE_MODE_CONFINED || p_mode == MOUSE_MODE_CONFINED_HIDDEN, "MOUSE_MODE_CONFINED is not supported for the Web platform.");
  415. if (p_mode == mouse_get_mode()) {
  416. return;
  417. }
  418. if (p_mode == MOUSE_MODE_VISIBLE) {
  419. godot_js_display_cursor_set_visible(1);
  420. godot_js_display_cursor_lock_set(0);
  421. } else if (p_mode == MOUSE_MODE_HIDDEN) {
  422. godot_js_display_cursor_set_visible(0);
  423. godot_js_display_cursor_lock_set(0);
  424. } else if (p_mode == MOUSE_MODE_CAPTURED) {
  425. godot_js_display_cursor_set_visible(1);
  426. godot_js_display_cursor_lock_set(1);
  427. }
  428. }
  429. DisplayServer::MouseMode DisplayServerWeb::mouse_get_mode() const {
  430. if (godot_js_display_cursor_is_hidden()) {
  431. return MOUSE_MODE_HIDDEN;
  432. }
  433. if (godot_js_display_cursor_is_locked()) {
  434. return MOUSE_MODE_CAPTURED;
  435. }
  436. return MOUSE_MODE_VISIBLE;
  437. }
  438. Point2i DisplayServerWeb::mouse_get_position() const {
  439. return Input::get_singleton()->get_mouse_position();
  440. }
  441. // Wheel
  442. int DisplayServerWeb::mouse_wheel_callback(double p_delta_x, double p_delta_y) {
  443. if (!godot_js_display_canvas_is_focused()) {
  444. if (get_singleton()->cursor_inside_canvas) {
  445. godot_js_display_canvas_focus();
  446. } else {
  447. return false;
  448. }
  449. }
  450. Input *input = Input::get_singleton();
  451. Ref<InputEventMouseButton> ev;
  452. ev.instantiate();
  453. ev->set_position(input->get_mouse_position());
  454. ev->set_global_position(ev->get_position());
  455. ev->set_shift_pressed(input->is_key_pressed(Key::SHIFT));
  456. ev->set_alt_pressed(input->is_key_pressed(Key::ALT));
  457. ev->set_ctrl_pressed(input->is_key_pressed(Key::CTRL));
  458. ev->set_meta_pressed(input->is_key_pressed(Key::META));
  459. if (p_delta_y < 0) {
  460. ev->set_button_index(MouseButton::WHEEL_UP);
  461. } else if (p_delta_y > 0) {
  462. ev->set_button_index(MouseButton::WHEEL_DOWN);
  463. } else if (p_delta_x > 0) {
  464. ev->set_button_index(MouseButton::WHEEL_LEFT);
  465. } else if (p_delta_x < 0) {
  466. ev->set_button_index(MouseButton::WHEEL_RIGHT);
  467. } else {
  468. return false;
  469. }
  470. // Different browsers give wildly different delta values, and we can't
  471. // interpret deltaMode, so use default value for wheel events' factor.
  472. MouseButtonMask button_flag = mouse_button_to_mask(ev->get_button_index());
  473. BitField<MouseButtonMask> button_mask = input->get_mouse_button_mask();
  474. button_mask.set_flag(button_flag);
  475. ev->set_pressed(true);
  476. ev->set_button_mask(button_mask);
  477. input->parse_input_event(ev);
  478. Ref<InputEventMouseButton> release = ev->duplicate();
  479. release->set_pressed(false);
  480. release->set_button_mask(input->get_mouse_button_mask());
  481. input->parse_input_event(release);
  482. return true;
  483. }
  484. // Touch
  485. void DisplayServerWeb::touch_callback(int p_type, int p_count) {
  486. DisplayServerWeb *ds = get_singleton();
  487. const JSTouchEvent &touch_event = ds->touch_event;
  488. for (int i = 0; i < p_count; i++) {
  489. Point2 point(touch_event.coords[i * 2], touch_event.coords[i * 2 + 1]);
  490. if (p_type == 2) {
  491. // touchmove
  492. Ref<InputEventScreenDrag> ev;
  493. ev.instantiate();
  494. ev->set_index(touch_event.identifier[i]);
  495. ev->set_position(point);
  496. Point2 &prev = ds->touches[i];
  497. ev->set_relative(ev->get_position() - prev);
  498. prev = ev->get_position();
  499. Input::get_singleton()->parse_input_event(ev);
  500. } else {
  501. // touchstart/touchend
  502. Ref<InputEventScreenTouch> ev;
  503. // Resume audio context after input in case autoplay was denied.
  504. OS_Web::get_singleton()->resume_audio();
  505. ev.instantiate();
  506. ev->set_index(touch_event.identifier[i]);
  507. ev->set_position(point);
  508. ev->set_pressed(p_type == 0);
  509. ds->touches[i] = point;
  510. Input::get_singleton()->parse_input_event(ev);
  511. // Make sure to flush all events so we can call restricted APIs inside the event.
  512. Input::get_singleton()->flush_buffered_events();
  513. }
  514. }
  515. }
  516. bool DisplayServerWeb::is_touchscreen_available() const {
  517. return godot_js_display_touchscreen_is_available() || (Input::get_singleton() && Input::get_singleton()->is_emulating_touch_from_mouse());
  518. }
  519. // Virtual Keyboard
  520. void DisplayServerWeb::vk_input_text_callback(const char *p_text, int p_cursor) {
  521. DisplayServerWeb *ds = DisplayServerWeb::get_singleton();
  522. if (!ds || ds->input_text_callback.is_null()) {
  523. return;
  524. }
  525. // Call input_text
  526. Variant event = String::utf8(p_text);
  527. Variant *eventp = &event;
  528. Variant ret;
  529. Callable::CallError ce;
  530. ds->input_text_callback.callp((const Variant **)&eventp, 1, ret, ce);
  531. // Insert key right to reach position.
  532. Input *input = Input::get_singleton();
  533. Ref<InputEventKey> k;
  534. for (int i = 0; i < p_cursor; i++) {
  535. k.instantiate();
  536. k->set_pressed(true);
  537. k->set_echo(false);
  538. k->set_keycode(Key::RIGHT);
  539. input->parse_input_event(k);
  540. k.instantiate();
  541. k->set_pressed(false);
  542. k->set_echo(false);
  543. k->set_keycode(Key::RIGHT);
  544. input->parse_input_event(k);
  545. }
  546. }
  547. 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) {
  548. godot_js_display_vk_show(p_existing_text.utf8().get_data(), p_type, p_cursor_start, p_cursor_end);
  549. }
  550. void DisplayServerWeb::virtual_keyboard_hide() {
  551. godot_js_display_vk_hide();
  552. }
  553. void DisplayServerWeb::window_blur_callback() {
  554. Input::get_singleton()->release_pressed_events();
  555. }
  556. // Gamepad
  557. void DisplayServerWeb::gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid) {
  558. Input *input = Input::get_singleton();
  559. if (p_connected) {
  560. input->joy_connection_changed(p_index, true, String::utf8(p_id), String::utf8(p_guid));
  561. } else {
  562. input->joy_connection_changed(p_index, false, "");
  563. }
  564. }
  565. void DisplayServerWeb::process_joypads() {
  566. Input *input = Input::get_singleton();
  567. int32_t pads = godot_js_input_gamepad_sample_count();
  568. int32_t s_btns_num = 0;
  569. int32_t s_axes_num = 0;
  570. int32_t s_standard = 0;
  571. float s_btns[16];
  572. float s_axes[10];
  573. for (int idx = 0; idx < pads; idx++) {
  574. int err = godot_js_input_gamepad_sample_get(idx, s_btns, &s_btns_num, s_axes, &s_axes_num, &s_standard);
  575. if (err) {
  576. continue;
  577. }
  578. for (int b = 0; b < s_btns_num; b++) {
  579. // Buttons 6 and 7 in the standard mapping need to be
  580. // axis to be handled as JoyAxis::TRIGGER by Godot.
  581. if (s_standard && (b == 6 || b == 7)) {
  582. input->joy_axis(idx, (JoyAxis)b, s_btns[b]);
  583. } else {
  584. input->joy_button(idx, (JoyButton)b, s_btns[b]);
  585. }
  586. }
  587. for (int a = 0; a < s_axes_num; a++) {
  588. input->joy_axis(idx, (JoyAxis)a, s_axes[a]);
  589. }
  590. }
  591. }
  592. Vector<String> DisplayServerWeb::get_rendering_drivers_func() {
  593. Vector<String> drivers;
  594. #ifdef GLES3_ENABLED
  595. drivers.push_back("opengl3");
  596. #endif
  597. return drivers;
  598. }
  599. // Clipboard
  600. void DisplayServerWeb::update_clipboard_callback(const char *p_text) {
  601. get_singleton()->clipboard = String::utf8(p_text);
  602. }
  603. void DisplayServerWeb::clipboard_set(const String &p_text) {
  604. clipboard = p_text;
  605. int err = godot_js_display_clipboard_set(p_text.utf8().get_data());
  606. ERR_FAIL_COND_MSG(err, "Clipboard API is not supported.");
  607. }
  608. String DisplayServerWeb::clipboard_get() const {
  609. godot_js_display_clipboard_get(update_clipboard_callback);
  610. return clipboard;
  611. }
  612. void DisplayServerWeb::send_window_event_callback(int p_notification) {
  613. DisplayServerWeb *ds = get_singleton();
  614. if (!ds) {
  615. return;
  616. }
  617. if (p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER || p_notification == DisplayServer::WINDOW_EVENT_MOUSE_EXIT) {
  618. ds->cursor_inside_canvas = p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
  619. }
  620. if (!ds->window_event_callback.is_null()) {
  621. Variant event = int(p_notification);
  622. Variant *eventp = &event;
  623. Variant ret;
  624. Callable::CallError ce;
  625. ds->window_event_callback.callp((const Variant **)&eventp, 1, ret, ce);
  626. }
  627. }
  628. void DisplayServerWeb::set_icon(const Ref<Image> &p_icon) {
  629. if (p_icon.is_valid()) {
  630. ERR_FAIL_COND(p_icon->get_width() <= 0 || p_icon->get_height() <= 0);
  631. Ref<Image> icon = p_icon;
  632. if (icon->is_compressed()) {
  633. icon = icon->duplicate();
  634. ERR_FAIL_COND(icon->decompress() != OK);
  635. }
  636. if (icon->get_format() != Image::FORMAT_RGBA8) {
  637. if (icon == p_icon) {
  638. icon = icon->duplicate();
  639. }
  640. icon->convert(Image::FORMAT_RGBA8);
  641. }
  642. png_image png_meta;
  643. memset(&png_meta, 0, sizeof png_meta);
  644. png_meta.version = PNG_IMAGE_VERSION;
  645. png_meta.width = icon->get_width();
  646. png_meta.height = icon->get_height();
  647. png_meta.format = PNG_FORMAT_RGBA;
  648. PackedByteArray png;
  649. size_t len;
  650. PackedByteArray data = icon->get_data();
  651. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  652. png.resize(len);
  653. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  654. godot_js_display_window_icon_set(png.ptr(), len);
  655. } else {
  656. godot_js_display_window_icon_set(nullptr, 0);
  657. }
  658. }
  659. void DisplayServerWeb::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  660. Callable cb = get_singleton()->input_event_callback;
  661. if (!cb.is_null()) {
  662. Variant ev = p_event;
  663. Variant *evp = &ev;
  664. Variant ret;
  665. Callable::CallError ce;
  666. cb.callp((const Variant **)&evp, 1, ret, ce);
  667. }
  668. }
  669. 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, Error &r_error) {
  670. return memnew(DisplayServerWeb(p_rendering_driver, p_window_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, r_error));
  671. }
  672. 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, Error &r_error) {
  673. r_error = OK; // Always succeeds for now.
  674. tts = GLOBAL_GET("audio/general/text_to_speech");
  675. // Ensure the canvas ID.
  676. godot_js_config_canvas_id_get(canvas_id, 256);
  677. // Handle contextmenu, webglcontextlost
  678. 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);
  679. // Check if it's windows.
  680. swap_cancel_ok = godot_js_display_is_swap_ok_cancel() == 1;
  681. // Expose method for requesting quit.
  682. godot_js_os_request_quit_cb(request_quit_callback);
  683. #ifdef GLES3_ENABLED
  684. bool webgl2_inited = false;
  685. if (godot_js_display_has_webgl(2)) {
  686. EmscriptenWebGLContextAttributes attributes;
  687. emscripten_webgl_init_context_attributes(&attributes);
  688. attributes.alpha = OS::get_singleton()->is_layered_allowed();
  689. attributes.antialias = false;
  690. attributes.majorVersion = 2;
  691. attributes.explicitSwapControl = true;
  692. webgl_ctx = emscripten_webgl_create_context(canvas_id, &attributes);
  693. webgl2_inited = webgl_ctx && emscripten_webgl_make_context_current(webgl_ctx) == EMSCRIPTEN_RESULT_SUCCESS;
  694. }
  695. if (webgl2_inited) {
  696. if (!emscripten_webgl_enable_extension(webgl_ctx, "OVR_multiview2")) {
  697. print_verbose("Failed to enable WebXR extension.");
  698. }
  699. RasterizerGLES3::make_current();
  700. } else {
  701. OS::get_singleton()->alert(
  702. "Your browser seems not to support WebGL 2.\n\n"
  703. "If possible, consider updating your browser version and video card drivers.",
  704. "Unable to initialize WebGL 2 video driver");
  705. RasterizerDummy::make_current();
  706. }
  707. #else
  708. RasterizerDummy::make_current();
  709. #endif
  710. // JS Input interface (js/libs/library_godot_input.js)
  711. godot_js_input_mouse_button_cb(&DisplayServerWeb::mouse_button_callback);
  712. godot_js_input_mouse_move_cb(&DisplayServerWeb::mouse_move_callback);
  713. godot_js_input_mouse_wheel_cb(&DisplayServerWeb::mouse_wheel_callback);
  714. godot_js_input_touch_cb(&DisplayServerWeb::touch_callback, touch_event.identifier, touch_event.coords);
  715. godot_js_input_key_cb(&DisplayServerWeb::key_callback, key_event.code, key_event.key);
  716. godot_js_input_paste_cb(update_clipboard_callback);
  717. godot_js_input_drop_files_cb(drop_files_js_callback);
  718. godot_js_input_gamepad_cb(&DisplayServerWeb::gamepad_callback);
  719. // JS Display interface (js/libs/library_godot_display.js)
  720. godot_js_display_fullscreen_cb(&DisplayServerWeb::fullscreen_change_callback);
  721. godot_js_display_window_blur_cb(&window_blur_callback);
  722. godot_js_display_notification_cb(&send_window_event_callback,
  723. WINDOW_EVENT_MOUSE_ENTER,
  724. WINDOW_EVENT_MOUSE_EXIT,
  725. WINDOW_EVENT_FOCUS_IN,
  726. WINDOW_EVENT_FOCUS_OUT);
  727. godot_js_display_vk_cb(&vk_input_text_callback);
  728. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_event);
  729. }
  730. DisplayServerWeb::~DisplayServerWeb() {
  731. #ifdef GLES3_ENABLED
  732. if (webgl_ctx) {
  733. emscripten_webgl_commit_frame();
  734. emscripten_webgl_destroy_context(webgl_ctx);
  735. }
  736. #endif
  737. }
  738. bool DisplayServerWeb::has_feature(Feature p_feature) const {
  739. switch (p_feature) {
  740. //case FEATURE_GLOBAL_MENU:
  741. //case FEATURE_HIDPI:
  742. //case FEATURE_IME:
  743. case FEATURE_ICON:
  744. case FEATURE_CLIPBOARD:
  745. case FEATURE_CURSOR_SHAPE:
  746. case FEATURE_CUSTOM_CURSOR_SHAPE:
  747. case FEATURE_MOUSE:
  748. case FEATURE_TOUCHSCREEN:
  749. return true;
  750. //case FEATURE_MOUSE_WARP:
  751. //case FEATURE_NATIVE_DIALOG:
  752. //case FEATURE_NATIVE_ICON:
  753. //case FEATURE_WINDOW_TRANSPARENCY:
  754. //case FEATURE_KEEP_SCREEN_ON:
  755. //case FEATURE_ORIENTATION:
  756. case FEATURE_VIRTUAL_KEYBOARD:
  757. return godot_js_display_vk_available() != 0;
  758. case FEATURE_TEXT_TO_SPEECH:
  759. return tts && (godot_js_display_tts_available() != 0);
  760. default:
  761. return false;
  762. }
  763. }
  764. void DisplayServerWeb::register_web_driver() {
  765. register_create_function("web", create_func, get_rendering_drivers_func);
  766. }
  767. String DisplayServerWeb::get_name() const {
  768. return "web";
  769. }
  770. int DisplayServerWeb::get_screen_count() const {
  771. return 1;
  772. }
  773. int DisplayServerWeb::get_primary_screen() const {
  774. return 0;
  775. }
  776. Point2i DisplayServerWeb::screen_get_position(int p_screen) const {
  777. return Point2i(); // TODO offsetX/Y?
  778. }
  779. Size2i DisplayServerWeb::screen_get_size(int p_screen) const {
  780. int size[2];
  781. godot_js_display_screen_size_get(size, size + 1);
  782. return Size2(size[0], size[1]);
  783. }
  784. Rect2i DisplayServerWeb::screen_get_usable_rect(int p_screen) const {
  785. int size[2];
  786. godot_js_display_window_size_get(size, size + 1);
  787. return Rect2i(0, 0, size[0], size[1]);
  788. }
  789. int DisplayServerWeb::screen_get_dpi(int p_screen) const {
  790. return godot_js_display_screen_dpi_get();
  791. }
  792. float DisplayServerWeb::screen_get_scale(int p_screen) const {
  793. return godot_js_display_pixel_ratio_get();
  794. }
  795. float DisplayServerWeb::screen_get_refresh_rate(int p_screen) const {
  796. 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.
  797. }
  798. Vector<DisplayServer::WindowID> DisplayServerWeb::get_window_list() const {
  799. Vector<WindowID> ret;
  800. ret.push_back(MAIN_WINDOW_ID);
  801. return ret;
  802. }
  803. DisplayServerWeb::WindowID DisplayServerWeb::get_window_at_screen_position(const Point2i &p_position) const {
  804. return MAIN_WINDOW_ID;
  805. }
  806. void DisplayServerWeb::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
  807. window_attached_instance_id = p_instance;
  808. }
  809. ObjectID DisplayServerWeb::window_get_attached_instance_id(WindowID p_window) const {
  810. return window_attached_instance_id;
  811. }
  812. void DisplayServerWeb::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) {
  813. rect_changed_callback = p_callable;
  814. }
  815. void DisplayServerWeb::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
  816. window_event_callback = p_callable;
  817. }
  818. void DisplayServerWeb::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
  819. input_event_callback = p_callable;
  820. }
  821. void DisplayServerWeb::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
  822. input_text_callback = p_callable;
  823. }
  824. void DisplayServerWeb::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
  825. drop_files_callback = p_callable;
  826. }
  827. void DisplayServerWeb::window_set_title(const String &p_title, WindowID p_window) {
  828. godot_js_display_window_title_set(p_title.utf8().get_data());
  829. }
  830. int DisplayServerWeb::window_get_current_screen(WindowID p_window) const {
  831. return 1;
  832. }
  833. void DisplayServerWeb::window_set_current_screen(int p_screen, WindowID p_window) {
  834. // Not implemented.
  835. }
  836. Point2i DisplayServerWeb::window_get_position(WindowID p_window) const {
  837. return Point2i();
  838. }
  839. Point2i DisplayServerWeb::window_get_position_with_decorations(WindowID p_window) const {
  840. return Point2i();
  841. }
  842. void DisplayServerWeb::window_set_position(const Point2i &p_position, WindowID p_window) {
  843. // Not supported.
  844. }
  845. void DisplayServerWeb::window_set_transient(WindowID p_window, WindowID p_parent) {
  846. // Not supported.
  847. }
  848. void DisplayServerWeb::window_set_max_size(const Size2i p_size, WindowID p_window) {
  849. // Not supported.
  850. }
  851. Size2i DisplayServerWeb::window_get_max_size(WindowID p_window) const {
  852. return Size2i();
  853. }
  854. void DisplayServerWeb::window_set_min_size(const Size2i p_size, WindowID p_window) {
  855. // Not supported.
  856. }
  857. Size2i DisplayServerWeb::window_get_min_size(WindowID p_window) const {
  858. return Size2i();
  859. }
  860. void DisplayServerWeb::window_set_size(const Size2i p_size, WindowID p_window) {
  861. godot_js_display_desired_size_set(p_size.x, p_size.y);
  862. }
  863. Size2i DisplayServerWeb::window_get_size(WindowID p_window) const {
  864. int size[2];
  865. godot_js_display_window_size_get(size, size + 1);
  866. return Size2i(size[0], size[1]);
  867. }
  868. Size2i DisplayServerWeb::window_get_size_with_decorations(WindowID p_window) const {
  869. return window_get_size(p_window);
  870. }
  871. void DisplayServerWeb::window_set_mode(WindowMode p_mode, WindowID p_window) {
  872. if (window_mode == p_mode) {
  873. return;
  874. }
  875. switch (p_mode) {
  876. case WINDOW_MODE_WINDOWED: {
  877. if (window_mode == WINDOW_MODE_FULLSCREEN) {
  878. godot_js_display_fullscreen_exit();
  879. }
  880. window_mode = WINDOW_MODE_WINDOWED;
  881. } break;
  882. case WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
  883. case WINDOW_MODE_FULLSCREEN: {
  884. int result = godot_js_display_fullscreen_request();
  885. ERR_FAIL_COND_MSG(result, "The request was denied. Remember that enabling fullscreen is only possible from an input callback for the Web platform.");
  886. } break;
  887. case WINDOW_MODE_MAXIMIZED:
  888. case WINDOW_MODE_MINIMIZED:
  889. // WindowMode MAXIMIZED and MINIMIZED are not supported in Web platform.
  890. break;
  891. default:
  892. break;
  893. }
  894. }
  895. DisplayServerWeb::WindowMode DisplayServerWeb::window_get_mode(WindowID p_window) const {
  896. return window_mode;
  897. }
  898. bool DisplayServerWeb::window_is_maximize_allowed(WindowID p_window) const {
  899. return false;
  900. }
  901. void DisplayServerWeb::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  902. // Not supported.
  903. }
  904. bool DisplayServerWeb::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  905. return false;
  906. }
  907. void DisplayServerWeb::window_request_attention(WindowID p_window) {
  908. // Not supported.
  909. }
  910. void DisplayServerWeb::window_move_to_foreground(WindowID p_window) {
  911. // Not supported.
  912. }
  913. bool DisplayServerWeb::window_is_focused(WindowID p_window) const {
  914. return true;
  915. }
  916. bool DisplayServerWeb::window_can_draw(WindowID p_window) const {
  917. return true;
  918. }
  919. bool DisplayServerWeb::can_any_window_draw() const {
  920. return true;
  921. }
  922. DisplayServer::VSyncMode DisplayServerWeb::window_get_vsync_mode(WindowID p_vsync_mode) const {
  923. return DisplayServer::VSYNC_ENABLED;
  924. }
  925. void DisplayServerWeb::process_events() {
  926. Input::get_singleton()->flush_buffered_events();
  927. if (godot_js_input_gamepad_sample() == OK) {
  928. process_joypads();
  929. }
  930. }
  931. int DisplayServerWeb::get_current_video_driver() const {
  932. return 1;
  933. }
  934. bool DisplayServerWeb::get_swap_cancel_ok() {
  935. return swap_cancel_ok;
  936. }
  937. void DisplayServerWeb::swap_buffers() {
  938. #ifdef GLES3_ENABLED
  939. if (webgl_ctx) {
  940. emscripten_webgl_commit_frame();
  941. }
  942. #endif
  943. }