os.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*************************************************************************/
  2. /* os.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "os.h"
  31. #include "dir_access.h"
  32. #include "input.h"
  33. #include "os/file_access.h"
  34. #include "project_settings.h"
  35. #include "version_generated.gen.h"
  36. #include <stdarg.h>
  37. OS *OS::singleton = NULL;
  38. OS *OS::get_singleton() {
  39. return singleton;
  40. }
  41. uint32_t OS::get_ticks_msec() const {
  42. return get_ticks_usec() / 1000;
  43. }
  44. uint64_t OS::get_splash_tick_msec() const {
  45. return _msec_splash;
  46. }
  47. uint64_t OS::get_unix_time() const {
  48. return 0;
  49. };
  50. uint64_t OS::get_system_time_secs() const {
  51. return 0;
  52. }
  53. void OS::debug_break(){
  54. // something
  55. };
  56. void OS::_set_logger(CompositeLogger *p_logger) {
  57. if (_logger) {
  58. memdelete(_logger);
  59. }
  60. _logger = p_logger;
  61. }
  62. void OS::add_logger(Logger *p_logger) {
  63. if (!_logger) {
  64. Vector<Logger *> loggers;
  65. loggers.push_back(p_logger);
  66. _logger = memnew(CompositeLogger(loggers));
  67. } else {
  68. _logger->add_logger(p_logger);
  69. }
  70. }
  71. 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) {
  72. _logger->log_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
  73. }
  74. void OS::print(const char *p_format, ...) {
  75. va_list argp;
  76. va_start(argp, p_format);
  77. _logger->logv(p_format, argp, false);
  78. va_end(argp);
  79. };
  80. void OS::printerr(const char *p_format, ...) {
  81. va_list argp;
  82. va_start(argp, p_format);
  83. _logger->logv(p_format, argp, true);
  84. va_end(argp);
  85. };
  86. void OS::set_keep_screen_on(bool p_enabled) {
  87. _keep_screen_on = p_enabled;
  88. }
  89. bool OS::is_keep_screen_on() const {
  90. return _keep_screen_on;
  91. }
  92. void OS::set_low_processor_usage_mode(bool p_enabled) {
  93. low_processor_usage_mode = p_enabled;
  94. }
  95. bool OS::is_in_low_processor_usage_mode() const {
  96. return low_processor_usage_mode;
  97. }
  98. void OS::set_low_processor_usage_mode_sleep_usec(int p_usec) {
  99. low_processor_usage_mode_sleep_usec = p_usec;
  100. }
  101. int OS::get_low_processor_usage_mode_sleep_usec() const {
  102. return low_processor_usage_mode_sleep_usec;
  103. }
  104. void OS::set_clipboard(const String &p_text) {
  105. _local_clipboard = p_text;
  106. }
  107. String OS::get_clipboard() const {
  108. return _local_clipboard;
  109. }
  110. String OS::get_executable_path() const {
  111. return _execpath;
  112. }
  113. int OS::get_process_id() const {
  114. return -1;
  115. };
  116. bool OS::is_stdout_verbose() const {
  117. return _verbose_stdout;
  118. }
  119. void OS::set_last_error(const char *p_error) {
  120. GLOBAL_LOCK_FUNCTION
  121. if (p_error == NULL)
  122. p_error = "Unknown Error";
  123. if (last_error)
  124. memfree(last_error);
  125. last_error = NULL;
  126. int len = 0;
  127. while (p_error[len++])
  128. ;
  129. last_error = (char *)memalloc(len);
  130. for (int i = 0; i < len; i++)
  131. last_error[i] = p_error[i];
  132. }
  133. const char *OS::get_last_error() const {
  134. GLOBAL_LOCK_FUNCTION
  135. return last_error ? last_error : "";
  136. }
  137. void OS::dump_memory_to_file(const char *p_file) {
  138. //Memory::dump_static_mem_to_file(p_file);
  139. }
  140. static FileAccess *_OSPRF = NULL;
  141. static void _OS_printres(Object *p_obj) {
  142. Resource *res = Object::cast_to<Resource>(p_obj);
  143. if (!res)
  144. return;
  145. String str = itos(res->get_instance_id()) + String(res->get_class()) + ":" + String(res->get_name()) + " - " + res->get_path();
  146. if (_OSPRF)
  147. _OSPRF->store_line(str);
  148. else
  149. print_line(str);
  150. }
  151. bool OS::has_virtual_keyboard() const {
  152. return false;
  153. }
  154. void OS::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) {
  155. }
  156. void OS::hide_virtual_keyboard() {
  157. }
  158. int OS::get_virtual_keyboard_height() const {
  159. return 0;
  160. }
  161. void OS::print_all_resources(String p_to_file) {
  162. ERR_FAIL_COND(p_to_file != "" && _OSPRF);
  163. if (p_to_file != "") {
  164. Error err;
  165. _OSPRF = FileAccess::open(p_to_file, FileAccess::WRITE, &err);
  166. if (err != OK) {
  167. _OSPRF = NULL;
  168. ERR_FAIL_COND(err != OK);
  169. }
  170. }
  171. ObjectDB::debug_objects(_OS_printres);
  172. if (p_to_file != "") {
  173. if (_OSPRF)
  174. memdelete(_OSPRF);
  175. _OSPRF = NULL;
  176. }
  177. }
  178. void OS::print_resources_in_use(bool p_short) {
  179. ResourceCache::dump(NULL, p_short);
  180. }
  181. void OS::dump_resources_to_file(const char *p_file) {
  182. ResourceCache::dump(p_file);
  183. }
  184. void OS::clear_last_error() {
  185. GLOBAL_LOCK_FUNCTION
  186. if (last_error)
  187. memfree(last_error);
  188. last_error = NULL;
  189. }
  190. void OS::set_no_window_mode(bool p_enable) {
  191. _no_window = p_enable;
  192. }
  193. bool OS::is_no_window_mode_enabled() const {
  194. return _no_window;
  195. }
  196. int OS::get_exit_code() const {
  197. return _exit_code;
  198. }
  199. void OS::set_exit_code(int p_code) {
  200. _exit_code = p_code;
  201. }
  202. String OS::get_locale() const {
  203. return "en";
  204. }
  205. // Helper function used by OS_Unix and OS_Windows
  206. String OS::get_safe_application_name() const {
  207. String an = ProjectSettings::get_singleton()->get("application/config/name");
  208. Vector<String> invalid_char = String("\\ / : * ? \" < > |").split(" ");
  209. for (int i = 0; i < invalid_char.size(); i++) {
  210. an = an.replace(invalid_char[i], "-");
  211. }
  212. return an;
  213. }
  214. // Path to data, config, cache, etc. OS-specific folders
  215. // Get properly capitalized engine name for system paths
  216. String OS::get_godot_dir_name() const {
  217. // Default to lowercase, so only override when different case is needed
  218. return String(VERSION_SHORT_NAME).to_lower();
  219. }
  220. // OS equivalent of XDG_DATA_HOME
  221. String OS::get_data_path() const {
  222. return ".";
  223. }
  224. // OS equivalent of XDG_CONFIG_HOME
  225. String OS::get_config_path() const {
  226. return ".";
  227. }
  228. // OS equivalent of XDG_CACHE_HOME
  229. String OS::get_cache_path() const {
  230. return ".";
  231. }
  232. // OS specific path for user://
  233. String OS::get_user_data_dir() const {
  234. return ".";
  235. };
  236. // Absolute path to res://
  237. String OS::get_resource_dir() const {
  238. return ProjectSettings::get_singleton()->get_resource_path();
  239. }
  240. // Access system-specific dirs like Documents, Downloads, etc.
  241. String OS::get_system_dir(SystemDir p_dir) const {
  242. return ".";
  243. }
  244. Error OS::shell_open(String p_uri) {
  245. return ERR_UNAVAILABLE;
  246. };
  247. // implement these with the canvas?
  248. Error OS::dialog_show(String p_title, String p_description, Vector<String> p_buttons, Object *p_obj, String p_callback) {
  249. while (true) {
  250. print("%ls\n--------\n%ls\n", p_title.c_str(), p_description.c_str());
  251. for (int i = 0; i < p_buttons.size(); i++) {
  252. if (i > 0) print(", ");
  253. print("%i=%ls", i + 1, p_buttons[i].c_str());
  254. };
  255. print("\n");
  256. String res = get_stdin_string().strip_edges();
  257. if (!res.is_numeric())
  258. continue;
  259. int n = res.to_int();
  260. if (n < 0 || n >= p_buttons.size())
  261. continue;
  262. if (p_obj && p_callback != "")
  263. p_obj->call_deferred(p_callback, n);
  264. break;
  265. };
  266. return OK;
  267. };
  268. Error OS::dialog_input_text(String p_title, String p_description, String p_partial, Object *p_obj, String p_callback) {
  269. ERR_FAIL_COND_V(!p_obj, FAILED);
  270. ERR_FAIL_COND_V(p_callback == "", FAILED);
  271. print("%ls\n---------\n%ls\n[%ls]:\n", p_title.c_str(), p_description.c_str(), p_partial.c_str());
  272. String res = get_stdin_string().strip_edges();
  273. bool success = true;
  274. if (res == "") {
  275. res = p_partial;
  276. };
  277. p_obj->call_deferred(p_callback, success, res);
  278. return OK;
  279. };
  280. int OS::get_static_memory_usage() const {
  281. return Memory::get_mem_usage();
  282. }
  283. int OS::get_dynamic_memory_usage() const {
  284. return MemoryPool::total_memory;
  285. }
  286. int OS::get_static_memory_peak_usage() const {
  287. return Memory::get_mem_max_usage();
  288. }
  289. Error OS::set_cwd(const String &p_cwd) {
  290. return ERR_CANT_OPEN;
  291. }
  292. bool OS::has_touchscreen_ui_hint() const {
  293. //return false;
  294. return Input::get_singleton() && Input::get_singleton()->is_emulating_touchscreen();
  295. }
  296. int OS::get_free_static_memory() const {
  297. return Memory::get_mem_available();
  298. }
  299. void OS::yield() {
  300. }
  301. void OS::set_screen_orientation(ScreenOrientation p_orientation) {
  302. _orientation = p_orientation;
  303. }
  304. OS::ScreenOrientation OS::get_screen_orientation() const {
  305. return (OS::ScreenOrientation)_orientation;
  306. }
  307. void OS::_ensure_user_data_dir() {
  308. String dd = get_user_data_dir();
  309. DirAccess *da = DirAccess::open(dd);
  310. if (da) {
  311. memdelete(da);
  312. return;
  313. }
  314. da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  315. Error err = da->make_dir_recursive(dd);
  316. if (err != OK) {
  317. ERR_EXPLAIN("Error attempting to create data dir: " + dd);
  318. }
  319. ERR_FAIL_COND(err != OK);
  320. memdelete(da);
  321. }
  322. void OS::set_icon(const Ref<Image> &p_icon) {
  323. }
  324. String OS::get_model_name() const {
  325. return "GenericDevice";
  326. }
  327. void OS::set_cmdline(const char *p_execpath, const List<String> &p_args) {
  328. _execpath = p_execpath;
  329. _cmdline = p_args;
  330. };
  331. void OS::release_rendering_thread() {
  332. }
  333. void OS::make_rendering_thread() {
  334. }
  335. void OS::swap_buffers() {
  336. }
  337. String OS::get_unique_id() const {
  338. ERR_FAIL_V("");
  339. }
  340. int OS::get_processor_count() const {
  341. return 1;
  342. }
  343. Error OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  344. return FAILED;
  345. };
  346. bool OS::native_video_is_playing() const {
  347. return false;
  348. };
  349. void OS::native_video_pause(){
  350. };
  351. void OS::native_video_unpause(){
  352. };
  353. void OS::native_video_stop(){
  354. };
  355. void OS::set_mouse_mode(MouseMode p_mode) {
  356. }
  357. bool OS::can_use_threads() const {
  358. #ifdef NO_THREADS
  359. return false;
  360. #else
  361. return true;
  362. #endif
  363. }
  364. OS::MouseMode OS::get_mouse_mode() const {
  365. return MOUSE_MODE_VISIBLE;
  366. }
  367. OS::LatinKeyboardVariant OS::get_latin_keyboard_variant() const {
  368. return LATIN_KEYBOARD_QWERTY;
  369. }
  370. bool OS::is_joy_known(int p_device) {
  371. return true;
  372. }
  373. String OS::get_joy_guid(int p_device) const {
  374. return "Default Joypad";
  375. }
  376. void OS::set_context(int p_context) {
  377. }
  378. void OS::set_use_vsync(bool p_enable) {
  379. }
  380. bool OS::is_vsync_enabled() const {
  381. return true;
  382. }
  383. OS::PowerState OS::get_power_state() {
  384. return POWERSTATE_UNKNOWN;
  385. }
  386. int OS::get_power_seconds_left() {
  387. return -1;
  388. }
  389. int OS::get_power_percent_left() {
  390. return -1;
  391. }
  392. bool OS::has_feature(const String &p_feature) {
  393. if (p_feature == get_name())
  394. return true;
  395. #ifdef DEBUG_ENABLED
  396. if (p_feature == "debug")
  397. return true;
  398. #else
  399. if (p_feature == "release")
  400. return true;
  401. #endif
  402. if (sizeof(void *) == 8 && p_feature == "64") {
  403. return true;
  404. }
  405. if (sizeof(void *) == 4 && p_feature == "32") {
  406. return true;
  407. }
  408. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__)
  409. if (p_feature == "x86_64") {
  410. return true;
  411. }
  412. #elif (defined(__i386) || defined(__i386__))
  413. if (p_feature == "x86") {
  414. return true;
  415. }
  416. #elif defined(__aarch64__)
  417. if (p_feature == "arm64") {
  418. return true;
  419. }
  420. #elif defined(__arm__)
  421. #if defined(__ARM_ARCH_7A__)
  422. if (p_feature == "armv7a" || p_feature == "armv7") {
  423. return true;
  424. }
  425. #endif
  426. #if defined(__ARM_ARCH_7S__)
  427. if (p_feature == "armv7s" || p_feature == "armv7") {
  428. return true;
  429. }
  430. #endif
  431. if (p_feature == "arm") {
  432. return true;
  433. }
  434. #endif
  435. if (_check_internal_feature_support(p_feature))
  436. return true;
  437. return false;
  438. }
  439. void *OS::get_stack_bottom() const {
  440. return _stack_bottom;
  441. }
  442. OS::OS() {
  443. void *volatile stack_bottom;
  444. last_error = NULL;
  445. singleton = this;
  446. _keep_screen_on = true; // set default value to true, because this had been true before godot 2.0.
  447. low_processor_usage_mode = false;
  448. low_processor_usage_mode_sleep_usec = 10000;
  449. _verbose_stdout = false;
  450. _no_window = false;
  451. _exit_code = 0;
  452. _orientation = SCREEN_LANDSCAPE;
  453. _render_thread_mode = RENDER_THREAD_SAFE;
  454. _allow_hidpi = false;
  455. _stack_bottom = (void *)(&stack_bottom);
  456. _logger = NULL;
  457. Vector<Logger *> loggers;
  458. loggers.push_back(memnew(StdLogger));
  459. _set_logger(memnew(CompositeLogger(loggers)));
  460. }
  461. OS::~OS() {
  462. memdelete(_logger);
  463. singleton = NULL;
  464. }