display_server_x11.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*************************************************************************/
  2. /* display_server_x11.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 DISPLAY_SERVER_X11_H
  31. #define DISPLAY_SERVER_X11_H
  32. #ifdef X11_ENABLED
  33. #include "servers/display_server.h"
  34. #include "core/input/input.h"
  35. #include "drivers/alsa/audio_driver_alsa.h"
  36. #include "drivers/alsamidi/midi_driver_alsamidi.h"
  37. #include "drivers/pulseaudio/audio_driver_pulseaudio.h"
  38. #include "drivers/unix/os_unix.h"
  39. #include "joypad_linux.h"
  40. #include "servers/audio_server.h"
  41. #include "servers/visual/rasterizer.h"
  42. #include "servers/visual_server.h"
  43. #if defined(OPENGL_ENABLED)
  44. #include "context_gl_x11.h"
  45. #endif
  46. #if defined(VULKAN_ENABLED)
  47. #include "drivers/vulkan/rendering_device_vulkan.h"
  48. #include "platform/linuxbsd/vulkan_context_x11.h"
  49. #endif
  50. #include <X11/Xcursor/Xcursor.h>
  51. #include <X11/Xlib.h>
  52. #include <X11/extensions/XInput2.h>
  53. #include <X11/extensions/Xrandr.h>
  54. #include <X11/keysym.h>
  55. // Hints for X11 fullscreen
  56. typedef struct {
  57. unsigned long flags;
  58. unsigned long functions;
  59. unsigned long decorations;
  60. long inputMode;
  61. unsigned long status;
  62. } Hints;
  63. typedef struct _xrr_monitor_info {
  64. Atom name;
  65. Bool primary;
  66. Bool automatic;
  67. int noutput;
  68. int x;
  69. int y;
  70. int width;
  71. int height;
  72. int mwidth;
  73. int mheight;
  74. RROutput *outputs;
  75. } xrr_monitor_info;
  76. #undef CursorShape
  77. class DisplayServerX11 : public DisplayServer {
  78. //No need to register, it's platform-specific and nothing is added
  79. //GDCLASS(DisplayServerX11, DisplayServer)
  80. Atom wm_delete;
  81. Atom xdnd_enter;
  82. Atom xdnd_position;
  83. Atom xdnd_status;
  84. Atom xdnd_action_copy;
  85. Atom xdnd_drop;
  86. Atom xdnd_finished;
  87. Atom xdnd_selection;
  88. Atom xdnd_aware;
  89. Atom requested;
  90. int xdnd_version;
  91. #if defined(OPENGL_ENABLED)
  92. ContextGL_X11 *context_gles2;
  93. #endif
  94. #if defined(VULKAN_ENABLED)
  95. VulkanContextX11 *context_vulkan;
  96. RenderingDeviceVulkan *rendering_device_vulkan;
  97. #endif
  98. struct WindowData {
  99. Window x11_window;
  100. ::XIC xic;
  101. #if defined(VULKAN_ENABLED)
  102. int vulkan_window;
  103. #endif
  104. Size2i min_size;
  105. Size2i max_size;
  106. Size2i size;
  107. Size2i im_position;
  108. bool im_active = false;
  109. //better to guess on the fly, given WM can change it
  110. //WindowMode mode;
  111. bool fullscreen = false; //OS can't exit from this mode
  112. bool on_top = false;
  113. bool borderless = false;
  114. bool resize_disabled = false;
  115. Vector2i last_position_before_fs;
  116. };
  117. Map<WindowID, WindowData> windows;
  118. WindowID window_id_counter = MAIN_WINDOW_ID;
  119. WindowID _create_window(WindowMode p_mode, const Vector2i &p_resolution);
  120. String internal_clipboard;
  121. Window xdnd_source_window;
  122. ::Display *x11_display;
  123. char *xmbstring;
  124. int xmblen;
  125. unsigned long last_timestamp;
  126. ::Time last_keyrelease_time;
  127. ::XIM xim;
  128. ::XIMStyle xim_style;
  129. static void _xim_destroy_callback(::XIM im, ::XPointer client_data,
  130. ::XPointer call_data);
  131. Point2i last_mouse_pos;
  132. bool last_mouse_pos_valid;
  133. Point2i last_click_pos;
  134. uint64_t last_click_ms;
  135. int last_click_button_index;
  136. uint32_t last_button_state;
  137. struct {
  138. int opcode;
  139. Vector<int> touch_devices;
  140. Map<int, Vector2> absolute_devices;
  141. Map<int, Vector3> pen_devices;
  142. XIEventMask all_event_mask;
  143. Map<int, Vector2> state;
  144. double pressure;
  145. Vector2 tilt;
  146. Vector2 mouse_pos_to_filter;
  147. Vector2 relative_motion;
  148. Vector2 raw_pos;
  149. Vector2 old_raw_pos;
  150. ::Time last_relative_time;
  151. } xi;
  152. bool _refresh_device_info();
  153. unsigned int _get_mouse_button_state(unsigned int p_x11_button, int p_x11_type);
  154. void _get_key_modifier_state(unsigned int p_x11_state, Ref<InputEventWithModifiers> state);
  155. void _flush_mouse_motion();
  156. MouseMode mouse_mode;
  157. Point2i center;
  158. void _handle_key_event(WindowID p_window, XKeyEvent *p_event, bool p_echo = false);
  159. bool force_quit;
  160. bool minimized;
  161. bool window_has_focus;
  162. bool do_mouse_warp;
  163. const char *cursor_theme;
  164. int cursor_size;
  165. XcursorImage *img[CURSOR_MAX];
  166. Cursor cursors[CURSOR_MAX];
  167. Cursor null_cursor;
  168. CursorShape current_cursor;
  169. Map<CursorShape, Vector<Variant>> cursors_cache;
  170. bool layered_window;
  171. String rendering_driver;
  172. bool window_focused;
  173. //void set_wm_border(bool p_enabled);
  174. void set_wm_fullscreen(bool p_enabled);
  175. void set_wm_above(bool p_enabled);
  176. typedef xrr_monitor_info *(*xrr_get_monitors_t)(Display *dpy, Window window, Bool get_active, int *nmonitors);
  177. typedef void (*xrr_free_monitors_t)(xrr_monitor_info *monitors);
  178. xrr_get_monitors_t xrr_get_monitors;
  179. xrr_free_monitors_t xrr_free_monitors;
  180. void *xrandr_handle;
  181. Bool xrandr_ext_ok;
  182. struct Property {
  183. unsigned char *data;
  184. int format, nitems;
  185. Atom type;
  186. };
  187. static Property _read_property(Display *p_display, Window p_window, Atom p_property);
  188. void _update_real_mouse_position(const WindowData &wd);
  189. void _set_wm_fullscreen(WindowID p_window, bool p_enabled);
  190. void _set_wm_maximized(WindowID p_window, bool p_enabled);
  191. void _update_context(WindowData &wd);
  192. Context context = CONTEXT_ENGINE;
  193. protected:
  194. void _window_changed(XEvent *event);
  195. public:
  196. virtual bool has_feature(Feature p_feature) const;
  197. virtual String get_name() const;
  198. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  199. virtual void mouse_set_mode(MouseMode p_mode);
  200. virtual MouseMode mouse_get_mode() const;
  201. virtual void mouse_warp_to_position(const Point2i &p_to);
  202. virtual Point2i mouse_get_position() const;
  203. virtual int mouse_get_button_state() const;
  204. virtual void clipboard_set(const String &p_text);
  205. virtual String clipboard_get() const;
  206. virtual int get_screen_count() const;
  207. virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  208. virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  209. virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  210. virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  211. virtual Vector<int> get_window_list() const;
  212. virtual WindowID create_sub_window(WindowMode p_mode, uint32_t p_flags, const Rect2i &p_rect = Rect2i());
  213. virtual void delete_sub_window(WindowID p_id);
  214. virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID);
  215. virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const;
  216. virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID);
  217. virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const;
  218. virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID);
  219. virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID);
  220. virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const;
  221. virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID);
  222. virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const;
  223. virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID);
  224. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const;
  225. virtual Size2i window_get_real_size(WindowID p_window = MAIN_WINDOW_ID) const;
  226. virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID);
  227. virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const;
  228. virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const;
  229. virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID);
  230. virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const;
  231. virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID);
  232. virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID);
  233. virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const;
  234. virtual bool can_any_window_draw() const;
  235. virtual void window_set_ime_active(const bool p_active, WindowID p_window = MAIN_WINDOW_ID);
  236. virtual void window_set_ime_position(const Point2i &p_pos, WindowID p_window = MAIN_WINDOW_ID);
  237. virtual void cursor_set_shape(CursorShape p_shape);
  238. virtual CursorShape cursor_get_shape() const;
  239. virtual void cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
  240. virtual LatinKeyboardVariant get_latin_keyboard_variant() const;
  241. virtual void process_events();
  242. virtual void release_rendering_thread();
  243. virtual void make_rendering_thread();
  244. virtual void swap_buffers();
  245. virtual void set_context(Context p_context);
  246. virtual void set_native_icon(const String &p_filename);
  247. virtual void set_icon(const Ref<Image> &p_icon);
  248. static DisplayServer *create_func(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error);
  249. static Vector<String> get_rendering_drivers_func();
  250. static void register_x11_driver();
  251. DisplayServerX11(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error);
  252. ~DisplayServerX11();
  253. };
  254. #endif // X11 enabled
  255. #endif // DISPLAY_SERVER_X11_H