Commit 4707bde2 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧 Committed by Aleix Pol Gonzalez
Browse files

Introduce krfb-virtualmonitor

It implements a KWin protocol that is oriented towards serving a virtual
display specifically.
It requests KWin a stream that will act as a monitor that we can feed
into remote clients.
parent 608762c7
Pipeline #92236 passed with stage
in 47 seconds
......@@ -17,3 +17,5 @@ Dependencies:
'frameworks/kwidgetsaddons': '@stable'
'frameworks/kwindowsystem': '@stable'
'frameworks/kxmlgui': '@stable'
'frameworks/kwayland': '@stable'
'libraries/plasma-wayland-protocols': '@latest' # can be switched to @stable when 1.5.0 is released
......@@ -38,6 +38,7 @@ find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS
DocTools
Notifications
Wallet
Wayland
WidgetsAddons
WindowSystem
XmlGui
......@@ -80,6 +81,14 @@ find_package(LibVNCServer REQUIRED)
pkg_check_modules(PipeWire IMPORTED_TARGET libpipewire-0.3)
add_feature_info(PipeWire PipeWire_FOUND "Required for pipewire screencast plugin")
find_package(PlasmaWaylandProtocols 1.5.0)
if(PipeWire_FOUND AND PlasmaWaylandProtocols_FOUND)
find_package(QtWaylandScanner REQUIRED)
find_package(Qt5WaylandClient)
find_package(Qt5XkbCommonSupport)
find_package(Wayland REQUIRED COMPONENTS Client)
endif()
find_package(gbm)
set_package_properties(gbm PROPERTIES
......
......@@ -5,8 +5,14 @@ include_directories (${CMAKE_CURRENT_SOURCE_DIR}
set (krfb_framebuffer_pw_SRCS
pw_framebuffer.cpp
pw_framebufferplugin.cpp
screencasting.cpp
)
ecm_add_qtwayland_client_protocol(krfb_framebuffer_pw_SRCS
PROTOCOL ${PLASMA_WAYLAND_PROTOCOLS_DIR}/screencast.xml
BASENAME zkde-screencast-unstable-v1
)
ecm_qt_declare_logging_category(krfb_framebuffer_pw_SRCS
HEADER krfb_fb_pipewire_debug.h
......@@ -39,6 +45,8 @@ target_link_libraries(krfb_framebuffer_pw
Qt5::Gui
Qt5::DBus
KF5::CoreAddons
KF5::WaylandClient
Wayland::Client
krfbprivate
PkgConfig::PipeWire
)
......
......@@ -22,6 +22,9 @@
#include <QDebug>
#include <QRandomGenerator>
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/registry.h>
// pipewire
#include <sys/ioctl.h>
......@@ -38,6 +41,7 @@
#include "xdp_dbus_screencast_interface.h"
#include "xdp_dbus_remotedesktop_interface.h"
#include "krfb_fb_pipewire_debug.h"
#include "screencasting.h"
#if HAVE_DMA_BUF
#include <fcntl.h>
......@@ -904,10 +908,6 @@ PWFrameBuffer::PWFrameBuffer(WId winid, QObject *parent)
: FrameBuffer (winid, parent),
d(new Private(this))
{
// D-Bus is most important in init chain, no toys for us if something is wrong with XDP
// PipeWire connectivity is initialized after D-Bus session is started
d->initDbus();
fb = nullptr;
}
......@@ -917,6 +917,38 @@ PWFrameBuffer::~PWFrameBuffer()
fb = nullptr;
}
void PWFrameBuffer::initDBus()
{
d->initDbus();
}
void PWFrameBuffer::startVirtualMonitor(const QString& name, const QSize& resolution, qreal dpr)
{
d->videoSize = resolution * dpr;
using namespace KWayland::Client;
auto connection = ConnectionThread::fromApplication(this);
if (!connection) {
qWarning() << "Failed getting Wayland connection from QPA";
QCoreApplication::exit(1);
return;
}
auto registry = new Registry(this);
connect(registry, &KWayland::Client::Registry::interfaceAnnounced, this, [this, registry, name, dpr, resolution] (const QByteArray &interfaceName, quint32 wlname, quint32 version) {
if (interfaceName != "zkde_screencast_unstable_v1")
return;
auto screencasting = new Screencasting(registry, wlname, version, this);
auto r = screencasting->createVirtualMonitorStream(name, resolution, dpr, Screencasting::Metadata);
connect(r, &ScreencastingStream::created, this, [this] (quint32 nodeId) {
d->pwStreamNodeId = nodeId;
d->initPw();
});
});
registry->create(connection);
registry->setup();
}
int PWFrameBuffer::depth()
{
return 32;
......
......@@ -33,6 +33,9 @@ public:
PWFrameBuffer(WId winid, QObject *parent = nullptr);
virtual ~PWFrameBuffer() override;
void initDBus();
void startVirtualMonitor(const QString &name, const QSize &resolution, qreal dpr);
int depth() override;
int height() override;
int width() override;
......
......@@ -41,6 +41,13 @@ FrameBuffer *PWFrameBufferPlugin::frameBuffer(WId id, const QVariantMap &args)
//NOTE WId is irrelevant in Wayland
auto pwfb = new PWFrameBuffer(id);
if (args.contains(QLatin1String("name"))) {
pwfb->startVirtualMonitor(args[QStringLiteral("name")].toString(), args[QStringLiteral("resolution")].toSize(), args[QStringLiteral("scale")].toDouble());
} else {
// D-Bus is most important in XDG-Desktop-Portals init chain, no toys for us if something is wrong with XDP
// PipeWire connectivity is initialized after D-Bus session is started
pwfb->initDBus();
}
// sanity check for dbus/wayland/pipewire errors
if (!pwfb->isValid()) {
......
/*
SPDX-FileCopyrightText: 2020 Aleix Pol Gonzalez <aleixpol@kde.org>
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 <KWayland/Client/output.h>
#include <KWayland/Client/plasmawindowmanagement.h>
#include <KWayland/Client/registry.h>
#include <QDebug>
#include <QRect>
using namespace KWayland::Client;
class ScreencastingStreamPrivate : public QtWayland::zkde_screencast_stream_unstable_v1
{
public:
ScreencastingStreamPrivate(ScreencastingStream *q)
: q(q)
{
}
~ScreencastingStreamPrivate()
{
close();
q->deleteLater();
}
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);
}
uint m_nodeId = 0;
QPointer<ScreencastingStream> q;
};
ScreencastingStream::ScreencastingStream(QObject *parent)
: QObject(parent)
, d(new ScreencastingStreamPrivate(this))
{
}
ScreencastingStream::~ScreencastingStream() = default;
quint32 ScreencastingStream::nodeId() const
{
return d->m_nodeId;
}
class ScreencastingPrivate : public QtWayland::zkde_screencast_unstable_v1
{
public:
ScreencastingPrivate(Registry *registry, int id, int version, Screencasting *q)
: QtWayland::zkde_screencast_unstable_v1(*registry, id, version)
, q(q)
{
}
ScreencastingPrivate(::zkde_screencast_unstable_v1 *screencasting, Screencasting *q)
: QtWayland::zkde_screencast_unstable_v1(screencasting)
, q(q)
{
}
~ScreencastingPrivate()
{
destroy();
}
Screencasting *const q;
};
Screencasting::Screencasting(QObject *parent)
: QObject(parent)
{
}
Screencasting::Screencasting(Registry *registry, int id, int version, QObject *parent)
: QObject(parent)
, d(new ScreencastingPrivate(registry, id, version, this))
{
}
Screencasting::~Screencasting() = default;
ScreencastingStream *Screencasting::createOutputStream(Output *output, CursorMode mode)
{
auto stream = new ScreencastingStream(this);
stream->setObjectName(output->model());
stream->d->init(d->stream_output(*output, mode));
return stream;
}
ScreencastingStream *Screencasting::createWindowStream(PlasmaWindow *window, CursorMode mode)
{
auto stream = createWindowStream(QString::fromUtf8(window->uuid()), mode);
stream->setObjectName(window->appId());
return stream;
}
ScreencastingStream *Screencasting::createWindowStream(const QString &uuid, CursorMode mode)
{
auto stream = new ScreencastingStream(this);
stream->d->init(d->stream_window(uuid, mode));
return stream;
}
ScreencastingStream * Screencasting::createVirtualMonitorStream(const QString& name, const QSize& resolution, qreal dpr, Screencasting::CursorMode mode)
{
auto stream = new ScreencastingStream(this);
stream->d->init(d->stream_virtual_output(name, resolution.width(), resolution.height(), wl_fixed_from_double(dpr), mode));
return stream;
}
void Screencasting::setup(::zkde_screencast_unstable_v1 *screencasting)
{
d.reset(new ScreencastingPrivate(screencasting, this));
}
void Screencasting::destroy()
{
d.reset(nullptr);
}
/*
SPDX-FileCopyrightText: 2020 Aleix Pol Gonzalez <aleixpol@kde.org>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#pragma once
#include <QObject>
#include <QSharedPointer>
#include <QVector>
#include <optional>
struct zkde_screencast_unstable_v1;
namespace KWayland
{
namespace Client
{
class PlasmaWindow;
class Registry;
class Output;
}
}
class ScreencastingPrivate;
class ScreencastingSourcePrivate;
class ScreencastingStreamPrivate;
class ScreencastingStream : public QObject
{
Q_OBJECT
public:
ScreencastingStream(QObject *parent);
~ScreencastingStream() override;
quint32 nodeId() const;
Q_SIGNALS:
void created(quint32 nodeid);
void failed(const QString &error);
void closed();
private:
friend class Screencasting;
QScopedPointer<ScreencastingStreamPrivate> d;
};
class Screencasting : public QObject
{
Q_OBJECT
public:
explicit Screencasting(QObject *parent = nullptr);
explicit Screencasting(KWayland::Client::Registry *registry, int id, int version, QObject *parent = nullptr);
~Screencasting() override;
enum CursorMode {
Hidden = 1,
Embedded = 2,
Metadata = 4,
};
Q_ENUM(CursorMode);
ScreencastingStream *createOutputStream(KWayland::Client::Output *output, CursorMode mode);
ScreencastingStream *createWindowStream(KWayland::Client::PlasmaWindow *window, CursorMode mode);
ScreencastingStream *createWindowStream(const QString &uuid, CursorMode mode);
ScreencastingStream *createVirtualMonitorStream(const QString &name, const QSize &resolution, qreal dpr, CursorMode mode);
void setup(zkde_screencast_unstable_v1 *screencasting);
void destroy();
Q_SIGNALS:
void initialized();
void removed();
void sourcesChanged();
private:
QScopedPointer<ScreencastingPrivate> d;
};
......@@ -123,6 +123,41 @@ install (TARGETS krfb
${KDE_INSTALL_TARGETS_DEFAULT_ARGS}
)
#################################
kconfig_add_kcfg_files (krfbvm_SRCS
krfbconfig.kcfgc
)
ecm_qt_declare_logging_category(krfbvm_SRCS
HEADER krfbdebug.h
IDENTIFIER KRFB
CATEGORY_NAME krfb.krfb
DESCRIPTION "KRFB Application"
EXPORT KRFB
)
add_executable(krfb-virtualmonitor main-virtualmonitor.cpp ${krfbvm_SRCS}
rfbserver.cpp rfbclient.cpp rfbservermanager.cpp eventsmanager.cpp framebuffermanager.cpp sockethelpers.cpp)
target_link_libraries(krfb-virtualmonitor
krfbprivate
Qt5::Gui
Qt5::Network
KF5::ConfigGui
KF5::CoreAddons
KF5::I18n
KF5::Notifications
KF5::WindowSystem
)
install (TARGETS krfb-virtualmonitor
${KDE_INSTALL_TARGETS_DEFAULT_ARGS}
)
configure_file(org.kde.krfb.virtualmonitor.desktop.cmake ${CMAKE_CURRENT_BINARY_DIR}/org.kde.krfb.virtualmonitor.desktop @ONLY)
install (PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/org.kde.krfb.virtualmonitor.desktop
DESTINATION ${KDE_INSTALL_APPDIR}
)
########### install files ###############
install (PROGRAMS org.kde.krfb.desktop
......
/* This file is part of the KDE project
Copyright (C) 2021 Aleix Pol Gonzalez <aleixpol@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
*/
#include <QApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QDebug>
#include <QTimer>
#include <KNotification>
#include <KLocalizedString>
#include <KWindowSystem>
#include <KAboutData>
#include "sockethelpers.h"
#include "krfb_version.h"
#include "rfbserver.h"
#include <signal.h>
#include "rfbservermanager.h"
class VirtualMonitorRfbClient : public RfbClient
{
public:
explicit VirtualMonitorRfbClient(rfbClientPtr client, QObject *parent = nullptr)
: RfbClient(client, parent)
{}
};
class PendingVirtualMonitorRfbClient : public PendingRfbClient
{
public:
explicit PendingVirtualMonitorRfbClient(rfbClientPtr client, QObject *parent = nullptr)
: PendingRfbClient(client, parent)
{}
~PendingVirtualMonitorRfbClient() override {}
static QByteArray password;
protected:
void processNewClient() override {
qDebug() << "new client!";
const QString host = peerAddress(m_rfbClient->sock) + QLatin1Char(':') + QString::number(peerPort(m_rfbClient->sock));
KNotification::event(QStringLiteral("NewConnectionAutoAccepted"),
i18n("Creating a Virtual Monitor from1 %1", host));
}
bool checkPassword(const QByteArray & encryptedPassword) override {
bool b = vncAuthCheckPassword(password, encryptedPassword);
if (b) {
QTimer::singleShot(0, this, [this] {
accept(new VirtualMonitorRfbClient(m_rfbClient, parent()));
});
}
return b;
}
};
QByteArray PendingVirtualMonitorRfbClient::password;
class VirtualMonitorRfbServer : public RfbServer
{
public:
PendingRfbClient *newClient(rfbClientPtr client) {
qDebug() << "new client request";
return new PendingVirtualMonitorRfbClient(client, this);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
KLocalizedString::setApplicationDomain("krfb");
KAboutData aboutData(QStringLiteral("krfb-virtualmonitor"),
i18n("Remote Virtual Monitor"),
QStringLiteral(KRFB_VERSION_STRING),
i18n("Offer a Virtual Monitor that can be accessed remotely"),
KAboutLicense::GPL,
i18n("(c) 2009-2010, Collabora Ltd.\n"
"(c) 2007, Alessandro Praduroux\n"
"(c) 2001-2003, Tim Jansen\n"
"(c) 2001, Johannes E. Schindelin\n"
"(c) 2000-2001, Const Kaplinsky\n"
"(c) 2000, Tridia Corporation\n"
"(c) 1999, AT&T Laboratories Boston\n"));
aboutData.addAuthor(QStringLiteral("Aleix Pol i Gonzalez"), i18n("Virtual Monitor implementation"), QStringLiteral("aleixpol@kde.org"));
aboutData.addAuthor(i18n("George Kiagiadakis"), QString(), QStringLiteral("george.kiagiadakis@collabora.co.uk"));
aboutData.addAuthor(i18n("Alessandro Praduroux"), i18n("KDE4 porting"), QStringLiteral("pradu@pradu.it"));
aboutData.addAuthor(i18n("Tim Jansen"), i18n("Original author"), QStringLiteral("tim@tjansen.de"));
aboutData.addCredit(i18n("Johannes E. Schindelin"),
i18n("libvncserver"));
aboutData.addCredit(i18n("Const Kaplinsky"),
i18n("TightVNC encoder"));
aboutData.addCredit(i18n("Tridia Corporation"),
i18n("ZLib encoder"));
aboutData.addCredit(i18n("AT&T Laboratories Boston"),
i18n("original VNC encoders and "
"protocol design"));
KAboutData::setApplicationData(aboutData);
QCommandLineParser parser;
aboutData.setupCommandLine(&parser);
const QCommandLineOption resolutionOption({ QStringLiteral("resolution") }, i18n("Logical resolution of the new monitor"), i18n("resolution"));
parser.addOption(resolutionOption);
const QCommandLineOption nameOption({ QStringLiteral("name") }, i18n("Name of the monitor"), i18n("name"));
parser.addOption(nameOption);
const QCommandLineOption passwordOption({ QStringLiteral("password") }, i18n("Password for the client to connect to it"), i18n("password"));
parser.addOption(passwordOption);
const QCommandLineOption scaleOption({ QStringLiteral("scale") }, i18n("The device-pixel-ratio of the device, the scaling factor"), i18n("dpr"), QStringLiteral("1"));
parser.addOption(scaleOption);
const QCommandLineOption portOption({ QStringLiteral("port") }, i18n("The port we will be listening to"), i18n("number"), QStringLiteral("9999"));
parser.addOption(portOption);
parser.process(app);
aboutData.processCommandLine(&parser);
app.setQuitOnLastWindowClosed(false);
if (!KWindowSystem::isPlatformWayland()) {
qCritical() << "Virtual Monitors are only supported on Wayland";
return 1;
}
if (!parser.isSet(nameOption)) {
qCritical() << "error: please define --name";
return 2;
} else {
if (!parser.isSet(passwordOption)) {
qCritical() << "error: please define --password";
return 3;
}
if (!parser.isSet(resolutionOption)) {
qCritical() << "error: please define --resolution";
return 4;
}
}
if (!parser.isSet(portOption)) {
qCritical() << "error: please define --port";
return 5;
}
const QString res = parser.value(resolutionOption);
const auto resSplit = res.split(QLatin1Char('x'));
if (resSplit.size() != 2) {
qCritical() << "error: the resolution should be formatted as WIDTHxHEIGHT (e.g. --resolution 1920x1080)";
return 6;
}
if (parser.isSet(nameOption)) {
RfbServerManager::s_pluginArgs = {
{ QStringLiteral("name"), parser.value(nameOption) },
{ QStringLiteral("resolution"), QSize(resSplit[0].toInt(), resSplit[1].toInt()) },
{ QStringLiteral("scale"), parser.value(scaleOption).toDouble() },
};
}
VirtualMonitorRfbServer server;
server.setPasswordRequired(true);
server.setListeningPort(parser.value(portOption).toInt());
PendingVirtualMonitorRfbClient::password = parser.value(passwordOption).toUtf8();
sigset_t sigs;
sigemptyset(&sigs);
sigaddset(&sigs, SIGPIPE);
sigprocmask(SIG_BLOCK, &sigs, nullptr);
server.start();
return app.exec();
}
# KDE Config File
[Desktop Entry]
Type=Application
Exec=@CMAKE_INSTALL_PREFIX@/bin/krfb-virtualmonitor
Icon=krfb
Terminal=false
Name=KRFBs Virtual Monitor
Comment=Remote Virtual Monitor
NoDisplay=true
X-KDE-Wayland-Interfaces=zkde_screencast_unstable_v1
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment