File: xdgtest.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 (200 lines) | stat: -rw-r--r-- 6,550 bytes parent folder | download | duplicates (7)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
    SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "KWayland/Client/compositor.h"
#include "KWayland/Client/connection_thread.h"
#include "KWayland/Client/event_queue.h"
#include "KWayland/Client/pointer.h"
#include "KWayland/Client/registry.h"
#include "KWayland/Client/seat.h"
#include "KWayland/Client/shadow.h"
#include "KWayland/Client/shell.h"
#include "KWayland/Client/shm_pool.h"
#include "KWayland/Client/surface.h"
#include "KWayland/Client/xdgshell.h"
// Qt
#include <QGuiApplication>
#include <QImage>
#include <QPainter>
#include <QThread>

using namespace KWayland::Client;

class XdgTest : public QObject
{
    Q_OBJECT
public:
    explicit XdgTest(QObject *parent = nullptr);
    virtual ~XdgTest();

    void init();

private:
    void setupRegistry(Registry *registry);
    void createPopup();
    void render();
    void renderPopup();
    QThread *m_connectionThread;
    ConnectionThread *m_connectionThreadObject;
    EventQueue *m_eventQueue = nullptr;
    Compositor *m_compositor = nullptr;
    ShmPool *m_shm = nullptr;
    Surface *m_surface = nullptr;
    XdgShell *m_xdgShell = nullptr;
    XdgShellSurface *m_xdgShellSurface = nullptr;
    Surface *m_popupSurface = nullptr;
    XdgShellPopup *m_xdgShellPopup = nullptr;
};

XdgTest::XdgTest(QObject *parent)
    : QObject(parent)
    , m_connectionThread(new QThread(this))
    , m_connectionThreadObject(new ConnectionThread())
{
}

XdgTest::~XdgTest()
{
    m_connectionThread->quit();
    m_connectionThread->wait();
    m_connectionThreadObject->deleteLater();
}

void XdgTest::init()
{
    connect(
        m_connectionThreadObject,
        &ConnectionThread::connected,
        this,
        [this] {
            m_eventQueue = new EventQueue(this);
            m_eventQueue->setup(m_connectionThreadObject);

            Registry *registry = new Registry(this);
            setupRegistry(registry);
        },
        Qt::QueuedConnection);
    m_connectionThreadObject->moveToThread(m_connectionThread);
    m_connectionThread->start();

    m_connectionThreadObject->initConnection();
}

void XdgTest::setupRegistry(Registry *registry)
{
    connect(registry, &Registry::compositorAnnounced, this, [this, registry](quint32 name, quint32 version) {
        m_compositor = registry->createCompositor(name, version, this);
    });
    connect(registry, &Registry::shmAnnounced, this, [this, registry](quint32 name, quint32 version) {
        m_shm = registry->createShmPool(name, version, this);
    });
    connect(registry, &Registry::xdgShellUnstableV6Announced, this, [this, registry](quint32 name, quint32 version) {
        m_xdgShell = registry->createXdgShell(name, version, this);
        m_xdgShell->setEventQueue(m_eventQueue);
    });
    connect(registry, &Registry::interfacesAnnounced, this, [this] {
        Q_ASSERT(m_compositor);
        Q_ASSERT(m_xdgShell);
        Q_ASSERT(m_shm);
        m_surface = m_compositor->createSurface(this);
        Q_ASSERT(m_surface);
        m_xdgShellSurface = m_xdgShell->createSurface(m_surface, this);
        Q_ASSERT(m_xdgShellSurface);
        connect(m_xdgShellSurface,
                &XdgShellSurface::configureRequested,
                this,
                [this](const QSize &size, KWayland::Client::XdgShellSurface::States states, int serial) {
                    m_xdgShellSurface->ackConfigure(serial);
                    render();
                });

        m_xdgShellSurface->setTitle(QStringLiteral("Test Window"));

        m_surface->commit();
    });
    connect(registry, &Registry::seatAnnounced, this, [this, registry](quint32 name) {
        Seat *s = registry->createSeat(name, 2, this);
        connect(s, &Seat::hasPointerChanged, this, [this, s](bool has) {
            if (!has) {
                return;
            }
            Pointer *p = s->createPointer(this);
            connect(p, &Pointer::buttonStateChanged, this, [this](quint32 serial, quint32 time, quint32 button, Pointer::ButtonState state) {
                if (state == Pointer::ButtonState::Released) {
                    if (m_popupSurface) {
                        m_popupSurface->deleteLater();
                        m_popupSurface = nullptr;
                    } else {
                        createPopup();
                    }
                }
            });
        });
    });

    registry->setEventQueue(m_eventQueue);
    registry->create(m_connectionThreadObject);
    registry->setup();
}

void XdgTest::createPopup()
{
    if (m_popupSurface) {
        m_popupSurface->deleteLater();
    }

    m_popupSurface = m_compositor->createSurface(this);

    XdgPositioner positioner(QSize(200, 200), QRect(50, 50, 400, 400));
    positioner.setAnchorEdge(Qt::BottomEdge | Qt::RightEdge);
    positioner.setGravity(Qt::BottomEdge);
    positioner.setConstraints(XdgPositioner::Constraint::FlipX | XdgPositioner::Constraint::SlideY);
    m_xdgShellPopup = m_xdgShell->createPopup(m_popupSurface, m_xdgShellSurface, positioner, m_popupSurface);
    renderPopup();
}

void XdgTest::render()
{
    const QSize &size = m_xdgShellSurface->size().isValid() ? m_xdgShellSurface->size() : QSize(500, 500);
    auto buffer = m_shm->getBuffer(size, size.width() * 4).toStrongRef();
    buffer->setUsed(true);
    QImage image(buffer->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied);
    image.fill(QColor(255, 255, 255, 255));
    // draw a red rectangle indicating the anchor of the top level
    QPainter painter(&image);
    painter.setBrush(Qt::red);
    painter.setPen(Qt::black);
    painter.drawRect(50, 50, 400, 400);

    m_surface->attachBuffer(*buffer);
    m_surface->damage(QRect(QPoint(0, 0), size));
    m_surface->commit(Surface::CommitFlag::None);
    buffer->setUsed(false);
}

void XdgTest::renderPopup()
{
    QSize size(200, 200);
    auto buffer = m_shm->getBuffer(size, size.width() * 4).toStrongRef();
    buffer->setUsed(true);
    QImage image(buffer->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied);
    image.fill(QColor(0, 0, 255, 255));

    m_popupSurface->attachBuffer(*buffer);
    m_popupSurface->damage(QRect(QPoint(0, 0), size));
    m_popupSurface->commit(Surface::CommitFlag::None);
    buffer->setUsed(false);
}

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    XdgTest client;
    client.init();

    return app.exec();
}

#include "xdgtest.moc"