Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
wayland.cpp
Go to the documentation of this file.
1
5// standard includes
6#include <algorithm>
7#include <atomic>
8#include <cerrno>
9#include <chrono>
10#include <climits>
11#include <cmath>
12#include <cstdint>
13#include <cstdlib>
14#include <cstring>
15#include <map>
16#include <memory>
17#include <string>
18#include <thread>
19#include <utility>
20#include <vector>
21
22// platform includes
23#include <drm_fourcc.h>
24#include <fcntl.h>
25#include <gbm.h>
26#include <poll.h>
27#include <sys/stat.h>
28#include <sys/sysmacros.h>
29#include <unistd.h>
30#include <wayland-client.h>
31#include <wayland-util.h>
32#include <xf86drm.h>
33
34// local includes
35#include "graphics.h"
36#include "misc.h"
37#include "src/config.h"
38#include "src/logging.h"
39#include "src/platform/common.h"
40#include "src/round_robin.h"
41#include "src/utility.h"
42#include "wayland.h"
43
44extern const wl_interface wl_output_interface;
45
46using namespace std::literals;
47
48// Disable warning for converting incompatible functions
49#pragma GCC diagnostic push
50#pragma GCC diagnostic ignored "-Wpedantic"
51#pragma GCC diagnostic ignored "-Wpmf-conversions"
52
53namespace wl {
54 void logical_size(int mode_width, int mode_height, int transform, wl_fixed_t scale, int &width, int &height) {
55 const bool rotated = transform & 1;
56 width = rotated ? mode_height : mode_width;
57 height = rotated ? mode_width : mode_height;
58 // Fixed point is exact to 1/256, so two decimals recover any scale a user
59 // is likely to have configured without changing an exact one.
60 const double factor = std::round(wl_fixed_to_double(scale) * 100.0) / 100.0;
61 if (factor > 0.0) {
62 width = (int) std::lround(width / factor);
63 height = (int) std::lround(height / factor);
64 }
65 }
66
67 output_rect_t desktop_envelope(const std::vector<output_rect_t> &outputs) {
68 int left = INT_MAX;
69 int top = INT_MAX;
70 int right = INT_MIN;
71 int bottom = INT_MIN;
72 for (const auto &output : outputs) {
73 if (output.width <= 0 || output.height <= 0) {
74 continue;
75 }
76 left = std::min(left, output.x);
77 top = std::min(top, output.y);
78 right = std::max(right, output.x + output.width);
79 bottom = std::max(bottom, output.y + output.height);
80 }
81 if (left == INT_MAX) {
82 return {0, 0, 0, 0};
83 }
84 return {left, top, right - left, bottom - top};
85 }
86
87 input_geometry_t input_geometry(const output_rect_t &envelope, const output_rect_t &output, int mode_width, int mode_height) {
88 const double s_x = output.width > 0 && mode_width > 0 ? (double) mode_width / output.width : 1.0;
89 const double s_y = output.height > 0 && mode_height > 0 ? (double) mode_height / output.height : 1.0;
90 return {
91 (int) std::lround((output.x - envelope.x) * s_x),
92 (int) std::lround((output.y - envelope.y) * s_y),
93 (int) std::lround(envelope.width * s_x),
94 (int) std::lround(envelope.height * s_y),
95 };
96 }
97
98 // Helper to call C++ method from wayland C callback
99 template<class T, class Method, Method m, class... Params>
100 static auto classCall(void *data, Params... params) -> decltype(((*reinterpret_cast<T *>(data)).*m)(params...)) {
101 return ((*reinterpret_cast<T *>(data)).*m)(params...);
102 }
103
104#define CLASS_CALL(c, m) classCall<c, decltype(&c::m), &c::m>
105
106 // Define buffer params listener
107 static const struct zwp_linux_buffer_params_v1_listener params_listener = {
108 .created = dmabuf_t::buffer_params_created,
109 .failed = dmabuf_t::buffer_params_failed
110 };
111
112 int display_t::init(const char *display_name) {
113 if (!display_name) {
114 display_name = std::getenv("WAYLAND_DISPLAY");
115 }
116
117 if (!display_name) {
118 BOOST_LOG(error) << "Environment variable WAYLAND_DISPLAY has not been defined"sv;
119 return -1;
120 }
121
122 display_internal.reset(wl_display_connect(display_name));
123 if (!display_internal) {
124 BOOST_LOG(error) << "Couldn't connect to Wayland display: "sv << display_name;
125 return -1;
126 }
127
128 BOOST_LOG(info) << "Found display ["sv << display_name << ']';
129
130 return 0;
131 }
132
133 void display_t::roundtrip() {
134 wl_display_roundtrip(display_internal.get());
135 }
136
143 bool display_t::dispatch(std::chrono::milliseconds timeout) {
144 // Check if any events are queued already. If not, flush
145 // outgoing events, and prepare to wait for readability.
146 if (wl_display_prepare_read(display_internal.get()) == 0) {
147 wl_display_flush(display_internal.get());
148
149 // Wait for an event to come in
150 struct pollfd pfd = {};
151 pfd.fd = wl_display_get_fd(display_internal.get());
152 pfd.events = POLLIN;
153 // A hang-up or error without POLLIN still has to reach read_events, so
154 // the connection error is recorded instead of looking like a timeout.
155 if (poll(&pfd, 1, timeout.count()) == 1 && (pfd.revents & (POLLIN | POLLHUP | POLLERR))) {
156 // Read the new event(s). -1 only ever means the connection is dead.
157 if (wl_display_read_events(display_internal.get()) < 0) {
159 return false;
160 }
161 } else {
162 // We timed out, so unlock the queue now
163 wl_display_cancel_read(display_internal.get());
164 return false;
165 }
166 }
167
168 // Dispatch any existing or new pending events. -1 means the connection is dead.
169 if (wl_display_dispatch_pending(display_internal.get()) < 0) {
171 return false;
172 }
173 return true;
174 }
175
177 auto err = wl_display_get_error(display_internal.get());
178 if (err && !error_logged) {
179 error_logged = true;
180 if (err == EPROTO) {
181 const wl_interface *iface = nullptr;
182 std::uint32_t id = 0;
183 auto code = wl_display_get_protocol_error(display_internal.get(), &iface, &id);
184 BOOST_LOG(error) << "Wayland connection lost to a protocol error: "sv
185 << (iface ? iface->name : "unknown") << '#' << id << " error "sv << code
186 << ". Capture cannot continue on this connection."sv;
187 } else {
188 BOOST_LOG(error) << "Wayland connection lost: "sv << std::strerror(err);
189 }
190 }
191 return err;
192 }
193
194 wl_registry *display_t::registry() {
195 return wl_display_get_registry(display_internal.get());
196 }
197
198 namespace {
199 struct output_mode_t {
200 zwlr_output_mode_v1 *proxy {nullptr};
201 int width {0};
202 int height {0};
203 int refresh_rate {0};
204 bool preferred {false};
205 };
206
207 struct output_head_t {
208 zwlr_output_head_v1 *proxy {nullptr};
209 std::string name;
210 std::string description;
211 bool enabled {false};
212 output_mode_t *current_mode {nullptr};
213 int x {0};
214 int y {0};
215 int transform {WL_OUTPUT_TRANSFORM_NORMAL};
216 wl_fixed_t scale {wl_fixed_from_int(1)};
217 std::vector<std::unique_ptr<output_mode_t>> modes;
218 };
219
220 struct saved_output_t {
221 bool enabled {false};
222 int width {0};
223 int height {0};
224 int refresh_rate {0};
225 int x {0};
226 int y {0};
227 int transform {WL_OUTPUT_TRANSFORM_NORMAL};
228 wl_fixed_t scale {wl_fixed_from_int(1)};
229 };
230
231 std::map<std::string, saved_output_t> saved_output_layout;
232 bool saved_output_layout_available {false};
233
234 struct output_control_t {
235 display_t display;
236 zwlr_output_manager_v1 *manager {nullptr};
237 std::vector<std::unique_ptr<output_head_t>> heads;
238 uint32_t serial {0};
239 bool ready {false};
240 bool manager_finished {false};
241 int apply_result {0};
242
243 ~output_control_t() {
244 for (auto &head : heads) {
245 for (auto &mode : head->modes) {
246 if (mode->proxy) {
247 zwlr_output_mode_v1_destroy(mode->proxy);
248 }
249 }
250 if (head->proxy) {
251 zwlr_output_head_v1_destroy(head->proxy);
252 }
253 }
254 if (manager && !manager_finished) {
255 zwlr_output_manager_v1_destroy(manager);
256 }
257 }
258
259 bool init() {
260 if (display.init()) {
261 return false;
262 }
263
264 static const wl_registry_listener registry_listener {
265 .global = [](void *data, wl_registry *registry, uint32_t id, const char *interface, uint32_t version) {
266 auto *control = static_cast<output_control_t *>(data);
267 if (std::strcmp(interface, zwlr_output_manager_v1_interface.name) == 0) {
268 const auto bind_version = std::min(version, 4u);
269 control->manager = static_cast<zwlr_output_manager_v1 *>(
270 wl_registry_bind(registry, id, &zwlr_output_manager_v1_interface, bind_version)
271 );
272 zwlr_output_manager_v1_add_listener(control->manager, &manager_listener, control);
273 }
274 },
275 .global_remove = [](void *, wl_registry *, uint32_t) {},
276 };
277
278 wl_registry_add_listener(display.registry(), &registry_listener, this);
279 display.roundtrip();
280 return manager != nullptr;
281 }
282
283 bool collect() {
284 if (!manager) {
285 return false;
286 }
287 display.roundtrip();
288 // A complete layout can legitimately be empty while the compositor is
289 // waiting for a newly hotplugged DRM connector to appear.
290 return ready && !manager_finished;
291 }
292
293 output_head_t *find_head(const std::string &name) {
294 const auto it = std::find_if(heads.begin(), heads.end(), [&](const auto &head) {
295 return head->name == name;
296 });
297 return it == heads.end() ? nullptr : it->get();
298 }
299
300 static void mode_size(void *data, zwlr_output_mode_v1 *, int32_t width, int32_t height) {
301 auto *mode = static_cast<output_mode_t *>(data);
302 mode->width = width;
303 mode->height = height;
304 }
305
306 static void mode_refresh(void *data, zwlr_output_mode_v1 *, int32_t refresh_rate) {
307 static_cast<output_mode_t *>(data)->refresh_rate = refresh_rate;
308 }
309
310 static void mode_preferred(void *data, zwlr_output_mode_v1 *) {
311 static_cast<output_mode_t *>(data)->preferred = true;
312 }
313
314 static void mode_finished(void *, zwlr_output_mode_v1 *) {}
315
316 static void head_name(void *data, zwlr_output_head_v1 *, const char *name) {
317 static_cast<output_head_t *>(data)->name = name;
318 }
319
320 static void head_description(void *data, zwlr_output_head_v1 *, const char *description) {
321 static_cast<output_head_t *>(data)->description = description;
322 }
323
324 static void head_physical_size(void *, zwlr_output_head_v1 *, int32_t, int32_t) {}
325
326 static void head_mode(void *data, zwlr_output_head_v1 *, zwlr_output_mode_v1 *proxy) {
327 auto *head = static_cast<output_head_t *>(data);
328 auto mode = std::make_unique<output_mode_t>();
329 mode->proxy = proxy;
330 zwlr_output_mode_v1_add_listener(proxy, &mode_listener, mode.get());
331 head->modes.emplace_back(std::move(mode));
332 }
333
334 static void head_enabled(void *data, zwlr_output_head_v1 *, int32_t enabled) {
335 static_cast<output_head_t *>(data)->enabled = enabled != 0;
336 }
337
338 static void head_current_mode(void *data, zwlr_output_head_v1 *, zwlr_output_mode_v1 *proxy) {
339 auto *head = static_cast<output_head_t *>(data);
340 const auto it = std::find_if(head->modes.begin(), head->modes.end(), [&](const auto &mode) {
341 return mode->proxy == proxy;
342 });
343 head->current_mode = it == head->modes.end() ? nullptr : it->get();
344 }
345
346 static void head_position(void *data, zwlr_output_head_v1 *, int32_t x, int32_t y) {
347 auto *head = static_cast<output_head_t *>(data);
348 head->x = x;
349 head->y = y;
350 }
351
352 static void head_transform(void *data, zwlr_output_head_v1 *, int32_t transform) {
353 static_cast<output_head_t *>(data)->transform = transform;
354 }
355
356 static void head_scale(void *data, zwlr_output_head_v1 *, wl_fixed_t scale) {
357 static_cast<output_head_t *>(data)->scale = scale;
358 }
359
360 static void head_finished(void *, zwlr_output_head_v1 *) {}
361 static void head_string(void *, zwlr_output_head_v1 *, const char *) {}
362 static void head_adaptive_sync(void *, zwlr_output_head_v1 *, uint32_t) {}
363
364 static void manager_head(void *data, zwlr_output_manager_v1 *, zwlr_output_head_v1 *proxy) {
365 auto *control = static_cast<output_control_t *>(data);
366 auto head = std::make_unique<output_head_t>();
367 head->proxy = proxy;
368 zwlr_output_head_v1_add_listener(proxy, &head_listener, head.get());
369 control->heads.emplace_back(std::move(head));
370 }
371
372 static void manager_done(void *data, zwlr_output_manager_v1 *, uint32_t serial) {
373 auto *control = static_cast<output_control_t *>(data);
374 control->serial = serial;
375 control->ready = true;
376 }
377
378 static void manager_protocol_finished(void *data, zwlr_output_manager_v1 *) {
379 static_cast<output_control_t *>(data)->manager_finished = true;
380 }
381
382 static void configuration_succeeded(void *data, zwlr_output_configuration_v1 *) {
383 static_cast<output_control_t *>(data)->apply_result = 1;
384 }
385
386 static void configuration_failed(void *data, zwlr_output_configuration_v1 *) {
387 static_cast<output_control_t *>(data)->apply_result = -1;
388 }
389
390 static void configuration_cancelled(void *data, zwlr_output_configuration_v1 *) {
391 static_cast<output_control_t *>(data)->apply_result = -1;
392 }
393
394 static inline const zwlr_output_mode_v1_listener mode_listener {
395 .size = mode_size,
396 .refresh = mode_refresh,
397 .preferred = mode_preferred,
398 .finished = mode_finished,
399 };
400
401 static inline const zwlr_output_head_v1_listener head_listener {
402 .name = head_name,
403 .description = head_description,
404 .physical_size = head_physical_size,
405 .mode = head_mode,
406 .enabled = head_enabled,
407 .current_mode = head_current_mode,
408 .position = head_position,
409 .transform = head_transform,
410 .scale = head_scale,
411 .finished = head_finished,
412 .make = head_string,
413 .model = head_string,
414 .serial_number = head_string,
415 .adaptive_sync = head_adaptive_sync,
416 };
417
418 static inline const zwlr_output_manager_v1_listener manager_listener {
419 .head = manager_head,
420 .done = manager_done,
421 .finished = manager_protocol_finished,
422 };
423
424 static inline const zwlr_output_configuration_v1_listener configuration_listener {
425 .succeeded = configuration_succeeded,
426 .failed = configuration_failed,
427 .cancelled = configuration_cancelled,
428 };
429 };
430
431 output_mode_t *preferred_or_current_mode(output_head_t &head) {
432 if (head.current_mode) {
433 return head.current_mode;
434 }
435 const auto it = std::find_if(head.modes.begin(), head.modes.end(), [](const auto &mode) {
436 return mode->preferred;
437 });
438 return it == head.modes.end() ? (head.modes.empty() ? nullptr : head.modes.front().get()) : it->get();
439 }
440
441 output_mode_t *matching_mode(output_head_t &head, int width, int height, int refresh_rate) {
442 const auto it = std::find_if(head.modes.begin(), head.modes.end(), [&](const auto &mode) {
443 return mode->width == width && mode->height == height &&
444 (refresh_rate <= 0 || mode->refresh_rate == 0 || std::abs(mode->refresh_rate - refresh_rate) <= 1000);
445 });
446 return it == head.modes.end() ? nullptr : it->get();
447 }
448
449 bool apply_configuration(output_control_t &control, output_head_t &virtual_head, int width, int height, int refresh_rate, bool exclusive, bool restore) {
450 auto *configuration = zwlr_output_manager_v1_create_configuration(control.manager, control.serial);
451 if (!configuration) {
452 return false;
453 }
454
455 bool valid = true;
456 int right_edge = 0;
457 for (const auto &head : control.heads) {
458 if (head->enabled && head->current_mode && head.get() != &virtual_head) {
459 // Positions are logical, so the edge has to be logical too. Adding
460 // the mode width instead leaves a gap after a scaled output, which
461 // the cursor cannot cross.
462 int logical_width = 0;
463 int logical_height = 0;
464 logical_size(head->current_mode->width, head->current_mode->height, head->transform, head->scale, logical_width, logical_height);
465 right_edge = std::max(right_edge, head->x + logical_width);
466 }
467 }
468
469 for (const auto &head : control.heads) {
470 bool enable = head->enabled;
471 output_mode_t *mode = preferred_or_current_mode(*head);
472 int x = head->x;
473 int y = head->y;
474 int transform = head->transform;
475 wl_fixed_t scale = head->scale;
476 bool custom_mode = false;
477
478 if (restore) {
479 const auto saved = saved_output_layout.find(head->name);
480 if (saved != saved_output_layout.end()) {
481 enable = saved->second.enabled;
482 mode = matching_mode(*head, saved->second.width, saved->second.height, saved->second.refresh_rate);
483 x = saved->second.x;
484 y = saved->second.y;
485 transform = saved->second.transform;
486 scale = saved->second.scale;
487 }
488 } else if (head.get() == &virtual_head) {
489 enable = true;
490 mode = matching_mode(*head, width, height, refresh_rate);
491 custom_mode = mode == nullptr;
492 x = exclusive ? 0 : right_edge;
493 y = 0;
494 transform = WL_OUTPUT_TRANSFORM_NORMAL;
495 scale = wl_fixed_from_int(1);
496 } else if (exclusive) {
497 enable = false;
498 }
499
500 if (!enable) {
501 zwlr_output_configuration_v1_disable_head(configuration, head->proxy);
502 continue;
503 }
504
505 if (!mode && !custom_mode) {
506 BOOST_LOG(error) << "Wayland output "sv << head->name << " has no usable mode"sv;
507 valid = false;
508 break;
509 }
510
511 auto *configured_head = zwlr_output_configuration_v1_enable_head(configuration, head->proxy);
512 if (custom_mode) {
513 zwlr_output_configuration_head_v1_set_custom_mode(configured_head, width, height, refresh_rate);
514 } else {
515 zwlr_output_configuration_head_v1_set_mode(configured_head, mode->proxy);
516 }
517 zwlr_output_configuration_head_v1_set_position(configured_head, x, y);
518 zwlr_output_configuration_head_v1_set_transform(configured_head, transform);
519 zwlr_output_configuration_head_v1_set_scale(configured_head, scale);
520 }
521
522 if (!valid) {
523 zwlr_output_configuration_v1_destroy(configuration);
524 return false;
525 }
526
527 control.apply_result = 0;
528 zwlr_output_configuration_v1_add_listener(configuration, &output_control_t::configuration_listener, &control);
529 zwlr_output_configuration_v1_apply(configuration);
530 for (int attempt = 0; attempt < 20 && control.apply_result == 0; ++attempt) {
531 control.display.dispatch(100ms);
532 }
533 zwlr_output_configuration_v1_destroy(configuration);
534 return control.apply_result == 1;
535 }
536 } // namespace
537
538 inline monitor_t::monitor_t(wl_output *output):
539 output {output},
540 wl_listener {
541 &CLASS_CALL(monitor_t, wl_geometry),
542 &CLASS_CALL(monitor_t, wl_mode),
543 &CLASS_CALL(monitor_t, wl_done),
544 &CLASS_CALL(monitor_t, wl_scale),
545 },
546 xdg_listener {
547 &CLASS_CALL(monitor_t, xdg_position),
548 &CLASS_CALL(monitor_t, xdg_size),
549 &CLASS_CALL(monitor_t, xdg_done),
550 &CLASS_CALL(monitor_t, xdg_name),
551 &CLASS_CALL(monitor_t, xdg_description)
552 } {
553 }
554
555 inline void monitor_t::xdg_name(zxdg_output_v1 *, const char *name) {
556 this->name = name;
557
558 BOOST_LOG(info) << "Name: "sv << this->name;
559 }
560
561 void monitor_t::xdg_description(zxdg_output_v1 *, const char *description) {
562 this->description = description;
563
564 BOOST_LOG(info) << "Found monitor: "sv << this->description;
565 }
566
567 void monitor_t::xdg_position(zxdg_output_v1 *, std::int32_t x, std::int32_t y) {
568 viewport.offset_x = x;
569 viewport.offset_y = y;
570
571 BOOST_LOG(info) << "Offset: "sv << x << 'x' << y;
572 }
573
574 void monitor_t::xdg_size(zxdg_output_v1 *, std::int32_t width, std::int32_t height) {
575 logical_width = width;
576 logical_height = height;
577
578 BOOST_LOG(info) << "Logical size: "sv << width << 'x' << height;
579 }
580
581 void monitor_t::wl_mode(
582 wl_output *wl_output,
583 std::uint32_t flags,
584 std::int32_t width,
585 std::int32_t height,
586 std::int32_t refresh
587 ) {
588 viewport.width = width;
589 viewport.height = height;
590
591 BOOST_LOG(info) << "Resolution: "sv << width << 'x' << height;
592 }
593
594 void monitor_t::listen(zxdg_output_manager_v1 *output_manager) {
595 auto xdg_output = zxdg_output_manager_v1_get_xdg_output(output_manager, output);
596 zxdg_output_v1_add_listener(xdg_output, &xdg_listener, this);
597 wl_output_add_listener(output, &wl_listener, this);
598 }
599
600 interface_t::interface_t() noexcept
601 :
602 screencopy_manager {nullptr},
603 dmabuf_interface {nullptr},
604 output_manager {nullptr},
605 image_copy_capture_manager {nullptr},
606 image_capture_source_manager {nullptr},
607 listener {
608 &CLASS_CALL(interface_t, add_interface),
609 &CLASS_CALL(interface_t, del_interface)
610 } {
611 }
612
613 void interface_t::listen(wl_registry *registry) {
614 wl_registry_add_listener(registry, &listener, this);
615 }
616
617 void interface_t::add_interface(
618 wl_registry *registry,
619 std::uint32_t id,
620 const char *interface,
621 std::uint32_t version
622 ) {
623 BOOST_LOG(debug) << "Available interface: "sv << interface << '(' << id << ") version "sv << version;
624
625 if (!std::strcmp(interface, wl_output_interface.name)) {
626 BOOST_LOG(info) << "Found interface: "sv << interface << '(' << id << ") version "sv << version;
627 monitors.emplace_back(
628 std::make_unique<monitor_t>(
629 (wl_output *) wl_registry_bind(registry, id, &wl_output_interface, 2)
630 )
631 );
632 } else if (!std::strcmp(interface, zxdg_output_manager_v1_interface.name)) {
633 BOOST_LOG(info) << "Found interface: "sv << interface << '(' << id << ") version "sv << version;
634 output_manager = (zxdg_output_manager_v1 *) wl_registry_bind(registry, id, &zxdg_output_manager_v1_interface, version);
635
636 this->interface[XDG_OUTPUT] = true;
637 } else if (!std::strcmp(interface, zwlr_screencopy_manager_v1_interface.name)) {
638 BOOST_LOG(info) << "Found interface: "sv << interface << '(' << id << ") version "sv << version;
639 screencopy_manager = (zwlr_screencopy_manager_v1 *) wl_registry_bind(registry, id, &zwlr_screencopy_manager_v1_interface, version);
640
641 this->interface[WLR_EXPORT_DMABUF] = true;
642 } else if (!std::strcmp(interface, zwp_linux_dmabuf_v1_interface.name)) {
643 BOOST_LOG(info) << "Found interface: "sv << interface << '(' << id << ") version "sv << version;
644 // Never bind a version newer than the generated code knows about.
645 dmabuf_interface = (zwp_linux_dmabuf_v1 *) wl_registry_bind(registry, id, &zwp_linux_dmabuf_v1_interface, std::min<std::uint32_t>(version, zwp_linux_dmabuf_v1_interface.version));
646
647 this->interface[LINUX_DMABUF] = true;
648 } else if (!std::strcmp(interface, ext_image_copy_capture_manager_v1_interface.name)) {
649 BOOST_LOG(info) << "Found interface: "sv << interface << '(' << id << ") version "sv << version;
650 image_copy_capture_manager = (ext_image_copy_capture_manager_v1 *) wl_registry_bind(registry, id, &ext_image_copy_capture_manager_v1_interface, 1);
651
652 this->interface[IMAGE_COPY_CAPTURE] = true;
653 } else if (!std::strcmp(interface, ext_output_image_capture_source_manager_v1_interface.name)) {
654 BOOST_LOG(info) << "Found interface: "sv << interface << '(' << id << ") version "sv << version;
655 image_capture_source_manager = (ext_output_image_capture_source_manager_v1 *) wl_registry_bind(registry, id, &ext_output_image_capture_source_manager_v1_interface, 1);
656
657 this->interface[IMAGE_CAPTURE_SOURCE] = true;
658 }
659 }
660
661 void interface_t::del_interface(wl_registry *registry, uint32_t id) {
662 BOOST_LOG(info) << "Delete: "sv << id;
663 }
664
665 // A capture path that cannot allocate a buffer fails every frame, and every
666 // failure is reported by a different object (see dmabuf_capture_exhausted()),
667 // so both the giving-up decision and the log throttling have to be kept here.
668 static std::atomic<unsigned> gbm_failure_streak {0};
669 static std::atomic<std::int64_t> last_gbm_failure_ns {0};
670
671 // Long enough to ride out a compositor that is still bringing an output up,
672 // short enough that nobody watches a black screen for a second while we count.
673 static constexpr unsigned gbm_failure_limit = 10;
674
675 // A reinit retries within a frame time or two, so a failure arriving after a
676 // pause this long is not part of the same run. Without the gap the streak
677 // would be a lifetime tally: a user who fixed their configuration and started
678 // a new stream would be refused on the first frame, on the strength of a run
679 // that ended minutes ago.
680 static constexpr std::int64_t gbm_failure_gap_ns = 5'000'000'000;
681
682 static void note_gbm_failure() {
683 const auto now = std::chrono::duration_cast<std::chrono::nanoseconds>(
684 std::chrono::steady_clock::now().time_since_epoch()
685 )
686 .count();
687 const auto previous = last_gbm_failure_ns.exchange(now, std::memory_order_relaxed);
688
689 if (previous != 0 && now - previous > gbm_failure_gap_ns) {
690 gbm_failure_streak.store(1, std::memory_order_relaxed);
691 return;
692 }
693 gbm_failure_streak.fetch_add(1, std::memory_order_relaxed);
694 }
695
696 static void clear_gbm_failures() {
697 gbm_failure_streak.store(0, std::memory_order_relaxed);
698 last_gbm_failure_ns.store(0, std::memory_order_relaxed);
699 }
700
701 // The first failure of a streak carries the diagnosis; the rest are the same
702 // sentence at frame rate, which is how the original report ended up with tens
703 // of thousands of identical lines and no explanation among them.
704 static bool should_log_gbm_failure() {
705 return gbm_failure_streak.load(std::memory_order_relaxed) <= 1;
706 }
707
709 return gbm_failure_streak.load(std::memory_order_relaxed) >= gbm_failure_limit;
710 }
711
723 static gbm_device *open_usable_gbm_device(const char *node, int &out_fd) {
724 const int fd = platf::open_drm_node(node, O_RDWR);
725 if (fd < 0) {
726 BOOST_LOG(debug) << "GBM: cannot open "sv << node << ": "sv << std::strerror(errno);
727 return nullptr;
728 }
729
730 gbm_device *device = gbm_create_device(fd);
731 if (!device) {
732 BOOST_LOG(debug) << "GBM: "sv << node << " has no GBM device"sv;
733 close(fd);
734 return nullptr;
735 }
736
737 // Screencopy buffers are ordinary render targets, so the probe asks for the
738 // smallest buffer of the same kind rather than a frame-sized one.
739 gbm_bo *probe = gbm_bo_create(device, 64, 64, GBM_FORMAT_XRGB8888, GBM_BO_USE_RENDERING);
740 if (!probe) {
741 BOOST_LOG(debug) << "GBM: "sv << node << " opened but cannot allocate buffers"sv;
742 gbm_device_destroy(device);
743 close(fd);
744 return nullptr;
745 }
746 gbm_bo_destroy(probe);
747
748 out_fd = fd;
749 return device;
750 }
751
752 namespace {
753 struct feedback_ctx_t {
754 dev_t main_device {};
755 bool have_main_device {false};
756 bool done {false};
757 };
758
759 void feedback_done(void *data, zwp_linux_dmabuf_feedback_v1 *) {
760 static_cast<feedback_ctx_t *>(data)->done = true;
761 }
762
763 void feedback_format_table(void *, zwp_linux_dmabuf_feedback_v1 *, std::int32_t fd, std::uint32_t) {
764 close(fd);
765 }
766
767 void feedback_main_device(void *data, zwp_linux_dmabuf_feedback_v1 *, wl_array *device) {
768 auto ctx = static_cast<feedback_ctx_t *>(data);
769 if (device->size == sizeof(dev_t)) {
770 std::memcpy(&ctx->main_device, device->data, sizeof(dev_t));
771 ctx->have_main_device = true;
772 }
773 }
774
775 void feedback_tranche_done(void *, zwp_linux_dmabuf_feedback_v1 *) {}
776
777 void feedback_tranche_target_device(void *, zwp_linux_dmabuf_feedback_v1 *, wl_array *) {}
778
779 void feedback_tranche_formats(void *, zwp_linux_dmabuf_feedback_v1 *, wl_array *) {}
780
781 void feedback_tranche_flags(void *, zwp_linux_dmabuf_feedback_v1 *, std::uint32_t) {}
782
783 const zwp_linux_dmabuf_feedback_v1_listener feedback_listener {
784 .done = feedback_done,
785 .format_table = feedback_format_table,
786 .main_device = feedback_main_device,
787 .tranche_done = feedback_tranche_done,
788 .tranche_target_device = feedback_tranche_target_device,
789 .tranche_formats = feedback_tranche_formats,
790 .tranche_flags = feedback_tranche_flags,
791 };
792
803 std::string render_node_for_device(dev_t device_id) {
804 drmDevice *drm_device {nullptr};
805 if (drmGetDeviceFromDevId(device_id, 0, &drm_device) != 0) {
806 BOOST_LOG(warning) << "No DRM device found for device number "sv << major(device_id) << ':' << minor(device_id);
807 return {};
808 }
809 std::string node;
810 if (drm_device->available_nodes & (1 << DRM_NODE_RENDER)) {
811 node = drm_device->nodes[DRM_NODE_RENDER];
812 } else if (drm_device->available_nodes & (1 << DRM_NODE_PRIMARY)) {
813 node = drm_device->nodes[DRM_NODE_PRIMARY];
814 }
815 drmFreeDevice(&drm_device);
816 return node;
817 }
818 } // namespace
819
820 std::string compositor_render_node(display_t &display, zwp_linux_dmabuf_v1 *dmabuf_interface) {
821 if (!display.get() || !dmabuf_interface) {
822 return {};
823 }
824 if (zwp_linux_dmabuf_v1_get_version(dmabuf_interface) < ZWP_LINUX_DMABUF_V1_GET_DEFAULT_FEEDBACK_SINCE_VERSION) {
825 BOOST_LOG(info) << "linux-dmabuf v"sv << zwp_linux_dmabuf_v1_get_version(dmabuf_interface)
826 << " has no feedback; cannot learn which GPU the compositor renders on"sv;
827 return {};
828 }
829
830 feedback_ctx_t ctx;
831 auto feedback = zwp_linux_dmabuf_v1_get_default_feedback(dmabuf_interface);
832 zwp_linux_dmabuf_feedback_v1_add_listener(feedback, &feedback_listener, &ctx);
833 auto roundtrip = wl_display_roundtrip(display.get());
834 zwp_linux_dmabuf_feedback_v1_destroy(feedback);
835
836 if (roundtrip < 0) {
837 BOOST_LOG(warning) << "Wayland connection failed while reading linux-dmabuf feedback"sv;
838 return {};
839 }
840 if (!ctx.done) {
841 BOOST_LOG(warning) << "The compositor did not finish sending linux-dmabuf feedback"sv;
842 return {};
843 }
844 if (!ctx.have_main_device) {
845 BOOST_LOG(warning) << "The compositor sent no linux-dmabuf main device"sv;
846 return {};
847 }
848
849 auto node = render_node_for_device(ctx.main_device);
850 if (!node.empty()) {
851 BOOST_LOG(info) << "The compositor renders on "sv << node << " (linux-dmabuf feedback)"sv;
852 }
853 return node;
854 }
855
856 void dmabuf_t::set_render_node(std::string node) {
857 render_node = std::move(node);
858 }
859
881 bool dmabuf_t::init_gbm() {
882 if (gbm_device) {
883 return true;
884 }
885
886 if (!config::video.adapter_name.empty()) {
887 const auto &node = config::video.adapter_name;
888 gbm_device = open_usable_gbm_device(node.c_str(), drm_fd);
889 if (!gbm_device) {
890 BOOST_LOG(error) << "Configured adapter_name "sv << node
891 << " cannot allocate capture buffers. Point it at the render node the "sv
892 << "compositor renders on, or clear it to let Hermes search."sv;
893 return false;
894 }
895 BOOST_LOG(info) << "GBM capture device: "sv << node << " (adapter_name)"sv;
896 // Compared by device, not by path: adapter_name may be a symlink to the
897 // node the compositor named, or that GPU's primary node.
898 struct stat st {};
899 if (!render_node.empty() && stat(node.c_str(), &st) == 0 && render_node_for_device(st.st_rdev) != render_node) {
900 BOOST_LOG(warning) << "adapter_name "sv << node << " is not the GPU the compositor renders on ("sv
901 << render_node << "); the compositor may refuse buffers allocated there."sv;
902 }
903 return true;
904 }
905
906 if (!render_node.empty()) {
907 gbm_device = open_usable_gbm_device(render_node.c_str(), drm_fd);
908 if (gbm_device) {
909 BOOST_LOG(info) << "GBM capture device: "sv << render_node << " (named by the compositor)"sv;
910 return true;
911 }
912 BOOST_LOG(warning) << "The compositor renders on "sv << render_node
913 << ", which cannot allocate capture buffers; searching the other render nodes."sv;
914 }
915
916 // A wlroots compositor renders the captured output on this node whenever the
917 // session bothered to name one, which makes it the right answer before any
918 // guessing - notably in the container image, whose entrypoint points sway at
919 // the encode GPU precisely because the first node is the wrong one. It stays
920 // a hint: a value that cannot allocate falls through to the scan.
921 if (const char *wlr_node = std::getenv("WLR_RENDER_DRM_DEVICE"); wlr_node && wlr_node[0]) {
922 gbm_device = open_usable_gbm_device(wlr_node, drm_fd);
923 if (gbm_device) {
924 BOOST_LOG(info) << "GBM capture device: "sv << wlr_node << " (WLR_RENDER_DRM_DEVICE)"sv;
925 return true;
926 }
927 BOOST_LOG(warning) << "WLR_RENDER_DRM_DEVICE names "sv << wlr_node
928 << ", which cannot allocate capture buffers; searching the other render nodes."sv;
929 }
930
931 drmDevice *devices[16];
932 const int n = drmGetDevices2(0, devices, 16);
933 if (n <= 0) {
934 BOOST_LOG(error) << "No DRM devices found"sv;
935 return false;
936 }
937
938 std::string rejected;
939 for (int i = 0; i < n && !gbm_device; i++) {
940 if (!(devices[i]->available_nodes & (1 << DRM_NODE_RENDER))) {
941 continue;
942 }
943 const char *node = devices[i]->nodes[DRM_NODE_RENDER];
944 if (!node) {
945 continue;
946 }
947 gbm_device = open_usable_gbm_device(node, drm_fd);
948 if (gbm_device) {
949 BOOST_LOG(info) << "GBM capture device: "sv << node;
950 } else {
951 rejected += rejected.empty() ? node : ", "s + node;
952 }
953 }
954 drmFreeDevices(devices, n);
955
956 if (!gbm_device) {
957 BOOST_LOG(error) << "No render node can allocate capture buffers"sv
958 << (rejected.empty() ? ""s : " (tried "s + rejected + ")"s)
959 << ". Set adapter_name to the render node the compositor renders on."sv;
960 return false;
961 }
962
963 return true;
964 }
965
966 // Cleanup GBM
967 void dmabuf_t::cleanup_gbm() {
968 if (current_bo) {
969 gbm_bo_destroy(current_bo);
970 current_bo = nullptr;
971 }
972
973 if (current_wl_buffer) {
974 wl_buffer_destroy(current_wl_buffer);
975 current_wl_buffer = nullptr;
976 }
977 }
978
979 dmabuf_t::dmabuf_t():
980 status {READY},
981 frames {},
982 current_frame {&frames[0]},
983 listener {
984 &CLASS_CALL(dmabuf_t, buffer),
985 &CLASS_CALL(dmabuf_t, flags),
986 &CLASS_CALL(dmabuf_t, ready),
987 &CLASS_CALL(dmabuf_t, failed),
988 &CLASS_CALL(dmabuf_t, damage),
989 &CLASS_CALL(dmabuf_t, linux_dmabuf),
990 &CLASS_CALL(dmabuf_t, buffer_done),
991 } {
992 }
993
994 // Start capture
995 void dmabuf_t::listen(
996 zwlr_screencopy_manager_v1 *screencopy_manager,
997 zwp_linux_dmabuf_v1 *dmabuf_interface,
998 wl_output *output,
999 bool blend_cursor
1000 ) {
1001 this->dmabuf_interface = dmabuf_interface;
1002 // Reset state
1003 shm_info.supported = false;
1004 dmabuf_info.supported = false;
1005
1006 // Create new frame
1007 auto frame = zwlr_screencopy_manager_v1_capture_output(
1008 screencopy_manager,
1009 blend_cursor ? 1 : 0,
1010 output
1011 );
1012
1013 // Store frame data pointer for callbacks
1014 zwlr_screencopy_frame_v1_set_user_data(frame, this);
1015
1016 // Add listener
1017 zwlr_screencopy_frame_v1_add_listener(frame, &listener, this);
1018
1019 status = WAITING;
1020 }
1021
1022 dmabuf_t::~dmabuf_t() {
1023 icc_destroy_session();
1024 cleanup_gbm();
1025
1026 for (auto &frame : frames) {
1027 frame.destroy();
1028 }
1029
1030 if (gbm_device) {
1031 gbm_device_destroy(gbm_device);
1032 gbm_device = nullptr;
1033 }
1034
1035 // gbm_create_device() borrows the fd rather than adopting it, so destroying
1036 // the device leaves it open. Every reinit built a new dmabuf_t, so a capture
1037 // path that kept failing leaked one descriptor per retry.
1038 if (drm_fd >= 0) {
1039 close(drm_fd);
1040 drm_fd = -1;
1041 }
1042 }
1043
1044 // Buffer format callback
1045 void dmabuf_t::buffer(
1046 zwlr_screencopy_frame_v1 *frame,
1047 uint32_t format,
1048 uint32_t width,
1049 uint32_t height,
1050 uint32_t stride
1051 ) {
1052 shm_info.supported = true;
1053 shm_info.format = format;
1054 shm_info.width = width;
1055 shm_info.height = height;
1056 shm_info.stride = stride;
1057
1058 BOOST_LOG(debug) << "Screencopy supports SHM format: "sv << format;
1059 }
1060
1061 // DMA-BUF format callback
1062 void dmabuf_t::linux_dmabuf(
1063 zwlr_screencopy_frame_v1 *frame,
1064 std::uint32_t format,
1065 std::uint32_t width,
1066 std::uint32_t height
1067 ) {
1068 dmabuf_info.supported = true;
1069 dmabuf_info.format = format;
1070 dmabuf_info.width = width;
1071 dmabuf_info.height = height;
1072
1073 BOOST_LOG(debug) << "Screencopy supports DMA-BUF format: "sv << format;
1074 }
1075
1076 // Flags callback
1077 void dmabuf_t::flags(zwlr_screencopy_frame_v1 *frame, std::uint32_t flags) {
1078 y_invert = flags & ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT;
1079 BOOST_LOG(debug) << "Frame flags: "sv << flags << (y_invert ? " (y_invert)" : "");
1080 }
1081
1082 // DMA-BUF creation helper
1083 void dmabuf_t::create_and_copy_dmabuf(zwlr_screencopy_frame_v1 *frame) {
1084 if (!init_gbm()) {
1085 note_gbm_failure();
1086 zwlr_screencopy_frame_v1_destroy(frame);
1087 status = REINIT;
1088 return;
1089 }
1090
1091 // Create GBM buffer
1092 current_bo = gbm_bo_create(gbm_device, dmabuf_info.width, dmabuf_info.height, dmabuf_info.format, GBM_BO_USE_RENDERING);
1093 if (!current_bo) {
1094 note_gbm_failure();
1095 if (should_log_gbm_failure()) {
1096 BOOST_LOG(error) << "Failed to allocate a "sv << dmabuf_info.width << 'x' << dmabuf_info.height
1097 << " capture buffer in format "sv << dmabuf_info.format
1098 << ". The render node this was allocated on is not the one the compositor "sv
1099 << "renders on, or does not support the format it asked for."sv;
1100 }
1101 zwlr_screencopy_frame_v1_destroy(frame);
1102 status = REINIT;
1103 return;
1104 }
1105
1106 // Get buffer info
1107 int fd = gbm_bo_get_fd(current_bo);
1108 if (fd < 0) {
1109 BOOST_LOG(error) << "Failed to get buffer FD"sv;
1110 gbm_bo_destroy(current_bo);
1111 current_bo = nullptr;
1112 zwlr_screencopy_frame_v1_destroy(frame);
1113 status = REINIT;
1114 return;
1115 }
1116
1117 uint32_t stride = gbm_bo_get_stride(current_bo);
1118 uint64_t modifier = gbm_bo_get_modifier(current_bo);
1119
1120 // Store in surface descriptor for later use
1121 auto next_frame = get_next_frame();
1122 next_frame->sd.fds[0] = fd;
1123 next_frame->sd.pitches[0] = stride;
1124 next_frame->sd.offsets[0] = 0;
1125 next_frame->sd.modifier = modifier;
1126
1127 // Create linux-dmabuf buffer
1128 auto params = zwp_linux_dmabuf_v1_create_params(dmabuf_interface);
1129 zwp_linux_buffer_params_v1_add(params, fd, 0, 0, stride, modifier >> 32, modifier & 0xffffffff);
1130
1131 // Add listener for buffer creation
1132 zwp_linux_buffer_params_v1_add_listener(params, &params_listener, frame);
1133
1134 // Create Wayland buffer (async - callback will handle copy)
1135 zwp_linux_buffer_params_v1_create(params, dmabuf_info.width, dmabuf_info.height, dmabuf_info.format, 0);
1136 }
1137
1138 // Buffer done callback - time to create buffer
1139 void dmabuf_t::buffer_done(zwlr_screencopy_frame_v1 *frame) {
1140 auto next_frame = get_next_frame();
1141
1142 // Prefer DMA-BUF if supported
1143 if (dmabuf_info.supported && dmabuf_interface) {
1144 // Store format info first
1145 next_frame->sd.fourcc = dmabuf_info.format;
1146 next_frame->sd.width = dmabuf_info.width;
1147 next_frame->sd.height = dmabuf_info.height;
1148
1149 // Create and start copy
1150 create_and_copy_dmabuf(frame);
1151 } else if (shm_info.supported) {
1152 // Retrying will not grow a code path that was never written, so this is
1153 // counted as a failure rather than left to spin: the compositor offered
1154 // only the shared-memory buffer, and Hermes has no capture for it.
1155 note_gbm_failure();
1156 if (should_log_gbm_failure()) {
1157 BOOST_LOG(error) << "The compositor offers screencopy only as a shared-memory buffer, "sv
1158 << "which this capture backend does not implement. Capture needs "sv
1159 << "zwp_linux_dmabuf_v1 on this output."sv;
1160 }
1161 zwlr_screencopy_frame_v1_destroy(frame);
1162 status = REINIT;
1163 } else {
1164 note_gbm_failure();
1165 if (should_log_gbm_failure()) {
1166 BOOST_LOG(error) << "The compositor offered no buffer type for this frame."sv;
1167 }
1168 zwlr_screencopy_frame_v1_destroy(frame);
1169 status = REINIT;
1170 }
1171 }
1172
1173 // Buffer params created callback
1174 void dmabuf_t::buffer_params_created(
1175 void *data,
1176 struct zwp_linux_buffer_params_v1 *params,
1177 struct wl_buffer *buffer
1178 ) {
1179 auto frame = static_cast<zwlr_screencopy_frame_v1 *>(data);
1180 auto self = static_cast<dmabuf_t *>(zwlr_screencopy_frame_v1_get_user_data(frame));
1181
1182 // Store for cleanup
1183 self->current_wl_buffer = buffer;
1184
1185 // Start the actual copy
1186 zwlr_screencopy_frame_v1_copy(frame, buffer);
1187 }
1188
1189 // Buffer params failed callback
1190 void dmabuf_t::buffer_params_failed(
1191 void *data,
1192 struct zwp_linux_buffer_params_v1 *params
1193 ) {
1194 auto frame = static_cast<zwlr_screencopy_frame_v1 *>(data);
1195 auto self = static_cast<dmabuf_t *>(zwlr_screencopy_frame_v1_get_user_data(frame));
1196
1197 note_gbm_failure();
1198 if (should_log_gbm_failure()) {
1199 BOOST_LOG(error) << "The compositor refused the capture buffer. It was allocated on a device "sv
1200 << "the compositor cannot import from."sv;
1201 }
1202 self->cleanup_gbm();
1203
1204 zwlr_screencopy_frame_v1_destroy(frame);
1205 self->status = REINIT;
1206 }
1207
1208 // Ready callback
1209 void dmabuf_t::ready(
1210 zwlr_screencopy_frame_v1 *frame,
1211 std::uint32_t tv_sec_hi,
1212 std::uint32_t tv_sec_lo,
1213 std::uint32_t tv_nsec
1214 ) {
1215 BOOST_LOG(debug) << "Frame ready"sv;
1216
1217 // A frame arrived, so whatever went wrong before was transient.
1218 clear_gbm_failures();
1219
1220 // Frame is ready for use, GBM buffer now contains screen content
1221 current_frame->destroy();
1222 current_frame = get_next_frame();
1223
1224 // Keep the GBM buffer alive but destroy the Wayland objects
1225 if (current_wl_buffer) {
1226 wl_buffer_destroy(current_wl_buffer);
1227 current_wl_buffer = nullptr;
1228 }
1229
1230 cleanup_gbm();
1231
1232 zwlr_screencopy_frame_v1_destroy(frame);
1233 status = READY;
1234 }
1235
1236 // Failed callback
1237 void dmabuf_t::failed(zwlr_screencopy_frame_v1 *frame) {
1238 BOOST_LOG(error) << "Frame capture failed"sv;
1239
1240 // Clean up resources
1241 cleanup_gbm();
1242 auto next_frame = get_next_frame();
1243 next_frame->destroy();
1244
1245 zwlr_screencopy_frame_v1_destroy(frame);
1246 status = REINIT;
1247 }
1248
1249 void dmabuf_t::damage(
1250 zwlr_screencopy_frame_v1 *frame,
1251 std::uint32_t x,
1252 std::uint32_t y,
1253 std::uint32_t width,
1254 std::uint32_t height
1255 ) {};
1256
1257 // ---------------------------------------------------------------------------
1258 // ext-image-copy-capture
1259 //
1260 // Adapted from Dregu's draft for upstream Sunshine
1261 // (LizardByte/Sunshine#4788), which is where the protocol sequence below was
1262 // worked out. It differs in three places, each noted at the site: the render
1263 // device the compositor names is validated before it is trusted, the modifier
1264 // lists are owned rather than malloc'd, and buffer failures feed the same
1265 // streak counter the screencopy path uses so that a session which can never
1266 // produce a frame ends instead of retrying at frame rate.
1267 // ---------------------------------------------------------------------------
1268
1269 static const struct zwp_linux_buffer_params_v1_listener icc_params_listener = {
1270 .created = CLASS_CALL(dmabuf_t, icc_buffer_params_created),
1271 .failed = CLASS_CALL(dmabuf_t, icc_buffer_params_failed),
1272 };
1273
1274 static const struct ext_image_copy_capture_session_v1_listener icc_session_listener = {
1275 .buffer_size = CLASS_CALL(dmabuf_t, icc_session_buffer_size),
1276 .shm_format = CLASS_CALL(dmabuf_t, icc_session_shm_format),
1277 .dmabuf_device = CLASS_CALL(dmabuf_t, icc_session_dmabuf_device),
1278 .dmabuf_format = CLASS_CALL(dmabuf_t, icc_session_dmabuf_format),
1279 .done = CLASS_CALL(dmabuf_t, icc_session_done),
1280 .stopped = CLASS_CALL(dmabuf_t, icc_session_stopped),
1281 };
1282
1283 static const struct ext_image_copy_capture_frame_v1_listener icc_frame_listener = {
1284 .transform = CLASS_CALL(dmabuf_t, icc_frame_transform),
1285 .damage = CLASS_CALL(dmabuf_t, icc_frame_damage),
1286 .presentation_time = CLASS_CALL(dmabuf_t, icc_frame_presentation_time),
1287 .ready = CLASS_CALL(dmabuf_t, icc_frame_ready),
1288 .failed = CLASS_CALL(dmabuf_t, icc_frame_failed),
1289 };
1290
1291 void dmabuf_t::icc_destroy_session() {
1292 if (icc_session.frame) {
1293 ext_image_copy_capture_frame_v1_destroy(icc_session.frame);
1294 icc_session.frame = nullptr;
1295 }
1296 if (icc_session.session) {
1297 ext_image_copy_capture_session_v1_destroy(icc_session.session);
1298 icc_session.session = nullptr;
1299 }
1300 icc_session = {};
1301 }
1302
1304 ext_image_copy_capture_manager_v1 *manager,
1305 ext_output_image_capture_source_manager_v1 *source_manager,
1306 zwp_linux_dmabuf_v1 *dmabuf_interface,
1307 wl_output *output,
1308 bool blend_cursor
1309 ) {
1310 if (!manager || !source_manager || !dmabuf_interface) {
1311 return false;
1312 }
1313
1314 this->dmabuf_interface = dmabuf_interface;
1315 icc_destroy_session();
1316 icc_session.cursor = blend_cursor;
1317
1318 auto source = ext_output_image_capture_source_manager_v1_create_source(source_manager, output);
1319 if (!source) {
1320 BOOST_LOG(error) << "Could not name the Wayland output as an image capture source"sv;
1321 return false;
1322 }
1323
1324 auto session = ext_image_copy_capture_manager_v1_create_session(
1325 manager,
1326 source,
1327 blend_cursor ? EXT_IMAGE_COPY_CAPTURE_MANAGER_V1_OPTIONS_PAINT_CURSORS : 0
1328 );
1329
1330 // The session holds its own reference to the source, so the source object
1331 // is of no further use here.
1332 ext_image_capture_source_v1_destroy(source);
1333
1334 if (!session) {
1335 BOOST_LOG(error) << "Could not open an ext-image-copy-capture session"sv;
1336 return false;
1337 }
1338
1339 ext_image_copy_capture_session_v1_set_user_data(session, this);
1340 ext_image_copy_capture_session_v1_add_listener(session, &icc_session_listener, this);
1341 icc_session.session = session;
1342 status = WAITING;
1343
1344 return true;
1345 }
1346
1347 void dmabuf_t::icc_capture(bool blend_cursor) {
1348 if (!icc_session.session || blend_cursor != icc_session.cursor) {
1349 // A cursor-blending change is a different session, not a different frame.
1350 status = REINIT;
1351 return;
1352 }
1353
1354 if (!icc_session.done || icc_session.frame) {
1355 return;
1356 }
1357
1358 auto frame = ext_image_copy_capture_session_v1_create_frame(icc_session.session);
1359 if (!frame) {
1360 note_gbm_failure();
1361 status = REINIT;
1362 return;
1363 }
1364
1365 icc_session.frame = frame;
1366 ext_image_copy_capture_frame_v1_set_user_data(frame, this);
1367 ext_image_copy_capture_frame_v1_add_listener(frame, &icc_frame_listener, this);
1368 icc_create_and_copy_dmabuf(frame);
1369
1370 // Only if a frame is genuinely in flight. give_up() clears icc_session.frame
1371 // and sets REINIT, and overwriting that with WAITING here hid every
1372 // allocation failure: snapshot() saw a timeout instead of a reinit, so the
1373 // capture never ended and the streak counter never got the chance to stop
1374 // it - which is the permanent black stream this whole mechanism exists to
1375 // prevent.
1376 if (icc_session.frame) {
1377 status = WAITING;
1378 }
1379 }
1380
1381 void dmabuf_t::icc_create_and_copy_dmabuf(ext_image_copy_capture_frame_v1 *frame) {
1382 auto give_up = [&]() {
1383 note_gbm_failure();
1384 ext_image_copy_capture_frame_v1_destroy(frame);
1385 icc_session.frame = nullptr;
1386 status = REINIT;
1387 };
1388
1389 if (!init_gbm()) {
1390 give_up();
1391 return;
1392 }
1393
1394 if (!icc_session.width || !icc_session.height || !icc_session.format) {
1395 if (should_log_gbm_failure()) {
1396 BOOST_LOG(error) << "The capture session negotiated no usable buffer format"sv;
1397 }
1398 give_up();
1399 return;
1400 }
1401
1402 // Modifiers first: a compositor that offers them is telling us the layouts
1403 // it can write into, and an implicit-modifier buffer is only correct when it
1404 // offers none.
1405 if (auto it = icc_session.modifiers.find(icc_session.format);
1406 it != icc_session.modifiers.end() && !it->second.empty()) {
1407 current_bo = gbm_bo_create_with_modifiers(
1408 gbm_device,
1409 icc_session.width,
1410 icc_session.height,
1411 icc_session.format,
1412 it->second.data(),
1413 it->second.size()
1414 );
1415 }
1416
1417 if (!current_bo) {
1418 current_bo = gbm_bo_create(gbm_device, icc_session.width, icc_session.height, icc_session.format, GBM_BO_USE_RENDERING);
1419 }
1420
1421 if (!current_bo) {
1422 if (should_log_gbm_failure()) {
1423 BOOST_LOG(error) << "Failed to allocate a "sv << icc_session.width << 'x' << icc_session.height
1424 << " capture buffer in format "sv << icc_session.format
1425 << ". The render node this was allocated on is not the one the compositor "sv
1426 << "renders on, or does not support the format it asked for."sv;
1427 }
1428 give_up();
1429 return;
1430 }
1431
1432 // Allocating with modifiers can hand back a buffer of several planes - an
1433 // AMD compression modifier does exactly that - and a compositor offered
1434 // only the first plane of one cannot use it. That failure arrives as a bare
1435 // "unknown reason" on the frame, with nothing to say a plane was missing.
1436 const std::uint64_t modifier = gbm_bo_get_modifier(current_bo);
1437 const int planes = gbm_bo_get_plane_count(current_bo);
1438
1439 if (planes < 1 || planes > 4) {
1440 BOOST_LOG(error) << "Capture buffer has "sv << planes << " planes, which cannot be shared"sv;
1441 gbm_bo_destroy(current_bo);
1442 current_bo = nullptr;
1443 give_up();
1444 return;
1445 }
1446
1447 auto next_frame = get_next_frame();
1448 auto params = zwp_linux_dmabuf_v1_create_params(dmabuf_interface);
1449
1450 for (int plane = 0; plane < planes; ++plane) {
1451 int fd = gbm_bo_get_fd_for_plane(current_bo, plane);
1452 if (fd < 0) {
1453 BOOST_LOG(error) << "Failed to get buffer FD for plane "sv << plane;
1454 zwp_linux_buffer_params_v1_destroy(params);
1455 next_frame->destroy();
1456 gbm_bo_destroy(current_bo);
1457 current_bo = nullptr;
1458 give_up();
1459 return;
1460 }
1461
1462 const std::uint32_t stride = gbm_bo_get_stride_for_plane(current_bo, plane);
1463 const std::uint32_t offset = gbm_bo_get_offset(current_bo, plane);
1464
1465 next_frame->sd.fds[plane] = fd;
1466 next_frame->sd.pitches[plane] = stride;
1467 next_frame->sd.offsets[plane] = offset;
1468
1469 zwp_linux_buffer_params_v1_add(params, fd, plane, offset, stride, modifier >> 32, modifier & 0xffffffff);
1470 }
1471
1472 next_frame->sd.modifier = modifier;
1473 next_frame->sd.fourcc = icc_session.format;
1474 next_frame->sd.width = icc_session.width;
1475 next_frame->sd.height = icc_session.height;
1476
1477 BOOST_LOG(debug) << "Capture buffer: "sv << icc_session.width << 'x' << icc_session.height
1478 << ", "sv << planes << " plane(s), modifier "sv << std::hex << modifier << std::dec;
1479
1480 zwp_linux_buffer_params_v1_set_user_data(params, this);
1481 zwp_linux_buffer_params_v1_add_listener(params, &icc_params_listener, this);
1482 zwp_linux_buffer_params_v1_create(params, icc_session.width, icc_session.height, icc_session.format, 0);
1483 }
1484
1485 void dmabuf_t::icc_buffer_params_created(zwp_linux_buffer_params_v1 *params, struct wl_buffer *buffer) {
1486 current_wl_buffer = buffer;
1487 zwp_linux_buffer_params_v1_destroy(params);
1488
1489 if (!icc_session.frame) {
1490 return;
1491 }
1492
1493 ext_image_copy_capture_frame_v1_attach_buffer(icc_session.frame, buffer);
1494 ext_image_copy_capture_frame_v1_damage_buffer(icc_session.frame, 0, 0, icc_session.width, icc_session.height);
1495 ext_image_copy_capture_frame_v1_capture(icc_session.frame);
1496 }
1497
1498 void dmabuf_t::icc_buffer_params_failed(zwp_linux_buffer_params_v1 *params) {
1499 note_gbm_failure();
1500 if (should_log_gbm_failure()) {
1501 BOOST_LOG(error) << "The compositor refused the capture buffer offered to it"sv;
1502 }
1503
1504 zwp_linux_buffer_params_v1_destroy(params);
1505 cleanup_gbm();
1506
1507 if (icc_session.frame) {
1508 ext_image_copy_capture_frame_v1_destroy(icc_session.frame);
1509 icc_session.frame = nullptr;
1510 }
1511 status = REINIT;
1512 }
1513
1522 void dmabuf_t::icc_begin_constraints() {
1523 if (!icc_session.done) {
1524 return;
1525 }
1526 icc_session.done = false;
1527 icc_session.format = 0;
1528 icc_session.dmabuf_supported = false;
1529 icc_session.modifiers.clear();
1530 shm_info.supported = false;
1531 }
1532
1533 void dmabuf_t::icc_session_buffer_size(ext_image_copy_capture_session_v1 *session, std::uint32_t width, std::uint32_t height) {
1534 icc_begin_constraints();
1535 icc_session.width = width;
1536 icc_session.height = height;
1537 BOOST_LOG(debug) << "Capture session buffer size: "sv << width << 'x' << height;
1538 }
1539
1540 void dmabuf_t::icc_session_shm_format(ext_image_copy_capture_session_v1 *session, std::uint32_t format) {
1541 icc_begin_constraints();
1542 // Recorded for the same reason the screencopy path records it: to be able to
1543 // say the compositor offered only shared memory, which nothing here can use.
1544 shm_info.supported = true;
1545 shm_info.format = format;
1546 BOOST_LOG(debug) << "Capture session SHM format: "sv << format;
1547 }
1548
1549 void dmabuf_t::icc_session_dmabuf_device(ext_image_copy_capture_session_v1 *session, struct wl_array *device) {
1550 icc_begin_constraints();
1551 // The compositor names the device it renders on, per session, which is the
1552 // answer init_gbm() otherwise has to guess at. It is trusted the same way
1553 // as the linux-dmabuf feedback answer: init_gbm() proves the node can
1554 // allocate and falls through to the search when it cannot.
1555 if (device->size != sizeof(dev_t)) {
1556 BOOST_LOG(warning) << "Capture session named a device of "sv << device->size
1557 << " bytes, expected "sv << sizeof(dev_t) << "; searching instead."sv;
1558 return;
1559 }
1560
1561 dev_t device_id;
1562 std::memcpy(&device_id, device->data, sizeof(dev_t));
1563 set_render_node(render_node_for_device(device_id));
1564 }
1565
1566 void dmabuf_t::icc_session_dmabuf_format(ext_image_copy_capture_session_v1 *session, std::uint32_t format, struct wl_array *modifiers) {
1567 icc_begin_constraints();
1568 icc_session.dmabuf_supported = true;
1569
1570 auto &list = icc_session.modifiers[format];
1571 list.assign(
1572 (const std::uint64_t *) modifiers->data,
1573 (const std::uint64_t *) modifiers->data + modifiers->size / sizeof(std::uint64_t)
1574 );
1575
1576 // The compositor sends its preferred format first, so the first one that
1577 // arrives is the one to allocate in.
1578 if (!icc_session.format) {
1579 icc_session.format = format;
1580 }
1581
1582 char fourcc[5] {};
1583 std::memcpy(fourcc, &format, 4);
1584 BOOST_LOG(debug) << "Capture session DMA-BUF format: "sv << fourcc << " with "sv << list.size() << " modifier(s)"sv;
1585 }
1586
1587 void dmabuf_t::icc_session_done(ext_image_copy_capture_session_v1 *session) {
1588 if (!icc_session.session) {
1589 note_gbm_failure();
1590 status = REINIT;
1591 return;
1592 }
1593
1594 // Each of these is a session that will negotiate the same way every time,
1595 // so they have to feed the streak: a reinit alone would rebuild the display
1596 // forever. The screencopy path counts its equivalent failures for the same
1597 // reason.
1598 if (!icc_session.width || !icc_session.height) {
1599 note_gbm_failure();
1600 if (should_log_gbm_failure()) {
1601 BOOST_LOG(error) << "Capture session negotiated a zero-sized buffer"sv;
1602 }
1603 status = REINIT;
1604 return;
1605 }
1606
1607 if (!icc_session.dmabuf_supported) {
1608 note_gbm_failure();
1609 if (should_log_gbm_failure()) {
1610 BOOST_LOG(error) << "The compositor offers capture only as a shared-memory buffer, "sv
1611 << "which this capture backend does not implement."sv;
1612 }
1613 status = REINIT;
1614 return;
1615 }
1616
1617 icc_session.done = true;
1618 BOOST_LOG(debug) << "Capture session ready"sv;
1619 }
1620
1621 void dmabuf_t::icc_session_stopped(ext_image_copy_capture_session_v1 *session) {
1622 BOOST_LOG(warning) << "The compositor stopped the capture session"sv;
1623 icc_destroy_session();
1624 status = REINIT;
1625 }
1626
1627 void dmabuf_t::icc_frame_transform(ext_image_copy_capture_frame_v1 *frame, std::uint32_t transform) {
1628 BOOST_LOG(debug) << "Capture frame transform: "sv << transform;
1629 }
1630
1631 void dmabuf_t::icc_frame_damage(
1632 ext_image_copy_capture_frame_v1 *frame,
1633 std::int32_t x,
1634 std::int32_t y,
1635 std::int32_t width,
1636 std::int32_t height
1637 ) {}
1638
1639 void dmabuf_t::icc_frame_presentation_time(
1640 ext_image_copy_capture_frame_v1 *frame,
1641 std::uint32_t tv_sec_hi,
1642 std::uint32_t tv_sec_lo,
1643 std::uint32_t tv_nsec
1644 ) {}
1645
1646 void dmabuf_t::icc_frame_ready(ext_image_copy_capture_frame_v1 *frame) {
1647 clear_gbm_failures();
1648
1649 current_frame->destroy();
1650 current_frame = get_next_frame();
1651
1652 if (current_wl_buffer) {
1653 wl_buffer_destroy(current_wl_buffer);
1654 current_wl_buffer = nullptr;
1655 }
1656 cleanup_gbm();
1657
1658 ext_image_copy_capture_frame_v1_destroy(frame);
1659 icc_session.frame = nullptr;
1660 status = READY;
1661 }
1662
1663 void dmabuf_t::icc_frame_failed(ext_image_copy_capture_frame_v1 *frame, std::uint32_t reason) {
1664 note_gbm_failure();
1665
1666 ext_image_copy_capture_frame_v1_destroy(frame);
1667 icc_session.frame = nullptr;
1668
1669 if (reason == EXT_IMAGE_COPY_CAPTURE_FRAME_V1_FAILURE_REASON_STOPPED) {
1670 if (should_log_gbm_failure()) {
1671 BOOST_LOG(error) << "The capture session stopped while a frame was in flight"sv;
1672 }
1673 cleanup_gbm();
1674 get_next_frame()->destroy();
1675 icc_destroy_session();
1676 status = REINIT;
1677 return;
1678 }
1679
1680 if (reason == EXT_IMAGE_COPY_CAPTURE_FRAME_V1_FAILURE_REASON_BUFFER_CONSTRAINTS) {
1681 // The negotiated constraints no longer describe the output - a mode change,
1682 // most often. The session renegotiates them, so drop what we cached.
1683 if (should_log_gbm_failure()) {
1684 BOOST_LOG(warning) << "Capture buffer no longer meets the session's constraints; renegotiating"sv;
1685 }
1686 cleanup_gbm();
1687 get_next_frame()->destroy();
1688 status = REINIT;
1689 return;
1690 }
1691
1692 if (should_log_gbm_failure()) {
1693 BOOST_LOG(error) << "The compositor failed the capture frame for an unknown reason ("sv << reason << ')';
1694 }
1695 // Every path out of here has to release the buffer the frame was carrying.
1696 // The plane descriptors are the expensive half: a multi-plane modifier
1697 // buffer holds one fd per plane, and this is the reason a compositor is
1698 // most likely to hand back, so leaking here leaks at frame rate.
1699 cleanup_gbm();
1700 get_next_frame()->destroy();
1701 status = REINIT;
1702 }
1703
1704 void frame_t::destroy() {
1705 for (auto x = 0; x < 4; ++x) {
1706 if (sd.fds[x] >= 0) {
1707 close(sd.fds[x]);
1708
1709 sd.fds[x] = -1;
1710 }
1711 }
1712 }
1713
1714 frame_t::frame_t() {
1715 // File descriptors aren't open
1716 std::fill_n(sd.fds, 4, -1);
1717 };
1718
1719 std::vector<std::unique_ptr<monitor_t>> monitors(const char *display_name) {
1721
1722 if (display.init(display_name)) {
1723 return {};
1724 }
1725
1726 interface_t interface;
1727 interface.listen(display.registry());
1728
1729 display.roundtrip();
1730
1731 if (!interface[interface_t::XDG_OUTPUT]) {
1732 BOOST_LOG(error) << "Missing Wayland wire XDG_OUTPUT"sv;
1733 return {};
1734 }
1735
1736 for (auto &monitor : interface.monitors) {
1737 monitor->listen(interface.output_manager);
1738 }
1739
1740 display.roundtrip();
1741
1742 return std::move(interface.monitors);
1743 }
1744
1745 bool render_node_can_allocate(const char *node) {
1746 if (!node || !node[0]) {
1747 return false;
1748 }
1749 int fd = -1;
1750 gbm_device *device = open_usable_gbm_device(node, fd);
1751 if (!device) {
1752 return false;
1753 }
1754 gbm_device_destroy(device);
1755 // gbm_create_device() borrows the fd rather than adopting it.
1756 ::close(fd);
1757 return true;
1758 }
1759
1760 bool configure_virtual_output(const std::string &output_name, int width, int height, int refresh_rate, bool exclusive) {
1761 for (int attempt = 0; attempt < 5; ++attempt) {
1762 output_control_t control;
1763 if (!control.init()) {
1764 BOOST_LOG(warning) << "Wayland compositor does not expose wlr-output-management; cannot activate virtual output "sv << output_name;
1765 return false;
1766 }
1767
1768 if (!control.collect()) {
1769 // Not a verdict on the layout, just a layout we have not been told about
1770 // yet: collect() only succeeds once the manager has sent its first done
1771 // event. A compositor sitting at zero outputs — a headless session, or a
1772 // container that has just come up — can still be assembling that state in
1773 // the roundtrip we make here. Returning now would abandon the activation
1774 // in the same millisecond the connector was created, before the compositor
1775 // could possibly have processed the hotplug, so retry on the same footing
1776 // as a head that has not appeared yet.
1777 std::this_thread::sleep_for(100ms);
1778 continue;
1779 }
1780
1781 auto *virtual_head = control.find_head(output_name);
1782 if (!virtual_head) {
1783 // Virtual DRM connectors are published asynchronously. Give the compositor
1784 // a short window to process the hotplug before declaring it unavailable.
1785 std::this_thread::sleep_for(100ms);
1786 continue;
1787 }
1788
1789 std::map<std::string, saved_output_t> previous_layout;
1790 if (exclusive) {
1791 for (const auto &head : control.heads) {
1792 if (head.get() == virtual_head) {
1793 continue;
1794 }
1795 const auto *mode = preferred_or_current_mode(*head);
1796 previous_layout[head->name] = {
1797 .enabled = head->enabled,
1798 .width = mode ? mode->width : 0,
1799 .height = mode ? mode->height : 0,
1800 .refresh_rate = mode ? mode->refresh_rate : 0,
1801 .x = head->x,
1802 .y = head->y,
1803 .transform = head->transform,
1804 .scale = head->scale,
1805 };
1806 }
1807 }
1808
1809 const bool applied = apply_configuration(control, *virtual_head, width, height, refresh_rate, exclusive, false);
1810 if (applied && exclusive) {
1811 saved_output_layout = std::move(previous_layout);
1812 saved_output_layout_available = true;
1813 }
1814 if (!applied) {
1815 BOOST_LOG(warning) << "Wayland compositor rejected the virtual output layout for "sv << output_name;
1816 }
1817 return applied;
1818 }
1819
1820 BOOST_LOG(warning) << "Virtual connector "sv << output_name << " did not appear in the Wayland output layout."sv;
1821 return false;
1822 }
1823
1825 if (!saved_output_layout_available) {
1826 return true;
1827 }
1828
1829 output_control_t control;
1830 if (!control.init() || !control.collect() || control.heads.empty()) {
1831 BOOST_LOG(warning) << "Cannot restore the Wayland physical output layout."sv;
1832 return false;
1833 }
1834
1835 const bool restored = apply_configuration(control, *control.heads.front(), 0, 0, 0, false, true);
1836 if (restored) {
1837 saved_output_layout.clear();
1838 saved_output_layout_available = false;
1839 BOOST_LOG(info) << "Restored Wayland physical output layout."sv;
1840 } else {
1841 BOOST_LOG(warning) << "Wayland compositor rejected restoration of the physical output layout."sv;
1842 }
1843 return restored;
1844 }
1845
1847 output_control_t control;
1848 return control.init() && control.collect();
1849 }
1850
1852 session_protocols_t protocols {};
1853
1854 display_t display;
1855 if (display.init() != 0) {
1856 // Not a failure worth logging as an error: an X11 session reaching the
1857 // diagnostics path is the ordinary case, and `connected` says so.
1858 return protocols;
1859 }
1860 protocols.connected = true;
1861
1862 // A probe must not require bindings for protocols Hermes does not
1863 // implement, so the two ext-* globals are matched by name. They are staging
1864 // protocols whose names are part of their wire identity and cannot change
1865 // without a new global, which is exactly what makes the string safe here.
1866 static constexpr auto image_copy_capture_name = "ext_image_copy_capture_manager_v1"sv;
1867 static constexpr auto output_capture_source_name = "ext_output_image_capture_source_manager_v1"sv;
1868
1869 struct probe_state_t {
1870 session_protocols_t *protocols;
1871 wl_registry_listener listener;
1872 };
1873
1874 probe_state_t state {
1875 .protocols = &protocols,
1876 .listener = {
1877 .global = [](void *data, wl_registry *, std::uint32_t, const char *interface, std::uint32_t) {
1878 auto *found = static_cast<probe_state_t *>(data)->protocols;
1879 const std::string_view name {interface};
1880 if (name == zwlr_output_manager_v1_interface.name) {
1881 found->output_management = true;
1882 } else if (name == zwlr_screencopy_manager_v1_interface.name) {
1883 found->screencopy = true;
1884 } else if (name == zwp_linux_dmabuf_v1_interface.name) {
1885 found->linux_dmabuf = true;
1886 } else if (name == zxdg_output_manager_v1_interface.name) {
1887 found->xdg_output = true;
1888 } else if (name == image_copy_capture_name) {
1889 found->image_copy_capture = true;
1890 } else if (name == output_capture_source_name) {
1891 found->output_capture_source = true;
1892 }
1893 },
1894 .global_remove = [](void *, wl_registry *, std::uint32_t) {},
1895 },
1896 };
1897
1898 wl_registry_add_listener(display.registry(), &state.listener, &state);
1899 display.roundtrip();
1900
1901 return protocols;
1902 }
1903
1904 static bool validate() {
1905 display_t display;
1906
1907 return display.init() == 0;
1908 }
1909
1910 int init() {
1911 static bool validated = validate();
1912
1913 return !validated;
1914 }
1915
1916} // namespace wl
1917
1918#pragma GCC diagnostic pop
int init(const char *display_name=nullptr)
Initialize display. If display_name == nullptr -> display_name = std::getenv("WAYLAND_DISPLAY").
Definition wayland.cpp:112
int connection_error()
The errno-style code of the connection's fatal error, 0 for none. A protocol error (EPROTO) is fatal ...
Definition wayland.cpp:176
bool dispatch(std::chrono::milliseconds timeout)
Waits up to the specified timeout to dispatch new events on the wl_display.
Definition wayland.cpp:143
void icc_capture(bool blend_cursor=false)
Request one frame from an established ICC session.
Definition wayland.cpp:1347
bool icc_listen(ext_image_copy_capture_manager_v1 *manager, ext_output_image_capture_source_manager_v1 *source_manager, zwp_linux_dmabuf_v1 *dmabuf_interface, wl_output *output, bool blend_cursor=false)
Open an ext-image-copy-capture session on an output.
Definition wayland.cpp:1303
@ WAITING
Waiting for a frame.
Definition wayland.h:52
@ READY
Frame is ready.
Definition wayland.h:53
@ REINIT
Reinitialize the frame.
Definition wayland.h:54
void set_render_node(std::string node)
Name the render node the compositor renders on, so init_gbm() allocates there. See compositor_render_...
Definition wayland.cpp:856
@ XDG_OUTPUT
xdg-output
Definition wayland.h:222
Declarations for common platform specific utilities.
std::shared_ptr< display_t > display(mem_type_e hwdevice_type, const std::string &display_name, const video::config_t &config)
Get the display_t instance for the given hwdevice_type. If display_name is empty, use the first monit...
Definition misc.cpp:1658
Declarations for the configuration of Sunshine.
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).
@ drm_device
–drm-device, the shorter downstream patch reported in Hermes#57.
Declarations for graphics related functions.
Declarations for logging related functions.
int open_drm_node(const char *path, int flags)
Open a DRM device node without becoming its DRM master.
Definition misc.cpp:138
Miscellaneous declarations for Linux.
int version()
Print the version to stdout, then exit.
Definition wayland.cpp:53
bool dmabuf_capture_exhausted()
Whether the dmabuf capture path has failed too often to be retried.
Definition wayland.cpp:708
bool restore_virtual_output_layout()
Definition wayland.cpp:1824
output_rect_t desktop_envelope(const std::vector< output_rect_t > &outputs)
The desktop envelope: the bounding box of every output in logical coordinates, origin included....
Definition wayland.cpp:67
void logical_size(int mode_width, int mode_height, int transform, wl_fixed_t scale, int &width, int &height)
Size of a head in the compositor's logical coordinates, the wlr counterpart of mutter::state_t::logic...
Definition wayland.cpp:54
bool configure_virtual_output(const std::string &output_name, int width, int height, int refresh_rate, bool exclusive)
Definition wayland.cpp:1760
input_geometry_t input_geometry(const output_rect_t &envelope, const output_rect_t &output, int mode_width, int mode_height)
Express an output's offset within the envelope, and the envelope, in that output's own capture pixels...
Definition wayland.cpp:87
std::string compositor_render_node(display_t &display, zwp_linux_dmabuf_v1 *dmabuf_interface)
Ask the compositor which GPU it renders on.
Definition wayland.cpp:820
bool render_node_can_allocate(const char *node)
Whether GBM can actually allocate a buffer on this render node.
Definition wayland.cpp:1745
bool output_management_supported()
Definition wayland.cpp:1846
session_protocols_t probe_protocols()
Probe the session's globals in a single registry roundtrip.
Definition wayland.cpp:1851
Declarations for a round-robin iterator.
Which Wayland globals the running session advertises.
Definition wayland.h:395
bool connected
A Wayland display was reachable at all.
Definition wayland.h:396
Declarations for utility functions.
Declarations for Wayland capture.