os.cpp 12 KB

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