KWin
Loading...
Searching...
No Matches
xdgtest.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6#include "KWayland/Client/compositor.h"
7#include "KWayland/Client/connection_thread.h"
8#include "KWayland/Client/event_queue.h"
9#include "KWayland/Client/pointer.h"
10#include "KWayland/Client/registry.h"
11#include "KWayland/Client/seat.h"
12#include "KWayland/Client/shadow.h"
13#include "KWayland/Client/shell.h"
14#include "KWayland/Client/shm_pool.h"
15#include "KWayland/Client/surface.h"
16#include "KWayland/Client/xdgshell.h"
17// Qt
18#include <QGuiApplication>
19#include <QImage>
20#include <QPainter>
21#include <QThread>
22
23using namespace KWayland::Client;
24
25class XdgTest : public QObject
26{
27 Q_OBJECT
28public:
29 explicit XdgTest(QObject *parent = nullptr);
30 virtual ~XdgTest();
31
32 void init();
33
34private:
35 void setupRegistry(Registry *registry);
36 void createPopup();
37 void render();
38 void renderPopup();
39 QThread *m_connectionThread;
40 ConnectionThread *m_connectionThreadObject;
41 EventQueue *m_eventQueue = nullptr;
42 Compositor *m_compositor = nullptr;
43 ShmPool *m_shm = nullptr;
44 Surface *m_surface = nullptr;
45 XdgShell *m_xdgShell = nullptr;
46 XdgShellSurface *m_xdgShellSurface = nullptr;
47 Surface *m_popupSurface = nullptr;
48 XdgShellPopup *m_xdgShellPopup = nullptr;
49};
50
51XdgTest::XdgTest(QObject *parent)
52 : QObject(parent)
53 , m_connectionThread(new QThread(this))
54 , m_connectionThreadObject(new ConnectionThread())
55{
56}
57
59{
60 m_connectionThread->quit();
61 m_connectionThread->wait();
62 m_connectionThreadObject->deleteLater();
63}
64
66{
67 connect(
68 m_connectionThreadObject,
69 &ConnectionThread::connected,
70 this,
71 [this] {
72 m_eventQueue = new EventQueue(this);
73 m_eventQueue->setup(m_connectionThreadObject);
74
75 Registry *registry = new Registry(this);
76 setupRegistry(registry);
77 },
78 Qt::QueuedConnection);
79 m_connectionThreadObject->moveToThread(m_connectionThread);
80 m_connectionThread->start();
81
82 m_connectionThreadObject->initConnection();
83}
84
85void XdgTest::setupRegistry(Registry *registry)
86{
87 connect(registry, &Registry::compositorAnnounced, this, [this, registry](quint32 name, quint32 version) {
88 m_compositor = registry->createCompositor(name, version, this);
89 });
90 connect(registry, &Registry::shmAnnounced, this, [this, registry](quint32 name, quint32 version) {
91 m_shm = registry->createShmPool(name, version, this);
92 });
93 connect(registry, &Registry::xdgShellUnstableV6Announced, this, [this, registry](quint32 name, quint32 version) {
94 m_xdgShell = registry->createXdgShell(name, version, this);
95 m_xdgShell->setEventQueue(m_eventQueue);
96 });
97 connect(registry, &Registry::interfacesAnnounced, this, [this] {
98 Q_ASSERT(m_compositor);
99 Q_ASSERT(m_xdgShell);
100 Q_ASSERT(m_shm);
101 m_surface = m_compositor->createSurface(this);
102 Q_ASSERT(m_surface);
103 m_xdgShellSurface = m_xdgShell->createSurface(m_surface, this);
104 Q_ASSERT(m_xdgShellSurface);
105 connect(m_xdgShellSurface,
106 &XdgShellSurface::configureRequested,
107 this,
108 [this](const QSize &size, KWayland::Client::XdgShellSurface::States states, int serial) {
109 m_xdgShellSurface->ackConfigure(serial);
110 render();
111 });
112
113 m_xdgShellSurface->setTitle(QStringLiteral("Test Window"));
114
115 m_surface->commit();
116 });
117 connect(registry, &Registry::seatAnnounced, this, [this, registry](quint32 name) {
118 Seat *s = registry->createSeat(name, 2, this);
119 connect(s, &Seat::hasPointerChanged, this, [this, s](bool has) {
120 if (!has) {
121 return;
122 }
123 Pointer *p = s->createPointer(this);
124 connect(p, &Pointer::buttonStateChanged, this, [this](quint32 serial, quint32 time, quint32 button, Pointer::ButtonState state) {
125 if (state == Pointer::ButtonState::Released) {
126 if (m_popupSurface) {
127 m_popupSurface->deleteLater();
128 m_popupSurface = nullptr;
129 } else {
130 createPopup();
131 }
132 }
133 });
134 });
135 });
136
137 registry->setEventQueue(m_eventQueue);
138 registry->create(m_connectionThreadObject);
139 registry->setup();
140}
141
142void XdgTest::createPopup()
143{
144 if (m_popupSurface) {
145 m_popupSurface->deleteLater();
146 }
147
148 m_popupSurface = m_compositor->createSurface(this);
149
150 XdgPositioner positioner(QSize(200, 200), QRect(50, 50, 400, 400));
151 positioner.setAnchorEdge(Qt::BottomEdge | Qt::RightEdge);
152 positioner.setGravity(Qt::BottomEdge);
153 positioner.setConstraints(XdgPositioner::Constraint::FlipX | XdgPositioner::Constraint::SlideY);
154 m_xdgShellPopup = m_xdgShell->createPopup(m_popupSurface, m_xdgShellSurface, positioner, m_popupSurface);
155 renderPopup();
156}
157
158void XdgTest::render()
159{
160 const QSize &size = m_xdgShellSurface->size().isValid() ? m_xdgShellSurface->size() : QSize(500, 500);
161 auto buffer = m_shm->getBuffer(size, size.width() * 4).toStrongRef();
162 buffer->setUsed(true);
163 QImage image(buffer->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied);
164 image.fill(QColor(255, 255, 255, 255));
165 // draw a red rectangle indicating the anchor of the top level
166 QPainter painter(&image);
167 painter.setBrush(Qt::red);
168 painter.setPen(Qt::black);
169 painter.drawRect(50, 50, 400, 400);
170
171 m_surface->attachBuffer(*buffer);
172 m_surface->damage(QRect(QPoint(0, 0), size));
173 m_surface->commit(Surface::CommitFlag::None);
174 buffer->setUsed(false);
175}
176
177void XdgTest::renderPopup()
178{
179 QSize size(200, 200);
180 auto buffer = m_shm->getBuffer(size, size.width() * 4).toStrongRef();
181 buffer->setUsed(true);
182 QImage image(buffer->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied);
183 image.fill(QColor(0, 0, 255, 255));
184
185 m_popupSurface->attachBuffer(*buffer);
186 m_popupSurface->damage(QRect(QPoint(0, 0), size));
187 m_popupSurface->commit(Surface::CommitFlag::None);
188 buffer->setUsed(false);
189}
190
191int main(int argc, char **argv)
192{
193 QCoreApplication app(argc, argv);
194 XdgTest client;
195 client.init();
196
197 return app.exec();
198}
199
200#include "xdgtest.moc"
XdgTest(QObject *parent=nullptr)
Definition xdgtest.cpp:51
virtual ~XdgTest()
Definition xdgtest.cpp:58
void init()
Definition xdgtest.cpp:65
KWayland::Client::Registry * registry
constexpr int version