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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "globals.h"
  33. #include "input.h"
  34. #include "os/file_access.h"
  35. #include <stdarg.h>
  36. // For get_engine_version, could be removed if it's moved to a new Engine singleton
  37. #include "version.h"
  38. OS *OS::singleton = NULL;
  39. OS *OS::get_singleton() {
  40. return singleton;
  41. }
  42. uint32_t OS::get_ticks_msec() const {
  43. return get_ticks_usec() / 1000;
  44. }
  45. uint64_t OS::get_splash_tick_msec() const {
  46. return _msec_splash;
  47. }
  48. uint64_t OS::get_unix_time() const {
  49. return 0;
  50. };
  51. uint64_t OS::get_system_time_secs() const {
  52. return 0;
  53. }
  54. void OS::debug_break(){
  55. // something
  56. };
  57. void OS::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) {
  58. const char *err_type;
  59. switch (p_type) {
  60. case ERR_ERROR: err_type = "**ERROR**"; break;
  61. case ERR_WARNING: err_type = "**WARNING**"; break;
  62. case ERR_SCRIPT: err_type = "**SCRIPT ERROR**"; break;
  63. }
  64. if (p_rationale && *p_rationale)
  65. print("%s: %s\n ", err_type, p_rationale);
  66. print("%s: At: %s:%i:%s() - %s\n", err_type, p_file, p_line, p_function, p_code);
  67. }
  68. void OS::print(const char *p_format, ...) {
  69. va_list argp;
  70. va_start(argp, p_format);
  71. vprint(p_format, argp);
  72. va_end(argp);
  73. };
  74. void OS::printerr(const char *p_format, ...) {
  75. va_list argp;
  76. va_start(argp, p_format);
  77. vprint(p_format, argp, true);
  78. va_end(argp);
  79. };
  80. void OS::set_iterations_per_second(int p_ips) {
  81. ips = p_ips;
  82. }
  83. int OS::get_iterations_per_second() const {
  84. return ips;
  85. }
  86. void OS::set_target_fps(int p_fps) {
  87. _target_fps = p_fps > 0 ? p_fps : 0;
  88. }
  89. float OS::get_target_fps() const {
  90. return _target_fps;
  91. }
  92. void OS::set_keep_screen_on(bool p_enabled) {
  93. _keep_screen_on = p_enabled;
  94. }
  95. bool OS::is_keep_screen_on() const {
  96. return _keep_screen_on;
  97. }
  98. void OS::set_low_processor_usage_mode(bool p_enabled) {
  99. low_processor_usage_mode = p_enabled;
  100. }
  101. bool OS::is_in_low_processor_usage_mode() const {
  102. return low_processor_usage_mode;
  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. uint64_t OS::get_frames_drawn() {
  117. return frames_drawn;
  118. }
  119. bool OS::is_stdout_verbose() const {
  120. return _verbose_stdout;
  121. }
  122. void OS::set_last_error(const char *p_error) {
  123. GLOBAL_LOCK_FUNCTION
  124. if (p_error == NULL)
  125. p_error = "Unknown Error";
  126. if (last_error)
  127. memfree(last_error);
  128. last_error = NULL;
  129. int len = 0;
  130. while (p_error[len++])
  131. ;
  132. last_error = (char *)memalloc(len);
  133. for (int i = 0; i < len; i++)
  134. last_error[i] = p_error[i];
  135. }
  136. const char *OS::get_last_error() const {
  137. GLOBAL_LOCK_FUNCTION
  138. return last_error ? last_error : "";
  139. }
  140. void OS::dump_memory_to_file(const char *p_file) {
  141. Memory::dump_static_mem_to_file(p_file);
  142. }
  143. static FileAccess *_OSPRF = NULL;
  144. static void _OS_printres(Object *p_obj) {
  145. Resource *res = p_obj->cast_to<Resource>();
  146. if (!res)
  147. return;
  148. String str = itos(res->get_instance_ID()) + String(res->get_type()) + ":" + String(res->get_name()) + " - " + res->get_path();
  149. if (_OSPRF)
  150. _OSPRF->store_line(str);
  151. else
  152. print_line(str);
  153. }
  154. bool OS::has_virtual_keyboard() const {
  155. return false;
  156. }
  157. void OS::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) {
  158. }
  159. void OS::hide_virtual_keyboard() {
  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_frame_delay(uint32_t p_msec) {
  191. _frame_delay = p_msec;
  192. }
  193. uint32_t OS::get_frame_delay() const {
  194. return _frame_delay;
  195. }
  196. void OS::set_no_window_mode(bool p_enable) {
  197. _no_window = p_enable;
  198. }
  199. bool OS::is_no_window_mode_enabled() const {
  200. return _no_window;
  201. }
  202. int OS::get_exit_code() const {
  203. return _exit_code;
  204. }
  205. void OS::set_exit_code(int p_code) {
  206. _exit_code = p_code;
  207. }
  208. String OS::get_locale() const {
  209. return "en";
  210. }
  211. String OS::get_resource_dir() const {
  212. return Globals::get_singleton()->get_resource_path();
  213. }
  214. String OS::get_system_dir(SystemDir p_dir) const {
  215. return ".";
  216. }
  217. String OS::get_safe_application_name() const {
  218. String an = Globals::get_singleton()->get("application/name");
  219. Vector<String> invalid_char = String("\\ / : * ? \" < > |").split(" ");
  220. for (int i = 0; i < invalid_char.size(); i++) {
  221. an = an.replace(invalid_char[i], "-");
  222. }
  223. return an;
  224. }
  225. String OS::get_data_dir() const {
  226. return ".";
  227. };
  228. Error OS::shell_open(String p_uri) {
  229. return ERR_UNAVAILABLE;
  230. };
  231. // implement these with the canvas?
  232. Error OS::dialog_show(String p_title, String p_description, Vector<String> p_buttons, Object *p_obj, String p_callback) {
  233. while (true) {
  234. print("%ls\n--------\n%ls\n", p_title.c_str(), p_description.c_str());
  235. for (int i = 0; i < p_buttons.size(); i++) {
  236. if (i > 0) print(", ");
  237. print("%i=%ls", i + 1, p_buttons[i].c_str());
  238. };
  239. print("\n");
  240. String res = get_stdin_string().strip_edges();
  241. if (!res.is_numeric())
  242. continue;
  243. int n = res.to_int();
  244. if (n < 0 || n >= p_buttons.size())
  245. continue;
  246. if (p_obj && p_callback != "")
  247. p_obj->call_deferred(p_callback, n);
  248. break;
  249. };
  250. return OK;
  251. };
  252. Error OS::dialog_input_text(String p_title, String p_description, String p_partial, Object *p_obj, String p_callback) {
  253. ERR_FAIL_COND_V(!p_obj, FAILED);
  254. ERR_FAIL_COND_V(p_callback == "", FAILED);
  255. print("%ls\n---------\n%ls\n[%ls]:\n", p_title.c_str(), p_description.c_str(), p_partial.c_str());
  256. String res = get_stdin_string().strip_edges();
  257. bool success = true;
  258. if (res == "") {
  259. res = p_partial;
  260. };
  261. p_obj->call_deferred(p_callback, success, res);
  262. return OK;
  263. };
  264. int OS::get_static_memory_usage() const {
  265. return Memory::get_static_mem_usage();
  266. }
  267. int OS::get_dynamic_memory_usage() const {
  268. return Memory::get_dynamic_mem_usage();
  269. }
  270. int OS::get_static_memory_peak_usage() const {
  271. return Memory::get_static_mem_max_usage();
  272. }
  273. Error OS::set_cwd(const String &p_cwd) {
  274. return ERR_CANT_OPEN;
  275. }
  276. bool OS::has_touchscreen_ui_hint() const {
  277. //return false;
  278. return Input::get_singleton() && Input::get_singleton()->is_emulating_touchscreen();
  279. }
  280. int OS::get_free_static_memory() const {
  281. return Memory::get_static_mem_available();
  282. }
  283. void OS::yield() {
  284. }
  285. void OS::set_screen_orientation(ScreenOrientation p_orientation) {
  286. _orientation = p_orientation;
  287. }
  288. OS::ScreenOrientation OS::get_screen_orientation() const {
  289. return (OS::ScreenOrientation)_orientation;
  290. }
  291. void OS::_ensure_data_dir() {
  292. String dd = get_data_dir();
  293. DirAccess *da = DirAccess::open(dd);
  294. if (da) {
  295. memdelete(da);
  296. return;
  297. }
  298. da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  299. Error err = da->make_dir_recursive(dd);
  300. if (err != OK) {
  301. ERR_EXPLAIN("Error attempting to create data dir: " + dd);
  302. }
  303. ERR_FAIL_COND(err != OK);
  304. memdelete(da);
  305. }
  306. void OS::set_icon(const Image &p_icon) {
  307. }
  308. String OS::get_model_name() const {
  309. return "GenericDevice";
  310. }
  311. void OS::set_cmdline(const char *p_execpath, const List<String> &p_args) {
  312. _execpath = p_execpath;
  313. _cmdline = p_args;
  314. };
  315. void OS::release_rendering_thread() {
  316. }
  317. void OS::make_rendering_thread() {
  318. }
  319. void OS::swap_buffers() {
  320. }
  321. String OS::get_unique_ID() const {
  322. ERR_FAIL_V("");
  323. }
  324. int OS::get_processor_count() const {
  325. return 1;
  326. }
  327. Error OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  328. return FAILED;
  329. };
  330. bool OS::native_video_is_playing() const {
  331. return false;
  332. };
  333. void OS::native_video_pause(){
  334. };
  335. void OS::native_video_unpause(){
  336. };
  337. void OS::native_video_stop(){
  338. };
  339. void OS::set_mouse_mode(MouseMode p_mode) {
  340. }
  341. bool OS::can_use_threads() const {
  342. #ifdef NO_THREADS
  343. return false;
  344. #else
  345. return true;
  346. #endif
  347. }
  348. OS::MouseMode OS::get_mouse_mode() const {
  349. return MOUSE_MODE_VISIBLE;
  350. }
  351. void OS::set_time_scale(float p_scale) {
  352. _time_scale = p_scale;
  353. }
  354. OS::LatinKeyboardVariant OS::get_latin_keyboard_variant() const {
  355. return LATIN_KEYBOARD_QWERTY;
  356. }
  357. float OS::get_time_scale() const {
  358. return _time_scale;
  359. }
  360. bool OS::is_joy_known(int p_device) {
  361. return true;
  362. }
  363. String OS::get_joy_guid(int p_device) const {
  364. return "Default Joystick";
  365. }
  366. void OS::set_context(int p_context) {
  367. }
  368. void OS::set_use_vsync(bool p_enable) {
  369. }
  370. bool OS::is_vsync_enabled() const {
  371. return true;
  372. }
  373. Dictionary OS::get_engine_version() const {
  374. Dictionary dict;
  375. dict["major"] = _MKSTR(VERSION_MAJOR);
  376. dict["minor"] = _MKSTR(VERSION_MINOR);
  377. #ifdef VERSION_PATCH
  378. dict["patch"] = _MKSTR(VERSION_PATCH);
  379. #else
  380. dict["patch"] = "";
  381. #endif
  382. dict["status"] = _MKSTR(VERSION_STATUS);
  383. dict["revision"] = _MKSTR(VERSION_REVISION);
  384. String stringver = String(dict["major"]) + "." + String(dict["minor"]);
  385. if (dict["patch"] != "")
  386. stringver += "." + String(dict["patch"]);
  387. stringver += "-" + String(dict["status"]) + " (" + String(dict["revision"]) + ")";
  388. dict["string"] = stringver;
  389. return dict;
  390. }
  391. void OS::center_window() {
  392. if (is_window_fullscreen()) return;
  393. Size2 scr = get_screen_size(get_current_screen());
  394. Size2 wnd = get_real_window_size();
  395. int x = scr.width / 2 - wnd.width / 2;
  396. int y = scr.height / 2 - wnd.height / 2;
  397. set_window_position(Vector2(x, y));
  398. }
  399. OS::OS() {
  400. last_error = NULL;
  401. frames_drawn = 0;
  402. singleton = this;
  403. ips = 60;
  404. _keep_screen_on = true; // set default value to true, because this had been true before godot 2.0.
  405. low_processor_usage_mode = false;
  406. _verbose_stdout = false;
  407. _frame_delay = 0;
  408. _no_window = false;
  409. _exit_code = 0;
  410. _orientation = SCREEN_LANDSCAPE;
  411. _fps = 1;
  412. _target_fps = 0;
  413. _render_thread_mode = RENDER_THREAD_SAFE;
  414. _time_scale = 1.0;
  415. _pixel_snap = false;
  416. _allow_hidpi = true;
  417. Math::seed(1234567);
  418. }
  419. OS::~OS() {
  420. singleton = NULL;
  421. }