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;
60 const double factor = std::round(wl_fixed_to_double(scale) * 100.0) / 100.0;
62 width = (int) std::lround(width / factor);
63 height = (int) std::lround(height / factor);
72 for (
const auto &output : outputs) {
73 if (output.width <= 0 || output.height <= 0) {
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);
81 if (left == INT_MAX) {
84 return {left, top, right - left, bottom - top};
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;
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),
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...);
104#define CLASS_CALL(c, m) classCall<c, decltype(&c::m), &c::m>
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
114 display_name = std::getenv(
"WAYLAND_DISPLAY");
118 BOOST_LOG(error) <<
"Environment variable WAYLAND_DISPLAY has not been defined"sv;
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;
128 BOOST_LOG(info) <<
"Found display ["sv << display_name <<
']';
133 void display_t::roundtrip() {
134 wl_display_roundtrip(display_internal.get());
146 if (wl_display_prepare_read(display_internal.get()) == 0) {
147 wl_display_flush(display_internal.get());
150 struct pollfd pfd = {};
151 pfd.fd = wl_display_get_fd(display_internal.get());
155 if (poll(&pfd, 1, timeout.count()) == 1 && (pfd.revents & (POLLIN | POLLHUP | POLLERR))) {
157 if (wl_display_read_events(display_internal.get()) < 0) {
163 wl_display_cancel_read(display_internal.get());
169 if (wl_display_dispatch_pending(display_internal.get()) < 0) {
177 auto err = wl_display_get_error(display_internal.get());
178 if (err && !error_logged) {
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;
188 BOOST_LOG(error) <<
"Wayland connection lost: "sv << std::strerror(err);
194 wl_registry *display_t::registry() {
195 return wl_display_get_registry(display_internal.get());
199 struct output_mode_t {
200 zwlr_output_mode_v1 *proxy {
nullptr};
203 int refresh_rate {0};
204 bool preferred {
false};
207 struct output_head_t {
208 zwlr_output_head_v1 *proxy {
nullptr};
210 std::string description;
211 bool enabled {
false};
212 output_mode_t *current_mode {
nullptr};
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;
220 struct saved_output_t {
221 bool enabled {
false};
224 int refresh_rate {0};
227 int transform {WL_OUTPUT_TRANSFORM_NORMAL};
228 wl_fixed_t scale {wl_fixed_from_int(1)};
231 std::map<std::string, saved_output_t> saved_output_layout;
232 bool saved_output_layout_available {
false};
234 struct output_control_t {
236 zwlr_output_manager_v1 *manager {
nullptr};
237 std::vector<std::unique_ptr<output_head_t>> heads;
240 bool manager_finished {
false};
241 int apply_result {0};
243 ~output_control_t() {
244 for (
auto &head : heads) {
245 for (
auto &mode : head->modes) {
247 zwlr_output_mode_v1_destroy(mode->proxy);
251 zwlr_output_head_v1_destroy(head->proxy);
254 if (manager && !manager_finished) {
255 zwlr_output_manager_v1_destroy(manager);
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)
272 zwlr_output_manager_v1_add_listener(control->manager, &manager_listener, control);
275 .global_remove = [](
void *, wl_registry *, uint32_t) {},
278 wl_registry_add_listener(
display.registry(), ®istry_listener,
this);
280 return manager !=
nullptr;
290 return ready && !manager_finished;
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;
297 return it == heads.end() ? nullptr : it->get();
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);
303 mode->height = height;
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;
310 static void mode_preferred(
void *data, zwlr_output_mode_v1 *) {
311 static_cast<output_mode_t *
>(data)->preferred =
true;
314 static void mode_finished(
void *, zwlr_output_mode_v1 *) {}
316 static void head_name(
void *data, zwlr_output_head_v1 *,
const char *name) {
317 static_cast<output_head_t *
>(data)->name = name;
320 static void head_description(
void *data, zwlr_output_head_v1 *,
const char *description) {
321 static_cast<output_head_t *
>(data)->description = description;
324 static void head_physical_size(
void *, zwlr_output_head_v1 *, int32_t, int32_t) {}
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>();
330 zwlr_output_mode_v1_add_listener(proxy, &mode_listener, mode.get());
331 head->modes.emplace_back(std::move(mode));
334 static void head_enabled(
void *data, zwlr_output_head_v1 *, int32_t enabled) {
335 static_cast<output_head_t *
>(data)->enabled = enabled != 0;
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;
343 head->current_mode = it == head->modes.end() ? nullptr : it->get();
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);
352 static void head_transform(
void *data, zwlr_output_head_v1 *, int32_t transform) {
353 static_cast<output_head_t *
>(data)->transform = transform;
356 static void head_scale(
void *data, zwlr_output_head_v1 *, wl_fixed_t scale) {
357 static_cast<output_head_t *
>(data)->scale = scale;
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) {}
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>();
368 zwlr_output_head_v1_add_listener(proxy, &head_listener, head.get());
369 control->heads.emplace_back(std::move(head));
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;
378 static void manager_protocol_finished(
void *data, zwlr_output_manager_v1 *) {
379 static_cast<output_control_t *
>(data)->manager_finished =
true;
382 static void configuration_succeeded(
void *data, zwlr_output_configuration_v1 *) {
383 static_cast<output_control_t *
>(data)->apply_result = 1;
386 static void configuration_failed(
void *data, zwlr_output_configuration_v1 *) {
387 static_cast<output_control_t *
>(data)->apply_result = -1;
390 static void configuration_cancelled(
void *data, zwlr_output_configuration_v1 *) {
391 static_cast<output_control_t *
>(data)->apply_result = -1;
394 static inline const zwlr_output_mode_v1_listener mode_listener {
396 .refresh = mode_refresh,
397 .preferred = mode_preferred,
398 .finished = mode_finished,
401 static inline const zwlr_output_head_v1_listener head_listener {
403 .description = head_description,
404 .physical_size = head_physical_size,
406 .enabled = head_enabled,
407 .current_mode = head_current_mode,
408 .position = head_position,
409 .transform = head_transform,
411 .finished = head_finished,
413 .model = head_string,
414 .serial_number = head_string,
415 .adaptive_sync = head_adaptive_sync,
418 static inline const zwlr_output_manager_v1_listener manager_listener {
419 .head = manager_head,
420 .done = manager_done,
421 .finished = manager_protocol_finished,
424 static inline const zwlr_output_configuration_v1_listener configuration_listener {
425 .succeeded = configuration_succeeded,
426 .failed = configuration_failed,
427 .cancelled = configuration_cancelled,
431 output_mode_t *preferred_or_current_mode(output_head_t &head) {
432 if (head.current_mode) {
433 return head.current_mode;
435 const auto it = std::find_if(head.modes.begin(), head.modes.end(), [](
const auto &mode) {
436 return mode->preferred;
438 return it == head.modes.end() ? (head.modes.empty() ?
nullptr : head.modes.front().get()) : it->get();
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);
446 return it == head.modes.end() ? nullptr : it->get();
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) {
457 for (
const auto &head : control.heads) {
458 if (head->enabled && head->current_mode && head.get() != &virtual_head) {
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);
469 for (
const auto &head : control.heads) {
470 bool enable = head->enabled;
471 output_mode_t *mode = preferred_or_current_mode(*head);
474 int transform = head->transform;
475 wl_fixed_t scale = head->scale;
476 bool custom_mode =
false;
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);
485 transform = saved->second.transform;
486 scale = saved->second.scale;
488 }
else if (head.get() == &virtual_head) {
490 mode = matching_mode(*head, width, height, refresh_rate);
491 custom_mode = mode ==
nullptr;
492 x = exclusive ? 0 : right_edge;
494 transform = WL_OUTPUT_TRANSFORM_NORMAL;
495 scale = wl_fixed_from_int(1);
496 }
else if (exclusive) {
501 zwlr_output_configuration_v1_disable_head(configuration, head->proxy);
505 if (!mode && !custom_mode) {
506 BOOST_LOG(error) <<
"Wayland output "sv << head->name <<
" has no usable mode"sv;
511 auto *configured_head = zwlr_output_configuration_v1_enable_head(configuration, head->proxy);
513 zwlr_output_configuration_head_v1_set_custom_mode(configured_head, width, height, refresh_rate);
515 zwlr_output_configuration_head_v1_set_mode(configured_head, mode->proxy);
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);
523 zwlr_output_configuration_v1_destroy(configuration);
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);
533 zwlr_output_configuration_v1_destroy(configuration);
534 return control.apply_result == 1;
538 inline monitor_t::monitor_t(wl_output *output):
555 inline void monitor_t::xdg_name(zxdg_output_v1 *,
const char *name) {
558 BOOST_LOG(info) <<
"Name: "sv << this->name;
561 void monitor_t::xdg_description(zxdg_output_v1 *,
const char *description) {
562 this->description = description;
564 BOOST_LOG(info) <<
"Found monitor: "sv << this->description;
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;
571 BOOST_LOG(info) <<
"Offset: "sv << x <<
'x' << y;
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;
578 BOOST_LOG(info) <<
"Logical size: "sv << width <<
'x' << height;
581 void monitor_t::wl_mode(
582 wl_output *wl_output,
588 viewport.width = width;
589 viewport.height = height;
591 BOOST_LOG(info) <<
"Resolution: "sv << width <<
'x' << height;
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);
600 interface_t::interface_t() noexcept
602 screencopy_manager {
nullptr},
603 dmabuf_interface {
nullptr},
604 output_manager {
nullptr},
605 image_copy_capture_manager {
nullptr},
606 image_capture_source_manager {
nullptr},
613 void interface_t::listen(wl_registry *registry) {
614 wl_registry_add_listener(registry, &listener,
this);
617 void interface_t::add_interface(
618 wl_registry *registry,
620 const char *interface,
621 std::uint32_t version
623 BOOST_LOG(debug) <<
"Available interface: "sv <<
interface << '(' << id <<
") version "sv <<
version;
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)
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);
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);
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;
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));
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);
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);
657 this->interface[IMAGE_CAPTURE_SOURCE] = true;
661 void interface_t::del_interface(wl_registry *registry, uint32_t
id) {
662 BOOST_LOG(info) <<
"Delete: "sv << id;
668 static std::atomic<unsigned> gbm_failure_streak {0};
669 static std::atomic<std::int64_t> last_gbm_failure_ns {0};
673 static constexpr unsigned gbm_failure_limit = 10;
680 static constexpr std::int64_t gbm_failure_gap_ns = 5'000'000'000;
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()
687 const auto previous = last_gbm_failure_ns.exchange(now, std::memory_order_relaxed);
689 if (previous != 0 && now - previous > gbm_failure_gap_ns) {
690 gbm_failure_streak.store(1, std::memory_order_relaxed);
693 gbm_failure_streak.fetch_add(1, std::memory_order_relaxed);
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);
704 static bool should_log_gbm_failure() {
705 return gbm_failure_streak.load(std::memory_order_relaxed) <= 1;
709 return gbm_failure_streak.load(std::memory_order_relaxed) >= gbm_failure_limit;
723 static gbm_device *open_usable_gbm_device(
const char *node,
int &out_fd) {
726 BOOST_LOG(debug) <<
"GBM: cannot open "sv << node <<
": "sv << std::strerror(errno);
730 gbm_device *device = gbm_create_device(fd);
732 BOOST_LOG(debug) <<
"GBM: "sv << node <<
" has no GBM device"sv;
739 gbm_bo *probe = gbm_bo_create(device, 64, 64, GBM_FORMAT_XRGB8888, GBM_BO_USE_RENDERING);
741 BOOST_LOG(debug) <<
"GBM: "sv << node <<
" opened but cannot allocate buffers"sv;
742 gbm_device_destroy(device);
746 gbm_bo_destroy(probe);
753 struct feedback_ctx_t {
754 dev_t main_device {};
755 bool have_main_device {
false};
759 void feedback_done(
void *data, zwp_linux_dmabuf_feedback_v1 *) {
760 static_cast<feedback_ctx_t *
>(data)->done =
true;
763 void feedback_format_table(
void *, zwp_linux_dmabuf_feedback_v1 *, std::int32_t fd, std::uint32_t) {
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;
775 void feedback_tranche_done(
void *, zwp_linux_dmabuf_feedback_v1 *) {}
777 void feedback_tranche_target_device(
void *, zwp_linux_dmabuf_feedback_v1 *, wl_array *) {}
779 void feedback_tranche_formats(
void *, zwp_linux_dmabuf_feedback_v1 *, wl_array *) {}
781 void feedback_tranche_flags(
void *, zwp_linux_dmabuf_feedback_v1 *, std::uint32_t) {}
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,
803 std::string render_node_for_device(dev_t device_id) {
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);
810 if (
drm_device->available_nodes & (1 << DRM_NODE_RENDER)) {
812 }
else if (
drm_device->available_nodes & (1 << DRM_NODE_PRIMARY)) {
815 drmFreeDevice(&drm_device);
821 if (!display.get() || !dmabuf_interface) {
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;
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);
837 BOOST_LOG(warning) <<
"Wayland connection failed while reading linux-dmabuf feedback"sv;
841 BOOST_LOG(warning) <<
"The compositor did not finish sending linux-dmabuf feedback"sv;
844 if (!ctx.have_main_device) {
845 BOOST_LOG(warning) <<
"The compositor sent no linux-dmabuf main device"sv;
849 auto node = render_node_for_device(ctx.main_device);
851 BOOST_LOG(info) <<
"The compositor renders on "sv << node <<
" (linux-dmabuf feedback)"sv;
857 render_node = std::move(node);
881 bool dmabuf_t::init_gbm() {
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);
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;
895 BOOST_LOG(info) <<
"GBM capture device: "sv << node <<
" (adapter_name)"sv;
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;
906 if (!render_node.empty()) {
907 gbm_device = open_usable_gbm_device(render_node.c_str(), drm_fd);
909 BOOST_LOG(info) <<
"GBM capture device: "sv << render_node <<
" (named by the compositor)"sv;
912 BOOST_LOG(warning) <<
"The compositor renders on "sv << render_node
913 <<
", which cannot allocate capture buffers; searching the other render nodes."sv;
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);
924 BOOST_LOG(info) <<
"GBM capture device: "sv << wlr_node <<
" (WLR_RENDER_DRM_DEVICE)"sv;
927 BOOST_LOG(warning) <<
"WLR_RENDER_DRM_DEVICE names "sv << wlr_node
928 <<
", which cannot allocate capture buffers; searching the other render nodes."sv;
931 drmDevice *devices[16];
932 const int n = drmGetDevices2(0, devices, 16);
934 BOOST_LOG(error) <<
"No DRM devices found"sv;
938 std::string rejected;
939 for (
int i = 0; i < n && !gbm_device; i++) {
940 if (!(devices[i]->available_nodes & (1 << DRM_NODE_RENDER))) {
943 const char *node = devices[i]->nodes[DRM_NODE_RENDER];
947 gbm_device = open_usable_gbm_device(node, drm_fd);
949 BOOST_LOG(info) <<
"GBM capture device: "sv << node;
951 rejected += rejected.empty() ? node :
", "s + node;
954 drmFreeDevices(devices, n);
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;
967 void dmabuf_t::cleanup_gbm() {
969 gbm_bo_destroy(current_bo);
970 current_bo =
nullptr;
973 if (current_wl_buffer) {
974 wl_buffer_destroy(current_wl_buffer);
975 current_wl_buffer =
nullptr;
979 dmabuf_t::dmabuf_t():
982 current_frame {&frames[0]},
989 &CLASS_CALL(
dmabuf_t, linux_dmabuf),
995 void dmabuf_t::listen(
996 zwlr_screencopy_manager_v1 *screencopy_manager,
997 zwp_linux_dmabuf_v1 *dmabuf_interface,
1001 this->dmabuf_interface = dmabuf_interface;
1003 shm_info.supported =
false;
1004 dmabuf_info.supported =
false;
1007 auto frame = zwlr_screencopy_manager_v1_capture_output(
1009 blend_cursor ? 1 : 0,
1014 zwlr_screencopy_frame_v1_set_user_data(frame,
this);
1017 zwlr_screencopy_frame_v1_add_listener(frame, &listener,
this);
1022 dmabuf_t::~dmabuf_t() {
1023 icc_destroy_session();
1026 for (
auto &frame : frames) {
1031 gbm_device_destroy(gbm_device);
1032 gbm_device =
nullptr;
1045 void dmabuf_t::buffer(
1046 zwlr_screencopy_frame_v1 *frame,
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;
1058 BOOST_LOG(debug) <<
"Screencopy supports SHM format: "sv << format;
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
1068 dmabuf_info.supported =
true;
1069 dmabuf_info.format = format;
1070 dmabuf_info.width = width;
1071 dmabuf_info.height = height;
1073 BOOST_LOG(debug) <<
"Screencopy supports DMA-BUF format: "sv << format;
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)" :
"");
1083 void dmabuf_t::create_and_copy_dmabuf(zwlr_screencopy_frame_v1 *frame) {
1086 zwlr_screencopy_frame_v1_destroy(frame);
1092 current_bo = gbm_bo_create(gbm_device, dmabuf_info.width, dmabuf_info.height, dmabuf_info.format, GBM_BO_USE_RENDERING);
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;
1101 zwlr_screencopy_frame_v1_destroy(frame);
1107 int fd = gbm_bo_get_fd(current_bo);
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);
1117 uint32_t stride = gbm_bo_get_stride(current_bo);
1118 uint64_t modifier = gbm_bo_get_modifier(current_bo);
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;
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);
1132 zwp_linux_buffer_params_v1_add_listener(params, ¶ms_listener, frame);
1135 zwp_linux_buffer_params_v1_create(params, dmabuf_info.width, dmabuf_info.height, dmabuf_info.format, 0);
1139 void dmabuf_t::buffer_done(zwlr_screencopy_frame_v1 *frame) {
1140 auto next_frame = get_next_frame();
1143 if (dmabuf_info.supported && dmabuf_interface) {
1145 next_frame->sd.fourcc = dmabuf_info.format;
1146 next_frame->sd.width = dmabuf_info.width;
1147 next_frame->sd.height = dmabuf_info.height;
1150 create_and_copy_dmabuf(frame);
1151 }
else if (shm_info.supported) {
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;
1161 zwlr_screencopy_frame_v1_destroy(frame);
1165 if (should_log_gbm_failure()) {
1166 BOOST_LOG(error) <<
"The compositor offered no buffer type for this frame."sv;
1168 zwlr_screencopy_frame_v1_destroy(frame);
1174 void dmabuf_t::buffer_params_created(
1176 struct zwp_linux_buffer_params_v1 *params,
1177 struct wl_buffer *buffer
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));
1183 self->current_wl_buffer = buffer;
1186 zwlr_screencopy_frame_v1_copy(frame, buffer);
1190 void dmabuf_t::buffer_params_failed(
1192 struct zwp_linux_buffer_params_v1 *params
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));
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;
1202 self->cleanup_gbm();
1204 zwlr_screencopy_frame_v1_destroy(frame);
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
1215 BOOST_LOG(debug) <<
"Frame ready"sv;
1218 clear_gbm_failures();
1221 current_frame->destroy();
1222 current_frame = get_next_frame();
1225 if (current_wl_buffer) {
1226 wl_buffer_destroy(current_wl_buffer);
1227 current_wl_buffer =
nullptr;
1232 zwlr_screencopy_frame_v1_destroy(frame);
1237 void dmabuf_t::failed(zwlr_screencopy_frame_v1 *frame) {
1238 BOOST_LOG(error) <<
"Frame capture failed"sv;
1242 auto next_frame = get_next_frame();
1243 next_frame->destroy();
1245 zwlr_screencopy_frame_v1_destroy(frame);
1249 void dmabuf_t::damage(
1250 zwlr_screencopy_frame_v1 *frame,
1253 std::uint32_t width,
1254 std::uint32_t height
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),
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),
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),
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;
1296 if (icc_session.session) {
1297 ext_image_copy_capture_session_v1_destroy(icc_session.session);
1298 icc_session.session =
nullptr;
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,
1310 if (!manager || !source_manager || !dmabuf_interface) {
1314 this->dmabuf_interface = dmabuf_interface;
1315 icc_destroy_session();
1316 icc_session.cursor = blend_cursor;
1318 auto source = ext_output_image_capture_source_manager_v1_create_source(source_manager, output);
1320 BOOST_LOG(error) <<
"Could not name the Wayland output as an image capture source"sv;
1324 auto session = ext_image_copy_capture_manager_v1_create_session(
1327 blend_cursor ? EXT_IMAGE_COPY_CAPTURE_MANAGER_V1_OPTIONS_PAINT_CURSORS : 0
1332 ext_image_capture_source_v1_destroy(source);
1335 BOOST_LOG(error) <<
"Could not open an ext-image-copy-capture session"sv;
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;
1348 if (!icc_session.session || blend_cursor != icc_session.cursor) {
1354 if (!icc_session.done || icc_session.frame) {
1358 auto frame = ext_image_copy_capture_session_v1_create_frame(icc_session.session);
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);
1376 if (icc_session.frame) {
1381 void dmabuf_t::icc_create_and_copy_dmabuf(ext_image_copy_capture_frame_v1 *frame) {
1382 auto give_up = [&]() {
1384 ext_image_copy_capture_frame_v1_destroy(frame);
1385 icc_session.frame =
nullptr;
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;
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(
1418 current_bo = gbm_bo_create(gbm_device, icc_session.width, icc_session.height, icc_session.format, GBM_BO_USE_RENDERING);
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;
1436 const std::uint64_t modifier = gbm_bo_get_modifier(current_bo);
1437 const int planes = gbm_bo_get_plane_count(current_bo);
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;
1447 auto next_frame = get_next_frame();
1448 auto params = zwp_linux_dmabuf_v1_create_params(dmabuf_interface);
1450 for (
int plane = 0; plane < planes; ++plane) {
1451 int fd = gbm_bo_get_fd_for_plane(current_bo, plane);
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;
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);
1465 next_frame->sd.fds[plane] = fd;
1466 next_frame->sd.pitches[plane] = stride;
1467 next_frame->sd.offsets[plane] = offset;
1469 zwp_linux_buffer_params_v1_add(params, fd, plane, offset, stride, modifier >> 32, modifier & 0xffffffff);
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;
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;
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);
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);
1489 if (!icc_session.frame) {
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);
1498 void dmabuf_t::icc_buffer_params_failed(zwp_linux_buffer_params_v1 *params) {
1500 if (should_log_gbm_failure()) {
1501 BOOST_LOG(error) <<
"The compositor refused the capture buffer offered to it"sv;
1504 zwp_linux_buffer_params_v1_destroy(params);
1507 if (icc_session.frame) {
1508 ext_image_copy_capture_frame_v1_destroy(icc_session.frame);
1509 icc_session.frame =
nullptr;
1522 void dmabuf_t::icc_begin_constraints() {
1523 if (!icc_session.done) {
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;
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;
1540 void dmabuf_t::icc_session_shm_format(ext_image_copy_capture_session_v1 *session, std::uint32_t format) {
1541 icc_begin_constraints();
1544 shm_info.supported =
true;
1545 shm_info.format = format;
1546 BOOST_LOG(debug) <<
"Capture session SHM format: "sv << format;
1549 void dmabuf_t::icc_session_dmabuf_device(ext_image_copy_capture_session_v1 *session,
struct wl_array *device) {
1550 icc_begin_constraints();
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;
1562 std::memcpy(&device_id, device->data,
sizeof(dev_t));
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;
1570 auto &list = icc_session.modifiers[format];
1572 (
const std::uint64_t *) modifiers->data,
1573 (
const std::uint64_t *) modifiers->data + modifiers->size /
sizeof(std::uint64_t)
1578 if (!icc_session.format) {
1579 icc_session.format = format;
1583 std::memcpy(fourcc, &format, 4);
1584 BOOST_LOG(debug) <<
"Capture session DMA-BUF format: "sv << fourcc <<
" with "sv << list.size() <<
" modifier(s)"sv;
1587 void dmabuf_t::icc_session_done(ext_image_copy_capture_session_v1 *session) {
1588 if (!icc_session.session) {
1598 if (!icc_session.width || !icc_session.height) {
1600 if (should_log_gbm_failure()) {
1601 BOOST_LOG(error) <<
"Capture session negotiated a zero-sized buffer"sv;
1607 if (!icc_session.dmabuf_supported) {
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;
1617 icc_session.done =
true;
1618 BOOST_LOG(debug) <<
"Capture session ready"sv;
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();
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;
1631 void dmabuf_t::icc_frame_damage(
1632 ext_image_copy_capture_frame_v1 *frame,
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
1646 void dmabuf_t::icc_frame_ready(ext_image_copy_capture_frame_v1 *frame) {
1647 clear_gbm_failures();
1649 current_frame->destroy();
1650 current_frame = get_next_frame();
1652 if (current_wl_buffer) {
1653 wl_buffer_destroy(current_wl_buffer);
1654 current_wl_buffer =
nullptr;
1658 ext_image_copy_capture_frame_v1_destroy(frame);
1659 icc_session.frame =
nullptr;
1663 void dmabuf_t::icc_frame_failed(ext_image_copy_capture_frame_v1 *frame, std::uint32_t reason) {
1666 ext_image_copy_capture_frame_v1_destroy(frame);
1667 icc_session.frame =
nullptr;
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;
1674 get_next_frame()->destroy();
1675 icc_destroy_session();
1680 if (reason == EXT_IMAGE_COPY_CAPTURE_FRAME_V1_FAILURE_REASON_BUFFER_CONSTRAINTS) {
1683 if (should_log_gbm_failure()) {
1684 BOOST_LOG(warning) <<
"Capture buffer no longer meets the session's constraints; renegotiating"sv;
1687 get_next_frame()->destroy();
1692 if (should_log_gbm_failure()) {
1693 BOOST_LOG(error) <<
"The compositor failed the capture frame for an unknown reason ("sv << reason <<
')';
1700 get_next_frame()->destroy();
1704 void frame_t::destroy() {
1705 for (
auto x = 0; x < 4; ++x) {
1706 if (sd.fds[x] >= 0) {
1714 frame_t::frame_t() {
1716 std::fill_n(sd.fds, 4, -1);
1719 std::vector<std::unique_ptr<monitor_t>> monitors(
const char *display_name) {
1722 if (
display.init(display_name)) {
1727 interface.listen(
display.registry());
1732 BOOST_LOG(error) <<
"Missing Wayland wire XDG_OUTPUT"sv;
1736 for (
auto &monitor : interface.monitors) {
1737 monitor->listen(interface.output_manager);
1742 return std::move(interface.monitors);
1746 if (!node || !node[0]) {
1750 gbm_device *device = open_usable_gbm_device(node, fd);
1754 gbm_device_destroy(device);
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;
1768 if (!control.collect()) {
1777 std::this_thread::sleep_for(100ms);
1781 auto *virtual_head = control.find_head(output_name);
1782 if (!virtual_head) {
1785 std::this_thread::sleep_for(100ms);
1789 std::map<std::string, saved_output_t> previous_layout;
1791 for (
const auto &head : control.heads) {
1792 if (head.get() == virtual_head) {
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,
1803 .transform = head->transform,
1804 .scale = head->scale,
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;
1815 BOOST_LOG(warning) <<
"Wayland compositor rejected the virtual output layout for "sv << output_name;
1820 BOOST_LOG(warning) <<
"Virtual connector "sv << output_name <<
" did not appear in the Wayland output layout."sv;
1825 if (!saved_output_layout_available) {
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;
1835 const bool restored = apply_configuration(control, *control.heads.front(), 0, 0, 0,
false,
true);
1837 saved_output_layout.clear();
1838 saved_output_layout_available =
false;
1839 BOOST_LOG(info) <<
"Restored Wayland physical output layout."sv;
1841 BOOST_LOG(warning) <<
"Wayland compositor rejected restoration of the physical output layout."sv;
1847 output_control_t control;
1848 return control.init() && control.collect();
1855 if (display.init() != 0) {
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;
1869 struct probe_state_t {
1871 wl_registry_listener listener;
1874 probe_state_t state {
1875 .protocols = &protocols,
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;
1894 .global_remove = [](
void *, wl_registry *, std::uint32_t) {},
1898 wl_registry_add_listener(display.registry(), &state.listener, &state);
1899 display.roundtrip();
1904 static bool validate() {
1907 return display.init() == 0;
1911 static bool validated = validate();