display_server.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*************************************************************************/
  2. /* display_server.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 DISPLAY_SERVER_H
  31. #define DISPLAY_SERVER_H
  32. #include "core/input/input.h"
  33. #include "core/io/resource.h"
  34. #include "core/os/os.h"
  35. #include "core/variant/callable.h"
  36. class Texture2D;
  37. class DisplayServer : public Object {
  38. GDCLASS(DisplayServer, Object)
  39. static DisplayServer *singleton;
  40. static bool hidpi_allowed;
  41. public:
  42. _FORCE_INLINE_ static DisplayServer *get_singleton() {
  43. return singleton;
  44. }
  45. enum WindowMode {
  46. WINDOW_MODE_WINDOWED,
  47. WINDOW_MODE_MINIMIZED,
  48. WINDOW_MODE_MAXIMIZED,
  49. WINDOW_MODE_FULLSCREEN
  50. };
  51. // Keep the VSyncMode enum values in sync with the `display/window/vsync/vsync_mode`
  52. // project setting hint.
  53. enum VSyncMode {
  54. VSYNC_DISABLED,
  55. VSYNC_ENABLED,
  56. VSYNC_ADAPTIVE,
  57. VSYNC_MAILBOX
  58. };
  59. typedef DisplayServer *(*CreateFunction)(const String &, WindowMode, VSyncMode, uint32_t, const Size2i &, Error &r_error);
  60. typedef Vector<String> (*GetRenderingDriversFunction)();
  61. private:
  62. static void _input_set_mouse_mode(Input::MouseMode p_mode);
  63. static Input::MouseMode _input_get_mouse_mode();
  64. static void _input_warp(const Vector2 &p_to_pos);
  65. static Input::CursorShape _input_get_current_cursor_shape();
  66. static void _input_set_custom_mouse_cursor_func(const RES &, Input::CursorShape, const Vector2 &p_hostspot);
  67. protected:
  68. static void _bind_methods();
  69. enum {
  70. MAX_SERVERS = 64
  71. };
  72. struct DisplayServerCreate {
  73. const char *name;
  74. CreateFunction create_function;
  75. GetRenderingDriversFunction get_rendering_drivers_function;
  76. };
  77. static DisplayServerCreate server_create_functions[MAX_SERVERS];
  78. static int server_create_count;
  79. friend class RendererViewport;
  80. public:
  81. enum Feature {
  82. FEATURE_GLOBAL_MENU,
  83. FEATURE_SUBWINDOWS,
  84. FEATURE_TOUCHSCREEN,
  85. FEATURE_MOUSE,
  86. FEATURE_MOUSE_WARP,
  87. FEATURE_CLIPBOARD,
  88. FEATURE_VIRTUAL_KEYBOARD,
  89. FEATURE_CURSOR_SHAPE,
  90. FEATURE_CUSTOM_CURSOR_SHAPE,
  91. FEATURE_NATIVE_DIALOG,
  92. FEATURE_CONSOLE_WINDOW,
  93. FEATURE_IME,
  94. FEATURE_WINDOW_TRANSPARENCY,
  95. FEATURE_HIDPI,
  96. FEATURE_ICON,
  97. FEATURE_NATIVE_ICON,
  98. FEATURE_ORIENTATION,
  99. FEATURE_SWAP_BUFFERS,
  100. FEATURE_KEEP_SCREEN_ON,
  101. FEATURE_CLIPBOARD_PRIMARY,
  102. };
  103. virtual bool has_feature(Feature p_feature) const = 0;
  104. virtual String get_name() const = 0;
  105. virtual void global_menu_add_item(const String &p_menu_root, const String &p_label, const Callable &p_callback, const Variant &p_tag = Variant());
  106. virtual void global_menu_add_check_item(const String &p_menu_root, const String &p_label, const Callable &p_callback, const Variant &p_tag = Variant());
  107. virtual void global_menu_add_submenu_item(const String &p_menu_root, const String &p_label, const String &p_submenu);
  108. virtual void global_menu_add_separator(const String &p_menu_root);
  109. virtual bool global_menu_is_item_checked(const String &p_menu_root, int p_idx) const;
  110. virtual bool global_menu_is_item_checkable(const String &p_menu_root, int p_idx) const;
  111. virtual Callable global_menu_get_item_callback(const String &p_menu_root, int p_idx);
  112. virtual Variant global_menu_get_item_tag(const String &p_menu_root, int p_idx);
  113. virtual String global_menu_get_item_text(const String &p_menu_root, int p_idx);
  114. virtual String global_menu_get_item_submenu(const String &p_menu_root, int p_idx);
  115. virtual void global_menu_set_item_checked(const String &p_menu_root, int p_idx, bool p_checked);
  116. virtual void global_menu_set_item_checkable(const String &p_menu_root, int p_idx, bool p_checkable);
  117. virtual void global_menu_set_item_callback(const String &p_menu_root, int p_idx, const Callable &p_callback);
  118. virtual void global_menu_set_item_tag(const String &p_menu_root, int p_idx, const Variant &p_tag);
  119. virtual void global_menu_set_item_text(const String &p_menu_root, int p_idx, const String &p_text);
  120. virtual void global_menu_set_item_submenu(const String &p_menu_root, int p_idx, const String &p_submenu);
  121. virtual int global_menu_get_item_count(const String &p_menu_root) const;
  122. virtual void global_menu_remove_item(const String &p_menu_root, int p_idx);
  123. virtual void global_menu_clear(const String &p_menu_root);
  124. enum MouseMode {
  125. MOUSE_MODE_VISIBLE,
  126. MOUSE_MODE_HIDDEN,
  127. MOUSE_MODE_CAPTURED,
  128. MOUSE_MODE_CONFINED,
  129. MOUSE_MODE_CONFINED_HIDDEN,
  130. };
  131. virtual void mouse_set_mode(MouseMode p_mode);
  132. virtual MouseMode mouse_get_mode() const;
  133. virtual void mouse_warp_to_position(const Point2i &p_to);
  134. virtual Point2i mouse_get_position() const;
  135. virtual Point2i mouse_get_absolute_position() const;
  136. virtual MouseButton mouse_get_button_state() const;
  137. virtual void clipboard_set(const String &p_text);
  138. virtual String clipboard_get() const;
  139. virtual void clipboard_set_primary(const String &p_text);
  140. virtual String clipboard_get_primary() const;
  141. enum {
  142. SCREEN_OF_MAIN_WINDOW = -1
  143. };
  144. virtual int get_screen_count() const = 0;
  145. virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const = 0;
  146. virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const = 0;
  147. virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const = 0;
  148. virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const = 0;
  149. virtual float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  150. virtual float screen_get_max_scale() const {
  151. float scale = 1.f;
  152. int screen_count = get_screen_count();
  153. for (int i = 0; i < screen_count; i++) {
  154. scale = fmax(scale, screen_get_scale(i));
  155. }
  156. return scale;
  157. }
  158. virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  159. // Keep the ScreenOrientation enum values in sync with the `display/window/handheld/orientation`
  160. // project setting hint.
  161. enum ScreenOrientation {
  162. SCREEN_LANDSCAPE,
  163. SCREEN_PORTRAIT,
  164. SCREEN_REVERSE_LANDSCAPE,
  165. SCREEN_REVERSE_PORTRAIT,
  166. SCREEN_SENSOR_LANDSCAPE,
  167. SCREEN_SENSOR_PORTRAIT,
  168. SCREEN_SENSOR,
  169. };
  170. virtual void screen_set_orientation(ScreenOrientation p_orientation, int p_screen = SCREEN_OF_MAIN_WINDOW);
  171. virtual ScreenOrientation screen_get_orientation(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  172. virtual void screen_set_keep_on(bool p_enable); //disable screensaver
  173. virtual bool screen_is_kept_on() const;
  174. enum {
  175. MAIN_WINDOW_ID = 0,
  176. INVALID_WINDOW_ID = -1
  177. };
  178. typedef int WindowID;
  179. virtual Vector<DisplayServer::WindowID> get_window_list() const = 0;
  180. enum WindowFlags {
  181. WINDOW_FLAG_RESIZE_DISABLED,
  182. WINDOW_FLAG_BORDERLESS,
  183. WINDOW_FLAG_ALWAYS_ON_TOP,
  184. WINDOW_FLAG_TRANSPARENT,
  185. WINDOW_FLAG_NO_FOCUS,
  186. WINDOW_FLAG_MAX,
  187. };
  188. // Separate enum otherwise we get warnings in switches not handling all values.
  189. enum WindowFlagsBit {
  190. WINDOW_FLAG_RESIZE_DISABLED_BIT = (1 << WINDOW_FLAG_RESIZE_DISABLED),
  191. WINDOW_FLAG_BORDERLESS_BIT = (1 << WINDOW_FLAG_BORDERLESS),
  192. WINDOW_FLAG_ALWAYS_ON_TOP_BIT = (1 << WINDOW_FLAG_ALWAYS_ON_TOP),
  193. WINDOW_FLAG_TRANSPARENT_BIT = (1 << WINDOW_FLAG_TRANSPARENT),
  194. WINDOW_FLAG_NO_FOCUS_BIT = (1 << WINDOW_FLAG_NO_FOCUS)
  195. };
  196. virtual WindowID create_sub_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect = Rect2i());
  197. virtual void show_window(WindowID p_id);
  198. virtual void delete_sub_window(WindowID p_id);
  199. virtual WindowID get_window_at_screen_position(const Point2i &p_position) const = 0;
  200. virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID) = 0;
  201. virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const = 0;
  202. virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) = 0;
  203. enum WindowEvent {
  204. WINDOW_EVENT_MOUSE_ENTER,
  205. WINDOW_EVENT_MOUSE_EXIT,
  206. WINDOW_EVENT_FOCUS_IN,
  207. WINDOW_EVENT_FOCUS_OUT,
  208. WINDOW_EVENT_CLOSE_REQUEST,
  209. WINDOW_EVENT_GO_BACK_REQUEST,
  210. WINDOW_EVENT_DPI_CHANGE,
  211. };
  212. virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) = 0;
  213. virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) = 0;
  214. virtual void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) = 0;
  215. virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) = 0;
  216. virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) = 0;
  217. virtual void window_set_mouse_passthrough(const Vector<Vector2> &p_region, WindowID p_window = MAIN_WINDOW_ID);
  218. virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const = 0;
  219. virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID) = 0;
  220. virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const = 0;
  221. virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID) = 0;
  222. virtual void window_set_transient(WindowID p_window, WindowID p_parent) = 0;
  223. virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) = 0;
  224. virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const = 0;
  225. virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) = 0;
  226. virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const = 0;
  227. virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) = 0;
  228. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const = 0;
  229. virtual Size2i window_get_real_size(WindowID p_window = MAIN_WINDOW_ID) const = 0; // FIXME: Find clearer name for this.
  230. virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID) = 0;
  231. virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const = 0;
  232. virtual void window_set_vsync_mode(VSyncMode p_vsync_mode, WindowID p_window = MAIN_WINDOW_ID);
  233. virtual VSyncMode window_get_vsync_mode(WindowID p_window) const;
  234. virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const = 0;
  235. virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID) = 0;
  236. virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const = 0;
  237. virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID) = 0;
  238. virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID) = 0;
  239. virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const = 0;
  240. virtual bool can_any_window_draw() const = 0;
  241. virtual void window_set_ime_active(const bool p_active, WindowID p_window = MAIN_WINDOW_ID);
  242. virtual void window_set_ime_position(const Point2i &p_pos, WindowID p_window = MAIN_WINDOW_ID);
  243. // necessary for GL focus, may be able to use one of the existing functions for this, not sure yet
  244. virtual void gl_window_make_current(DisplayServer::WindowID p_window_id);
  245. virtual Point2i ime_get_selection() const;
  246. virtual String ime_get_text() const;
  247. virtual void console_set_visible(bool p_enabled);
  248. virtual bool is_console_visible() const;
  249. virtual void virtual_keyboard_show(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2(), bool p_multiline = false, int p_max_length = -1, int p_cursor_start = -1, int p_cursor_end = -1);
  250. virtual void virtual_keyboard_hide();
  251. // returns height of the currently shown virtual keyboard (0 if keyboard is hidden)
  252. virtual int virtual_keyboard_get_height() const;
  253. enum CursorShape {
  254. CURSOR_ARROW,
  255. CURSOR_IBEAM,
  256. CURSOR_POINTING_HAND,
  257. CURSOR_CROSS,
  258. CURSOR_WAIT,
  259. CURSOR_BUSY,
  260. CURSOR_DRAG,
  261. CURSOR_CAN_DROP,
  262. CURSOR_FORBIDDEN,
  263. CURSOR_VSIZE,
  264. CURSOR_HSIZE,
  265. CURSOR_BDIAGSIZE,
  266. CURSOR_FDIAGSIZE,
  267. CURSOR_MOVE,
  268. CURSOR_VSPLIT,
  269. CURSOR_HSPLIT,
  270. CURSOR_HELP,
  271. CURSOR_MAX
  272. };
  273. virtual void cursor_set_shape(CursorShape p_shape);
  274. virtual CursorShape cursor_get_shape() const;
  275. virtual void cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape = CURSOR_ARROW, const Vector2 &p_hotspot = Vector2());
  276. virtual bool get_swap_cancel_ok();
  277. virtual void enable_for_stealing_focus(OS::ProcessID pid);
  278. virtual Error dialog_show(String p_title, String p_description, Vector<String> p_buttons, const Callable &p_callback);
  279. virtual Error dialog_input_text(String p_title, String p_description, String p_partial, const Callable &p_callback);
  280. virtual int keyboard_get_layout_count() const;
  281. virtual int keyboard_get_current_layout() const;
  282. virtual void keyboard_set_current_layout(int p_index);
  283. virtual String keyboard_get_layout_language(int p_index) const;
  284. virtual String keyboard_get_layout_name(int p_index) const;
  285. virtual Key keyboard_get_keycode_from_physical(Key p_keycode) const;
  286. virtual int tablet_get_driver_count() const { return 1; };
  287. virtual String tablet_get_driver_name(int p_driver) const { return "default"; };
  288. virtual String tablet_get_current_driver() const { return "default"; };
  289. virtual void tablet_set_current_driver(const String &p_driver){};
  290. virtual void process_events() = 0;
  291. virtual void force_process_and_drop_events();
  292. virtual void release_rendering_thread();
  293. virtual void make_rendering_thread();
  294. virtual void swap_buffers();
  295. virtual void set_native_icon(const String &p_filename);
  296. virtual void set_icon(const Ref<Image> &p_icon);
  297. enum Context {
  298. CONTEXT_EDITOR,
  299. CONTEXT_PROJECTMAN,
  300. CONTEXT_ENGINE,
  301. };
  302. virtual void set_context(Context p_context);
  303. static void register_create_function(const char *p_name, CreateFunction p_function, GetRenderingDriversFunction p_get_drivers);
  304. static int get_create_function_count();
  305. static const char *get_create_function_name(int p_index);
  306. static Vector<String> get_create_function_rendering_drivers(int p_index);
  307. static DisplayServer *create(int p_index, const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error);
  308. DisplayServer();
  309. ~DisplayServer();
  310. };
  311. VARIANT_ENUM_CAST(DisplayServer::WindowEvent)
  312. VARIANT_ENUM_CAST(DisplayServer::Feature)
  313. VARIANT_ENUM_CAST(DisplayServer::MouseMode)
  314. VARIANT_ENUM_CAST(DisplayServer::ScreenOrientation)
  315. VARIANT_ENUM_CAST(DisplayServer::WindowMode)
  316. VARIANT_ENUM_CAST(DisplayServer::WindowFlags)
  317. VARIANT_ENUM_CAST(DisplayServer::CursorShape)
  318. VARIANT_ENUM_CAST(DisplayServer::VSyncMode)
  319. #endif // DISPLAY_SERVER_H