os.cpp 16 KB

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