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
31#ifndef DISPLAY_SERVER_X11_H
32#define DISPLAY_SERVER_X11_H
33
34#ifdef X11_ENABLED
35
36#include "joypad_linux.h"
37
38#include "core/input/input.h"
39#include "core/os/mutex.h"
40#include "core/os/thread.h"
41#include "core/templates/local_vector.h"
42#include "drivers/alsa/audio_driver_alsa.h"
43#include "drivers/alsamidi/midi_driver_alsamidi.h"
44#include "drivers/pulseaudio/audio_driver_pulseaudio.h"
45#include "drivers/unix/os_unix.h"
46#include "servers/audio_server.h"
47#include "servers/display_server.h"
48#include "servers/rendering/renderer_compositor.h"
49#include "servers/rendering_server.h"
50
51#if defined(SPEECHD_ENABLED)
52#include "tts_linux.h"
53#endif
54
55#if defined(GLES3_ENABLED)
56#include "x11/gl_manager_x11.h"
57#endif
58
59#if defined(VULKAN_ENABLED)
60#include "x11/vulkan_context_x11.h"
61
62#include "drivers/vulkan/rendering_device_vulkan.h"
63#endif
64
65#if defined(DBUS_ENABLED)
66#include "freedesktop_portal_desktop.h"
67#include "freedesktop_screensaver.h"
68#endif
69
70#include <X11/Xatom.h>
71#include <X11/Xlib.h>
72#include <X11/Xutil.h>
73#include <X11/keysym.h>
74
75#ifdef SOWRAP_ENABLED
76#include "x11/dynwrappers/xlib-so_wrap.h"
77
78#include "x11/dynwrappers/xcursor-so_wrap.h"
79#include "x11/dynwrappers/xext-so_wrap.h"
80#include "x11/dynwrappers/xinerama-so_wrap.h"
81#include "x11/dynwrappers/xinput2-so_wrap.h"
82#include "x11/dynwrappers/xrandr-so_wrap.h"
83#include "x11/dynwrappers/xrender-so_wrap.h"
84
85#include "xkbcommon-so_wrap.h"
86#else
87#include <X11/XKBlib.h>
88#include <X11/Xlib.h>
89#include <X11/Xutil.h>
90
91#include <X11/Xcursor/Xcursor.h>
92#include <X11/extensions/XInput2.h>
93#include <X11/extensions/Xext.h>
94#include <X11/extensions/Xinerama.h>
95#include <X11/extensions/Xrandr.h>
96#include <X11/extensions/Xrender.h>
97#include <X11/extensions/shape.h>
98
99#ifdef XKB_ENABLED
100#include <xkbcommon/xkbcommon-compose.h>
101#include <xkbcommon/xkbcommon-keysyms.h>
102#include <xkbcommon/xkbcommon.h>
103#endif
104#endif
105
106typedef struct _xrr_monitor_info {
107 Atom name;
108 Bool primary = false;
109 Bool automatic = false;
110 int noutput = 0;
111 int x = 0;
112 int y = 0;
113 int width = 0;
114 int height = 0;
115 int mwidth = 0;
116 int mheight = 0;
117 RROutput *outputs = nullptr;
118} xrr_monitor_info;
119
120#undef CursorShape
121
122class DisplayServerX11 : public DisplayServer {
123 // No need to register with GDCLASS, it's platform-specific and nothing is added.
124
125 _THREAD_SAFE_CLASS_
126
127 Atom wm_delete;
128 Atom xdnd_enter;
129 Atom xdnd_position;
130 Atom xdnd_status;
131 Atom xdnd_action_copy;
132 Atom xdnd_drop;
133 Atom xdnd_finished;
134 Atom xdnd_selection;
135 Atom xdnd_aware;
136 Atom requested = None;
137 int xdnd_version = 5;
138
139#if defined(GLES3_ENABLED)
140 GLManager_X11 *gl_manager = nullptr;
141#endif
142#if defined(VULKAN_ENABLED)
143 VulkanContextX11 *context_vulkan = nullptr;
144 RenderingDeviceVulkan *rendering_device_vulkan = nullptr;
145#endif
146
147#if defined(DBUS_ENABLED)
148 FreeDesktopScreenSaver *screensaver = nullptr;
149 bool keep_screen_on = false;
150#endif
151
152#ifdef SPEECHD_ENABLED
153 TTS_Linux *tts = nullptr;
154#endif
155
156#if defined(DBUS_ENABLED)
157 FreeDesktopPortalDesktop *portal_desktop = nullptr;
158#endif
159
160 struct WindowData {
161 Window x11_window;
162 Window x11_xim_window;
163 Window parent;
164 ::XIC xic;
165 bool ime_active = false;
166 bool ime_in_progress = false;
167 bool ime_suppress_next_keyup = false;
168#ifdef XKB_ENABLED
169 xkb_compose_state *xkb_state = nullptr;
170#endif
171
172 Size2i min_size;
173 Size2i max_size;
174 Point2i position;
175 Size2i size;
176 Callable rect_changed_callback;
177 Callable event_callback;
178 Callable input_event_callback;
179 Callable input_text_callback;
180 Callable drop_files_callback;
181
182 Vector<Vector2> mpath;
183
184 WindowID transient_parent = INVALID_WINDOW_ID;
185 HashSet<WindowID> transient_children;
186
187 ObjectID instance_id;
188
189 bool no_focus = false;
190
191 //better to guess on the fly, given WM can change it
192 //WindowMode mode;
193 bool fullscreen = false; //OS can't exit from this mode
194 bool exclusive_fullscreen = false;
195 bool on_top = false;
196 bool borderless = false;
197 bool resize_disabled = false;
198 Vector2i last_position_before_fs;
199 bool focused = true;
200 bool minimized = false;
201 bool maximized = false;
202 bool is_popup = false;
203 bool layered_window = false;
204 bool mpass = false;
205
206 Rect2i parent_safe_rect;
207
208 unsigned int focus_order = 0;
209 };
210
211 Point2i im_selection;
212 String im_text;
213
214#ifdef XKB_ENABLED
215 bool xkb_loaded_v05p = false;
216 bool xkb_loaded_v08p = false;
217 xkb_context *xkb_ctx = nullptr;
218 xkb_compose_table *dead_tbl = nullptr;
219#endif
220
221 HashMap<WindowID, WindowData> windows;
222
223 unsigned int last_mouse_monitor_mask = 0;
224 uint64_t time_since_popup = 0;
225
226 List<WindowID> popup_list;
227
228 WindowID window_mouseover_id = INVALID_WINDOW_ID;
229 WindowID last_focused_window = INVALID_WINDOW_ID;
230
231 WindowID window_id_counter = MAIN_WINDOW_ID;
232 WindowID _create_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect);
233
234 String internal_clipboard;
235 String internal_clipboard_primary;
236 Window xdnd_source_window = 0;
237 ::Display *x11_display;
238 char *xmbstring = nullptr;
239 int xmblen = 0;
240 unsigned long last_timestamp = 0;
241 ::Time last_keyrelease_time = 0;
242 ::XIM xim;
243 ::XIMStyle xim_style;
244
245 static int _xim_preedit_start_callback(::XIM xim, ::XPointer client_data,
246 ::XPointer call_data);
247 static void _xim_preedit_done_callback(::XIM xim, ::XPointer client_data,
248 ::XPointer call_data);
249 static void _xim_preedit_draw_callback(::XIM xim, ::XPointer client_data,
250 ::XIMPreeditDrawCallbackStruct *call_data);
251 static void _xim_preedit_caret_callback(::XIM xim, ::XPointer client_data,
252 ::XIMPreeditCaretCallbackStruct *call_data);
253 static void _xim_destroy_callback(::XIM im, ::XPointer client_data,
254 ::XPointer call_data);
255
256 Point2i last_mouse_pos;
257 bool last_mouse_pos_valid = false;
258 Point2i last_click_pos = Point2i(-100, -100);
259 uint64_t last_click_ms = 0;
260 MouseButton last_click_button_index = MouseButton::NONE;
261 BitField<MouseButtonMask> last_button_state;
262 bool app_focused = false;
263 uint64_t time_since_no_focus = 0;
264
265 struct {
266 int opcode;
267 Vector<int> touch_devices;
268 HashMap<int, Vector2> absolute_devices;
269 HashMap<int, Vector2> pen_pressure_range;
270 HashMap<int, Vector2> pen_tilt_x_range;
271 HashMap<int, Vector2> pen_tilt_y_range;
272 HashMap<int, bool> pen_inverted_devices;
273 XIEventMask all_event_mask;
274 HashMap<int, Vector2> state;
275 double pressure;
276 bool pressure_supported;
277 bool pen_inverted;
278 Vector2 tilt;
279 Vector2 mouse_pos_to_filter;
280 Vector2 relative_motion;
281 Vector2 raw_pos;
282 Vector2 old_raw_pos;
283 ::Time last_relative_time;
284 } xi;
285
286 bool _refresh_device_info();
287
288 Rect2i _screen_get_rect(int p_screen) const;
289
290 BitField<MouseButtonMask> _get_mouse_button_state(MouseButton p_x11_button, int p_x11_type);
291 void _get_key_modifier_state(unsigned int p_x11_state, Ref<InputEventWithModifiers> state);
292 void _flush_mouse_motion();
293
294 MouseMode mouse_mode = MOUSE_MODE_VISIBLE;
295 Point2i center;
296
297 void _handle_key_event(WindowID p_window, XKeyEvent *p_event, LocalVector<XEvent> &p_events, uint32_t &p_event_index, bool p_echo = false);
298
299 Atom _process_selection_request_target(Atom p_target, Window p_requestor, Atom p_property, Atom p_selection) const;
300 void _handle_selection_request_event(XSelectionRequestEvent *p_event) const;
301 void _update_window_mouse_passthrough(WindowID p_window);
302
303 String _clipboard_get_impl(Atom p_source, Window x11_window, Atom target) const;
304 String _clipboard_get(Atom p_source, Window x11_window) const;
305 void _clipboard_transfer_ownership(Atom p_source, Window x11_window) const;
306
307 bool do_mouse_warp = false;
308
309 const char *cursor_theme = nullptr;
310 int cursor_size = 0;
311 XcursorImage *cursor_img[CURSOR_MAX];
312 Cursor cursors[CURSOR_MAX];
313 Cursor null_cursor;
314 CursorShape current_cursor = CURSOR_ARROW;
315 HashMap<CursorShape, Vector<Variant>> cursors_cache;
316
317 String rendering_driver;
318 void set_wm_fullscreen(bool p_enabled);
319 void set_wm_above(bool p_enabled);
320
321 typedef xrr_monitor_info *(*xrr_get_monitors_t)(Display *dpy, Window window, Bool get_active, int *nmonitors);
322 typedef void (*xrr_free_monitors_t)(xrr_monitor_info *monitors);
323 xrr_get_monitors_t xrr_get_monitors = nullptr;
324 xrr_free_monitors_t xrr_free_monitors = nullptr;
325 void *xrandr_handle = nullptr;
326 bool xrandr_ext_ok = true;
327 bool xinerama_ext_ok = true;
328 bool xshaped_ext_ok = true;
329
330 struct Property {
331 unsigned char *data;
332 int format, nitems;
333 Atom type;
334 };
335 static Property _read_property(Display *p_display, Window p_window, Atom p_property);
336
337 void _update_real_mouse_position(const WindowData &wd);
338 bool _window_maximize_check(WindowID p_window, const char *p_atom_name) const;
339 bool _window_fullscreen_check(WindowID p_window) const;
340 bool _window_minimize_check(WindowID p_window) const;
341 void _validate_mode_on_map(WindowID p_window);
342 void _update_size_hints(WindowID p_window);
343 void _set_wm_fullscreen(WindowID p_window, bool p_enabled, bool p_exclusive);
344 void _set_wm_maximized(WindowID p_window, bool p_enabled);
345 void _set_wm_minimized(WindowID p_window, bool p_enabled);
346
347 void _update_context(WindowData &wd);
348
349 Context context = CONTEXT_ENGINE;
350
351 WindowID _get_focused_window_or_popup() const;
352
353 void _send_window_event(const WindowData &wd, WindowEvent p_event);
354 static void _dispatch_input_events(const Ref<InputEvent> &p_event);
355 void _dispatch_input_event(const Ref<InputEvent> &p_event);
356
357 mutable Mutex events_mutex;
358 Thread events_thread;
359 SafeFlag events_thread_done;
360 LocalVector<XEvent> polled_events;
361 static void _poll_events_thread(void *ud);
362 bool _wait_for_events() const;
363 void _poll_events();
364 void _check_pending_events(LocalVector<XEvent> &r_events);
365
366 static Bool _predicate_all_events(Display *display, XEvent *event, XPointer arg);
367 static Bool _predicate_clipboard_selection(Display *display, XEvent *event, XPointer arg);
368 static Bool _predicate_clipboard_incr(Display *display, XEvent *event, XPointer arg);
369 static Bool _predicate_clipboard_save_targets(Display *display, XEvent *event, XPointer arg);
370
371protected:
372 void _window_changed(XEvent *event);
373
374public:
375 bool mouse_process_popups();
376 void popup_open(WindowID p_window);
377 void popup_close(WindowID p_window);
378
379 virtual bool has_feature(Feature p_feature) const override;
380 virtual String get_name() const override;
381
382#ifdef SPEECHD_ENABLED
383 virtual bool tts_is_speaking() const override;
384 virtual bool tts_is_paused() const override;
385 virtual TypedArray<Dictionary> tts_get_voices() const override;
386
387 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;
388 virtual void tts_pause() override;
389 virtual void tts_resume() override;
390 virtual void tts_stop() override;
391#endif
392
393#if defined(DBUS_ENABLED)
394 virtual bool is_dark_mode_supported() const override;
395 virtual bool is_dark_mode() const override;
396
397 virtual Error file_dialog_show(const String &p_title, const String &p_current_directory, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback) override;
398#endif
399
400 virtual void mouse_set_mode(MouseMode p_mode) override;
401 virtual MouseMode mouse_get_mode() const override;
402
403 virtual void warp_mouse(const Point2i &p_position) override;
404 virtual Point2i mouse_get_position() const override;
405 virtual BitField<MouseButtonMask> mouse_get_button_state() const override;
406
407 virtual void clipboard_set(const String &p_text) override;
408 virtual String clipboard_get() const override;
409 virtual void clipboard_set_primary(const String &p_text) override;
410 virtual String clipboard_get_primary() const override;
411
412 virtual int get_screen_count() const override;
413 virtual int get_primary_screen() const override;
414 virtual int get_keyboard_focus_screen() const override;
415 virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
416 virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
417 virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
418 virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
419 virtual float screen_get_refresh_rate(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
420 virtual Color screen_get_pixel(const Point2i &p_position) const override;
421 virtual Ref<Image> screen_get_image(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
422
423#if defined(DBUS_ENABLED)
424 virtual void screen_set_keep_on(bool p_enable) override;
425 virtual bool screen_is_kept_on() const override;
426#endif
427
428 virtual Vector<DisplayServer::WindowID> get_window_list() const override;
429
430 virtual WindowID create_sub_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect = Rect2i()) override;
431 virtual void show_window(WindowID p_id) override;
432 virtual void delete_sub_window(WindowID p_id) override;
433
434 virtual WindowID window_get_active_popup() const override;
435 virtual void window_set_popup_safe_rect(WindowID p_window, const Rect2i &p_rect) override;
436 virtual Rect2i window_get_popup_safe_rect(WindowID p_window) const override;
437
438 virtual WindowID get_window_at_screen_position(const Point2i &p_position) const override;
439
440 virtual int64_t window_get_native_handle(HandleType p_handle_type, WindowID p_window = MAIN_WINDOW_ID) const override;
441
442 virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID) override;
443 virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const override;
444
445 virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) override;
446 virtual void window_set_mouse_passthrough(const Vector<Vector2> &p_region, WindowID p_window = MAIN_WINDOW_ID) override;
447
448 virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
449 virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
450 virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
451 virtual void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
452 virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
453
454 virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const override;
455 virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID) override;
456
457 virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const override;
458 virtual Point2i window_get_position_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
459 virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID) override;
460
461 virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
462 virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const override;
463 virtual void gl_window_make_current(DisplayServer::WindowID p_window_id) override;
464
465 virtual void window_set_transient(WindowID p_window, WindowID p_parent) override;
466
467 virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
468 virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const override;
469
470 virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
471 virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override;
472 virtual Size2i window_get_size_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
473
474 virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID) override;
475 virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const override;
476
477 virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const override;
478
479 virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID) override;
480 virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const override;
481
482 virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID) override;
483
484 virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID) override;
485 virtual bool window_is_focused(WindowID p_window = MAIN_WINDOW_ID) const override;
486
487 virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const override;
488
489 virtual bool can_any_window_draw() const override;
490
491 virtual void window_set_ime_active(const bool p_active, WindowID p_window = MAIN_WINDOW_ID) override;
492 virtual void window_set_ime_position(const Point2i &p_pos, WindowID p_window = MAIN_WINDOW_ID) override;
493
494 virtual Point2i ime_get_selection() const override;
495 virtual String ime_get_text() const override;
496
497 virtual void window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window = MAIN_WINDOW_ID) override;
498 virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override;
499
500 virtual void cursor_set_shape(CursorShape p_shape) override;
501 virtual CursorShape cursor_get_shape() const override;
502 virtual void cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) override;
503
504 virtual int keyboard_get_layout_count() const override;
505 virtual int keyboard_get_current_layout() const override;
506 virtual void keyboard_set_current_layout(int p_index) override;
507 virtual String keyboard_get_layout_language(int p_index) const override;
508 virtual String keyboard_get_layout_name(int p_index) const override;
509 virtual Key keyboard_get_keycode_from_physical(Key p_keycode) const override;
510 virtual Key keyboard_get_label_from_physical(Key p_keycode) const override;
511
512 virtual void process_events() override;
513
514 virtual void release_rendering_thread() override;
515 virtual void make_rendering_thread() override;
516 virtual void swap_buffers() override;
517
518 virtual void set_context(Context p_context) override;
519
520 virtual void set_native_icon(const String &p_filename) override;
521 virtual void set_icon(const Ref<Image> &p_icon) override;
522
523 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);
524 static Vector<String> get_rendering_drivers_func();
525
526 static void register_x11_driver();
527
528 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);
529 ~DisplayServerX11();
530};
531
532#endif // X11_ENABLED
533
534#endif // DISPLAY_SERVER_X11_H
535