/* SPDX-FileCopyrightText: 2020 Aleix Pol Gonzalez SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL */ #include "screencasting.h" #include "qwayland-zkde-screencast-unstable-v1.h" #include #include #include #include #include #include #include #include using namespace KWayland::Client; class ScreencastingStreamPrivate : public QtWayland::zkde_screencast_stream_unstable_v1 { public: explicit ScreencastingStreamPrivate(ScreencastingStream *q) : q(q) { } ~ScreencastingStreamPrivate() override { if (isInitialized()) { close(); } q->deleteLater(); } Q_DISABLE_COPY_MOVE(ScreencastingStreamPrivate) void zkde_screencast_stream_unstable_v1_created(uint32_t node) override { m_nodeId = node; Q_EMIT q->created(node); } void zkde_screencast_stream_unstable_v1_closed() override { Q_EMIT q->closed(); } void zkde_screencast_stream_unstable_v1_failed(const QString &error) override { Q_EMIT q->failed(error); } std::optional m_nodeId; QPointer q; }; ScreencastingStream::ScreencastingStream(QObject *parent) : QObject(parent) , d(new ScreencastingStreamPrivate(this)) { } ScreencastingStream::~ScreencastingStream() = default; quint32 ScreencastingStream::nodeId() const { Q_ASSERT(d->m_nodeId.has_value()); return d->m_nodeId.value_or(0); } class ScreencastingPrivate : public QWaylandClientExtensionTemplate, public QtWayland::zkde_screencast_unstable_v1 { public: explicit ScreencastingPrivate(Screencasting *q) : QWaylandClientExtensionTemplate(ZKDE_SCREENCAST_UNSTABLE_V1_STREAM_REGION_SINCE_VERSION) , q(q) { #if QTWAYLANDCLIENT_VERSION >= QT_VERSION_CHECK(6, 2, 0) initialize(); #else // QWaylandClientExtensionTemplate invokes this with a QueuedConnection but // we want it called immediately QMetaObject::invokeMethod(this, "addRegistryListener", Qt::DirectConnection); #endif if (!isInitialized()) { qWarning() << "Remember requesting the interface on your desktop file: " "X-KDE-Wayland-Interfaces=zkde_screencast_unstable_v1"; } Q_ASSERT(isInitialized()); } ~ScreencastingPrivate() override { destroy(); } Q_DISABLE_COPY_MOVE(ScreencastingPrivate) Screencasting *const q; }; Screencasting::Screencasting(QObject *parent) : QObject(parent) , d(new ScreencastingPrivate(this)) { } Screencasting::~Screencasting() = default; ScreencastingStream *Screencasting::createRegionStream(const QRect &geometry, qreal scaling, CursorMode mode) { Q_ASSERT(d->QWaylandClientExtension::version() >= ZKDE_SCREENCAST_UNSTABLE_V1_STREAM_REGION_SINCE_VERSION); auto stream = new ScreencastingStream(this); stream->setObjectName(QStringLiteral("region-%1,%2 (%3x%4)").arg(geometry.x()).arg(geometry.y()).arg(geometry.width()).arg(geometry.height())); stream->d->init(d->stream_region(geometry.x(), geometry.y(), geometry.width(), geometry.height(), wl_fixed_from_double(scaling), mode)); return stream; } void Screencasting::destroy() { d.reset(nullptr); }