|
| 1 | +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +#include "xdgactivation_p.h" |
| 6 | + |
| 7 | +#include <DSGApplication> |
| 8 | +#include <QGuiApplication> |
| 9 | +#include <QLoggingCategory> |
| 10 | +#include <QMetaObject> |
| 11 | +#include <QPointer> |
| 12 | +#include <QtWaylandClient/QWaylandClientExtension> |
| 13 | + |
| 14 | +#include "qwayland-xdg-activation-v1.h" |
| 15 | +#include <private/qwaylanddisplay_p.h> |
| 16 | +#include <private/qwaylandinputdevice_p.h> |
| 17 | +#include <private/qwaylandwindow_p.h> |
| 18 | + |
| 19 | +Q_LOGGING_CATEGORY(trayXdgActivation, "dde.tray.xdgactivation") |
| 20 | + |
| 21 | +namespace tray { |
| 22 | + |
| 23 | +class XdgActivationTokenV1 : public QObject, public QtWayland::xdg_activation_token_v1 |
| 24 | +{ |
| 25 | + Q_OBJECT |
| 26 | + |
| 27 | +public: |
| 28 | + ~XdgActivationTokenV1() override |
| 29 | + { |
| 30 | + destroy(); |
| 31 | + } |
| 32 | + |
| 33 | +Q_SIGNALS: |
| 34 | + void done(const QString &token); |
| 35 | + |
| 36 | +protected: |
| 37 | + void xdg_activation_token_v1_done(const QString &token) override |
| 38 | + { |
| 39 | + Q_EMIT done(token); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +namespace { |
| 44 | + |
| 45 | +class XdgActivationV1 : public QWaylandClientExtensionTemplate<XdgActivationV1>, |
| 46 | + public QtWayland::xdg_activation_v1 |
| 47 | +{ |
| 48 | +public: |
| 49 | + XdgActivationV1() |
| 50 | + : QWaylandClientExtensionTemplate<XdgActivationV1>(1) |
| 51 | + { |
| 52 | + initialize(); |
| 53 | + } |
| 54 | + |
| 55 | + ~XdgActivationV1() override |
| 56 | + { |
| 57 | + if (isInitialized()) |
| 58 | + destroy(); |
| 59 | + } |
| 60 | + |
| 61 | + XdgActivationTokenV1 *createTokenProvider(QWindow *window, const QString &appId) |
| 62 | + { |
| 63 | + auto *provider = new XdgActivationTokenV1; |
| 64 | + provider->init(get_activation_token()); |
| 65 | + |
| 66 | + if (window) { |
| 67 | + if (auto *waylandWindow = dynamic_cast<QtWaylandClient::QWaylandWindow *>(window->handle())) { |
| 68 | + if (auto *surface = waylandWindow->wlSurface()) |
| 69 | + provider->set_surface(surface); |
| 70 | + if (auto *inputDevice = waylandWindow->display()->lastInputDevice()) |
| 71 | + provider->set_serial(inputDevice->serial(), inputDevice->wl_seat()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (!appId.isEmpty()) |
| 76 | + provider->set_app_id(appId); |
| 77 | + |
| 78 | + provider->commit(); |
| 79 | + return provider; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +XdgActivationV1 *activationV1() |
| 84 | +{ |
| 85 | + static QPointer<XdgActivationV1> activation; |
| 86 | + if (activation) |
| 87 | + return activation; |
| 88 | + |
| 89 | + activation = new XdgActivationV1; |
| 90 | + activation->setParent(qApp); |
| 91 | + return activation; |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace |
| 95 | + |
| 96 | +// --------------------------------------------------------------------------- |
| 97 | +// XdgActivationPrivate |
| 98 | +// --------------------------------------------------------------------------- |
| 99 | + |
| 100 | +XdgActivationPrivate::XdgActivationPrivate(XdgActivation *qq) |
| 101 | + : DObjectPrivate(qq) |
| 102 | +{ |
| 103 | +} |
| 104 | + |
| 105 | +XdgActivationPrivate::~XdgActivationPrivate() = default; |
| 106 | + |
| 107 | +// --------------------------------------------------------------------------- |
| 108 | +// XdgActivation |
| 109 | +// --------------------------------------------------------------------------- |
| 110 | + |
| 111 | +XdgActivation::XdgActivation(QObject *parent) |
| 112 | + : QObject(parent) |
| 113 | + , DObject(*new XdgActivationPrivate(this)) |
| 114 | +{ |
| 115 | +} |
| 116 | + |
| 117 | +XdgActivation::~XdgActivation() = default; |
| 118 | + |
| 119 | +bool XdgActivation::isActive() const |
| 120 | +{ |
| 121 | + auto *activation = activationV1(); |
| 122 | + const bool active = activation && activation->isActive(); |
| 123 | + qCDebug(trayXdgActivation) << "isActive:" << active; |
| 124 | + return active; |
| 125 | +} |
| 126 | + |
| 127 | +void XdgActivation::requestToken(QWindow *window, const QString &appId) |
| 128 | +{ |
| 129 | + D_D(XdgActivation); |
| 130 | + |
| 131 | + if (d->provider) { |
| 132 | + qCWarning(trayXdgActivation) << "XDG activation token request already started"; |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + if (!isActive()) { |
| 137 | + qCDebug(trayXdgActivation) << "xdg_activation_v1 is not active; token request skipped"; |
| 138 | + Q_EMIT tokenReady({}); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + const QString effectiveAppId = appId.isEmpty() ? QString::fromUtf8(DTK_CORE_NAMESPACE::DSGApplication::id()) : appId; |
| 143 | + if (effectiveAppId.isEmpty()) |
| 144 | + qCWarning(trayXdgActivation) << "XDG activation request has empty app id"; |
| 145 | + |
| 146 | + auto effectiveWindow = window ? window : QGuiApplication::focusWindow(); |
| 147 | + if (!effectiveWindow) { |
| 148 | + qCWarning(trayXdgActivation) << "XDG activation request has no target window"; |
| 149 | + Q_EMIT tokenReady({}); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + auto *provider = activationV1()->createTokenProvider(effectiveWindow, effectiveAppId); |
| 154 | + provider->setParent(this); |
| 155 | + d->provider = provider; |
| 156 | + |
| 157 | + connect(provider, &XdgActivationTokenV1::done, this, [this, provider, effectiveAppId](const QString &token) { |
| 158 | + D_D(XdgActivation); |
| 159 | + d->provider = nullptr; |
| 160 | + |
| 161 | + if (token.isEmpty()) |
| 162 | + qCWarning(trayXdgActivation) << "XDG activation token missing for app:" << effectiveAppId; |
| 163 | + else |
| 164 | + qCDebug(trayXdgActivation) << "XDG activation token received for app:" << effectiveAppId; |
| 165 | + |
| 166 | + provider->deleteLater(); |
| 167 | + Q_EMIT tokenReady(token); |
| 168 | + }, Qt::SingleShotConnection); |
| 169 | +} |
| 170 | + |
| 171 | +} // namespace tray |
| 172 | + |
| 173 | +#include "xdgactivation.moc" |
0 commit comments