File: lockscreenoverlaytest.cpp

package info (click to toggle)
kwin 4%3A6.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,376 kB
  • sloc: cpp: 240,899; javascript: 2,225; xml: 2,096; ansic: 775; sh: 37; python: 15; makefile: 8
file content (90 lines) | stat: -rw-r--r-- 2,809 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
    SPDX-FileCopyrightText: 2022 Aleix Pol Gonzalez <aleixpol@kde.org>

    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/

#include "qwayland-kde-lockscreen-overlay-v1.h"
#include <KWaylandExtras>
#include <KWindowSystem>
#include <QWaylandClientExtensionTemplate>
#include <QtWidgets>
#include <qpa/qplatformnativeinterface.h>

class WaylandAboveLockscreen : public QWaylandClientExtensionTemplate<WaylandAboveLockscreen>, public QtWayland::kde_lockscreen_overlay_v1
{
public:
    WaylandAboveLockscreen()
        : QWaylandClientExtensionTemplate<WaylandAboveLockscreen>(1)
    {
        QMetaObject::invokeMethod(this, "addRegistryListener");
    }

    void allowWindow(QWindow *window)
    {
        QPlatformNativeInterface *native = qGuiApp->platformNativeInterface();
        wl_surface *surface = reinterpret_cast<wl_surface *>(native->nativeResourceForWindow(QByteArrayLiteral("surface"), window));
        allow(surface);
    }
};

class WidgetAllower : public QObject
{
public:
    WidgetAllower(QWidget *widget)
        : QObject(widget)
        , m_widget(widget)
    {
        widget->installEventFilter(this);
    }

    bool eventFilter(QObject * /*watched*/, QEvent *event) override
    {
        if (auto w = m_widget->windowHandle()) {
            WaylandAboveLockscreen aboveLockscreen;
            Q_ASSERT(aboveLockscreen.isInitialized());
            aboveLockscreen.allowWindow(w);
            deleteLater();
        }
        return false;
    }

    QWidget *const m_widget;
};

int main(int argc, char *argv[])
{

    QApplication app(argc, argv);
    QWidget window1(nullptr, Qt::Window);
    window1.setWindowTitle("Window 1");
    window1.setLayout(new QVBoxLayout);
    QPushButton p("Lock && Raise the Window 2");
    window1.layout()->addWidget(&p);
    window1.show();

    QWidget window2(nullptr, Qt::Window);
    window2.setWindowTitle("Window 2");
    window2.setLayout(new QVBoxLayout);
    QPushButton p2("Close");
    window2.layout()->addWidget(&p2);

    new WidgetAllower(&window2);
    auto raiseWindow2 = [&] {
        KWaylandExtras::requestXdgActivationToken(window2.windowHandle(), 0, "lockscreenoverlaytest.desktop");
    };
    QObject::connect(KWaylandExtras::self(), &KWaylandExtras::xdgActivationTokenArrived, &window2, [&window2](int, const QString &token) {
        KWindowSystem::setCurrentXdgActivationToken(token);
        KWindowSystem::activateWindow(window2.windowHandle());
    });

    QObject::connect(&p, &QPushButton::clicked, &app, [&] {
        QProcess::execute("loginctl", {"lock-session"});
        window2.showFullScreen();
        QTimer::singleShot(3000, &app, raiseWindow2);
    });

    QObject::connect(&p2, &QPushButton::clicked, &window2, &QWidget::close);

    return app.exec();
}