File indexing completed on 2024-04-21 16:13:08

0001 /*
0002     SPDX-FileCopyrightText: 2020 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "screencastingrequest.h"
0008 #include "logging.h"
0009 
0010 #include <KWayland/Client/connection_thread.h>
0011 #include <KWayland/Client/registry.h>
0012 #include <QCoreApplication>
0013 #include <QDebug>
0014 #include <QPointer>
0015 #include <functional>
0016 
0017 struct ScreencastingRequestPrivate {
0018     QPointer<ScreencastingStream> m_stream;
0019     QString m_uuid;
0020     QString m_outputName;
0021     quint32 m_nodeId = 0;
0022 };
0023 
0024 ScreencastingRequest::ScreencastingRequest(QObject *parent)
0025     : QObject(parent)
0026     , d(new ScreencastingRequestPrivate)
0027 {
0028 }
0029 
0030 ScreencastingRequest::~ScreencastingRequest() = default;
0031 
0032 quint32 ScreencastingRequest::nodeId() const
0033 {
0034     return d->m_nodeId;
0035 }
0036 
0037 void ScreencastingRequest::setUuid(const QString &uuid)
0038 {
0039     if (d->m_uuid == uuid) {
0040         return;
0041     }
0042 
0043     d->m_stream->deleteLater();
0044     setNodeid(0);
0045 
0046     d->m_uuid = uuid;
0047     Q_EMIT uuidChanged(uuid);
0048 
0049     if (!d->m_uuid.isEmpty()) {
0050         auto screencasting = new Screencasting(this);
0051         auto stream = screencasting->createWindowStream(d->m_uuid, Screencasting::CursorMode::Hidden);
0052         adopt(stream);
0053     }
0054 }
0055 
0056 void ScreencastingRequest::setOutputName(const QString &outputName)
0057 {
0058     if (d->m_outputName == outputName) {
0059         return;
0060     }
0061 
0062     setNodeid(0);
0063     d->m_outputName = outputName;
0064     Q_EMIT outputNameChanged(outputName);
0065 
0066     if (!d->m_outputName.isEmpty()) {
0067         auto screencasting = new Screencasting(this);
0068         auto stream = screencasting->createOutputStream(d->m_outputName, Screencasting::CursorMode::Hidden);
0069         adopt(stream);
0070         stream->setObjectName(d->m_outputName);
0071     }
0072 }
0073 
0074 void ScreencastingRequest::adopt(ScreencastingStream *stream)
0075 {
0076     connect(stream, &ScreencastingStream::created, this, &ScreencastingRequest::setNodeid);
0077     connect(stream, &ScreencastingStream::failed, this, [](const QString &error) {
0078         qWarning() << "error creating screencast" << error;
0079     });
0080     connect(stream, &ScreencastingStream::closed, this, [this, stream] {
0081         if (stream->nodeId() == d->m_nodeId) {
0082             setNodeid(0);
0083         }
0084     });
0085 }
0086 
0087 void ScreencastingRequest::setNodeid(uint nodeId)
0088 {
0089     if (nodeId == d->m_nodeId) {
0090         return;
0091     }
0092 
0093     d->m_nodeId = nodeId;
0094     Q_EMIT nodeIdChanged(nodeId);
0095 }
0096 
0097 QString ScreencastingRequest::uuid() const
0098 {
0099     return d->m_uuid;
0100 }
0101 
0102 QString ScreencastingRequest::outputName() const
0103 {
0104     return d->m_outputName;
0105 }