os.cpp 15 KB

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