os.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*************************************************************************/
  2. /* os.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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. }
  62. if (p_rationale && *p_rationale)
  63. print("%s: %s\n ",err_type,p_rationale);
  64. print("%s: At: %s:%i:%s() - %s\n",err_type,p_file,p_line,p_function,p_code);
  65. }
  66. void OS::print(const char* p_format, ...) {
  67. va_list argp;
  68. va_start(argp, p_format);
  69. vprint(p_format, argp);
  70. va_end(argp);
  71. };
  72. void OS::printerr(const char* p_format, ...) {
  73. va_list argp;
  74. va_start(argp, p_format);
  75. vprint(p_format, argp, true);
  76. va_end(argp);
  77. };
  78. void OS::set_iterations_per_second(int p_ips) {
  79. ips=p_ips;
  80. }
  81. int OS::get_iterations_per_second() const {
  82. return ips;
  83. }
  84. void OS::set_target_fps(int p_fps) {
  85. _target_fps=p_fps>0? p_fps : 0;
  86. }
  87. float OS::get_target_fps() const {
  88. return _target_fps;
  89. }
  90. void OS::set_keep_screen_on(bool p_enabled) {
  91. _keep_screen_on=p_enabled;
  92. }
  93. bool OS::is_keep_screen_on() const {
  94. return _keep_screen_on;
  95. }
  96. void OS::set_low_processor_usage_mode(bool p_enabled) {
  97. low_processor_usage_mode=p_enabled;
  98. }
  99. bool OS::is_in_low_processor_usage_mode() const {
  100. return low_processor_usage_mode;
  101. }
  102. void OS::set_clipboard(const String& p_text) {
  103. _local_clipboard=p_text;
  104. }
  105. String OS::get_clipboard() const {
  106. return _local_clipboard;
  107. }
  108. String OS::get_executable_path() const {
  109. return _execpath;
  110. }
  111. int OS::get_process_ID() const {
  112. return -1;
  113. };
  114. uint64_t OS::get_frames_drawn() {
  115. return frames_drawn;
  116. }
  117. bool OS::is_stdout_verbose() const {
  118. return _verbose_stdout;
  119. }
  120. void OS::set_last_error(const char* p_error) {
  121. GLOBAL_LOCK_FUNCTION
  122. if (p_error==NULL)
  123. p_error="Unknown Error";
  124. if (last_error)
  125. memfree(last_error);
  126. last_error=NULL;
  127. int len =0;
  128. while(p_error[len++]);
  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 = p_obj->cast_to<Resource>();
  143. if (!res)
  144. return;
  145. String str = itos(res->get_instance_ID())+String(res->get_type())+":"+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. void OS::print_all_resources(String p_to_file) {
  159. ERR_FAIL_COND(p_to_file!="" && _OSPRF);
  160. if (p_to_file!="") {
  161. Error err;
  162. _OSPRF=FileAccess::open(p_to_file,FileAccess::WRITE,&err);
  163. if (err!=OK) {
  164. _OSPRF=NULL;
  165. ERR_FAIL_COND(err!=OK);
  166. }
  167. }
  168. ObjectDB::debug_objects(_OS_printres);
  169. if (p_to_file!="") {
  170. if (_OSPRF)
  171. memdelete(_OSPRF);
  172. _OSPRF=NULL;
  173. }
  174. }
  175. void OS::print_resources_in_use(bool p_short) {
  176. ResourceCache::dump(NULL,p_short);
  177. }
  178. void OS::dump_resources_to_file(const char* p_file) {
  179. ResourceCache::dump(p_file);
  180. }
  181. void OS::clear_last_error() {
  182. GLOBAL_LOCK_FUNCTION
  183. if (last_error)
  184. memfree(last_error);
  185. last_error=NULL;
  186. }
  187. void OS::set_frame_delay(uint32_t p_msec) {
  188. _frame_delay=p_msec;
  189. }
  190. uint32_t OS::get_frame_delay() const {
  191. return _frame_delay;
  192. }
  193. void OS::set_no_window_mode(bool p_enable) {
  194. _no_window=p_enable;
  195. }
  196. bool OS::is_no_window_mode_enabled() const {
  197. return _no_window;
  198. }
  199. int OS::get_exit_code() const {
  200. return _exit_code;
  201. }
  202. void OS::set_exit_code(int p_code) {
  203. _exit_code=p_code;
  204. }
  205. String OS::get_locale() const {
  206. return "en";
  207. }
  208. String OS::get_resource_dir() const {
  209. return Globals::get_singleton()->get_resource_path();
  210. }
  211. String OS::get_system_dir(SystemDir p_dir) const {
  212. return ".";
  213. }
  214. String OS::get_safe_application_name() const {
  215. String an = Globals::get_singleton()->get("application/name");
  216. Vector<String> invalid_char = String("\\ / : * ? \" < > |").split(" ");
  217. for (int i=0;i<invalid_char.size();i++) {
  218. an = an.replace(invalid_char[i],"-");
  219. }
  220. return an;
  221. }
  222. String OS::get_data_dir() const {
  223. return ".";
  224. };
  225. Error OS::shell_open(String p_uri) {
  226. return ERR_UNAVAILABLE;
  227. };
  228. // implement these with the canvas?
  229. Error OS::dialog_show(String p_title, String p_description, Vector<String> p_buttons, Object* p_obj, String p_callback) {
  230. while (true) {
  231. print("%ls\n--------\n%ls\n", p_title.c_str(), p_description.c_str());
  232. for (int i=0; i<p_buttons.size(); i++) {
  233. if (i>0) print(", ");
  234. print("%i=%ls", i+1, p_buttons[i].c_str());
  235. };
  236. print("\n");
  237. String res = get_stdin_string().strip_edges();
  238. if (!res.is_numeric())
  239. continue;
  240. int n = res.to_int();
  241. if (n < 0 || n >= p_buttons.size())
  242. continue;
  243. if (p_obj && p_callback != "")
  244. p_obj->call_deferred(p_callback, n);
  245. break;
  246. };
  247. return OK;
  248. };
  249. Error OS::dialog_input_text(String p_title, String p_description, String p_partial, Object* p_obj, String p_callback) {
  250. ERR_FAIL_COND_V(!p_obj, FAILED);
  251. ERR_FAIL_COND_V(p_callback == "", FAILED);
  252. print("%ls\n---------\n%ls\n[%ls]:\n", p_title.c_str(), p_description.c_str(), p_partial.c_str());
  253. String res = get_stdin_string().strip_edges();
  254. bool success = true;
  255. if (res == "") {
  256. res = p_partial;
  257. };
  258. p_obj->call_deferred(p_callback, success, res);
  259. return OK;
  260. };
  261. int OS::get_static_memory_usage() const {
  262. return Memory::get_static_mem_usage();
  263. }
  264. int OS::get_dynamic_memory_usage() const{
  265. return Memory::get_dynamic_mem_usage();
  266. }
  267. int OS::get_static_memory_peak_usage() const {
  268. return Memory::get_static_mem_max_usage();
  269. }
  270. Error OS::set_cwd(const String& p_cwd) {
  271. return ERR_CANT_OPEN;
  272. }
  273. bool OS::has_touchscreen_ui_hint() const {
  274. //return false;
  275. return Input::get_singleton() && Input::get_singleton()->is_emulating_touchscreen();
  276. }
  277. int OS::get_free_static_memory() const {
  278. return Memory::get_static_mem_available();
  279. }
  280. void OS::yield() {
  281. }
  282. void OS::set_screen_orientation(ScreenOrientation p_orientation) {
  283. _orientation=p_orientation;
  284. }
  285. OS::ScreenOrientation OS::get_screen_orientation() const {
  286. return (OS::ScreenOrientation)_orientation;
  287. }
  288. void OS::_ensure_data_dir() {
  289. String dd = get_data_dir();
  290. DirAccess *da = DirAccess::open(dd);
  291. if (da) {
  292. memdelete(da);
  293. return;
  294. }
  295. da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  296. Error err = da->make_dir_recursive(dd);
  297. if (err!=OK) {
  298. ERR_EXPLAIN("Error attempting to create data dir: "+dd);
  299. }
  300. ERR_FAIL_COND(err!=OK);
  301. memdelete(da);
  302. }
  303. void OS::set_icon(const Image& p_icon) {
  304. }
  305. String OS::get_model_name() const {
  306. return "GenericDevice";
  307. }
  308. void OS::set_cmdline(const char* p_execpath, const List<String>& p_args) {
  309. _execpath = p_execpath;
  310. _cmdline = p_args;
  311. };
  312. void OS::release_rendering_thread() {
  313. }
  314. void OS::make_rendering_thread() {
  315. }
  316. void OS::swap_buffers() {
  317. }
  318. String OS::get_unique_ID() const {
  319. ERR_FAIL_V("");
  320. }
  321. int OS::get_processor_count() const {
  322. return 1;
  323. }
  324. Error OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  325. return FAILED;
  326. };
  327. bool OS::native_video_is_playing() const {
  328. return false;
  329. };
  330. void OS::native_video_pause() {
  331. };
  332. void OS::native_video_unpause() {
  333. };
  334. void OS::native_video_stop() {
  335. };
  336. void OS::set_mouse_mode(MouseMode p_mode) {
  337. }
  338. bool OS::can_use_threads() const {
  339. #ifdef NO_THREADS
  340. return false;
  341. #else
  342. return true;
  343. #endif
  344. }
  345. OS::MouseMode OS::get_mouse_mode() const{
  346. return MOUSE_MODE_VISIBLE;
  347. }
  348. void OS::set_time_scale(float p_scale) {
  349. _time_scale=p_scale;
  350. }
  351. OS::LatinKeyboardVariant OS::get_latin_keyboard_variant() const {
  352. return LATIN_KEYBOARD_QWERTY;
  353. }
  354. float OS::get_time_scale() const {
  355. return _time_scale;
  356. }
  357. bool OS::is_joy_known(int p_device) {
  358. return true;
  359. }
  360. String OS::get_joy_guid(int p_device) const {
  361. return "Default Joystick";
  362. }
  363. void OS::set_context(int p_context) {
  364. }
  365. void OS::set_use_vsync(bool p_enable) {
  366. }
  367. bool OS::is_vsnc_enabled() const{
  368. return true;
  369. }
  370. Dictionary OS::get_engine_version() const {
  371. Dictionary dict;
  372. dict["major"] = _MKSTR(VERSION_MAJOR);
  373. dict["minor"] = _MKSTR(VERSION_MINOR);
  374. #ifdef VERSION_PATCH
  375. dict["patch"] = _MKSTR(VERSION_PATCH);
  376. #else
  377. dict["patch"] = "";
  378. #endif
  379. dict["status"] = _MKSTR(VERSION_STATUS);
  380. dict["revision"] = _MKSTR(VERSION_REVISION);
  381. String stringver = String(dict["major"]) + "." + String(dict["minor"]);
  382. if (dict["patch"] != "")
  383. stringver += "." + String(dict["patch"]);
  384. stringver += "-" + String(dict["status"]) + " (" + String(dict["revision"]) + ")";
  385. dict["string"] = stringver;
  386. return dict;
  387. }
  388. OS::OS() {
  389. last_error=NULL;
  390. frames_drawn=0;
  391. singleton=this;
  392. ips=60;
  393. _keep_screen_on=true; // set default value to true, because this had been true before godot 2.0.
  394. low_processor_usage_mode=false;
  395. _verbose_stdout=false;
  396. _frame_delay=0;
  397. _no_window=false;
  398. _exit_code=0;
  399. _orientation=SCREEN_LANDSCAPE;
  400. _fps=1;
  401. _target_fps=0;
  402. _render_thread_mode=RENDER_THREAD_SAFE;
  403. _time_scale=1.0;
  404. _pixel_snap=false;
  405. _allow_hidpi=true;
  406. Math::seed(1234567);
  407. }
  408. OS::~OS() {
  409. singleton=NULL;
  410. }