os.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*************************************************************************/
  2. /* os.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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. OS* OS::singleton=NULL;
  35. OS* OS::get_singleton() {
  36. return singleton;
  37. }
  38. uint32_t OS::get_ticks_msec() const
  39. { return get_ticks_usec()/1000; }
  40. uint64_t OS::get_splash_tick_msec() const {
  41. return _msec_splash;
  42. }
  43. uint64_t OS::get_unix_time() const {
  44. return 0;
  45. };
  46. void OS::debug_break() {
  47. // something
  48. };
  49. 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) {
  50. if (p_rationale && *p_rationale)
  51. print("**ERROR**: %s\n ",p_rationale);
  52. print("**ERROR**: At: %s:%i:%s() - %s\n",p_file,p_line,p_function,p_code);
  53. }
  54. void OS::print(const char* p_format, ...) {
  55. va_list argp;
  56. va_start(argp, p_format);
  57. vprint(p_format, argp);
  58. va_end(argp);
  59. };
  60. void OS::printerr(const char* p_format, ...) {
  61. va_list argp;
  62. va_start(argp, p_format);
  63. vprint(p_format, argp, true);
  64. va_end(argp);
  65. };
  66. void OS::set_iterations_per_second(int p_ips) {
  67. ips=p_ips;
  68. }
  69. int OS::get_iterations_per_second() const {
  70. return ips;
  71. }
  72. void OS::set_target_fps(int p_fps) {
  73. _target_fps=p_fps>0? p_fps : 0;
  74. }
  75. float OS::get_target_fps() const {
  76. return _target_fps;
  77. }
  78. void OS::set_low_processor_usage_mode(bool p_enabled) {
  79. low_processor_usage_mode=p_enabled;
  80. }
  81. bool OS::is_in_low_processor_usage_mode() const {
  82. return low_processor_usage_mode;
  83. }
  84. void OS::set_clipboard(const String& p_text) {
  85. _local_clipboard=p_text;
  86. }
  87. String OS::get_clipboard() const {
  88. return _local_clipboard;
  89. }
  90. String OS::get_executable_path() const {
  91. return _execpath;
  92. }
  93. int OS::get_process_ID() const {
  94. return -1;
  95. };
  96. uint64_t OS::get_frames_drawn() {
  97. return frames_drawn;
  98. }
  99. bool OS::is_stdout_verbose() const {
  100. return _verbose_stdout;
  101. }
  102. void OS::set_last_error(const char* p_error) {
  103. GLOBAL_LOCK_FUNCTION
  104. if (p_error==NULL)
  105. p_error="Unknown Error";
  106. if (last_error)
  107. memfree(last_error);
  108. last_error=NULL;
  109. int len =0;
  110. while(p_error[len++]);
  111. last_error=(char*)memalloc(len);
  112. for(int i=0;i<len;i++)
  113. last_error[i]=p_error[i];
  114. }
  115. const char *OS::get_last_error() const {
  116. GLOBAL_LOCK_FUNCTION
  117. return last_error?last_error:"";
  118. }
  119. void OS::dump_memory_to_file(const char* p_file) {
  120. Memory::dump_static_mem_to_file(p_file);
  121. }
  122. static FileAccess *_OSPRF=NULL;
  123. static void _OS_printres(Object *p_obj) {
  124. Resource *res = p_obj->cast_to<Resource>();
  125. if (!res)
  126. return;
  127. String str = itos(res->get_instance_ID())+String(res->get_type())+":"+String(res->get_name())+" - "+res->get_path();
  128. if (_OSPRF)
  129. _OSPRF->store_line(str);
  130. else
  131. print_line(str);
  132. }
  133. bool OS::has_virtual_keyboard() const {
  134. return false;
  135. }
  136. void OS::show_virtual_keyboard(const String& p_existing_text,const Rect2& p_screen_rect) {
  137. }
  138. void OS::hide_virtual_keyboard(){
  139. }
  140. void OS::print_all_resources(String p_to_file) {
  141. ERR_FAIL_COND(p_to_file!="" && _OSPRF);
  142. if (p_to_file!="") {
  143. Error err;
  144. _OSPRF=FileAccess::open(p_to_file,FileAccess::WRITE,&err);
  145. if (err!=OK) {
  146. _OSPRF=NULL;
  147. ERR_FAIL_COND(err!=OK);
  148. }
  149. }
  150. ObjectDB::debug_objects(_OS_printres);
  151. if (p_to_file!="") {
  152. if (_OSPRF)
  153. memdelete(_OSPRF);
  154. _OSPRF=NULL;
  155. }
  156. }
  157. void OS::print_resources_in_use(bool p_short) {
  158. ResourceCache::dump(NULL,p_short);
  159. }
  160. void OS::dump_resources_to_file(const char* p_file) {
  161. ResourceCache::dump(p_file);
  162. }
  163. void OS::clear_last_error() {
  164. GLOBAL_LOCK_FUNCTION
  165. if (last_error)
  166. memfree(last_error);
  167. last_error=NULL;
  168. }
  169. void OS::set_frame_delay(uint32_t p_msec) {
  170. _frame_delay=p_msec;
  171. }
  172. uint32_t OS::get_frame_delay() const {
  173. return _frame_delay;
  174. }
  175. void OS::set_no_window_mode(bool p_enable) {
  176. _no_window=p_enable;
  177. }
  178. bool OS::is_no_window_mode_enabled() const {
  179. return _no_window;
  180. }
  181. int OS::get_exit_code() const {
  182. return _exit_code;
  183. }
  184. void OS::set_exit_code(int p_code) {
  185. _exit_code=p_code;
  186. }
  187. String OS::get_locale() const {
  188. return "en";
  189. }
  190. String OS::get_resource_dir() const {
  191. return Globals::get_singleton()->get_resource_path();
  192. }
  193. String OS::get_system_dir(SystemDir p_dir) const {
  194. return ".";
  195. }
  196. String OS::get_data_dir() const {
  197. return ".";
  198. };
  199. Error OS::shell_open(String p_uri) {
  200. return ERR_UNAVAILABLE;
  201. };
  202. // implement these with the canvas?
  203. Error OS::dialog_show(String p_title, String p_description, Vector<String> p_buttons, Object* p_obj, String p_callback) {
  204. while (true) {
  205. print("%ls\n--------\n%ls\n", p_title.c_str(), p_description.c_str());
  206. for (int i=0; i<p_buttons.size(); i++) {
  207. if (i>0) print(", ");
  208. print("%i=%ls", i+1, p_buttons[i].c_str());
  209. };
  210. print("\n");
  211. String res = get_stdin_string().strip_edges();
  212. if (!res.is_numeric())
  213. continue;
  214. int n = res.to_int();
  215. if (n < 0 || n >= p_buttons.size())
  216. continue;
  217. if (p_obj && p_callback != "")
  218. p_obj->call_deferred(p_callback, n);
  219. break;
  220. };
  221. return OK;
  222. };
  223. Error OS::dialog_input_text(String p_title, String p_description, String p_partial, Object* p_obj, String p_callback) {
  224. ERR_FAIL_COND_V(!p_obj, FAILED);
  225. ERR_FAIL_COND_V(p_callback == "", FAILED);
  226. print("%ls\n---------\n%ls\n[%ls]:\n", p_title.c_str(), p_description.c_str(), p_partial.c_str());
  227. String res = get_stdin_string().strip_edges();
  228. bool success = true;
  229. if (res == "") {
  230. res = p_partial;
  231. };
  232. p_obj->call_deferred(p_callback, success, res);
  233. return OK;
  234. };
  235. int OS::get_static_memory_usage() const {
  236. return Memory::get_static_mem_usage();
  237. }
  238. int OS::get_dynamic_memory_usage() const{
  239. return Memory::get_dynamic_mem_usage();
  240. }
  241. int OS::get_static_memory_peak_usage() const {
  242. return Memory::get_static_mem_max_usage();
  243. }
  244. Error OS::set_cwd(const String& p_cwd) {
  245. return ERR_CANT_OPEN;
  246. }
  247. bool OS::has_touchscreen_ui_hint() const {
  248. //return false;
  249. return GLOBAL_DEF("display/emulate_touchscreen",false);
  250. }
  251. int OS::get_free_static_memory() const {
  252. return Memory::get_static_mem_available();
  253. }
  254. void OS::yield() {
  255. }
  256. void OS::set_screen_orientation(ScreenOrientation p_orientation) {
  257. _orientation=p_orientation;
  258. }
  259. OS::ScreenOrientation OS::get_screen_orientation() const {
  260. return (OS::ScreenOrientation)_orientation;
  261. }
  262. void OS::_ensure_data_dir() {
  263. String dd = get_data_dir();
  264. DirAccess *da = DirAccess::open(dd);
  265. if (da) {
  266. memdelete(da);
  267. return;
  268. }
  269. da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  270. Error err = da->make_dir_recursive(dd);
  271. if (err!=OK) {
  272. ERR_EXPLAIN("Error attempting to create data dir: "+dd);
  273. }
  274. ERR_FAIL_COND(err!=OK);
  275. memdelete(da);
  276. }
  277. void OS::set_icon(const Image& p_icon) {
  278. }
  279. String OS::get_model_name() const {
  280. return "GenericDevice";
  281. }
  282. void OS::set_cmdline(const char* p_execpath, const List<String>& p_args) {
  283. _execpath = p_execpath;
  284. _cmdline = p_args;
  285. };
  286. void OS::release_rendering_thread() {
  287. }
  288. void OS::make_rendering_thread() {
  289. }
  290. void OS::swap_buffers() {
  291. }
  292. String OS::get_unique_ID() const {
  293. ERR_FAIL_V("");
  294. }
  295. int OS::get_processor_count() const {
  296. return 1;
  297. }
  298. Error OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  299. return FAILED;
  300. };
  301. bool OS::native_video_is_playing() const {
  302. return false;
  303. };
  304. void OS::native_video_pause() {
  305. };
  306. void OS::native_video_stop() {
  307. };
  308. void OS::set_mouse_mode(MouseMode p_mode) {
  309. }
  310. bool OS::can_use_threads() const {
  311. #ifdef NO_THREADS
  312. return false;
  313. #else
  314. return true;
  315. #endif
  316. }
  317. OS::MouseMode OS::get_mouse_mode() const{
  318. return MOUSE_MODE_VISIBLE;
  319. }
  320. void OS::set_time_scale(float p_scale) {
  321. _time_scale=p_scale;
  322. }
  323. OS::LatinKeyboardVariant OS::get_latin_keyboard_variant() const {
  324. return LATIN_KEYBOARD_QWERTY;
  325. }
  326. float OS::get_time_scale() const {
  327. return _time_scale;
  328. }
  329. OS::OS() {
  330. last_error=NULL;
  331. frames_drawn=0;
  332. singleton=this;
  333. ips=60;
  334. low_processor_usage_mode=false;
  335. _verbose_stdout=false;
  336. _frame_delay=0;
  337. _no_window=false;
  338. _exit_code=0;
  339. _orientation=SCREEN_LANDSCAPE;
  340. _fps=1;
  341. _target_fps=0;
  342. _render_thread_mode=RENDER_THREAD_SAFE;
  343. _time_scale=1.0;
  344. Math::seed(1234567);
  345. }
  346. OS::~OS() {
  347. singleton=NULL;
  348. }