wayland_thread.h 44 KB

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