os.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /**************************************************************************/
  2. /* os.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.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/dir_access.h"
  34. #include "core/io/file_access.h"
  35. #include "core/os/midi_driver.h"
  36. #include "core/version_generated.gen.h"
  37. #include <stdarg.h>
  38. #include <thread>
  39. OS *OS::singleton = nullptr;
  40. uint64_t OS::target_ticks = 0;
  41. OS *OS::get_singleton() {
  42. return singleton;
  43. }
  44. uint64_t OS::get_ticks_msec() const {
  45. return get_ticks_usec() / 1000ULL;
  46. }
  47. double OS::get_unix_time() const {
  48. return 0;
  49. }
  50. void OS::_set_logger(CompositeLogger *p_logger) {
  51. if (_logger) {
  52. memdelete(_logger);
  53. }
  54. _logger = p_logger;
  55. }
  56. void OS::add_logger(Logger *p_logger) {
  57. if (!_logger) {
  58. Vector<Logger *> loggers;
  59. loggers.push_back(p_logger);
  60. _logger = memnew(CompositeLogger(loggers));
  61. } else {
  62. _logger->add_logger(p_logger);
  63. }
  64. }
  65. void OS::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify, Logger::ErrorType p_type) {
  66. if (!_stderr_enabled) {
  67. return;
  68. }
  69. if (_logger) {
  70. _logger->log_error(p_function, p_file, p_line, p_code, p_rationale, p_editor_notify, p_type);
  71. }
  72. }
  73. void OS::print(const char *p_format, ...) {
  74. if (!_stdout_enabled) {
  75. return;
  76. }
  77. va_list argp;
  78. va_start(argp, p_format);
  79. if (_logger) {
  80. _logger->logv(p_format, argp, false);
  81. }
  82. va_end(argp);
  83. }
  84. void OS::print_rich(const char *p_format, ...) {
  85. if (!_stdout_enabled) {
  86. return;
  87. }
  88. va_list argp;
  89. va_start(argp, p_format);
  90. if (_logger) {
  91. _logger->logv(p_format, argp, false);
  92. }
  93. va_end(argp);
  94. }
  95. void OS::printerr(const char *p_format, ...) {
  96. if (!_stderr_enabled) {
  97. return;
  98. }
  99. va_list argp;
  100. va_start(argp, p_format);
  101. if (_logger) {
  102. _logger->logv(p_format, argp, true);
  103. }
  104. va_end(argp);
  105. }
  106. void OS::alert(const String &p_alert, const String &p_title) {
  107. fprintf(stderr, "%s: %s\n", p_title.utf8().get_data(), p_alert.utf8().get_data());
  108. }
  109. void OS::set_low_processor_usage_mode(bool p_enabled) {
  110. low_processor_usage_mode = p_enabled;
  111. }
  112. bool OS::is_in_low_processor_usage_mode() const {
  113. return low_processor_usage_mode;
  114. }
  115. void OS::set_low_processor_usage_mode_sleep_usec(int p_usec) {
  116. low_processor_usage_mode_sleep_usec = p_usec;
  117. }
  118. int OS::get_low_processor_usage_mode_sleep_usec() const {
  119. return low_processor_usage_mode_sleep_usec;
  120. }
  121. String OS::get_executable_path() const {
  122. return _execpath;
  123. }
  124. int OS::get_process_id() const {
  125. return -1;
  126. }
  127. bool OS::is_stdout_verbose() const {
  128. return _verbose_stdout;
  129. }
  130. bool OS::is_stdout_debug_enabled() const {
  131. return _debug_stdout;
  132. }
  133. bool OS::is_stdout_enabled() const {
  134. return _stdout_enabled;
  135. }
  136. bool OS::is_stderr_enabled() const {
  137. return _stderr_enabled;
  138. }
  139. void OS::set_stdout_enabled(bool p_enabled) {
  140. _stdout_enabled = p_enabled;
  141. }
  142. void OS::set_stderr_enabled(bool p_enabled) {
  143. _stderr_enabled = p_enabled;
  144. }
  145. int OS::get_exit_code() const {
  146. return _exit_code;
  147. }
  148. void OS::set_exit_code(int p_code) {
  149. _exit_code = p_code;
  150. }
  151. String OS::get_locale() const {
  152. return "en";
  153. }
  154. // Non-virtual helper to extract the 2 or 3-letter language code from
  155. // `get_locale()` in a way that's consistent for all platforms.
  156. String OS::get_locale_language() const {
  157. return get_locale().left(3).replace("_", "");
  158. }
  159. // Embedded PCK offset.
  160. uint64_t OS::get_embedded_pck_offset() const {
  161. return 0;
  162. }
  163. // Helper function to ensure that a dir name/path will be valid on the OS
  164. String OS::get_safe_dir_name(const String &p_dir_name, bool p_allow_paths) const {
  165. String safe_dir_name = p_dir_name;
  166. Vector<String> invalid_chars = String(": * ? \" < > |").split(" ");
  167. if (p_allow_paths) {
  168. // Dir separators are allowed, but disallow ".." to avoid going up the filesystem
  169. invalid_chars.push_back("..");
  170. safe_dir_name = safe_dir_name.replace("\\", "/").strip_edges();
  171. } else {
  172. invalid_chars.push_back("/");
  173. invalid_chars.push_back("\\");
  174. safe_dir_name = safe_dir_name.strip_edges();
  175. // These directory names are invalid.
  176. if (safe_dir_name == ".") {
  177. safe_dir_name = "dot";
  178. } else if (safe_dir_name == "..") {
  179. safe_dir_name = "twodots";
  180. }
  181. }
  182. for (int i = 0; i < invalid_chars.size(); i++) {
  183. safe_dir_name = safe_dir_name.replace(invalid_chars[i], "-");
  184. }
  185. return safe_dir_name;
  186. }
  187. // Path to data, config, cache, etc. OS-specific folders
  188. // Get properly capitalized engine name for system paths
  189. String OS::get_godot_dir_name() const {
  190. // Default to lowercase, so only override when different case is needed
  191. return String(VERSION_SHORT_NAME).to_lower();
  192. }
  193. // OS equivalent of XDG_DATA_HOME
  194. String OS::get_data_path() const {
  195. return ".";
  196. }
  197. // OS equivalent of XDG_CONFIG_HOME
  198. String OS::get_config_path() const {
  199. return ".";
  200. }
  201. // OS equivalent of XDG_CACHE_HOME
  202. String OS::get_cache_path() const {
  203. return ".";
  204. }
  205. // Path to macOS .app bundle resources
  206. String OS::get_bundle_resource_dir() const {
  207. return ".";
  208. }
  209. // Path to macOS .app bundle embedded icon
  210. String OS::get_bundle_icon_path() const {
  211. return String();
  212. }
  213. // OS specific path for user://
  214. String OS::get_user_data_dir() const {
  215. return ".";
  216. }
  217. // Absolute path to res://
  218. String OS::get_resource_dir() const {
  219. return ProjectSettings::get_singleton()->get_resource_path();
  220. }
  221. // Access system-specific dirs like Documents, Downloads, etc.
  222. String OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  223. return ".";
  224. }
  225. Error OS::shell_open(String p_uri) {
  226. return ERR_UNAVAILABLE;
  227. }
  228. // implement these with the canvas?
  229. uint64_t OS::get_static_memory_usage() const {
  230. return Memory::get_mem_usage();
  231. }
  232. uint64_t OS::get_static_memory_peak_usage() const {
  233. return Memory::get_mem_max_usage();
  234. }
  235. Error OS::set_cwd(const String &p_cwd) {
  236. return ERR_CANT_OPEN;
  237. }
  238. uint64_t OS::get_free_static_memory() const {
  239. return Memory::get_mem_available();
  240. }
  241. void OS::yield() {
  242. }
  243. void OS::ensure_user_data_dir() {
  244. String dd = get_user_data_dir();
  245. if (DirAccess::exists(dd)) {
  246. return;
  247. }
  248. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  249. Error err = da->make_dir_recursive(dd);
  250. ERR_FAIL_COND_MSG(err != OK, "Error attempting to create data dir: " + dd + ".");
  251. }
  252. String OS::get_model_name() const {
  253. return "GenericDevice";
  254. }
  255. void OS::set_cmdline(const char *p_execpath, const List<String> &p_args, const List<String> &p_user_args) {
  256. _execpath = String::utf8(p_execpath);
  257. _cmdline = p_args;
  258. _user_args = p_user_args;
  259. }
  260. String OS::get_unique_id() const {
  261. ERR_FAIL_V("");
  262. }
  263. int OS::get_processor_count() const {
  264. return std::thread::hardware_concurrency();
  265. }
  266. String OS::get_processor_name() const {
  267. return "";
  268. }
  269. void OS::set_has_server_feature_callback(HasServerFeatureCallback p_callback) {
  270. has_server_feature_callback = p_callback;
  271. }
  272. bool OS::has_feature(const String &p_feature) {
  273. // Feature tags are always lowercase for consistency.
  274. if (p_feature == get_name().to_lower()) {
  275. return true;
  276. }
  277. // Catch-all `linuxbsd` feature tag that matches on both Linux and BSD.
  278. // This is the one exposed in the project settings dialog.
  279. if (p_feature == "linuxbsd" && (get_name() == "Linux" || get_name() == "FreeBSD" || get_name() == "NetBSD" || get_name() == "OpenBSD" || get_name() == "BSD")) {
  280. return true;
  281. }
  282. if (p_feature == "movie") {
  283. return _writing_movie;
  284. }
  285. #ifdef DEBUG_ENABLED
  286. if (p_feature == "debug") {
  287. return true;
  288. }
  289. #endif // DEBUG_ENABLED
  290. #ifdef TOOLS_ENABLED
  291. if (p_feature == "editor") {
  292. return true;
  293. }
  294. #else
  295. if (p_feature == "template") {
  296. return true;
  297. }
  298. #ifdef DEBUG_ENABLED
  299. if (p_feature == "template_debug") {
  300. return true;
  301. }
  302. #else
  303. if (p_feature == "template_release" || p_feature == "release") {
  304. return true;
  305. }
  306. #endif // DEBUG_ENABLED
  307. #endif // TOOLS_ENABLED
  308. #ifdef REAL_T_IS_DOUBLE
  309. if (p_feature == "double") {
  310. return true;
  311. }
  312. #else
  313. if (p_feature == "single") {
  314. return true;
  315. }
  316. #endif // REAL_T_IS_DOUBLE
  317. if (sizeof(void *) == 8 && p_feature == "64") {
  318. return true;
  319. }
  320. if (sizeof(void *) == 4 && p_feature == "32") {
  321. return true;
  322. }
  323. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(_M_X64)
  324. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
  325. if (p_feature == "x86_64") {
  326. return true;
  327. }
  328. #elif defined(__i386) || defined(__i386__) || defined(_M_IX86)
  329. if (p_feature == "x86_32") {
  330. return true;
  331. }
  332. #endif
  333. if (p_feature == "x86") {
  334. return true;
  335. }
  336. #elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
  337. #if defined(__aarch64__) || defined(_M_ARM64)
  338. if (p_feature == "arm64") {
  339. return true;
  340. }
  341. #elif defined(__arm__) || defined(_M_ARM)
  342. if (p_feature == "arm32") {
  343. return true;
  344. }
  345. #endif
  346. #if defined(__ARM_ARCH_7A__)
  347. if (p_feature == "armv7a" || p_feature == "armv7") {
  348. return true;
  349. }
  350. #endif
  351. #if defined(__ARM_ARCH_7S__)
  352. if (p_feature == "armv7s" || p_feature == "armv7") {
  353. return true;
  354. }
  355. #endif
  356. if (p_feature == "arm") {
  357. return true;
  358. }
  359. #elif defined(__riscv)
  360. #if __riscv_xlen == 8
  361. if (p_feature == "rv64") {
  362. return true;
  363. }
  364. #endif
  365. if (p_feature == "riscv") {
  366. return true;
  367. }
  368. #elif defined(__powerpc__)
  369. #if defined(__powerpc64__)
  370. if (p_feature == "ppc64") {
  371. return true;
  372. }
  373. #endif
  374. if (p_feature == "ppc") {
  375. return true;
  376. }
  377. #elif defined(__wasm__)
  378. #if defined(__wasm64__)
  379. if (p_feature == "wasm64") {
  380. return true;
  381. }
  382. #elif defined(__wasm32__)
  383. if (p_feature == "wasm32") {
  384. return true;
  385. }
  386. #endif
  387. if (p_feature == "wasm") {
  388. return true;
  389. }
  390. #endif
  391. if (_check_internal_feature_support(p_feature)) {
  392. return true;
  393. }
  394. if (has_server_feature_callback && has_server_feature_callback(p_feature)) {
  395. return true;
  396. }
  397. if (ProjectSettings::get_singleton()->has_custom_feature(p_feature)) {
  398. return true;
  399. }
  400. return false;
  401. }
  402. void OS::set_restart_on_exit(bool p_restart, const List<String> &p_restart_arguments) {
  403. restart_on_exit = p_restart;
  404. restart_commandline = p_restart_arguments;
  405. }
  406. bool OS::is_restart_on_exit_set() const {
  407. return restart_on_exit;
  408. }
  409. List<String> OS::get_restart_on_exit_arguments() const {
  410. return restart_commandline;
  411. }
  412. PackedStringArray OS::get_connected_midi_inputs() {
  413. if (MIDIDriver::get_singleton()) {
  414. return MIDIDriver::get_singleton()->get_connected_inputs();
  415. }
  416. PackedStringArray list;
  417. ERR_FAIL_V_MSG(list, vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  418. }
  419. void OS::open_midi_inputs() {
  420. if (MIDIDriver::get_singleton()) {
  421. MIDIDriver::get_singleton()->open();
  422. } else {
  423. ERR_PRINT(vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  424. }
  425. }
  426. void OS::close_midi_inputs() {
  427. if (MIDIDriver::get_singleton()) {
  428. MIDIDriver::get_singleton()->close();
  429. } else {
  430. ERR_PRINT(vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  431. }
  432. }
  433. void OS::add_frame_delay(bool p_can_draw) {
  434. const uint32_t frame_delay = Engine::get_singleton()->get_frame_delay();
  435. if (frame_delay) {
  436. // Add fixed frame delay to decrease CPU/GPU usage. This doesn't take
  437. // the actual frame time into account.
  438. // Due to the high fluctuation of the actual sleep duration, it's not recommended
  439. // to use this as a FPS limiter.
  440. delay_usec(frame_delay * 1000);
  441. }
  442. // Add a dynamic frame delay to decrease CPU/GPU usage. This takes the
  443. // previous frame time into account for a smoother result.
  444. uint64_t dynamic_delay = 0;
  445. if (is_in_low_processor_usage_mode() || !p_can_draw) {
  446. dynamic_delay = get_low_processor_usage_mode_sleep_usec();
  447. }
  448. const int max_fps = Engine::get_singleton()->get_max_fps();
  449. if (max_fps > 0 && !Engine::get_singleton()->is_editor_hint()) {
  450. // Override the low processor usage mode sleep delay if the target FPS is lower.
  451. dynamic_delay = MAX(dynamic_delay, (uint64_t)(1000000 / max_fps));
  452. }
  453. if (dynamic_delay > 0) {
  454. target_ticks += dynamic_delay;
  455. uint64_t current_ticks = get_ticks_usec();
  456. if (current_ticks < target_ticks) {
  457. delay_usec(target_ticks - current_ticks);
  458. }
  459. current_ticks = get_ticks_usec();
  460. target_ticks = MIN(MAX(target_ticks, current_ticks - dynamic_delay), current_ticks + dynamic_delay);
  461. }
  462. }
  463. OS::OS() {
  464. singleton = this;
  465. Vector<Logger *> loggers;
  466. loggers.push_back(memnew(StdLogger));
  467. _set_logger(memnew(CompositeLogger(loggers)));
  468. }
  469. OS::~OS() {
  470. if (_logger) {
  471. memdelete(_logger);
  472. }
  473. singleton = nullptr;
  474. }