os.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*************************************************************************/
  2. /* os.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/os/dir_access.h"
  32. #include "core/os/file_access.h"
  33. #include "core/os/input.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 = NULL;
  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_keep_screen_on(bool p_enabled) {
  118. _keep_screen_on = p_enabled;
  119. }
  120. bool OS::is_keep_screen_on() const {
  121. return _keep_screen_on;
  122. }
  123. void OS::set_low_processor_usage_mode(bool p_enabled) {
  124. low_processor_usage_mode = p_enabled;
  125. }
  126. bool OS::is_in_low_processor_usage_mode() const {
  127. return low_processor_usage_mode;
  128. }
  129. void OS::set_low_processor_usage_mode_sleep_usec(int p_usec) {
  130. low_processor_usage_mode_sleep_usec = p_usec;
  131. }
  132. int OS::get_low_processor_usage_mode_sleep_usec() const {
  133. return low_processor_usage_mode_sleep_usec;
  134. }
  135. void OS::set_clipboard(const String &p_text) {
  136. _local_clipboard = p_text;
  137. }
  138. String OS::get_clipboard() const {
  139. return _local_clipboard;
  140. }
  141. String OS::get_executable_path() const {
  142. return _execpath;
  143. }
  144. int OS::get_process_id() const {
  145. return -1;
  146. };
  147. void OS::vibrate_handheld(int p_duration_ms) {
  148. WARN_PRINTS("vibrate_handheld() only works with Android and iOS");
  149. }
  150. bool OS::is_stdout_verbose() const {
  151. return _verbose_stdout;
  152. }
  153. void OS::set_last_error(const char *p_error) {
  154. GLOBAL_LOCK_FUNCTION
  155. if (p_error == NULL)
  156. p_error = "Unknown Error";
  157. if (last_error)
  158. memfree(last_error);
  159. last_error = NULL;
  160. int len = 0;
  161. while (p_error[len++])
  162. ;
  163. last_error = (char *)memalloc(len);
  164. for (int i = 0; i < len; i++)
  165. last_error[i] = p_error[i];
  166. }
  167. const char *OS::get_last_error() const {
  168. GLOBAL_LOCK_FUNCTION
  169. return last_error ? last_error : "";
  170. }
  171. void OS::dump_memory_to_file(const char *p_file) {
  172. //Memory::dump_static_mem_to_file(p_file);
  173. }
  174. static FileAccess *_OSPRF = NULL;
  175. static void _OS_printres(Object *p_obj) {
  176. Resource *res = Object::cast_to<Resource>(p_obj);
  177. if (!res)
  178. return;
  179. String str = itos(res->get_instance_id()) + String(res->get_class()) + ":" + String(res->get_name()) + " - " + res->get_path();
  180. if (_OSPRF)
  181. _OSPRF->store_line(str);
  182. else
  183. print_line(str);
  184. }
  185. bool OS::has_virtual_keyboard() const {
  186. return false;
  187. }
  188. void OS::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) {
  189. }
  190. void OS::hide_virtual_keyboard() {
  191. }
  192. int OS::get_virtual_keyboard_height() const {
  193. return 0;
  194. }
  195. void OS::set_cursor_shape(CursorShape p_shape) {
  196. }
  197. OS::CursorShape OS::get_cursor_shape() const {
  198. return CURSOR_ARROW;
  199. }
  200. void OS::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  201. }
  202. void OS::print_all_resources(String p_to_file) {
  203. ERR_FAIL_COND(p_to_file != "" && _OSPRF);
  204. if (p_to_file != "") {
  205. Error err;
  206. _OSPRF = FileAccess::open(p_to_file, FileAccess::WRITE, &err);
  207. if (err != OK) {
  208. _OSPRF = NULL;
  209. ERR_FAIL_MSG("Can't print all resources to file: " + String(p_to_file) + ".");
  210. }
  211. }
  212. ObjectDB::debug_objects(_OS_printres);
  213. if (p_to_file != "") {
  214. if (_OSPRF)
  215. memdelete(_OSPRF);
  216. _OSPRF = NULL;
  217. }
  218. }
  219. void OS::print_resources_in_use(bool p_short) {
  220. ResourceCache::dump(NULL, p_short);
  221. }
  222. void OS::dump_resources_to_file(const char *p_file) {
  223. ResourceCache::dump(p_file);
  224. }
  225. void OS::clear_last_error() {
  226. GLOBAL_LOCK_FUNCTION
  227. if (last_error)
  228. memfree(last_error);
  229. last_error = NULL;
  230. }
  231. void OS::set_no_window_mode(bool p_enable) {
  232. _no_window = p_enable;
  233. }
  234. bool OS::is_no_window_mode_enabled() const {
  235. return _no_window;
  236. }
  237. int OS::get_exit_code() const {
  238. return _exit_code;
  239. }
  240. void OS::set_exit_code(int p_code) {
  241. _exit_code = p_code;
  242. }
  243. String OS::get_locale() const {
  244. return "en";
  245. }
  246. // Helper function to ensure that a dir name/path will be valid on the OS
  247. String OS::get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator) const {
  248. Vector<String> invalid_chars = String(": * ? \" < > |").split(" ");
  249. if (p_allow_dir_separator) {
  250. // Dir separators are allowed, but disallow ".." to avoid going up the filesystem
  251. invalid_chars.push_back("..");
  252. } else {
  253. invalid_chars.push_back("/");
  254. }
  255. String safe_dir_name = p_dir_name.replace("\\", "/").strip_edges();
  256. for (int i = 0; i < invalid_chars.size(); i++) {
  257. safe_dir_name = safe_dir_name.replace(invalid_chars[i], "-");
  258. }
  259. return safe_dir_name;
  260. }
  261. // Path to data, config, cache, etc. OS-specific folders
  262. // Get properly capitalized engine name for system paths
  263. String OS::get_godot_dir_name() const {
  264. // Default to lowercase, so only override when different case is needed
  265. return String(VERSION_SHORT_NAME).to_lower();
  266. }
  267. // OS equivalent of XDG_DATA_HOME
  268. String OS::get_data_path() const {
  269. return ".";
  270. }
  271. // OS equivalent of XDG_CONFIG_HOME
  272. String OS::get_config_path() const {
  273. return ".";
  274. }
  275. // OS equivalent of XDG_CACHE_HOME
  276. String OS::get_cache_path() const {
  277. return ".";
  278. }
  279. // OS specific path for user://
  280. String OS::get_user_data_dir() const {
  281. return ".";
  282. };
  283. // Absolute path to res://
  284. String OS::get_resource_dir() const {
  285. return ProjectSettings::get_singleton()->get_resource_path();
  286. }
  287. // Access system-specific dirs like Documents, Downloads, etc.
  288. String OS::get_system_dir(SystemDir p_dir) const {
  289. return ".";
  290. }
  291. Error OS::shell_open(String p_uri) {
  292. return ERR_UNAVAILABLE;
  293. };
  294. // implement these with the canvas?
  295. Error OS::dialog_show(String p_title, String p_description, Vector<String> p_buttons, Object *p_obj, String p_callback) {
  296. while (true) {
  297. print("%ls\n--------\n%ls\n", p_title.c_str(), p_description.c_str());
  298. for (int i = 0; i < p_buttons.size(); i++) {
  299. if (i > 0) print(", ");
  300. print("%i=%ls", i + 1, p_buttons[i].c_str());
  301. };
  302. print("\n");
  303. String res = get_stdin_string().strip_edges();
  304. if (!res.is_numeric())
  305. continue;
  306. int n = res.to_int();
  307. if (n < 0 || n >= p_buttons.size())
  308. continue;
  309. if (p_obj && p_callback != "")
  310. p_obj->call_deferred(p_callback, n);
  311. break;
  312. };
  313. return OK;
  314. };
  315. Error OS::dialog_input_text(String p_title, String p_description, String p_partial, Object *p_obj, String p_callback) {
  316. ERR_FAIL_COND_V(!p_obj, FAILED);
  317. ERR_FAIL_COND_V(p_callback == "", FAILED);
  318. print("%ls\n---------\n%ls\n[%ls]:\n", p_title.c_str(), p_description.c_str(), p_partial.c_str());
  319. String res = get_stdin_string().strip_edges();
  320. bool success = true;
  321. if (res == "") {
  322. res = p_partial;
  323. };
  324. p_obj->call_deferred(p_callback, success, res);
  325. return OK;
  326. };
  327. uint64_t OS::get_static_memory_usage() const {
  328. return Memory::get_mem_usage();
  329. }
  330. uint64_t OS::get_dynamic_memory_usage() const {
  331. return MemoryPool::total_memory;
  332. }
  333. uint64_t OS::get_static_memory_peak_usage() const {
  334. return Memory::get_mem_max_usage();
  335. }
  336. Error OS::set_cwd(const String &p_cwd) {
  337. return ERR_CANT_OPEN;
  338. }
  339. bool OS::has_touchscreen_ui_hint() const {
  340. //return false;
  341. return Input::get_singleton() && Input::get_singleton()->is_emulating_touch_from_mouse();
  342. }
  343. uint64_t OS::get_free_static_memory() const {
  344. return Memory::get_mem_available();
  345. }
  346. void OS::yield() {
  347. }
  348. void OS::set_screen_orientation(ScreenOrientation p_orientation) {
  349. _orientation = p_orientation;
  350. }
  351. OS::ScreenOrientation OS::get_screen_orientation() const {
  352. return (OS::ScreenOrientation)_orientation;
  353. }
  354. void OS::_ensure_user_data_dir() {
  355. String dd = get_user_data_dir();
  356. DirAccess *da = DirAccess::open(dd);
  357. if (da) {
  358. memdelete(da);
  359. return;
  360. }
  361. da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  362. Error err = da->make_dir_recursive(dd);
  363. ERR_FAIL_COND_MSG(err != OK, "Error attempting to create data dir: " + dd + ".");
  364. memdelete(da);
  365. }
  366. void OS::set_native_icon(const String &p_filename) {
  367. }
  368. void OS::set_icon(const Ref<Image> &p_icon) {
  369. }
  370. String OS::get_model_name() const {
  371. return "GenericDevice";
  372. }
  373. void OS::set_cmdline(const char *p_execpath, const List<String> &p_args) {
  374. _execpath = p_execpath;
  375. _cmdline = p_args;
  376. };
  377. void OS::release_rendering_thread() {
  378. }
  379. void OS::make_rendering_thread() {
  380. }
  381. void OS::swap_buffers() {
  382. }
  383. String OS::get_unique_id() const {
  384. ERR_FAIL_V("");
  385. }
  386. int OS::get_processor_count() const {
  387. return 1;
  388. }
  389. Error OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  390. return FAILED;
  391. };
  392. bool OS::native_video_is_playing() const {
  393. return false;
  394. };
  395. void OS::native_video_pause(){
  396. };
  397. void OS::native_video_unpause(){
  398. };
  399. void OS::native_video_stop(){
  400. };
  401. void OS::set_mouse_mode(MouseMode p_mode) {
  402. }
  403. bool OS::can_use_threads() const {
  404. #ifdef NO_THREADS
  405. return false;
  406. #else
  407. return true;
  408. #endif
  409. }
  410. OS::MouseMode OS::get_mouse_mode() const {
  411. return MOUSE_MODE_VISIBLE;
  412. }
  413. OS::LatinKeyboardVariant OS::get_latin_keyboard_variant() const {
  414. return LATIN_KEYBOARD_QWERTY;
  415. }
  416. bool OS::is_joy_known(int p_device) {
  417. return true;
  418. }
  419. String OS::get_joy_guid(int p_device) const {
  420. return "Default Joypad";
  421. }
  422. void OS::set_context(int p_context) {
  423. }
  424. OS::SwitchVSyncCallbackInThread OS::switch_vsync_function = NULL;
  425. void OS::set_use_vsync(bool p_enable) {
  426. _use_vsync = p_enable;
  427. if (switch_vsync_function) { //if a function was set, use function
  428. switch_vsync_function(p_enable);
  429. } else { //otherwise just call here
  430. _set_use_vsync(p_enable);
  431. }
  432. }
  433. bool OS::is_vsync_enabled() const {
  434. return _use_vsync;
  435. }
  436. OS::PowerState OS::get_power_state() {
  437. return POWERSTATE_UNKNOWN;
  438. }
  439. int OS::get_power_seconds_left() {
  440. return -1;
  441. }
  442. int OS::get_power_percent_left() {
  443. return -1;
  444. }
  445. void OS::set_has_server_feature_callback(HasServerFeatureCallback p_callback) {
  446. has_server_feature_callback = p_callback;
  447. }
  448. bool OS::has_feature(const String &p_feature) {
  449. if (p_feature == get_name())
  450. return true;
  451. #ifdef DEBUG_ENABLED
  452. if (p_feature == "debug")
  453. return true;
  454. #else
  455. if (p_feature == "release")
  456. return true;
  457. #endif
  458. #ifdef TOOLS_ENABLED
  459. if (p_feature == "editor")
  460. return true;
  461. #else
  462. if (p_feature == "standalone")
  463. return true;
  464. #endif
  465. if (sizeof(void *) == 8 && p_feature == "64") {
  466. return true;
  467. }
  468. if (sizeof(void *) == 4 && p_feature == "32") {
  469. return true;
  470. }
  471. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__)
  472. if (p_feature == "x86_64") {
  473. return true;
  474. }
  475. #elif (defined(__i386) || defined(__i386__))
  476. if (p_feature == "x86") {
  477. return true;
  478. }
  479. #elif defined(__aarch64__)
  480. if (p_feature == "arm64") {
  481. return true;
  482. }
  483. #elif defined(__arm__)
  484. #if defined(__ARM_ARCH_7A__)
  485. if (p_feature == "armv7a" || p_feature == "armv7") {
  486. return true;
  487. }
  488. #endif
  489. #if defined(__ARM_ARCH_7S__)
  490. if (p_feature == "armv7s" || p_feature == "armv7") {
  491. return true;
  492. }
  493. #endif
  494. if (p_feature == "arm") {
  495. return true;
  496. }
  497. #endif
  498. if (_check_internal_feature_support(p_feature))
  499. return true;
  500. if (has_server_feature_callback && has_server_feature_callback(p_feature)) {
  501. return true;
  502. }
  503. if (ProjectSettings::get_singleton()->has_custom_feature(p_feature))
  504. return true;
  505. return false;
  506. }
  507. void OS::center_window() {
  508. if (is_window_fullscreen()) return;
  509. Point2 sp = get_screen_position(get_current_screen());
  510. Size2 scr = get_screen_size(get_current_screen());
  511. Size2 wnd = get_real_window_size();
  512. int x = sp.width + (scr.width - wnd.width) / 2;
  513. int y = sp.height + (scr.height - wnd.height) / 2;
  514. set_window_position(Vector2(x, y));
  515. }
  516. int OS::get_video_driver_count() const {
  517. return 2;
  518. }
  519. const char *OS::get_video_driver_name(int p_driver) const {
  520. switch (p_driver) {
  521. case VIDEO_DRIVER_GLES2:
  522. return "GLES2";
  523. case VIDEO_DRIVER_GLES3:
  524. default:
  525. return "GLES3";
  526. }
  527. }
  528. int OS::get_audio_driver_count() const {
  529. return AudioDriverManager::get_driver_count();
  530. }
  531. const char *OS::get_audio_driver_name(int p_driver) const {
  532. AudioDriver *driver = AudioDriverManager::get_driver(p_driver);
  533. ERR_FAIL_COND_V(!driver, "");
  534. return AudioDriverManager::get_driver(p_driver)->get_name();
  535. }
  536. void OS::set_restart_on_exit(bool p_restart, const List<String> &p_restart_arguments) {
  537. restart_on_exit = p_restart;
  538. restart_commandline = p_restart_arguments;
  539. }
  540. bool OS::is_restart_on_exit_set() const {
  541. return restart_on_exit;
  542. }
  543. List<String> OS::get_restart_on_exit_arguments() const {
  544. return restart_commandline;
  545. }
  546. PoolStringArray OS::get_connected_midi_inputs() {
  547. if (MIDIDriver::get_singleton())
  548. return MIDIDriver::get_singleton()->get_connected_inputs();
  549. PoolStringArray list;
  550. return list;
  551. }
  552. void OS::open_midi_inputs() {
  553. if (MIDIDriver::get_singleton())
  554. MIDIDriver::get_singleton()->open();
  555. }
  556. void OS::close_midi_inputs() {
  557. if (MIDIDriver::get_singleton())
  558. MIDIDriver::get_singleton()->close();
  559. }
  560. OS::OS() {
  561. void *volatile stack_bottom;
  562. restart_on_exit = false;
  563. last_error = NULL;
  564. singleton = this;
  565. _keep_screen_on = true; // set default value to true, because this had been true before godot 2.0.
  566. low_processor_usage_mode = false;
  567. low_processor_usage_mode_sleep_usec = 10000;
  568. _verbose_stdout = false;
  569. _no_window = false;
  570. _exit_code = 0;
  571. _orientation = SCREEN_LANDSCAPE;
  572. _render_thread_mode = RENDER_THREAD_SAFE;
  573. _allow_hidpi = false;
  574. _allow_layered = false;
  575. _stack_bottom = (void *)(&stack_bottom);
  576. _logger = NULL;
  577. has_server_feature_callback = NULL;
  578. Vector<Logger *> loggers;
  579. loggers.push_back(memnew(StdLogger));
  580. _set_logger(memnew(CompositeLogger(loggers)));
  581. }
  582. OS::~OS() {
  583. if (last_error)
  584. memfree(last_error);
  585. memdelete(_logger);
  586. singleton = NULL;
  587. }