1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
/*
SPDX-FileCopyrightText: 2020 Aleix Pol Gonzalez <aleixpol@kde.org>
SPDX-FileCopyrightText: 2023 iaom <zhangpengfei@kylinos.cn>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "screen-casting-request.h"
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/registry.h>
#include <QCoreApplication>
#include <QDebug>
#include <QPointer>
#include <functional>
class ScreencastingSingleton : public QObject
{
Q_OBJECT
public:
ScreencastingSingleton(QObject *parent)
: QObject(parent)
{
KWayland::Client::ConnectionThread *connection = KWayland::Client::ConnectionThread::fromApplication(this);
if (!connection) {
return;
}
KWayland::Client::Registry *registry = new KWayland::Client::Registry(this);
connect(registry,
&KWayland::Client::Registry::interfaceAnnounced,
this,
[this, registry](const QByteArray &interfaceName, quint32 name, quint32 version) {
if (interfaceName != "zkde_screencast_unstable_v1")
return;
m_screencasting = new Screencasting(registry, name, version, this);
Q_EMIT created(m_screencasting);
});
registry->create(connection);
registry->setup();
}
static ScreencastingSingleton *self()
{
static QPointer<ScreencastingSingleton> s_self;
if (!s_self && QCoreApplication::instance())
s_self = new ScreencastingSingleton(QCoreApplication::instance());
return s_self;
}
void requestInterface(ScreenCastingRequest *item)
{
if (!m_screencasting) {
connect(this, &ScreencastingSingleton::created, item, &ScreenCastingRequest::create, Qt::UniqueConnection);
} else {
item->create(m_screencasting);
}
}
Q_SIGNALS:
void created(Screencasting *screencasting);
private:
Screencasting *m_screencasting = nullptr;
};
ScreenCastingRequest::ScreenCastingRequest(QObject *parent)
:QObject(parent)
{
}
ScreenCastingRequest::~ScreenCastingRequest() = default;
quint32 ScreenCastingRequest::nodeId() const
{
return m_nodeId;
}
void ScreenCastingRequest::setNodeid(uint nodeId)
{
if (nodeId == m_nodeId) {
return;
}
m_nodeId = nodeId;
Q_EMIT nodeIdChanged(nodeId);
}
QString ScreenCastingRequest::uuid() const
{
return m_uuid;
}
void ScreenCastingRequest::setUuid(const QString &uuid)
{
if (m_uuid == uuid) {
return;
}
Q_EMIT closeRunningStreams();
setNodeid(0);
m_uuid = uuid;
if (!m_uuid.isEmpty()) {
ScreencastingSingleton::self()->requestInterface(this);
}
Q_EMIT uuidChanged(uuid);
}
void ScreenCastingRequest::create(Screencasting *screencasting)
{
auto stream = screencasting->createWindowStream(m_uuid, Screencasting::CursorMode::Hidden);
stream->setObjectName(m_uuid);
connect(stream, &ScreencastingStream::created, this, [stream, this](int nodeId) {
if (stream->objectName() == m_uuid) {
setNodeid(nodeId);
}
});
connect(stream, &ScreencastingStream::failed, this, [](const QString &error) {
qWarning() << "error creating screencast" << error;
});
connect(stream, &ScreencastingStream::closed, this, [this, stream] {
if (stream->nodeId() == m_nodeId) {
setNodeid(0);
}
});
connect(this, &ScreenCastingRequest::closeRunningStreams, stream, &QObject::deleteLater);
}
#include "screen-casting-request.moc"
|