javascript_main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*************************************************************************/
  2. /* javascript_main.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "core/io/resource_loader.h"
  31. #include "main/main.h"
  32. #include "platform/javascript/os_javascript.h"
  33. #include <emscripten/emscripten.h>
  34. #include <stdlib.h>
  35. static OS_JavaScript *os = NULL;
  36. static uint64_t target_ticks = 0;
  37. // Files drop (implemented in JS for now).
  38. extern "C" EMSCRIPTEN_KEEPALIVE void _drop_files_callback(char *p_filev[], int p_filec) {
  39. if (!os || !os->get_main_loop()) {
  40. ERR_FAIL_MSG("Unable to drop files because the OS or MainLoop are not active");
  41. }
  42. Vector<String> files;
  43. for (int i = 0; i < p_filec; i++) {
  44. files.push_back(String::utf8(p_filev[i]));
  45. }
  46. os->get_main_loop()->drop_files(files);
  47. }
  48. extern "C" EMSCRIPTEN_KEEPALIVE void _request_quit_callback(char *p_filev[], int p_filec) {
  49. if (os && os->get_main_loop()) {
  50. os->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
  51. }
  52. }
  53. void exit_callback() {
  54. emscripten_cancel_main_loop(); // After this, we can exit!
  55. Main::cleanup();
  56. int exit_code = OS_JavaScript::get_singleton()->get_exit_code();
  57. memdelete(os);
  58. os = NULL;
  59. emscripten_force_exit(exit_code); // No matter that we call cancel_main_loop, regular "exit" will not work, forcing.
  60. }
  61. void main_loop_callback() {
  62. uint64_t current_ticks = os->get_ticks_usec();
  63. bool force_draw = os->check_size_force_redraw();
  64. if (force_draw) {
  65. Main::force_redraw();
  66. } else if (current_ticks < target_ticks && !force_draw) {
  67. return; // Skip frame.
  68. }
  69. int target_fps = Engine::get_singleton()->get_target_fps();
  70. if (target_fps > 0) {
  71. target_ticks += (uint64_t)(1000000 / target_fps);
  72. }
  73. if (os->main_loop_iterate()) {
  74. emscripten_cancel_main_loop(); // Cancel current loop and wait for finalize_async.
  75. /* clang-format off */
  76. EM_ASM({
  77. // This will contain the list of operations that need to complete before cleanup.
  78. Module.async_finish = [
  79. // Always contains at least one async promise, to avoid firing immediately if nothing is added.
  80. new Promise(function(accept, reject) {
  81. setTimeout(accept, 0);
  82. })
  83. ];
  84. });
  85. /* clang-format on */
  86. os->get_main_loop()->finish();
  87. os->finalize_async(); // Will add all the async finish functions.
  88. /* clang-format off */
  89. EM_ASM({
  90. Promise.all(Module.async_finish).then(function() {
  91. Module.async_finish = [];
  92. return new Promise(function(accept, reject) {
  93. if (!Module.idbfs) {
  94. accept();
  95. return;
  96. }
  97. FS.syncfs(function(error) {
  98. if (error) {
  99. err('Failed to save IDB file system: ' + error.message);
  100. }
  101. accept();
  102. });
  103. });
  104. }).then(function() {
  105. ccall("cleanup_after_sync", null, []);
  106. });
  107. });
  108. /* clang-format on */
  109. }
  110. }
  111. extern "C" EMSCRIPTEN_KEEPALIVE void cleanup_after_sync() {
  112. emscripten_set_main_loop(exit_callback, -1, false);
  113. }
  114. int main(int argc, char *argv[]) {
  115. os = new OS_JavaScript(argc, argv);
  116. // Set canvas ID
  117. char canvas_ptr[256];
  118. /* clang-format off */
  119. EM_ASM({
  120. stringToUTF8("#" + Module['canvas'].id, $0, 255);
  121. }, canvas_ptr);
  122. /* clang-format on */
  123. os->canvas_id.parse_utf8(canvas_ptr, 255);
  124. // Set locale
  125. char locale_ptr[16];
  126. /* clang-format off */
  127. EM_ASM({
  128. stringToUTF8(Module['locale'], $0, 16);
  129. }, locale_ptr);
  130. /* clang-format on */
  131. setenv("LANG", locale_ptr, true);
  132. // Set IDBFS status
  133. os->set_idb_available((bool)EM_ASM_INT({ return Module.idbfs }));
  134. Main::setup(argv[0], argc - 1, &argv[1]);
  135. // Ease up compatibility.
  136. ResourceLoader::set_abort_on_missing_resources(false);
  137. Main::start();
  138. os->get_main_loop()->init();
  139. // Expose method for requesting quit.
  140. EM_ASM({
  141. Module['request_quit'] = function() {
  142. ccall("_request_quit_callback", null, []);
  143. };
  144. });
  145. emscripten_set_main_loop(main_loop_callback, -1, false);
  146. // Immediately run the first iteration.
  147. // We are inside an animation frame, we want to immediately draw on the newly setup canvas.
  148. main_loop_callback();
  149. }