os_javascript.cpp 36 KB

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