File indexing completed on 2024-11-10 04:58:08

0001 /*
0002     SPDX-FileCopyrightText: 2022 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "qwayland-kde-lockscreen-overlay-v1.h"
0008 #include <KWindowSystem>
0009 #include <QWaylandClientExtensionTemplate>
0010 #include <QtWidgets>
0011 #include <qpa/qplatformnativeinterface.h>
0012 
0013 class WaylandAboveLockscreen : public QWaylandClientExtensionTemplate<WaylandAboveLockscreen>, public QtWayland::kde_lockscreen_overlay_v1
0014 {
0015 public:
0016     WaylandAboveLockscreen()
0017         : QWaylandClientExtensionTemplate<WaylandAboveLockscreen>(1)
0018     {
0019         QMetaObject::invokeMethod(this, "addRegistryListener");
0020     }
0021 
0022     void allowWindow(QWindow *window)
0023     {
0024         QPlatformNativeInterface *native = qGuiApp->platformNativeInterface();
0025         wl_surface *surface = reinterpret_cast<wl_surface *>(native->nativeResourceForWindow(QByteArrayLiteral("surface"), window));
0026         allow(surface);
0027     }
0028 };
0029 
0030 class WidgetAllower : public QObject
0031 {
0032 public:
0033     WidgetAllower(QWidget *widget)
0034         : QObject(widget)
0035         , m_widget(widget)
0036     {
0037         widget->installEventFilter(this);
0038     }
0039 
0040     bool eventFilter(QObject * /*watched*/, QEvent *event) override
0041     {
0042         if (auto w = m_widget->windowHandle()) {
0043             WaylandAboveLockscreen aboveLockscreen;
0044             Q_ASSERT(aboveLockscreen.isInitialized());
0045             aboveLockscreen.allowWindow(w);
0046             deleteLater();
0047         }
0048         return false;
0049     }
0050 
0051     QWidget *const m_widget;
0052 };
0053 
0054 int main(int argc, char *argv[])
0055 {
0056 
0057     QApplication app(argc, argv);
0058     QWidget window1(nullptr, Qt::Window);
0059     window1.setWindowTitle("Window 1");
0060     window1.setLayout(new QVBoxLayout);
0061     QPushButton p("Lock && Raise the Window 2");
0062     window1.layout()->addWidget(&p);
0063     window1.show();
0064 
0065     QWidget window2(nullptr, Qt::Window);
0066     window2.setWindowTitle("Window 2");
0067     window2.setLayout(new QVBoxLayout);
0068     QPushButton p2("Close");
0069     window2.layout()->addWidget(&p2);
0070 
0071     new WidgetAllower(&window2);
0072     auto raiseWindow2 = [&] {
0073         KWindowSystem::requestXdgActivationToken(window2.windowHandle(), 0, "lockscreenoverlaytest.desktop");
0074     };
0075     QObject::connect(KWindowSystem::self(), &KWindowSystem::xdgActivationTokenArrived, &window2, [&window2](int, const QString &token) {
0076         KWindowSystem::setCurrentXdgActivationToken(token);
0077         KWindowSystem::activateWindow(window2.windowHandle());
0078     });
0079 
0080     QObject::connect(&p, &QPushButton::clicked, &app, [&] {
0081         QProcess::execute("loginctl", {"lock-session"});
0082         window2.showFullScreen();
0083         QTimer::singleShot(3000, &app, raiseWindow2);
0084     });
0085 
0086     QObject::connect(&p2, &QPushButton::clicked, &window2, &QWidget::close);
0087 
0088     return app.exec();
0089 }