display_server_web.cpp 43 KB

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