display_server_web.cpp 43 KB

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