os.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 "list.h"
  33. #include "os/main_loop.h"
  34. #include "ustring.h"
  35. #include "vector.h"
  36. #include <stdarg.h>
  37. /**
  38. @author Juan Linietsky <reduzio@gmail.com>
  39. */
  40. class OS {
  41. static OS *singleton;
  42. String _execpath;
  43. String _custom_level;
  44. List<String> _cmdline;
  45. int ips;
  46. bool _keep_screen_on;
  47. bool low_processor_usage_mode;
  48. bool _verbose_stdout;
  49. String _local_clipboard;
  50. uint64_t frames_drawn;
  51. uint32_t _frame_delay;
  52. uint64_t _msec_splash;
  53. bool _no_window;
  54. int _exit_code;
  55. int _orientation;
  56. float _fps;
  57. int _target_fps;
  58. float _time_scale;
  59. bool _pixel_snap;
  60. bool _allow_hidpi;
  61. char *last_error;
  62. public:
  63. enum RenderThreadMode {
  64. RENDER_THREAD_UNSAFE,
  65. RENDER_THREAD_SAFE,
  66. RENDER_SEPARATE_THREAD
  67. };
  68. struct VideoMode {
  69. int width, height;
  70. bool fullscreen;
  71. bool resizable;
  72. bool borderless_window;
  73. bool always_on_top;
  74. float get_aspect() const { return (float)width / (float)height; }
  75. VideoMode(int p_width = 1024, int p_height = 600, bool p_fullscreen = false, bool p_resizable = true, bool p_borderless_window = false, bool p_always_on_top = false) {
  76. width = p_width;
  77. height = p_height;
  78. fullscreen = p_fullscreen;
  79. resizable = p_resizable;
  80. borderless_window = p_borderless_window;
  81. always_on_top = p_always_on_top;
  82. }
  83. };
  84. protected:
  85. friend class Main;
  86. RenderThreadMode _render_thread_mode;
  87. // functions used by main to initialize/deintialize the OS
  88. virtual int get_video_driver_count() const = 0;
  89. virtual const char *get_video_driver_name(int p_driver) const = 0;
  90. virtual VideoMode get_default_video_mode() const = 0;
  91. virtual int get_audio_driver_count() const = 0;
  92. virtual const char *get_audio_driver_name(int p_driver) const = 0;
  93. virtual void initialize_core() = 0;
  94. virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) = 0;
  95. virtual void set_main_loop(MainLoop *p_main_loop) = 0;
  96. virtual void delete_main_loop() = 0;
  97. virtual void finalize() = 0;
  98. virtual void finalize_core() = 0;
  99. virtual void set_cmdline(const char *p_execpath, const List<String> &p_args);
  100. void _ensure_data_dir();
  101. public:
  102. typedef int64_t ProcessID;
  103. static OS *get_singleton();
  104. enum ErrorType {
  105. ERR_ERROR,
  106. ERR_WARNING,
  107. ERR_SCRIPT
  108. };
  109. virtual void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR);
  110. virtual void print(const char *p_format, ...);
  111. virtual void printerr(const char *p_format, ...);
  112. virtual void vprint(const char *p_format, va_list p_list, bool p_stderr = false) = 0;
  113. virtual void alert(const String &p_alert, const String &p_title = "ALERT!") = 0;
  114. virtual String get_stdin_string(bool p_block = true) = 0;
  115. virtual void set_last_error(const char *p_error);
  116. virtual const char *get_last_error() const;
  117. virtual void clear_last_error();
  118. enum MouseMode {
  119. MOUSE_MODE_VISIBLE,
  120. MOUSE_MODE_HIDDEN,
  121. MOUSE_MODE_CAPTURED
  122. };
  123. virtual void set_mouse_mode(MouseMode p_mode);
  124. virtual MouseMode get_mouse_mode() const;
  125. virtual void warp_mouse_pos(const Point2 &p_to) {}
  126. virtual Point2 get_mouse_pos() const = 0;
  127. virtual int get_mouse_button_state() const = 0;
  128. virtual void set_window_title(const String &p_title) = 0;
  129. virtual void set_clipboard(const String &p_text);
  130. virtual String get_clipboard() const;
  131. virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0) = 0;
  132. virtual VideoMode get_video_mode(int p_screen = 0) const = 0;
  133. virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const = 0;
  134. virtual int get_screen_count() const { return 1; }
  135. virtual int get_current_screen() const { return 0; }
  136. virtual void set_current_screen(int p_screen) {}
  137. virtual Point2 get_screen_position(int p_screen = 0) const { return Point2(); }
  138. virtual Size2 get_screen_size(int p_screen = 0) const { return get_window_size(); }
  139. virtual int get_screen_dpi(int p_screen = 0) const { return 72; }
  140. virtual Point2 get_window_position() const { return Vector2(); }
  141. virtual void set_window_position(const Point2 &p_position) {}
  142. virtual Size2 get_window_size() const = 0;
  143. virtual Size2 get_real_window_size() const { return get_window_size(); }
  144. virtual void set_window_size(const Size2 p_size) {}
  145. virtual void set_window_fullscreen(bool p_enabled) {}
  146. virtual bool is_window_fullscreen() const { return true; }
  147. virtual void set_window_resizable(bool p_enabled) {}
  148. virtual bool is_window_resizable() const { return false; }
  149. virtual void set_window_minimized(bool p_enabled) {}
  150. virtual bool is_window_minimized() const { return false; }
  151. virtual void set_window_maximized(bool p_enabled) {}
  152. virtual bool is_window_maximized() const { return true; }
  153. virtual void set_window_always_on_top(bool p_enabled) {}
  154. virtual bool is_window_always_on_top() const { return false; }
  155. virtual void request_attention() {}
  156. virtual void center_window();
  157. virtual void set_borderless_window(int p_borderless) {}
  158. virtual bool get_borderless_window() { return 0; }
  159. virtual void set_iterations_per_second(int p_ips);
  160. virtual int get_iterations_per_second() const;
  161. virtual void set_target_fps(int p_fps);
  162. virtual float get_target_fps() const;
  163. virtual float get_frames_per_second() const { return _fps; }
  164. virtual void set_keep_screen_on(bool p_enabled);
  165. virtual bool is_keep_screen_on() const;
  166. virtual void set_low_processor_usage_mode(bool p_enabled);
  167. virtual bool is_in_low_processor_usage_mode() const;
  168. virtual String get_installed_templates_path() const { return ""; }
  169. virtual String get_executable_path() const;
  170. virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id = NULL, String *r_pipe = NULL, int *r_exitcode = NULL, bool read_stderr = false) = 0;
  171. virtual Error kill(const ProcessID &p_pid) = 0;
  172. virtual int get_process_ID() const;
  173. virtual Error shell_open(String p_uri);
  174. virtual Error set_cwd(const String &p_cwd);
  175. virtual bool has_environment(const String &p_var) const = 0;
  176. virtual String get_environment(const String &p_var) const = 0;
  177. virtual String get_name() = 0;
  178. virtual List<String> get_cmdline_args() const { return _cmdline; }
  179. virtual String get_model_name() const;
  180. virtual MainLoop *get_main_loop() const = 0;
  181. String get_custom_level() const { return _custom_level; }
  182. virtual void yield();
  183. enum Weekday {
  184. DAY_SUNDAY,
  185. DAY_MONDAY,
  186. DAY_TUESDAY,
  187. DAY_WEDNESDAY,
  188. DAY_THURSDAY,
  189. DAY_FRIDAY,
  190. DAY_SATURDAY
  191. };
  192. enum Month {
  193. /// Start at 1 to follow Windows SYSTEMTIME structure
  194. /// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v=vs.85).aspx
  195. MONTH_JANUARY = 1,
  196. MONTH_FEBRUARY,
  197. MONTH_MARCH,
  198. MONTH_APRIL,
  199. MONTH_MAY,
  200. MONTH_JUNE,
  201. MONTH_JULY,
  202. MONTH_AUGUST,
  203. MONTH_SEPTEMBER,
  204. MONTH_OCTOBER,
  205. MONTH_NOVEMBER,
  206. MONTH_DECEMBER
  207. };
  208. struct Date {
  209. int year;
  210. Month month;
  211. int day;
  212. Weekday weekday;
  213. bool dst;
  214. };
  215. struct Time {
  216. int hour;
  217. int min;
  218. int sec;
  219. };
  220. struct TimeZoneInfo {
  221. int bias;
  222. String name;
  223. };
  224. virtual Date get_date(bool local = false) const = 0;
  225. virtual Time get_time(bool local = false) const = 0;
  226. virtual TimeZoneInfo get_time_zone_info() const = 0;
  227. virtual uint64_t get_unix_time() const;
  228. virtual uint64_t get_system_time_secs() const;
  229. virtual void delay_usec(uint32_t p_usec) const = 0;
  230. virtual uint64_t get_ticks_usec() const = 0;
  231. uint32_t get_ticks_msec() const;
  232. uint64_t get_splash_tick_msec() const;
  233. void set_frame_delay(uint32_t p_msec);
  234. uint32_t get_frame_delay() const;
  235. virtual bool can_draw() const = 0;
  236. uint64_t get_frames_drawn();
  237. bool is_stdout_verbose() const;
  238. virtual void disable_crash_handler() {}
  239. virtual bool is_disable_crash_handler() const { return false; }
  240. enum CursorShape {
  241. CURSOR_ARROW,
  242. CURSOR_IBEAM,
  243. CURSOR_POINTING_HAND,
  244. CURSOR_CROSS,
  245. CURSOR_WAIT,
  246. CURSOR_BUSY,
  247. CURSOR_DRAG,
  248. CURSOR_CAN_DROP,
  249. CURSOR_FORBIDDEN,
  250. CURSOR_VSIZE,
  251. CURSOR_HSIZE,
  252. CURSOR_BDIAGSIZE,
  253. CURSOR_FDIAGSIZE,
  254. CURSOR_MOVE,
  255. CURSOR_VSPLIT,
  256. CURSOR_HSPLIT,
  257. CURSOR_HELP,
  258. CURSOR_MAX
  259. };
  260. virtual bool has_virtual_keyboard() const;
  261. virtual void show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2());
  262. virtual void hide_virtual_keyboard();
  263. virtual void set_cursor_shape(CursorShape p_shape) = 0;
  264. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) = 0;
  265. virtual bool get_swap_ok_cancel() { return false; }
  266. virtual void dump_memory_to_file(const char *p_file);
  267. virtual void dump_resources_to_file(const char *p_file);
  268. virtual void print_resources_in_use(bool p_short = false);
  269. virtual void print_all_resources(String p_to_file = "");
  270. virtual int get_static_memory_usage() const;
  271. virtual int get_static_memory_peak_usage() const;
  272. virtual int get_dynamic_memory_usage() const;
  273. virtual int get_free_static_memory() const;
  274. RenderThreadMode get_render_thread_mode() const { return _render_thread_mode; }
  275. virtual String get_locale() const;
  276. String get_safe_application_name() const;
  277. virtual String get_data_dir() const;
  278. virtual String get_resource_dir() const;
  279. virtual Error move_path_to_trash(String p_dir) { return FAILED; }
  280. enum SystemDir {
  281. SYSTEM_DIR_DESKTOP,
  282. SYSTEM_DIR_DCIM,
  283. SYSTEM_DIR_DOCUMENTS,
  284. SYSTEM_DIR_DOWNLOADS,
  285. SYSTEM_DIR_MOVIES,
  286. SYSTEM_DIR_MUSIC,
  287. SYSTEM_DIR_PICTURES,
  288. SYSTEM_DIR_RINGTONES,
  289. };
  290. virtual String get_system_dir(SystemDir p_dir) const;
  291. virtual void set_no_window_mode(bool p_enable);
  292. virtual bool is_no_window_mode_enabled() const;
  293. virtual bool has_touchscreen_ui_hint() const;
  294. enum ScreenOrientation {
  295. SCREEN_LANDSCAPE,
  296. SCREEN_PORTRAIT,
  297. SCREEN_REVERSE_LANDSCAPE,
  298. SCREEN_REVERSE_PORTRAIT,
  299. SCREEN_SENSOR_LANDSCAPE,
  300. SCREEN_SENSOR_PORTRAIT,
  301. SCREEN_SENSOR,
  302. };
  303. virtual void set_screen_orientation(ScreenOrientation p_orientation);
  304. ScreenOrientation get_screen_orientation() const;
  305. virtual void enable_for_stealing_focus(ProcessID pid) {}
  306. virtual void move_window_to_foreground() {}
  307. virtual void debug_break();
  308. virtual void release_rendering_thread();
  309. virtual void make_rendering_thread();
  310. virtual void swap_buffers();
  311. virtual void set_icon(const Image &p_icon);
  312. virtual int get_exit_code() const;
  313. virtual void set_exit_code(int p_code);
  314. virtual int get_processor_count() const;
  315. virtual String get_unique_ID() const;
  316. virtual Error native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track);
  317. virtual bool native_video_is_playing() const;
  318. virtual void native_video_pause();
  319. virtual void native_video_unpause();
  320. virtual void native_video_stop();
  321. virtual bool can_use_threads() const;
  322. virtual Error dialog_show(String p_title, String p_description, Vector<String> p_buttons, Object *p_obj, String p_callback);
  323. virtual Error dialog_input_text(String p_title, String p_description, String p_partial, Object *p_obj, String p_callback);
  324. enum LatinKeyboardVariant {
  325. LATIN_KEYBOARD_QWERTY,
  326. LATIN_KEYBOARD_QWERTZ,
  327. LATIN_KEYBOARD_AZERTY,
  328. LATIN_KEYBOARD_QZERTY,
  329. LATIN_KEYBOARD_DVORAK,
  330. LATIN_KEYBOARD_NEO,
  331. LATIN_KEYBOARD_COLEMAK,
  332. };
  333. virtual LatinKeyboardVariant get_latin_keyboard_variant() const;
  334. void set_time_scale(float p_scale);
  335. float get_time_scale() const;
  336. _FORCE_INLINE_ bool get_use_pixel_snap() const { return _pixel_snap; }
  337. virtual bool is_joy_known(int p_device);
  338. virtual String get_joy_guid(int p_device) const;
  339. enum EngineContext {
  340. CONTEXT_EDITOR,
  341. CONTEXT_PROJECTMAN,
  342. };
  343. virtual void set_context(int p_context);
  344. virtual void set_use_vsync(bool p_enable);
  345. virtual bool is_vsync_enabled() const;
  346. Dictionary get_engine_version() const;
  347. bool is_hidpi_allowed() const { return _allow_hidpi; }
  348. OS();
  349. virtual ~OS();
  350. };
  351. #endif