os_javascript.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*************************************************************************/
  2. /* os_javascript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "drivers/unix/dir_access_unix.h"
  33. #include "drivers/unix/file_access_unix.h"
  34. #include "main/main.h"
  35. #include "platform/javascript/display_server_javascript.h"
  36. #include "modules/modules_enabled.gen.h" // For websocket.
  37. #ifdef MODULE_WEBSOCKET_ENABLED
  38. #include "modules/websocket/remote_debugger_peer_websocket.h"
  39. #endif
  40. #include <dlfcn.h>
  41. #include <emscripten.h>
  42. #include <stdlib.h>
  43. #include "api/javascript_singleton.h"
  44. #include "godot_js.h"
  45. void OS_JavaScript::alert(const String &p_alert, const String &p_title) {
  46. godot_js_display_alert(p_alert.utf8().get_data());
  47. }
  48. // Lifecycle
  49. void OS_JavaScript::initialize() {
  50. OS_Unix::initialize_core();
  51. DisplayServerJavaScript::register_javascript_driver();
  52. #ifdef MODULE_WEBSOCKET_ENABLED
  53. EngineDebugger::register_uri_handler("ws://", RemoteDebuggerPeerWebSocket::create);
  54. EngineDebugger::register_uri_handler("wss://", RemoteDebuggerPeerWebSocket::create);
  55. #endif
  56. }
  57. void OS_JavaScript::resume_audio() {
  58. AudioDriverJavaScript::resume();
  59. }
  60. void OS_JavaScript::set_main_loop(MainLoop *p_main_loop) {
  61. main_loop = p_main_loop;
  62. }
  63. MainLoop *OS_JavaScript::get_main_loop() const {
  64. return main_loop;
  65. }
  66. void OS_JavaScript::fs_sync_callback() {
  67. get_singleton()->idb_is_syncing = false;
  68. }
  69. bool OS_JavaScript::main_loop_iterate() {
  70. if (is_userfs_persistent() && idb_needs_sync && !idb_is_syncing) {
  71. idb_is_syncing = true;
  72. idb_needs_sync = false;
  73. godot_js_os_fs_sync(&fs_sync_callback);
  74. }
  75. DisplayServer::get_singleton()->process_events();
  76. return Main::iteration();
  77. }
  78. void OS_JavaScript::delete_main_loop() {
  79. if (main_loop) {
  80. memdelete(main_loop);
  81. }
  82. main_loop = nullptr;
  83. }
  84. void OS_JavaScript::finalize() {
  85. delete_main_loop();
  86. for (AudioDriverJavaScript *driver : audio_drivers) {
  87. memdelete(driver);
  88. }
  89. audio_drivers.clear();
  90. }
  91. // Miscellaneous
  92. Error OS_JavaScript::execute(const String &p_path, const List<String> &p_arguments, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex, bool p_open_console) {
  93. return create_process(p_path, p_arguments);
  94. }
  95. Error OS_JavaScript::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id, bool p_open_console) {
  96. Array args;
  97. for (const String &E : p_arguments) {
  98. args.push_back(E);
  99. }
  100. String json_args = Variant(args).to_json_string();
  101. int failed = godot_js_os_execute(json_args.utf8().get_data());
  102. ERR_FAIL_COND_V_MSG(failed, ERR_UNAVAILABLE, "OS::execute() or create_process() must be implemented in JavaScript via 'engine.setOnExecute' if required.");
  103. return OK;
  104. }
  105. Error OS_JavaScript::kill(const ProcessID &p_pid) {
  106. ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "OS::kill() is not available on the HTML5 platform.");
  107. }
  108. int OS_JavaScript::get_process_id() const {
  109. ERR_FAIL_V_MSG(0, "OS::get_process_id() is not available on the HTML5 platform.");
  110. }
  111. bool OS_JavaScript::is_process_running(const ProcessID &p_pid) const {
  112. return false;
  113. }
  114. int OS_JavaScript::get_processor_count() const {
  115. return godot_js_os_hw_concurrency_get();
  116. }
  117. bool OS_JavaScript::_check_internal_feature_support(const String &p_feature) {
  118. if (p_feature == "html5" || p_feature == "web") {
  119. return true;
  120. }
  121. #ifdef JAVASCRIPT_EVAL_ENABLED
  122. if (p_feature == "javascript") {
  123. return true;
  124. }
  125. #endif
  126. #ifndef NO_THREADS
  127. if (p_feature == "threads") {
  128. return true;
  129. }
  130. #endif
  131. #if WASM_GDNATIVE
  132. if (p_feature == "wasm32") {
  133. return true;
  134. }
  135. #endif
  136. return false;
  137. }
  138. String OS_JavaScript::get_executable_path() const {
  139. return OS::get_executable_path();
  140. }
  141. Error OS_JavaScript::shell_open(String p_uri) {
  142. // Open URI in a new tab, browser will deal with it by protocol.
  143. godot_js_os_shell_open(p_uri.utf8().get_data());
  144. return OK;
  145. }
  146. String OS_JavaScript::get_name() const {
  147. return "HTML5";
  148. }
  149. void OS_JavaScript::vibrate_handheld(int p_duration_ms) {
  150. godot_js_input_vibrate_handheld(p_duration_ms);
  151. }
  152. String OS_JavaScript::get_user_data_dir() const {
  153. return "/userfs";
  154. }
  155. String OS_JavaScript::get_cache_path() const {
  156. return "/home/web_user/.cache";
  157. }
  158. String OS_JavaScript::get_config_path() const {
  159. return "/home/web_user/.config";
  160. }
  161. String OS_JavaScript::get_data_path() const {
  162. return "/home/web_user/.local/share";
  163. }
  164. void OS_JavaScript::file_access_close_callback(const String &p_file, int p_flags) {
  165. OS_JavaScript *os = OS_JavaScript::get_singleton();
  166. if (!(os->is_userfs_persistent() && (p_flags & FileAccess::WRITE))) {
  167. return; // FS persistence is not working or we are not writing.
  168. }
  169. bool is_file_persistent = p_file.begins_with("/userfs");
  170. #ifdef TOOLS_ENABLED
  171. // Hack for editor persistence (can we track).
  172. is_file_persistent = is_file_persistent || p_file.begins_with("/home/web_user/");
  173. #endif
  174. if (is_file_persistent) {
  175. os->idb_needs_sync = true;
  176. }
  177. }
  178. void OS_JavaScript::update_pwa_state_callback() {
  179. if (OS_JavaScript::get_singleton()) {
  180. OS_JavaScript::get_singleton()->pwa_is_waiting = true;
  181. }
  182. if (JavaScript::get_singleton()) {
  183. JavaScript::get_singleton()->emit_signal("pwa_update_available");
  184. }
  185. }
  186. Error OS_JavaScript::pwa_update() {
  187. return godot_js_pwa_update() ? FAILED : OK;
  188. }
  189. bool OS_JavaScript::is_userfs_persistent() const {
  190. return idb_available;
  191. }
  192. Error OS_JavaScript::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path, String *r_resolved_path) {
  193. String path = p_path.get_file();
  194. p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
  195. ERR_FAIL_COND_V_MSG(!p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + p_path + ". Error: " + dlerror());
  196. if (r_resolved_path != nullptr) {
  197. *r_resolved_path = path;
  198. }
  199. return OK;
  200. }
  201. OS_JavaScript *OS_JavaScript::get_singleton() {
  202. return static_cast<OS_JavaScript *>(OS::get_singleton());
  203. }
  204. void OS_JavaScript::initialize_joypads() {
  205. }
  206. OS_JavaScript::OS_JavaScript() {
  207. char locale_ptr[16];
  208. godot_js_config_locale_get(locale_ptr, 16);
  209. setenv("LANG", locale_ptr, true);
  210. godot_js_pwa_cb(&OS_JavaScript::update_pwa_state_callback);
  211. if (AudioDriverJavaScript::is_available()) {
  212. #ifdef NO_THREADS
  213. audio_drivers.push_back(memnew(AudioDriverScriptProcessor));
  214. #endif
  215. audio_drivers.push_back(memnew(AudioDriverWorklet));
  216. }
  217. for (int i = 0; i < audio_drivers.size(); i++) {
  218. AudioDriverManager::add_driver(audio_drivers[i]);
  219. }
  220. idb_available = godot_js_os_fs_is_persistent();
  221. Vector<Logger *> loggers;
  222. loggers.push_back(memnew(StdLogger));
  223. _set_logger(memnew(CompositeLogger(loggers)));
  224. FileAccessUnix::close_notification_func = file_access_close_callback;
  225. }