os.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*************************************************************************/
  2. /* os.h */
  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. #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. /**
  41. @author Juan Linietsky <reduzio@gmail.com>
  42. */
  43. enum VideoDriver {
  44. VIDEO_DRIVER_GLES3,
  45. VIDEO_DRIVER_GLES2,
  46. VIDEO_DRIVER_MAX,
  47. };
  48. class OS {
  49. static OS *singleton;
  50. String _execpath;
  51. List<String> _cmdline;
  52. bool _keep_screen_on;
  53. bool low_processor_usage_mode;
  54. int low_processor_usage_mode_sleep_usec;
  55. bool _verbose_stdout;
  56. String _local_clipboard;
  57. uint64_t _msec_splash;
  58. bool _no_window;
  59. int _exit_code;
  60. int _orientation;
  61. bool _allow_hidpi;
  62. bool _allow_layered;
  63. bool _use_vsync;
  64. char *last_error;
  65. void *_stack_bottom;
  66. CompositeLogger *_logger;
  67. bool restart_on_exit;
  68. List<String> restart_commandline;
  69. protected:
  70. void _set_logger(CompositeLogger *p_logger);
  71. public:
  72. typedef void (*ImeCallback)(void *p_inp, String p_text, Point2 p_selection);
  73. enum PowerState {
  74. POWERSTATE_UNKNOWN, /**< cannot determine power status */
  75. POWERSTATE_ON_BATTERY, /**< Not plugged in, running on the battery */
  76. POWERSTATE_NO_BATTERY, /**< Plugged in, no battery available */
  77. POWERSTATE_CHARGING, /**< Plugged in, charging battery */
  78. POWERSTATE_CHARGED /**< Plugged in, battery charged */
  79. };
  80. enum RenderThreadMode {
  81. RENDER_THREAD_UNSAFE,
  82. RENDER_THREAD_SAFE,
  83. RENDER_SEPARATE_THREAD
  84. };
  85. struct VideoMode {
  86. int width, height;
  87. bool fullscreen;
  88. bool resizable;
  89. bool borderless_window;
  90. bool maximized;
  91. bool always_on_top;
  92. bool use_vsync;
  93. bool layered_splash;
  94. bool layered;
  95. float get_aspect() const { return (float)width / (float)height; }
  96. VideoMode(int p_width = 1024, int p_height = 600, bool p_fullscreen = false, bool p_resizable = true, bool p_borderless_window = false, bool p_maximized = false, bool p_always_on_top = false, bool p_use_vsync = false) {
  97. width = p_width;
  98. height = p_height;
  99. fullscreen = p_fullscreen;
  100. resizable = p_resizable;
  101. borderless_window = p_borderless_window;
  102. maximized = p_maximized;
  103. always_on_top = p_always_on_top;
  104. use_vsync = p_use_vsync;
  105. layered = false;
  106. layered_splash = false;
  107. }
  108. };
  109. protected:
  110. friend class Main;
  111. RenderThreadMode _render_thread_mode;
  112. // functions used by main to initialize/deinitialize the OS
  113. void add_logger(Logger *p_logger);
  114. virtual void initialize_core() = 0;
  115. virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) = 0;
  116. virtual void set_main_loop(MainLoop *p_main_loop) = 0;
  117. virtual void delete_main_loop() = 0;
  118. virtual void finalize() = 0;
  119. virtual void finalize_core() = 0;
  120. virtual void set_cmdline(const char *p_execpath, const List<String> &p_args);
  121. void _ensure_user_data_dir();
  122. virtual bool _check_internal_feature_support(const String &p_feature) = 0;
  123. public:
  124. typedef int64_t ProcessID;
  125. static OS *get_singleton();
  126. 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);
  127. void print(const char *p_format, ...);
  128. void printerr(const char *p_format, ...);
  129. virtual void alert(const String &p_alert, const String &p_title = "ALERT!") = 0;
  130. virtual String get_stdin_string(bool p_block = true) = 0;
  131. virtual void set_last_error(const char *p_error);
  132. virtual const char *get_last_error() const;
  133. virtual void clear_last_error();
  134. enum MouseMode {
  135. MOUSE_MODE_VISIBLE,
  136. MOUSE_MODE_HIDDEN,
  137. MOUSE_MODE_CAPTURED,
  138. MOUSE_MODE_CONFINED
  139. };
  140. virtual void set_mouse_mode(MouseMode p_mode);
  141. virtual MouseMode get_mouse_mode() const;
  142. virtual void warp_mouse_position(const Point2 &p_to) {}
  143. virtual Point2 get_mouse_position() const = 0;
  144. virtual int get_mouse_button_state() const = 0;
  145. virtual void set_window_title(const String &p_title) = 0;
  146. virtual void set_clipboard(const String &p_text);
  147. virtual String get_clipboard() const;
  148. virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0) = 0;
  149. virtual VideoMode get_video_mode(int p_screen = 0) const = 0;
  150. virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const = 0;
  151. virtual int get_video_driver_count() const;
  152. virtual const char *get_video_driver_name(int p_driver) const;
  153. virtual int get_current_video_driver() const = 0;
  154. virtual int get_audio_driver_count() const;
  155. virtual const char *get_audio_driver_name(int p_driver) const;
  156. virtual PoolStringArray get_connected_midi_inputs();
  157. virtual void open_midi_inputs();
  158. virtual void close_midi_inputs();
  159. virtual int get_screen_count() const { return 1; }
  160. virtual int get_current_screen() const { return 0; }
  161. virtual void set_current_screen(int p_screen) {}
  162. virtual Point2 get_screen_position(int p_screen = -1) const { return Point2(); }
  163. virtual Size2 get_screen_size(int p_screen = -1) const { return get_window_size(); }
  164. virtual int get_screen_dpi(int p_screen = -1) const { return 72; }
  165. virtual Point2 get_window_position() const { return Vector2(); }
  166. virtual void set_window_position(const Point2 &p_position) {}
  167. virtual Size2 get_window_size() const = 0;
  168. virtual Size2 get_real_window_size() const { return get_window_size(); }
  169. virtual void set_window_size(const Size2 p_size) {}
  170. virtual void set_window_fullscreen(bool p_enabled) {}
  171. virtual bool is_window_fullscreen() const { return true; }
  172. virtual void set_window_resizable(bool p_enabled) {}
  173. virtual bool is_window_resizable() const { return false; }
  174. virtual void set_window_minimized(bool p_enabled) {}
  175. virtual bool is_window_minimized() const { return false; }
  176. virtual void set_window_maximized(bool p_enabled) {}
  177. virtual bool is_window_maximized() const { return true; }
  178. virtual void set_window_always_on_top(bool p_enabled) {}
  179. virtual bool is_window_always_on_top() const { return false; }
  180. virtual void request_attention() {}
  181. virtual void center_window();
  182. // Returns window area free of hardware controls and other obstacles.
  183. // The application should use this to determine where to place UI elements.
  184. //
  185. // Keep in mind the area returned is in window coordinates rather than
  186. // viewport coordinates - you should perform the conversion on your own.
  187. //
  188. // The maximum size of the area is Rect2(0, 0, window_size.width, window_size.height).
  189. virtual Rect2 get_window_safe_area() const {
  190. Size2 window_size = get_window_size();
  191. return Rect2(0, 0, window_size.width, window_size.height);
  192. }
  193. virtual void set_borderless_window(bool p_borderless) {}
  194. virtual bool get_borderless_window() { return 0; }
  195. virtual bool get_window_per_pixel_transparency_enabled() const { return false; }
  196. virtual void set_window_per_pixel_transparency_enabled(bool p_enabled) {}
  197. virtual uint8_t *get_layered_buffer_data() { return NULL; }
  198. virtual Size2 get_layered_buffer_size() { return Size2(0, 0); }
  199. virtual void swap_layered_buffer() {}
  200. virtual void set_ime_active(const bool p_active) {}
  201. virtual void set_ime_position(const Point2 &p_pos) {}
  202. virtual Point2 get_ime_selection() const { return Point2(); }
  203. virtual String get_ime_text() const { return String(); }
  204. virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false) { return ERR_UNAVAILABLE; }
  205. virtual Error close_dynamic_library(void *p_library_handle) { return ERR_UNAVAILABLE; }
  206. 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; }
  207. virtual void set_keep_screen_on(bool p_enabled);
  208. virtual bool is_keep_screen_on() const;
  209. virtual void set_low_processor_usage_mode(bool p_enabled);
  210. virtual bool is_in_low_processor_usage_mode() const;
  211. virtual void set_low_processor_usage_mode_sleep_usec(int p_usec);
  212. virtual int get_low_processor_usage_mode_sleep_usec() const;
  213. virtual String get_executable_path() const;
  214. 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;
  215. virtual Error kill(const ProcessID &p_pid) = 0;
  216. virtual int get_process_id() const;
  217. virtual Error shell_open(String p_uri);
  218. virtual Error set_cwd(const String &p_cwd);
  219. virtual bool has_environment(const String &p_var) const = 0;
  220. virtual String get_environment(const String &p_var) const = 0;
  221. virtual String get_name() = 0;
  222. virtual List<String> get_cmdline_args() const { return _cmdline; }
  223. virtual String get_model_name() const;
  224. virtual MainLoop *get_main_loop() const = 0;
  225. virtual void yield();
  226. enum Weekday {
  227. DAY_SUNDAY,
  228. DAY_MONDAY,
  229. DAY_TUESDAY,
  230. DAY_WEDNESDAY,
  231. DAY_THURSDAY,
  232. DAY_FRIDAY,
  233. DAY_SATURDAY
  234. };
  235. enum Month {
  236. /// Start at 1 to follow Windows SYSTEMTIME structure
  237. /// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v=vs.85).aspx
  238. MONTH_JANUARY = 1,
  239. MONTH_FEBRUARY,
  240. MONTH_MARCH,
  241. MONTH_APRIL,
  242. MONTH_MAY,
  243. MONTH_JUNE,
  244. MONTH_JULY,
  245. MONTH_AUGUST,
  246. MONTH_SEPTEMBER,
  247. MONTH_OCTOBER,
  248. MONTH_NOVEMBER,
  249. MONTH_DECEMBER
  250. };
  251. struct Date {
  252. int year;
  253. Month month;
  254. int day;
  255. Weekday weekday;
  256. bool dst;
  257. };
  258. struct Time {
  259. int hour;
  260. int min;
  261. int sec;
  262. };
  263. struct TimeZoneInfo {
  264. int bias;
  265. String name;
  266. };
  267. virtual Date get_date(bool local = false) const = 0;
  268. virtual Time get_time(bool local = false) const = 0;
  269. virtual TimeZoneInfo get_time_zone_info() const = 0;
  270. virtual uint64_t get_unix_time() const;
  271. virtual uint64_t get_system_time_secs() const;
  272. virtual uint64_t get_system_time_msecs() const;
  273. virtual void delay_usec(uint32_t p_usec) const = 0;
  274. virtual uint64_t get_ticks_usec() const = 0;
  275. uint32_t get_ticks_msec() const;
  276. uint64_t get_splash_tick_msec() const;
  277. virtual bool can_draw() const = 0;
  278. virtual bool is_userfs_persistent() const { return true; }
  279. bool is_stdout_verbose() const;
  280. virtual void disable_crash_handler() {}
  281. virtual bool is_disable_crash_handler() const { return false; }
  282. virtual void initialize_debugging() {}
  283. enum CursorShape {
  284. CURSOR_ARROW,
  285. CURSOR_IBEAM,
  286. CURSOR_POINTING_HAND,
  287. CURSOR_CROSS,
  288. CURSOR_WAIT,
  289. CURSOR_BUSY,
  290. CURSOR_DRAG,
  291. CURSOR_CAN_DROP,
  292. CURSOR_FORBIDDEN,
  293. CURSOR_VSIZE,
  294. CURSOR_HSIZE,
  295. CURSOR_BDIAGSIZE,
  296. CURSOR_FDIAGSIZE,
  297. CURSOR_MOVE,
  298. CURSOR_VSPLIT,
  299. CURSOR_HSPLIT,
  300. CURSOR_HELP,
  301. CURSOR_MAX
  302. };
  303. virtual bool has_virtual_keyboard() const;
  304. virtual void show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2());
  305. virtual void hide_virtual_keyboard();
  306. // returns height of the currently shown virtual keyboard (0 if keyboard is hidden)
  307. virtual int get_virtual_keyboard_height() const;
  308. virtual void set_cursor_shape(CursorShape p_shape) = 0;
  309. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) = 0;
  310. virtual bool get_swap_ok_cancel() { return false; }
  311. virtual void dump_memory_to_file(const char *p_file);
  312. virtual void dump_resources_to_file(const char *p_file);
  313. virtual void print_resources_in_use(bool p_short = false);
  314. virtual void print_all_resources(String p_to_file = "");
  315. virtual int get_static_memory_usage() const;
  316. virtual int get_static_memory_peak_usage() const;
  317. virtual int get_dynamic_memory_usage() const;
  318. virtual int get_free_static_memory() const;
  319. RenderThreadMode get_render_thread_mode() const { return _render_thread_mode; }
  320. virtual String get_locale() const;
  321. String get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator = false) const;
  322. virtual String get_godot_dir_name() const;
  323. virtual String get_data_path() const;
  324. virtual String get_config_path() const;
  325. virtual String get_cache_path() const;
  326. virtual String get_user_data_dir() const;
  327. virtual String get_resource_dir() const;
  328. enum SystemDir {
  329. SYSTEM_DIR_DESKTOP,
  330. SYSTEM_DIR_DCIM,
  331. SYSTEM_DIR_DOCUMENTS,
  332. SYSTEM_DIR_DOWNLOADS,
  333. SYSTEM_DIR_MOVIES,
  334. SYSTEM_DIR_MUSIC,
  335. SYSTEM_DIR_PICTURES,
  336. SYSTEM_DIR_RINGTONES,
  337. };
  338. virtual String get_system_dir(SystemDir p_dir) const;
  339. virtual Error move_to_trash(const String &p_path) { return FAILED; }
  340. virtual void set_no_window_mode(bool p_enable);
  341. virtual bool is_no_window_mode_enabled() const;
  342. virtual bool has_touchscreen_ui_hint() const;
  343. enum ScreenOrientation {
  344. SCREEN_LANDSCAPE,
  345. SCREEN_PORTRAIT,
  346. SCREEN_REVERSE_LANDSCAPE,
  347. SCREEN_REVERSE_PORTRAIT,
  348. SCREEN_SENSOR_LANDSCAPE,
  349. SCREEN_SENSOR_PORTRAIT,
  350. SCREEN_SENSOR,
  351. };
  352. virtual void set_screen_orientation(ScreenOrientation p_orientation);
  353. ScreenOrientation get_screen_orientation() const;
  354. virtual void enable_for_stealing_focus(ProcessID pid) {}
  355. virtual void move_window_to_foreground() {}
  356. virtual void debug_break();
  357. virtual void release_rendering_thread();
  358. virtual void make_rendering_thread();
  359. virtual void swap_buffers();
  360. virtual void set_icon(const Ref<Image> &p_icon);
  361. virtual int get_exit_code() const;
  362. virtual void set_exit_code(int p_code);
  363. virtual int get_processor_count() const;
  364. virtual String get_unique_id() const;
  365. virtual Error native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track);
  366. virtual bool native_video_is_playing() const;
  367. virtual void native_video_pause();
  368. virtual void native_video_unpause();
  369. virtual void native_video_stop();
  370. virtual bool can_use_threads() const;
  371. virtual Error dialog_show(String p_title, String p_description, Vector<String> p_buttons, Object *p_obj, String p_callback);
  372. virtual Error dialog_input_text(String p_title, String p_description, String p_partial, Object *p_obj, String p_callback);
  373. enum LatinKeyboardVariant {
  374. LATIN_KEYBOARD_QWERTY,
  375. LATIN_KEYBOARD_QWERTZ,
  376. LATIN_KEYBOARD_AZERTY,
  377. LATIN_KEYBOARD_QZERTY,
  378. LATIN_KEYBOARD_DVORAK,
  379. LATIN_KEYBOARD_NEO,
  380. LATIN_KEYBOARD_COLEMAK,
  381. };
  382. virtual LatinKeyboardVariant get_latin_keyboard_variant() const;
  383. virtual bool is_joy_known(int p_device);
  384. virtual String get_joy_guid(int p_device) const;
  385. enum EngineContext {
  386. CONTEXT_EDITOR,
  387. CONTEXT_PROJECTMAN,
  388. CONTEXT_ENGINE,
  389. };
  390. virtual void set_context(int p_context);
  391. //amazing hack because OpenGL needs this to be set on a separate thread..
  392. //also core can't access servers, so a callback must be used
  393. typedef void (*SwitchVSyncCallbackInThread)(bool);
  394. static SwitchVSyncCallbackInThread switch_vsync_function;
  395. void set_use_vsync(bool p_enable);
  396. bool is_vsync_enabled() const;
  397. //real, actual overridable function to switch vsync, which needs to be called from graphics thread if needed
  398. virtual void _set_use_vsync(bool p_enable) {}
  399. virtual OS::PowerState get_power_state();
  400. virtual int get_power_seconds_left();
  401. virtual int get_power_percent_left();
  402. virtual void force_process_input(){};
  403. bool has_feature(const String &p_feature);
  404. bool is_layered_allowed() const { return _allow_layered; }
  405. bool is_hidpi_allowed() const { return _allow_hidpi; }
  406. void set_restart_on_exit(bool p_restart, const List<String> &p_restart_arguments);
  407. bool is_restart_on_exit_set() const;
  408. List<String> get_restart_on_exit_arguments() const;
  409. OS();
  410. virtual ~OS();
  411. };
  412. VARIANT_ENUM_CAST(OS::PowerState);
  413. #endif