display_server_x11.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**************************************************************************/
  2. /* display_server_x11.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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(SPEECHD_ENABLED)
  45. #include "../tts_linux.h"
  46. #endif
  47. #if defined(GLES3_ENABLED)
  48. #include "gl_manager_x11.h"
  49. #endif
  50. #if defined(VULKAN_ENABLED)
  51. #include "drivers/vulkan/rendering_device_vulkan.h"
  52. #include "vulkan_context_x11.h"
  53. #endif
  54. #if defined(DBUS_ENABLED)
  55. #include "../freedesktop_portal_desktop.h"
  56. #include "../freedesktop_screensaver.h"
  57. #endif
  58. #include <X11/Xatom.h>
  59. #include <X11/Xlib.h>
  60. #include <X11/Xutil.h>
  61. #include <X11/keysym.h>
  62. #include "dynwrappers/xlib-so_wrap.h"
  63. #include "dynwrappers/xcursor-so_wrap.h"
  64. #include "dynwrappers/xext-so_wrap.h"
  65. #include "dynwrappers/xinerama-so_wrap.h"
  66. #include "dynwrappers/xinput2-so_wrap.h"
  67. #include "dynwrappers/xrandr-so_wrap.h"
  68. #include "dynwrappers/xrender-so_wrap.h"
  69. #include "../xkbcommon-so_wrap.h"
  70. typedef struct _xrr_monitor_info {
  71. Atom name;
  72. Bool primary = false;
  73. Bool automatic = false;
  74. int noutput = 0;
  75. int x = 0;
  76. int y = 0;
  77. int width = 0;
  78. int height = 0;
  79. int mwidth = 0;
  80. int mheight = 0;
  81. RROutput *outputs = nullptr;
  82. } xrr_monitor_info;
  83. #undef CursorShape
  84. class DisplayServerX11 : public DisplayServer {
  85. //No need to register, it's platform-specific and nothing is added
  86. //GDCLASS(DisplayServerX11, DisplayServer)
  87. _THREAD_SAFE_CLASS_
  88. Atom wm_delete;
  89. Atom xdnd_enter;
  90. Atom xdnd_position;
  91. Atom xdnd_status;
  92. Atom xdnd_action_copy;
  93. Atom xdnd_drop;
  94. Atom xdnd_finished;
  95. Atom xdnd_selection;
  96. Atom xdnd_aware;
  97. Atom requested = None;
  98. int xdnd_version = 5;
  99. #if defined(GLES3_ENABLED)
  100. GLManager_X11 *gl_manager = nullptr;
  101. #endif
  102. #if defined(VULKAN_ENABLED)
  103. VulkanContextX11 *context_vulkan = nullptr;
  104. RenderingDeviceVulkan *rendering_device_vulkan = nullptr;
  105. #endif
  106. #if defined(DBUS_ENABLED)
  107. FreeDesktopScreenSaver *screensaver = nullptr;
  108. bool keep_screen_on = false;
  109. #endif
  110. #ifdef SPEECHD_ENABLED
  111. TTS_Linux *tts = nullptr;
  112. #endif
  113. #if defined(DBUS_ENABLED)
  114. FreeDesktopPortalDesktop *portal_desktop = nullptr;
  115. #endif
  116. struct WindowData {
  117. Window x11_window;
  118. Window x11_xim_window;
  119. ::XIC xic;
  120. bool ime_active = false;
  121. bool ime_in_progress = false;
  122. bool ime_suppress_next_keyup = false;
  123. xkb_compose_state *xkb_state = nullptr;
  124. Size2i min_size;
  125. Size2i max_size;
  126. Point2i position;
  127. Size2i size;
  128. Callable rect_changed_callback;
  129. Callable event_callback;
  130. Callable input_event_callback;
  131. Callable input_text_callback;
  132. Callable drop_files_callback;
  133. Vector<Vector2> mpath;
  134. WindowID transient_parent = INVALID_WINDOW_ID;
  135. HashSet<WindowID> transient_children;
  136. ObjectID instance_id;
  137. bool no_focus = false;
  138. //better to guess on the fly, given WM can change it
  139. //WindowMode mode;
  140. bool fullscreen = false; //OS can't exit from this mode
  141. bool exclusive_fullscreen = false;
  142. bool on_top = false;
  143. bool borderless = false;
  144. bool resize_disabled = false;
  145. Vector2i last_position_before_fs;
  146. bool focused = true;
  147. bool minimized = false;
  148. bool maximized = false;
  149. bool is_popup = false;
  150. bool layered_window = false;
  151. bool mpass = false;
  152. Rect2i parent_safe_rect;
  153. unsigned int focus_order = 0;
  154. };
  155. Point2i im_selection;
  156. String im_text;
  157. bool xkb_loaded = false;
  158. xkb_context *xkb_ctx = nullptr;
  159. xkb_compose_table *dead_tbl = nullptr;
  160. HashMap<WindowID, WindowData> windows;
  161. unsigned int last_mouse_monitor_mask = 0;
  162. uint64_t time_since_popup = 0;
  163. List<WindowID> popup_list;
  164. WindowID last_focused_window = INVALID_WINDOW_ID;
  165. WindowID window_id_counter = MAIN_WINDOW_ID;
  166. WindowID _create_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect);
  167. String internal_clipboard;
  168. String internal_clipboard_primary;
  169. Window xdnd_source_window = 0;
  170. ::Display *x11_display;
  171. char *xmbstring = nullptr;
  172. int xmblen = 0;
  173. unsigned long last_timestamp = 0;
  174. ::Time last_keyrelease_time = 0;
  175. ::XIM xim;
  176. ::XIMStyle xim_style;
  177. static int _xim_preedit_start_callback(::XIM xim, ::XPointer client_data,
  178. ::XPointer call_data);
  179. static void _xim_preedit_done_callback(::XIM xim, ::XPointer client_data,
  180. ::XPointer call_data);
  181. static void _xim_preedit_draw_callback(::XIM xim, ::XPointer client_data,
  182. ::XIMPreeditDrawCallbackStruct *call_data);
  183. static void _xim_preedit_caret_callback(::XIM xim, ::XPointer client_data,
  184. ::XIMPreeditCaretCallbackStruct *call_data);
  185. static void _xim_destroy_callback(::XIM im, ::XPointer client_data,
  186. ::XPointer call_data);
  187. Point2i last_mouse_pos;
  188. bool last_mouse_pos_valid = false;
  189. Point2i last_click_pos = Point2i(-100, -100);
  190. uint64_t last_click_ms = 0;
  191. MouseButton last_click_button_index = MouseButton::NONE;
  192. BitField<MouseButtonMask> last_button_state;
  193. bool app_focused = false;
  194. uint64_t time_since_no_focus = 0;
  195. struct {
  196. int opcode;
  197. Vector<int> touch_devices;
  198. HashMap<int, Vector2> absolute_devices;
  199. HashMap<int, Vector2> pen_pressure_range;
  200. HashMap<int, Vector2> pen_tilt_x_range;
  201. HashMap<int, Vector2> pen_tilt_y_range;
  202. HashMap<int, bool> pen_inverted_devices;
  203. XIEventMask all_event_mask;
  204. HashMap<int, Vector2> state;
  205. double pressure;
  206. bool pressure_supported;
  207. bool pen_inverted;
  208. Vector2 tilt;
  209. Vector2 mouse_pos_to_filter;
  210. Vector2 relative_motion;
  211. Vector2 raw_pos;
  212. Vector2 old_raw_pos;
  213. ::Time last_relative_time;
  214. } xi;
  215. bool _refresh_device_info();
  216. Rect2i _screen_get_rect(int p_screen) const;
  217. BitField<MouseButtonMask> _get_mouse_button_state(MouseButton p_x11_button, int p_x11_type);
  218. void _get_key_modifier_state(unsigned int p_x11_state, Ref<InputEventWithModifiers> state);
  219. void _flush_mouse_motion();
  220. MouseMode mouse_mode = MOUSE_MODE_VISIBLE;
  221. Point2i center;
  222. void _handle_key_event(WindowID p_window, XKeyEvent *p_event, LocalVector<XEvent> &p_events, uint32_t &p_event_index, bool p_echo = false);
  223. Atom _process_selection_request_target(Atom p_target, Window p_requestor, Atom p_property, Atom p_selection) const;
  224. void _handle_selection_request_event(XSelectionRequestEvent *p_event) const;
  225. void _update_window_mouse_passthrough(WindowID p_window);
  226. String _clipboard_get_impl(Atom p_source, Window x11_window, Atom target) const;
  227. String _clipboard_get(Atom p_source, Window x11_window) const;
  228. void _clipboard_transfer_ownership(Atom p_source, Window x11_window) const;
  229. bool do_mouse_warp = false;
  230. const char *cursor_theme = nullptr;
  231. int cursor_size = 0;
  232. XcursorImage *img[CURSOR_MAX];
  233. Cursor cursors[CURSOR_MAX];
  234. Cursor null_cursor;
  235. CursorShape current_cursor = CURSOR_ARROW;
  236. HashMap<CursorShape, Vector<Variant>> cursors_cache;
  237. String rendering_driver;
  238. void set_wm_fullscreen(bool p_enabled);
  239. void set_wm_above(bool p_enabled);
  240. typedef xrr_monitor_info *(*xrr_get_monitors_t)(Display *dpy, Window window, Bool get_active, int *nmonitors);
  241. typedef void (*xrr_free_monitors_t)(xrr_monitor_info *monitors);
  242. xrr_get_monitors_t xrr_get_monitors = nullptr;
  243. xrr_free_monitors_t xrr_free_monitors = nullptr;
  244. void *xrandr_handle = nullptr;
  245. Bool xrandr_ext_ok;
  246. struct Property {
  247. unsigned char *data;
  248. int format, nitems;
  249. Atom type;
  250. };
  251. static Property _read_property(Display *p_display, Window p_window, Atom p_property);
  252. void _update_real_mouse_position(const WindowData &wd);
  253. bool _window_maximize_check(WindowID p_window, const char *p_atom_name) const;
  254. bool _window_fullscreen_check(WindowID p_window) const;
  255. bool _window_minimize_check(WindowID p_window) const;
  256. void _validate_mode_on_map(WindowID p_window);
  257. void _update_size_hints(WindowID p_window);
  258. void _set_wm_fullscreen(WindowID p_window, bool p_enabled, bool p_exclusive);
  259. void _set_wm_maximized(WindowID p_window, bool p_enabled);
  260. void _set_wm_minimized(WindowID p_window, bool p_enabled);
  261. void _update_context(WindowData &wd);
  262. Context context = CONTEXT_ENGINE;
  263. WindowID _get_focused_window_or_popup() const;
  264. void _send_window_event(const WindowData &wd, WindowEvent p_event);
  265. static void _dispatch_input_events(const Ref<InputEvent> &p_event);
  266. void _dispatch_input_event(const Ref<InputEvent> &p_event);
  267. mutable Mutex events_mutex;
  268. Thread events_thread;
  269. SafeFlag events_thread_done;
  270. LocalVector<XEvent> polled_events;
  271. static void _poll_events_thread(void *ud);
  272. bool _wait_for_events() const;
  273. void _poll_events();
  274. void _check_pending_events(LocalVector<XEvent> &r_events);
  275. static Bool _predicate_all_events(Display *display, XEvent *event, XPointer arg);
  276. static Bool _predicate_clipboard_selection(Display *display, XEvent *event, XPointer arg);
  277. static Bool _predicate_clipboard_incr(Display *display, XEvent *event, XPointer arg);
  278. static Bool _predicate_clipboard_save_targets(Display *display, XEvent *event, XPointer arg);
  279. protected:
  280. void _window_changed(XEvent *event);
  281. public:
  282. bool mouse_process_popups();
  283. void popup_open(WindowID p_window);
  284. void popup_close(WindowID p_window);
  285. virtual bool has_feature(Feature p_feature) const override;
  286. virtual String get_name() const override;
  287. #ifdef SPEECHD_ENABLED
  288. virtual bool tts_is_speaking() const override;
  289. virtual bool tts_is_paused() const override;
  290. virtual TypedArray<Dictionary> tts_get_voices() const override;
  291. virtual void tts_speak(const String &p_text, const String &p_voice, int p_volume = 50, float p_pitch = 1.f, float p_rate = 1.f, int p_utterance_id = 0, bool p_interrupt = false) override;
  292. virtual void tts_pause() override;
  293. virtual void tts_resume() override;
  294. virtual void tts_stop() override;
  295. #endif
  296. #if defined(DBUS_ENABLED)
  297. virtual bool is_dark_mode_supported() const override;
  298. virtual bool is_dark_mode() const override;
  299. #endif
  300. virtual void mouse_set_mode(MouseMode p_mode) override;
  301. virtual MouseMode mouse_get_mode() const override;
  302. virtual void warp_mouse(const Point2i &p_position) override;
  303. virtual Point2i mouse_get_position() const override;
  304. virtual BitField<MouseButtonMask> mouse_get_button_state() const override;
  305. virtual void clipboard_set(const String &p_text) override;
  306. virtual String clipboard_get() const override;
  307. virtual void clipboard_set_primary(const String &p_text) override;
  308. virtual String clipboard_get_primary() const override;
  309. virtual int get_screen_count() const override;
  310. virtual int get_primary_screen() const override;
  311. virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  312. virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  313. virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  314. virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  315. virtual float screen_get_refresh_rate(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  316. #if defined(DBUS_ENABLED)
  317. virtual void screen_set_keep_on(bool p_enable) override;
  318. virtual bool screen_is_kept_on() const override;
  319. #endif
  320. virtual Vector<DisplayServer::WindowID> get_window_list() const override;
  321. virtual WindowID create_sub_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect = Rect2i()) override;
  322. virtual void show_window(WindowID p_id) override;
  323. virtual void delete_sub_window(WindowID p_id) override;
  324. virtual WindowID window_get_active_popup() const override;
  325. virtual void window_set_popup_safe_rect(WindowID p_window, const Rect2i &p_rect) override;
  326. virtual Rect2i window_get_popup_safe_rect(WindowID p_window) const override;
  327. virtual WindowID get_window_at_screen_position(const Point2i &p_position) const override;
  328. virtual int64_t window_get_native_handle(HandleType p_handle_type, WindowID p_window = MAIN_WINDOW_ID) const override;
  329. virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID) override;
  330. virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const override;
  331. virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) override;
  332. virtual void window_set_mouse_passthrough(const Vector<Vector2> &p_region, WindowID p_window = MAIN_WINDOW_ID) override;
  333. virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  334. virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  335. virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  336. virtual void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  337. virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  338. virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const override;
  339. virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID) override;
  340. virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const override;
  341. virtual Point2i window_get_position_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
  342. virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID) override;
  343. virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  344. virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  345. virtual void gl_window_make_current(DisplayServer::WindowID p_window_id) override;
  346. virtual void window_set_transient(WindowID p_window, WindowID p_parent) override;
  347. virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  348. virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  349. virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  350. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  351. virtual Size2i window_get_size_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
  352. virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID) override;
  353. virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const override;
  354. virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const override;
  355. virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID) override;
  356. virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const override;
  357. virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID) override;
  358. virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID) override;
  359. virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const override;
  360. virtual bool can_any_window_draw() const override;
  361. virtual void window_set_ime_active(const bool p_active, WindowID p_window = MAIN_WINDOW_ID) override;
  362. virtual void window_set_ime_position(const Point2i &p_pos, WindowID p_window = MAIN_WINDOW_ID) override;
  363. virtual Point2i ime_get_selection() const override;
  364. virtual String ime_get_text() const override;
  365. virtual void window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window = MAIN_WINDOW_ID) override;
  366. virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override;
  367. virtual void cursor_set_shape(CursorShape p_shape) override;
  368. virtual CursorShape cursor_get_shape() const override;
  369. virtual void cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) override;
  370. virtual int keyboard_get_layout_count() const override;
  371. virtual int keyboard_get_current_layout() const override;
  372. virtual void keyboard_set_current_layout(int p_index) override;
  373. virtual String keyboard_get_layout_language(int p_index) const override;
  374. virtual String keyboard_get_layout_name(int p_index) const override;
  375. virtual Key keyboard_get_keycode_from_physical(Key p_keycode) const override;
  376. virtual void process_events() override;
  377. virtual void release_rendering_thread() override;
  378. virtual void make_rendering_thread() override;
  379. virtual void swap_buffers() override;
  380. virtual void set_context(Context p_context) override;
  381. virtual void set_native_icon(const String &p_filename) override;
  382. virtual void set_icon(const Ref<Image> &p_icon) override;
  383. static DisplayServer *create_func(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Error &r_error);
  384. static Vector<String> get_rendering_drivers_func();
  385. static void register_x11_driver();
  386. DisplayServerX11(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Error &r_error);
  387. ~DisplayServerX11();
  388. };
  389. #endif // X11 enabled
  390. #endif // DISPLAY_SERVER_X11_H