os.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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_clipboard(const String &p_text) {
  99. _local_clipboard = p_text;
  100. }
  101. String OS::get_clipboard() const {
  102. return _local_clipboard;
  103. }
  104. String OS::get_executable_path() const {
  105. return _execpath;
  106. }
  107. int OS::get_process_id() const {
  108. return -1;
  109. };
  110. bool OS::is_stdout_verbose() const {
  111. return _verbose_stdout;
  112. }
  113. void OS::set_last_error(const char *p_error) {
  114. GLOBAL_LOCK_FUNCTION
  115. if (p_error == NULL)
  116. p_error = "Unknown Error";
  117. if (last_error)
  118. memfree(last_error);
  119. last_error = NULL;
  120. int len = 0;
  121. while (p_error[len++])
  122. ;
  123. last_error = (char *)memalloc(len);
  124. for (int i = 0; i < len; i++)
  125. last_error[i] = p_error[i];
  126. }
  127. const char *OS::get_last_error() const {
  128. GLOBAL_LOCK_FUNCTION
  129. return last_error ? last_error : "";
  130. }
  131. void OS::dump_memory_to_file(const char *p_file) {
  132. //Memory::dump_static_mem_to_file(p_file);
  133. }
  134. static FileAccess *_OSPRF = NULL;
  135. static void _OS_printres(Object *p_obj) {
  136. Resource *res = Object::cast_to<Resource>(p_obj);
  137. if (!res)
  138. return;
  139. String str = itos(res->get_instance_id()) + String(res->get_class()) + ":" + String(res->get_name()) + " - " + res->get_path();
  140. if (_OSPRF)
  141. _OSPRF->store_line(str);
  142. else
  143. print_line(str);
  144. }
  145. bool OS::has_virtual_keyboard() const {
  146. return false;
  147. }
  148. void OS::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) {
  149. }
  150. void OS::hide_virtual_keyboard() {
  151. }
  152. int OS::get_virtual_keyboard_height() const {
  153. return 0;
  154. }
  155. void OS::print_all_resources(String p_to_file) {
  156. ERR_FAIL_COND(p_to_file != "" && _OSPRF);
  157. if (p_to_file != "") {
  158. Error err;
  159. _OSPRF = FileAccess::open(p_to_file, FileAccess::WRITE, &err);
  160. if (err != OK) {
  161. _OSPRF = NULL;
  162. ERR_FAIL_COND(err != OK);
  163. }
  164. }
  165. ObjectDB::debug_objects(_OS_printres);
  166. if (p_to_file != "") {
  167. if (_OSPRF)
  168. memdelete(_OSPRF);
  169. _OSPRF = NULL;
  170. }
  171. }
  172. void OS::print_resources_in_use(bool p_short) {
  173. ResourceCache::dump(NULL, p_short);
  174. }
  175. void OS::dump_resources_to_file(const char *p_file) {
  176. ResourceCache::dump(p_file);
  177. }
  178. void OS::clear_last_error() {
  179. GLOBAL_LOCK_FUNCTION
  180. if (last_error)
  181. memfree(last_error);
  182. last_error = NULL;
  183. }
  184. void OS::set_no_window_mode(bool p_enable) {
  185. _no_window = p_enable;
  186. }
  187. bool OS::is_no_window_mode_enabled() const {
  188. return _no_window;
  189. }
  190. int OS::get_exit_code() const {
  191. return _exit_code;
  192. }
  193. void OS::set_exit_code(int p_code) {
  194. _exit_code = p_code;
  195. }
  196. String OS::get_locale() const {
  197. return "en";
  198. }
  199. // Helper function used by OS_Unix and OS_Windows
  200. String OS::get_safe_application_name() const {
  201. String an = ProjectSettings::get_singleton()->get("application/config/name");
  202. Vector<String> invalid_char = String("\\ / : * ? \" < > |").split(" ");
  203. for (int i = 0; i < invalid_char.size(); i++) {
  204. an = an.replace(invalid_char[i], "-");
  205. }
  206. return an;
  207. }
  208. // Path to data, config, cache, etc. OS-specific folders
  209. // Get properly capitalized engine name for system paths
  210. String OS::get_godot_dir_name() const {
  211. // Default to lowercase, so only override when different case is needed
  212. return String(VERSION_SHORT_NAME).to_lower();
  213. }
  214. // OS equivalent of XDG_DATA_HOME
  215. String OS::get_data_path() const {
  216. return ".";
  217. }
  218. // OS equivalent of XDG_CONFIG_HOME
  219. String OS::get_config_path() const {
  220. return ".";
  221. }
  222. // OS equivalent of XDG_CACHE_HOME
  223. String OS::get_cache_path() const {
  224. return ".";
  225. }
  226. // OS specific path for user://
  227. String OS::get_user_data_dir() const {
  228. return ".";
  229. };
  230. // Absolute path to res://
  231. String OS::get_resource_dir() const {
  232. return ProjectSettings::get_singleton()->get_resource_path();
  233. }
  234. // Access system-specific dirs like Documents, Downloads, etc.
  235. String OS::get_system_dir(SystemDir p_dir) const {
  236. return ".";
  237. }
  238. Error OS::shell_open(String p_uri) {
  239. return ERR_UNAVAILABLE;
  240. };
  241. // implement these with the canvas?
  242. Error OS::dialog_show(String p_title, String p_description, Vector<String> p_buttons, Object *p_obj, String p_callback) {
  243. while (true) {
  244. print("%ls\n--------\n%ls\n", p_title.c_str(), p_description.c_str());
  245. for (int i = 0; i < p_buttons.size(); i++) {
  246. if (i > 0) print(", ");
  247. print("%i=%ls", i + 1, p_buttons[i].c_str());
  248. };
  249. print("\n");
  250. String res = get_stdin_string().strip_edges();
  251. if (!res.is_numeric())
  252. continue;
  253. int n = res.to_int();
  254. if (n < 0 || n >= p_buttons.size())
  255. continue;
  256. if (p_obj && p_callback != "")
  257. p_obj->call_deferred(p_callback, n);
  258. break;
  259. };
  260. return OK;
  261. };
  262. Error OS::dialog_input_text(String p_title, String p_description, String p_partial, Object *p_obj, String p_callback) {
  263. ERR_FAIL_COND_V(!p_obj, FAILED);
  264. ERR_FAIL_COND_V(p_callback == "", FAILED);
  265. print("%ls\n---------\n%ls\n[%ls]:\n", p_title.c_str(), p_description.c_str(), p_partial.c_str());
  266. String res = get_stdin_string().strip_edges();
  267. bool success = true;
  268. if (res == "") {
  269. res = p_partial;
  270. };
  271. p_obj->call_deferred(p_callback, success, res);
  272. return OK;
  273. };
  274. int OS::get_static_memory_usage() const {
  275. return Memory::get_mem_usage();
  276. }
  277. int OS::get_dynamic_memory_usage() const {
  278. return MemoryPool::total_memory;
  279. }
  280. int OS::get_static_memory_peak_usage() const {
  281. return Memory::get_mem_max_usage();
  282. }
  283. Error OS::set_cwd(const String &p_cwd) {
  284. return ERR_CANT_OPEN;
  285. }
  286. bool OS::has_touchscreen_ui_hint() const {
  287. //return false;
  288. return Input::get_singleton() && Input::get_singleton()->is_emulating_touchscreen();
  289. }
  290. int OS::get_free_static_memory() const {
  291. return Memory::get_mem_available();
  292. }
  293. void OS::yield() {
  294. }
  295. void OS::set_screen_orientation(ScreenOrientation p_orientation) {
  296. _orientation = p_orientation;
  297. }
  298. OS::ScreenOrientation OS::get_screen_orientation() const {
  299. return (OS::ScreenOrientation)_orientation;
  300. }
  301. void OS::_ensure_user_data_dir() {
  302. String dd = get_user_data_dir();
  303. DirAccess *da = DirAccess::open(dd);
  304. if (da) {
  305. memdelete(da);
  306. return;
  307. }
  308. da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  309. Error err = da->make_dir_recursive(dd);
  310. if (err != OK) {
  311. ERR_EXPLAIN("Error attempting to create data dir: " + dd);
  312. }
  313. ERR_FAIL_COND(err != OK);
  314. memdelete(da);
  315. }
  316. void OS::set_icon(const Ref<Image> &p_icon) {
  317. }
  318. String OS::get_model_name() const {
  319. return "GenericDevice";
  320. }
  321. void OS::set_cmdline(const char *p_execpath, const List<String> &p_args) {
  322. _execpath = p_execpath;
  323. _cmdline = p_args;
  324. };
  325. void OS::release_rendering_thread() {
  326. }
  327. void OS::make_rendering_thread() {
  328. }
  329. void OS::swap_buffers() {
  330. }
  331. String OS::get_unique_id() const {
  332. ERR_FAIL_V("");
  333. }
  334. int OS::get_processor_count() const {
  335. return 1;
  336. }
  337. Error OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  338. return FAILED;
  339. };
  340. bool OS::native_video_is_playing() const {
  341. return false;
  342. };
  343. void OS::native_video_pause(){
  344. };
  345. void OS::native_video_unpause(){
  346. };
  347. void OS::native_video_stop(){
  348. };
  349. void OS::set_mouse_mode(MouseMode p_mode) {
  350. }
  351. bool OS::can_use_threads() const {
  352. #ifdef NO_THREADS
  353. return false;
  354. #else
  355. return true;
  356. #endif
  357. }
  358. OS::MouseMode OS::get_mouse_mode() const {
  359. return MOUSE_MODE_VISIBLE;
  360. }
  361. OS::LatinKeyboardVariant OS::get_latin_keyboard_variant() const {
  362. return LATIN_KEYBOARD_QWERTY;
  363. }
  364. bool OS::is_joy_known(int p_device) {
  365. return true;
  366. }
  367. String OS::get_joy_guid(int p_device) const {
  368. return "Default Joypad";
  369. }
  370. void OS::set_context(int p_context) {
  371. }
  372. void OS::set_use_vsync(bool p_enable) {
  373. }
  374. bool OS::is_vsync_enabled() const {
  375. return true;
  376. }
  377. OS::PowerState OS::get_power_state() {
  378. return POWERSTATE_UNKNOWN;
  379. }
  380. int OS::get_power_seconds_left() {
  381. return -1;
  382. }
  383. int OS::get_power_percent_left() {
  384. return -1;
  385. }
  386. bool OS::has_feature(const String &p_feature) {
  387. if (p_feature == get_name())
  388. return true;
  389. #ifdef DEBUG_ENABLED
  390. if (p_feature == "debug")
  391. return true;
  392. #else
  393. if (p_feature == "release")
  394. return true;
  395. #endif
  396. if (sizeof(void *) == 8 && p_feature == "64") {
  397. return true;
  398. }
  399. if (sizeof(void *) == 4 && p_feature == "32") {
  400. return true;
  401. }
  402. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__)
  403. if (p_feature == "x86_64") {
  404. return true;
  405. }
  406. #elif (defined(__i386) || defined(__i386__))
  407. if (p_feature == "x86") {
  408. return true;
  409. }
  410. #elif defined(__aarch64__)
  411. if (p_feature == "arm64") {
  412. return true;
  413. }
  414. #elif defined(__arm__)
  415. #if defined(__ARM_ARCH_7A__)
  416. if (p_feature == "armv7a" || p_feature == "armv7") {
  417. return true;
  418. }
  419. #endif
  420. #if defined(__ARM_ARCH_7S__)
  421. if (p_feature == "armv7s" || p_feature == "armv7") {
  422. return true;
  423. }
  424. #endif
  425. if (p_feature == "arm") {
  426. return true;
  427. }
  428. #endif
  429. if (_check_internal_feature_support(p_feature))
  430. return true;
  431. return false;
  432. }
  433. void *OS::get_stack_bottom() const {
  434. return _stack_bottom;
  435. }
  436. OS::OS() {
  437. void *volatile stack_bottom;
  438. last_error = NULL;
  439. singleton = this;
  440. _keep_screen_on = true; // set default value to true, because this had been true before godot 2.0.
  441. low_processor_usage_mode = false;
  442. _verbose_stdout = false;
  443. _no_window = false;
  444. _exit_code = 0;
  445. _orientation = SCREEN_LANDSCAPE;
  446. _render_thread_mode = RENDER_THREAD_SAFE;
  447. _allow_hidpi = false;
  448. _stack_bottom = (void *)(&stack_bottom);
  449. _logger = NULL;
  450. Vector<Logger *> loggers;
  451. loggers.push_back(memnew(StdLogger));
  452. _set_logger(memnew(CompositeLogger(loggers)));
  453. }
  454. OS::~OS() {
  455. memdelete(_logger);
  456. singleton = NULL;
  457. }