From 7bcd2c3e76fe6a98f59ae1afdb6ddc1a2c8ce2aa Mon Sep 17 00:00:00 2001 From: Gres Date: Sat, 24 Apr 2021 12:15:57 +0300 Subject: [PATCH] Make wayland implementation ifdef'ed --- src/capture/capturer.cpp | 5 +---- src/capture/waylandcapturer.cpp | 33 +++++++++++++++++++++++---------- src/capture/waylandcapturer.h | 22 ++++++++++++++++++---- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/capture/capturer.cpp b/src/capture/capturer.cpp index 08a2c58..f391e6a 100644 --- a/src/capture/capturer.cpp +++ b/src/capture/capturer.cpp @@ -16,11 +16,8 @@ Capturer::Capturer(Manager &manager, const Settings &settings, , settings_(settings) , selector_(std::make_unique(*this, settings_, models, pixmap_)) + , wayland_(WaylandCapturer::create()) { -#ifdef Q_OS_LINUX - if (WaylandCapturer::isWayland()) - wayland_ = std::make_unique(); -#endif } Capturer::~Capturer() = default; diff --git a/src/capture/waylandcapturer.cpp b/src/capture/waylandcapturer.cpp index 78befb3..27dd2b3 100644 --- a/src/capture/waylandcapturer.cpp +++ b/src/capture/waylandcapturer.cpp @@ -1,6 +1,8 @@ #include "waylandcapturer.h" #include "debug.h" +#ifdef Q_OS_LINUX + #include #include #include @@ -44,13 +46,13 @@ X-KDE-DBUS-Restricted-Interfaces=org.kde.kwin.Screenshot } // namespace -bool WaylandCapturer::isWayland() +bool WaylandCapturerImpl::isWayland() { return qEnvironmentVariable("XDG_SESSION_TYPE").toLower() == QStringLiteral("wayland"); } -WaylandCapturer::Method WaylandCapturer::getMethod() +WaylandCapturerImpl::Method WaylandCapturerImpl::getMethod() { auto de = qEnvironmentVariable("XDG_CURRENT_DESKTOP").toLower(); if (de == QLatin1String("kde")) { @@ -61,20 +63,20 @@ WaylandCapturer::Method WaylandCapturer::getMethod() return Method::Freedesktop; } -WaylandCapturer::WaylandCapturer() +WaylandCapturerImpl::WaylandCapturerImpl() : method_(getMethod()) { if (method_ == Method::Kde) writeDesktop(); } -WaylandCapturer::~WaylandCapturer() +WaylandCapturerImpl::~WaylandCapturerImpl() { if (method_ == Method::Kde) removeDesktop(); } -QPixmap WaylandCapturer::grab() +QPixmap WaylandCapturerImpl::grab() { switch (method_) { case Method::Gnome: return grabGnome(); @@ -84,7 +86,7 @@ QPixmap WaylandCapturer::grab() return {}; } -QPixmap WaylandCapturer::grabKde() +QPixmap WaylandCapturerImpl::grabKde() { auto request = QDBusMessage::createMethodCall( QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"), @@ -106,7 +108,7 @@ QPixmap WaylandCapturer::grabKde() return result; } -QPixmap WaylandCapturer::grabGnome() +QPixmap WaylandCapturerImpl::grabGnome() { auto request = QDBusMessage::createMethodCall( QStringLiteral("org.gnome.Shell.Screenshot"), @@ -138,7 +140,7 @@ QPixmap WaylandCapturer::grabGnome() return QPixmap(f.fileName()); } -QPixmap WaylandCapturer::grabFreedesktop() +QPixmap WaylandCapturerImpl::grabFreedesktop() { auto request = QDBusMessage::createMethodCall( QStringLiteral("org.freedesktop.portal.Desktop"), @@ -172,8 +174,8 @@ QPixmap WaylandCapturer::grabFreedesktop() return result_; } -void WaylandCapturer::parseFreedesktopResult(uint response, - const QVariantMap &results) +void WaylandCapturerImpl::parseFreedesktopResult(uint response, + const QVariantMap &results) { if (response == 0) { const auto name = QUrl(results["uri"].toString()).toLocalFile(); @@ -182,3 +184,14 @@ void WaylandCapturer::parseFreedesktopResult(uint response, } loop_.exit(); } + +#endif + +std::unique_ptr WaylandCapturer::create() +{ +#ifdef Q_OS_LINUX + if (WaylandCapturerImpl::isWayland()) + return std::make_unique(); +#endif + return {}; +} diff --git a/src/capture/waylandcapturer.h b/src/capture/waylandcapturer.h index ccfc187..22c581e 100644 --- a/src/capture/waylandcapturer.h +++ b/src/capture/waylandcapturer.h @@ -4,16 +4,28 @@ #include #include -class WaylandCapturer : public QObject +class WaylandCapturer +{ +public: + virtual ~WaylandCapturer() = default; + + virtual QPixmap grab() = 0; + + static std::unique_ptr create(); +}; + +#ifdef Q_OS_LINUX + +class WaylandCapturerImpl : public QObject, public WaylandCapturer { Q_OBJECT public: - WaylandCapturer(); - ~WaylandCapturer(); + WaylandCapturerImpl(); + ~WaylandCapturerImpl(); static bool isWayland(); - QPixmap grab(); + QPixmap grab() override; private slots: void parseFreedesktopResult(uint response, const QVariantMap &results); @@ -30,3 +42,5 @@ private: QEventLoop loop_; QPixmap result_; }; + +#endif