Skip to content

Commit 06b2953

Browse files
committed
refactor(linux/kwin): Restructure to make implementing future protocol versions easier
1 parent ba8976e commit 06b2953

1 file changed

Lines changed: 42 additions & 41 deletions

File tree

src/platform/linux/kwingrab.cpp

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,13 @@ namespace kwin {
252252
screencast_t &operator=(screencast_t &&) = delete; // Do not allow to copying
253253

254254
~screencast_t() {
255-
if (zkde_screencast_stream) {
256-
zkde_screencast_stream_unstable_v1_close(zkde_screencast_stream);
257-
zkde_screencast_stream = nullptr;
255+
if (kde_screencast_stream_v1_) {
256+
zkde_screencast_stream_unstable_v1_close(kde_screencast_stream_v1_);
257+
kde_screencast_stream_v1_ = nullptr;
258258
}
259-
260-
if (zkde_screencast) {
261-
zkde_screencast_unstable_v1_destroy(zkde_screencast);
262-
zkde_screencast = nullptr;
259+
if (kde_screencast_v1_) {
260+
zkde_screencast_unstable_v1_destroy(kde_screencast_v1_);
261+
kde_screencast_v1_ = nullptr;
263262
}
264263

265264
if (kde_output_order) {
@@ -358,22 +357,11 @@ namespace kwin {
358357
* output width/height/x/y are populated.
359358
*/
360359
int start(const std::string_view &output_name) {
361-
// Check if KDE screencast protocol is available
362-
if (!zkde_screencast) {
363-
if (screencast_permission_helper_t::is_newly_initialized()) {
364-
BOOST_LOG(error) << "[kwingrab] zkde_screencast_unstable_v1 not found in registry. "sv
365-
"A new permission desktop file was automatically created but might now have been recognized yet. Try restarting."sv;
366-
} else {
367-
BOOST_LOG(error) << "[kwingrab] zkde_screencast_unstable_v1 not found in registry. Check permission "sv
368-
"desktop file for sunshine binary or set KWIN_WAYLAND_NO_PERMISSION_CHECKS=1"sv;
369-
}
370-
return -1;
371-
}
360+
// Try find correct output by name
372361
if (outputs.empty()) {
373362
BOOST_LOG(error) << "[kwingrab] no wl_output found"sv;
374363
return -1;
375364
}
376-
// Try find correct output by name
377365
struct wl_output *output = nullptr;
378366
if (!output_name.empty()) {
379367
for (auto const &[output_, params_] : outputs) {
@@ -391,8 +379,21 @@ namespace kwin {
391379
}
392380

393381
// Request a stream for the chosen output with embedded cursor
394-
zkde_screencast_stream = zkde_screencast_unstable_v1_stream_output(zkde_screencast, output, ZKDE_SCREENCAST_UNSTABLE_V1_POINTER_EMBEDDED);
395-
zkde_screencast_stream_unstable_v1_add_listener(zkde_screencast_stream, &stream_listener, this);
382+
if (kde_screencast_v1_) {
383+
kde_screencast_stream_v1_ = zkde_screencast_unstable_v1_stream_output(kde_screencast_v1_, output, ZKDE_SCREENCAST_UNSTABLE_V1_POINTER_EMBEDDED);
384+
zkde_screencast_stream_unstable_v1_add_listener(kde_screencast_stream_v1_, &stream_listener, this);
385+
} else {
386+
// No screencast protocol found. Output an error based on newly initialized permission file.
387+
if (screencast_permission_helper_t::is_newly_initialized()) {
388+
BOOST_LOG(error) << "[kwingrab] zkde_screencast_unstable_v1 not found in registry. "sv
389+
"A new permission desktop file was automatically created but might now have been recognized yet. "sv
390+
"Try restarting sunshine or set KWIN_WAYLAND_NO_PERMISSION_CHECKS=1 to fully disable permission checks."sv;
391+
} else {
392+
BOOST_LOG(error) << "[kwingrab] zkde_screencast_unstable_v1 not found in registry. Check permission desktop file "sv
393+
"for sunshine binary or set KWIN_WAYLAND_NO_PERMISSION_CHECKS=1 to fully disable permission checks."sv;
394+
}
395+
return -1;
396+
}
396397

397398
// Dispatch until we get created/failed, with a 5s timeout
398399
auto deadline = std::chrono::steady_clock::now() + 5s;
@@ -447,8 +448,8 @@ namespace kwin {
447448
struct wl_display *wl_display = nullptr;
448449
struct wl_registry *wl_registry = nullptr;
449450
struct kde_output_order_v1 *kde_output_order = nullptr;
450-
struct zkde_screencast_unstable_v1 *zkde_screencast = nullptr;
451-
struct zkde_screencast_stream_unstable_v1 *zkde_screencast_stream = nullptr;
451+
struct zkde_screencast_unstable_v1 *kde_screencast_v1_ = nullptr;
452+
struct zkde_screencast_stream_unstable_v1 *kde_screencast_stream_v1_ = nullptr;
452453
std::map<struct wl_output *, std::shared_ptr<output_parameter_t>> outputs;
453454
std::vector<std::string> output_order;
454455
bool stream_failed = false;
@@ -479,7 +480,7 @@ namespace kwin {
479480
} else if (!std::strcmp(interface, zkde_screencast_unstable_v1_interface.name)) {
480481
// Bind version 1 — we only use stream_output which is v1
481482
uint32_t bind_ver = std::min(version, static_cast<uint32_t>(1));
482-
self->zkde_screencast = static_cast<struct zkde_screencast_unstable_v1 *>(
483+
self->kde_screencast_v1_ = static_cast<struct zkde_screencast_unstable_v1 *>(
483484
wl_registry_bind(reg, name, &zkde_screencast_unstable_v1_interface, bind_ver)
484485
);
485486
BOOST_LOG(debug) << "[kwingrab] bound zkde_screencast_unstable_v1 v"sv << bind_ver;
@@ -493,7 +494,7 @@ namespace kwin {
493494
const auto [_, inserted] = self->outputs.try_emplace(output, std::make_shared<output_parameter_t>());
494495
if (inserted) {
495496
wl_output_add_listener(output, &output_listener, self);
496-
BOOST_LOG(debug) << "[kwingrab] bound wl_output_v4 v"sv << bind_ver << " instance: "sv << output;
497+
BOOST_LOG(debug) << "[kwingrab] bound wl_output v"sv << bind_ver << " instance: "sv << output;
497498
} else {
498499
// If we for some odd reason cannot add the output to the map clean it up and log a warning
499500
BOOST_LOG(warning) << "[kwingrab] Ignoring output "sv << output << " because map emplace failed."sv;
@@ -555,7 +556,22 @@ namespace kwin {
555556
.description = on_output_description,
556557
};
557558

558-
// ─── ScreenCast stream listener ───
559+
// ─── Output order listener ───
560+
static void on_output_order_output(void *data, struct kde_output_order_v1 *kde_output_order_v1 [[maybe_unused]], const char *output_name) {
561+
auto *self = static_cast<screencast_t *>(data);
562+
self->output_order.emplace_back(output_name);
563+
}
564+
565+
static void on_output_order_done(void *data [[maybe_unused]], struct kde_output_order_v1 *kde_output_order_v1 [[maybe_unused]]) {
566+
// Currently unused
567+
}
568+
569+
static constexpr kde_output_order_v1_listener output_order_listener = {
570+
.output = on_output_order_output,
571+
.done = on_output_order_done,
572+
};
573+
574+
// ─── ScreenCast v1 stream listener ───
559575
static void on_stream_closed(void *data, struct zkde_screencast_stream_unstable_v1 *stream [[maybe_unused]]) {
560576
auto *self = static_cast<screencast_t *>(data);
561577
BOOST_LOG(warning) << "[kwingrab] stream closed by server"sv;
@@ -581,21 +597,6 @@ namespace kwin {
581597
.created = on_stream_created,
582598
.failed = on_stream_failed,
583599
};
584-
585-
// ─── Output order listener ───
586-
static void on_output_order_output(void *data, struct kde_output_order_v1 *kde_output_order_v1 [[maybe_unused]], const char *output_name) {
587-
auto *self = static_cast<screencast_t *>(data);
588-
self->output_order.emplace_back(output_name);
589-
}
590-
591-
static void on_output_order_done(void *data [[maybe_unused]], struct kde_output_order_v1 *kde_output_order_v1 [[maybe_unused]]) {
592-
// Currently unused
593-
}
594-
595-
static constexpr kde_output_order_v1_listener output_order_listener = {
596-
.output = on_output_order_output,
597-
.done = on_output_order_done,
598-
};
599600
};
600601

601602
// ─── Display backend ─────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)