os.cpp 9.7 KB

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