display_server_x11.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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-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_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 "core/templates/local_vector.h"
  36. #include "drivers/alsa/audio_driver_alsa.h"
  37. #include "drivers/alsamidi/midi_driver_alsamidi.h"
  38. #include "drivers/pulseaudio/audio_driver_pulseaudio.h"
  39. #include "drivers/unix/os_unix.h"
  40. #include "joypad_linux.h"
  41. #include "servers/audio_server.h"
  42. #include "servers/rendering/renderer_compositor.h"
  43. #include "servers/rendering_server.h"
  44. #if defined(OPENGL_ENABLED)
  45. #include "context_gl_x11.h"
  46. #endif
  47. #if defined(VULKAN_ENABLED)
  48. #include "drivers/vulkan/rendering_device_vulkan.h"
  49. #include "platform/linuxbsd/vulkan_context_x11.h"
  50. #endif
  51. #include <X11/Xcursor/Xcursor.h>
  52. #include <X11/Xlib.h>
  53. #include <X11/extensions/XInput2.h>
  54. #include <X11/extensions/Xrandr.h>
  55. #include <X11/keysym.h>
  56. typedef struct _xrr_monitor_info {
  57. Atom name;
  58. Bool primary = false;
  59. Bool automatic = false;
  60. int noutput = 0;
  61. int x = 0;
  62. int y = 0;
  63. int width = 0;
  64. int height = 0;
  65. int mwidth = 0;
  66. int mheight = 0;
  67. RROutput *outputs = nullptr;
  68. } xrr_monitor_info;
  69. #undef CursorShape
  70. class DisplayServerX11 : public DisplayServer {
  71. //No need to register, it's platform-specific and nothing is added
  72. //GDCLASS(DisplayServerX11, DisplayServer)
  73. _THREAD_SAFE_CLASS_
  74. Atom wm_delete;
  75. Atom xdnd_enter;
  76. Atom xdnd_position;
  77. Atom xdnd_status;
  78. Atom xdnd_action_copy;
  79. Atom xdnd_drop;
  80. Atom xdnd_finished;
  81. Atom xdnd_selection;
  82. Atom xdnd_aware;
  83. Atom requested;
  84. int xdnd_version;
  85. #if defined(OPENGL_ENABLED)
  86. ContextGL_X11 *context_gles2;
  87. #endif
  88. #if defined(VULKAN_ENABLED)
  89. VulkanContextX11 *context_vulkan;
  90. RenderingDeviceVulkan *rendering_device_vulkan;
  91. #endif
  92. struct WindowData {
  93. Window x11_window;
  94. ::XIC xic;
  95. Size2i min_size;
  96. Size2i max_size;
  97. Point2i position;
  98. Size2i size;
  99. Point2i im_position;
  100. bool im_active = false;
  101. Callable rect_changed_callback;
  102. Callable event_callback;
  103. Callable input_event_callback;
  104. Callable input_text_callback;
  105. Callable drop_files_callback;
  106. WindowID transient_parent = INVALID_WINDOW_ID;
  107. Set<WindowID> transient_children;
  108. ObjectID instance_id;
  109. bool menu_type = false;
  110. bool no_focus = false;
  111. //better to guess on the fly, given WM can change it
  112. //WindowMode mode;
  113. bool fullscreen = false; //OS can't exit from this mode
  114. bool on_top = false;
  115. bool borderless = false;
  116. bool resize_disabled = false;
  117. Vector2i last_position_before_fs;
  118. bool focused = false;
  119. bool minimized = false;
  120. unsigned int focus_order = 0;
  121. };
  122. Map<WindowID, WindowData> windows;
  123. WindowID window_id_counter = MAIN_WINDOW_ID;
  124. WindowID _create_window(WindowMode p_mode, uint32_t p_flags, const Rect2i &p_rect);
  125. String internal_clipboard;
  126. Window xdnd_source_window;
  127. ::Display *x11_display;
  128. char *xmbstring;
  129. int xmblen;
  130. unsigned long last_timestamp;
  131. ::Time last_keyrelease_time;
  132. ::XIM xim;
  133. ::XIMStyle xim_style;
  134. static void _xim_destroy_callback(::XIM im, ::XPointer client_data,
  135. ::XPointer call_data);
  136. Point2i last_mouse_pos;
  137. bool last_mouse_pos_valid;
  138. Point2i last_click_pos;
  139. uint64_t last_click_ms;
  140. int last_click_button_index;
  141. uint32_t last_button_state;
  142. bool app_focused = false;
  143. uint64_t time_since_no_focus = 0;
  144. struct {
  145. int opcode;
  146. Vector<int> touch_devices;
  147. Map<int, Vector2> absolute_devices;
  148. Map<int, Vector2> pen_pressure_range;
  149. Map<int, Vector2> pen_tilt_x_range;
  150. Map<int, Vector2> pen_tilt_y_range;
  151. XIEventMask all_event_mask;
  152. Map<int, Vector2> state;
  153. double pressure;
  154. bool pressure_supported;
  155. Vector2 tilt;
  156. Vector2 mouse_pos_to_filter;
  157. Vector2 relative_motion;
  158. Vector2 raw_pos;
  159. Vector2 old_raw_pos;
  160. ::Time last_relative_time;
  161. } xi;
  162. bool _refresh_device_info();
  163. unsigned int _get_mouse_button_state(unsigned int p_x11_button, int p_x11_type);
  164. void _get_key_modifier_state(unsigned int p_x11_state, Ref<InputEventWithModifiers> state);
  165. void _flush_mouse_motion();
  166. MouseMode mouse_mode;
  167. Point2i center;
  168. void _handle_key_event(WindowID p_window, XKeyEvent *p_event, LocalVector<XEvent> &p_events, uint32_t &p_event_index, bool p_echo = false);
  169. Atom _process_selection_request_target(Atom p_target, Window p_requestor, Atom p_property) const;
  170. void _handle_selection_request_event(XSelectionRequestEvent *p_event) const;
  171. String _clipboard_get_impl(Atom p_source, Window x11_window, Atom target) const;
  172. String _clipboard_get(Atom p_source, Window x11_window) const;
  173. void _clipboard_transfer_ownership(Atom p_source, Window x11_window) const;
  174. //bool minimized;
  175. //bool window_has_focus;
  176. bool do_mouse_warp;
  177. const char *cursor_theme;
  178. int cursor_size;
  179. XcursorImage *img[CURSOR_MAX];
  180. Cursor cursors[CURSOR_MAX];
  181. Cursor null_cursor;
  182. CursorShape current_cursor;
  183. Map<CursorShape, Vector<Variant>> cursors_cache;
  184. bool layered_window;
  185. String rendering_driver;
  186. //bool window_focused;
  187. //void set_wm_border(bool p_enabled);
  188. void set_wm_fullscreen(bool p_enabled);
  189. void set_wm_above(bool p_enabled);
  190. typedef xrr_monitor_info *(*xrr_get_monitors_t)(Display *dpy, Window window, Bool get_active, int *nmonitors);
  191. typedef void (*xrr_free_monitors_t)(xrr_monitor_info *monitors);
  192. xrr_get_monitors_t xrr_get_monitors;
  193. xrr_free_monitors_t xrr_free_monitors;
  194. void *xrandr_handle;
  195. Bool xrandr_ext_ok;
  196. struct Property {
  197. unsigned char *data;
  198. int format, nitems;
  199. Atom type;
  200. };
  201. static Property _read_property(Display *p_display, Window p_window, Atom p_property);
  202. void _update_real_mouse_position(const WindowData &wd);
  203. bool _window_maximize_check(WindowID p_window, const char *p_atom_name) const;
  204. void _update_size_hints(WindowID p_window);
  205. void _set_wm_fullscreen(WindowID p_window, bool p_enabled);
  206. void _set_wm_maximized(WindowID p_window, bool p_enabled);
  207. void _update_context(WindowData &wd);
  208. Context context = CONTEXT_ENGINE;
  209. void _send_window_event(const WindowData &wd, WindowEvent p_event);
  210. static void _dispatch_input_events(const Ref<InputEvent> &p_event);
  211. void _dispatch_input_event(const Ref<InputEvent> &p_event);
  212. mutable Mutex events_mutex;
  213. Thread events_thread;
  214. SafeFlag events_thread_done;
  215. LocalVector<XEvent> polled_events;
  216. static void _poll_events_thread(void *ud);
  217. bool _wait_for_events() const;
  218. void _poll_events();
  219. static Bool _predicate_all_events(Display *display, XEvent *event, XPointer arg);
  220. static Bool _predicate_clipboard_selection(Display *display, XEvent *event, XPointer arg);
  221. static Bool _predicate_clipboard_incr(Display *display, XEvent *event, XPointer arg);
  222. static Bool _predicate_clipboard_save_targets(Display *display, XEvent *event, XPointer arg);
  223. protected:
  224. void _window_changed(XEvent *event);
  225. public:
  226. virtual bool has_feature(Feature p_feature) const;
  227. virtual String get_name() const;
  228. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  229. virtual void mouse_set_mode(MouseMode p_mode);
  230. virtual MouseMode mouse_get_mode() const;
  231. virtual void mouse_warp_to_position(const Point2i &p_to);
  232. virtual Point2i mouse_get_position() const;
  233. virtual Point2i mouse_get_absolute_position() const;
  234. virtual int mouse_get_button_state() const;
  235. virtual void clipboard_set(const String &p_text);
  236. virtual String clipboard_get() const;
  237. virtual int get_screen_count() const;
  238. virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  239. virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  240. virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  241. virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  242. virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  243. virtual Vector<DisplayServer::WindowID> get_window_list() const;
  244. virtual WindowID create_sub_window(WindowMode p_mode, uint32_t p_flags, const Rect2i &p_rect = Rect2i());
  245. virtual void show_window(WindowID p_id);
  246. virtual void delete_sub_window(WindowID p_id);
  247. virtual WindowID get_window_at_screen_position(const Point2i &p_position) const;
  248. virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID);
  249. virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const;
  250. virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID);
  251. virtual void window_set_mouse_passthrough(const Vector<Vector2> &p_region, WindowID p_window = MAIN_WINDOW_ID);
  252. virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  253. virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  254. virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  255. virtual void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  256. virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  257. virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const;
  258. virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID);
  259. virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const;
  260. virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID);
  261. virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID);
  262. virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const;
  263. virtual void window_set_transient(WindowID p_window, WindowID p_parent);
  264. virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID);
  265. virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const;
  266. virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID);
  267. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const;
  268. virtual Size2i window_get_real_size(WindowID p_window = MAIN_WINDOW_ID) const;
  269. virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID);
  270. virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const;
  271. virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const;
  272. virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID);
  273. virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const;
  274. virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID);
  275. virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID);
  276. virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const;
  277. virtual bool can_any_window_draw() const;
  278. virtual void window_set_ime_active(const bool p_active, WindowID p_window = MAIN_WINDOW_ID);
  279. virtual void window_set_ime_position(const Point2i &p_pos, WindowID p_window = MAIN_WINDOW_ID);
  280. virtual void cursor_set_shape(CursorShape p_shape);
  281. virtual CursorShape cursor_get_shape() const;
  282. virtual void cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
  283. virtual int keyboard_get_layout_count() const;
  284. virtual int keyboard_get_current_layout() const;
  285. virtual void keyboard_set_current_layout(int p_index);
  286. virtual String keyboard_get_layout_language(int p_index) const;
  287. virtual String keyboard_get_layout_name(int p_index) const;
  288. virtual void process_events();
  289. virtual void release_rendering_thread();
  290. virtual void make_rendering_thread();
  291. virtual void swap_buffers();
  292. virtual void set_context(Context p_context);
  293. virtual void set_native_icon(const String &p_filename);
  294. virtual void set_icon(const Ref<Image> &p_icon);
  295. static DisplayServer *create_func(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error);
  296. static Vector<String> get_rendering_drivers_func();
  297. static void register_x11_driver();
  298. DisplayServerX11(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error);
  299. ~DisplayServerX11();
  300. };
  301. #endif // X11 enabled
  302. #endif // DISPLAY_SERVER_X11_H