2021-10-19 00:25:14 +01:00
|
|
|
/**
|
|
|
|
* SPDX-FileCopyrightText: 2021 Aleix Pol i Gonzalez <aleixpol@kde.org>
|
virtualmonitor: implemented capabilities check
BUG: 485829
## Summary
Currently, the plugin just fails silently if the local device is missing the `krfb` package or if the remote device misses an `vnc://` protocol/scheme handler. You click the button and nothing happens.
One issue is, that the plugin is considered `virtualmonitor.available` in the `DeviceDelegate.qml`, even if the check for `krfb-virtualmonitor` fails and no dbus-path is provided. I investigated the behavior a bit, but ignored it in the end as this MR benefits from being shown for device constellations that _could_ provide this feature.
A warning is shown with brief instructions, how to get the plugin working correctly.
- Check if krfb-virtualmonitor is available locally
- Check default scheme handler for vnc:// on device (Linux)
- Show warnings / reasons, if no connection could be established
## Test Plan
Regarding if the devices have mentioned packages installed, we should see different behaviors.
If the remote device has no VNC client, it can not connect to out server. _A warning should be shown._
If the local device hasn't the `krfb-virtualmonitor` available, the remote device couldn't connect. _A warning should be shown._
If both problems are present, _both warnings should be shown._
If none of these are present, no warning should be shown and we should try to establish a connection.
The connection attempts failed? _A warning should be shown._
2024-05-31 00:13:54 +01:00
|
|
|
* SPDX-FileCopyrightText: 2024 Fabian Arndt <fabian.arndt@root-core.net>
|
2021-10-19 00:25:14 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "virtualmonitorplugin.h"
|
|
|
|
|
|
|
|
#include <KPluginFactory>
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "plugin_virtualmonitor_debug.h"
|
2021-10-19 00:25:14 +01:00
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QScreen>
|
2023-04-16 08:57:30 +01:00
|
|
|
#include <QStandardPaths>
|
2021-10-19 00:25:14 +01:00
|
|
|
|
virtualmonitor: implemented capabilities check
BUG: 485829
## Summary
Currently, the plugin just fails silently if the local device is missing the `krfb` package or if the remote device misses an `vnc://` protocol/scheme handler. You click the button and nothing happens.
One issue is, that the plugin is considered `virtualmonitor.available` in the `DeviceDelegate.qml`, even if the check for `krfb-virtualmonitor` fails and no dbus-path is provided. I investigated the behavior a bit, but ignored it in the end as this MR benefits from being shown for device constellations that _could_ provide this feature.
A warning is shown with brief instructions, how to get the plugin working correctly.
- Check if krfb-virtualmonitor is available locally
- Check default scheme handler for vnc:// on device (Linux)
- Show warnings / reasons, if no connection could be established
## Test Plan
Regarding if the devices have mentioned packages installed, we should see different behaviors.
If the remote device has no VNC client, it can not connect to out server. _A warning should be shown._
If the local device hasn't the `krfb-virtualmonitor` available, the remote device couldn't connect. _A warning should be shown._
If both problems are present, _both warnings should be shown._
If none of these are present, no warning should be shown and we should try to establish a connection.
The connection attempts failed? _A warning should be shown._
2024-05-31 00:13:54 +01:00
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
#include <QSettings>
|
|
|
|
#else
|
|
|
|
#include <KApplicationTrader>
|
|
|
|
#endif
|
|
|
|
|
2021-10-19 00:25:14 +01:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(VirtualMonitorPlugin, "kdeconnect_virtualmonitor.json")
|
|
|
|
#define QS QLatin1String
|
2024-04-28 15:14:26 +01:00
|
|
|
static const int DEFAULT_PORT = 5901;
|
2021-10-19 00:25:14 +01:00
|
|
|
|
|
|
|
VirtualMonitorPlugin::~VirtualMonitorPlugin()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualMonitorPlugin::stop()
|
|
|
|
{
|
|
|
|
if (!m_process)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_process->terminate();
|
|
|
|
if (!m_process->waitForFinished()) {
|
|
|
|
m_process->kill();
|
|
|
|
m_process->waitForFinished();
|
|
|
|
}
|
|
|
|
delete m_process;
|
|
|
|
m_process = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualMonitorPlugin::connected()
|
|
|
|
{
|
virtualmonitor: implemented capabilities check
BUG: 485829
## Summary
Currently, the plugin just fails silently if the local device is missing the `krfb` package or if the remote device misses an `vnc://` protocol/scheme handler. You click the button and nothing happens.
One issue is, that the plugin is considered `virtualmonitor.available` in the `DeviceDelegate.qml`, even if the check for `krfb-virtualmonitor` fails and no dbus-path is provided. I investigated the behavior a bit, but ignored it in the end as this MR benefits from being shown for device constellations that _could_ provide this feature.
A warning is shown with brief instructions, how to get the plugin working correctly.
- Check if krfb-virtualmonitor is available locally
- Check default scheme handler for vnc:// on device (Linux)
- Show warnings / reasons, if no connection could be established
## Test Plan
Regarding if the devices have mentioned packages installed, we should see different behaviors.
If the remote device has no VNC client, it can not connect to out server. _A warning should be shown._
If the local device hasn't the `krfb-virtualmonitor` available, the remote device couldn't connect. _A warning should be shown._
If both problems are present, _both warnings should be shown._
If none of these are present, no warning should be shown and we should try to establish a connection.
The connection attempts failed? _A warning should be shown._
2024-05-31 00:13:54 +01:00
|
|
|
// Get local capabilities
|
|
|
|
// We test again, since the user may have installed the necessary packages in the meantime
|
|
|
|
m_capabilitiesLocal.vncClient = checkVncClient();
|
|
|
|
m_capabilitiesLocal.virtualMonitor = !QStandardPaths::findExecutable(QS("krfb-virtualmonitor")).isEmpty();
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_VIRTUALMONITOR) << "Local device supports VNC:" << m_capabilitiesLocal.vncClient;
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_VIRTUALMONITOR) << "Local device supports Virtual Monitor:" << m_capabilitiesLocal.virtualMonitor;
|
|
|
|
|
2021-10-19 00:25:14 +01:00
|
|
|
auto screen = QGuiApplication::primaryScreen();
|
|
|
|
auto resolution = screen->size();
|
|
|
|
QString resolutionString = QString::number(resolution.width()) + QLatin1Char('x') + QString::number(resolution.height());
|
2022-09-10 22:23:52 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_VIRTUALMONITOR,
|
|
|
|
{
|
|
|
|
{QS("resolutions"),
|
|
|
|
QJsonArray{QJsonObject{
|
|
|
|
{QS("resolution"), resolutionString},
|
|
|
|
{QS("scale"), screen->devicePixelRatio()},
|
|
|
|
}}},
|
virtualmonitor: implemented capabilities check
BUG: 485829
## Summary
Currently, the plugin just fails silently if the local device is missing the `krfb` package or if the remote device misses an `vnc://` protocol/scheme handler. You click the button and nothing happens.
One issue is, that the plugin is considered `virtualmonitor.available` in the `DeviceDelegate.qml`, even if the check for `krfb-virtualmonitor` fails and no dbus-path is provided. I investigated the behavior a bit, but ignored it in the end as this MR benefits from being shown for device constellations that _could_ provide this feature.
A warning is shown with brief instructions, how to get the plugin working correctly.
- Check if krfb-virtualmonitor is available locally
- Check default scheme handler for vnc:// on device (Linux)
- Show warnings / reasons, if no connection could be established
## Test Plan
Regarding if the devices have mentioned packages installed, we should see different behaviors.
If the remote device has no VNC client, it can not connect to out server. _A warning should be shown._
If the local device hasn't the `krfb-virtualmonitor` available, the remote device couldn't connect. _A warning should be shown._
If both problems are present, _both warnings should be shown._
If none of these are present, no warning should be shown and we should try to establish a connection.
The connection attempts failed? _A warning should be shown._
2024-05-31 00:13:54 +01:00
|
|
|
{QS("supports_vnc"), m_capabilitiesLocal.vncClient},
|
|
|
|
{QS("supports_virt_mon"), m_capabilitiesLocal.virtualMonitor},
|
2022-09-10 22:23:52 +01:00
|
|
|
});
|
2021-10-19 00:25:14 +01:00
|
|
|
sendPacket(np);
|
|
|
|
}
|
|
|
|
|
2023-07-31 08:25:45 +01:00
|
|
|
void VirtualMonitorPlugin::receivePacket(const NetworkPacket &received)
|
2021-10-19 00:25:14 +01:00
|
|
|
{
|
2024-04-28 15:14:26 +01:00
|
|
|
if (received.type() == PACKET_TYPE_VIRTUALMONITOR_REQUEST) {
|
|
|
|
// At least a password is necessary, we have defaults for all other parameters
|
|
|
|
if (!received.has(QS("password"))) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_VIRTUALMONITOR) << "Request invalid, missing password";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to get the IP address of the paired device
|
|
|
|
QHostAddress addr = device()->getLocalIpAddress();
|
|
|
|
if (addr == QHostAddress::Null) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_VIRTUALMONITOR) << "Device doesn't have a LanDeviceLink, unable to get IP address";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl url;
|
|
|
|
url.setScheme(received.get<QString>(QS("protocol"), QS("vnc")));
|
|
|
|
url.setUserName(received.get<QString>(QS("username"), QS("user")));
|
|
|
|
url.setPassword(received.get<QString>(QS("password")));
|
|
|
|
url.setPort(received.get<int>(QS("port"), DEFAULT_PORT));
|
|
|
|
url.setHost(addr.toString());
|
|
|
|
|
|
|
|
qCInfo(KDECONNECT_PLUGIN_VIRTUALMONITOR) << "Received request, try connecting to" << url.toDisplayString();
|
|
|
|
|
2021-10-19 00:25:14 +01:00
|
|
|
if (!QDesktopServices::openUrl(url)) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_VIRTUALMONITOR) << "Failed to open" << url.toDisplayString();
|
2022-09-10 22:23:52 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_VIRTUALMONITOR, {{QS("failed"), 0}});
|
2021-10-19 00:25:14 +01:00
|
|
|
sendPacket(np);
|
|
|
|
}
|
|
|
|
} else if (received.type() == PACKET_TYPE_VIRTUALMONITOR) {
|
|
|
|
if (received.has(QS("resolutions"))) {
|
|
|
|
m_remoteResolution = received.get<QJsonArray>(QS("resolutions")).first().toObject();
|
|
|
|
}
|
|
|
|
if (received.has(QS("failed"))) {
|
|
|
|
stop();
|
|
|
|
}
|
virtualmonitor: implemented capabilities check
BUG: 485829
## Summary
Currently, the plugin just fails silently if the local device is missing the `krfb` package or if the remote device misses an `vnc://` protocol/scheme handler. You click the button and nothing happens.
One issue is, that the plugin is considered `virtualmonitor.available` in the `DeviceDelegate.qml`, even if the check for `krfb-virtualmonitor` fails and no dbus-path is provided. I investigated the behavior a bit, but ignored it in the end as this MR benefits from being shown for device constellations that _could_ provide this feature.
A warning is shown with brief instructions, how to get the plugin working correctly.
- Check if krfb-virtualmonitor is available locally
- Check default scheme handler for vnc:// on device (Linux)
- Show warnings / reasons, if no connection could be established
## Test Plan
Regarding if the devices have mentioned packages installed, we should see different behaviors.
If the remote device has no VNC client, it can not connect to out server. _A warning should be shown._
If the local device hasn't the `krfb-virtualmonitor` available, the remote device couldn't connect. _A warning should be shown._
If both problems are present, _both warnings should be shown._
If none of these are present, no warning should be shown and we should try to establish a connection.
The connection attempts failed? _A warning should be shown._
2024-05-31 00:13:54 +01:00
|
|
|
|
|
|
|
// Get device's capabilities
|
|
|
|
m_capabilitiesRemote.vncClient = received.get<bool>(QS("supports_vnc"), false);
|
|
|
|
m_capabilitiesRemote.virtualMonitor = received.get<bool>(QS("supports_virt_mon"), false);
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_VIRTUALMONITOR) << "Remote device supports VNC:" << m_capabilitiesRemote.vncClient;
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_VIRTUALMONITOR) << "Remote device supports Virtual Monitor:" << m_capabilitiesRemote.virtualMonitor;
|
2021-10-19 00:25:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString VirtualMonitorPlugin::dbusPath() const
|
|
|
|
{
|
2023-08-12 12:26:05 +01:00
|
|
|
return QLatin1String("/modules/kdeconnect/devices/%1/virtualmonitor").arg(device()->id());
|
2021-10-19 00:25:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VirtualMonitorPlugin::requestVirtualMonitor()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
if (m_remoteResolution.isEmpty()) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_VIRTUALMONITOR) << "Cannot start a request without a resolution";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_VIRTUALMONITOR) << "Requesting virtual display " << device()->name();
|
|
|
|
|
|
|
|
QUuid uuid = QUuid::createUuid();
|
2024-04-28 15:14:26 +01:00
|
|
|
static int s_port = DEFAULT_PORT;
|
2021-10-19 00:25:14 +01:00
|
|
|
const QString port = QString::number(s_port++);
|
|
|
|
|
|
|
|
m_process = new QProcess(this);
|
|
|
|
m_process->setProgram(QS("krfb-virtualmonitor"));
|
|
|
|
const double scale = m_remoteResolution.value(QLatin1String("scale")).toDouble();
|
2022-09-10 22:23:52 +01:00
|
|
|
m_process->setArguments({QS("--name"),
|
|
|
|
device()->name(),
|
|
|
|
QS("--resolution"),
|
|
|
|
m_remoteResolution.value(QLatin1String("resolution")).toString(),
|
|
|
|
QS("--scale"),
|
|
|
|
QString::number(scale),
|
|
|
|
QS("--password"),
|
|
|
|
uuid.toString(),
|
|
|
|
QS("--port"),
|
|
|
|
port});
|
2023-07-21 10:49:04 +01:00
|
|
|
connect(m_process, &QProcess::finished, this, [this](int exitCode, QProcess::ExitStatus exitStatus) {
|
2023-01-20 12:28:28 +00:00
|
|
|
qCWarning(KDECONNECT_PLUGIN_VIRTUALMONITOR) << "virtual display finished with" << device()->name() << m_process->readAllStandardError();
|
|
|
|
|
2021-10-19 00:25:14 +01:00
|
|
|
if (m_retries < 5 && (exitCode == 1 || exitStatus == QProcess::CrashExit)) {
|
|
|
|
m_retries++;
|
|
|
|
requestVirtualMonitor();
|
|
|
|
} else {
|
|
|
|
m_process->deleteLater();
|
2022-10-16 23:33:01 +01:00
|
|
|
m_process = nullptr;
|
2021-10-19 00:25:14 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
m_process->start();
|
|
|
|
|
|
|
|
if (!m_process->waitForStarted()) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_VIRTUALMONITOR) << "Failed to start krfb-virtualmonitor" << m_process->error() << m_process->errorString();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-04-28 15:14:26 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_VIRTUALMONITOR_REQUEST);
|
|
|
|
np.set(QS("protocol"), QS("vnc"));
|
|
|
|
np.set(QS("username"), QS("user"));
|
|
|
|
np.set(QS("password"), uuid.toString());
|
|
|
|
np.set(QS("port"), port);
|
2021-10-19 00:25:14 +01:00
|
|
|
sendPacket(np);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
virtualmonitor: implemented capabilities check
BUG: 485829
## Summary
Currently, the plugin just fails silently if the local device is missing the `krfb` package or if the remote device misses an `vnc://` protocol/scheme handler. You click the button and nothing happens.
One issue is, that the plugin is considered `virtualmonitor.available` in the `DeviceDelegate.qml`, even if the check for `krfb-virtualmonitor` fails and no dbus-path is provided. I investigated the behavior a bit, but ignored it in the end as this MR benefits from being shown for device constellations that _could_ provide this feature.
A warning is shown with brief instructions, how to get the plugin working correctly.
- Check if krfb-virtualmonitor is available locally
- Check default scheme handler for vnc:// on device (Linux)
- Show warnings / reasons, if no connection could be established
## Test Plan
Regarding if the devices have mentioned packages installed, we should see different behaviors.
If the remote device has no VNC client, it can not connect to out server. _A warning should be shown._
If the local device hasn't the `krfb-virtualmonitor` available, the remote device couldn't connect. _A warning should be shown._
If both problems are present, _both warnings should be shown._
If none of these are present, no warning should be shown and we should try to establish a connection.
The connection attempts failed? _A warning should be shown._
2024-05-31 00:13:54 +01:00
|
|
|
bool VirtualMonitorPlugin::checkVncClient() const
|
|
|
|
{
|
|
|
|
#if defined(Q_OS_MAC)
|
|
|
|
// TODO: macOS has some VNC client preinstalled.. does it work for us?
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
// krdc is default on KDE
|
|
|
|
if (!QStandardPaths::findExecutable(QS("krdc")).isEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check if another client is available
|
|
|
|
return checkDefaultSchemeHandler(QS("vnc"));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VirtualMonitorPlugin::checkDefaultSchemeHandler(const QString &scheme) const
|
|
|
|
{
|
|
|
|
// TODO: macOS built-in tool: defaults read com.apple.LaunchServices/com.apple.launchservices.secure
|
|
|
|
// A function to use such a program was implemented and later removed in MR !670 (Commit e72f688ae1fad8846c006a3f87d57e507f6dbcb6)
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
// Scheme handlers are stored in the registry on Windows
|
|
|
|
// https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa767914(v=vs.85)
|
|
|
|
|
|
|
|
// Check if url handler is present
|
|
|
|
QSettings handler(QS("HKEY_CLASSES_ROOT\\") + scheme, QSettings::Registry64Format);
|
|
|
|
if (handler.value("URL Protocol").isNull())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check if a command is actually present
|
|
|
|
QSettings command(QS("HKEY_CLASSES_ROOT\\") + scheme + QS("\\shell\\open\\command"), QSettings::Registry64Format);
|
|
|
|
return !command.value("Default").isNull();
|
|
|
|
#else
|
|
|
|
KService::Ptr service = KApplicationTrader::preferredService(QS("x-scheme-handler/") + scheme);
|
|
|
|
const QString defaultApp = service ? service->desktopEntryName() : QString();
|
|
|
|
return !defaultApp.isEmpty();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-07-26 09:15:11 +01:00
|
|
|
#include "moc_virtualmonitorplugin.cpp"
|
2021-10-19 00:25:14 +01:00
|
|
|
#include "virtualmonitorplugin.moc"
|