// SPDX-License-Identifier: MIT #include "taskbargeometry.h" #include #include #include #include namespace { constexpr auto scriptName = "se.ajpanton.task-group-shortcuts.geometry"; constexpr auto objectPath = "/TaskbarGeometry"; } TaskbarGeometry::TaskbarGeometry(QObject *parent) : QObject(parent) { auto bus = QDBusConnection::sessionBus(); if (!bus.registerObject(QString::fromLatin1(objectPath), this, QDBusConnection::ExportScriptableContents)) { qFatal("Cannot register the taskbar geometry interface"); } QDBusInterface scripting(QStringLiteral("org.kde.KWin"), QStringLiteral("/Scripting"), QStringLiteral("org.kde.kwin.Scripting")); // Replace any copy left behind by an earlier process that was terminated. scripting.call(QStringLiteral("unloadScript"), QString::fromLatin1(scriptName)); const QDBusReply loaded = scripting.call(QStringLiteral("loadDeclarativeScript"), QStringLiteral(GEOMETRY_SCRIPT_PATH), QString::fromLatin1(scriptName)); if (!loaded.isValid() || loaded.value() < 0) { qWarning() << "Cannot load taskbar geometry script:" << loaded.error().message(); return; } m_loaded = true; QDBusInterface script(QStringLiteral("org.kde.KWin"), QStringLiteral("/Scripting/Script%1").arg(loaded.value()), QStringLiteral("org.kde.kwin.Script")); const QDBusReply started = script.call(QStringLiteral("run")); if (!started.isValid()) { qWarning() << "Cannot start taskbar geometry script:" << started.error().message(); m_loaded = false; } } TaskbarGeometry::~TaskbarGeometry() { QDBusInterface scripting(QStringLiteral("org.kde.KWin"), QStringLiteral("/Scripting"), QStringLiteral("org.kde.kwin.Scripting")); scripting.call(QStringLiteral("unloadScript"), QString::fromLatin1(scriptName)); QDBusConnection::sessionBus().unregisterObject(QString::fromLatin1(objectPath)); } void TaskbarGeometry::request(const QString &uuid) { ++m_serial; m_uuid = uuid; if (m_ready) { Q_EMIT lookup(m_serial, uuid); } else if (!m_loaded) { Q_EMIT received({}); } } void TaskbarGeometry::cancel() { ++m_serial; m_uuid.clear(); } void TaskbarGeometry::bridgeReady() { m_ready = true; if (!m_uuid.isEmpty()) { Q_EMIT lookup(m_serial, m_uuid); } } void TaskbarGeometry::complete(int serial, double x, double y, double width, double height) { if (serial == m_serial && !m_uuid.isEmpty()) { Q_EMIT received(QRectF(x, y, width, height)); } }