os_javascript.cpp 35 KB

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