os.cpp 10 KB

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