display_server_web.cpp 40 KB

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