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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
// SPDX-FileCopyrightText: 2024 Arjen Hiemstra <ahiemstra@heimr.nl>
// SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
#include "SessionController.h"
#include <Cursor.h>
#include <InputHandler.h>
#include <KLocalizedString>
#include <PortalSession.h>
#include <QAction>
#include <QCoreApplication>
#include <QDBusInterface>
#include <QMenu>
#include <RdpConnection.h>
#include <Server.h>
#ifdef WITH_PLASMA_SESSION
#include <PlasmaScreencastV1Session.h>
#endif
class SessionWrapper : public QObject
{
Q_OBJECT
public:
SessionWrapper(KRdp::RdpConnection *conn, std::unique_ptr<KRdp::AbstractSession> &&sess, KStatusNotifierItem *sni)
: session(std::move(sess))
, connection(conn)
{
m_sni = sni;
connect(session.get(), &KRdp::AbstractSession::frameReceived, connection->videoStream(), &KRdp::VideoStream::queueFrame);
connect(session.get(), &KRdp::AbstractSession::cursorUpdate, this, &SessionWrapper::onCursorUpdate);
connect(session.get(), &KRdp::AbstractSession::error, this, &SessionWrapper::sessionError);
connect(connection->videoStream(), &KRdp::VideoStream::enabledChanged, this, &SessionWrapper::onVideoStreamEnabledChanged);
connect(connection->videoStream(), &KRdp::VideoStream::requestedFrameRateChanged, this, &SessionWrapper::onRequestedFrameRateChanged);
connect(connection->inputHandler(), &KRdp::InputHandler::inputEvent, session.get(), &KRdp::AbstractSession::sendEvent);
connect(connection, &QObject::destroyed, this, &SessionWrapper::onConnectionDestroyed);
}
void onCursorUpdate(const PipeWireCursor &cursor)
{
if (!connection) {
return;
}
KRdp::Cursor::CursorUpdate update;
update.hotspot = cursor.hotspot;
update.image = cursor.texture;
connection->cursor()->update(update);
}
void onVideoStreamEnabledChanged()
{
if (connection->videoStream()->enabled()) {
session->requestStreamingEnable(connection->videoStream());
} else {
session->requestStreamingDisable(connection->videoStream());
}
}
void onRequestedFrameRateChanged()
{
session->setVideoFrameRate(connection->videoStream()->requestedFrameRate());
}
void onConnectionDestroyed()
{
Q_EMIT connectionDestroyed(this);
}
Q_SIGNAL void sessionError();
Q_SIGNAL void connectionDestroyed(SessionWrapper *wrapper);
std::unique_ptr<KRdp::AbstractSession> session;
QPointer<KRdp::RdpConnection> connection;
KStatusNotifierItem *m_sni;
};
SessionController::SessionController(KRdp::Server *server, SessionType sessionType)
: m_server(server)
, m_sessionType(sessionType)
{
connect(m_server, &KRdp::Server::newConnectionCreated, this, &SessionController::onNewConnection);
// Status notification item
m_sni = new KStatusNotifierItem(u"krdpserver"_qs, this);
auto menu = new QMenu(u"quitMenu"_qs);
// Disable default quit button since it has confirmation dialog
m_sni->setStandardActionsEnabled(false);
m_sni->setTitle(i18n("RDP Server"));
m_sni->setIconByName(u"preferences-system-network-remote"_qs);
m_sni->setStatus(KStatusNotifierItem::Passive);
auto quitAction = new QAction(i18n("Quit"), menu);
quitAction->setIcon(QIcon::fromTheme(QStringLiteral("application-exit")));
connect(quitAction, &QAction::triggered, this, &SessionController::stopFromSNI);
menu->addAction(quitAction);
m_sni->setContextMenu(menu);
// Create a single temporary session to request permissions from the portal
// on startup.
if (sessionType == SessionType::Portal) {
m_initializationSession = makeSession();
auto cleanup = [this]() {
auto session = m_initializationSession.release();
session->deleteLater();
};
// Destroy the session after we've successfully connected, we just use
// it to get permissions. Reusing it is going to lead to problems with
// reconnection.
connect(m_initializationSession.get(), &KRdp::AbstractSession::started, this, cleanup);
connect(m_initializationSession.get(), &KRdp::AbstractSession::error, this, cleanup);
m_initializationSession->start();
}
}
SessionController::~SessionController() noexcept
{
}
void SessionController::setMonitorIndex(const std::optional<int> &index)
{
m_monitorIndex = index;
}
void SessionController::setVirtualMonitor(const KRdp::VirtualMonitor &virtualMonitor)
{
m_virtualMonitor = virtualMonitor;
}
void SessionController::setQuality(const std::optional<int> &quality)
{
m_quality = quality;
}
void SessionController::onNewConnection(KRdp::RdpConnection *newConnection)
{
auto wrapper = std::make_unique<SessionWrapper>(newConnection, makeSession(), m_sni);
if (m_virtualMonitor) {
wrapper->session->setVirtualMonitor(*m_virtualMonitor);
} else {
wrapper->session->setActiveStream(m_monitorIndex.value_or(-1));
}
wrapper->session->setVideoQuality(m_quality.value());
connect(wrapper.get(), &SessionWrapper::connectionDestroyed, this, [this](SessionWrapper *wrapper) {
m_wrappers.erase(std::remove_if(m_wrappers.begin(),
m_wrappers.end(),
[wrapper](std::unique_ptr<SessionWrapper> &entry) {
return entry.get() == wrapper;
}),
m_wrappers.end());
});
connect(wrapper.get(), &SessionWrapper::sessionError, this, [newConnection] {
newConnection->close(KRdp::RdpConnection::CloseReason::None);
});
wrapper->session->start();
m_wrappers.push_back(std::move(wrapper));
}
void SessionController::stopFromSNI()
{
// Uses dbus to stop the server service, like in the KCM
// This kills all krdpserver instances, like a "panic button"
QDBusInterface unit(u"org.freedesktop.systemd1"_qs,
u"/org/freedesktop/systemd1/unit/plasma_2dkrdp_5fserver_2eservice"_qs,
u"org.freedesktop.systemd1.Unit"_qs);
unit.asyncCall(u"Stop"_qs);
QCoreApplication::quit();
}
std::unique_ptr<KRdp::AbstractSession> SessionController::makeSession()
{
#ifdef WITH_PLASMA_SESSION
if (m_sessionType == SessionType::Plasma) {
return std::make_unique<KRdp::PlasmaScreencastV1Session>(m_server);
} else
#endif
{
return std::make_unique<KRdp::PortalSession>(m_server);
}
}
#include "SessionController.moc"
|