os.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**************************************************************************/
  2. /* os.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef OS_H
  31. #define OS_H
  32. #include "core/config/engine.h"
  33. #include "core/io/logger.h"
  34. #include "core/io/remote_filesystem_client.h"
  35. #include "core/os/time_enums.h"
  36. #include "core/string/ustring.h"
  37. #include "core/templates/list.h"
  38. #include "core/templates/vector.h"
  39. #include <stdarg.h>
  40. #include <stdlib.h>
  41. class OS {
  42. static OS *singleton;
  43. static uint64_t target_ticks;
  44. String _execpath;
  45. List<String> _cmdline;
  46. List<String> _user_args;
  47. bool _keep_screen_on = true; // set default value to true, because this had been true before godot 2.0.
  48. bool low_processor_usage_mode = false;
  49. int low_processor_usage_mode_sleep_usec = 10000;
  50. bool _delta_smoothing_enabled = false;
  51. bool _verbose_stdout = false;
  52. bool _debug_stdout = false;
  53. String _local_clipboard;
  54. // Assume success by default, all failure cases need to set EXIT_FAILURE explicitly.
  55. int _exit_code = EXIT_SUCCESS;
  56. bool _allow_hidpi = false;
  57. bool _allow_layered = false;
  58. bool _stdout_enabled = true;
  59. bool _stderr_enabled = true;
  60. bool _writing_movie = false;
  61. bool _in_editor = false;
  62. CompositeLogger *_logger = nullptr;
  63. bool restart_on_exit = false;
  64. List<String> restart_commandline;
  65. // for the user interface we keep a record of the current display driver
  66. // so we can retrieve the rendering drivers available
  67. int _display_driver_id = -1;
  68. String _current_rendering_driver_name;
  69. String _current_rendering_method;
  70. bool _is_gles_over_gl = false;
  71. RemoteFilesystemClient default_rfs;
  72. // For tracking benchmark data
  73. bool use_benchmark = false;
  74. String benchmark_file;
  75. HashMap<Pair<String, String>, uint64_t, PairHash<String, String>> benchmark_marks_from;
  76. HashMap<Pair<String, String>, double, PairHash<String, String>> benchmark_marks_final;
  77. protected:
  78. void _set_logger(CompositeLogger *p_logger);
  79. public:
  80. typedef void (*ImeCallback)(void *p_inp, const String &p_text, Point2 p_selection);
  81. typedef bool (*HasServerFeatureCallback)(const String &p_feature);
  82. enum RenderThreadMode {
  83. RENDER_THREAD_UNSAFE,
  84. RENDER_THREAD_SAFE,
  85. RENDER_SEPARATE_THREAD,
  86. };
  87. enum StdHandleType {
  88. STD_HANDLE_INVALID,
  89. STD_HANDLE_CONSOLE,
  90. STD_HANDLE_FILE,
  91. STD_HANDLE_PIPE,
  92. STD_HANDLE_UNKNOWN,
  93. };
  94. protected:
  95. friend class Main;
  96. // Needed by tests to setup command-line args.
  97. friend int test_main(int argc, char *argv[]);
  98. HasServerFeatureCallback has_server_feature_callback = nullptr;
  99. RenderThreadMode _render_thread_mode = RENDER_THREAD_SAFE;
  100. // Functions used by Main to initialize/deinitialize the OS.
  101. void add_logger(Logger *p_logger);
  102. virtual void initialize() = 0;
  103. virtual void initialize_joypads() = 0;
  104. void set_display_driver_id(int p_display_driver_id) { _display_driver_id = p_display_driver_id; }
  105. virtual void set_main_loop(MainLoop *p_main_loop) = 0;
  106. virtual void delete_main_loop() = 0;
  107. virtual void finalize() = 0;
  108. virtual void finalize_core() = 0;
  109. virtual void set_cmdline(const char *p_execpath, const List<String> &p_args, const List<String> &p_user_args);
  110. virtual bool _check_internal_feature_support(const String &p_feature) = 0;
  111. public:
  112. typedef int64_t ProcessID;
  113. static OS *get_singleton();
  114. void set_current_rendering_driver_name(const String &p_driver_name) { _current_rendering_driver_name = p_driver_name; }
  115. void set_current_rendering_method(const String &p_name) { _current_rendering_method = p_name; }
  116. void set_gles_over_gl(bool p_enabled) { _is_gles_over_gl = p_enabled; }
  117. String get_current_rendering_driver_name() const { return _current_rendering_driver_name; }
  118. String get_current_rendering_method() const { return _current_rendering_method; }
  119. bool get_gles_over_gl() const { return _is_gles_over_gl; }
  120. int get_display_driver_id() const { return _display_driver_id; }
  121. virtual Vector<String> get_video_adapter_driver_info() const = 0;
  122. virtual bool get_user_prefers_integrated_gpu() const { return false; }
  123. void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify = false, Logger::ErrorType p_type = Logger::ERR_ERROR);
  124. void print(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
  125. void print_rich(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
  126. void printerr(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
  127. virtual String get_stdin_string(int64_t p_buffer_size = 1024) = 0;
  128. virtual PackedByteArray get_stdin_buffer(int64_t p_buffer_size = 1024) = 0;
  129. virtual StdHandleType get_stdin_type() const { return STD_HANDLE_UNKNOWN; }
  130. virtual StdHandleType get_stdout_type() const { return STD_HANDLE_UNKNOWN; }
  131. virtual StdHandleType get_stderr_type() const { return STD_HANDLE_UNKNOWN; }
  132. virtual Error get_entropy(uint8_t *r_buffer, int p_bytes) = 0; // Should return cryptographically-safe random bytes.
  133. virtual String get_system_ca_certificates() { return ""; } // Concatenated certificates in PEM format.
  134. virtual PackedStringArray get_connected_midi_inputs();
  135. virtual void open_midi_inputs();
  136. virtual void close_midi_inputs();
  137. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  138. struct GDExtensionData {
  139. bool also_set_library_path = false;
  140. String *r_resolved_path = nullptr;
  141. bool generate_temp_files = false;
  142. PackedStringArray *library_dependencies = nullptr;
  143. };
  144. virtual Error open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data = nullptr) { return ERR_UNAVAILABLE; }
  145. virtual Error close_dynamic_library(void *p_library_handle) { return ERR_UNAVAILABLE; }
  146. virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String &p_name, void *&p_symbol_handle, bool p_optional = false) { return ERR_UNAVAILABLE; }
  147. virtual void set_low_processor_usage_mode(bool p_enabled);
  148. virtual bool is_in_low_processor_usage_mode() const;
  149. virtual void set_low_processor_usage_mode_sleep_usec(int p_usec);
  150. virtual int get_low_processor_usage_mode_sleep_usec() const;
  151. void set_delta_smoothing(bool p_enabled);
  152. bool is_delta_smoothing_enabled() const;
  153. virtual Vector<String> get_system_fonts() const { return Vector<String>(); }
  154. virtual String get_system_font_path(const String &p_font_name, int p_weight = 400, int p_stretch = 100, bool p_italic = false) const { return String(); }
  155. virtual Vector<String> get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale = String(), const String &p_script = String(), int p_weight = 400, int p_stretch = 100, bool p_italic = false) const { return Vector<String>(); }
  156. virtual String get_executable_path() const;
  157. virtual Error execute(const String &p_path, const List<String> &p_arguments, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr, bool p_open_console = false) = 0;
  158. virtual Dictionary execute_with_pipe(const String &p_path, const List<String> &p_arguments, bool p_blocking = true) { return Dictionary(); }
  159. virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) = 0;
  160. virtual Error create_instance(const List<String> &p_arguments, ProcessID *r_child_id = nullptr) { return create_process(get_executable_path(), p_arguments, r_child_id); }
  161. virtual Error kill(const ProcessID &p_pid) = 0;
  162. virtual int get_process_id() const;
  163. virtual bool is_process_running(const ProcessID &p_pid) const = 0;
  164. virtual int get_process_exit_code(const ProcessID &p_pid) const = 0;
  165. virtual void vibrate_handheld(int p_duration_ms = 500, float p_amplitude = -1.0) {}
  166. virtual Error shell_open(const String &p_uri);
  167. virtual Error shell_show_in_file_manager(String p_path, bool p_open_folder = true);
  168. virtual Error set_cwd(const String &p_cwd);
  169. virtual bool has_environment(const String &p_var) const = 0;
  170. virtual String get_environment(const String &p_var) const = 0;
  171. virtual void set_environment(const String &p_var, const String &p_value) const = 0;
  172. virtual void unset_environment(const String &p_var) const = 0;
  173. virtual String get_name() const = 0;
  174. virtual String get_identifier() const;
  175. virtual String get_distribution_name() const = 0;
  176. virtual String get_version() const = 0;
  177. virtual List<String> get_cmdline_args() const { return _cmdline; }
  178. virtual List<String> get_cmdline_user_args() const { return _user_args; }
  179. virtual List<String> get_cmdline_platform_args() const { return List<String>(); }
  180. virtual String get_model_name() const;
  181. bool is_layered_allowed() const { return _allow_layered; }
  182. bool is_hidpi_allowed() const { return _allow_hidpi; }
  183. void ensure_user_data_dir();
  184. virtual MainLoop *get_main_loop() const = 0;
  185. virtual void yield();
  186. struct DateTime {
  187. int64_t year;
  188. Month month;
  189. uint8_t day;
  190. Weekday weekday;
  191. uint8_t hour;
  192. uint8_t minute;
  193. uint8_t second;
  194. bool dst;
  195. };
  196. struct TimeZoneInfo {
  197. int bias;
  198. String name;
  199. };
  200. virtual DateTime get_datetime(bool utc = false) const = 0;
  201. virtual TimeZoneInfo get_time_zone_info() const = 0;
  202. virtual double get_unix_time() const;
  203. virtual void delay_usec(uint32_t p_usec) const = 0;
  204. virtual void add_frame_delay(bool p_can_draw);
  205. virtual uint64_t get_ticks_usec() const = 0;
  206. uint64_t get_ticks_msec() const;
  207. virtual bool is_userfs_persistent() const { return true; }
  208. bool is_stdout_verbose() const;
  209. bool is_stdout_debug_enabled() const;
  210. bool is_stdout_enabled() const;
  211. bool is_stderr_enabled() const;
  212. void set_stdout_enabled(bool p_enabled);
  213. void set_stderr_enabled(bool p_enabled);
  214. virtual void disable_crash_handler() {}
  215. virtual bool is_disable_crash_handler() const { return false; }
  216. virtual void initialize_debugging() {}
  217. virtual uint64_t get_static_memory_usage() const;
  218. virtual uint64_t get_static_memory_peak_usage() const;
  219. virtual Dictionary get_memory_info() const;
  220. RenderThreadMode get_render_thread_mode() const { return _render_thread_mode; }
  221. virtual String get_locale() const;
  222. String get_locale_language() const;
  223. virtual uint64_t get_embedded_pck_offset() const;
  224. String get_safe_dir_name(const String &p_dir_name, bool p_allow_paths = false) const;
  225. virtual String get_godot_dir_name() const;
  226. virtual String get_data_path() const;
  227. virtual String get_config_path() const;
  228. virtual String get_cache_path() const;
  229. virtual String get_bundle_resource_dir() const;
  230. virtual String get_bundle_icon_path() const;
  231. virtual String get_user_data_dir() const;
  232. virtual String get_resource_dir() const;
  233. enum SystemDir {
  234. SYSTEM_DIR_DESKTOP,
  235. SYSTEM_DIR_DCIM,
  236. SYSTEM_DIR_DOCUMENTS,
  237. SYSTEM_DIR_DOWNLOADS,
  238. SYSTEM_DIR_MOVIES,
  239. SYSTEM_DIR_MUSIC,
  240. SYSTEM_DIR_PICTURES,
  241. SYSTEM_DIR_RINGTONES,
  242. };
  243. virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
  244. virtual Error move_to_trash(const String &p_path) { return FAILED; }
  245. virtual int get_exit_code() const;
  246. // `set_exit_code` should only be used from `SceneTree` (or from a similar
  247. // level, e.g. from the `Main::start` if leaving without creating a `SceneTree`).
  248. // For other components, `SceneTree.quit()` should be used instead.
  249. virtual void set_exit_code(int p_code);
  250. virtual int get_processor_count() const;
  251. virtual String get_processor_name() const;
  252. virtual int get_default_thread_pool_size() const { return get_processor_count(); }
  253. virtual String get_unique_id() const;
  254. bool has_feature(const String &p_feature);
  255. virtual bool is_sandboxed() const;
  256. void set_has_server_feature_callback(HasServerFeatureCallback p_callback);
  257. void set_restart_on_exit(bool p_restart, const List<String> &p_restart_arguments);
  258. bool is_restart_on_exit_set() const;
  259. List<String> get_restart_on_exit_arguments() const;
  260. virtual bool request_permission(const String &p_name) { return true; }
  261. virtual bool request_permissions() { return true; }
  262. virtual Vector<String> get_granted_permissions() const { return Vector<String>(); }
  263. virtual void revoke_granted_permissions() {}
  264. // For recording / measuring benchmark data. Only enabled with tools
  265. void set_use_benchmark(bool p_use_benchmark);
  266. bool is_use_benchmark_set();
  267. void set_benchmark_file(const String &p_benchmark_file);
  268. String get_benchmark_file();
  269. virtual void benchmark_begin_measure(const String &p_context, const String &p_what);
  270. virtual void benchmark_end_measure(const String &p_context, const String &p_what);
  271. virtual void benchmark_dump();
  272. virtual Error setup_remote_filesystem(const String &p_server_host, int p_port, const String &p_password, String &r_project_path);
  273. enum PreferredTextureFormat {
  274. PREFERRED_TEXTURE_FORMAT_S3TC_BPTC,
  275. PREFERRED_TEXTURE_FORMAT_ETC2_ASTC
  276. };
  277. virtual PreferredTextureFormat get_preferred_texture_format() const;
  278. // Load GDExtensions specific to this platform.
  279. // This is invoked by the GDExtensionManager after loading GDExtensions specified by the project.
  280. virtual void load_platform_gdextensions() const {}
  281. OS();
  282. virtual ~OS();
  283. };
  284. #endif // OS_H