| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2021 Aleix Pol <aleixpol@kde.org> |
| 4 | SPDX-FileCopyrightText: 2023 Nicolas Fella <nicolas.fella@gmx.de> |
| 5 | |
| 6 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 7 | */ |
| 8 | |
| 9 | #include "kwaylandextras.h" |
| 10 | |
| 11 | #include "kwindowsystem.h" |
| 12 | #include "kwindowsystem_p.h" |
| 13 | |
| 14 | #include <QTimer> |
| 15 | |
| 16 | KWaylandExtras::KWaylandExtras() |
| 17 | : QObject() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | KWaylandExtras::~KWaylandExtras() = default; |
| 22 | |
| 23 | KWaylandExtras *KWaylandExtras::self() |
| 24 | { |
| 25 | static KWaylandExtras instance; |
| 26 | return &instance; |
| 27 | } |
| 28 | |
| 29 | #if KWINDOWSYSTEM_BUILD_DEPRECATED_SINCE(6, 19) |
| 30 | void KWaylandExtras::requestXdgActivationToken(QWindow *window, uint32_t serial, const QString &app_id) |
| 31 | { |
| 32 | auto dv2 = dynamic_cast<KWindowSystemPrivateV2 *>(KWindowSystem::d_func()); |
| 33 | if (!dv2) { |
| 34 | // Ensure that xdgActivationTokenArrived is always emitted asynchronously |
| 35 | QTimer::singleShot(interval: 0, slot: [serial] { |
| 36 | Q_EMIT KWaylandExtras::self()->xdgActivationTokenArrived(serial, token: {}); |
| 37 | }); |
| 38 | |
| 39 | return; |
| 40 | } |
| 41 | dv2->requestToken(win: window, serial, app_id); |
| 42 | } |
| 43 | #endif |
| 44 | |
| 45 | quint32 KWaylandExtras::lastInputSerial(QWindow *window) |
| 46 | { |
| 47 | auto dv2 = dynamic_cast<KWindowSystemPrivateV2 *>(KWindowSystem::d_func()); |
| 48 | if (!dv2) { |
| 49 | return 0; |
| 50 | } |
| 51 | return dv2->lastInputSerial(window); |
| 52 | } |
| 53 | |
| 54 | void KWaylandExtras::exportWindow(QWindow *window) |
| 55 | { |
| 56 | if (auto dv2 = dynamic_cast<KWindowSystemPrivateV2 *>(KWindowSystem::d_func())) { |
| 57 | dv2->exportWindow(window); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void KWaylandExtras::unexportWindow(QWindow *window) |
| 62 | { |
| 63 | if (auto dv2 = dynamic_cast<KWindowSystemPrivateV2 *>(KWindowSystem::d_func())) { |
| 64 | dv2->unexportWindow(window); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | QFuture<QString> KWaylandExtras::xdgActivationToken(QWindow *window, uint32_t serial, const QString &appId) |
| 69 | { |
| 70 | if (auto dv3 = dynamic_cast<KWindowSystemPrivateV3 *>(KWindowSystem::d_func())) { |
| 71 | return dv3->xdgActivationToken(window, serial, appId); |
| 72 | } else { |
| 73 | #if KWINDOWSYSTEM_BUILD_DEPRECATED_SINCE(6, 19) |
| 74 | requestXdgActivationToken(window, serial, app_id: appId); |
| 75 | QPromise<QString> promise; |
| 76 | auto future = promise.future(); |
| 77 | |
| 78 | connect( |
| 79 | sender: self(), |
| 80 | signal: &KWaylandExtras::xdgActivationTokenArrived, |
| 81 | context: self(), |
| 82 | slot: [p = std::move(promise)](int /*serial*/, const QString &token) mutable { |
| 83 | p.addResult(result: token); |
| 84 | p.finish(); |
| 85 | }, |
| 86 | type: Qt::SingleShotConnection); |
| 87 | |
| 88 | return future; |
| 89 | #else |
| 90 | return QtFuture::makeReadyValueFuture(QString()); |
| 91 | #endif |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | QFuture<QString> KWaylandExtras::xdgActivationToken(QWindow *window, const QString &appId) |
| 96 | { |
| 97 | return xdgActivationToken(window, serial: lastInputSerial(window), appId); |
| 98 | } |
| 99 | |
| 100 | void KWaylandExtras::setXdgToplevelTag(QWindow *window, const QString &tag) |
| 101 | { |
| 102 | if (auto dv4 = dynamic_cast<KWindowSystemPrivateV4 *>(KWindowSystem::d_func())) { |
| 103 | dv4->setXdgToplevelTag(window, tag); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void KWaylandExtras::setXdgToplevelDescription(QWindow *window, const QString &description) |
| 108 | { |
| 109 | if (auto dv4 = dynamic_cast<KWindowSystemPrivateV4 *>(KWindowSystem::d_func())) { |
| 110 | dv4->setXdgToplevelDescription(window, description); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #include "moc_kwaylandextras.cpp" |
| 115 | |