2
0

os.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*************************************************************************/
  2. /* os.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #ifndef OS_H
  31. #define OS_H
  32. #include "core/config/engine.h"
  33. #include "core/io/image.h"
  34. #include "core/io/logger.h"
  35. #include "core/os/main_loop.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. bool _keep_screen_on = true; // set default value to true, because this had been true before godot 2.0.
  47. bool low_processor_usage_mode = false;
  48. int low_processor_usage_mode_sleep_usec = 10000;
  49. bool _verbose_stdout = false;
  50. bool _debug_stdout = false;
  51. bool _single_window = false;
  52. String _local_clipboard;
  53. int _exit_code = EXIT_FAILURE; // unexpected exit is marked as failure
  54. int _orientation;
  55. bool _allow_hidpi = false;
  56. bool _allow_layered = false;
  57. bool _stdout_enabled = true;
  58. bool _stderr_enabled = true;
  59. char *last_error;
  60. CompositeLogger *_logger = nullptr;
  61. bool restart_on_exit = false;
  62. List<String> restart_commandline;
  63. // for the user interface we keep a record of the current display driver
  64. // so we can retrieve the rendering drivers available
  65. int _display_driver_id = -1;
  66. String _current_rendering_driver_name = "";
  67. protected:
  68. void _set_logger(CompositeLogger *p_logger);
  69. public:
  70. typedef void (*ImeCallback)(void *p_inp, String p_text, Point2 p_selection);
  71. typedef bool (*HasServerFeatureCallback)(const String &p_feature);
  72. enum RenderThreadMode {
  73. RENDER_THREAD_UNSAFE,
  74. RENDER_THREAD_SAFE,
  75. RENDER_SEPARATE_THREAD
  76. };
  77. enum RenderMainThreadMode {
  78. RENDER_MAIN_THREAD_ONLY,
  79. RENDER_ANY_THREAD,
  80. };
  81. protected:
  82. friend class Main;
  83. // Needed by tests to setup command-line args.
  84. friend int test_main(int argc, char *argv[]);
  85. HasServerFeatureCallback has_server_feature_callback = nullptr;
  86. RenderThreadMode _render_thread_mode = RENDER_THREAD_SAFE;
  87. RenderMainThreadMode _render_main_thread_mode = RENDER_ANY_THREAD;
  88. // Functions used by Main to initialize/deinitialize the OS.
  89. void add_logger(Logger *p_logger);
  90. virtual void initialize() = 0;
  91. virtual void initialize_joypads() = 0;
  92. void set_current_rendering_driver_name(String p_driver_name) { _current_rendering_driver_name = p_driver_name; }
  93. void set_display_driver_id(int p_display_driver_id) { _display_driver_id = p_display_driver_id; }
  94. virtual void set_main_loop(MainLoop *p_main_loop) = 0;
  95. virtual void delete_main_loop() = 0;
  96. virtual void finalize() = 0;
  97. virtual void finalize_core() = 0;
  98. virtual void set_cmdline(const char *p_execpath, const List<String> &p_args);
  99. virtual bool _check_internal_feature_support(const String &p_feature) = 0;
  100. public:
  101. typedef int64_t ProcessID;
  102. static OS *get_singleton();
  103. String get_current_rendering_driver_name() const { return _current_rendering_driver_name; }
  104. int get_display_driver_id() const { return _display_driver_id; }
  105. 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);
  106. void print(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
  107. void printerr(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
  108. virtual String get_stdin_string(bool p_block = true) = 0;
  109. virtual PackedStringArray get_connected_midi_inputs();
  110. virtual void open_midi_inputs();
  111. virtual void close_midi_inputs();
  112. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  113. virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false) { return ERR_UNAVAILABLE; }
  114. virtual Error close_dynamic_library(void *p_library_handle) { return ERR_UNAVAILABLE; }
  115. 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; }
  116. virtual void set_low_processor_usage_mode(bool p_enabled);
  117. virtual bool is_in_low_processor_usage_mode() const;
  118. virtual void set_low_processor_usage_mode_sleep_usec(int p_usec);
  119. virtual int get_low_processor_usage_mode_sleep_usec() const;
  120. virtual String get_executable_path() const;
  121. 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) = 0;
  122. virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr) = 0;
  123. 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); };
  124. virtual Error kill(const ProcessID &p_pid) = 0;
  125. virtual int get_process_id() const;
  126. virtual void vibrate_handheld(int p_duration_ms = 500);
  127. virtual Error shell_open(String p_uri);
  128. virtual Error set_cwd(const String &p_cwd);
  129. virtual bool has_environment(const String &p_var) const = 0;
  130. virtual String get_environment(const String &p_var) const = 0;
  131. virtual bool set_environment(const String &p_var, const String &p_value) const = 0;
  132. virtual String get_name() const = 0;
  133. virtual List<String> get_cmdline_args() const { return _cmdline; }
  134. virtual String get_model_name() const;
  135. bool is_layered_allowed() const { return _allow_layered; }
  136. bool is_hidpi_allowed() const { return _allow_hidpi; }
  137. void ensure_user_data_dir();
  138. virtual MainLoop *get_main_loop() const = 0;
  139. virtual void yield();
  140. enum Weekday : uint8_t {
  141. WEEKDAY_SUNDAY,
  142. WEEKDAY_MONDAY,
  143. WEEKDAY_TUESDAY,
  144. WEEKDAY_WEDNESDAY,
  145. WEEKDAY_THURSDAY,
  146. WEEKDAY_FRIDAY,
  147. WEEKDAY_SATURDAY,
  148. };
  149. enum Month : uint8_t {
  150. /// Start at 1 to follow Windows SYSTEMTIME structure
  151. /// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v=vs.85).aspx
  152. MONTH_JANUARY = 1,
  153. MONTH_FEBRUARY,
  154. MONTH_MARCH,
  155. MONTH_APRIL,
  156. MONTH_MAY,
  157. MONTH_JUNE,
  158. MONTH_JULY,
  159. MONTH_AUGUST,
  160. MONTH_SEPTEMBER,
  161. MONTH_OCTOBER,
  162. MONTH_NOVEMBER,
  163. MONTH_DECEMBER,
  164. };
  165. struct Date {
  166. int64_t year;
  167. Month month;
  168. uint8_t day;
  169. Weekday weekday;
  170. bool dst;
  171. };
  172. struct Time {
  173. uint8_t hour;
  174. uint8_t minute;
  175. uint8_t second;
  176. };
  177. struct TimeZoneInfo {
  178. int bias;
  179. String name;
  180. };
  181. virtual Date get_date(bool p_utc = false) const = 0;
  182. virtual Time get_time(bool p_utc = false) const = 0;
  183. virtual TimeZoneInfo get_time_zone_info() const = 0;
  184. virtual double get_unix_time() const;
  185. virtual void delay_usec(uint32_t p_usec) const = 0;
  186. virtual void add_frame_delay(bool p_can_draw);
  187. virtual uint64_t get_ticks_usec() const = 0;
  188. uint64_t get_ticks_msec() const;
  189. virtual bool is_userfs_persistent() const { return true; }
  190. bool is_stdout_verbose() const;
  191. bool is_stdout_debug_enabled() const;
  192. bool is_stdout_enabled() const;
  193. bool is_stderr_enabled() const;
  194. void set_stdout_enabled(bool p_enabled);
  195. void set_stderr_enabled(bool p_enabled);
  196. bool is_single_window() const;
  197. virtual void disable_crash_handler() {}
  198. virtual bool is_disable_crash_handler() const { return false; }
  199. virtual void initialize_debugging() {}
  200. virtual void dump_memory_to_file(const char *p_file);
  201. virtual void dump_resources_to_file(const char *p_file);
  202. virtual void print_resources_in_use(bool p_short = false);
  203. virtual void print_all_resources(String p_to_file = "");
  204. virtual uint64_t get_static_memory_usage() const;
  205. virtual uint64_t get_static_memory_peak_usage() const;
  206. virtual uint64_t get_free_static_memory() const;
  207. RenderThreadMode get_render_thread_mode() const { return _render_thread_mode; }
  208. RenderMainThreadMode get_render_main_thread_mode() const { return _render_main_thread_mode; }
  209. void set_render_main_thread_mode(RenderMainThreadMode p_thread_mode) { _render_main_thread_mode = p_thread_mode; }
  210. virtual String get_locale() const;
  211. String get_locale_language() const;
  212. String get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator = false) const;
  213. virtual String get_godot_dir_name() const;
  214. virtual String get_data_path() const;
  215. virtual String get_config_path() const;
  216. virtual String get_cache_path() const;
  217. virtual String get_bundle_resource_dir() const;
  218. virtual String get_bundle_icon_path() const;
  219. virtual String get_user_data_dir() const;
  220. virtual String get_resource_dir() const;
  221. enum SystemDir {
  222. SYSTEM_DIR_DESKTOP,
  223. SYSTEM_DIR_DCIM,
  224. SYSTEM_DIR_DOCUMENTS,
  225. SYSTEM_DIR_DOWNLOADS,
  226. SYSTEM_DIR_MOVIES,
  227. SYSTEM_DIR_MUSIC,
  228. SYSTEM_DIR_PICTURES,
  229. SYSTEM_DIR_RINGTONES,
  230. };
  231. virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
  232. virtual Error move_to_trash(const String &p_path) { return FAILED; }
  233. virtual void debug_break();
  234. virtual int get_exit_code() const;
  235. virtual void set_exit_code(int p_code);
  236. virtual int get_processor_count() const;
  237. virtual String get_unique_id() const;
  238. virtual bool can_use_threads() const;
  239. bool has_feature(const String &p_feature);
  240. void set_has_server_feature_callback(HasServerFeatureCallback p_callback);
  241. void set_restart_on_exit(bool p_restart, const List<String> &p_restart_arguments);
  242. bool is_restart_on_exit_set() const;
  243. List<String> get_restart_on_exit_arguments() const;
  244. virtual bool request_permission(const String &p_name) { return true; }
  245. virtual bool request_permissions() { return true; }
  246. virtual Vector<String> get_granted_permissions() const { return Vector<String>(); }
  247. virtual void process_and_drop_events() {}
  248. OS();
  249. virtual ~OS();
  250. };
  251. #endif // OS_H