os.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*************************************************************************/
  2. /* os.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "dir_access.h"
  32. #include "input.h"
  33. #include "os/file_access.h"
  34. #include "project_settings.h"
  35. #include "servers/audio_server.h"
  36. #include "version_generated.gen.h"
  37. #include <stdarg.h>
  38. OS *OS::singleton = NULL;
  39. OS *OS::get_singleton() {
  40. return singleton;
  41. }
  42. uint32_t OS::get_ticks_msec() const {
  43. return get_ticks_usec() / 1000;
  44. }
  45. uint64_t OS::get_splash_tick_msec() const {
  46. return _msec_splash;
  47. }
  48. uint64_t OS::get_unix_time() const {
  49. return 0;
  50. };
  51. uint64_t OS::get_system_time_secs() const {
  52. return 0;
  53. }
  54. void OS::debug_break(){
  55. // something
  56. };
  57. void OS::_set_logger(CompositeLogger *p_logger) {
  58. if (_logger) {
  59. memdelete(_logger);
  60. }
  61. _logger = p_logger;
  62. }
  63. void OS::add_logger(Logger *p_logger) {
  64. if (!_logger) {
  65. Vector<Logger *> loggers;
  66. loggers.push_back(p_logger);
  67. _logger = memnew(CompositeLogger(loggers));
  68. } else {
  69. _logger->add_logger(p_logger);
  70. }
  71. }
  72. 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) {
  73. _logger->log_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
  74. }
  75. void OS::print(const char *p_format, ...) {
  76. va_list argp;
  77. va_start(argp, p_format);
  78. _logger->logv(p_format, argp, false);
  79. va_end(argp);
  80. };
  81. void OS::printerr(const char *p_format, ...) {
  82. va_list argp;
  83. va_start(argp, p_format);
  84. _logger->logv(p_format, argp, true);
  85. va_end(argp);
  86. };
  87. void OS::set_keep_screen_on(bool p_enabled) {
  88. _keep_screen_on = p_enabled;
  89. }
  90. bool OS::is_keep_screen_on() const {
  91. return _keep_screen_on;
  92. }
  93. void OS::set_low_processor_usage_mode(bool p_enabled) {
  94. low_processor_usage_mode = p_enabled;
  95. }
  96. bool OS::is_in_low_processor_usage_mode() const {
  97. return low_processor_usage_mode;
  98. }
  99. void OS::set_low_processor_usage_mode_sleep_usec(int p_usec) {
  100. low_processor_usage_mode_sleep_usec = p_usec;
  101. }
  102. int OS::get_low_processor_usage_mode_sleep_usec() const {
  103. return low_processor_usage_mode_sleep_usec;
  104. }
  105. void OS::set_clipboard(const String &p_text) {
  106. _local_clipboard = p_text;
  107. }
  108. String OS::get_clipboard() const {
  109. return _local_clipboard;
  110. }
  111. String OS::get_executable_path() const {
  112. return _execpath;
  113. }
  114. int OS::get_process_id() const {
  115. return -1;
  116. };
  117. bool OS::is_stdout_verbose() const {
  118. return _verbose_stdout;
  119. }
  120. void OS::set_last_error(const char *p_error) {
  121. GLOBAL_LOCK_FUNCTION
  122. if (p_error == NULL)
  123. p_error = "Unknown Error";
  124. if (last_error)
  125. memfree(last_error);
  126. last_error = NULL;
  127. int len = 0;
  128. while (p_error[len++])
  129. ;
  130. last_error = (char *)memalloc(len);
  131. for (int i = 0; i < len; i++)
  132. last_error[i] = p_error[i];
  133. }
  134. const char *OS::get_last_error() const {
  135. GLOBAL_LOCK_FUNCTION
  136. return last_error ? last_error : "";
  137. }
  138. void OS::dump_memory_to_file(const char *p_file) {
  139. //Memory::dump_static_mem_to_file(p_file);
  140. }
  141. static FileAccess *_OSPRF = NULL;
  142. static void _OS_printres(Object *p_obj) {
  143. Resource *res = Object::cast_to<Resource>(p_obj);
  144. if (!res)
  145. return;
  146. String str = itos(res->get_instance_id()) + String(res->get_class()) + ":" + String(res->get_name()) + " - " + res->get_path();
  147. if (_OSPRF)
  148. _OSPRF->store_line(str);
  149. else
  150. print_line(str);
  151. }
  152. bool OS::has_virtual_keyboard() const {
  153. return false;
  154. }
  155. void OS::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) {
  156. }
  157. void OS::hide_virtual_keyboard() {
  158. }
  159. int OS::get_virtual_keyboard_height() const {
  160. return 0;
  161. }
  162. void OS::print_all_resources(String p_to_file) {
  163. ERR_FAIL_COND(p_to_file != "" && _OSPRF);
  164. if (p_to_file != "") {
  165. Error err;
  166. _OSPRF = FileAccess::open(p_to_file, FileAccess::WRITE, &err);
  167. if (err != OK) {
  168. _OSPRF = NULL;
  169. ERR_FAIL_COND(err != OK);
  170. }
  171. }
  172. ObjectDB::debug_objects(_OS_printres);
  173. if (p_to_file != "") {
  174. if (_OSPRF)
  175. memdelete(_OSPRF);
  176. _OSPRF = NULL;
  177. }
  178. }
  179. void OS::print_resources_in_use(bool p_short) {
  180. ResourceCache::dump(NULL, p_short);
  181. }
  182. void OS::dump_resources_to_file(const char *p_file) {
  183. ResourceCache::dump(p_file);
  184. }
  185. void OS::clear_last_error() {
  186. GLOBAL_LOCK_FUNCTION
  187. if (last_error)
  188. memfree(last_error);
  189. last_error = NULL;
  190. }
  191. void OS::set_no_window_mode(bool p_enable) {
  192. _no_window = p_enable;
  193. }
  194. bool OS::is_no_window_mode_enabled() const {
  195. return _no_window;
  196. }
  197. int OS::get_exit_code() const {
  198. return _exit_code;
  199. }
  200. void OS::set_exit_code(int p_code) {
  201. _exit_code = p_code;
  202. }
  203. String OS::get_locale() const {
  204. return "en";
  205. }
  206. // Helper function to ensure that a dir name/path will be valid on the OS
  207. String OS::get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator) const {
  208. Vector<String> invalid_chars = String(": * ? \" < > |").split(" ");
  209. if (p_allow_dir_separator) {
  210. // Dir separators are allowed, but disallow ".." to avoid going up the filesystem
  211. invalid_chars.push_back("..");
  212. } else {
  213. invalid_chars.push_back("/");
  214. }
  215. String safe_dir_name = p_dir_name.replace("\\", "/").strip_edges();
  216. for (int i = 0; i < invalid_chars.size(); i++) {
  217. safe_dir_name = safe_dir_name.replace(invalid_chars[i], "-");
  218. }
  219. return safe_dir_name;
  220. }
  221. // Path to data, config, cache, etc. OS-specific folders
  222. // Get properly capitalized engine name for system paths
  223. String OS::get_godot_dir_name() const {
  224. // Default to lowercase, so only override when different case is needed
  225. return String(VERSION_SHORT_NAME).to_lower();
  226. }
  227. // OS equivalent of XDG_DATA_HOME
  228. String OS::get_data_path() const {
  229. return ".";
  230. }
  231. // OS equivalent of XDG_CONFIG_HOME
  232. String OS::get_config_path() const {
  233. return ".";
  234. }
  235. // OS equivalent of XDG_CACHE_HOME
  236. String OS::get_cache_path() const {
  237. return ".";
  238. }
  239. // OS specific path for user://
  240. String OS::get_user_data_dir() const {
  241. return ".";
  242. };
  243. // Absolute path to res://
  244. String OS::get_resource_dir() const {
  245. return ProjectSettings::get_singleton()->get_resource_path();
  246. }
  247. // Access system-specific dirs like Documents, Downloads, etc.
  248. String OS::get_system_dir(SystemDir p_dir) const {
  249. return ".";
  250. }
  251. Error OS::shell_open(String p_uri) {
  252. return ERR_UNAVAILABLE;
  253. };
  254. // implement these with the canvas?
  255. Error OS::dialog_show(String p_title, String p_description, Vector<String> p_buttons, Object *p_obj, String p_callback) {
  256. while (true) {
  257. print("%ls\n--------\n%ls\n", p_title.c_str(), p_description.c_str());
  258. for (int i = 0; i < p_buttons.size(); i++) {
  259. if (i > 0) print(", ");
  260. print("%i=%ls", i + 1, p_buttons[i].c_str());
  261. };
  262. print("\n");
  263. String res = get_stdin_string().strip_edges();
  264. if (!res.is_numeric())
  265. continue;
  266. int n = res.to_int();
  267. if (n < 0 || n >= p_buttons.size())
  268. continue;
  269. if (p_obj && p_callback != "")
  270. p_obj->call_deferred(p_callback, n);
  271. break;
  272. };
  273. return OK;
  274. };
  275. Error OS::dialog_input_text(String p_title, String p_description, String p_partial, Object *p_obj, String p_callback) {
  276. ERR_FAIL_COND_V(!p_obj, FAILED);
  277. ERR_FAIL_COND_V(p_callback == "", FAILED);
  278. print("%ls\n---------\n%ls\n[%ls]:\n", p_title.c_str(), p_description.c_str(), p_partial.c_str());
  279. String res = get_stdin_string().strip_edges();
  280. bool success = true;
  281. if (res == "") {
  282. res = p_partial;
  283. };
  284. p_obj->call_deferred(p_callback, success, res);
  285. return OK;
  286. };
  287. int OS::get_static_memory_usage() const {
  288. return Memory::get_mem_usage();
  289. }
  290. int OS::get_dynamic_memory_usage() const {
  291. return MemoryPool::total_memory;
  292. }
  293. int OS::get_static_memory_peak_usage() const {
  294. return Memory::get_mem_max_usage();
  295. }
  296. Error OS::set_cwd(const String &p_cwd) {
  297. return ERR_CANT_OPEN;
  298. }
  299. bool OS::has_touchscreen_ui_hint() const {
  300. //return false;
  301. return Input::get_singleton() && Input::get_singleton()->is_emulating_touch_from_mouse();
  302. }
  303. int OS::get_free_static_memory() const {
  304. return Memory::get_mem_available();
  305. }
  306. void OS::yield() {
  307. }
  308. void OS::set_screen_orientation(ScreenOrientation p_orientation) {
  309. _orientation = p_orientation;
  310. }
  311. OS::ScreenOrientation OS::get_screen_orientation() const {
  312. return (OS::ScreenOrientation)_orientation;
  313. }
  314. void OS::_ensure_user_data_dir() {
  315. String dd = get_user_data_dir();
  316. DirAccess *da = DirAccess::open(dd);
  317. if (da) {
  318. memdelete(da);
  319. return;
  320. }
  321. da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  322. Error err = da->make_dir_recursive(dd);
  323. if (err != OK) {
  324. ERR_EXPLAIN("Error attempting to create data dir: " + dd);
  325. }
  326. ERR_FAIL_COND(err != OK);
  327. memdelete(da);
  328. }
  329. void OS::set_icon(const Ref<Image> &p_icon) {
  330. }
  331. String OS::get_model_name() const {
  332. return "GenericDevice";
  333. }
  334. void OS::set_cmdline(const char *p_execpath, const List<String> &p_args) {
  335. _execpath = p_execpath;
  336. _cmdline = p_args;
  337. };
  338. void OS::release_rendering_thread() {
  339. }
  340. void OS::make_rendering_thread() {
  341. }
  342. void OS::swap_buffers() {
  343. }
  344. String OS::get_unique_id() const {
  345. ERR_FAIL_V("");
  346. }
  347. int OS::get_processor_count() const {
  348. return 1;
  349. }
  350. Error OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  351. return FAILED;
  352. };
  353. bool OS::native_video_is_playing() const {
  354. return false;
  355. };
  356. void OS::native_video_pause(){
  357. };
  358. void OS::native_video_unpause(){
  359. };
  360. void OS::native_video_stop(){
  361. };
  362. void OS::set_mouse_mode(MouseMode p_mode) {
  363. }
  364. bool OS::can_use_threads() const {
  365. #ifdef NO_THREADS
  366. return false;
  367. #else
  368. return true;
  369. #endif
  370. }
  371. OS::MouseMode OS::get_mouse_mode() const {
  372. return MOUSE_MODE_VISIBLE;
  373. }
  374. OS::LatinKeyboardVariant OS::get_latin_keyboard_variant() const {
  375. return LATIN_KEYBOARD_QWERTY;
  376. }
  377. bool OS::is_joy_known(int p_device) {
  378. return true;
  379. }
  380. String OS::get_joy_guid(int p_device) const {
  381. return "Default Joypad";
  382. }
  383. void OS::set_context(int p_context) {
  384. }
  385. OS::SwitchVSyncCallbackInThread OS::switch_vsync_function = NULL;
  386. void OS::set_use_vsync(bool p_enable) {
  387. _use_vsync = p_enable;
  388. if (switch_vsync_function) { //if a function was set, use function
  389. switch_vsync_function(p_enable);
  390. } else { //otherwise just call here
  391. _set_use_vsync(p_enable);
  392. }
  393. }
  394. bool OS::is_vsync_enabled() const {
  395. return _use_vsync;
  396. }
  397. OS::PowerState OS::get_power_state() {
  398. return POWERSTATE_UNKNOWN;
  399. }
  400. int OS::get_power_seconds_left() {
  401. return -1;
  402. }
  403. int OS::get_power_percent_left() {
  404. return -1;
  405. }
  406. bool OS::has_feature(const String &p_feature) {
  407. if (p_feature == get_name())
  408. return true;
  409. #ifdef DEBUG_ENABLED
  410. if (p_feature == "debug")
  411. return true;
  412. #else
  413. if (p_feature == "release")
  414. return true;
  415. #endif
  416. if (sizeof(void *) == 8 && p_feature == "64") {
  417. return true;
  418. }
  419. if (sizeof(void *) == 4 && p_feature == "32") {
  420. return true;
  421. }
  422. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__)
  423. if (p_feature == "x86_64") {
  424. return true;
  425. }
  426. #elif (defined(__i386) || defined(__i386__))
  427. if (p_feature == "x86") {
  428. return true;
  429. }
  430. #elif defined(__aarch64__)
  431. if (p_feature == "arm64") {
  432. return true;
  433. }
  434. #elif defined(__arm__)
  435. #if defined(__ARM_ARCH_7A__)
  436. if (p_feature == "armv7a" || p_feature == "armv7") {
  437. return true;
  438. }
  439. #endif
  440. #if defined(__ARM_ARCH_7S__)
  441. if (p_feature == "armv7s" || p_feature == "armv7") {
  442. return true;
  443. }
  444. #endif
  445. if (p_feature == "arm") {
  446. return true;
  447. }
  448. #endif
  449. if (_check_internal_feature_support(p_feature))
  450. return true;
  451. if (ProjectSettings::get_singleton()->has_custom_feature(p_feature))
  452. return true;
  453. return false;
  454. }
  455. void OS::center_window() {
  456. if (is_window_fullscreen()) return;
  457. Size2 scr = get_screen_size(get_current_screen());
  458. Size2 wnd = get_real_window_size();
  459. int x = scr.width / 2 - wnd.width / 2;
  460. int y = scr.height / 2 - wnd.height / 2;
  461. set_window_position(Vector2(x, y));
  462. }
  463. int OS::get_video_driver_count() const {
  464. return 2;
  465. }
  466. const char *OS::get_video_driver_name(int p_driver) const {
  467. switch (p_driver) {
  468. case VIDEO_DRIVER_GLES2:
  469. return "GLES2";
  470. case VIDEO_DRIVER_GLES3:
  471. default:
  472. return "GLES3";
  473. }
  474. }
  475. int OS::get_audio_driver_count() const {
  476. return AudioDriverManager::get_driver_count();
  477. }
  478. const char *OS::get_audio_driver_name(int p_driver) const {
  479. AudioDriver *driver = AudioDriverManager::get_driver(p_driver);
  480. ERR_FAIL_COND_V(!driver, "");
  481. return AudioDriverManager::get_driver(p_driver)->get_name();
  482. }
  483. void OS::set_restart_on_exit(bool p_restart, const List<String> &p_restart_arguments) {
  484. restart_on_exit = p_restart;
  485. restart_commandline = p_restart_arguments;
  486. }
  487. bool OS::is_restart_on_exit_set() const {
  488. return restart_on_exit;
  489. }
  490. List<String> OS::get_restart_on_exit_argumens() const {
  491. return restart_commandline;
  492. }
  493. OS::OS() {
  494. void *volatile stack_bottom;
  495. restart_on_exit = false;
  496. last_error = NULL;
  497. singleton = this;
  498. _keep_screen_on = true; // set default value to true, because this had been true before godot 2.0.
  499. low_processor_usage_mode = false;
  500. low_processor_usage_mode_sleep_usec = 10000;
  501. _verbose_stdout = false;
  502. _no_window = false;
  503. _exit_code = 0;
  504. _orientation = SCREEN_LANDSCAPE;
  505. _render_thread_mode = RENDER_THREAD_SAFE;
  506. _allow_hidpi = false;
  507. _allow_layered = false;
  508. _stack_bottom = (void *)(&stack_bottom);
  509. _logger = NULL;
  510. Vector<Logger *> loggers;
  511. loggers.push_back(memnew(StdLogger));
  512. _set_logger(memnew(CompositeLogger(loggers)));
  513. }
  514. OS::~OS() {
  515. memdelete(_logger);
  516. singleton = NULL;
  517. }