|
| 1 | +// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +#include "xdgactivationclient.h" |
| 6 | + |
| 7 | +#include <QGuiApplication> |
| 8 | +#include <QLoggingCategory> |
| 9 | +#include <QPointer> |
| 10 | +#include <QWindow> |
| 11 | +#include <QtWaylandClient/QWaylandClientExtension> |
| 12 | + |
| 13 | +#include "qwayland-xdg-activation-v1.h" |
| 14 | +#include <private/qwaylanddisplay_p.h> |
| 15 | +#include <private/qwaylandinputdevice_p.h> |
| 16 | +#include <private/qwaylandwindow_p.h> |
| 17 | + |
| 18 | +Q_LOGGING_CATEGORY(trayXdgActivation, "dde.tray.xdgactivation") |
| 19 | + |
| 20 | +namespace tray { |
| 21 | + |
| 22 | +class XdgActivationTokenV1 : public QObject, public QtWayland::xdg_activation_token_v1 |
| 23 | +{ |
| 24 | + Q_OBJECT |
| 25 | +public: |
| 26 | + ~XdgActivationTokenV1() override { destroy(); } |
| 27 | + |
| 28 | +Q_SIGNALS: |
| 29 | + void done(const QString &token); |
| 30 | + |
| 31 | +protected: |
| 32 | + void xdg_activation_token_v1_done(const QString &token) override |
| 33 | + { |
| 34 | + Q_EMIT done(token); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +class XdgActivationV1 : public QWaylandClientExtensionTemplate<XdgActivationV1>, |
| 39 | + public QtWayland::xdg_activation_v1 |
| 40 | +{ |
| 41 | +public: |
| 42 | + XdgActivationV1() |
| 43 | + : QWaylandClientExtensionTemplate<XdgActivationV1>(1) |
| 44 | + { |
| 45 | + initialize(); |
| 46 | + } |
| 47 | + |
| 48 | + ~XdgActivationV1() override |
| 49 | + { |
| 50 | + if (isInitialized()) |
| 51 | + destroy(); |
| 52 | + } |
| 53 | + |
| 54 | + XdgActivationTokenV1 *createTokenProvider(QWindow *window, const QString &appId) |
| 55 | + { |
| 56 | + auto *provider = new XdgActivationTokenV1; |
| 57 | + provider->init(get_activation_token()); |
| 58 | + |
| 59 | + if (window) { |
| 60 | + if (auto *waylandWindow = dynamic_cast<QtWaylandClient::QWaylandWindow *>(window->handle())) { |
| 61 | + if (auto *surface = waylandWindow->wlSurface()) |
| 62 | + provider->set_surface(surface); |
| 63 | + if (auto *inputDevice = waylandWindow->display()->lastInputDevice()) |
| 64 | + provider->set_serial(inputDevice->serial(), inputDevice->wl_seat()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (!appId.isEmpty()) |
| 69 | + provider->set_app_id(appId); |
| 70 | + |
| 71 | + provider->commit(); |
| 72 | + return provider; |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +XdgActivationClient *XdgActivationClient::instance() |
| 77 | +{ |
| 78 | + static XdgActivationClient *s_instance = nullptr; |
| 79 | + if (!s_instance) { |
| 80 | + s_instance = new XdgActivationClient(qApp); |
| 81 | + } |
| 82 | + return s_instance; |
| 83 | +} |
| 84 | + |
| 85 | +XdgActivationClient::XdgActivationClient(QObject *parent) |
| 86 | + : QObject(parent) |
| 87 | + , m_activation(new XdgActivationV1) |
| 88 | +{ |
| 89 | + m_activation->setParent(this); |
| 90 | +} |
| 91 | + |
| 92 | +XdgActivationClient::~XdgActivationClient() = default; |
| 93 | + |
| 94 | +bool XdgActivationClient::isActive() const |
| 95 | +{ |
| 96 | + return m_activation && m_activation->isActive(); |
| 97 | +} |
| 98 | + |
| 99 | +void XdgActivationClient::requestToken(QWindow *window, const QString &appId, TokenCallback callback) |
| 100 | +{ |
| 101 | + if (m_pendingProvider) { |
| 102 | + qCWarning(trayXdgActivation) << "Token request already in progress"; |
| 103 | + if (callback) callback(QString()); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + if (!isActive()) { |
| 108 | + qCDebug(trayXdgActivation) << "xdg_activation_v1 not active"; |
| 109 | + if (callback) callback(QString()); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + auto effectiveWindow = window ? window : QGuiApplication::focusWindow(); |
| 114 | + if (!effectiveWindow) { |
| 115 | + qCWarning(trayXdgActivation) << "No target window for activation token"; |
| 116 | + if (callback) callback(QString()); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + auto *provider = m_activation->createTokenProvider(effectiveWindow, appId); |
| 121 | + m_pendingProvider = provider; |
| 122 | + |
| 123 | + connect(provider, &XdgActivationTokenV1::done, this, [this, callback](const QString &token) { |
| 124 | + m_pendingProvider = nullptr; |
| 125 | + |
| 126 | + if (token.isEmpty()) |
| 127 | + qCWarning(trayXdgActivation) << "Empty activation token received"; |
| 128 | + else |
| 129 | + qCDebug(trayXdgActivation) << "Activation token received"; |
| 130 | + |
| 131 | + if (callback) callback(token); |
| 132 | + sender()->deleteLater(); |
| 133 | + }, Qt::SingleShotConnection); |
| 134 | +} |
| 135 | + |
| 136 | +} // namespace tray |
| 137 | + |
| 138 | +#include "xdgactivationclient.moc" |
0 commit comments