Make wayland implementation ifdef'ed

This commit is contained in:
Gres 2021-04-24 12:15:57 +03:00
parent 594fc3e665
commit 7bcd2c3e76
3 changed files with 42 additions and 18 deletions

View File

@ -16,11 +16,8 @@ Capturer::Capturer(Manager &manager, const Settings &settings,
, settings_(settings) , settings_(settings)
, selector_(std::make_unique<CaptureAreaSelector>(*this, settings_, models, , selector_(std::make_unique<CaptureAreaSelector>(*this, settings_, models,
pixmap_)) pixmap_))
, wayland_(WaylandCapturer::create())
{ {
#ifdef Q_OS_LINUX
if (WaylandCapturer::isWayland())
wayland_ = std::make_unique<WaylandCapturer>();
#endif
} }
Capturer::~Capturer() = default; Capturer::~Capturer() = default;

View File

@ -1,6 +1,8 @@
#include "waylandcapturer.h" #include "waylandcapturer.h"
#include "debug.h" #include "debug.h"
#ifdef Q_OS_LINUX
#include <QCoreApplication> #include <QCoreApplication>
#include <QDBusConnection> #include <QDBusConnection>
#include <QDBusPendingReply> #include <QDBusPendingReply>
@ -44,13 +46,13 @@ X-KDE-DBUS-Restricted-Interfaces=org.kde.kwin.Screenshot
} // namespace } // namespace
bool WaylandCapturer::isWayland() bool WaylandCapturerImpl::isWayland()
{ {
return qEnvironmentVariable("XDG_SESSION_TYPE").toLower() == return qEnvironmentVariable("XDG_SESSION_TYPE").toLower() ==
QStringLiteral("wayland"); QStringLiteral("wayland");
} }
WaylandCapturer::Method WaylandCapturer::getMethod() WaylandCapturerImpl::Method WaylandCapturerImpl::getMethod()
{ {
auto de = qEnvironmentVariable("XDG_CURRENT_DESKTOP").toLower(); auto de = qEnvironmentVariable("XDG_CURRENT_DESKTOP").toLower();
if (de == QLatin1String("kde")) { if (de == QLatin1String("kde")) {
@ -61,20 +63,20 @@ WaylandCapturer::Method WaylandCapturer::getMethod()
return Method::Freedesktop; return Method::Freedesktop;
} }
WaylandCapturer::WaylandCapturer() WaylandCapturerImpl::WaylandCapturerImpl()
: method_(getMethod()) : method_(getMethod())
{ {
if (method_ == Method::Kde) if (method_ == Method::Kde)
writeDesktop(); writeDesktop();
} }
WaylandCapturer::~WaylandCapturer() WaylandCapturerImpl::~WaylandCapturerImpl()
{ {
if (method_ == Method::Kde) if (method_ == Method::Kde)
removeDesktop(); removeDesktop();
} }
QPixmap WaylandCapturer::grab() QPixmap WaylandCapturerImpl::grab()
{ {
switch (method_) { switch (method_) {
case Method::Gnome: return grabGnome(); case Method::Gnome: return grabGnome();
@ -84,7 +86,7 @@ QPixmap WaylandCapturer::grab()
return {}; return {};
} }
QPixmap WaylandCapturer::grabKde() QPixmap WaylandCapturerImpl::grabKde()
{ {
auto request = QDBusMessage::createMethodCall( auto request = QDBusMessage::createMethodCall(
QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"), QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"),
@ -106,7 +108,7 @@ QPixmap WaylandCapturer::grabKde()
return result; return result;
} }
QPixmap WaylandCapturer::grabGnome() QPixmap WaylandCapturerImpl::grabGnome()
{ {
auto request = QDBusMessage::createMethodCall( auto request = QDBusMessage::createMethodCall(
QStringLiteral("org.gnome.Shell.Screenshot"), QStringLiteral("org.gnome.Shell.Screenshot"),
@ -138,7 +140,7 @@ QPixmap WaylandCapturer::grabGnome()
return QPixmap(f.fileName()); return QPixmap(f.fileName());
} }
QPixmap WaylandCapturer::grabFreedesktop() QPixmap WaylandCapturerImpl::grabFreedesktop()
{ {
auto request = QDBusMessage::createMethodCall( auto request = QDBusMessage::createMethodCall(
QStringLiteral("org.freedesktop.portal.Desktop"), QStringLiteral("org.freedesktop.portal.Desktop"),
@ -172,8 +174,8 @@ QPixmap WaylandCapturer::grabFreedesktop()
return result_; return result_;
} }
void WaylandCapturer::parseFreedesktopResult(uint response, void WaylandCapturerImpl::parseFreedesktopResult(uint response,
const QVariantMap &results) const QVariantMap &results)
{ {
if (response == 0) { if (response == 0) {
const auto name = QUrl(results["uri"].toString()).toLocalFile(); const auto name = QUrl(results["uri"].toString()).toLocalFile();
@ -182,3 +184,14 @@ void WaylandCapturer::parseFreedesktopResult(uint response,
} }
loop_.exit(); loop_.exit();
} }
#endif
std::unique_ptr<WaylandCapturer> WaylandCapturer::create()
{
#ifdef Q_OS_LINUX
if (WaylandCapturerImpl::isWayland())
return std::make_unique<WaylandCapturerImpl>();
#endif
return {};
}

View File

@ -4,16 +4,28 @@
#include <QObject> #include <QObject>
#include <QPixmap> #include <QPixmap>
class WaylandCapturer : public QObject class WaylandCapturer
{
public:
virtual ~WaylandCapturer() = default;
virtual QPixmap grab() = 0;
static std::unique_ptr<WaylandCapturer> create();
};
#ifdef Q_OS_LINUX
class WaylandCapturerImpl : public QObject, public WaylandCapturer
{ {
Q_OBJECT Q_OBJECT
public: public:
WaylandCapturer(); WaylandCapturerImpl();
~WaylandCapturer(); ~WaylandCapturerImpl();
static bool isWayland(); static bool isWayland();
QPixmap grab(); QPixmap grab() override;
private slots: private slots:
void parseFreedesktopResult(uint response, const QVariantMap &results); void parseFreedesktopResult(uint response, const QVariantMap &results);
@ -30,3 +42,5 @@ private:
QEventLoop loop_; QEventLoop loop_;
QPixmap result_; QPixmap result_;
}; };
#endif