os.cpp 9.2 KB

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