File indexing completed on 2024-04-28 16:49:19

0001 /*
0002     SPDX-FileCopyrightText: 2022 Aleix Pol Gonzalez <aleixpol@kde.org>
0003     SPDX-FileCopyrightText: 2022 Ilya Fedin <fedin-ilja2010@ya.ru>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "qwayland-xdg-activation-v1.h"
0009 #include <QWaylandClientExtensionTemplate>
0010 #include <QtWidgets>
0011 #include <qpa/qplatformnativeinterface.h>
0012 
0013 class WaylandXdgActivationTokenV1 : public QObject, public QtWayland::xdg_activation_token_v1
0014 {
0015     Q_OBJECT
0016 public:
0017     void xdg_activation_token_v1_done(const QString &token) override
0018     {
0019         Q_EMIT done(token);
0020     }
0021 
0022 Q_SIGNALS:
0023     void done(const QString &token);
0024 };
0025 
0026 class WaylandXdgActivationV1 : public QWaylandClientExtensionTemplate<WaylandXdgActivationV1>, public QtWayland::xdg_activation_v1
0027 {
0028 public:
0029     WaylandXdgActivationV1()
0030         : QWaylandClientExtensionTemplate<WaylandXdgActivationV1>(1)
0031     {
0032         // QWaylandClientExtensionTemplate invokes this with a QueuedConnection but we want shortcuts
0033         // to be inhibited immediately.
0034         QMetaObject::invokeMethod(this, "addRegistryListener");
0035     }
0036 
0037     WaylandXdgActivationTokenV1 *requestXdgActivationToken(wl_seat *seat, struct ::wl_surface *surface, uint32_t serial, const QString &app_id)
0038     {
0039         auto wl = get_activation_token();
0040         auto provider = new WaylandXdgActivationTokenV1;
0041         provider->init(wl);
0042         if (surface) {
0043             provider->set_surface(surface);
0044         }
0045 
0046         if (!app_id.isEmpty()) {
0047             provider->set_app_id(app_id);
0048         }
0049 
0050         if (seat) {
0051             provider->set_serial(serial, seat);
0052         }
0053         provider->commit();
0054         return provider;
0055     }
0056 };
0057 
0058 int main(int argc, char *argv[])
0059 {
0060 
0061     QApplication app(argc, argv);
0062     QWidget window1(nullptr, Qt::Window);
0063     window1.setWindowTitle("Window 1");
0064     window1.setLayout(new QVBoxLayout);
0065     QPushButton p("Raise the Window 2");
0066     window1.layout()->addWidget(&p);
0067     window1.show();
0068 
0069     QWidget window2(nullptr, Qt::Window);
0070     window2.setWindowTitle("Window 2");
0071     window2.show();
0072 
0073     WaylandXdgActivationV1 activation;
0074     QObject::connect(&p, &QPushButton::clicked, &app, [&] {
0075         QPlatformNativeInterface *native = qGuiApp->platformNativeInterface();
0076         auto seat = static_cast<wl_seat *>(native->nativeResourceForIntegration("wl_seat"));
0077         wl_surface *surface = reinterpret_cast<wl_surface *>(native->nativeResourceForWindow(QByteArrayLiteral("surface"), window1.windowHandle()));
0078         auto req = activation.requestXdgActivationToken(seat, surface, 0, {});
0079         QObject::connect(req, &WaylandXdgActivationTokenV1::done, &app, [&activation, &window2](const QString &token) {
0080             window2.show();
0081             QPlatformNativeInterface *native = qGuiApp->platformNativeInterface();
0082             wl_surface *surface = reinterpret_cast<wl_surface *>(native->nativeResourceForWindow(QByteArrayLiteral("surface"), window2.windowHandle()));
0083             activation.activate(token, surface);
0084         });
0085     });
0086 
0087     return app.exec();
0088 }
0089 
0090 #include "xdgactivationtest-qt5.moc"