2
0

os.cpp 10 KB

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