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