wayland_thread.h 44 KB

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