os.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*************************************************************************/
  2. /* os.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.h"
  31. #include "core/input/input.h"
  32. #include "core/os/dir_access.h"
  33. #include "core/os/file_access.h"
  34. #include "core/os/midi_driver.h"
  35. #include "core/project_settings.h"
  36. #include "core/version_generated.gen.h"
  37. #include "servers/audio_server.h"
  38. #include <stdarg.h>
  39. OS *OS::singleton = nullptr;
  40. OS *OS::get_singleton() {
  41. return singleton;
  42. }
  43. uint32_t OS::get_ticks_msec() const {
  44. return get_ticks_usec() / 1000;
  45. }
  46. String OS::get_iso_date_time(bool local) const {
  47. OS::Date date = get_date(local);
  48. OS::Time time = get_time(local);
  49. String timezone;
  50. if (!local) {
  51. TimeZoneInfo zone = get_time_zone_info();
  52. if (zone.bias >= 0) {
  53. timezone = "+";
  54. }
  55. timezone = timezone + itos(zone.bias / 60).pad_zeros(2) + itos(zone.bias % 60).pad_zeros(2);
  56. } else {
  57. timezone = "Z";
  58. }
  59. return itos(date.year).pad_zeros(2) +
  60. "-" +
  61. itos(date.month).pad_zeros(2) +
  62. "-" +
  63. itos(date.day).pad_zeros(2) +
  64. "T" +
  65. itos(time.hour).pad_zeros(2) +
  66. ":" +
  67. itos(time.min).pad_zeros(2) +
  68. ":" +
  69. itos(time.sec).pad_zeros(2) +
  70. timezone;
  71. }
  72. uint64_t OS::get_splash_tick_msec() const {
  73. return _msec_splash;
  74. }
  75. uint64_t OS::get_unix_time() const {
  76. return 0;
  77. };
  78. uint64_t OS::get_system_time_secs() const {
  79. return 0;
  80. }
  81. uint64_t OS::get_system_time_msecs() const {
  82. return 0;
  83. }
  84. void OS::debug_break(){
  85. // something
  86. };
  87. void OS::_set_logger(CompositeLogger *p_logger) {
  88. if (_logger) {
  89. memdelete(_logger);
  90. }
  91. _logger = p_logger;
  92. }
  93. void OS::add_logger(Logger *p_logger) {
  94. if (!_logger) {
  95. Vector<Logger *> loggers;
  96. loggers.push_back(p_logger);
  97. _logger = memnew(CompositeLogger(loggers));
  98. } else {
  99. _logger->add_logger(p_logger);
  100. }
  101. }
  102. void OS::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, Logger::ErrorType p_type) {
  103. _logger->log_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
  104. }
  105. void OS::print(const char *p_format, ...) {
  106. va_list argp;
  107. va_start(argp, p_format);
  108. _logger->logv(p_format, argp, false);
  109. va_end(argp);
  110. };
  111. void OS::printerr(const char *p_format, ...) {
  112. va_list argp;
  113. va_start(argp, p_format);
  114. _logger->logv(p_format, argp, true);
  115. va_end(argp);
  116. };
  117. void OS::set_low_processor_usage_mode(bool p_enabled) {
  118. low_processor_usage_mode = p_enabled;
  119. }
  120. bool OS::is_in_low_processor_usage_mode() const {
  121. return low_processor_usage_mode;
  122. }
  123. void OS::set_low_processor_usage_mode_sleep_usec(int p_usec) {
  124. low_processor_usage_mode_sleep_usec = p_usec;
  125. }
  126. int OS::get_low_processor_usage_mode_sleep_usec() const {
  127. return low_processor_usage_mode_sleep_usec;
  128. }
  129. String OS::get_executable_path() const {
  130. return _execpath;
  131. }
  132. int OS::get_process_id() const {
  133. return -1;
  134. };
  135. void OS::vibrate_handheld(int p_duration_ms) {
  136. WARN_PRINT("vibrate_handheld() only works with Android and iOS");
  137. }
  138. bool OS::is_stdout_verbose() const {
  139. return _verbose_stdout;
  140. }
  141. void OS::dump_memory_to_file(const char *p_file) {
  142. //Memory::dump_static_mem_to_file(p_file);
  143. }
  144. static FileAccess *_OSPRF = nullptr;
  145. static void _OS_printres(Object *p_obj) {
  146. Resource *res = Object::cast_to<Resource>(p_obj);
  147. if (!res)
  148. return;
  149. String str = itos(res->get_instance_id()) + String(res->get_class()) + ":" + String(res->get_name()) + " - " + res->get_path();
  150. if (_OSPRF)
  151. _OSPRF->store_line(str);
  152. else
  153. print_line(str);
  154. }
  155. void OS::print_all_resources(String p_to_file) {
  156. ERR_FAIL_COND(p_to_file != "" && _OSPRF);
  157. if (p_to_file != "") {
  158. Error err;
  159. _OSPRF = FileAccess::open(p_to_file, FileAccess::WRITE, &err);
  160. if (err != OK) {
  161. _OSPRF = nullptr;
  162. ERR_FAIL_MSG("Can't print all resources to file: " + String(p_to_file) + ".");
  163. }
  164. }
  165. ObjectDB::debug_objects(_OS_printres);
  166. if (p_to_file != "") {
  167. if (_OSPRF)
  168. memdelete(_OSPRF);
  169. _OSPRF = nullptr;
  170. }
  171. }
  172. void OS::print_resources_in_use(bool p_short) {
  173. ResourceCache::dump(nullptr, p_short);
  174. }
  175. void OS::dump_resources_to_file(const char *p_file) {
  176. ResourceCache::dump(p_file);
  177. }
  178. void OS::set_no_window_mode(bool p_enable) {
  179. _no_window = p_enable;
  180. }
  181. bool OS::is_no_window_mode_enabled() const {
  182. return _no_window;
  183. }
  184. int OS::get_exit_code() const {
  185. return _exit_code;
  186. }
  187. void OS::set_exit_code(int p_code) {
  188. _exit_code = p_code;
  189. }
  190. String OS::get_locale() const {
  191. return "en";
  192. }
  193. // Helper function to ensure that a dir name/path will be valid on the OS
  194. String OS::get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator) const {
  195. Vector<String> invalid_chars = String(": * ? \" < > |").split(" ");
  196. if (p_allow_dir_separator) {
  197. // Dir separators are allowed, but disallow ".." to avoid going up the filesystem
  198. invalid_chars.push_back("..");
  199. } else {
  200. invalid_chars.push_back("/");
  201. }
  202. String safe_dir_name = p_dir_name.replace("\\", "/").strip_edges();
  203. for (int i = 0; i < invalid_chars.size(); i++) {
  204. safe_dir_name = safe_dir_name.replace(invalid_chars[i], "-");
  205. }
  206. return safe_dir_name;
  207. }
  208. // Path to data, config, cache, etc. OS-specific folders
  209. // Get properly capitalized engine name for system paths
  210. String OS::get_godot_dir_name() const {
  211. // Default to lowercase, so only override when different case is needed
  212. return String(VERSION_SHORT_NAME).to_lower();
  213. }
  214. // OS equivalent of XDG_DATA_HOME
  215. String OS::get_data_path() const {
  216. return ".";
  217. }
  218. // OS equivalent of XDG_CONFIG_HOME
  219. String OS::get_config_path() const {
  220. return ".";
  221. }
  222. // OS equivalent of XDG_CACHE_HOME
  223. String OS::get_cache_path() const {
  224. return ".";
  225. }
  226. // Path to macOS .app bundle resources
  227. String OS::get_bundle_resource_dir() const {
  228. return ".";
  229. };
  230. // OS specific path for user://
  231. String OS::get_user_data_dir() const {
  232. return ".";
  233. };
  234. // Absolute path to res://
  235. String OS::get_resource_dir() const {
  236. return ProjectSettings::get_singleton()->get_resource_path();
  237. }
  238. // Access system-specific dirs like Documents, Downloads, etc.
  239. String OS::get_system_dir(SystemDir p_dir) const {
  240. return ".";
  241. }
  242. Error OS::shell_open(String p_uri) {
  243. return ERR_UNAVAILABLE;
  244. };
  245. // implement these with the canvas?
  246. uint64_t OS::get_static_memory_usage() const {
  247. return Memory::get_mem_usage();
  248. }
  249. uint64_t OS::get_static_memory_peak_usage() const {
  250. return Memory::get_mem_max_usage();
  251. }
  252. Error OS::set_cwd(const String &p_cwd) {
  253. return ERR_CANT_OPEN;
  254. }
  255. uint64_t OS::get_free_static_memory() const {
  256. return Memory::get_mem_available();
  257. }
  258. void OS::yield() {
  259. }
  260. void OS::ensure_user_data_dir() {
  261. String dd = get_user_data_dir();
  262. DirAccess *da = DirAccess::open(dd);
  263. if (da) {
  264. memdelete(da);
  265. return;
  266. }
  267. da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  268. Error err = da->make_dir_recursive(dd);
  269. ERR_FAIL_COND_MSG(err != OK, "Error attempting to create data dir: " + dd + ".");
  270. memdelete(da);
  271. }
  272. String OS::get_model_name() const {
  273. return "GenericDevice";
  274. }
  275. void OS::set_cmdline(const char *p_execpath, const List<String> &p_args) {
  276. _execpath = p_execpath;
  277. _cmdline = p_args;
  278. };
  279. String OS::get_unique_id() const {
  280. ERR_FAIL_V("");
  281. }
  282. int OS::get_processor_count() const {
  283. return 1;
  284. }
  285. bool OS::can_use_threads() const {
  286. #ifdef NO_THREADS
  287. return false;
  288. #else
  289. return true;
  290. #endif
  291. }
  292. void OS::set_has_server_feature_callback(HasServerFeatureCallback p_callback) {
  293. has_server_feature_callback = p_callback;
  294. }
  295. bool OS::has_feature(const String &p_feature) {
  296. if (p_feature == get_name())
  297. return true;
  298. #ifdef DEBUG_ENABLED
  299. if (p_feature == "debug")
  300. return true;
  301. #else
  302. if (p_feature == "release")
  303. return true;
  304. #endif
  305. #ifdef TOOLS_ENABLED
  306. if (p_feature == "editor")
  307. return true;
  308. #else
  309. if (p_feature == "standalone")
  310. return true;
  311. #endif
  312. if (sizeof(void *) == 8 && p_feature == "64") {
  313. return true;
  314. }
  315. if (sizeof(void *) == 4 && p_feature == "32") {
  316. return true;
  317. }
  318. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__)
  319. if (p_feature == "x86_64") {
  320. return true;
  321. }
  322. #elif (defined(__i386) || defined(__i386__))
  323. if (p_feature == "x86") {
  324. return true;
  325. }
  326. #elif defined(__aarch64__)
  327. if (p_feature == "arm64") {
  328. return true;
  329. }
  330. #elif defined(__arm__)
  331. #if defined(__ARM_ARCH_7A__)
  332. if (p_feature == "armv7a" || p_feature == "armv7") {
  333. return true;
  334. }
  335. #endif
  336. #if defined(__ARM_ARCH_7S__)
  337. if (p_feature == "armv7s" || p_feature == "armv7") {
  338. return true;
  339. }
  340. #endif
  341. if (p_feature == "arm") {
  342. return true;
  343. }
  344. #endif
  345. if (_check_internal_feature_support(p_feature))
  346. return true;
  347. if (has_server_feature_callback && has_server_feature_callback(p_feature)) {
  348. return true;
  349. }
  350. if (ProjectSettings::get_singleton()->has_custom_feature(p_feature))
  351. return true;
  352. return false;
  353. }
  354. void OS::set_restart_on_exit(bool p_restart, const List<String> &p_restart_arguments) {
  355. restart_on_exit = p_restart;
  356. restart_commandline = p_restart_arguments;
  357. }
  358. bool OS::is_restart_on_exit_set() const {
  359. return restart_on_exit;
  360. }
  361. List<String> OS::get_restart_on_exit_arguments() const {
  362. return restart_commandline;
  363. }
  364. PackedStringArray OS::get_connected_midi_inputs() {
  365. if (MIDIDriver::get_singleton())
  366. return MIDIDriver::get_singleton()->get_connected_inputs();
  367. PackedStringArray list;
  368. return list;
  369. }
  370. void OS::open_midi_inputs() {
  371. if (MIDIDriver::get_singleton())
  372. MIDIDriver::get_singleton()->open();
  373. }
  374. void OS::close_midi_inputs() {
  375. if (MIDIDriver::get_singleton())
  376. MIDIDriver::get_singleton()->close();
  377. }
  378. OS::OS() {
  379. void *volatile stack_bottom;
  380. singleton = this;
  381. _stack_bottom = (void *)(&stack_bottom);
  382. Vector<Logger *> loggers;
  383. loggers.push_back(memnew(StdLogger));
  384. _set_logger(memnew(CompositeLogger(loggers)));
  385. }
  386. OS::~OS() {
  387. memdelete(_logger);
  388. singleton = nullptr;
  389. }