os.cpp 21 KB

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