1 /*************************************************************************/
2 /*  os.cpp                                                               */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 #include "os.h"
31 #include "dir_access.h"
32 #include "globals.h"
33 #include "input.h"
34 #include "os/file_access.h"
35 #include <stdarg.h>
36 // For get_engine_version, could be removed if it's moved to a new Engine singleton
37 #include "version.h"
38 
39 OS *OS::singleton = NULL;
40 
get_singleton()41 OS *OS::get_singleton() {
42 
43 	return singleton;
44 }
45 
get_ticks_msec() const46 uint32_t OS::get_ticks_msec() const {
47 	return get_ticks_usec() / 1000;
48 }
49 
get_splash_tick_msec() const50 uint64_t OS::get_splash_tick_msec() const {
51 	return _msec_splash;
52 }
get_unix_time() const53 uint64_t OS::get_unix_time() const {
54 
55 	return 0;
56 };
get_system_time_secs() const57 uint64_t OS::get_system_time_secs() const {
58 	return 0;
59 }
debug_break()60 void OS::debug_break(){
61 
62 	// something
63 };
64 
print_error(const char * p_function,const char * p_file,int p_line,const char * p_code,const char * p_rationale,ErrorType p_type)65 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) {
66 
67 	const char *err_type;
68 	switch (p_type) {
69 		case ERR_ERROR: err_type = "**ERROR**"; break;
70 		case ERR_WARNING: err_type = "**WARNING**"; break;
71 		case ERR_SCRIPT: err_type = "**SCRIPT ERROR**"; break;
72 	}
73 
74 	if (p_rationale && *p_rationale)
75 		print("%s: %s\n ", err_type, p_rationale);
76 	print("%s: At: %s:%i:%s() - %s\n", err_type, p_file, p_line, p_function, p_code);
77 }
78 
print(const char * p_format,...)79 void OS::print(const char *p_format, ...) {
80 
81 	va_list argp;
82 	va_start(argp, p_format);
83 
84 	vprint(p_format, argp);
85 
86 	va_end(argp);
87 };
88 
printerr(const char * p_format,...)89 void OS::printerr(const char *p_format, ...) {
90 
91 	va_list argp;
92 	va_start(argp, p_format);
93 
94 	vprint(p_format, argp, true);
95 
96 	va_end(argp);
97 };
98 
set_iterations_per_second(int p_ips)99 void OS::set_iterations_per_second(int p_ips) {
100 
101 	ips = p_ips;
102 }
get_iterations_per_second() const103 int OS::get_iterations_per_second() const {
104 
105 	return ips;
106 }
107 
set_target_fps(int p_fps)108 void OS::set_target_fps(int p_fps) {
109 	_target_fps = p_fps > 0 ? p_fps : 0;
110 }
111 
get_target_fps() const112 float OS::get_target_fps() const {
113 	return _target_fps;
114 }
115 
set_keep_screen_on(bool p_enabled)116 void OS::set_keep_screen_on(bool p_enabled) {
117 	_keep_screen_on = p_enabled;
118 }
119 
is_keep_screen_on() const120 bool OS::is_keep_screen_on() const {
121 	return _keep_screen_on;
122 }
123 
set_low_processor_usage_mode(bool p_enabled)124 void OS::set_low_processor_usage_mode(bool p_enabled) {
125 
126 	low_processor_usage_mode = p_enabled;
127 }
128 
is_in_low_processor_usage_mode() const129 bool OS::is_in_low_processor_usage_mode() const {
130 
131 	return low_processor_usage_mode;
132 }
133 
set_clipboard(const String & p_text)134 void OS::set_clipboard(const String &p_text) {
135 
136 	_local_clipboard = p_text;
137 }
get_clipboard() const138 String OS::get_clipboard() const {
139 
140 	return _local_clipboard;
141 }
142 
get_executable_path() const143 String OS::get_executable_path() const {
144 
145 	return _execpath;
146 }
147 
get_process_ID() const148 int OS::get_process_ID() const {
149 
150 	return -1;
151 };
152 
get_frames_drawn()153 uint64_t OS::get_frames_drawn() {
154 
155 	return frames_drawn;
156 }
157 
is_stdout_verbose() const158 bool OS::is_stdout_verbose() const {
159 
160 	return _verbose_stdout;
161 }
162 
set_last_error(const char * p_error)163 void OS::set_last_error(const char *p_error) {
164 
165 	GLOBAL_LOCK_FUNCTION
166 	if (p_error == NULL)
167 		p_error = "Unknown Error";
168 
169 	if (last_error)
170 		memfree(last_error);
171 	last_error = NULL;
172 	int len = 0;
173 	while (p_error[len++])
174 		;
175 
176 	last_error = (char *)memalloc(len);
177 	for (int i = 0; i < len; i++)
178 		last_error[i] = p_error[i];
179 }
180 
get_last_error() const181 const char *OS::get_last_error() const {
182 	GLOBAL_LOCK_FUNCTION
183 	return last_error ? last_error : "";
184 }
185 
dump_memory_to_file(const char * p_file)186 void OS::dump_memory_to_file(const char *p_file) {
187 
188 	Memory::dump_static_mem_to_file(p_file);
189 }
190 
191 static FileAccess *_OSPRF = NULL;
192 
_OS_printres(Object * p_obj)193 static void _OS_printres(Object *p_obj) {
194 
195 	Resource *res = p_obj->cast_to<Resource>();
196 	if (!res)
197 		return;
198 
199 	String str = itos(res->get_instance_ID()) + String(res->get_type()) + ":" + String(res->get_name()) + " - " + res->get_path();
200 	if (_OSPRF)
201 		_OSPRF->store_line(str);
202 	else
203 		print_line(str);
204 }
205 
has_virtual_keyboard() const206 bool OS::has_virtual_keyboard() const {
207 
208 	return false;
209 }
210 
show_virtual_keyboard(const String & p_existing_text,const Rect2 & p_screen_rect)211 void OS::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) {
212 }
213 
hide_virtual_keyboard()214 void OS::hide_virtual_keyboard() {
215 }
216 
print_all_resources(String p_to_file)217 void OS::print_all_resources(String p_to_file) {
218 
219 	ERR_FAIL_COND(p_to_file != "" && _OSPRF);
220 	if (p_to_file != "") {
221 
222 		Error err;
223 		_OSPRF = FileAccess::open(p_to_file, FileAccess::WRITE, &err);
224 		if (err != OK) {
225 			_OSPRF = NULL;
226 			ERR_FAIL_COND(err != OK);
227 		}
228 	}
229 
230 	ObjectDB::debug_objects(_OS_printres);
231 
232 	if (p_to_file != "") {
233 
234 		if (_OSPRF)
235 			memdelete(_OSPRF);
236 		_OSPRF = NULL;
237 	}
238 }
239 
print_resources_in_use(bool p_short)240 void OS::print_resources_in_use(bool p_short) {
241 
242 	ResourceCache::dump(NULL, p_short);
243 }
244 
dump_resources_to_file(const char * p_file)245 void OS::dump_resources_to_file(const char *p_file) {
246 
247 	ResourceCache::dump(p_file);
248 }
249 
clear_last_error()250 void OS::clear_last_error() {
251 
252 	GLOBAL_LOCK_FUNCTION
253 	if (last_error)
254 		memfree(last_error);
255 	last_error = NULL;
256 }
set_frame_delay(uint32_t p_msec)257 void OS::set_frame_delay(uint32_t p_msec) {
258 
259 	_frame_delay = p_msec;
260 }
261 
get_frame_delay() const262 uint32_t OS::get_frame_delay() const {
263 
264 	return _frame_delay;
265 }
266 
set_no_window_mode(bool p_enable)267 void OS::set_no_window_mode(bool p_enable) {
268 
269 	_no_window = p_enable;
270 }
271 
is_no_window_mode_enabled() const272 bool OS::is_no_window_mode_enabled() const {
273 
274 	return _no_window;
275 }
276 
get_exit_code() const277 int OS::get_exit_code() const {
278 
279 	return _exit_code;
280 }
set_exit_code(int p_code)281 void OS::set_exit_code(int p_code) {
282 
283 	_exit_code = p_code;
284 }
285 
get_locale() const286 String OS::get_locale() const {
287 
288 	return "en";
289 }
290 
get_resource_dir() const291 String OS::get_resource_dir() const {
292 
293 	return Globals::get_singleton()->get_resource_path();
294 }
295 
get_system_dir(SystemDir p_dir) const296 String OS::get_system_dir(SystemDir p_dir) const {
297 
298 	return ".";
299 }
300 
get_safe_application_name() const301 String OS::get_safe_application_name() const {
302 	String an = Globals::get_singleton()->get("application/name");
303 	Vector<String> invalid_char = String("\\ / : * ? \" < > |").split(" ");
304 	for (int i = 0; i < invalid_char.size(); i++) {
305 		an = an.replace(invalid_char[i], "-");
306 	}
307 	return an;
308 }
309 
get_data_dir() const310 String OS::get_data_dir() const {
311 
312 	return ".";
313 };
314 
shell_open(String p_uri)315 Error OS::shell_open(String p_uri) {
316 	return ERR_UNAVAILABLE;
317 };
318 
319 // implement these with the canvas?
dialog_show(String p_title,String p_description,Vector<String> p_buttons,Object * p_obj,String p_callback)320 Error OS::dialog_show(String p_title, String p_description, Vector<String> p_buttons, Object *p_obj, String p_callback) {
321 
322 	while (true) {
323 
324 		print("%ls\n--------\n%ls\n", p_title.c_str(), p_description.c_str());
325 		for (int i = 0; i < p_buttons.size(); i++) {
326 			if (i > 0) print(", ");
327 			print("%i=%ls", i + 1, p_buttons[i].c_str());
328 		};
329 		print("\n");
330 		String res = get_stdin_string().strip_edges();
331 		if (!res.is_numeric())
332 			continue;
333 		int n = res.to_int();
334 		if (n < 0 || n >= p_buttons.size())
335 			continue;
336 		if (p_obj && p_callback != "")
337 			p_obj->call_deferred(p_callback, n);
338 		break;
339 	};
340 	return OK;
341 };
342 
dialog_input_text(String p_title,String p_description,String p_partial,Object * p_obj,String p_callback)343 Error OS::dialog_input_text(String p_title, String p_description, String p_partial, Object *p_obj, String p_callback) {
344 
345 	ERR_FAIL_COND_V(!p_obj, FAILED);
346 	ERR_FAIL_COND_V(p_callback == "", FAILED);
347 	print("%ls\n---------\n%ls\n[%ls]:\n", p_title.c_str(), p_description.c_str(), p_partial.c_str());
348 
349 	String res = get_stdin_string().strip_edges();
350 	bool success = true;
351 	if (res == "") {
352 		res = p_partial;
353 	};
354 
355 	p_obj->call_deferred(p_callback, success, res);
356 
357 	return OK;
358 };
359 
get_static_memory_usage() const360 int OS::get_static_memory_usage() const {
361 
362 	return Memory::get_static_mem_usage();
363 }
get_dynamic_memory_usage() const364 int OS::get_dynamic_memory_usage() const {
365 
366 	return Memory::get_dynamic_mem_usage();
367 }
368 
get_static_memory_peak_usage() const369 int OS::get_static_memory_peak_usage() const {
370 
371 	return Memory::get_static_mem_max_usage();
372 }
373 
set_cwd(const String & p_cwd)374 Error OS::set_cwd(const String &p_cwd) {
375 
376 	return ERR_CANT_OPEN;
377 }
378 
has_touchscreen_ui_hint() const379 bool OS::has_touchscreen_ui_hint() const {
380 
381 	//return false;
382 	return Input::get_singleton() && Input::get_singleton()->is_emulating_touchscreen();
383 }
384 
get_free_static_memory() const385 int OS::get_free_static_memory() const {
386 
387 	return Memory::get_static_mem_available();
388 }
389 
yield()390 void OS::yield() {
391 }
392 
set_screen_orientation(ScreenOrientation p_orientation)393 void OS::set_screen_orientation(ScreenOrientation p_orientation) {
394 
395 	_orientation = p_orientation;
396 }
397 
get_screen_orientation() const398 OS::ScreenOrientation OS::get_screen_orientation() const {
399 
400 	return (OS::ScreenOrientation)_orientation;
401 }
402 
_ensure_data_dir()403 void OS::_ensure_data_dir() {
404 
405 	String dd = get_data_dir();
406 	DirAccess *da = DirAccess::open(dd);
407 	if (da) {
408 		memdelete(da);
409 		return;
410 	}
411 
412 	da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
413 	Error err = da->make_dir_recursive(dd);
414 	if (err != OK) {
415 		ERR_EXPLAIN("Error attempting to create data dir: " + dd);
416 	}
417 	ERR_FAIL_COND(err != OK);
418 
419 	memdelete(da);
420 }
421 
set_icon(const Image & p_icon)422 void OS::set_icon(const Image &p_icon) {
423 }
424 
get_model_name() const425 String OS::get_model_name() const {
426 
427 	return "GenericDevice";
428 }
429 
set_cmdline(const char * p_execpath,const List<String> & p_args)430 void OS::set_cmdline(const char *p_execpath, const List<String> &p_args) {
431 
432 	_execpath = p_execpath;
433 	_cmdline = p_args;
434 };
435 
release_rendering_thread()436 void OS::release_rendering_thread() {
437 }
438 
make_rendering_thread()439 void OS::make_rendering_thread() {
440 }
441 
swap_buffers()442 void OS::swap_buffers() {
443 }
444 
get_unique_ID() const445 String OS::get_unique_ID() const {
446 
447 	ERR_FAIL_V("");
448 }
449 
get_processor_count() const450 int OS::get_processor_count() const {
451 
452 	return 1;
453 }
454 
native_video_play(String p_path,float p_volume,String p_audio_track,String p_subtitle_track)455 Error OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
456 
457 	return FAILED;
458 };
459 
native_video_is_playing() const460 bool OS::native_video_is_playing() const {
461 
462 	return false;
463 };
464 
native_video_pause()465 void OS::native_video_pause(){
466 
467 };
468 
native_video_unpause()469 void OS::native_video_unpause(){
470 
471 };
472 
native_video_stop()473 void OS::native_video_stop(){
474 
475 };
476 
set_mouse_mode(MouseMode p_mode)477 void OS::set_mouse_mode(MouseMode p_mode) {
478 }
479 
can_use_threads() const480 bool OS::can_use_threads() const {
481 
482 #ifdef NO_THREADS
483 	return false;
484 #else
485 	return true;
486 #endif
487 }
488 
get_mouse_mode() const489 OS::MouseMode OS::get_mouse_mode() const {
490 
491 	return MOUSE_MODE_VISIBLE;
492 }
493 
set_time_scale(float p_scale)494 void OS::set_time_scale(float p_scale) {
495 
496 	_time_scale = p_scale;
497 }
498 
get_latin_keyboard_variant() const499 OS::LatinKeyboardVariant OS::get_latin_keyboard_variant() const {
500 
501 	return LATIN_KEYBOARD_QWERTY;
502 }
503 
get_time_scale() const504 float OS::get_time_scale() const {
505 
506 	return _time_scale;
507 }
508 
is_joy_known(int p_device)509 bool OS::is_joy_known(int p_device) {
510 	return true;
511 }
512 
get_joy_guid(int p_device) const513 String OS::get_joy_guid(int p_device) const {
514 	return "Default Joystick";
515 }
516 
set_context(int p_context)517 void OS::set_context(int p_context) {
518 }
set_use_vsync(bool p_enable)519 void OS::set_use_vsync(bool p_enable) {
520 }
521 
is_vsync_enabled() const522 bool OS::is_vsync_enabled() const {
523 
524 	return true;
525 }
526 
get_engine_version() const527 Dictionary OS::get_engine_version() const {
528 
529 	Dictionary dict;
530 	dict["major"] = _MKSTR(VERSION_MAJOR);
531 	dict["minor"] = _MKSTR(VERSION_MINOR);
532 #ifdef VERSION_PATCH
533 	dict["patch"] = _MKSTR(VERSION_PATCH);
534 #else
535 	dict["patch"] = "";
536 #endif
537 	dict["status"] = _MKSTR(VERSION_STATUS);
538 	dict["revision"] = _MKSTR(VERSION_REVISION);
539 
540 	String stringver = String(dict["major"]) + "." + String(dict["minor"]);
541 	if (dict["patch"] != "")
542 		stringver += "." + String(dict["patch"]);
543 	stringver += "-" + String(dict["status"]) + " (" + String(dict["revision"]) + ")";
544 	dict["string"] = stringver;
545 
546 	return dict;
547 }
548 
center_window()549 void OS::center_window() {
550 
551 	if (is_window_fullscreen()) return;
552 
553 	Size2 scr = get_screen_size(get_current_screen());
554 	Size2 wnd = get_real_window_size();
555 	int x = scr.width / 2 - wnd.width / 2;
556 	int y = scr.height / 2 - wnd.height / 2;
557 	set_window_position(Vector2(x, y));
558 }
559 
OS()560 OS::OS() {
561 	last_error = NULL;
562 	frames_drawn = 0;
563 	singleton = this;
564 	ips = 60;
565 	_keep_screen_on = true; // set default value to true, because this had been true before godot 2.0.
566 	low_processor_usage_mode = false;
567 	_verbose_stdout = false;
568 	_frame_delay = 0;
569 	_no_window = false;
570 	_exit_code = 0;
571 	_orientation = SCREEN_LANDSCAPE;
572 	_fps = 1;
573 	_target_fps = 0;
574 	_render_thread_mode = RENDER_THREAD_SAFE;
575 	_time_scale = 1.0;
576 	_pixel_snap = false;
577 	_allow_hidpi = true;
578 	Math::seed(1234567);
579 }
580 
~OS()581 OS::~OS() {
582 
583 	singleton = NULL;
584 }
585