os_javascript.cpp 35 KB

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