display_server_web.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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 || b == 7)) {
  768. input->joy_axis(idx, (JoyAxis)b, s_btns[b]);
  769. } else {
  770. input->joy_button(idx, (JoyButton)b, s_btns[b]);
  771. }
  772. }
  773. for (int a = 0; a < s_axes_num; a++) {
  774. input->joy_axis(idx, (JoyAxis)a, s_axes[a]);
  775. }
  776. }
  777. }
  778. Vector<String> DisplayServerWeb::get_rendering_drivers_func() {
  779. Vector<String> drivers;
  780. #ifdef GLES3_ENABLED
  781. drivers.push_back("opengl3");
  782. #endif
  783. return drivers;
  784. }
  785. // Clipboard
  786. void DisplayServerWeb::update_clipboard_callback(const char *p_text) {
  787. String text = p_text;
  788. #ifdef PROXY_TO_PTHREAD_ENABLED
  789. if (!Thread::is_main_thread()) {
  790. callable_mp_static(DisplayServerWeb::_update_clipboard_callback).call_deferred(text);
  791. return;
  792. }
  793. #endif
  794. _update_clipboard_callback(text);
  795. }
  796. void DisplayServerWeb::_update_clipboard_callback(const String &p_text) {
  797. get_singleton()->clipboard = p_text;
  798. }
  799. void DisplayServerWeb::clipboard_set(const String &p_text) {
  800. clipboard = p_text;
  801. int err = godot_js_display_clipboard_set(p_text.utf8().get_data());
  802. ERR_FAIL_COND_MSG(err, "Clipboard API is not supported.");
  803. }
  804. String DisplayServerWeb::clipboard_get() const {
  805. godot_js_display_clipboard_get(update_clipboard_callback);
  806. return clipboard;
  807. }
  808. void DisplayServerWeb::send_window_event_callback(int p_notification) {
  809. #ifdef PROXY_TO_PTHREAD_ENABLED
  810. if (!Thread::is_main_thread()) {
  811. callable_mp_static(DisplayServerWeb::_send_window_event_callback).call_deferred(p_notification);
  812. return;
  813. }
  814. #endif
  815. _send_window_event_callback(p_notification);
  816. }
  817. void DisplayServerWeb::_send_window_event_callback(int p_notification) {
  818. DisplayServerWeb *ds = get_singleton();
  819. if (!ds) {
  820. return;
  821. }
  822. if (p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER || p_notification == DisplayServer::WINDOW_EVENT_MOUSE_EXIT) {
  823. ds->cursor_inside_canvas = p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
  824. }
  825. if (godot_js_is_ime_focused() && (p_notification == DisplayServer::WINDOW_EVENT_FOCUS_IN || p_notification == DisplayServer::WINDOW_EVENT_FOCUS_OUT)) {
  826. return;
  827. }
  828. if (ds->window_event_callback.is_valid()) {
  829. Variant event = int(p_notification);
  830. ds->window_event_callback.call(event);
  831. }
  832. }
  833. void DisplayServerWeb::set_icon(const Ref<Image> &p_icon) {
  834. if (p_icon.is_valid()) {
  835. ERR_FAIL_COND(p_icon->get_width() <= 0 || p_icon->get_height() <= 0);
  836. Ref<Image> icon = p_icon;
  837. if (icon->is_compressed()) {
  838. icon = icon->duplicate();
  839. ERR_FAIL_COND(icon->decompress() != OK);
  840. }
  841. if (icon->get_format() != Image::FORMAT_RGBA8) {
  842. if (icon == p_icon) {
  843. icon = icon->duplicate();
  844. }
  845. icon->convert(Image::FORMAT_RGBA8);
  846. }
  847. png_image png_meta;
  848. memset(&png_meta, 0, sizeof png_meta);
  849. png_meta.version = PNG_IMAGE_VERSION;
  850. png_meta.width = icon->get_width();
  851. png_meta.height = icon->get_height();
  852. png_meta.format = PNG_FORMAT_RGBA;
  853. PackedByteArray png;
  854. size_t len;
  855. PackedByteArray data = icon->get_data();
  856. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  857. png.resize(len);
  858. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  859. godot_js_display_window_icon_set(png.ptr(), len);
  860. } else {
  861. godot_js_display_window_icon_set(nullptr, 0);
  862. }
  863. }
  864. void DisplayServerWeb::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  865. Callable cb = get_singleton()->input_event_callback;
  866. if (cb.is_valid()) {
  867. cb.call(p_event);
  868. }
  869. }
  870. 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) {
  871. return memnew(DisplayServerWeb(p_rendering_driver, p_window_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, p_context, r_error));
  872. }
  873. 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) {
  874. r_error = OK; // Always succeeds for now.
  875. tts = GLOBAL_GET("audio/general/text_to_speech");
  876. native_menu = memnew(NativeMenu); // Dummy native menu.
  877. // Ensure the canvas ID.
  878. godot_js_config_canvas_id_get(canvas_id, 256);
  879. // Handle contextmenu, webglcontextlost
  880. 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);
  881. // Check if it's windows.
  882. swap_cancel_ok = godot_js_display_is_swap_ok_cancel() == 1;
  883. // Expose method for requesting quit.
  884. godot_js_os_request_quit_cb(request_quit_callback);
  885. #ifdef GLES3_ENABLED
  886. bool webgl2_inited = false;
  887. if (godot_js_display_has_webgl(2)) {
  888. EmscriptenWebGLContextAttributes attributes;
  889. emscripten_webgl_init_context_attributes(&attributes);
  890. attributes.alpha = OS::get_singleton()->is_layered_allowed();
  891. attributes.antialias = false;
  892. attributes.majorVersion = 2;
  893. attributes.explicitSwapControl = true;
  894. webgl_ctx = emscripten_webgl_create_context(canvas_id, &attributes);
  895. webgl2_inited = webgl_ctx && emscripten_webgl_make_context_current(webgl_ctx) == EMSCRIPTEN_RESULT_SUCCESS;
  896. }
  897. if (webgl2_inited) {
  898. if (!emscripten_webgl_enable_extension(webgl_ctx, "OVR_multiview2")) {
  899. print_verbose("Failed to enable WebXR extension.");
  900. }
  901. RasterizerGLES3::make_current(false);
  902. } else {
  903. OS::get_singleton()->alert(
  904. "Your browser seems not to support WebGL 2.\n\n"
  905. "If possible, consider updating your browser version and video card drivers.",
  906. "Unable to initialize WebGL 2 video driver");
  907. RasterizerDummy::make_current();
  908. }
  909. #else
  910. RasterizerDummy::make_current();
  911. #endif
  912. // JS Input interface (js/libs/library_godot_input.js)
  913. godot_js_input_mouse_button_cb(&DisplayServerWeb::mouse_button_callback);
  914. godot_js_input_mouse_move_cb(&DisplayServerWeb::mouse_move_callback);
  915. godot_js_input_mouse_wheel_cb(&DisplayServerWeb::mouse_wheel_callback);
  916. godot_js_input_touch_cb(&DisplayServerWeb::touch_callback, touch_event.identifier, touch_event.coords);
  917. godot_js_input_key_cb(&DisplayServerWeb::key_callback, key_event.code, key_event.key);
  918. godot_js_input_paste_cb(&DisplayServerWeb::update_clipboard_callback);
  919. godot_js_input_drop_files_cb(&DisplayServerWeb::drop_files_js_callback);
  920. godot_js_input_gamepad_cb(&DisplayServerWeb::gamepad_callback);
  921. godot_js_set_ime_cb(&DisplayServerWeb::ime_callback, &DisplayServerWeb::key_callback, key_event.code, key_event.key);
  922. // JS Display interface (js/libs/library_godot_display.js)
  923. godot_js_display_fullscreen_cb(&DisplayServerWeb::fullscreen_change_callback);
  924. godot_js_display_window_blur_cb(&DisplayServerWeb::window_blur_callback);
  925. godot_js_display_notification_cb(&DisplayServerWeb::send_window_event_callback,
  926. WINDOW_EVENT_MOUSE_ENTER,
  927. WINDOW_EVENT_MOUSE_EXIT,
  928. WINDOW_EVENT_FOCUS_IN,
  929. WINDOW_EVENT_FOCUS_OUT);
  930. godot_js_display_vk_cb(&DisplayServerWeb::vk_input_text_callback);
  931. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_event);
  932. }
  933. DisplayServerWeb::~DisplayServerWeb() {
  934. if (native_menu) {
  935. memdelete(native_menu);
  936. native_menu = nullptr;
  937. }
  938. #ifdef GLES3_ENABLED
  939. if (webgl_ctx) {
  940. emscripten_webgl_commit_frame();
  941. emscripten_webgl_destroy_context(webgl_ctx);
  942. }
  943. #endif
  944. }
  945. bool DisplayServerWeb::has_feature(Feature p_feature) const {
  946. switch (p_feature) {
  947. #ifndef DISABLE_DEPRECATED
  948. case FEATURE_GLOBAL_MENU: {
  949. return (native_menu && native_menu->has_feature(NativeMenu::FEATURE_GLOBAL_MENU));
  950. } break;
  951. #endif
  952. //case FEATURE_HIDPI:
  953. case FEATURE_ICON:
  954. case FEATURE_CLIPBOARD:
  955. case FEATURE_CURSOR_SHAPE:
  956. case FEATURE_CUSTOM_CURSOR_SHAPE:
  957. case FEATURE_MOUSE:
  958. case FEATURE_TOUCHSCREEN:
  959. return true;
  960. //case FEATURE_MOUSE_WARP:
  961. //case FEATURE_NATIVE_DIALOG:
  962. //case FEATURE_NATIVE_DIALOG_INPUT:
  963. //case FEATURE_NATIVE_DIALOG_FILE:
  964. //case FEATURE_NATIVE_ICON:
  965. //case FEATURE_WINDOW_TRANSPARENCY:
  966. //case FEATURE_KEEP_SCREEN_ON:
  967. //case FEATURE_ORIENTATION:
  968. case FEATURE_IME:
  969. // IME does not work with experimental VK support.
  970. return godot_js_display_vk_available() == 0;
  971. case FEATURE_VIRTUAL_KEYBOARD:
  972. return godot_js_display_vk_available() != 0;
  973. case FEATURE_TEXT_TO_SPEECH:
  974. return tts && (godot_js_display_tts_available() != 0);
  975. default:
  976. return false;
  977. }
  978. }
  979. void DisplayServerWeb::register_web_driver() {
  980. register_create_function("web", create_func, get_rendering_drivers_func);
  981. }
  982. String DisplayServerWeb::get_name() const {
  983. return "web";
  984. }
  985. int DisplayServerWeb::get_screen_count() const {
  986. return 1;
  987. }
  988. int DisplayServerWeb::get_primary_screen() const {
  989. return 0;
  990. }
  991. Point2i DisplayServerWeb::screen_get_position(int p_screen) const {
  992. return Point2i(); // TODO offsetX/Y?
  993. }
  994. Size2i DisplayServerWeb::screen_get_size(int p_screen) const {
  995. int size[2];
  996. godot_js_display_screen_size_get(size, size + 1);
  997. return Size2(size[0], size[1]);
  998. }
  999. Rect2i DisplayServerWeb::screen_get_usable_rect(int p_screen) const {
  1000. int size[2];
  1001. godot_js_display_window_size_get(size, size + 1);
  1002. return Rect2i(0, 0, size[0], size[1]);
  1003. }
  1004. int DisplayServerWeb::screen_get_dpi(int p_screen) const {
  1005. return godot_js_display_screen_dpi_get();
  1006. }
  1007. float DisplayServerWeb::screen_get_scale(int p_screen) const {
  1008. return godot_js_display_pixel_ratio_get();
  1009. }
  1010. float DisplayServerWeb::screen_get_refresh_rate(int p_screen) const {
  1011. 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.
  1012. }
  1013. Vector<DisplayServer::WindowID> DisplayServerWeb::get_window_list() const {
  1014. Vector<WindowID> ret;
  1015. ret.push_back(MAIN_WINDOW_ID);
  1016. return ret;
  1017. }
  1018. DisplayServerWeb::WindowID DisplayServerWeb::get_window_at_screen_position(const Point2i &p_position) const {
  1019. return MAIN_WINDOW_ID;
  1020. }
  1021. void DisplayServerWeb::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
  1022. window_attached_instance_id = p_instance;
  1023. }
  1024. ObjectID DisplayServerWeb::window_get_attached_instance_id(WindowID p_window) const {
  1025. return window_attached_instance_id;
  1026. }
  1027. void DisplayServerWeb::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) {
  1028. rect_changed_callback = p_callable;
  1029. }
  1030. void DisplayServerWeb::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
  1031. window_event_callback = p_callable;
  1032. }
  1033. void DisplayServerWeb::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
  1034. input_event_callback = p_callable;
  1035. }
  1036. void DisplayServerWeb::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
  1037. input_text_callback = p_callable;
  1038. }
  1039. void DisplayServerWeb::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
  1040. drop_files_callback = p_callable;
  1041. }
  1042. void DisplayServerWeb::window_set_title(const String &p_title, WindowID p_window) {
  1043. godot_js_display_window_title_set(p_title.utf8().get_data());
  1044. }
  1045. int DisplayServerWeb::window_get_current_screen(WindowID p_window) const {
  1046. return 1;
  1047. }
  1048. void DisplayServerWeb::window_set_current_screen(int p_screen, WindowID p_window) {
  1049. // Not implemented.
  1050. }
  1051. Point2i DisplayServerWeb::window_get_position(WindowID p_window) const {
  1052. return Point2i();
  1053. }
  1054. Point2i DisplayServerWeb::window_get_position_with_decorations(WindowID p_window) const {
  1055. return Point2i();
  1056. }
  1057. void DisplayServerWeb::window_set_position(const Point2i &p_position, WindowID p_window) {
  1058. // Not supported.
  1059. }
  1060. void DisplayServerWeb::window_set_transient(WindowID p_window, WindowID p_parent) {
  1061. // Not supported.
  1062. }
  1063. void DisplayServerWeb::window_set_max_size(const Size2i p_size, WindowID p_window) {
  1064. // Not supported.
  1065. }
  1066. Size2i DisplayServerWeb::window_get_max_size(WindowID p_window) const {
  1067. return Size2i();
  1068. }
  1069. void DisplayServerWeb::window_set_min_size(const Size2i p_size, WindowID p_window) {
  1070. // Not supported.
  1071. }
  1072. Size2i DisplayServerWeb::window_get_min_size(WindowID p_window) const {
  1073. return Size2i();
  1074. }
  1075. void DisplayServerWeb::window_set_size(const Size2i p_size, WindowID p_window) {
  1076. godot_js_display_desired_size_set(p_size.x, p_size.y);
  1077. }
  1078. Size2i DisplayServerWeb::window_get_size(WindowID p_window) const {
  1079. int size[2];
  1080. godot_js_display_window_size_get(size, size + 1);
  1081. return Size2i(size[0], size[1]);
  1082. }
  1083. Size2i DisplayServerWeb::window_get_size_with_decorations(WindowID p_window) const {
  1084. return window_get_size(p_window);
  1085. }
  1086. void DisplayServerWeb::window_set_mode(WindowMode p_mode, WindowID p_window) {
  1087. if (window_mode == p_mode) {
  1088. return;
  1089. }
  1090. switch (p_mode) {
  1091. case WINDOW_MODE_WINDOWED: {
  1092. if (window_mode == WINDOW_MODE_FULLSCREEN) {
  1093. godot_js_display_fullscreen_exit();
  1094. }
  1095. window_mode = WINDOW_MODE_WINDOWED;
  1096. } break;
  1097. case WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
  1098. case WINDOW_MODE_FULLSCREEN: {
  1099. int result = godot_js_display_fullscreen_request();
  1100. ERR_FAIL_COND_MSG(result, "The request was denied. Remember that enabling fullscreen is only possible from an input callback for the Web platform.");
  1101. } break;
  1102. case WINDOW_MODE_MAXIMIZED:
  1103. case WINDOW_MODE_MINIMIZED:
  1104. // WindowMode MAXIMIZED and MINIMIZED are not supported in Web platform.
  1105. break;
  1106. default:
  1107. break;
  1108. }
  1109. }
  1110. DisplayServerWeb::WindowMode DisplayServerWeb::window_get_mode(WindowID p_window) const {
  1111. return window_mode;
  1112. }
  1113. bool DisplayServerWeb::window_is_maximize_allowed(WindowID p_window) const {
  1114. return false;
  1115. }
  1116. void DisplayServerWeb::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  1117. // Not supported.
  1118. }
  1119. bool DisplayServerWeb::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  1120. return false;
  1121. }
  1122. void DisplayServerWeb::window_request_attention(WindowID p_window) {
  1123. // Not supported.
  1124. }
  1125. void DisplayServerWeb::window_move_to_foreground(WindowID p_window) {
  1126. // Not supported.
  1127. }
  1128. bool DisplayServerWeb::window_is_focused(WindowID p_window) const {
  1129. return true;
  1130. }
  1131. bool DisplayServerWeb::window_can_draw(WindowID p_window) const {
  1132. return true;
  1133. }
  1134. bool DisplayServerWeb::can_any_window_draw() const {
  1135. return true;
  1136. }
  1137. DisplayServer::VSyncMode DisplayServerWeb::window_get_vsync_mode(WindowID p_vsync_mode) const {
  1138. return DisplayServer::VSYNC_ENABLED;
  1139. }
  1140. void DisplayServerWeb::process_events() {
  1141. process_keys();
  1142. Input::get_singleton()->flush_buffered_events();
  1143. if (godot_js_input_gamepad_sample() == OK) {
  1144. process_joypads();
  1145. }
  1146. }
  1147. void DisplayServerWeb::process_keys() {
  1148. for (int i = 0; i < key_event_pos; i++) {
  1149. const DisplayServerWeb::KeyEvent &ke = key_event_buffer[i];
  1150. Ref<InputEventKey> ev;
  1151. ev.instantiate();
  1152. ev->set_pressed(ke.pressed);
  1153. ev->set_echo(ke.echo);
  1154. ev->set_keycode(ke.keycode);
  1155. ev->set_physical_keycode(ke.physical_keycode);
  1156. ev->set_key_label(ke.key_label);
  1157. ev->set_unicode(ke.unicode);
  1158. ev->set_location(ke.location);
  1159. if (ke.raw) {
  1160. dom2godot_mod(ev, ke.mod, ke.keycode);
  1161. }
  1162. Input::get_singleton()->parse_input_event(ev);
  1163. }
  1164. key_event_pos = 0;
  1165. }
  1166. int DisplayServerWeb::get_current_video_driver() const {
  1167. return 1;
  1168. }
  1169. bool DisplayServerWeb::get_swap_cancel_ok() {
  1170. return swap_cancel_ok;
  1171. }
  1172. void DisplayServerWeb::swap_buffers() {
  1173. #ifdef GLES3_ENABLED
  1174. if (webgl_ctx) {
  1175. emscripten_webgl_commit_frame();
  1176. }
  1177. #endif
  1178. }