os_javascript.cpp 7.4 KB

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