os_javascript.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 <dlfcn.h>
  43. #include <emscripten.h>
  44. #include <stdlib.h>
  45. #include "godot_js.h"
  46. // Lifecycle
  47. void OS_JavaScript::initialize() {
  48. OS_Unix::initialize_core();
  49. FileAccess::make_default<FileAccessBufferedFA<FileAccessUnix>>(FileAccess::ACCESS_RESOURCES);
  50. DisplayServerJavaScript::register_javascript_driver();
  51. #ifdef MODULE_WEBSOCKET_ENABLED
  52. EngineDebugger::register_uri_handler("ws://", RemoteDebuggerPeerWebSocket::create);
  53. EngineDebugger::register_uri_handler("wss://", RemoteDebuggerPeerWebSocket::create);
  54. #endif
  55. }
  56. void OS_JavaScript::resume_audio() {
  57. if (audio_driver_javascript) {
  58. audio_driver_javascript->resume();
  59. }
  60. }
  61. void OS_JavaScript::set_main_loop(MainLoop *p_main_loop) {
  62. main_loop = p_main_loop;
  63. }
  64. MainLoop *OS_JavaScript::get_main_loop() const {
  65. return main_loop;
  66. }
  67. void OS_JavaScript::fs_sync_callback() {
  68. get_singleton()->idb_is_syncing = false;
  69. }
  70. bool OS_JavaScript::main_loop_iterate() {
  71. if (is_userfs_persistent() && idb_needs_sync && !idb_is_syncing) {
  72. idb_is_syncing = true;
  73. idb_needs_sync = false;
  74. godot_js_os_fs_sync(&fs_sync_callback);
  75. }
  76. DisplayServer::get_singleton()->process_events();
  77. return Main::iteration();
  78. }
  79. void OS_JavaScript::delete_main_loop() {
  80. if (main_loop) {
  81. memdelete(main_loop);
  82. }
  83. main_loop = nullptr;
  84. }
  85. void OS_JavaScript::finalize() {
  86. delete_main_loop();
  87. if (audio_driver_javascript) {
  88. memdelete(audio_driver_javascript);
  89. audio_driver_javascript = nullptr;
  90. }
  91. }
  92. // Miscellaneous
  93. 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) {
  94. Array args;
  95. for (const List<String>::Element *E = p_arguments.front(); E; E = E->next()) {
  96. args.push_back(E->get());
  97. }
  98. String json_args = JSON::print(args);
  99. int failed = godot_js_os_execute(json_args.utf8().get_data());
  100. ERR_FAIL_COND_V_MSG(failed, ERR_UNAVAILABLE, "OS::execute() must be implemented in JavaScript via 'engine.setOnExecute' if required.");
  101. return OK;
  102. }
  103. Error OS_JavaScript::kill(const ProcessID &p_pid) {
  104. ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "OS::kill() is not available on the HTML5 platform.");
  105. }
  106. int OS_JavaScript::get_process_id() const {
  107. ERR_FAIL_V_MSG(0, "OS::get_process_id() is not available on the HTML5 platform.");
  108. }
  109. bool OS_JavaScript::_check_internal_feature_support(const String &p_feature) {
  110. if (p_feature == "HTML5" || p_feature == "web")
  111. return true;
  112. #ifdef JAVASCRIPT_EVAL_ENABLED
  113. if (p_feature == "JavaScript")
  114. return true;
  115. #endif
  116. return false;
  117. }
  118. String OS_JavaScript::get_executable_path() const {
  119. return OS::get_executable_path();
  120. }
  121. Error OS_JavaScript::shell_open(String p_uri) {
  122. // Open URI in a new tab, browser will deal with it by protocol.
  123. godot_js_os_shell_open(p_uri.utf8().get_data());
  124. return OK;
  125. }
  126. String OS_JavaScript::get_name() const {
  127. return "HTML5";
  128. }
  129. String OS_JavaScript::get_user_data_dir() const {
  130. return "/userfs";
  131. };
  132. String OS_JavaScript::get_cache_path() const {
  133. return "/home/web_user/.cache";
  134. }
  135. String OS_JavaScript::get_config_path() const {
  136. return "/home/web_user/.config";
  137. }
  138. String OS_JavaScript::get_data_path() const {
  139. return "/home/web_user/.local/share";
  140. }
  141. void OS_JavaScript::file_access_close_callback(const String &p_file, int p_flags) {
  142. OS_JavaScript *os = OS_JavaScript::get_singleton();
  143. if (!(os->is_userfs_persistent() && (p_flags & FileAccess::WRITE))) {
  144. return; // FS persistence is not working or we are not writing.
  145. }
  146. bool is_file_persistent = p_file.begins_with("/userfs");
  147. #ifdef TOOLS_ENABLED
  148. // Hack for editor persistence (can we track).
  149. is_file_persistent = is_file_persistent || p_file.begins_with("/home/web_user/");
  150. #endif
  151. if (is_file_persistent) {
  152. os->idb_needs_sync = true;
  153. }
  154. }
  155. bool OS_JavaScript::is_userfs_persistent() const {
  156. return idb_available;
  157. }
  158. Error OS_JavaScript::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) {
  159. String path = p_path.get_file();
  160. p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
  161. ERR_FAIL_COND_V_MSG(!p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + p_path + ". Error: " + dlerror());
  162. return OK;
  163. }
  164. OS_JavaScript *OS_JavaScript::get_singleton() {
  165. return static_cast<OS_JavaScript *>(OS::get_singleton());
  166. }
  167. void OS_JavaScript::initialize_joypads() {
  168. }
  169. OS_JavaScript::OS_JavaScript() {
  170. char locale_ptr[16];
  171. godot_js_config_locale_get(locale_ptr, 16);
  172. setenv("LANG", locale_ptr, true);
  173. if (AudioDriverJavaScript::is_available()) {
  174. audio_driver_javascript = memnew(AudioDriverJavaScript);
  175. AudioDriverManager::add_driver(audio_driver_javascript);
  176. }
  177. idb_available = godot_js_os_fs_is_persistent();
  178. Vector<Logger *> loggers;
  179. loggers.push_back(memnew(StdLogger));
  180. _set_logger(memnew(CompositeLogger(loggers)));
  181. FileAccessUnix::close_notification_func = file_access_close_callback;
  182. }