KWin
Loading...
Searching...
No Matches
lockscreenoverlaytest.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2022 Aleix Pol Gonzalez <aleixpol@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5*/
6
7#include "qwayland-kde-lockscreen-overlay-v1.h"
8#include <KWindowSystem>
9#include <QWaylandClientExtensionTemplate>
10#include <QtWidgets>
11#include <qpa/qplatformnativeinterface.h>
12
13class WaylandAboveLockscreen : public QWaylandClientExtensionTemplate<WaylandAboveLockscreen>, public QtWayland::kde_lockscreen_overlay_v1
14{
15public:
17 : QWaylandClientExtensionTemplate<WaylandAboveLockscreen>(1)
18 {
19 QMetaObject::invokeMethod(this, "addRegistryListener");
20 }
21
22 void allowWindow(QWindow *window)
23 {
24 QPlatformNativeInterface *native = qGuiApp->platformNativeInterface();
25 wl_surface *surface = reinterpret_cast<wl_surface *>(native->nativeResourceForWindow(QByteArrayLiteral("surface"), window));
26 allow(surface);
27 }
28};
29
30class WidgetAllower : public QObject
31{
32public:
33 WidgetAllower(QWidget *widget)
34 : QObject(widget)
35 , m_widget(widget)
36 {
37 widget->installEventFilter(this);
38 }
39
40 bool eventFilter(QObject * /*watched*/, QEvent *event) override
41 {
42 if (auto w = m_widget->windowHandle()) {
43 WaylandAboveLockscreen aboveLockscreen;
44 Q_ASSERT(aboveLockscreen.isInitialized());
45 aboveLockscreen.allowWindow(w);
46 deleteLater();
47 }
48 return false;
49 }
50
51 QWidget *const m_widget;
52};
53
54int main(int argc, char *argv[])
55{
56
57 QApplication app(argc, argv);
58 QWidget window1(nullptr, Qt::Window);
59 window1.setWindowTitle("Window 1");
60 window1.setLayout(new QVBoxLayout);
61 QPushButton p("Lock && Raise the Window 2");
62 window1.layout()->addWidget(&p);
63 window1.show();
64
65 QWidget window2(nullptr, Qt::Window);
66 window2.setWindowTitle("Window 2");
67 window2.setLayout(new QVBoxLayout);
68 QPushButton p2("Close");
69 window2.layout()->addWidget(&p2);
70
71 new WidgetAllower(&window2);
72 auto raiseWindow2 = [&] {
73 KWindowSystem::requestXdgActivationToken(window2.windowHandle(), 0, "lockscreenoverlaytest.desktop");
74 };
75 QObject::connect(KWindowSystem::self(), &KWindowSystem::xdgActivationTokenArrived, &window2, [&window2](int, const QString &token) {
76 KWindowSystem::setCurrentXdgActivationToken(token);
77 KWindowSystem::activateWindow(window2.windowHandle());
78 });
79
80 QObject::connect(&p, &QPushButton::clicked, &app, [&] {
81 QProcess::execute("loginctl", {"lock-session"});
82 window2.showFullScreen();
83 QTimer::singleShot(3000, &app, raiseWindow2);
84 });
85
86 QObject::connect(&p2, &QPushButton::clicked, &window2, &QWidget::close);
87
88 return app.exec();
89}
void allowWindow(QWindow *window)
bool eventFilter(QObject *, QEvent *event) override
QWidget *const m_widget
WidgetAllower(QWidget *widget)