Sunshine v2025.118.151840
Self-hosted game stream host for Moonlight.
wayland.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <bitset>
8
9#ifdef SUNSHINE_BUILD_WAYLAND
10 #include <wlr-export-dmabuf-unstable-v1.h>
11 #include <xdg-output-unstable-v1.h>
12#endif
13
14#include "graphics.h"
15
20#ifdef SUNSHINE_BUILD_WAYLAND
21
22namespace wl {
24
25 class frame_t {
26 public:
27 frame_t();
29
30 void
31 destroy();
32 };
33
34 class dmabuf_t {
35 public:
41
42 dmabuf_t(dmabuf_t &&) = delete;
43 dmabuf_t(const dmabuf_t &) = delete;
44
45 dmabuf_t &
46 operator=(const dmabuf_t &) = delete;
47 dmabuf_t &
48 operator=(dmabuf_t &&) = delete;
49
50 dmabuf_t();
51
52 void
53 listen(zwlr_export_dmabuf_manager_v1 *dmabuf_manager, wl_output *output, bool blend_cursor = false);
54
55 ~dmabuf_t();
56
57 void
58 frame(
59 zwlr_export_dmabuf_frame_v1 *frame,
60 std::uint32_t width, std::uint32_t height,
61 std::uint32_t x, std::uint32_t y,
62 std::uint32_t buffer_flags, std::uint32_t flags,
63 std::uint32_t format,
64 std::uint32_t high, std::uint32_t low,
65 std::uint32_t obj_count);
66
67 void
68 object(
69 zwlr_export_dmabuf_frame_v1 *frame,
70 std::uint32_t index,
71 std::int32_t fd,
72 std::uint32_t size,
73 std::uint32_t offset,
74 std::uint32_t stride,
75 std::uint32_t plane_index);
76
77 void
78 ready(
79 zwlr_export_dmabuf_frame_v1 *frame,
80 std::uint32_t tv_sec_hi, std::uint32_t tv_sec_lo, std::uint32_t tv_nsec);
81
82 void
83 cancel(
84 zwlr_export_dmabuf_frame_v1 *frame,
85 std::uint32_t reason);
86
87 inline frame_t *
88 get_next_frame() {
89 return current_frame == &frames[0] ? &frames[1] : &frames[0];
90 }
91
92 status_e status;
93
94 std::array<frame_t, 2> frames;
95 frame_t *current_frame;
96
97 zwlr_export_dmabuf_frame_v1_listener listener;
98 };
99
100 class monitor_t {
101 public:
102 monitor_t(monitor_t &&) = delete;
103 monitor_t(const monitor_t &) = delete;
104
105 monitor_t &
106 operator=(const monitor_t &) = delete;
107 monitor_t &
108 operator=(monitor_t &&) = delete;
109
110 monitor_t(wl_output *output);
111
112 void
113 xdg_name(zxdg_output_v1 *, const char *name);
114 void
115 xdg_description(zxdg_output_v1 *, const char *description);
116 void
117 xdg_position(zxdg_output_v1 *, std::int32_t x, std::int32_t y);
118 void
119 xdg_size(zxdg_output_v1 *, std::int32_t width, std::int32_t height);
120 void
121 xdg_done(zxdg_output_v1 *) {}
122
123 void
124 wl_geometry(wl_output *wl_output, std::int32_t x, std::int32_t y,
125 std::int32_t physical_width, std::int32_t physical_height, std::int32_t subpixel,
126 const char *make, const char *model, std::int32_t transform) {}
127 void
128 wl_mode(wl_output *wl_output, std::uint32_t flags,
129 std::int32_t width, std::int32_t height, std::int32_t refresh);
130 void
131 wl_done(wl_output *wl_output) {}
132 void
133 wl_scale(wl_output *wl_output, std::int32_t factor) {}
134
135 void
136 listen(zxdg_output_manager_v1 *output_manager);
137
138 wl_output *output;
139
140 std::string name;
141 std::string description;
142
143 platf::touch_port_t viewport;
144
145 wl_output_listener wl_listener;
146 zxdg_output_v1_listener xdg_listener;
147 };
148
150 struct bind_t {
151 std::uint32_t id;
152 std::uint32_t version;
153 };
154
155 public:
161
162 interface_t(interface_t &&) = delete;
163 interface_t(const interface_t &) = delete;
164
166 operator=(const interface_t &) = delete;
168 operator=(interface_t &&) = delete;
169
170 interface_t() noexcept;
171
172 void
173 listen(wl_registry *registry);
174
175 std::vector<std::unique_ptr<monitor_t>> monitors;
176
177 zwlr_export_dmabuf_manager_v1 *dmabuf_manager;
178 zxdg_output_manager_v1 *output_manager;
179
180 bool
181 operator[](interface_e bit) const {
182 return interface[bit];
183 }
184
185 private:
186 void
187 add_interface(wl_registry *registry, std::uint32_t id, const char *interface, std::uint32_t version);
188 void
189 del_interface(wl_registry *registry, uint32_t id);
190
191 std::bitset<MAX_INTERFACES> interface;
192
193 wl_registry_listener listener;
194 };
195
196 class display_t {
197 public:
204 int
205 init(const char *display_name = nullptr);
206
207 // Roundtrip with Wayland connection
208 void
209 roundtrip();
210
211 // Wait up to the timeout to read and dispatch new events
212 bool
213 dispatch(std::chrono::milliseconds timeout);
214
215 // Get the registry associated with the display
216 // No need to manually free the registry
217 wl_registry *
218 registry();
219
220 inline display_internal_t::pointer
221 get() {
222 return display_internal.get();
223 }
224
225 private:
226 display_internal_t display_internal;
227 };
228
229 std::vector<std::unique_ptr<monitor_t>>
230 monitors(const char *display_name = nullptr);
231
232 int
233 init();
234} // namespace wl
235#else
236
237struct wl_output;
238struct zxdg_output_manager_v1;
239
240namespace wl {
241 class monitor_t {
242 public:
243 monitor_t(monitor_t &&) = delete;
244 monitor_t(const monitor_t &) = delete;
245
246 monitor_t &
247 operator=(const monitor_t &) = delete;
248 monitor_t &
249 operator=(monitor_t &&) = delete;
250
251 monitor_t(wl_output *output);
252
253 void
254 listen(zxdg_output_manager_v1 *output_manager);
255
256 wl_output *output;
257
258 std::string name;
259 std::string description;
260
261 platf::touch_port_t viewport;
262 };
263
264 inline std::vector<std::unique_ptr<monitor_t>>
265 monitors(const char *display_name = nullptr) { return {}; }
266
267 inline int
268 init() { return -1; }
269} // namespace wl
270#endif
Definition utility.h:496
Definition wayland.h:196
int init(const char *display_name=nullptr)
Initialize display. If display_name == nullptr -> display_name = std::getenv("WAYLAND_DISPLAY")
Definition wayland.cpp:39
bool dispatch(std::chrono::milliseconds timeout)
Waits up to the specified timeout to dispatch new events on the wl_display.
Definition wayland.cpp:71
Definition wayland.h:34
status_e
Definition wayland.h:36
@ WAITING
Waiting for a frame.
Definition wayland.h:37
@ READY
Frame is ready.
Definition wayland.h:38
@ REINIT
Reinitialize the frame.
Definition wayland.h:39
Definition wayland.h:25
Definition wayland.h:149
interface_e
Definition wayland.h:156
@ WLR_EXPORT_DMABUF
Export dmabuf.
Definition wayland.h:158
@ MAX_INTERFACES
Maximum number of interfaces.
Definition wayland.h:159
@ XDG_OUTPUT
xdg-output
Definition wayland.h:157
Definition wayland.h:100
std::unique_ptr< platf::deinit_t > init(const std::filesystem::path &persistence_filepath, const config::video_t &video_config)
Initialize the implementation and perform the initial state recovery (if needed).
Definition display_device.cpp:721
Declarations for graphics related functions.
Definition wayland.cpp:27
Definition graphics.h:251
Definition common.h:246