2
0

os.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*************************************************************************/
  2. /* os.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 = nullptr;
  40. uint64_t OS::target_ticks = 0;
  41. OS *OS::get_singleton() {
  42. return singleton;
  43. }
  44. uint32_t OS::get_ticks_msec() const {
  45. return get_ticks_usec() / 1000;
  46. }
  47. String OS::get_iso_date_time(bool local) const {
  48. OS::Date date = get_date(local);
  49. OS::Time time = get_time(local);
  50. String timezone;
  51. if (!local) {
  52. TimeZoneInfo zone = get_time_zone_info();
  53. if (zone.bias >= 0) {
  54. timezone = "+";
  55. }
  56. timezone = timezone + itos(zone.bias / 60).pad_zeros(2) + itos(zone.bias % 60).pad_zeros(2);
  57. } else {
  58. timezone = "Z";
  59. }
  60. return itos(date.year).pad_zeros(2) +
  61. "-" +
  62. itos(date.month).pad_zeros(2) +
  63. "-" +
  64. itos(date.day).pad_zeros(2) +
  65. "T" +
  66. itos(time.hour).pad_zeros(2) +
  67. ":" +
  68. itos(time.min).pad_zeros(2) +
  69. ":" +
  70. itos(time.sec).pad_zeros(2) +
  71. timezone;
  72. }
  73. uint64_t OS::get_splash_tick_msec() const {
  74. return _msec_splash;
  75. }
  76. uint64_t OS::get_unix_time() const {
  77. return 0;
  78. };
  79. uint64_t OS::get_system_time_secs() const {
  80. return 0;
  81. }
  82. uint64_t OS::get_system_time_msecs() const {
  83. return 0;
  84. }
  85. void OS::debug_break(){
  86. // something
  87. };
  88. void OS::_set_logger(CompositeLogger *p_logger) {
  89. if (_logger) {
  90. memdelete(_logger);
  91. }
  92. _logger = p_logger;
  93. }
  94. void OS::add_logger(Logger *p_logger) {
  95. if (!_logger) {
  96. Vector<Logger *> loggers;
  97. loggers.push_back(p_logger);
  98. _logger = memnew(CompositeLogger(loggers));
  99. } else {
  100. _logger->add_logger(p_logger);
  101. }
  102. }
  103. 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) {
  104. _logger->log_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
  105. }
  106. void OS::print(const char *p_format, ...) {
  107. va_list argp;
  108. va_start(argp, p_format);
  109. _logger->logv(p_format, argp, false);
  110. va_end(argp);
  111. };
  112. void OS::printerr(const char *p_format, ...) {
  113. va_list argp;
  114. va_start(argp, p_format);
  115. _logger->logv(p_format, argp, true);
  116. va_end(argp);
  117. };
  118. void OS::set_keep_screen_on(bool p_enabled) {
  119. _keep_screen_on = p_enabled;
  120. }
  121. bool OS::is_keep_screen_on() const {
  122. return _keep_screen_on;
  123. }
  124. void OS::set_low_processor_usage_mode(bool p_enabled) {
  125. low_processor_usage_mode = p_enabled;
  126. }
  127. bool OS::is_in_low_processor_usage_mode() const {
  128. return low_processor_usage_mode;
  129. }
  130. void OS::set_low_processor_usage_mode_sleep_usec(int p_usec) {
  131. low_processor_usage_mode_sleep_usec = p_usec;
  132. }
  133. int OS::get_low_processor_usage_mode_sleep_usec() const {
  134. return low_processor_usage_mode_sleep_usec;
  135. }
  136. void OS::set_clipboard(const String &p_text) {
  137. _local_clipboard = p_text;
  138. }
  139. String OS::get_clipboard() const {
  140. return _local_clipboard;
  141. }
  142. String OS::get_executable_path() const {
  143. return _execpath;
  144. }
  145. int OS::get_process_id() const {
  146. return -1;
  147. };
  148. void OS::vibrate_handheld(int p_duration_ms) {
  149. WARN_PRINT("vibrate_handheld() only works with Android and iOS");
  150. }
  151. bool OS::is_stdout_verbose() const {
  152. return _verbose_stdout;
  153. }
  154. bool OS::is_stdout_debug_enabled() const {
  155. return _debug_stdout;
  156. }
  157. void OS::dump_memory_to_file(const char *p_file) {
  158. //Memory::dump_static_mem_to_file(p_file);
  159. }
  160. static FileAccess *_OSPRF = nullptr;
  161. static void _OS_printres(Object *p_obj) {
  162. Resource *res = Object::cast_to<Resource>(p_obj);
  163. if (!res) {
  164. return;
  165. }
  166. String str = itos(res->get_instance_id()) + String(res->get_class()) + ":" + String(res->get_name()) + " - " + res->get_path();
  167. if (_OSPRF) {
  168. _OSPRF->store_line(str);
  169. } else {
  170. print_line(str);
  171. }
  172. }
  173. bool OS::has_virtual_keyboard() const {
  174. return false;
  175. }
  176. void OS::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect, bool p_multiline, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
  177. }
  178. void OS::hide_virtual_keyboard() {
  179. }
  180. int OS::get_virtual_keyboard_height() const {
  181. return 0;
  182. }
  183. void OS::set_cursor_shape(CursorShape p_shape) {
  184. }
  185. OS::CursorShape OS::get_cursor_shape() const {
  186. return CURSOR_ARROW;
  187. }
  188. void OS::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  189. }
  190. void OS::print_all_resources(String p_to_file) {
  191. ERR_FAIL_COND(p_to_file != "" && _OSPRF);
  192. if (p_to_file != "") {
  193. Error err;
  194. _OSPRF = FileAccess::open(p_to_file, FileAccess::WRITE, &err);
  195. if (err != OK) {
  196. _OSPRF = nullptr;
  197. ERR_FAIL_MSG("Can't print all resources to file: " + String(p_to_file) + ".");
  198. }
  199. }
  200. ObjectDB::debug_objects(_OS_printres);
  201. if (p_to_file != "") {
  202. if (_OSPRF) {
  203. memdelete(_OSPRF);
  204. }
  205. _OSPRF = nullptr;
  206. }
  207. }
  208. void OS::print_resources_in_use(bool p_short) {
  209. ResourceCache::dump(nullptr, p_short);
  210. }
  211. void OS::dump_resources_to_file(const char *p_file) {
  212. ResourceCache::dump(p_file);
  213. }
  214. void OS::set_no_window_mode(bool p_enable) {
  215. _no_window = p_enable;
  216. }
  217. bool OS::is_no_window_mode_enabled() const {
  218. return _no_window;
  219. }
  220. int OS::get_exit_code() const {
  221. return _exit_code;
  222. }
  223. void OS::set_exit_code(int p_code) {
  224. _exit_code = p_code;
  225. _is_custom_exit_code = true;
  226. }
  227. bool OS::is_custom_exit_code() {
  228. return _is_custom_exit_code;
  229. }
  230. String OS::get_locale() const {
  231. return "en";
  232. }
  233. // Helper function to ensure that a dir name/path will be valid on the OS
  234. String OS::get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator) const {
  235. Vector<String> invalid_chars = String(": * ? \" < > |").split(" ");
  236. if (p_allow_dir_separator) {
  237. // Dir separators are allowed, but disallow ".." to avoid going up the filesystem
  238. invalid_chars.push_back("..");
  239. } else {
  240. invalid_chars.push_back("/");
  241. }
  242. String safe_dir_name = p_dir_name.replace("\\", "/").strip_edges();
  243. for (int i = 0; i < invalid_chars.size(); i++) {
  244. safe_dir_name = safe_dir_name.replace(invalid_chars[i], "-");
  245. }
  246. return safe_dir_name;
  247. }
  248. // Path to data, config, cache, etc. OS-specific folders
  249. // Get properly capitalized engine name for system paths
  250. String OS::get_godot_dir_name() const {
  251. // Default to lowercase, so only override when different case is needed
  252. return String(VERSION_SHORT_NAME).to_lower();
  253. }
  254. // OS equivalent of XDG_DATA_HOME
  255. String OS::get_data_path() const {
  256. return ".";
  257. }
  258. // OS equivalent of XDG_CONFIG_HOME
  259. String OS::get_config_path() const {
  260. return ".";
  261. }
  262. // OS equivalent of XDG_CACHE_HOME
  263. String OS::get_cache_path() const {
  264. return ".";
  265. }
  266. // Path to macOS .app bundle resources
  267. String OS::get_bundle_resource_dir() const {
  268. return ".";
  269. };
  270. // OS specific path for user://
  271. String OS::get_user_data_dir() const {
  272. return ".";
  273. };
  274. // Android OS path to app's external data storage
  275. String OS::get_external_data_dir() const {
  276. return get_user_data_dir();
  277. };
  278. // Absolute path to res://
  279. String OS::get_resource_dir() const {
  280. return ProjectSettings::get_singleton()->get_resource_path();
  281. }
  282. // Access system-specific dirs like Documents, Downloads, etc.
  283. String OS::get_system_dir(SystemDir p_dir) const {
  284. return ".";
  285. }
  286. Error OS::shell_open(String p_uri) {
  287. return ERR_UNAVAILABLE;
  288. };
  289. // implement these with the canvas?
  290. Error OS::dialog_show(String p_title, String p_description, Vector<String> p_buttons, Object *p_obj, String p_callback) {
  291. while (true) {
  292. print("%ls\n--------\n%ls\n", p_title.c_str(), p_description.c_str());
  293. for (int i = 0; i < p_buttons.size(); i++) {
  294. if (i > 0) {
  295. print(", ");
  296. }
  297. print("%i=%ls", i + 1, p_buttons[i].c_str());
  298. };
  299. print("\n");
  300. String res = get_stdin_string().strip_edges();
  301. if (!res.is_numeric()) {
  302. continue;
  303. }
  304. int n = res.to_int();
  305. if (n < 0 || n >= p_buttons.size()) {
  306. continue;
  307. }
  308. if (p_obj && p_callback != "") {
  309. p_obj->call_deferred(p_callback, n);
  310. }
  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. // Internal helper function that returns the screen orientation enum value from a string
  355. // (generally coming from the Project Settings).
  356. // This is required to keep compatibility with existing projects.
  357. OS::ScreenOrientation OS::get_screen_orientation_from_string(const String &p_orientation) const {
  358. if (p_orientation == "portrait") {
  359. return OS::SCREEN_PORTRAIT;
  360. } else if (p_orientation == "reverse_landscape") {
  361. return OS::SCREEN_REVERSE_LANDSCAPE;
  362. } else if (p_orientation == "reverse_portrait") {
  363. return OS::SCREEN_REVERSE_PORTRAIT;
  364. } else if (p_orientation == "sensor_landscape") {
  365. return OS::SCREEN_SENSOR_LANDSCAPE;
  366. } else if (p_orientation == "sensor_portrait") {
  367. return OS::SCREEN_SENSOR_PORTRAIT;
  368. } else if (p_orientation == "sensor") {
  369. return OS::SCREEN_SENSOR;
  370. }
  371. return OS::SCREEN_LANDSCAPE;
  372. }
  373. void OS::ensure_user_data_dir() {
  374. String dd = get_user_data_dir();
  375. DirAccess *da = DirAccess::open(dd);
  376. if (da) {
  377. memdelete(da);
  378. return;
  379. }
  380. da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  381. Error err = da->make_dir_recursive(dd);
  382. ERR_FAIL_COND_MSG(err != OK, "Error attempting to create data dir: " + dd + ".");
  383. memdelete(da);
  384. }
  385. void OS::set_native_icon(const String &p_filename) {
  386. }
  387. void OS::set_icon(const Ref<Image> &p_icon) {
  388. }
  389. String OS::get_model_name() const {
  390. return "GenericDevice";
  391. }
  392. void OS::set_cmdline(const char *p_execpath, const List<String> &p_args) {
  393. _execpath = p_execpath;
  394. _cmdline = p_args;
  395. };
  396. void OS::release_rendering_thread() {
  397. }
  398. void OS::make_rendering_thread() {
  399. }
  400. void OS::swap_buffers() {
  401. }
  402. String OS::get_unique_id() const {
  403. ERR_FAIL_V("");
  404. }
  405. int OS::get_processor_count() const {
  406. return 1;
  407. }
  408. Error OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  409. return FAILED;
  410. }
  411. bool OS::native_video_is_playing() const {
  412. return false;
  413. }
  414. void OS::native_video_pause() {
  415. }
  416. void OS::native_video_unpause() {
  417. }
  418. void OS::native_video_stop() {
  419. }
  420. void OS::set_mouse_mode(MouseMode p_mode) {
  421. }
  422. bool OS::can_use_threads() const {
  423. #ifdef NO_THREADS
  424. return false;
  425. #else
  426. return true;
  427. #endif
  428. }
  429. OS::MouseMode OS::get_mouse_mode() const {
  430. return MOUSE_MODE_VISIBLE;
  431. }
  432. OS::LatinKeyboardVariant OS::get_latin_keyboard_variant() const {
  433. return LATIN_KEYBOARD_QWERTY;
  434. }
  435. int OS::keyboard_get_layout_count() const {
  436. return 0;
  437. }
  438. int OS::keyboard_get_current_layout() const {
  439. return -1;
  440. }
  441. void OS::keyboard_set_current_layout(int p_index) {}
  442. String OS::keyboard_get_layout_language(int p_index) const {
  443. return "";
  444. }
  445. String OS::keyboard_get_layout_name(int p_index) const {
  446. return "";
  447. }
  448. bool OS::is_joy_known(int p_device) {
  449. return true;
  450. }
  451. String OS::get_joy_guid(int p_device) const {
  452. return "Default Joypad";
  453. }
  454. void OS::set_context(int p_context) {
  455. }
  456. OS::SwitchVSyncCallbackInThread OS::switch_vsync_function = nullptr;
  457. void OS::set_use_vsync(bool p_enable) {
  458. _use_vsync = p_enable;
  459. if (switch_vsync_function) { //if a function was set, use function
  460. switch_vsync_function(p_enable);
  461. } else { //otherwise just call here
  462. _set_use_vsync(p_enable);
  463. }
  464. }
  465. bool OS::is_vsync_enabled() const {
  466. return _use_vsync;
  467. }
  468. void OS::set_vsync_via_compositor(bool p_enable) {
  469. _vsync_via_compositor = p_enable;
  470. }
  471. bool OS::is_vsync_via_compositor_enabled() const {
  472. return _vsync_via_compositor;
  473. }
  474. void OS::set_delta_smoothing(bool p_enabled) {
  475. _delta_smoothing_enabled = p_enabled;
  476. }
  477. bool OS::is_delta_smoothing_enabled() const {
  478. return _delta_smoothing_enabled;
  479. }
  480. OS::PowerState OS::get_power_state() {
  481. return POWERSTATE_UNKNOWN;
  482. }
  483. int OS::get_power_seconds_left() {
  484. return -1;
  485. }
  486. int OS::get_power_percent_left() {
  487. return -1;
  488. }
  489. void OS::set_has_server_feature_callback(HasServerFeatureCallback p_callback) {
  490. has_server_feature_callback = p_callback;
  491. }
  492. bool OS::has_feature(const String &p_feature) {
  493. if (p_feature == get_name()) {
  494. return true;
  495. }
  496. #ifdef DEBUG_ENABLED
  497. if (p_feature == "debug") {
  498. return true;
  499. }
  500. #else
  501. if (p_feature == "release") {
  502. return true;
  503. }
  504. #endif
  505. #ifdef TOOLS_ENABLED
  506. if (p_feature == "editor") {
  507. return true;
  508. }
  509. #else
  510. if (p_feature == "standalone") {
  511. return true;
  512. }
  513. #endif
  514. if (sizeof(void *) == 8 && p_feature == "64") {
  515. return true;
  516. }
  517. if (sizeof(void *) == 4 && p_feature == "32") {
  518. return true;
  519. }
  520. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__)
  521. if (p_feature == "x86_64") {
  522. return true;
  523. }
  524. #elif (defined(__i386) || defined(__i386__))
  525. if (p_feature == "x86") {
  526. return true;
  527. }
  528. #elif defined(__aarch64__)
  529. if (p_feature == "arm64") {
  530. return true;
  531. }
  532. #elif defined(__arm__)
  533. #if defined(__ARM_ARCH_7A__)
  534. if (p_feature == "armv7a" || p_feature == "armv7") {
  535. return true;
  536. }
  537. #endif
  538. #if defined(__ARM_ARCH_7S__)
  539. if (p_feature == "armv7s" || p_feature == "armv7") {
  540. return true;
  541. }
  542. #endif
  543. if (p_feature == "arm") {
  544. return true;
  545. }
  546. #endif
  547. if (_check_internal_feature_support(p_feature)) {
  548. return true;
  549. }
  550. if (has_server_feature_callback && has_server_feature_callback(p_feature)) {
  551. return true;
  552. }
  553. if (ProjectSettings::get_singleton()->has_custom_feature(p_feature)) {
  554. return true;
  555. }
  556. return false;
  557. }
  558. void OS::center_window() {
  559. if (is_window_fullscreen()) {
  560. return;
  561. }
  562. Point2 sp = get_screen_position(get_current_screen());
  563. Size2 scr = get_screen_size(get_current_screen());
  564. Size2 wnd = get_real_window_size();
  565. int x = sp.width + (scr.width - wnd.width) / 2;
  566. int y = sp.height + (scr.height - wnd.height) / 2;
  567. set_window_position(Vector2(x, y));
  568. }
  569. int OS::get_video_driver_count() const {
  570. return 2;
  571. }
  572. const char *OS::get_video_driver_name(int p_driver) const {
  573. switch (p_driver) {
  574. case VIDEO_DRIVER_GLES2:
  575. return "GLES2";
  576. case VIDEO_DRIVER_GLES3:
  577. default:
  578. return "GLES3";
  579. }
  580. }
  581. int OS::get_audio_driver_count() const {
  582. return AudioDriverManager::get_driver_count();
  583. }
  584. const char *OS::get_audio_driver_name(int p_driver) const {
  585. AudioDriver *driver = AudioDriverManager::get_driver(p_driver);
  586. ERR_FAIL_COND_V_MSG(!driver, "", "Cannot get audio driver at index '" + itos(p_driver) + "'.");
  587. return AudioDriverManager::get_driver(p_driver)->get_name();
  588. }
  589. void OS::set_restart_on_exit(bool p_restart, const List<String> &p_restart_arguments) {
  590. restart_on_exit = p_restart;
  591. restart_commandline = p_restart_arguments;
  592. }
  593. bool OS::is_restart_on_exit_set() const {
  594. return restart_on_exit;
  595. }
  596. List<String> OS::get_restart_on_exit_arguments() const {
  597. return restart_commandline;
  598. }
  599. PoolStringArray OS::get_connected_midi_inputs() {
  600. if (MIDIDriver::get_singleton()) {
  601. return MIDIDriver::get_singleton()->get_connected_inputs();
  602. }
  603. PoolStringArray list;
  604. ERR_FAIL_V_MSG(list, vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  605. }
  606. void OS::open_midi_inputs() {
  607. if (MIDIDriver::get_singleton()) {
  608. MIDIDriver::get_singleton()->open();
  609. } else {
  610. ERR_PRINT(vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  611. }
  612. }
  613. void OS::close_midi_inputs() {
  614. if (MIDIDriver::get_singleton()) {
  615. MIDIDriver::get_singleton()->close();
  616. } else {
  617. ERR_PRINT(vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  618. }
  619. }
  620. void OS::add_frame_delay(bool p_can_draw) {
  621. const uint32_t frame_delay = Engine::get_singleton()->get_frame_delay();
  622. if (frame_delay) {
  623. // Add fixed frame delay to decrease CPU/GPU usage. This doesn't take
  624. // the actual frame time into account.
  625. // Due to the high fluctuation of the actual sleep duration, it's not recommended
  626. // to use this as a FPS limiter.
  627. delay_usec(frame_delay * 1000);
  628. }
  629. // Add a dynamic frame delay to decrease CPU/GPU usage. This takes the
  630. // previous frame time into account for a smoother result.
  631. uint64_t dynamic_delay = 0;
  632. if (is_in_low_processor_usage_mode() || !p_can_draw) {
  633. dynamic_delay = get_low_processor_usage_mode_sleep_usec();
  634. }
  635. const int target_fps = Engine::get_singleton()->get_target_fps();
  636. if (target_fps > 0 && !Engine::get_singleton()->is_editor_hint()) {
  637. // Override the low processor usage mode sleep delay if the target FPS is lower.
  638. dynamic_delay = MAX(dynamic_delay, (uint64_t)(1000000 / target_fps));
  639. }
  640. if (dynamic_delay > 0) {
  641. target_ticks += dynamic_delay;
  642. uint64_t current_ticks = get_ticks_usec();
  643. if (current_ticks < target_ticks) {
  644. delay_usec(target_ticks - current_ticks);
  645. }
  646. current_ticks = get_ticks_usec();
  647. target_ticks = MIN(MAX(target_ticks, current_ticks - dynamic_delay), current_ticks + dynamic_delay);
  648. }
  649. }
  650. OS::OS() {
  651. void *volatile stack_bottom;
  652. restart_on_exit = false;
  653. singleton = this;
  654. _keep_screen_on = true; // set default value to true, because this had been true before godot 2.0.
  655. low_processor_usage_mode = false;
  656. low_processor_usage_mode_sleep_usec = 10000;
  657. _verbose_stdout = false;
  658. _debug_stdout = false;
  659. _no_window = false;
  660. _exit_code = 0;
  661. _orientation = SCREEN_LANDSCAPE;
  662. _delta_smoothing_enabled = false;
  663. _render_thread_mode = RENDER_THREAD_SAFE;
  664. _allow_hidpi = false;
  665. _allow_layered = false;
  666. _stack_bottom = (void *)(&stack_bottom);
  667. _logger = nullptr;
  668. has_server_feature_callback = nullptr;
  669. Vector<Logger *> loggers;
  670. loggers.push_back(memnew(StdLogger));
  671. _set_logger(memnew(CompositeLogger(loggers)));
  672. }
  673. OS::~OS() {
  674. memdelete(_logger);
  675. singleton = nullptr;
  676. }