os_javascript.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. /**************************************************************************/
  2. /* os_javascript.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 "os_javascript.h"
  31. #include "core/io/json.h"
  32. #include "core/project_settings.h"
  33. #include "drivers/gles2/rasterizer_gles2.h"
  34. #include "drivers/gles3/rasterizer_gles3.h"
  35. #include "drivers/unix/dir_access_unix.h"
  36. #include "drivers/unix/file_access_unix.h"
  37. #include "main/main.h"
  38. #include "servers/visual/visual_server_raster.h"
  39. #ifndef NO_THREADS
  40. #include "servers/visual/visual_server_wrap_mt.h"
  41. #endif
  42. #include <dlfcn.h>
  43. #include <emscripten.h>
  44. #include <png.h>
  45. #include <stdlib.h>
  46. #include "api/javascript_singleton.h"
  47. #include "dom_keys.inc"
  48. #include "godot_js.h"
  49. #define DOM_BUTTON_LEFT 0
  50. #define DOM_BUTTON_MIDDLE 1
  51. #define DOM_BUTTON_RIGHT 2
  52. #define DOM_BUTTON_XBUTTON1 3
  53. #define DOM_BUTTON_XBUTTON2 4
  54. // Quit
  55. void OS_JavaScript::request_quit_callback() {
  56. OS_JavaScript *os = get_singleton();
  57. if (os && os->get_main_loop()) {
  58. os->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
  59. }
  60. }
  61. bool OS_JavaScript::tts_is_speaking() const {
  62. ERR_FAIL_COND_V_MSG(!tts, false, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  63. return godot_js_tts_is_speaking();
  64. }
  65. bool OS_JavaScript::tts_is_paused() const {
  66. ERR_FAIL_COND_V_MSG(!tts, false, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  67. return godot_js_tts_is_paused();
  68. }
  69. void OS_JavaScript::update_voices_callback(int p_size, const char **p_voice) {
  70. get_singleton()->voices.clear();
  71. for (int i = 0; i < p_size; i++) {
  72. Vector<String> tokens = String::utf8(p_voice[i]).split(";", true, 2);
  73. if (tokens.size() == 2) {
  74. Dictionary voice_d;
  75. voice_d["name"] = tokens[1];
  76. voice_d["id"] = tokens[1];
  77. voice_d["language"] = tokens[0];
  78. get_singleton()->voices.push_back(voice_d);
  79. }
  80. }
  81. }
  82. Array OS_JavaScript::tts_get_voices() const {
  83. ERR_FAIL_COND_V_MSG(!tts, Array(), "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  84. godot_js_tts_get_voices(update_voices_callback);
  85. return voices;
  86. }
  87. void OS_JavaScript::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) {
  88. ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  89. if (p_interrupt) {
  90. tts_stop();
  91. }
  92. if (p_text.empty()) {
  93. tts_post_utterance_event(OS::TTS_UTTERANCE_CANCELED, p_utterance_id);
  94. return;
  95. }
  96. CharString string = p_text.utf8();
  97. utterance_ids[p_utterance_id] = string;
  98. 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, OS_JavaScript::_js_utterance_callback);
  99. }
  100. void OS_JavaScript::tts_pause() {
  101. ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  102. godot_js_tts_pause();
  103. }
  104. void OS_JavaScript::tts_resume() {
  105. ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  106. godot_js_tts_resume();
  107. }
  108. void OS_JavaScript::tts_stop() {
  109. ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  110. for (Map<int, CharString>::Element *E = utterance_ids.front(); E; E = E->next()) {
  111. tts_post_utterance_event(OS::TTS_UTTERANCE_CANCELED, E->key());
  112. }
  113. utterance_ids.clear();
  114. godot_js_tts_stop();
  115. }
  116. void OS_JavaScript::_js_utterance_callback(int p_event, int p_id, int p_pos) {
  117. OS_JavaScript *ds = (OS_JavaScript *)OS::get_singleton();
  118. if (ds->utterance_ids.has(p_id)) {
  119. int pos = 0;
  120. if ((TTSUtteranceEvent)p_event == OS::TTS_UTTERANCE_BOUNDARY) {
  121. // Convert position from UTF-8 to UTF-32.
  122. const CharString &string = ds->utterance_ids[p_id];
  123. for (int i = 0; i < MIN(p_pos, string.length()); i++) {
  124. uint8_t c = string[i];
  125. if ((c & 0xe0) == 0xc0) {
  126. i += 1;
  127. } else if ((c & 0xf0) == 0xe0) {
  128. i += 2;
  129. } else if ((c & 0xf8) == 0xf0) {
  130. i += 3;
  131. }
  132. pos++;
  133. }
  134. } else if ((TTSUtteranceEvent)p_event != OS::TTS_UTTERANCE_STARTED) {
  135. ds->utterance_ids.erase(p_id);
  136. }
  137. ds->tts_post_utterance_event((TTSUtteranceEvent)p_event, p_id, pos);
  138. }
  139. }
  140. // Files drop (implemented in JS for now).
  141. void OS_JavaScript::drop_files_callback(char **p_filev, int p_filec) {
  142. OS_JavaScript *os = get_singleton();
  143. if (!os || !os->get_main_loop()) {
  144. return;
  145. }
  146. Vector<String> files;
  147. for (int i = 0; i < p_filec; i++) {
  148. files.push_back(String::utf8(p_filev[i]));
  149. }
  150. os->get_main_loop()->drop_files(files);
  151. }
  152. void OS_JavaScript::send_notification_callback(int p_notification) {
  153. OS_JavaScript *os = get_singleton();
  154. if (!os) {
  155. return;
  156. }
  157. if (p_notification == MainLoop::NOTIFICATION_WM_MOUSE_ENTER || p_notification == MainLoop::NOTIFICATION_WM_MOUSE_EXIT) {
  158. os->cursor_inside_canvas = p_notification == MainLoop::NOTIFICATION_WM_MOUSE_ENTER;
  159. }
  160. MainLoop *loop = os->get_main_loop();
  161. if (loop) {
  162. loop->notification(p_notification);
  163. }
  164. }
  165. // Window (canvas)
  166. bool OS_JavaScript::check_size_force_redraw() {
  167. return godot_js_display_size_update() != 0;
  168. }
  169. void OS_JavaScript::fullscreen_change_callback(int p_fullscreen) {
  170. OS_JavaScript *os = get_singleton();
  171. os->video_mode.fullscreen = p_fullscreen;
  172. }
  173. void OS_JavaScript::window_blur_callback() {
  174. get_singleton()->input->release_pressed_events();
  175. }
  176. void OS_JavaScript::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  177. video_mode = p_video_mode;
  178. }
  179. OS::VideoMode OS_JavaScript::get_video_mode(int p_screen) const {
  180. return video_mode;
  181. }
  182. Size2 OS_JavaScript::get_screen_size(int p_screen) const {
  183. int size[2];
  184. godot_js_display_screen_size_get(size, size + 1);
  185. return Size2(size[0], size[1]);
  186. }
  187. void OS_JavaScript::set_window_size(const Size2 p_size) {
  188. if (video_mode.fullscreen) {
  189. set_window_fullscreen(false);
  190. }
  191. godot_js_display_desired_size_set(p_size.x, p_size.y);
  192. }
  193. Size2 OS_JavaScript::get_window_size() const {
  194. int size[2];
  195. godot_js_display_window_size_get(size, size + 1);
  196. return Size2(size[0], size[1]);
  197. }
  198. void OS_JavaScript::set_window_maximized(bool p_enabled) {
  199. }
  200. bool OS_JavaScript::is_window_maximized() const {
  201. return false;
  202. }
  203. void OS_JavaScript::set_window_fullscreen(bool p_enabled) {
  204. if (p_enabled == video_mode.fullscreen) {
  205. return;
  206. }
  207. // Just request changes here, if successful, logic continues in
  208. // fullscreen_change_callback.
  209. if (p_enabled) {
  210. int result = godot_js_display_fullscreen_request();
  211. ERR_FAIL_COND_MSG(result, "The request was denied. Remember that enabling fullscreen is only possible from an input callback for the HTML5 platform.");
  212. } else {
  213. // No logic allowed here, since exiting w/ ESC key won't use this function.
  214. ERR_FAIL_COND(godot_js_display_fullscreen_exit());
  215. }
  216. }
  217. bool OS_JavaScript::is_window_fullscreen() const {
  218. return video_mode.fullscreen;
  219. }
  220. void OS_JavaScript::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  221. Size2 screen = get_screen_size();
  222. p_list->push_back(OS::VideoMode(screen.width, screen.height, true));
  223. }
  224. bool OS_JavaScript::get_window_per_pixel_transparency_enabled() const {
  225. if (!is_layered_allowed()) {
  226. return false;
  227. }
  228. return transparency_enabled;
  229. }
  230. void OS_JavaScript::set_window_per_pixel_transparency_enabled(bool p_enabled) {
  231. if (!is_layered_allowed()) {
  232. return;
  233. }
  234. transparency_enabled = p_enabled;
  235. }
  236. // Keys
  237. static void dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod) {
  238. ev->set_shift(p_mod & 1);
  239. ev->set_alt(p_mod & 2);
  240. ev->set_control(p_mod & 4);
  241. ev->set_metakey(p_mod & 8);
  242. }
  243. void OS_JavaScript::key_callback(int p_pressed, int p_repeat, int p_modifiers) {
  244. OS_JavaScript *os = get_singleton();
  245. JSKeyEvent &key_event = os->key_event;
  246. // Resume audio context after input in case autoplay was denied.
  247. os->resume_audio();
  248. Ref<InputEventKey> ev;
  249. ev.instance();
  250. ev->set_echo(p_repeat);
  251. ev->set_scancode(dom_code2godot_scancode(key_event.code, key_event.key, false));
  252. ev->set_physical_scancode(dom_code2godot_scancode(key_event.code, key_event.key, true));
  253. ev->set_pressed(p_pressed);
  254. dom2godot_mod(ev, p_modifiers);
  255. String unicode = String::utf8(key_event.key);
  256. if (unicode.length() == 1) {
  257. ev->set_unicode(unicode[0]);
  258. }
  259. os->input->parse_input_event(ev);
  260. // Make sure to flush all events so we can call restricted APIs inside the event.
  261. os->input->flush_buffered_events();
  262. }
  263. // Mouse
  264. Point2 OS_JavaScript::get_mouse_position() const {
  265. return input->get_mouse_position();
  266. }
  267. int OS_JavaScript::get_mouse_button_state() const {
  268. return input->get_mouse_button_mask();
  269. }
  270. int OS_JavaScript::mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers) {
  271. OS_JavaScript *os = get_singleton();
  272. Ref<InputEventMouseButton> ev;
  273. ev.instance();
  274. ev->set_pressed(p_pressed);
  275. ev->set_position(Point2(p_x, p_y));
  276. ev->set_global_position(ev->get_position());
  277. ev->set_pressed(p_pressed);
  278. dom2godot_mod(ev, p_modifiers);
  279. switch (p_button) {
  280. case DOM_BUTTON_LEFT:
  281. ev->set_button_index(BUTTON_LEFT);
  282. break;
  283. case DOM_BUTTON_MIDDLE:
  284. ev->set_button_index(BUTTON_MIDDLE);
  285. break;
  286. case DOM_BUTTON_RIGHT:
  287. ev->set_button_index(BUTTON_RIGHT);
  288. break;
  289. case DOM_BUTTON_XBUTTON1:
  290. ev->set_button_index(BUTTON_XBUTTON1);
  291. break;
  292. case DOM_BUTTON_XBUTTON2:
  293. ev->set_button_index(BUTTON_XBUTTON2);
  294. break;
  295. default:
  296. return false;
  297. }
  298. if (p_pressed) {
  299. uint64_t diff = (OS::get_singleton()->get_ticks_usec() / 1000) - os->last_click_ms;
  300. if (ev->get_button_index() == os->last_click_button_index) {
  301. if (diff < 400 && Point2(os->last_click_pos).distance_to(ev->get_position()) < 5) {
  302. os->last_click_ms = 0;
  303. os->last_click_pos = Point2(-100, -100);
  304. os->last_click_button_index = -1;
  305. ev->set_doubleclick(true);
  306. }
  307. } else {
  308. os->last_click_button_index = ev->get_button_index();
  309. }
  310. if (!ev->is_doubleclick()) {
  311. os->last_click_ms += diff;
  312. os->last_click_pos = ev->get_position();
  313. }
  314. }
  315. int mask = os->input->get_mouse_button_mask();
  316. int button_flag = 1 << (ev->get_button_index() - 1);
  317. if (ev->is_pressed()) {
  318. mask |= button_flag;
  319. } else if (mask & button_flag) {
  320. mask &= ~button_flag;
  321. } else {
  322. // Received release event, but press was outside the canvas, so ignore.
  323. return false;
  324. }
  325. ev->set_button_mask(mask);
  326. os->input->parse_input_event(ev);
  327. // Resume audio context after input in case autoplay was denied.
  328. os->resume_audio();
  329. // Make sure to flush all events so we can call restricted APIs inside the event.
  330. os->input->flush_buffered_events();
  331. // Prevent multi-click text selection and wheel-click scrolling anchor.
  332. // Context menu is prevented through contextmenu event.
  333. return true;
  334. }
  335. void OS_JavaScript::mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers) {
  336. OS_JavaScript *os = get_singleton();
  337. int input_mask = os->input->get_mouse_button_mask();
  338. // For motion outside the canvas, only read mouse movement if dragging
  339. // started inside the canvas; imitating desktop app behaviour.
  340. if (!os->cursor_inside_canvas && !input_mask)
  341. return;
  342. Ref<InputEventMouseMotion> ev;
  343. ev.instance();
  344. dom2godot_mod(ev, p_modifiers);
  345. ev->set_button_mask(input_mask);
  346. ev->set_position(Point2(p_x, p_y));
  347. ev->set_global_position(ev->get_position());
  348. ev->set_relative(Vector2(p_rel_x, p_rel_y));
  349. ev->set_speed(os->input->get_last_mouse_speed());
  350. os->input->parse_input_event(ev);
  351. }
  352. static const char *godot2dom_cursor(OS::CursorShape p_shape) {
  353. switch (p_shape) {
  354. case OS::CURSOR_ARROW:
  355. default:
  356. return "default";
  357. case OS::CURSOR_IBEAM:
  358. return "text";
  359. case OS::CURSOR_POINTING_HAND:
  360. return "pointer";
  361. case OS::CURSOR_CROSS:
  362. return "crosshair";
  363. case OS::CURSOR_WAIT:
  364. return "wait";
  365. case OS::CURSOR_BUSY:
  366. return "progress";
  367. case OS::CURSOR_DRAG:
  368. return "grab";
  369. case OS::CURSOR_CAN_DROP:
  370. return "grabbing";
  371. case OS::CURSOR_FORBIDDEN:
  372. return "no-drop";
  373. case OS::CURSOR_VSIZE:
  374. return "ns-resize";
  375. case OS::CURSOR_HSIZE:
  376. return "ew-resize";
  377. case OS::CURSOR_BDIAGSIZE:
  378. return "nesw-resize";
  379. case OS::CURSOR_FDIAGSIZE:
  380. return "nwse-resize";
  381. case OS::CURSOR_MOVE:
  382. return "move";
  383. case OS::CURSOR_VSPLIT:
  384. return "row-resize";
  385. case OS::CURSOR_HSPLIT:
  386. return "col-resize";
  387. case OS::CURSOR_HELP:
  388. return "help";
  389. }
  390. }
  391. void OS_JavaScript::set_cursor_shape(CursorShape p_shape) {
  392. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  393. if (cursor_shape == p_shape) {
  394. return;
  395. }
  396. cursor_shape = p_shape;
  397. godot_js_display_cursor_set_shape(godot2dom_cursor(cursor_shape));
  398. }
  399. OS::CursorShape OS_JavaScript::get_cursor_shape() const {
  400. return cursor_shape;
  401. }
  402. void OS_JavaScript::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  403. if (p_cursor.is_valid()) {
  404. Ref<Texture> texture = p_cursor;
  405. Ref<AtlasTexture> atlas_texture = p_cursor;
  406. Ref<Image> image;
  407. Size2 texture_size;
  408. Rect2 atlas_rect;
  409. if (texture.is_valid()) {
  410. image = texture->get_data();
  411. if (image.is_valid()) {
  412. image->duplicate();
  413. }
  414. }
  415. if (!image.is_valid() && atlas_texture.is_valid()) {
  416. texture = atlas_texture->get_atlas();
  417. atlas_rect.size.width = texture->get_width();
  418. atlas_rect.size.height = texture->get_height();
  419. atlas_rect.position.x = atlas_texture->get_region().position.x;
  420. atlas_rect.position.y = atlas_texture->get_region().position.y;
  421. texture_size.width = atlas_texture->get_region().size.x;
  422. texture_size.height = atlas_texture->get_region().size.y;
  423. } else if (image.is_valid()) {
  424. texture_size.width = texture->get_width();
  425. texture_size.height = texture->get_height();
  426. }
  427. ERR_FAIL_COND(!texture.is_valid());
  428. ERR_FAIL_COND(p_hotspot.x < 0 || p_hotspot.y < 0);
  429. ERR_FAIL_COND(texture_size.width > 256 || texture_size.height > 256);
  430. ERR_FAIL_COND(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height);
  431. image = texture->get_data();
  432. ERR_FAIL_COND(!image.is_valid());
  433. image = image->duplicate();
  434. if (atlas_texture.is_valid())
  435. image->crop_from_point(
  436. atlas_rect.position.x,
  437. atlas_rect.position.y,
  438. texture_size.width,
  439. texture_size.height);
  440. if (image->get_format() != Image::FORMAT_RGBA8) {
  441. image->convert(Image::FORMAT_RGBA8);
  442. }
  443. png_image png_meta;
  444. memset(&png_meta, 0, sizeof png_meta);
  445. png_meta.version = PNG_IMAGE_VERSION;
  446. png_meta.width = texture_size.width;
  447. png_meta.height = texture_size.height;
  448. png_meta.format = PNG_FORMAT_RGBA;
  449. PoolByteArray png;
  450. size_t len;
  451. PoolByteArray::Read r = image->get_data().read();
  452. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, r.ptr(), 0, NULL));
  453. png.resize(len);
  454. PoolByteArray::Write w = png.write();
  455. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, w.ptr(), &len, 0, r.ptr(), 0, NULL));
  456. w = PoolByteArray::Write();
  457. r = png.read();
  458. godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), r.ptr(), len, p_hotspot.x, p_hotspot.y);
  459. r = PoolByteArray::Read();
  460. } else {
  461. godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), NULL, 0, 0, 0);
  462. }
  463. }
  464. void OS_JavaScript::set_mouse_mode(OS::MouseMode p_mode) {
  465. ERR_FAIL_COND_MSG(p_mode == MOUSE_MODE_CONFINED || p_mode == MOUSE_MODE_CONFINED_HIDDEN, "MOUSE_MODE_CONFINED is not supported for the HTML5 platform.");
  466. if (p_mode == get_mouse_mode()) {
  467. return;
  468. }
  469. if (p_mode == MOUSE_MODE_VISIBLE) {
  470. godot_js_display_cursor_set_visible(1);
  471. godot_js_display_cursor_lock_set(false);
  472. } else if (p_mode == MOUSE_MODE_HIDDEN) {
  473. godot_js_display_cursor_set_visible(0);
  474. godot_js_display_cursor_lock_set(false);
  475. } else if (p_mode == MOUSE_MODE_CAPTURED) {
  476. godot_js_display_cursor_set_visible(1);
  477. godot_js_display_cursor_lock_set(true);
  478. }
  479. }
  480. OS::MouseMode OS_JavaScript::get_mouse_mode() const {
  481. if (godot_js_display_cursor_is_hidden()) {
  482. return MOUSE_MODE_HIDDEN;
  483. }
  484. if (godot_js_display_cursor_is_locked()) {
  485. return MOUSE_MODE_CAPTURED;
  486. }
  487. return MOUSE_MODE_VISIBLE;
  488. }
  489. // Wheel
  490. int OS_JavaScript::mouse_wheel_callback(double p_delta_x, double p_delta_y) {
  491. OS_JavaScript *os = get_singleton();
  492. if (!godot_js_display_canvas_is_focused()) {
  493. if (os->cursor_inside_canvas) {
  494. godot_js_display_canvas_focus();
  495. } else {
  496. return false;
  497. }
  498. }
  499. InputDefault *input = os->input;
  500. Ref<InputEventMouseButton> ev;
  501. ev.instance();
  502. ev->set_position(input->get_mouse_position());
  503. ev->set_global_position(ev->get_position());
  504. ev->set_shift(input->is_key_pressed(KEY_SHIFT));
  505. ev->set_alt(input->is_key_pressed(KEY_ALT));
  506. ev->set_control(input->is_key_pressed(KEY_CONTROL));
  507. ev->set_metakey(input->is_key_pressed(KEY_META));
  508. if (p_delta_y < 0) {
  509. ev->set_button_index(BUTTON_WHEEL_UP);
  510. } else if (p_delta_y > 0) {
  511. ev->set_button_index(BUTTON_WHEEL_DOWN);
  512. } else if (p_delta_x > 0) {
  513. ev->set_button_index(BUTTON_WHEEL_LEFT);
  514. } else if (p_delta_x < 0) {
  515. ev->set_button_index(BUTTON_WHEEL_RIGHT);
  516. } else {
  517. return false;
  518. }
  519. // Different browsers give wildly different delta values, and we can't
  520. // interpret deltaMode, so use default value for wheel events' factor.
  521. int button_flag = 1 << (ev->get_button_index() - 1);
  522. ev->set_pressed(true);
  523. ev->set_button_mask(input->get_mouse_button_mask() | button_flag);
  524. input->parse_input_event(ev);
  525. Ref<InputEventMouseButton> release = ev->duplicate();
  526. release->set_pressed(false);
  527. release->set_button_mask(input->get_mouse_button_mask() & ~button_flag);
  528. input->parse_input_event(release);
  529. return true;
  530. }
  531. // Touch
  532. bool OS_JavaScript::has_touchscreen_ui_hint() const {
  533. return godot_js_display_touchscreen_is_available();
  534. }
  535. void OS_JavaScript::touch_callback(int p_type, int p_count) {
  536. OS_JavaScript *os = get_singleton();
  537. // Resume audio context after input in case autoplay was denied.
  538. os->resume_audio();
  539. const JSTouchEvent &touch_event = os->touch_event;
  540. for (int i = 0; i < p_count; i++) {
  541. Point2 point(touch_event.coords[i * 2], touch_event.coords[i * 2 + 1]);
  542. if (p_type == 2) {
  543. // touchmove
  544. Ref<InputEventScreenDrag> ev;
  545. ev.instance();
  546. ev->set_index(touch_event.identifier[i]);
  547. ev->set_position(point);
  548. Point2 &prev = os->touches[i];
  549. ev->set_relative(ev->get_position() - prev);
  550. prev = ev->get_position();
  551. os->input->parse_input_event(ev);
  552. } else {
  553. // touchstart/touchend
  554. Ref<InputEventScreenTouch> ev;
  555. ev.instance();
  556. ev->set_index(touch_event.identifier[i]);
  557. ev->set_position(point);
  558. ev->set_pressed(p_type == 0);
  559. os->touches[i] = point;
  560. os->input->parse_input_event(ev);
  561. // Make sure to flush all events so we can call restricted APIs inside the event.
  562. os->input->flush_buffered_events();
  563. }
  564. }
  565. }
  566. // Gamepad
  567. void OS_JavaScript::gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid) {
  568. InputDefault *input = get_singleton()->input;
  569. if (p_connected) {
  570. input->joy_connection_changed(p_index, true, String::utf8(p_id), String::utf8(p_guid));
  571. } else {
  572. input->joy_connection_changed(p_index, false, "");
  573. }
  574. }
  575. void OS_JavaScript::process_joypads() {
  576. int32_t pads = godot_js_input_gamepad_sample_count();
  577. int32_t s_btns_num = 0;
  578. int32_t s_axes_num = 0;
  579. int32_t s_standard = 0;
  580. float s_btns[16];
  581. float s_axes[10];
  582. for (int idx = 0; idx < pads; idx++) {
  583. int err = godot_js_input_gamepad_sample_get(idx, s_btns, &s_btns_num, s_axes, &s_axes_num, &s_standard);
  584. if (err) {
  585. continue;
  586. }
  587. for (int b = 0; b < s_btns_num; b++) {
  588. // Buttons 6 and 7 in the standard mapping need to be
  589. // axis to be handled as JOY_ANALOG by Godot.
  590. if (s_standard && (b == 6 || b == 7)) {
  591. input->joy_axis(idx, b, s_btns[b]);
  592. } else {
  593. input->joy_button(idx, b, s_btns[b]);
  594. }
  595. }
  596. for (int a = 0; a < s_axes_num; a++) {
  597. input->joy_axis(idx, a, s_axes[a]);
  598. }
  599. }
  600. }
  601. bool OS_JavaScript::is_joy_known(int p_device) {
  602. return input->is_joy_mapped(p_device);
  603. }
  604. String OS_JavaScript::get_joy_guid(int p_device) const {
  605. return input->get_joy_guid_remapped(p_device);
  606. }
  607. // Video
  608. int OS_JavaScript::get_video_driver_count() const {
  609. return VIDEO_DRIVER_MAX;
  610. }
  611. const char *OS_JavaScript::get_video_driver_name(int p_driver) const {
  612. switch (p_driver) {
  613. case VIDEO_DRIVER_GLES3:
  614. return "GLES3";
  615. case VIDEO_DRIVER_GLES2:
  616. return "GLES2";
  617. }
  618. ERR_FAIL_V_MSG(NULL, "Invalid video driver index: " + itos(p_driver) + ".");
  619. }
  620. // Audio
  621. int OS_JavaScript::get_audio_driver_count() const {
  622. return audio_drivers.size();
  623. }
  624. const char *OS_JavaScript::get_audio_driver_name(int p_driver) const {
  625. if (audio_drivers.size() <= p_driver) {
  626. return "Unknown";
  627. }
  628. return audio_drivers[p_driver]->get_name();
  629. }
  630. // Clipboard
  631. void OS_JavaScript::update_clipboard_callback(const char *p_text) {
  632. // Only call set_clipboard from OS (sets local clipboard)
  633. get_singleton()->OS::set_clipboard(String::utf8(p_text));
  634. }
  635. void OS_JavaScript::set_clipboard(const String &p_text) {
  636. OS::set_clipboard(p_text);
  637. int err = godot_js_display_clipboard_set(p_text.utf8().get_data());
  638. ERR_FAIL_COND_MSG(err, "Clipboard API is not supported.");
  639. }
  640. String OS_JavaScript::get_clipboard() const {
  641. godot_js_display_clipboard_get(update_clipboard_callback);
  642. return this->OS::get_clipboard();
  643. }
  644. // Lifecycle
  645. int OS_JavaScript::get_current_video_driver() const {
  646. return video_driver_index;
  647. }
  648. void OS_JavaScript::initialize_core() {
  649. OS_Unix::initialize_core();
  650. }
  651. Error OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  652. video_mode = p_desired;
  653. // fullscreen_change_callback will correct this if the request is successful.
  654. video_mode.fullscreen = false;
  655. // Handle contextmenu, webglcontextlost, initial canvas setup.
  656. godot_js_display_setup_canvas(video_mode.width, video_mode.height, video_mode.fullscreen, is_hidpi_allowed() ? 1 : 0);
  657. swap_ok_cancel = godot_js_display_is_swap_ok_cancel() == 1;
  658. EmscriptenWebGLContextAttributes attributes;
  659. emscripten_webgl_init_context_attributes(&attributes);
  660. attributes.alpha = GLOBAL_GET("display/window/per_pixel_transparency/allowed");
  661. attributes.antialias = false;
  662. attributes.explicitSwapControl = true;
  663. ERR_FAIL_INDEX_V(p_video_driver, VIDEO_DRIVER_MAX, ERR_INVALID_PARAMETER);
  664. if (p_desired.layered) {
  665. set_window_per_pixel_transparency_enabled(true);
  666. }
  667. bool gles3 = true;
  668. if (p_video_driver == VIDEO_DRIVER_GLES2) {
  669. gles3 = false;
  670. }
  671. bool gl_initialization_error = false;
  672. while (true) {
  673. if (gles3) {
  674. if (godot_js_display_has_webgl(2) && RasterizerGLES3::is_viable() == OK) {
  675. attributes.majorVersion = 2;
  676. RasterizerGLES3::register_config();
  677. RasterizerGLES3::make_current();
  678. break;
  679. } else {
  680. if (GLOBAL_GET("rendering/quality/driver/fallback_to_gles2")) {
  681. p_video_driver = VIDEO_DRIVER_GLES2;
  682. gles3 = false;
  683. continue;
  684. } else {
  685. gl_initialization_error = true;
  686. break;
  687. }
  688. }
  689. } else {
  690. if (godot_js_display_has_webgl(1) && RasterizerGLES2::is_viable() == OK) {
  691. attributes.majorVersion = 1;
  692. RasterizerGLES2::register_config();
  693. RasterizerGLES2::make_current();
  694. break;
  695. } else {
  696. gl_initialization_error = true;
  697. break;
  698. }
  699. }
  700. }
  701. webgl_ctx = emscripten_webgl_create_context(canvas_id, &attributes);
  702. if (emscripten_webgl_make_context_current(webgl_ctx) != EMSCRIPTEN_RESULT_SUCCESS) {
  703. gl_initialization_error = true;
  704. }
  705. if (gl_initialization_error) {
  706. OS::get_singleton()->alert("Your browser does not support any of the supported WebGL versions.\n"
  707. "Please update your browser version.",
  708. "Unable to initialize Video driver");
  709. return ERR_UNAVAILABLE;
  710. }
  711. video_driver_index = p_video_driver;
  712. AudioDriverManager::initialize(p_audio_driver);
  713. visual_server = memnew(VisualServerRaster());
  714. #ifndef NO_THREADS
  715. visual_server = memnew(VisualServerWrapMT(visual_server, false));
  716. #endif
  717. input = memnew(InputDefault);
  718. // JS Input interface (js/libs/library_godot_input.js)
  719. godot_js_input_mouse_button_cb(&OS_JavaScript::mouse_button_callback);
  720. godot_js_input_mouse_move_cb(&OS_JavaScript::mouse_move_callback);
  721. godot_js_input_mouse_wheel_cb(&OS_JavaScript::mouse_wheel_callback);
  722. godot_js_input_touch_cb(&OS_JavaScript::touch_callback, touch_event.identifier, touch_event.coords);
  723. godot_js_input_key_cb(&OS_JavaScript::key_callback, key_event.code, key_event.key);
  724. godot_js_input_gamepad_cb(&OS_JavaScript::gamepad_callback);
  725. godot_js_input_paste_cb(&OS_JavaScript::update_clipboard_callback);
  726. godot_js_input_drop_files_cb(&OS_JavaScript::drop_files_callback);
  727. // JS Display interface (js/libs/library_godot_display.js)
  728. godot_js_display_fullscreen_cb(&OS_JavaScript::fullscreen_change_callback);
  729. godot_js_display_window_blur_cb(&window_blur_callback);
  730. godot_js_display_notification_cb(&OS_JavaScript::send_notification_callback,
  731. MainLoop::NOTIFICATION_WM_MOUSE_ENTER,
  732. MainLoop::NOTIFICATION_WM_MOUSE_EXIT,
  733. MainLoop::NOTIFICATION_WM_FOCUS_IN,
  734. MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  735. godot_js_display_vk_cb(&input_text_callback);
  736. visual_server->init();
  737. return OK;
  738. }
  739. void OS_JavaScript::input_text_callback(const char *p_text, int p_cursor) {
  740. OS_JavaScript *os = OS_JavaScript::get_singleton();
  741. if (!os || !os->get_main_loop()) {
  742. return;
  743. }
  744. os->get_main_loop()->input_text(String::utf8(p_text));
  745. Ref<InputEventKey> k;
  746. for (int i = 0; i < p_cursor; i++) {
  747. k.instance();
  748. k->set_pressed(true);
  749. k->set_echo(false);
  750. k->set_scancode(KEY_RIGHT);
  751. os->input->parse_input_event(k);
  752. k.instance();
  753. k->set_pressed(false);
  754. k->set_echo(false);
  755. k->set_scancode(KEY_RIGHT);
  756. os->input->parse_input_event(k);
  757. }
  758. }
  759. bool OS_JavaScript::has_virtual_keyboard() const {
  760. return godot_js_display_vk_available() != 0;
  761. }
  762. void OS_JavaScript::show_virtual_keyboard(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) {
  763. godot_js_display_vk_show(p_existing_text.utf8().get_data(), p_type, p_cursor_start, p_cursor_end);
  764. }
  765. void OS_JavaScript::hide_virtual_keyboard() {
  766. godot_js_display_vk_hide();
  767. }
  768. bool OS_JavaScript::get_swap_ok_cancel() {
  769. return swap_ok_cancel;
  770. }
  771. void OS_JavaScript::swap_buffers() {
  772. emscripten_webgl_commit_frame();
  773. }
  774. void OS_JavaScript::set_main_loop(MainLoop *p_main_loop) {
  775. main_loop = p_main_loop;
  776. input->set_main_loop(p_main_loop);
  777. }
  778. MainLoop *OS_JavaScript::get_main_loop() const {
  779. return main_loop;
  780. }
  781. void OS_JavaScript::resume_audio() {
  782. AudioDriverJavaScript::resume();
  783. }
  784. void OS_JavaScript::fs_sync_callback() {
  785. get_singleton()->idb_is_syncing = false;
  786. }
  787. bool OS_JavaScript::main_loop_iterate() {
  788. if (is_userfs_persistent() && idb_needs_sync && !idb_is_syncing) {
  789. idb_is_syncing = true;
  790. idb_needs_sync = false;
  791. godot_js_os_fs_sync(&OS_JavaScript::fs_sync_callback);
  792. }
  793. input->flush_buffered_events();
  794. if (godot_js_input_gamepad_sample() == OK) {
  795. process_joypads();
  796. }
  797. return Main::iteration();
  798. }
  799. int OS_JavaScript::get_screen_dpi(int p_screen) const {
  800. return godot_js_display_screen_dpi_get();
  801. }
  802. float OS_JavaScript::get_screen_scale(int p_screen) const {
  803. return godot_js_display_pixel_ratio_get();
  804. }
  805. float OS_JavaScript::get_screen_max_scale() const {
  806. return get_screen_scale();
  807. }
  808. void OS_JavaScript::delete_main_loop() {
  809. memdelete(main_loop);
  810. main_loop = NULL;
  811. }
  812. void OS_JavaScript::finalize() {
  813. memdelete(input);
  814. visual_server->finish();
  815. emscripten_webgl_commit_frame();
  816. memdelete(visual_server);
  817. emscripten_webgl_destroy_context(webgl_ctx);
  818. for (int i = 0; i < audio_drivers.size(); i++) {
  819. memdelete(audio_drivers[i]);
  820. }
  821. audio_drivers.clear();
  822. }
  823. // Miscellaneous
  824. Error OS_JavaScript::execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex, bool p_open_console) {
  825. Array args;
  826. for (const List<String>::Element *E = p_arguments.front(); E; E = E->next()) {
  827. args.push_back(E->get());
  828. }
  829. String json_args = JSON::print(args);
  830. int failed = godot_js_os_execute(json_args.utf8().get_data());
  831. ERR_FAIL_COND_V_MSG(failed, ERR_UNAVAILABLE, "OS::execute() must be implemented in Javascript via 'engine.setOnExecute' if required.");
  832. return OK;
  833. }
  834. Error OS_JavaScript::kill(const ProcessID &p_pid) {
  835. ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "OS::kill() is not available on the HTML5 platform.");
  836. }
  837. int OS_JavaScript::get_process_id() const {
  838. ERR_FAIL_V_MSG(0, "OS::get_process_id() is not available on the HTML5 platform.");
  839. }
  840. bool OS_JavaScript::is_process_running(const ProcessID &p_pid) const {
  841. return false;
  842. }
  843. int OS_JavaScript::get_processor_count() const {
  844. return godot_js_os_hw_concurrency_get();
  845. }
  846. bool OS_JavaScript::_check_internal_feature_support(const String &p_feature) {
  847. if (p_feature == "HTML5" || p_feature == "web") {
  848. return true;
  849. }
  850. #ifdef JAVASCRIPT_EVAL_ENABLED
  851. if (p_feature == "JavaScript") {
  852. return true;
  853. }
  854. #endif
  855. #ifndef NO_THREADS
  856. if (p_feature == "threads") {
  857. return true;
  858. }
  859. #endif
  860. #if WASM_GDNATIVE
  861. if (p_feature == "wasm32") {
  862. return true;
  863. }
  864. #endif
  865. return false;
  866. }
  867. void OS_JavaScript::alert(const String &p_alert, const String &p_title) {
  868. godot_js_display_alert(p_alert.utf8().get_data());
  869. }
  870. void OS_JavaScript::set_window_title(const String &p_title) {
  871. godot_js_display_window_title_set(p_title.utf8().get_data());
  872. }
  873. void OS_JavaScript::set_icon(const Ref<Image> &p_icon) {
  874. ERR_FAIL_COND(p_icon.is_null());
  875. Ref<Image> icon = p_icon;
  876. if (icon->is_compressed()) {
  877. icon = icon->duplicate();
  878. ERR_FAIL_COND(icon->decompress() != OK);
  879. }
  880. if (icon->get_format() != Image::FORMAT_RGBA8) {
  881. if (icon == p_icon)
  882. icon = icon->duplicate();
  883. icon->convert(Image::FORMAT_RGBA8);
  884. }
  885. png_image png_meta;
  886. memset(&png_meta, 0, sizeof png_meta);
  887. png_meta.version = PNG_IMAGE_VERSION;
  888. png_meta.width = icon->get_width();
  889. png_meta.height = icon->get_height();
  890. png_meta.format = PNG_FORMAT_RGBA;
  891. PoolByteArray png;
  892. size_t len;
  893. PoolByteArray::Read r = icon->get_data().read();
  894. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, r.ptr(), 0, NULL));
  895. png.resize(len);
  896. PoolByteArray::Write w = png.write();
  897. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, w.ptr(), &len, 0, r.ptr(), 0, NULL));
  898. w = PoolByteArray::Write();
  899. r = png.read();
  900. godot_js_display_window_icon_set(r.ptr(), len);
  901. }
  902. String OS_JavaScript::get_executable_path() const {
  903. return OS::get_executable_path();
  904. }
  905. Error OS_JavaScript::shell_open(String p_uri) {
  906. // Open URI in a new tab, browser will deal with it by protocol.
  907. godot_js_os_shell_open(p_uri.utf8().get_data());
  908. return OK;
  909. }
  910. String OS_JavaScript::get_name() const {
  911. return "HTML5";
  912. }
  913. bool OS_JavaScript::can_draw() const {
  914. return true; // Always?
  915. }
  916. void OS_JavaScript::vibrate_handheld(int p_duration_ms) {
  917. godot_js_input_vibrate_handheld(p_duration_ms);
  918. }
  919. String OS_JavaScript::get_user_data_dir() const {
  920. return "/userfs";
  921. };
  922. String OS_JavaScript::get_cache_path() const {
  923. return "/home/web_user/.cache";
  924. }
  925. String OS_JavaScript::get_config_path() const {
  926. return "/home/web_user/.config";
  927. }
  928. String OS_JavaScript::get_data_path() const {
  929. return "/home/web_user/.local/share";
  930. }
  931. OS::PowerState OS_JavaScript::get_power_state() {
  932. WARN_PRINT_ONCE("Power management is not supported for the HTML5 platform, defaulting to POWERSTATE_UNKNOWN");
  933. return OS::POWERSTATE_UNKNOWN;
  934. }
  935. int OS_JavaScript::get_power_seconds_left() {
  936. WARN_PRINT_ONCE("Power management is not supported for the HTML5 platform, defaulting to -1");
  937. return -1;
  938. }
  939. int OS_JavaScript::get_power_percent_left() {
  940. WARN_PRINT_ONCE("Power management is not supported for the HTML5 platform, defaulting to -1");
  941. return -1;
  942. }
  943. void OS_JavaScript::file_access_close_callback(const String &p_file, int p_flags) {
  944. OS_JavaScript *os = get_singleton();
  945. if (!(os->is_userfs_persistent() && p_flags & FileAccess::WRITE)) {
  946. return; // FS persistence is not working or we are not writing.
  947. }
  948. bool is_file_persistent = p_file.begins_with("/userfs");
  949. #ifdef TOOLS_ENABLED
  950. // Hack for editor persistence (can we track).
  951. is_file_persistent = is_file_persistent || p_file.begins_with("/home/web_user/");
  952. #endif
  953. if (is_file_persistent) {
  954. os->idb_needs_sync = true;
  955. }
  956. }
  957. void OS_JavaScript::update_pwa_state_callback() {
  958. if (OS_JavaScript::get_singleton()) {
  959. OS_JavaScript::get_singleton()->pwa_is_waiting = true;
  960. }
  961. if (JavaScript::get_singleton()) {
  962. JavaScript::get_singleton()->emit_signal("pwa_update_available");
  963. }
  964. }
  965. Error OS_JavaScript::pwa_update() {
  966. return godot_js_pwa_update() ? FAILED : OK;
  967. }
  968. void OS_JavaScript::force_fs_sync() {
  969. if (is_userfs_persistent()) {
  970. idb_needs_sync = true;
  971. }
  972. }
  973. bool OS_JavaScript::is_userfs_persistent() const {
  974. return idb_available;
  975. }
  976. Error OS_JavaScript::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) {
  977. String path = p_path.get_file();
  978. p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
  979. ERR_FAIL_COND_V_MSG(!p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + p_path + ". Error: " + dlerror());
  980. return OK;
  981. }
  982. OS_JavaScript *OS_JavaScript::get_singleton() {
  983. return static_cast<OS_JavaScript *>(OS::get_singleton());
  984. }
  985. OS_JavaScript::OS_JavaScript() {
  986. tts = GLOBAL_GET("audio/general/text_to_speech");
  987. // Expose method for requesting quit.
  988. godot_js_os_request_quit_cb(&request_quit_callback);
  989. // Set canvas ID
  990. godot_js_config_canvas_id_get(canvas_id, sizeof(canvas_id));
  991. cursor_inside_canvas = true;
  992. cursor_shape = OS::CURSOR_ARROW;
  993. last_click_button_index = -1;
  994. last_click_ms = 0;
  995. last_click_pos = Point2(-100, -100);
  996. transparency_enabled = false;
  997. main_loop = NULL;
  998. visual_server = NULL;
  999. swap_ok_cancel = false;
  1000. idb_available = godot_js_os_fs_is_persistent() != 0;
  1001. idb_needs_sync = false;
  1002. idb_is_syncing = false;
  1003. pwa_is_waiting = false;
  1004. godot_js_pwa_cb(&OS_JavaScript::update_pwa_state_callback);
  1005. if (AudioDriverJavaScript::is_available()) {
  1006. #ifdef NO_THREADS
  1007. audio_drivers.push_back(memnew(AudioDriverScriptProcessor));
  1008. #endif
  1009. audio_drivers.push_back(memnew(AudioDriverWorklet));
  1010. }
  1011. for (int i = 0; i < audio_drivers.size(); i++) {
  1012. AudioDriverManager::add_driver(audio_drivers[i]);
  1013. }
  1014. Vector<Logger *> loggers;
  1015. loggers.push_back(memnew(StdLogger));
  1016. _set_logger(memnew(CompositeLogger(loggers)));
  1017. FileAccessUnix::close_notification_func = file_access_close_callback;
  1018. }