os.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*************************************************************************/
  2. /* os.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/engine.h"
  33. #include "core/image.h"
  34. #include "core/io/logger.h"
  35. #include "core/list.h"
  36. #include "core/os/main_loop.h"
  37. #include "core/ustring.h"
  38. #include "core/vector.h"
  39. #include <stdarg.h>
  40. class OS {
  41. static OS *singleton;
  42. String _execpath;
  43. List<String> _cmdline;
  44. bool _keep_screen_on;
  45. bool low_processor_usage_mode;
  46. int low_processor_usage_mode_sleep_usec;
  47. bool _verbose_stdout;
  48. String _local_clipboard;
  49. uint64_t _msec_splash;
  50. bool _no_window;
  51. int _exit_code;
  52. int _orientation;
  53. bool _allow_hidpi;
  54. bool _allow_layered;
  55. bool _use_vsync;
  56. bool _vsync_via_compositor;
  57. char *last_error;
  58. void *_stack_bottom;
  59. CompositeLogger *_logger;
  60. bool restart_on_exit;
  61. List<String> restart_commandline;
  62. protected:
  63. void _set_logger(CompositeLogger *p_logger);
  64. public:
  65. typedef void (*ImeCallback)(void *p_inp, String p_text, Point2 p_selection);
  66. typedef bool (*HasServerFeatureCallback)(const String &p_feature);
  67. enum RenderThreadMode {
  68. RENDER_THREAD_UNSAFE,
  69. RENDER_THREAD_SAFE,
  70. RENDER_SEPARATE_THREAD
  71. };
  72. protected:
  73. friend class Main;
  74. HasServerFeatureCallback has_server_feature_callback;
  75. RenderThreadMode _render_thread_mode;
  76. // functions used by main to initialize/deinitialize the OS
  77. void add_logger(Logger *p_logger);
  78. virtual void initialize() = 0;
  79. virtual void initialize_joypads() = 0;
  80. virtual void set_main_loop(MainLoop *p_main_loop) = 0;
  81. virtual void delete_main_loop() = 0;
  82. virtual void finalize() = 0;
  83. virtual void finalize_core() = 0;
  84. virtual void set_cmdline(const char *p_execpath, const List<String> &p_args);
  85. virtual bool _check_internal_feature_support(const String &p_feature) = 0;
  86. public:
  87. typedef int64_t ProcessID;
  88. static OS *get_singleton();
  89. void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, Logger::ErrorType p_type = Logger::ERR_ERROR);
  90. void print(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
  91. void printerr(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
  92. virtual String get_stdin_string(bool p_block = true) = 0;
  93. virtual PackedStringArray get_connected_midi_inputs();
  94. virtual void open_midi_inputs();
  95. virtual void close_midi_inputs();
  96. virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false) { return ERR_UNAVAILABLE; }
  97. virtual Error close_dynamic_library(void *p_library_handle) { return ERR_UNAVAILABLE; }
  98. 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; }
  99. virtual void set_low_processor_usage_mode(bool p_enabled);
  100. virtual bool is_in_low_processor_usage_mode() const;
  101. virtual void set_low_processor_usage_mode_sleep_usec(int p_usec);
  102. virtual int get_low_processor_usage_mode_sleep_usec() const;
  103. virtual String get_executable_path() const;
  104. virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking = true, ProcessID *r_child_id = NULL, String *r_pipe = NULL, int *r_exitcode = NULL, bool read_stderr = false, Mutex *p_pipe_mutex = NULL) = 0;
  105. virtual Error kill(const ProcessID &p_pid) = 0;
  106. virtual int get_process_id() const;
  107. virtual void vibrate_handheld(int p_duration_ms = 500);
  108. virtual Error shell_open(String p_uri);
  109. virtual Error set_cwd(const String &p_cwd);
  110. virtual bool has_environment(const String &p_var) const = 0;
  111. virtual String get_environment(const String &p_var) const = 0;
  112. virtual bool set_environment(const String &p_var, const String &p_value) const = 0;
  113. virtual String get_name() const = 0;
  114. virtual List<String> get_cmdline_args() const { return _cmdline; }
  115. virtual String get_model_name() const;
  116. void ensure_user_data_dir();
  117. virtual MainLoop *get_main_loop() const = 0;
  118. virtual void yield();
  119. enum Weekday {
  120. DAY_SUNDAY,
  121. DAY_MONDAY,
  122. DAY_TUESDAY,
  123. DAY_WEDNESDAY,
  124. DAY_THURSDAY,
  125. DAY_FRIDAY,
  126. DAY_SATURDAY
  127. };
  128. enum Month {
  129. /// Start at 1 to follow Windows SYSTEMTIME structure
  130. /// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v=vs.85).aspx
  131. MONTH_JANUARY = 1,
  132. MONTH_FEBRUARY,
  133. MONTH_MARCH,
  134. MONTH_APRIL,
  135. MONTH_MAY,
  136. MONTH_JUNE,
  137. MONTH_JULY,
  138. MONTH_AUGUST,
  139. MONTH_SEPTEMBER,
  140. MONTH_OCTOBER,
  141. MONTH_NOVEMBER,
  142. MONTH_DECEMBER
  143. };
  144. struct Date {
  145. int year;
  146. Month month;
  147. int day;
  148. Weekday weekday;
  149. bool dst;
  150. };
  151. struct Time {
  152. int hour;
  153. int min;
  154. int sec;
  155. };
  156. struct TimeZoneInfo {
  157. int bias;
  158. String name;
  159. };
  160. virtual Date get_date(bool local = false) const = 0;
  161. virtual Time get_time(bool local = false) const = 0;
  162. virtual TimeZoneInfo get_time_zone_info() const = 0;
  163. virtual String get_iso_date_time(bool local = false) const;
  164. virtual uint64_t get_unix_time() const;
  165. virtual uint64_t get_system_time_secs() const;
  166. virtual uint64_t get_system_time_msecs() const;
  167. virtual void delay_usec(uint32_t p_usec) const = 0;
  168. virtual uint64_t get_ticks_usec() const = 0;
  169. uint32_t get_ticks_msec() const;
  170. uint64_t get_splash_tick_msec() const;
  171. virtual bool is_userfs_persistent() const { return true; }
  172. bool is_stdout_verbose() const;
  173. virtual void disable_crash_handler() {}
  174. virtual bool is_disable_crash_handler() const { return false; }
  175. virtual void initialize_debugging() {}
  176. virtual void dump_memory_to_file(const char *p_file);
  177. virtual void dump_resources_to_file(const char *p_file);
  178. virtual void print_resources_in_use(bool p_short = false);
  179. virtual void print_all_resources(String p_to_file = "");
  180. virtual uint64_t get_static_memory_usage() const;
  181. virtual uint64_t get_static_memory_peak_usage() const;
  182. virtual uint64_t get_free_static_memory() const;
  183. RenderThreadMode get_render_thread_mode() const { return _render_thread_mode; }
  184. virtual String get_locale() const;
  185. String get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator = false) const;
  186. virtual String get_godot_dir_name() const;
  187. virtual String get_data_path() const;
  188. virtual String get_config_path() const;
  189. virtual String get_cache_path() const;
  190. virtual String get_bundle_resource_dir() const;
  191. virtual String get_user_data_dir() const;
  192. virtual String get_resource_dir() const;
  193. enum SystemDir {
  194. SYSTEM_DIR_DESKTOP,
  195. SYSTEM_DIR_DCIM,
  196. SYSTEM_DIR_DOCUMENTS,
  197. SYSTEM_DIR_DOWNLOADS,
  198. SYSTEM_DIR_MOVIES,
  199. SYSTEM_DIR_MUSIC,
  200. SYSTEM_DIR_PICTURES,
  201. SYSTEM_DIR_RINGTONES,
  202. };
  203. virtual String get_system_dir(SystemDir p_dir) const;
  204. virtual Error move_to_trash(const String &p_path) { return FAILED; }
  205. virtual void set_no_window_mode(bool p_enable);
  206. virtual bool is_no_window_mode_enabled() const;
  207. virtual void debug_break();
  208. virtual int get_exit_code() const;
  209. virtual void set_exit_code(int p_code);
  210. virtual int get_processor_count() const;
  211. virtual String get_unique_id() const;
  212. virtual bool can_use_threads() const;
  213. bool has_feature(const String &p_feature);
  214. void set_has_server_feature_callback(HasServerFeatureCallback p_callback);
  215. void set_restart_on_exit(bool p_restart, const List<String> &p_restart_arguments);
  216. bool is_restart_on_exit_set() const;
  217. List<String> get_restart_on_exit_arguments() const;
  218. virtual bool request_permission(const String &p_name) { return true; }
  219. virtual bool request_permissions() { return true; }
  220. virtual Vector<String> get_granted_permissions() const { return Vector<String>(); }
  221. virtual void process_and_drop_events() {}
  222. OS();
  223. virtual ~OS();
  224. };
  225. #endif // OS_H