os.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*************************************************************************/
  2. /* os.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "os.h"
  30. #include "os/file_access.h"
  31. #include <stdarg.h>
  32. #include "dir_access.h"
  33. #include "globals.h"
  34. #include "input.h"
  35. // For get_engine_version, could be removed if it's moved to a new Engine singleton
  36. #include "version.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. uint64_t OS::get_splash_tick_msec() const {
  44. return _msec_splash;
  45. }
  46. uint64_t OS::get_unix_time() const {
  47. return 0;
  48. };
  49. uint64_t OS::get_system_time_secs() const {
  50. return 0;
  51. }
  52. void OS::debug_break() {
  53. // something
  54. };
  55. 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) {
  56. const char* err_type;
  57. switch(p_type) {
  58. case ERR_ERROR: err_type="**ERROR**"; break;
  59. case ERR_WARNING: err_type="**WARNING**"; break;
  60. case ERR_SCRIPT: err_type="**SCRIPT ERROR**"; break;
  61. case ERR_SHADER: err_type="**SHADER ERROR**"; break;
  62. }
  63. if (p_rationale && *p_rationale)
  64. print("%s: %s\n ",err_type,p_rationale);
  65. print("%s: At: %s:%i:%s() - %s\n",err_type,p_file,p_line,p_function,p_code);
  66. }
  67. void OS::print(const char* p_format, ...) {
  68. va_list argp;
  69. va_start(argp, p_format);
  70. vprint(p_format, argp);
  71. va_end(argp);
  72. };
  73. void OS::printerr(const char* p_format, ...) {
  74. va_list argp;
  75. va_start(argp, p_format);
  76. vprint(p_format, argp, true);
  77. va_end(argp);
  78. };
  79. void OS::set_iterations_per_second(int p_ips) {
  80. ips=p_ips;
  81. }
  82. int OS::get_iterations_per_second() const {
  83. return ips;
  84. }
  85. void OS::set_target_fps(int p_fps) {
  86. _target_fps=p_fps>0? p_fps : 0;
  87. }
  88. float OS::get_target_fps() const {
  89. return _target_fps;
  90. }
  91. void OS::set_keep_screen_on(bool p_enabled) {
  92. _keep_screen_on=p_enabled;
  93. }
  94. bool OS::is_keep_screen_on() const {
  95. return _keep_screen_on;
  96. }
  97. void OS::set_low_processor_usage_mode(bool p_enabled) {
  98. low_processor_usage_mode=p_enabled;
  99. }
  100. bool OS::is_in_low_processor_usage_mode() const {
  101. return low_processor_usage_mode;
  102. }
  103. void OS::set_clipboard(const String& p_text) {
  104. _local_clipboard=p_text;
  105. }
  106. String OS::get_clipboard() const {
  107. return _local_clipboard;
  108. }
  109. String OS::get_executable_path() const {
  110. return _execpath;
  111. }
  112. int OS::get_process_ID() const {
  113. return -1;
  114. };
  115. uint64_t OS::get_frames_drawn() {
  116. return frames_drawn;
  117. }
  118. bool OS::is_stdout_verbose() const {
  119. return _verbose_stdout;
  120. }
  121. void OS::set_last_error(const char* p_error) {
  122. GLOBAL_LOCK_FUNCTION
  123. if (p_error==NULL)
  124. p_error="Unknown Error";
  125. if (last_error)
  126. memfree(last_error);
  127. last_error=NULL;
  128. int len =0;
  129. while(p_error[len++]);
  130. last_error=(char*)memalloc(len);
  131. for(int i=0;i<len;i++)
  132. last_error[i]=p_error[i];
  133. }
  134. const char *OS::get_last_error() const {
  135. GLOBAL_LOCK_FUNCTION
  136. return last_error?last_error:"";
  137. }
  138. void OS::dump_memory_to_file(const char* p_file) {
  139. Memory::dump_static_mem_to_file(p_file);
  140. }
  141. static FileAccess *_OSPRF=NULL;
  142. static void _OS_printres(Object *p_obj) {
  143. Resource *res = p_obj->cast_to<Resource>();
  144. if (!res)
  145. return;
  146. String str = itos(res->get_instance_ID())+String(res->get_class())+":"+String(res->get_name())+" - "+res->get_path();
  147. if (_OSPRF)
  148. _OSPRF->store_line(str);
  149. else
  150. print_line(str);
  151. }
  152. bool OS::has_virtual_keyboard() const {
  153. return false;
  154. }
  155. void OS::show_virtual_keyboard(const String& p_existing_text,const Rect2& p_screen_rect) {
  156. }
  157. void OS::hide_virtual_keyboard(){
  158. }
  159. void OS::print_all_resources(String p_to_file) {
  160. ERR_FAIL_COND(p_to_file!="" && _OSPRF);
  161. if (p_to_file!="") {
  162. Error err;
  163. _OSPRF=FileAccess::open(p_to_file,FileAccess::WRITE,&err);
  164. if (err!=OK) {
  165. _OSPRF=NULL;
  166. ERR_FAIL_COND(err!=OK);
  167. }
  168. }
  169. ObjectDB::debug_objects(_OS_printres);
  170. if (p_to_file!="") {
  171. if (_OSPRF)
  172. memdelete(_OSPRF);
  173. _OSPRF=NULL;
  174. }
  175. }
  176. void OS::print_resources_in_use(bool p_short) {
  177. ResourceCache::dump(NULL,p_short);
  178. }
  179. void OS::dump_resources_to_file(const char* p_file) {
  180. ResourceCache::dump(p_file);
  181. }
  182. void OS::clear_last_error() {
  183. GLOBAL_LOCK_FUNCTION
  184. if (last_error)
  185. memfree(last_error);
  186. last_error=NULL;
  187. }
  188. void OS::set_frame_delay(uint32_t p_msec) {
  189. _frame_delay=p_msec;
  190. }
  191. uint32_t OS::get_frame_delay() const {
  192. return _frame_delay;
  193. }
  194. void OS::set_no_window_mode(bool p_enable) {
  195. _no_window=p_enable;
  196. }
  197. bool OS::is_no_window_mode_enabled() const {
  198. return _no_window;
  199. }
  200. int OS::get_exit_code() const {
  201. return _exit_code;
  202. }
  203. void OS::set_exit_code(int p_code) {
  204. _exit_code=p_code;
  205. }
  206. String OS::get_locale() const {
  207. return "en";
  208. }
  209. String OS::get_resource_dir() const {
  210. return GlobalConfig::get_singleton()->get_resource_path();
  211. }
  212. String OS::get_system_dir(SystemDir p_dir) const {
  213. return ".";
  214. }
  215. String OS::get_safe_application_name() const {
  216. String an = GlobalConfig::get_singleton()->get("application/name");
  217. Vector<String> invalid_char = String("\\ / : * ? \" < > |").split(" ");
  218. for (int i=0;i<invalid_char.size();i++) {
  219. an = an.replace(invalid_char[i],"-");
  220. }
  221. return an;
  222. }
  223. String OS::get_data_dir() const {
  224. return ".";
  225. };
  226. Error OS::shell_open(String p_uri) {
  227. return ERR_UNAVAILABLE;
  228. };
  229. // implement these with the canvas?
  230. Error OS::dialog_show(String p_title, String p_description, Vector<String> p_buttons, Object* p_obj, String p_callback) {
  231. while (true) {
  232. print("%ls\n--------\n%ls\n", p_title.c_str(), p_description.c_str());
  233. for (int i=0; i<p_buttons.size(); i++) {
  234. if (i>0) print(", ");
  235. print("%i=%ls", i+1, p_buttons[i].c_str());
  236. };
  237. print("\n");
  238. String res = get_stdin_string().strip_edges();
  239. if (!res.is_numeric())
  240. continue;
  241. int n = res.to_int();
  242. if (n < 0 || n >= p_buttons.size())
  243. continue;
  244. if (p_obj && p_callback != "")
  245. p_obj->call_deferred(p_callback, n);
  246. break;
  247. };
  248. return OK;
  249. };
  250. Error OS::dialog_input_text(String p_title, String p_description, String p_partial, Object* p_obj, String p_callback) {
  251. ERR_FAIL_COND_V(!p_obj, FAILED);
  252. ERR_FAIL_COND_V(p_callback == "", FAILED);
  253. print("%ls\n---------\n%ls\n[%ls]:\n", p_title.c_str(), p_description.c_str(), p_partial.c_str());
  254. String res = get_stdin_string().strip_edges();
  255. bool success = true;
  256. if (res == "") {
  257. res = p_partial;
  258. };
  259. p_obj->call_deferred(p_callback, success, res);
  260. return OK;
  261. };
  262. int OS::get_static_memory_usage() const {
  263. return Memory::get_static_mem_usage();
  264. }
  265. int OS::get_dynamic_memory_usage() const{
  266. return Memory::get_dynamic_mem_usage();
  267. }
  268. int OS::get_static_memory_peak_usage() const {
  269. return Memory::get_static_mem_max_usage();
  270. }
  271. Error OS::set_cwd(const String& p_cwd) {
  272. return ERR_CANT_OPEN;
  273. }
  274. bool OS::has_touchscreen_ui_hint() const {
  275. //return false;
  276. return Input::get_singleton() && Input::get_singleton()->is_emulating_touchscreen();
  277. }
  278. int OS::get_free_static_memory() const {
  279. return Memory::get_static_mem_available();
  280. }
  281. void OS::yield() {
  282. }
  283. void OS::set_screen_orientation(ScreenOrientation p_orientation) {
  284. _orientation=p_orientation;
  285. }
  286. OS::ScreenOrientation OS::get_screen_orientation() const {
  287. return (OS::ScreenOrientation)_orientation;
  288. }
  289. void OS::_ensure_data_dir() {
  290. String dd = get_data_dir();
  291. DirAccess *da = DirAccess::open(dd);
  292. if (da) {
  293. memdelete(da);
  294. return;
  295. }
  296. da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  297. Error err = da->make_dir_recursive(dd);
  298. if (err!=OK) {
  299. ERR_EXPLAIN("Error attempting to create data dir: "+dd);
  300. }
  301. ERR_FAIL_COND(err!=OK);
  302. memdelete(da);
  303. }
  304. void OS::set_icon(const Image& p_icon) {
  305. }
  306. String OS::get_model_name() const {
  307. return "GenericDevice";
  308. }
  309. void OS::set_cmdline(const char* p_execpath, const List<String>& p_args) {
  310. _execpath = p_execpath;
  311. _cmdline = p_args;
  312. };
  313. void OS::release_rendering_thread() {
  314. }
  315. void OS::make_rendering_thread() {
  316. }
  317. void OS::swap_buffers() {
  318. }
  319. String OS::get_unique_ID() const {
  320. ERR_FAIL_V("");
  321. }
  322. int OS::get_processor_count() const {
  323. return 1;
  324. }
  325. Error OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  326. return FAILED;
  327. };
  328. bool OS::native_video_is_playing() const {
  329. return false;
  330. };
  331. void OS::native_video_pause() {
  332. };
  333. void OS::native_video_unpause() {
  334. };
  335. void OS::native_video_stop() {
  336. };
  337. void OS::set_mouse_mode(MouseMode p_mode) {
  338. }
  339. bool OS::can_use_threads() const {
  340. #ifdef NO_THREADS
  341. return false;
  342. #else
  343. return true;
  344. #endif
  345. }
  346. OS::MouseMode OS::get_mouse_mode() const{
  347. return MOUSE_MODE_VISIBLE;
  348. }
  349. void OS::set_time_scale(float p_scale) {
  350. _time_scale=p_scale;
  351. }
  352. OS::LatinKeyboardVariant OS::get_latin_keyboard_variant() const {
  353. return LATIN_KEYBOARD_QWERTY;
  354. }
  355. float OS::get_time_scale() const {
  356. return _time_scale;
  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 Joystick";
  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. Dictionary OS::get_engine_version() const {
  372. Dictionary dict;
  373. dict["major"] = _MKSTR(VERSION_MAJOR);
  374. dict["minor"] = _MKSTR(VERSION_MINOR);
  375. #ifdef VERSION_PATCH
  376. dict["patch"] = _MKSTR(VERSION_PATCH);
  377. #else
  378. dict["patch"] = "";
  379. #endif
  380. dict["status"] = _MKSTR(VERSION_STATUS);
  381. dict["revision"] = _MKSTR(VERSION_REVISION);
  382. String stringver = String(dict["major"]) + "." + String(dict["minor"]);
  383. if (dict["patch"] != "")
  384. stringver += "." + String(dict["patch"]);
  385. stringver += "-" + String(dict["status"]) + " (" + String(dict["revision"]) + ")";
  386. dict["string"] = stringver;
  387. return dict;
  388. }
  389. OS::OS() {
  390. last_error=NULL;
  391. frames_drawn=0;
  392. singleton=this;
  393. ips=60;
  394. _keep_screen_on=true; // set default value to true, because this had been true before godot 2.0.
  395. low_processor_usage_mode=false;
  396. _verbose_stdout=false;
  397. _frame_delay=0;
  398. _no_window=false;
  399. _exit_code=0;
  400. _orientation=SCREEN_LANDSCAPE;
  401. _fps=1;
  402. _target_fps=0;
  403. _render_thread_mode=RENDER_THREAD_SAFE;
  404. _time_scale=1.0;
  405. _pixel_snap=false;
  406. _allow_hidpi=true;
  407. _fixed_frames=0;
  408. _idle_frames=0;
  409. _in_fixed=false;
  410. Math::seed(1234567);
  411. }
  412. OS::~OS() {
  413. singleton=NULL;
  414. }