wayland_thread.h 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /**************************************************************************/
  2. /* wayland_thread.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. #pragma once
  31. #ifdef WAYLAND_ENABLED
  32. #include "key_mapping_xkb.h"
  33. #ifdef SOWRAP_ENABLED
  34. #include "wayland/dynwrappers/wayland-client-core-so_wrap.h"
  35. #include "wayland/dynwrappers/wayland-cursor-so_wrap.h"
  36. #include "wayland/dynwrappers/wayland-egl-core-so_wrap.h"
  37. #include "xkbcommon-so_wrap.h"
  38. #else
  39. #include <wayland-client-core.h>
  40. #include <wayland-cursor.h>
  41. #ifdef GLES3_ENABLED
  42. #include <wayland-egl-core.h>
  43. #endif
  44. #include <xkbcommon/xkbcommon-compose.h>
  45. #include <xkbcommon/xkbcommon.h>
  46. #endif // SOWRAP_ENABLED
  47. // These must go after the Wayland client include to work properly.
  48. #include "wayland/protocol/idle_inhibit.gen.h"
  49. #include "wayland/protocol/primary_selection.gen.h"
  50. // These four protocol headers name wl_pointer method arguments as `pointer`,
  51. // which is the same name as X11's pointer typedef. This trips some very
  52. // annoying shadowing warnings. A `#define` works around this issue.
  53. #define pointer wl_pointer
  54. #include "wayland/protocol/cursor_shape.gen.h"
  55. #include "wayland/protocol/pointer_constraints.gen.h"
  56. #include "wayland/protocol/pointer_gestures.gen.h"
  57. #include "wayland/protocol/relative_pointer.gen.h"
  58. #undef pointer
  59. #include "wayland/protocol/fractional_scale.gen.h"
  60. #include "wayland/protocol/tablet.gen.h"
  61. #include "wayland/protocol/text_input.gen.h"
  62. #include "wayland/protocol/viewporter.gen.h"
  63. #include "wayland/protocol/wayland.gen.h"
  64. #include "wayland/protocol/xdg_activation.gen.h"
  65. #include "wayland/protocol/xdg_decoration.gen.h"
  66. #include "wayland/protocol/xdg_foreign_v2.gen.h"
  67. #include "wayland/protocol/xdg_shell.gen.h"
  68. #include "wayland/protocol/xdg_system_bell.gen.h"
  69. #include "wayland/protocol/xdg_toplevel_icon.gen.h"
  70. #include "wayland/protocol/godot_embedding_compositor.gen.h"
  71. // NOTE: Deprecated.
  72. #include "wayland/protocol/xdg_foreign_v1.gen.h"
  73. #ifdef LIBDECOR_ENABLED
  74. #ifdef SOWRAP_ENABLED
  75. #include "dynwrappers/libdecor-so_wrap.h"
  76. #else
  77. #include <libdecor.h>
  78. #endif // SOWRAP_ENABLED
  79. #endif // LIBDECOR_ENABLED
  80. #include "core/os/thread.h"
  81. #include "servers/display/display_server.h"
  82. #include "wayland_embedder.h"
  83. class WaylandThread {
  84. public:
  85. // Messages used for exchanging information between Godot's and Wayland's thread.
  86. class Message : public RefCounted {
  87. GDSOFTCLASS(Message, RefCounted);
  88. public:
  89. Message() {}
  90. virtual ~Message() = default;
  91. };
  92. class WindowMessage : public Message {
  93. GDSOFTCLASS(WindowMessage, Message);
  94. public:
  95. DisplayServer::WindowID id = DisplayServer::INVALID_WINDOW_ID;
  96. };
  97. // Message data for window rect changes.
  98. class WindowRectMessage : public WindowMessage {
  99. GDSOFTCLASS(WindowRectMessage, WindowMessage);
  100. public:
  101. // NOTE: This is in "scaled" terms. For example, if there's a 1920x1080 rect
  102. // with a scale factor of 2, the actual value of `rect` will be 3840x2160.
  103. Rect2i rect;
  104. };
  105. class WindowEventMessage : public WindowMessage {
  106. GDSOFTCLASS(WindowEventMessage, WindowMessage);
  107. public:
  108. DisplayServer::WindowEvent event;
  109. };
  110. class InputEventMessage : public Message {
  111. GDSOFTCLASS(InputEventMessage, Message);
  112. public:
  113. Ref<InputEvent> event;
  114. };
  115. class DropFilesEventMessage : public WindowMessage {
  116. GDSOFTCLASS(DropFilesEventMessage, WindowMessage);
  117. public:
  118. Vector<String> files;
  119. };
  120. class IMEUpdateEventMessage : public WindowMessage {
  121. GDSOFTCLASS(IMEUpdateEventMessage, WindowMessage);
  122. public:
  123. String text;
  124. Vector2i selection;
  125. };
  126. class IMECommitEventMessage : public WindowMessage {
  127. GDSOFTCLASS(IMECommitEventMessage, WindowMessage);
  128. public:
  129. String text;
  130. };
  131. struct RegistryState {
  132. WaylandThread *wayland_thread;
  133. // Core Wayland globals.
  134. struct wl_shm *wl_shm = nullptr;
  135. uint32_t wl_shm_name = 0;
  136. struct wl_compositor *wl_compositor = nullptr;
  137. uint32_t wl_compositor_name = 0;
  138. struct wl_subcompositor *wl_subcompositor = nullptr;
  139. uint32_t wl_subcompositor_name = 0;
  140. struct wl_data_device_manager *wl_data_device_manager = nullptr;
  141. uint32_t wl_data_device_manager_name = 0;
  142. List<struct wl_output *> wl_outputs;
  143. List<struct wl_seat *> wl_seats;
  144. // xdg-shell globals.
  145. struct xdg_wm_base *xdg_wm_base = nullptr;
  146. uint32_t xdg_wm_base_name = 0;
  147. // NOTE: Deprecated.
  148. struct zxdg_exporter_v1 *xdg_exporter_v1 = nullptr;
  149. uint32_t xdg_exporter_v1_name = 0;
  150. uint32_t xdg_exporter_v2_name = 0;
  151. struct zxdg_exporter_v2 *xdg_exporter_v2 = nullptr;
  152. // wayland-protocols globals.
  153. struct wp_viewporter *wp_viewporter = nullptr;
  154. uint32_t wp_viewporter_name = 0;
  155. struct wp_fractional_scale_manager_v1 *wp_fractional_scale_manager = nullptr;
  156. uint32_t wp_fractional_scale_manager_name = 0;
  157. struct wp_cursor_shape_manager_v1 *wp_cursor_shape_manager = nullptr;
  158. uint32_t wp_cursor_shape_manager_name = 0;
  159. struct zxdg_decoration_manager_v1 *xdg_decoration_manager = nullptr;
  160. uint32_t xdg_decoration_manager_name = 0;
  161. struct xdg_system_bell_v1 *xdg_system_bell = nullptr;
  162. uint32_t xdg_system_bell_name = 0;
  163. struct xdg_toplevel_icon_manager_v1 *xdg_toplevel_icon_manager = nullptr;
  164. uint32_t xdg_toplevel_icon_manager_name = 0;
  165. struct xdg_activation_v1 *xdg_activation = nullptr;
  166. uint32_t xdg_activation_name = 0;
  167. struct zwp_primary_selection_device_manager_v1 *wp_primary_selection_device_manager = nullptr;
  168. uint32_t wp_primary_selection_device_manager_name = 0;
  169. struct zwp_relative_pointer_manager_v1 *wp_relative_pointer_manager = nullptr;
  170. uint32_t wp_relative_pointer_manager_name = 0;
  171. struct zwp_pointer_constraints_v1 *wp_pointer_constraints = nullptr;
  172. uint32_t wp_pointer_constraints_name = 0;
  173. struct zwp_pointer_gestures_v1 *wp_pointer_gestures = nullptr;
  174. uint32_t wp_pointer_gestures_name = 0;
  175. struct zwp_idle_inhibit_manager_v1 *wp_idle_inhibit_manager = nullptr;
  176. uint32_t wp_idle_inhibit_manager_name = 0;
  177. struct zwp_tablet_manager_v2 *wp_tablet_manager = nullptr;
  178. uint32_t wp_tablet_manager_name = 0;
  179. struct zwp_text_input_manager_v3 *wp_text_input_manager = nullptr;
  180. uint32_t wp_text_input_manager_name = 0;
  181. // We're really not meant to use this one directly but we still need to know
  182. // whether it's available.
  183. uint32_t wp_fifo_manager_name = 0;
  184. struct godot_embedding_compositor *godot_embedding_compositor = nullptr;
  185. uint32_t godot_embedding_compositor_name = 0;
  186. };
  187. // General Wayland-specific states. Shouldn't be accessed directly.
  188. // TODO: Make private?
  189. struct WindowState {
  190. DisplayServer::WindowID id = DisplayServer::INVALID_WINDOW_ID;
  191. DisplayServer::WindowID parent_id = DisplayServer::INVALID_WINDOW_ID;
  192. Rect2i rect;
  193. DisplayServer::WindowMode mode = DisplayServer::WINDOW_MODE_WINDOWED;
  194. bool suspended = false;
  195. // These are true by default as it isn't guaranteed that we'll find an
  196. // xdg-shell implementation with wm_capabilities available. If and once we
  197. // receive a wm_capabilities event these will get reset and updated with
  198. // whatever the compositor says.
  199. bool can_minimize = false;
  200. bool can_maximize = false;
  201. bool can_fullscreen = false;
  202. HashSet<struct wl_output *> wl_outputs;
  203. // NOTE: If for whatever reason this callback is destroyed _while_ the event
  204. // thread is still running, it might be a good idea to set its user data to
  205. // `nullptr`. From some initial testing of mine, it looks like it might still
  206. // be called even after being destroyed, pointing to probably invalid window
  207. // data by then and segfaulting hard.
  208. struct wl_callback *frame_callback = nullptr;
  209. uint64_t last_frame_time = 0;
  210. struct wl_surface *wl_surface = nullptr;
  211. struct xdg_surface *xdg_surface = nullptr;
  212. struct xdg_toplevel *xdg_toplevel = nullptr;
  213. struct wp_viewport *wp_viewport = nullptr;
  214. struct wp_fractional_scale_v1 *wp_fractional_scale = nullptr;
  215. // NOTE: Deprecated.
  216. struct zxdg_exported_v1 *xdg_exported_v1 = nullptr;
  217. struct zxdg_exported_v2 *xdg_exported_v2 = nullptr;
  218. struct xdg_popup *xdg_popup = nullptr;
  219. String exported_handle;
  220. // Currently applied buffer scale.
  221. int buffer_scale = 1;
  222. // Buffer scale must be applied right before rendering but _after_ committing
  223. // everything else or otherwise we might have an inconsistent state (e.g.
  224. // double scale and odd resolution). This flag assists with that; when set,
  225. // on the next frame, we'll commit whatever is set in `buffer_scale`.
  226. bool buffer_scale_changed = false;
  227. // NOTE: The preferred buffer scale is currently only dynamically calculated.
  228. // It can be accessed by calling `window_state_get_preferred_buffer_scale`.
  229. // Override used by the fractional scale add-on object. If less or equal to 0
  230. // (default) then the normal output-based scale is used instead.
  231. double fractional_scale = 0;
  232. // What the compositor is recommending us.
  233. double preferred_fractional_scale = 0;
  234. struct zxdg_toplevel_decoration_v1 *xdg_toplevel_decoration = nullptr;
  235. struct zwp_idle_inhibitor_v1 *wp_idle_inhibitor = nullptr;
  236. #ifdef LIBDECOR_ENABLED
  237. // If this is null the xdg_* variables must be set and vice-versa. This way we
  238. // can handle this mess gracefully enough to hopefully being able of getting
  239. // rid of this cleanly once we have our own CSDs.
  240. struct libdecor_frame *libdecor_frame = nullptr;
  241. struct libdecor_configuration *pending_libdecor_configuration = nullptr;
  242. #endif
  243. RegistryState *registry;
  244. WaylandThread *wayland_thread;
  245. };
  246. // "High level" Godot-side screen data.
  247. struct ScreenData {
  248. // Geometry data.
  249. Point2i position;
  250. String make;
  251. String model;
  252. Size2i size;
  253. Size2i physical_size;
  254. float refresh_rate = -1;
  255. int scale = 1;
  256. };
  257. struct ScreenState {
  258. uint32_t wl_output_name = 0;
  259. ScreenData pending_data;
  260. ScreenData data;
  261. WaylandThread *wayland_thread;
  262. };
  263. enum class Gesture {
  264. NONE,
  265. MAGNIFY,
  266. };
  267. enum class PointerConstraint {
  268. NONE,
  269. LOCKED,
  270. CONFINED,
  271. };
  272. struct PointerData {
  273. Point2 position;
  274. uint32_t motion_time = 0;
  275. // Relative motion has its own optional event and so needs its own time.
  276. Vector2 relative_motion;
  277. uint32_t relative_motion_time = 0;
  278. BitField<MouseButtonMask> pressed_button_mask = MouseButtonMask::NONE;
  279. MouseButton last_button_pressed = MouseButton::NONE;
  280. Point2 last_pressed_position;
  281. DisplayServer::WindowID pointed_id = DisplayServer::INVALID_WINDOW_ID;
  282. DisplayServer::WindowID last_pointed_id = DisplayServer::INVALID_WINDOW_ID;
  283. // This is needed to check for a new double click every time.
  284. bool double_click_begun = false;
  285. uint32_t button_time = 0;
  286. uint32_t button_serial = 0;
  287. uint32_t scroll_type = WL_POINTER_AXIS_SOURCE_WHEEL;
  288. // The amount "scrolled" in pixels, in each direction.
  289. Vector2 scroll_vector;
  290. // The amount of scroll "clicks" in each direction, in fractions of 120.
  291. Vector2i discrete_scroll_vector_120;
  292. uint32_t pinch_scale = 1;
  293. };
  294. struct TabletToolData {
  295. Point2 position;
  296. Vector2 tilt;
  297. uint32_t pressure = 0;
  298. BitField<MouseButtonMask> pressed_button_mask = MouseButtonMask::NONE;
  299. MouseButton last_button_pressed = MouseButton::NONE;
  300. Point2 last_pressed_position;
  301. bool double_click_begun = false;
  302. uint64_t button_time = 0;
  303. uint64_t motion_time = 0;
  304. DisplayServer::WindowID proximal_id = DisplayServer::INVALID_WINDOW_ID;
  305. DisplayServer::WindowID last_proximal_id = DisplayServer::INVALID_WINDOW_ID;
  306. uint32_t proximity_serial = 0;
  307. };
  308. struct TabletToolState {
  309. struct wl_seat *wl_seat = nullptr;
  310. bool is_eraser = false;
  311. TabletToolData data_pending;
  312. TabletToolData data;
  313. };
  314. struct OfferState {
  315. HashSet<String> mime_types;
  316. };
  317. struct SeatState {
  318. RegistryState *registry = nullptr;
  319. WaylandThread *wayland_thread = nullptr;
  320. struct wl_seat *wl_seat = nullptr;
  321. uint32_t wl_seat_name = 0;
  322. // Pointer.
  323. struct wl_pointer *wl_pointer = nullptr;
  324. uint32_t pointer_enter_serial = 0;
  325. struct wp_cursor_shape_device_v1 *wp_cursor_shape_device = nullptr;
  326. struct zwp_relative_pointer_v1 *wp_relative_pointer = nullptr;
  327. struct zwp_locked_pointer_v1 *wp_locked_pointer = nullptr;
  328. struct zwp_confined_pointer_v1 *wp_confined_pointer = nullptr;
  329. struct zwp_pointer_gesture_pinch_v1 *wp_pointer_gesture_pinch = nullptr;
  330. // NOTE: According to the wp_pointer_gestures protocol specification, there
  331. // can be only one active gesture at a time.
  332. Gesture active_gesture = Gesture::NONE;
  333. // Used for delta calculations.
  334. // NOTE: The wp_pointer_gestures protocol keeps track of the total scale of
  335. // the pinch gesture, while godot instead wants its delta.
  336. wl_fixed_t old_pinch_scale = 0;
  337. struct wl_surface *cursor_surface = nullptr;
  338. struct wl_callback *cursor_frame_callback = nullptr;
  339. uint32_t cursor_time_ms = 0;
  340. // This variable is needed to buffer all pointer changes until a
  341. // wl_pointer.frame event, as per Wayland's specification. Everything is
  342. // first set in `data_buffer` and then `data` is set with its contents on
  343. // an input frame event. All methods should generally read from
  344. // `pointer_data` and write to `data_buffer`.
  345. PointerData pointer_data_buffer;
  346. PointerData pointer_data;
  347. // Keyboard.
  348. struct wl_keyboard *wl_keyboard = nullptr;
  349. // For key events.
  350. DisplayServer::WindowID focused_id = DisplayServer::INVALID_WINDOW_ID;
  351. struct xkb_context *xkb_context = nullptr;
  352. struct xkb_keymap *xkb_keymap = nullptr;
  353. struct xkb_state *xkb_state = nullptr;
  354. struct xkb_compose_state *xkb_compose_state = nullptr;
  355. struct xkb_compose_table *xkb_compose_table = nullptr;
  356. const char *keymap_buffer = nullptr;
  357. uint32_t keymap_buffer_size = 0;
  358. HashMap<xkb_keycode_t, Key> pressed_keycodes;
  359. xkb_layout_index_t current_layout_index = 0;
  360. // Clients with `wl_seat`s older than version 4 do not support
  361. // `wl_keyboard::repeat_info`, so we'll provide a reasonable default of 25
  362. // keys per second, with a start delay of 600 milliseconds.
  363. int32_t repeat_key_delay_msec = 1000 / 25;
  364. int32_t repeat_start_delay_msec = 600;
  365. xkb_keycode_t repeating_keycode = XKB_KEYCODE_INVALID;
  366. uint64_t last_repeat_start_msec = 0;
  367. uint64_t last_repeat_msec = 0;
  368. uint32_t mods_depressed = 0;
  369. uint32_t mods_latched = 0;
  370. uint32_t mods_locked = 0;
  371. bool shift_pressed = false;
  372. bool ctrl_pressed = false;
  373. bool alt_pressed = false;
  374. bool meta_pressed = false;
  375. uint32_t last_key_pressed_serial = 0;
  376. struct wl_data_device *wl_data_device = nullptr;
  377. // Drag and drop.
  378. DisplayServer::WindowID dnd_id = DisplayServer::INVALID_WINDOW_ID;
  379. struct wl_data_offer *wl_data_offer_dnd = nullptr;
  380. uint32_t dnd_enter_serial = 0;
  381. // Clipboard.
  382. struct wl_data_source *wl_data_source_selection = nullptr;
  383. Vector<uint8_t> selection_data;
  384. struct wl_data_offer *wl_data_offer_selection = nullptr;
  385. // Primary selection.
  386. struct zwp_primary_selection_device_v1 *wp_primary_selection_device = nullptr;
  387. struct zwp_primary_selection_source_v1 *wp_primary_selection_source = nullptr;
  388. Vector<uint8_t> primary_data;
  389. struct zwp_primary_selection_offer_v1 *wp_primary_selection_offer = nullptr;
  390. // Tablet.
  391. struct zwp_tablet_seat_v2 *wp_tablet_seat = nullptr;
  392. List<struct zwp_tablet_tool_v2 *> tablet_tools;
  393. // IME.
  394. struct zwp_text_input_v3 *wp_text_input = nullptr;
  395. DisplayServer::WindowID ime_window_id = DisplayServer::INVALID_WINDOW_ID;
  396. bool ime_enabled = false;
  397. bool ime_active = false;
  398. String ime_text;
  399. String ime_text_commit;
  400. Vector2i ime_cursor;
  401. Rect2i ime_rect;
  402. };
  403. struct CustomCursor {
  404. struct wl_buffer *wl_buffer = nullptr;
  405. uint32_t *buffer_data = nullptr;
  406. uint32_t buffer_data_size = 0;
  407. Point2i hotspot;
  408. };
  409. struct EmbeddingCompositorState {
  410. LocalVector<struct godot_embedded_client *> clients;
  411. // Only a client per PID can create a window.
  412. HashMap<int, struct godot_embedded_client *> mapped_clients;
  413. OS::ProcessID focused_pid = -1;
  414. };
  415. struct EmbeddedClientState {
  416. struct godot_embedding_compositor *embedding_compositor = nullptr;
  417. uint32_t pid = 0;
  418. bool window_mapped = false;
  419. };
  420. private:
  421. struct ThreadData {
  422. SafeFlag thread_done;
  423. Mutex mutex;
  424. struct wl_display *wl_display = nullptr;
  425. };
  426. // FIXME: Is this the right thing to do?
  427. inline static const char *proxy_tag = "godot";
  428. Thread events_thread;
  429. ThreadData thread_data;
  430. HashMap<DisplayServer::WindowID, WindowState> windows;
  431. List<Ref<Message>> messages;
  432. xdg_toplevel_icon_v1 *xdg_icon = nullptr;
  433. wl_buffer *icon_buffer = nullptr;
  434. String cursor_theme_name;
  435. int unscaled_cursor_size = 24;
  436. // NOTE: Regarding screen scale handling, the cursor cache is currently
  437. // "static", by which I mean that we try to change it as little as possible and
  438. // thus will be as big as the largest screen. This is mainly due to the fact
  439. // that doing it dynamically doesn't look like it's worth it to me currently,
  440. // especially as usually screen scales don't change continuously.
  441. int cursor_scale = 1;
  442. // Use cursor-shape-v1 protocol if the compositor supports it.
  443. wp_cursor_shape_device_v1_shape standard_cursors[DisplayServer::CURSOR_MAX] = {
  444. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT, //CURSOR_ARROW
  445. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT, //CURSOR_IBEAM
  446. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER, //CURSOR_POINTING_HAND
  447. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CROSSHAIR, //CURSOR_CROSS
  448. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_WAIT, //CURSOR_WAIT
  449. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_PROGRESS, //CURSOR_BUSY
  450. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRAB, //CURSOR_DRAG
  451. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRABBING, //CURSOR_CAN_DROP
  452. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NO_DROP, //CURSOR_FORBIDDEN
  453. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NS_RESIZE, //CURSOR_VSIZE
  454. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_EW_RESIZE, //CURSOR_HSIZE
  455. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NESW_RESIZE, //CURSOR_BDIAGSIZE
  456. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NWSE_RESIZE, //CURSOR_FDIAGSIZE
  457. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_MOVE, //CURSOR_MOVE
  458. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ROW_RESIZE, //CURSOR_VSPLIT
  459. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COL_RESIZE, //CURSOR_HSPLIT
  460. wp_cursor_shape_device_v1_shape::WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_HELP, //CURSOR_HELP
  461. };
  462. // Fallback to reading $XCURSOR and system themes if the compositor does not.
  463. struct wl_cursor_theme *wl_cursor_theme = nullptr;
  464. struct wl_cursor *wl_cursors[DisplayServer::CURSOR_MAX] = {};
  465. // User-defined cursor overrides. Take precedence over standard and wl cursors.
  466. HashMap<DisplayServer::CursorShape, CustomCursor> custom_cursors;
  467. DisplayServer::CursorShape cursor_shape = DisplayServer::CURSOR_ARROW;
  468. bool cursor_visible = true;
  469. PointerConstraint pointer_constraint = PointerConstraint::NONE;
  470. struct wl_display *wl_display = nullptr;
  471. struct wl_registry *wl_registry = nullptr;
  472. struct wl_seat *wl_seat_current = nullptr;
  473. bool frame = true;
  474. RegistryState registry;
  475. bool initialized = false;
  476. #ifdef TOOLS_ENABLED
  477. WaylandEmbedder embedder;
  478. #endif
  479. #ifdef LIBDECOR_ENABLED
  480. struct libdecor *libdecor_context = nullptr;
  481. #endif // LIBDECOR_ENABLED
  482. // Main polling method.
  483. static void _poll_events_thread(void *p_data);
  484. // Core Wayland event handlers.
  485. static void _wl_registry_on_global(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface, uint32_t version);
  486. static void _wl_registry_on_global_remove(void *data, struct wl_registry *wl_registry, uint32_t name);
  487. static void _wl_surface_on_enter(void *data, struct wl_surface *wl_surface, struct wl_output *wl_output);
  488. static void _wl_surface_on_leave(void *data, struct wl_surface *wl_surface, struct wl_output *wl_output);
  489. static void _wl_surface_on_preferred_buffer_scale(void *data, struct wl_surface *wl_surface, int32_t factor);
  490. static void _wl_surface_on_preferred_buffer_transform(void *data, struct wl_surface *wl_surface, uint32_t transform);
  491. static void _frame_wl_callback_on_done(void *data, struct wl_callback *wl_callback, uint32_t callback_data);
  492. static void _wl_output_on_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, int32_t subpixel, const char *make, const char *model, int32_t transform);
  493. static void _wl_output_on_mode(void *data, struct wl_output *wl_output, uint32_t flags, int32_t width, int32_t height, int32_t refresh);
  494. static void _wl_output_on_done(void *data, struct wl_output *wl_output);
  495. static void _wl_output_on_scale(void *data, struct wl_output *wl_output, int32_t factor);
  496. static void _wl_output_on_name(void *data, struct wl_output *wl_output, const char *name);
  497. static void _wl_output_on_description(void *data, struct wl_output *wl_output, const char *description);
  498. static void _wl_seat_on_capabilities(void *data, struct wl_seat *wl_seat, uint32_t capabilities);
  499. static void _wl_seat_on_name(void *data, struct wl_seat *wl_seat, const char *name);
  500. static void _cursor_frame_callback_on_done(void *data, struct wl_callback *wl_callback, uint32_t time_ms);
  501. static void _wl_pointer_on_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y);
  502. static void _wl_pointer_on_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface);
  503. static void _wl_pointer_on_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y);
  504. static void _wl_pointer_on_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state);
  505. static void _wl_pointer_on_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value);
  506. static void _wl_pointer_on_frame(void *data, struct wl_pointer *wl_pointer);
  507. static void _wl_pointer_on_axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t axis_source);
  508. static void _wl_pointer_on_axis_stop(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis);
  509. static void _wl_pointer_on_axis_discrete(void *data, struct wl_pointer *wl_pointer, uint32_t axis, int32_t discrete);
  510. static void _wl_pointer_on_axis_value120(void *data, struct wl_pointer *wl_pointer, uint32_t axis, int32_t value120);
  511. static void _wl_pointer_on_axis_relative_direction(void *data, struct wl_pointer *wl_pointer, uint32_t axis, uint32_t direction);
  512. static void _wl_keyboard_on_keymap(void *data, struct wl_keyboard *wl_keyboard, uint32_t format, int32_t fd, uint32_t size);
  513. static void _wl_keyboard_on_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys);
  514. static void _wl_keyboard_on_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface);
  515. static void _wl_keyboard_on_key(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
  516. static void _wl_keyboard_on_modifiers(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group);
  517. static void _wl_keyboard_on_repeat_info(void *data, struct wl_keyboard *wl_keyboard, int32_t rate, int32_t delay);
  518. static void _wl_data_device_on_data_offer(void *data, struct wl_data_device *wl_data_device, struct wl_data_offer *id);
  519. static void _wl_data_device_on_enter(void *data, struct wl_data_device *wl_data_device, uint32_t serial, struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y, struct wl_data_offer *id);
  520. static void _wl_data_device_on_leave(void *data, struct wl_data_device *wl_data_device);
  521. static void _wl_data_device_on_motion(void *data, struct wl_data_device *wl_data_device, uint32_t time, wl_fixed_t x, wl_fixed_t y);
  522. static void _wl_data_device_on_drop(void *data, struct wl_data_device *wl_data_device);
  523. static void _wl_data_device_on_selection(void *data, struct wl_data_device *wl_data_device, struct wl_data_offer *id);
  524. static void _wl_data_offer_on_offer(void *data, struct wl_data_offer *wl_data_offer, const char *mime_type);
  525. static void _wl_data_offer_on_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions);
  526. static void _wl_data_offer_on_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action);
  527. static void _wl_data_source_on_target(void *data, struct wl_data_source *wl_data_source, const char *mime_type);
  528. static void _wl_data_source_on_send(void *data, struct wl_data_source *wl_data_source, const char *mime_type, int32_t fd);
  529. static void _wl_data_source_on_cancelled(void *data, struct wl_data_source *wl_data_source);
  530. static void _wl_data_source_on_dnd_drop_performed(void *data, struct wl_data_source *wl_data_source);
  531. static void _wl_data_source_on_dnd_finished(void *data, struct wl_data_source *wl_data_source);
  532. static void _wl_data_source_on_action(void *data, struct wl_data_source *wl_data_source, uint32_t dnd_action);
  533. // xdg-shell event handlers.
  534. static void _xdg_wm_base_on_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial);
  535. static void _xdg_surface_on_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial);
  536. static void _xdg_toplevel_on_configure(void *data, struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height, struct wl_array *states);
  537. static void _xdg_toplevel_on_close(void *data, struct xdg_toplevel *xdg_toplevel);
  538. static void _xdg_toplevel_on_configure_bounds(void *data, struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height);
  539. static void _xdg_toplevel_on_wm_capabilities(void *data, struct xdg_toplevel *xdg_toplevel, struct wl_array *capabilities);
  540. static void _xdg_popup_on_configure(void *data, struct xdg_popup *xdg_popup, int32_t x, int32_t y, int32_t width, int32_t height);
  541. static void _xdg_popup_on_popup_done(void *data, struct xdg_popup *xdg_popup);
  542. static void _xdg_popup_on_repositioned(void *data, struct xdg_popup *xdg_popup, uint32_t token);
  543. // wayland-protocols event handlers.
  544. static void _wp_fractional_scale_on_preferred_scale(void *data, struct wp_fractional_scale_v1 *wp_fractional_scale_v1, uint32_t scale);
  545. static void _wp_relative_pointer_on_relative_motion(void *data, struct zwp_relative_pointer_v1 *wp_relative_pointer_v1, uint32_t uptime_hi, uint32_t uptime_lo, wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t dx_unaccel, wl_fixed_t dy_unaccel);
  546. static void _wp_pointer_gesture_pinch_on_begin(void *data, struct zwp_pointer_gesture_pinch_v1 *wp_pointer_gesture_pinch_v1, uint32_t serial, uint32_t time, struct wl_surface *surface, uint32_t fingers);
  547. static void _wp_pointer_gesture_pinch_on_update(void *data, struct zwp_pointer_gesture_pinch_v1 *wp_pointer_gesture_pinch_v1, uint32_t time, wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t scale, wl_fixed_t rotation);
  548. static void _wp_pointer_gesture_pinch_on_end(void *data, struct zwp_pointer_gesture_pinch_v1 *zp_pointer_gesture_pinch_v1, uint32_t serial, uint32_t time, int32_t cancelled);
  549. static void _wp_primary_selection_device_on_data_offer(void *data, struct zwp_primary_selection_device_v1 *wp_primary_selection_device_v1, struct zwp_primary_selection_offer_v1 *offer);
  550. static void _wp_primary_selection_device_on_selection(void *data, struct zwp_primary_selection_device_v1 *wp_primary_selection_device_v1, struct zwp_primary_selection_offer_v1 *id);
  551. static void _wp_primary_selection_offer_on_offer(void *data, struct zwp_primary_selection_offer_v1 *wp_primary_selection_offer_v1, const char *mime_type);
  552. static void _wp_primary_selection_source_on_send(void *data, struct zwp_primary_selection_source_v1 *wp_primary_selection_source_v1, const char *mime_type, int32_t fd);
  553. static void _wp_primary_selection_source_on_cancelled(void *data, struct zwp_primary_selection_source_v1 *wp_primary_selection_source_v1);
  554. static void _wp_tablet_seat_on_tablet_added(void *data, struct zwp_tablet_seat_v2 *wp_tablet_seat_v2, struct zwp_tablet_v2 *id);
  555. static void _wp_tablet_seat_on_tool_added(void *data, struct zwp_tablet_seat_v2 *wp_tablet_seat_v2, struct zwp_tablet_tool_v2 *id);
  556. static void _wp_tablet_seat_on_pad_added(void *data, struct zwp_tablet_seat_v2 *wp_tablet_seat_v2, struct zwp_tablet_pad_v2 *id);
  557. static void _wp_tablet_tool_on_type(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t tool_type);
  558. static void _wp_tablet_tool_on_hardware_serial(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t hardware_serial_hi, uint32_t hardware_serial_lo);
  559. static void _wp_tablet_tool_on_hardware_id_wacom(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t hardware_id_hi, uint32_t hardware_id_lo);
  560. static void _wp_tablet_tool_on_capability(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t capability);
  561. static void _wp_tablet_tool_on_done(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2);
  562. static void _wp_tablet_tool_on_removed(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2);
  563. static void _wp_tablet_tool_on_proximity_in(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t serial, struct zwp_tablet_v2 *tablet, struct wl_surface *surface);
  564. static void _wp_tablet_tool_on_proximity_out(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2);
  565. static void _wp_tablet_tool_on_down(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t serial);
  566. static void _wp_tablet_tool_on_up(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2);
  567. static void _wp_tablet_tool_on_motion(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, wl_fixed_t x, wl_fixed_t y);
  568. static void _wp_tablet_tool_on_pressure(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t pressure);
  569. static void _wp_tablet_tool_on_distance(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t distance);
  570. static void _wp_tablet_tool_on_tilt(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, wl_fixed_t tilt_x, wl_fixed_t tilt_y);
  571. static void _wp_tablet_tool_on_rotation(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, wl_fixed_t degrees);
  572. static void _wp_tablet_tool_on_slider(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, int32_t position);
  573. static void _wp_tablet_tool_on_wheel(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, wl_fixed_t degrees, int32_t clicks);
  574. static void _wp_tablet_tool_on_button(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t serial, uint32_t button, uint32_t state);
  575. static void _wp_tablet_tool_on_frame(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t time);
  576. static void _wp_text_input_on_enter(void *data, struct zwp_text_input_v3 *wp_text_input_v3, struct wl_surface *surface);
  577. static void _wp_text_input_on_leave(void *data, struct zwp_text_input_v3 *wp_text_input_v3, struct wl_surface *surface);
  578. static void _wp_text_input_on_preedit_string(void *data, struct zwp_text_input_v3 *wp_text_input_v3, const char *text, int32_t cursor_begin, int32_t cursor_end);
  579. static void _wp_text_input_on_commit_string(void *data, struct zwp_text_input_v3 *wp_text_input_v3, const char *text);
  580. static void _wp_text_input_on_delete_surrounding_text(void *data, struct zwp_text_input_v3 *wp_text_input_v3, uint32_t before_length, uint32_t after_length);
  581. static void _wp_text_input_on_done(void *data, struct zwp_text_input_v3 *wp_text_input_v3, uint32_t serial);
  582. static void _xdg_toplevel_decoration_on_configure(void *data, struct zxdg_toplevel_decoration_v1 *xdg_toplevel_decoration, uint32_t mode);
  583. // NOTE: Deprecated.
  584. static void _xdg_exported_v1_on_handle(void *data, zxdg_exported_v1 *exported, const char *handle);
  585. static void _xdg_exported_v2_on_handle(void *data, zxdg_exported_v2 *exported, const char *handle);
  586. static void _xdg_activation_token_on_done(void *data, struct xdg_activation_token_v1 *xdg_activation_token, const char *token);
  587. static void _godot_embedding_compositor_on_client(void *data, struct godot_embedding_compositor *godot_embedding_compositor, struct godot_embedded_client *godot_embedded_client, int32_t pid);
  588. static void _godot_embedded_client_on_disconnected(void *data, struct godot_embedded_client *godot_embedded_client);
  589. static void _godot_embedded_client_on_window_embedded(void *data, struct godot_embedded_client *godot_embedded_client);
  590. static void _godot_embedded_client_on_window_focus_in(void *data, struct godot_embedded_client *godot_embedded_client);
  591. static void _godot_embedded_client_on_window_focus_out(void *data, struct godot_embedded_client *godot_embedded_client);
  592. // Core Wayland event listeners.
  593. static constexpr struct wl_registry_listener wl_registry_listener = {
  594. .global = _wl_registry_on_global,
  595. .global_remove = _wl_registry_on_global_remove,
  596. };
  597. static constexpr struct wl_surface_listener wl_surface_listener = {
  598. .enter = _wl_surface_on_enter,
  599. .leave = _wl_surface_on_leave,
  600. .preferred_buffer_scale = _wl_surface_on_preferred_buffer_scale,
  601. .preferred_buffer_transform = _wl_surface_on_preferred_buffer_transform,
  602. };
  603. static constexpr struct wl_callback_listener frame_wl_callback_listener = {
  604. .done = _frame_wl_callback_on_done,
  605. };
  606. static constexpr struct wl_output_listener wl_output_listener = {
  607. .geometry = _wl_output_on_geometry,
  608. .mode = _wl_output_on_mode,
  609. .done = _wl_output_on_done,
  610. .scale = _wl_output_on_scale,
  611. .name = _wl_output_on_name,
  612. .description = _wl_output_on_description,
  613. };
  614. static constexpr struct wl_seat_listener wl_seat_listener = {
  615. .capabilities = _wl_seat_on_capabilities,
  616. .name = _wl_seat_on_name,
  617. };
  618. static constexpr struct wl_callback_listener cursor_frame_callback_listener = {
  619. .done = _cursor_frame_callback_on_done,
  620. };
  621. static constexpr struct wl_pointer_listener wl_pointer_listener = {
  622. .enter = _wl_pointer_on_enter,
  623. .leave = _wl_pointer_on_leave,
  624. .motion = _wl_pointer_on_motion,
  625. .button = _wl_pointer_on_button,
  626. .axis = _wl_pointer_on_axis,
  627. .frame = _wl_pointer_on_frame,
  628. .axis_source = _wl_pointer_on_axis_source,
  629. .axis_stop = _wl_pointer_on_axis_stop,
  630. .axis_discrete = _wl_pointer_on_axis_discrete,
  631. .axis_value120 = _wl_pointer_on_axis_value120,
  632. .axis_relative_direction = _wl_pointer_on_axis_relative_direction,
  633. };
  634. static constexpr struct wl_keyboard_listener wl_keyboard_listener = {
  635. .keymap = _wl_keyboard_on_keymap,
  636. .enter = _wl_keyboard_on_enter,
  637. .leave = _wl_keyboard_on_leave,
  638. .key = _wl_keyboard_on_key,
  639. .modifiers = _wl_keyboard_on_modifiers,
  640. .repeat_info = _wl_keyboard_on_repeat_info,
  641. };
  642. static constexpr struct wl_data_device_listener wl_data_device_listener = {
  643. .data_offer = _wl_data_device_on_data_offer,
  644. .enter = _wl_data_device_on_enter,
  645. .leave = _wl_data_device_on_leave,
  646. .motion = _wl_data_device_on_motion,
  647. .drop = _wl_data_device_on_drop,
  648. .selection = _wl_data_device_on_selection,
  649. };
  650. static constexpr struct wl_data_offer_listener wl_data_offer_listener = {
  651. .offer = _wl_data_offer_on_offer,
  652. .source_actions = _wl_data_offer_on_source_actions,
  653. .action = _wl_data_offer_on_action,
  654. };
  655. static constexpr struct wl_data_source_listener wl_data_source_listener = {
  656. .target = _wl_data_source_on_target,
  657. .send = _wl_data_source_on_send,
  658. .cancelled = _wl_data_source_on_cancelled,
  659. .dnd_drop_performed = _wl_data_source_on_dnd_drop_performed,
  660. .dnd_finished = _wl_data_source_on_dnd_finished,
  661. .action = _wl_data_source_on_action,
  662. };
  663. // xdg-shell event listeners.
  664. static constexpr struct xdg_wm_base_listener xdg_wm_base_listener = {
  665. .ping = _xdg_wm_base_on_ping,
  666. };
  667. static constexpr struct xdg_surface_listener xdg_surface_listener = {
  668. .configure = _xdg_surface_on_configure,
  669. };
  670. static constexpr struct xdg_toplevel_listener xdg_toplevel_listener = {
  671. .configure = _xdg_toplevel_on_configure,
  672. .close = _xdg_toplevel_on_close,
  673. .configure_bounds = _xdg_toplevel_on_configure_bounds,
  674. .wm_capabilities = _xdg_toplevel_on_wm_capabilities,
  675. };
  676. static constexpr struct xdg_popup_listener xdg_popup_listener = {
  677. .configure = _xdg_popup_on_configure,
  678. .popup_done = _xdg_popup_on_popup_done,
  679. .repositioned = _xdg_popup_on_repositioned,
  680. };
  681. // wayland-protocols event listeners.
  682. static constexpr struct wp_fractional_scale_v1_listener wp_fractional_scale_listener = {
  683. .preferred_scale = _wp_fractional_scale_on_preferred_scale,
  684. };
  685. static constexpr struct zwp_relative_pointer_v1_listener wp_relative_pointer_listener = {
  686. .relative_motion = _wp_relative_pointer_on_relative_motion,
  687. };
  688. static constexpr struct zwp_pointer_gesture_pinch_v1_listener wp_pointer_gesture_pinch_listener = {
  689. .begin = _wp_pointer_gesture_pinch_on_begin,
  690. .update = _wp_pointer_gesture_pinch_on_update,
  691. .end = _wp_pointer_gesture_pinch_on_end,
  692. };
  693. static constexpr struct zwp_primary_selection_device_v1_listener wp_primary_selection_device_listener = {
  694. .data_offer = _wp_primary_selection_device_on_data_offer,
  695. .selection = _wp_primary_selection_device_on_selection,
  696. };
  697. static constexpr struct zwp_primary_selection_offer_v1_listener wp_primary_selection_offer_listener = {
  698. .offer = _wp_primary_selection_offer_on_offer,
  699. };
  700. static constexpr struct zwp_primary_selection_source_v1_listener wp_primary_selection_source_listener = {
  701. .send = _wp_primary_selection_source_on_send,
  702. .cancelled = _wp_primary_selection_source_on_cancelled,
  703. };
  704. static constexpr struct zwp_tablet_seat_v2_listener wp_tablet_seat_listener = {
  705. .tablet_added = _wp_tablet_seat_on_tablet_added,
  706. .tool_added = _wp_tablet_seat_on_tool_added,
  707. .pad_added = _wp_tablet_seat_on_pad_added,
  708. };
  709. static constexpr struct zwp_tablet_tool_v2_listener wp_tablet_tool_listener = {
  710. .type = _wp_tablet_tool_on_type,
  711. .hardware_serial = _wp_tablet_tool_on_hardware_serial,
  712. .hardware_id_wacom = _wp_tablet_tool_on_hardware_id_wacom,
  713. .capability = _wp_tablet_tool_on_capability,
  714. .done = _wp_tablet_tool_on_done,
  715. .removed = _wp_tablet_tool_on_removed,
  716. .proximity_in = _wp_tablet_tool_on_proximity_in,
  717. .proximity_out = _wp_tablet_tool_on_proximity_out,
  718. .down = _wp_tablet_tool_on_down,
  719. .up = _wp_tablet_tool_on_up,
  720. .motion = _wp_tablet_tool_on_motion,
  721. .pressure = _wp_tablet_tool_on_pressure,
  722. .distance = _wp_tablet_tool_on_distance,
  723. .tilt = _wp_tablet_tool_on_tilt,
  724. .rotation = _wp_tablet_tool_on_rotation,
  725. .slider = _wp_tablet_tool_on_slider,
  726. .wheel = _wp_tablet_tool_on_wheel,
  727. .button = _wp_tablet_tool_on_button,
  728. .frame = _wp_tablet_tool_on_frame,
  729. };
  730. static constexpr struct zwp_text_input_v3_listener wp_text_input_listener = {
  731. .enter = _wp_text_input_on_enter,
  732. .leave = _wp_text_input_on_leave,
  733. .preedit_string = _wp_text_input_on_preedit_string,
  734. .commit_string = _wp_text_input_on_commit_string,
  735. .delete_surrounding_text = _wp_text_input_on_delete_surrounding_text,
  736. .done = _wp_text_input_on_done,
  737. };
  738. // NOTE: Deprecated.
  739. static constexpr struct zxdg_exported_v1_listener xdg_exported_v1_listener = {
  740. .handle = _xdg_exported_v1_on_handle,
  741. };
  742. static constexpr struct zxdg_exported_v2_listener xdg_exported_v2_listener = {
  743. .handle = _xdg_exported_v2_on_handle,
  744. };
  745. static constexpr struct zxdg_toplevel_decoration_v1_listener xdg_toplevel_decoration_listener = {
  746. .configure = _xdg_toplevel_decoration_on_configure,
  747. };
  748. static constexpr struct xdg_activation_token_v1_listener xdg_activation_token_listener = {
  749. .done = _xdg_activation_token_on_done,
  750. };
  751. // Godot interfaces.
  752. static constexpr struct godot_embedding_compositor_listener godot_embedding_compositor_listener = {
  753. .client = _godot_embedding_compositor_on_client,
  754. };
  755. static constexpr struct godot_embedded_client_listener godot_embedded_client_listener = {
  756. .disconnected = _godot_embedded_client_on_disconnected,
  757. .window_embedded = _godot_embedded_client_on_window_embedded,
  758. .window_focus_in = _godot_embedded_client_on_window_focus_in,
  759. .window_focus_out = _godot_embedded_client_on_window_focus_out,
  760. };
  761. #ifdef LIBDECOR_ENABLED
  762. // libdecor event handlers.
  763. static void libdecor_on_error(struct libdecor *context, enum libdecor_error error, const char *message);
  764. static void libdecor_frame_on_configure(struct libdecor_frame *frame, struct libdecor_configuration *configuration, void *user_data);
  765. static void libdecor_frame_on_close(struct libdecor_frame *frame, void *user_data);
  766. static void libdecor_frame_on_commit(struct libdecor_frame *frame, void *user_data);
  767. static void libdecor_frame_on_dismiss_popup(struct libdecor_frame *frame, const char *seat_name, void *user_data);
  768. // libdecor event listeners.
  769. static constexpr struct libdecor_interface libdecor_interface = {
  770. .error = libdecor_on_error,
  771. .reserved0 = nullptr,
  772. .reserved1 = nullptr,
  773. .reserved2 = nullptr,
  774. .reserved3 = nullptr,
  775. .reserved4 = nullptr,
  776. .reserved5 = nullptr,
  777. .reserved6 = nullptr,
  778. .reserved7 = nullptr,
  779. .reserved8 = nullptr,
  780. .reserved9 = nullptr,
  781. };
  782. static constexpr struct libdecor_frame_interface libdecor_frame_interface = {
  783. .configure = libdecor_frame_on_configure,
  784. .close = libdecor_frame_on_close,
  785. .commit = libdecor_frame_on_commit,
  786. .dismiss_popup = libdecor_frame_on_dismiss_popup,
  787. .reserved0 = nullptr,
  788. .reserved1 = nullptr,
  789. .reserved2 = nullptr,
  790. .reserved3 = nullptr,
  791. .reserved4 = nullptr,
  792. .reserved5 = nullptr,
  793. .reserved6 = nullptr,
  794. .reserved7 = nullptr,
  795. .reserved8 = nullptr,
  796. .reserved9 = nullptr,
  797. };
  798. #endif // LIBDECOR_ENABLED
  799. static Vector<uint8_t> _read_fd(int fd);
  800. static int _allocate_shm_file(size_t size);
  801. static Vector<uint8_t> _wl_data_offer_read(struct wl_display *wl_display, const char *p_mime, struct wl_data_offer *wl_data_offer);
  802. static Vector<uint8_t> _wp_primary_selection_offer_read(struct wl_display *wl_display, const char *p_mime, struct zwp_primary_selection_offer_v1 *wp_primary_selection_offer);
  803. static void _seat_state_set_current(WaylandThread::SeatState &p_ss);
  804. static Ref<InputEventKey> _seat_state_get_key_event(SeatState *p_ss, xkb_keycode_t p_keycode, bool p_pressed);
  805. static Ref<InputEventKey> _seat_state_get_unstuck_key_event(SeatState *p_ss, xkb_keycode_t p_keycode, bool p_pressed, Key p_key);
  806. static void _seat_state_handle_xkb_keycode(SeatState *p_ss, xkb_keycode_t p_xkb_keycode, bool p_pressed, bool p_echo = false);
  807. static void _wayland_state_update_cursor();
  808. void _set_current_seat(struct wl_seat *p_seat);
  809. bool _load_cursor_theme(int p_cursor_size);
  810. void _update_scale(int p_scale);
  811. public:
  812. Mutex &mutex = thread_data.mutex;
  813. struct wl_display *get_wl_display() const;
  814. // Core Wayland utilities for integrating with our own data structures.
  815. static bool wl_proxy_is_godot(struct wl_proxy *p_proxy);
  816. static void wl_proxy_tag_godot(struct wl_proxy *p_proxy);
  817. static WindowState *wl_surface_get_window_state(struct wl_surface *p_surface);
  818. static ScreenState *wl_output_get_screen_state(struct wl_output *p_output);
  819. static SeatState *wl_seat_get_seat_state(struct wl_seat *p_seat);
  820. static TabletToolState *wp_tablet_tool_get_state(struct zwp_tablet_tool_v2 *p_tool);
  821. static OfferState *wl_data_offer_get_offer_state(struct wl_data_offer *p_offer);
  822. static OfferState *wp_primary_selection_offer_get_offer_state(struct zwp_primary_selection_offer_v1 *p_offer);
  823. static EmbeddingCompositorState *godot_embedding_compositor_get_state(struct godot_embedding_compositor *p_compositor);
  824. void seat_state_unlock_pointer(SeatState *p_ss);
  825. void seat_state_lock_pointer(SeatState *p_ss);
  826. void seat_state_set_hint(SeatState *p_ss, int p_x, int p_y);
  827. void seat_state_confine_pointer(SeatState *p_ss);
  828. static void seat_state_update_cursor(SeatState *p_ss);
  829. void seat_state_echo_keys(SeatState *p_ss);
  830. static int window_state_get_preferred_buffer_scale(WindowState *p_ws);
  831. static double window_state_get_scale_factor(const WindowState *p_ws);
  832. static void window_state_update_size(WindowState *p_ws, int p_width, int p_height);
  833. static Vector2i scale_vector2i(const Vector2i &p_vector, double p_amount);
  834. void push_message(Ref<Message> message);
  835. bool has_message();
  836. Ref<Message> pop_message();
  837. void beep() const;
  838. void set_icon(const Ref<Image> &p_icon);
  839. void window_create(DisplayServer::WindowID p_window_id, const Size2i &p_size, DisplayServer::WindowID p_parent_id = DisplayServer::INVALID_WINDOW_ID);
  840. void window_create_popup(DisplayServer::WindowID p_window_id, DisplayServer::WindowID p_parent_id, Rect2i p_rect);
  841. void window_destroy(DisplayServer::WindowID p_window_Id);
  842. void window_set_parent(DisplayServer::WindowID p_window_id, DisplayServer::WindowID p_parent_id);
  843. struct wl_surface *window_get_wl_surface(DisplayServer::WindowID p_window_id) const;
  844. WindowState *window_get_state(DisplayServer::WindowID p_window_id);
  845. const WindowState *window_get_state(DisplayServer::WindowID p_window_id) const;
  846. void window_start_resize(DisplayServer::WindowResizeEdge p_edge, DisplayServer::WindowID p_window);
  847. void window_set_max_size(DisplayServer::WindowID p_window_id, const Size2i &p_size);
  848. void window_set_min_size(DisplayServer::WindowID p_window_id, const Size2i &p_size);
  849. bool window_can_set_mode(DisplayServer::WindowID p_window_id, DisplayServer::WindowMode p_window_mode) const;
  850. void window_try_set_mode(DisplayServer::WindowID p_window_id, DisplayServer::WindowMode p_window_mode);
  851. DisplayServer::WindowMode window_get_mode(DisplayServer::WindowID p_window_id) const;
  852. void window_set_borderless(DisplayServer::WindowID p_window_id, bool p_borderless);
  853. void window_set_title(DisplayServer::WindowID p_window_id, const String &p_title);
  854. void window_set_app_id(DisplayServer::WindowID p_window_id, const String &p_app_id);
  855. bool window_is_focused(DisplayServer::WindowID p_window_id);
  856. // Optional - requires xdg_activation_v1
  857. void window_request_attention(DisplayServer::WindowID p_window_id);
  858. void window_start_drag(DisplayServer::WindowID p_window_id);
  859. // Optional - require idle_inhibit_unstable_v1
  860. void window_set_idle_inhibition(DisplayServer::WindowID p_window_id, bool p_enable);
  861. bool window_get_idle_inhibition(DisplayServer::WindowID p_window_id) const;
  862. ScreenData screen_get_data(int p_screen) const;
  863. int get_screen_count() const;
  864. void pointer_set_constraint(PointerConstraint p_constraint);
  865. void pointer_set_hint(const Point2i &p_hint);
  866. PointerConstraint pointer_get_constraint() const;
  867. DisplayServer::WindowID pointer_get_pointed_window_id() const;
  868. DisplayServer::WindowID pointer_get_last_pointed_window_id() const;
  869. BitField<MouseButtonMask> pointer_get_button_mask() const;
  870. void cursor_set_visible(bool p_visible);
  871. void cursor_set_shape(DisplayServer::CursorShape p_cursor_shape);
  872. void cursor_set_custom_shape(DisplayServer::CursorShape p_cursor_shape);
  873. void cursor_shape_set_custom_image(DisplayServer::CursorShape p_cursor_shape, Ref<Image> p_image, const Point2i &p_hotspot);
  874. void cursor_shape_clear_custom_image(DisplayServer::CursorShape p_cursor_shape);
  875. void window_set_ime_active(const bool p_active, DisplayServer::WindowID p_window_id);
  876. void window_set_ime_position(const Point2i &p_pos, DisplayServer::WindowID p_window_id);
  877. int keyboard_get_layout_count() const;
  878. int keyboard_get_current_layout_index() const;
  879. void keyboard_set_current_layout_index(int p_index);
  880. String keyboard_get_layout_name(int p_index) const;
  881. Key keyboard_get_key_from_physical(Key p_key) const;
  882. Key keyboard_get_label_from_physical(Key p_key) const;
  883. void keyboard_echo_keys();
  884. bool selection_has_mime(const String &p_mime) const;
  885. Vector<uint8_t> selection_get_mime(const String &p_mime) const;
  886. void selection_set_text(const String &p_text);
  887. // Optional primary support - requires wp_primary_selection_unstable_v1
  888. bool primary_has_mime(const String &p_mime) const;
  889. Vector<uint8_t> primary_get_mime(const String &p_mime) const;
  890. void primary_set_text(const String &p_text);
  891. void commit_surfaces();
  892. void set_frame();
  893. bool get_reset_frame();
  894. bool wait_frame_suspend_ms(int p_timeout);
  895. bool is_fifo_available() const;
  896. uint64_t window_get_last_frame_time(DisplayServer::WindowID p_window_id) const;
  897. bool window_is_suspended(DisplayServer::WindowID p_window_id) const;
  898. bool is_suspended() const;
  899. struct godot_embedding_compositor *get_embedding_compositor();
  900. OS::ProcessID embedded_compositor_get_focused_pid();
  901. Error init();
  902. void destroy();
  903. };
  904. #endif // WAYLAND_ENABLED