Commit c726bc90 authored by David Redondo's avatar David Redondo 🏎 Committed by Aleix Pol Gonzalez
Browse files

kcms/mouse: Use GetAll to fetch properties on Wayland

It allows to use more complex type which do not play nice
in combination of DBus and the Qt property system.
Also only does one call instead of multiple.
parent 56b9673b
......@@ -99,7 +99,7 @@ bool KWinWaylandBackend::applyConfig()
bool KWinWaylandBackend::getConfig()
{
return std::all_of(m_devices.constBegin(), m_devices.constEnd(), [](QObject *t) {
return static_cast<KWinWaylandDevice *>(t)->getConfig();
return static_cast<KWinWaylandDevice *>(t)->init();
});
}
......@@ -139,7 +139,7 @@ void KWinWaylandBackend::onDeviceAdded(QString sysName)
}
KWinWaylandDevice *dev = new KWinWaylandDevice(sysName);
if (!dev->init() || !dev->getConfig()) {
if (!dev->init()) {
Q_EMIT deviceAdded(false);
return;
}
......
......@@ -6,8 +6,10 @@
#include "kwin_wl_device.h"
#include <QDBusConnection>
#include <QDBusError>
#include <QDBusInterface>
#include <QDBusMessage>
#include <QDBusReply>
#include <QVector>
#include "logging.h"
......@@ -59,30 +61,44 @@ Qt::MouseButtons valueLoaderPart(QVariant const &reply)
}
KWinWaylandDevice::KWinWaylandDevice(const QString &dbusName)
: m_dbusName(dbusName)
{
m_iface = new QDBusInterface(QStringLiteral("org.kde.KWin"),
QStringLiteral("/org/kde/KWin/InputDevice/") + dbusName,
QStringLiteral("org.kde.KWin.InputDevice"),
QDBusConnection::sessionBus(),
this);
}
KWinWaylandDevice::~KWinWaylandDevice()
{
delete m_iface;
}
bool KWinWaylandDevice::init()
{
// need to do it here in order to populate combobox and handle events
return valueLoader(m_name) && valueLoader(m_sysName);
}
bool KWinWaylandDevice::getConfig()
{
bool success = true;
auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.KWin"),
QStringLiteral("/org/kde/KWin/InputDevice/") + m_dbusName,
QStringLiteral("org.freedesktop.DBus.Properties"),
QStringLiteral("GetAll"));
message << QStringLiteral("org.kde.KWin.InputDevice");
QDBusReply<QVariantMap> reply = QDBusConnection::sessionBus().call(message);
if (!reply.isValid()) {
return false;
}
auto valueLoader = [properties = reply.value(), this](auto &prop) {
if (QVariant variant = properties.value(prop.dbus); variant.isValid()) {
prop.avail = true;
prop.old = valueLoaderPart<typename std::remove_reference_t<decltype(prop)>::value_type>(variant);
prop.val = prop.old;
return true;
}
qCCritical(KCM_MOUSE) << "Device" << m_dbusName << "does not have property on d-bus read of" << prop.dbus;
prop.avail = false;
return false;
};
// general
success &= valueLoader(m_name);
success &= valueLoader(m_sysName);
success &= valueLoader(m_supportsDisableEvents);
success &= valueLoader(m_enabled);
// advanced
......@@ -169,29 +185,15 @@ QString KWinWaylandDevice::valueWriter(const Prop<T> &prop)
if (!prop.changed()) {
return QString();
}
m_iface->setProperty(prop.dbus, prop.val);
QDBusError error = m_iface->lastError();
if (error.isValid()) {
qCCritical(KCM_MOUSE) << error.message();
return error.message();
auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.KWin"),
QStringLiteral("/org/kde/KWin/InputDevice/") + m_dbusName,
QStringLiteral("org.freedesktop.DBus.Properties"),
QStringLiteral("Set"));
message << QStringLiteral("org.kde.KWin.InputDevice") << prop.dbus << QVariant::fromValue(QDBusVariant(prop.val));
QDBusReply<void> reply = QDBusConnection::sessionBus().call(message);
if (reply.error().isValid()) {
qCCritical(KCM_MOUSE) << reply.error().message();
return reply.error().message();
}
return QString();
}
template<typename T>
bool KWinWaylandDevice::valueLoader(Prop<T> &prop)
{
QVariant reply = m_iface->property(prop.dbus);
if (!reply.isValid()) {
qCCritical(KCM_MOUSE) << "Error on d-bus read of" << prop.dbus;
prop.avail = false;
return false;
}
prop.avail = true;
T replyValue = valueLoaderPart<T>(reply);
prop.old = replyValue;
prop.val = replyValue;
return true;
}
......@@ -62,7 +62,6 @@ public:
bool init();
bool getConfig();
bool getDefaultConfig();
bool applyConfig();
bool isChangedConfig() const;
......@@ -219,7 +218,8 @@ Q_SIGNALS:
private:
template<typename T>
struct Prop {
explicit Prop(const QByteArray &dbusName)
using value_type = T;
explicit Prop(const char *dbusName)
: dbus(dbusName)
{
}
......@@ -241,7 +241,7 @@ private:
return avail && (old != val);
}
QByteArray dbus;
QLatin1String dbus;
bool avail;
T old;
T val;
......@@ -293,7 +293,7 @@ private:
Prop<bool> m_naturalScroll = Prop<bool>("naturalScroll");
Prop<qreal> m_scrollFactor = Prop<qreal>("scrollFactor");
QDBusInterface *m_iface;
QString m_dbusName;
};
#endif // KWINWAYLANDDEVICE_H
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