|
| 1 | +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +#include "xdgactivation.h" |
| 6 | + |
| 7 | +#include <QEventLoop> |
| 8 | +#include <QTimer> |
| 9 | +#include <QWindow> |
| 10 | +#include <QLoggingCategory> |
| 11 | + |
| 12 | +#include <private/qwaylandwindow_p.h> |
| 13 | +#include <private/qwaylanddisplay_p.h> |
| 14 | +#include <private/qwaylandinputdevice_p.h> |
| 15 | + |
| 16 | +Q_DECLARE_LOGGING_CATEGORY(logDdeIntegration) |
| 17 | + |
| 18 | +namespace DDEIntegration { |
| 19 | + |
| 20 | +// --------------------------------------------------------------------------- |
| 21 | +// XdgActivationTokenV1 |
| 22 | +// --------------------------------------------------------------------------- |
| 23 | + |
| 24 | +XdgActivationTokenV1::~XdgActivationTokenV1() |
| 25 | +{ |
| 26 | + destroy(); |
| 27 | +} |
| 28 | + |
| 29 | +void XdgActivationTokenV1::xdg_activation_token_v1_done(const QString &token) |
| 30 | +{ |
| 31 | + Q_EMIT done(token); |
| 32 | +} |
| 33 | + |
| 34 | +// --------------------------------------------------------------------------- |
| 35 | +// XdgActivationV1 |
| 36 | +// --------------------------------------------------------------------------- |
| 37 | + |
| 38 | +XdgActivationV1 *XdgActivationV1::instance() |
| 39 | +{ |
| 40 | + static XdgActivationV1 s_instance; |
| 41 | + return &s_instance; |
| 42 | +} |
| 43 | + |
| 44 | +XdgActivationV1::XdgActivationV1() |
| 45 | + : QWaylandClientExtensionTemplate<XdgActivationV1>(1) |
| 46 | +{ |
| 47 | +} |
| 48 | + |
| 49 | +XdgActivationV1::~XdgActivationV1() |
| 50 | +{ |
| 51 | + if (isInitialized()) |
| 52 | + destroy(); |
| 53 | +} |
| 54 | + |
| 55 | +QString XdgActivationV1::requestToken(QWindow *window, const QString &appId) |
| 56 | +{ |
| 57 | + if (!isActive()) { |
| 58 | + qCWarning(logDdeIntegration) << "xdg_activation_v1 is not active, cannot request token"; |
| 59 | + return {}; |
| 60 | + } |
| 61 | + |
| 62 | + auto *provider = new XdgActivationTokenV1; |
| 63 | + provider->init(get_activation_token()); |
| 64 | + |
| 65 | + // Attach the surface and input serial of the requesting window so the |
| 66 | + // compositor can verify focus and apply focus-stealing-prevention rules. |
| 67 | + if (window) { |
| 68 | + if (auto *waylandWindow = |
| 69 | + dynamic_cast<QtWaylandClient::QWaylandWindow *>(window->handle())) { |
| 70 | + if (auto *surface = waylandWindow->wlSurface()) { |
| 71 | + provider->set_surface(surface); |
| 72 | + } |
| 73 | + // set_serial tells the compositor which input event triggered this |
| 74 | + // launch request; without it the compositor may deny focus for the |
| 75 | + // new window (focus-stealing prevention). |
| 76 | + if (auto *inputDevice = waylandWindow->display()->lastInputDevice()) { |
| 77 | + provider->set_serial(inputDevice->serial(), inputDevice->wl_seat()); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (!appId.isEmpty()) |
| 83 | + provider->set_app_id(appId); |
| 84 | + |
| 85 | + provider->commit(); |
| 86 | + |
| 87 | + // Block until the compositor delivers the token or the timeout fires. |
| 88 | + QString token; |
| 89 | + QEventLoop loop; |
| 90 | + QTimer timeout; |
| 91 | + timeout.setSingleShot(true); |
| 92 | + timeout.setInterval(2000); |
| 93 | + |
| 94 | + connect(provider, &XdgActivationTokenV1::done, &loop, |
| 95 | + [&token, &loop](const QString &t) { |
| 96 | + token = t; |
| 97 | + loop.quit(); |
| 98 | + }); |
| 99 | + connect(&timeout, &QTimer::timeout, &loop, &QEventLoop::quit); |
| 100 | + |
| 101 | + timeout.start(); |
| 102 | + loop.exec(); |
| 103 | + |
| 104 | + if (token.isEmpty()) |
| 105 | + qCWarning(logDdeIntegration) << "XDG activation token request timed out"; |
| 106 | + else |
| 107 | + qCDebug(logDdeIntegration) << "Received XDG activation token for app:" << appId; |
| 108 | + |
| 109 | + provider->deleteLater(); |
| 110 | + return token; |
| 111 | +} |
| 112 | + |
| 113 | +} // namespace DDEIntegration |
0 commit comments