os_javascript.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*************************************************************************/
  2. /* os_javascript.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 "os_javascript.h"
  31. #include "core/debugger/engine_debugger.h"
  32. #include "core/io/file_access_buffered_fa.h"
  33. #include "core/io/json.h"
  34. #include "drivers/unix/dir_access_unix.h"
  35. #include "drivers/unix/file_access_unix.h"
  36. #include "main/main.h"
  37. #include "modules/modules_enabled.gen.h"
  38. #include "platform/javascript/display_server_javascript.h"
  39. #ifdef MODULE_WEBSOCKET_ENABLED
  40. #include "modules/websocket/remote_debugger_peer_websocket.h"
  41. #endif
  42. #include <emscripten.h>
  43. #include <stdlib.h>
  44. // Lifecycle
  45. void OS_JavaScript::initialize() {
  46. OS_Unix::initialize_core();
  47. FileAccess::make_default<FileAccessBufferedFA<FileAccessUnix>>(FileAccess::ACCESS_RESOURCES);
  48. DisplayServerJavaScript::register_javascript_driver();
  49. #ifdef MODULE_WEBSOCKET_ENABLED
  50. EngineDebugger::register_uri_handler("ws://", RemoteDebuggerPeerWebSocket::create);
  51. EngineDebugger::register_uri_handler("wss://", RemoteDebuggerPeerWebSocket::create);
  52. #endif
  53. }
  54. void OS_JavaScript::resume_audio() {
  55. if (audio_driver_javascript) {
  56. audio_driver_javascript->resume();
  57. }
  58. }
  59. void OS_JavaScript::set_main_loop(MainLoop *p_main_loop) {
  60. main_loop = p_main_loop;
  61. }
  62. MainLoop *OS_JavaScript::get_main_loop() const {
  63. return main_loop;
  64. }
  65. extern "C" EMSCRIPTEN_KEEPALIVE void _idb_synced() {
  66. OS_JavaScript::get_singleton()->idb_is_syncing = false;
  67. }
  68. bool OS_JavaScript::main_loop_iterate() {
  69. if (is_userfs_persistent() && idb_needs_sync && !idb_is_syncing) {
  70. idb_is_syncing = true;
  71. idb_needs_sync = false;
  72. /* clang-format off */
  73. EM_ASM({
  74. FS.syncfs(function(error) {
  75. if (error) {
  76. err('Failed to save IDB file system: ' + error.message);
  77. }
  78. ccall("_idb_synced", 'void', [], []);
  79. });
  80. });
  81. /* clang-format on */
  82. }
  83. DisplayServer::get_singleton()->process_events();
  84. return Main::iteration();
  85. }
  86. void OS_JavaScript::delete_main_loop() {
  87. if (main_loop) {
  88. memdelete(main_loop);
  89. }
  90. main_loop = nullptr;
  91. }
  92. void OS_JavaScript::finalize_async() {
  93. finalizing = true;
  94. if (audio_driver_javascript) {
  95. audio_driver_javascript->finish_async();
  96. }
  97. }
  98. void OS_JavaScript::finalize() {
  99. delete_main_loop();
  100. if (audio_driver_javascript) {
  101. memdelete(audio_driver_javascript);
  102. audio_driver_javascript = nullptr;
  103. }
  104. }
  105. // Miscellaneous
  106. 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) {
  107. Array args;
  108. for (const List<String>::Element *E = p_arguments.front(); E; E = E->next()) {
  109. args.push_back(E->get());
  110. }
  111. String json_args = JSON::print(args);
  112. /* clang-format off */
  113. int failed = EM_ASM_INT({
  114. const json_args = UTF8ToString($0);
  115. const args = JSON.parse(json_args);
  116. if (Module["onExecute"]) {
  117. Module["onExecute"](args);
  118. return 0;
  119. }
  120. return 1;
  121. }, json_args.utf8().get_data());
  122. /* clang-format on */
  123. ERR_FAIL_COND_V_MSG(failed, ERR_UNAVAILABLE, "OS::execute() must be implemented in JavaScript via 'engine.setOnExecute' if required.");
  124. return OK;
  125. }
  126. Error OS_JavaScript::kill(const ProcessID &p_pid) {
  127. ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "OS::kill() is not available on the HTML5 platform.");
  128. }
  129. int OS_JavaScript::get_process_id() const {
  130. ERR_FAIL_V_MSG(0, "OS::get_process_id() is not available on the HTML5 platform.");
  131. }
  132. bool OS_JavaScript::_check_internal_feature_support(const String &p_feature) {
  133. if (p_feature == "HTML5" || p_feature == "web")
  134. return true;
  135. #ifdef JAVASCRIPT_EVAL_ENABLED
  136. if (p_feature == "JavaScript")
  137. return true;
  138. #endif
  139. return false;
  140. }
  141. String OS_JavaScript::get_executable_path() const {
  142. return OS::get_executable_path();
  143. }
  144. Error OS_JavaScript::shell_open(String p_uri) {
  145. // Open URI in a new tab, browser will deal with it by protocol.
  146. /* clang-format off */
  147. EM_ASM_({
  148. window.open(UTF8ToString($0), '_blank');
  149. }, p_uri.utf8().get_data());
  150. /* clang-format on */
  151. return OK;
  152. }
  153. String OS_JavaScript::get_name() const {
  154. return "HTML5";
  155. }
  156. String OS_JavaScript::get_user_data_dir() const {
  157. return "/userfs";
  158. };
  159. String OS_JavaScript::get_cache_path() const {
  160. return "/home/web_user/.cache";
  161. }
  162. String OS_JavaScript::get_config_path() const {
  163. return "/home/web_user/.config";
  164. }
  165. String OS_JavaScript::get_data_path() const {
  166. return "/home/web_user/.local/share";
  167. }
  168. void OS_JavaScript::file_access_close_callback(const String &p_file, int p_flags) {
  169. OS_JavaScript *os = OS_JavaScript::get_singleton();
  170. if (!(os->is_userfs_persistent() && (p_flags & FileAccess::WRITE))) {
  171. return; // FS persistence is not working or we are not writing.
  172. }
  173. bool is_file_persistent = p_file.begins_with("/userfs");
  174. #ifdef TOOLS_ENABLED
  175. // Hack for editor persistence (can we track).
  176. is_file_persistent = is_file_persistent || p_file.begins_with("/home/web_user/");
  177. #endif
  178. if (is_file_persistent) {
  179. os->idb_needs_sync = true;
  180. }
  181. }
  182. void OS_JavaScript::set_idb_available(bool p_idb_available) {
  183. idb_available = p_idb_available;
  184. }
  185. bool OS_JavaScript::is_userfs_persistent() const {
  186. return idb_available;
  187. }
  188. OS_JavaScript *OS_JavaScript::get_singleton() {
  189. return static_cast<OS_JavaScript *>(OS::get_singleton());
  190. }
  191. void OS_JavaScript::initialize_joypads() {
  192. }
  193. OS_JavaScript::OS_JavaScript() {
  194. if (AudioDriverJavaScript::is_available()) {
  195. audio_driver_javascript = memnew(AudioDriverJavaScript);
  196. AudioDriverManager::add_driver(audio_driver_javascript);
  197. }
  198. Vector<Logger *> loggers;
  199. loggers.push_back(memnew(StdLogger));
  200. _set_logger(memnew(CompositeLogger(loggers)));
  201. FileAccessUnix::close_notification_func = file_access_close_callback;
  202. }