os_web.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**************************************************************************/
  2. /* os_web.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_web.h"
  31. #include "api/javascript_bridge_singleton.h"
  32. #include "display_server_web.h"
  33. #include "godot_js.h"
  34. #include "ip_web.h"
  35. #include "net_socket_web.h"
  36. #include "core/config/project_settings.h"
  37. #include "core/debugger/engine_debugger.h"
  38. #include "core/io/file_access.h"
  39. #include "core/os/main_loop.h"
  40. #include "core/profiling/profiling.h"
  41. #include "drivers/unix/dir_access_unix.h"
  42. #include "drivers/unix/file_access_unix.h"
  43. #include "main/main.h"
  44. #include "modules/modules_enabled.gen.h" // For websocket.
  45. #include <dlfcn.h>
  46. #include <emscripten.h>
  47. #include <cstdlib>
  48. void OS_Web::alert(const String &p_alert, const String &p_title) {
  49. godot_js_display_alert(p_alert.utf8().get_data());
  50. }
  51. // Lifecycle
  52. void OS_Web::initialize() {
  53. OS_Unix::initialize_core();
  54. IPWeb::make_default();
  55. NetSocketWeb::make_default();
  56. DisplayServerWeb::register_web_driver();
  57. }
  58. void OS_Web::resume_audio() {
  59. AudioDriverWeb::resume();
  60. }
  61. void OS_Web::set_main_loop(MainLoop *p_main_loop) {
  62. main_loop = p_main_loop;
  63. }
  64. MainLoop *OS_Web::get_main_loop() const {
  65. return main_loop;
  66. }
  67. void OS_Web::fs_sync_callback() {
  68. get_singleton()->idb_is_syncing = false;
  69. }
  70. bool OS_Web::main_loop_iterate() {
  71. GodotProfileFrameMark;
  72. GodotProfileZone("OS_Web::main_loop_iterate");
  73. if (is_userfs_persistent() && idb_needs_sync && !idb_is_syncing) {
  74. idb_is_syncing = true;
  75. idb_needs_sync = false;
  76. godot_js_os_fs_sync(&fs_sync_callback);
  77. }
  78. DisplayServer::get_singleton()->process_events();
  79. return Main::iteration();
  80. }
  81. void OS_Web::delete_main_loop() {
  82. if (main_loop) {
  83. memdelete(main_loop);
  84. }
  85. main_loop = nullptr;
  86. }
  87. void OS_Web::finalize() {
  88. delete_main_loop();
  89. for (AudioDriverWeb *driver : audio_drivers) {
  90. memdelete(driver);
  91. }
  92. audio_drivers.clear();
  93. }
  94. // Miscellaneous
  95. Error OS_Web::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) {
  96. return create_process(p_path, p_arguments);
  97. }
  98. Dictionary OS_Web::execute_with_pipe(const String &p_path, const List<String> &p_arguments, bool p_blocking) {
  99. ERR_FAIL_V_MSG(Dictionary(), "OS::execute_with_pipe is not available on the Web platform.");
  100. }
  101. Error OS_Web::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id, bool p_open_console) {
  102. Array args;
  103. for (const String &E : p_arguments) {
  104. args.push_back(E);
  105. }
  106. String json_args = Variant(args).to_json_string();
  107. int failed = godot_js_os_execute(json_args.utf8().get_data());
  108. ERR_FAIL_COND_V_MSG(failed, ERR_UNAVAILABLE, "OS::execute() or create_process() must be implemented in Web via 'engine.setOnExecute' if required.");
  109. return OK;
  110. }
  111. Error OS_Web::kill(const ProcessID &p_pid) {
  112. ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "OS::kill() is not available on the Web platform.");
  113. }
  114. int OS_Web::get_process_id() const {
  115. return 0;
  116. }
  117. bool OS_Web::is_process_running(const ProcessID &p_pid) const {
  118. return false;
  119. }
  120. int OS_Web::get_process_exit_code(const ProcessID &p_pid) const {
  121. return -1;
  122. }
  123. int OS_Web::get_processor_count() const {
  124. return godot_js_os_hw_concurrency_get();
  125. }
  126. String OS_Web::get_unique_id() const {
  127. ERR_FAIL_V_MSG("", "OS::get_unique_id() is not available on the Web platform.");
  128. }
  129. int OS_Web::get_default_thread_pool_size() const {
  130. #ifdef THREADS_ENABLED
  131. return godot_js_os_thread_pool_size_get();
  132. #else // No threads.
  133. return 1;
  134. #endif
  135. }
  136. bool OS_Web::_check_internal_feature_support(const String &p_feature) {
  137. if (p_feature == "web") {
  138. return true;
  139. }
  140. if (p_feature == "web_extensions") {
  141. #ifdef WEB_DLINK_ENABLED
  142. return true;
  143. #else
  144. return false;
  145. #endif
  146. }
  147. if (p_feature == "web_noextensions") {
  148. #ifdef WEB_DLINK_ENABLED
  149. return false;
  150. #else
  151. return true;
  152. #endif
  153. }
  154. if (godot_js_os_has_feature(p_feature.utf8().get_data())) {
  155. return true;
  156. }
  157. return false;
  158. }
  159. String OS_Web::get_executable_path() const {
  160. return OS::get_executable_path();
  161. }
  162. Error OS_Web::shell_open(const String &p_uri) {
  163. // Open URI in a new tab, browser will deal with it by protocol.
  164. godot_js_os_shell_open(p_uri.utf8().get_data());
  165. return OK;
  166. }
  167. String OS_Web::get_name() const {
  168. return "Web";
  169. }
  170. void OS_Web::add_frame_delay(bool p_can_draw, bool p_wake_for_events) {
  171. #ifndef PROXY_TO_PTHREAD_ENABLED
  172. OS::add_frame_delay(p_can_draw, p_wake_for_events);
  173. #endif
  174. }
  175. void OS_Web::vibrate_handheld(int p_duration_ms, float p_amplitude) {
  176. godot_js_input_vibrate_handheld(p_duration_ms);
  177. }
  178. String OS_Web::get_user_data_dir(const String &p_user_dir) const {
  179. String userfs = "/userfs";
  180. return userfs.path_join(p_user_dir).replace_char('\\', '/');
  181. }
  182. String OS_Web::get_cache_path() const {
  183. return "/home/web_user/.cache";
  184. }
  185. String OS_Web::get_config_path() const {
  186. return "/home/web_user/.config";
  187. }
  188. String OS_Web::get_data_path() const {
  189. return "/home/web_user/.local/share";
  190. }
  191. void OS_Web::file_access_close_callback(const String &p_file, int p_flags) {
  192. OS_Web *os = OS_Web::get_singleton();
  193. if (!(os->is_userfs_persistent() && (p_flags & FileAccess::WRITE))) {
  194. return; // FS persistence is not working or we are not writing.
  195. }
  196. bool is_file_persistent = p_file.begins_with("/userfs");
  197. #ifdef TOOLS_ENABLED
  198. // Hack for editor persistence (can we track).
  199. is_file_persistent = is_file_persistent || p_file.begins_with("/home/web_user/");
  200. #endif
  201. if (is_file_persistent) {
  202. os->idb_needs_sync = true;
  203. }
  204. }
  205. void OS_Web::dir_access_remove_callback(const String &p_file) {
  206. OS_Web *os = OS_Web::get_singleton();
  207. bool is_file_persistent = p_file.begins_with("/userfs");
  208. #ifdef TOOLS_ENABLED
  209. // Hack for editor persistence (can we track).
  210. is_file_persistent = is_file_persistent || p_file.begins_with("/home/web_user/");
  211. #endif
  212. if (is_file_persistent) {
  213. os->idb_needs_sync = true;
  214. }
  215. }
  216. void OS_Web::update_pwa_state_callback() {
  217. if (OS_Web::get_singleton()) {
  218. OS_Web::get_singleton()->pwa_is_waiting = true;
  219. }
  220. if (JavaScriptBridge::get_singleton()) {
  221. JavaScriptBridge::get_singleton()->emit_signal("pwa_update_available");
  222. }
  223. }
  224. void OS_Web::force_fs_sync() {
  225. if (is_userfs_persistent()) {
  226. idb_needs_sync = true;
  227. }
  228. }
  229. Error OS_Web::pwa_update() {
  230. return godot_js_pwa_update() ? FAILED : OK;
  231. }
  232. bool OS_Web::is_userfs_persistent() const {
  233. return idb_available;
  234. }
  235. Error OS_Web::open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data) {
  236. String path = p_path.get_file();
  237. p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
  238. ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, dlerror()));
  239. if (p_data != nullptr && p_data->r_resolved_path != nullptr) {
  240. *p_data->r_resolved_path = path;
  241. }
  242. return OK;
  243. }
  244. OS_Web *OS_Web::get_singleton() {
  245. return static_cast<OS_Web *>(OS::get_singleton());
  246. }
  247. void OS_Web::initialize_joypads() {
  248. }
  249. OS_Web::OS_Web() {
  250. char locale_ptr[16];
  251. godot_js_config_locale_get(locale_ptr, 16);
  252. setenv("LANG", locale_ptr, true);
  253. godot_js_pwa_cb(&OS_Web::update_pwa_state_callback);
  254. if (AudioDriverWeb::is_available()) {
  255. audio_drivers.push_back(memnew(AudioDriverWorklet));
  256. audio_drivers.push_back(memnew(AudioDriverScriptProcessor));
  257. }
  258. for (AudioDriverWeb *audio_driver : audio_drivers) {
  259. AudioDriverManager::add_driver(audio_driver);
  260. }
  261. idb_available = godot_js_os_fs_is_persistent();
  262. Vector<Logger *> loggers;
  263. loggers.push_back(memnew(StdLogger));
  264. _set_logger(memnew(CompositeLogger(loggers)));
  265. FileAccessUnix::close_notification_func = file_access_close_callback;
  266. DirAccessUnix::remove_notification_func = dir_access_remove_callback;
  267. }