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"
18#include <QGuiApplication>
29 explicit XdgTest(QObject *parent =
nullptr);
35 void setupRegistry(Registry *registry);
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;
46 XdgShellSurface *m_xdgShellSurface =
nullptr;
47 Surface *m_popupSurface =
nullptr;
48 XdgShellPopup *m_xdgShellPopup =
nullptr;
53 , m_connectionThread(new QThread(this))
54 , m_connectionThreadObject(new ConnectionThread())
60 m_connectionThread->quit();
61 m_connectionThread->wait();
62 m_connectionThreadObject->deleteLater();
68 m_connectionThreadObject,
69 &ConnectionThread::connected,
72 m_eventQueue =
new EventQueue(
this);
73 m_eventQueue->setup(m_connectionThreadObject);
75 Registry *registry =
new Registry(
this);
76 setupRegistry(registry);
78 Qt::QueuedConnection);
79 m_connectionThreadObject->moveToThread(m_connectionThread);
80 m_connectionThread->start();
82 m_connectionThreadObject->initConnection();
85void XdgTest::setupRegistry(Registry *registry)
87 connect(registry, &Registry::compositorAnnounced,
this, [
this, registry](quint32 name, quint32
version) {
88 m_compositor = registry->createCompositor(name,
version,
this);
90 connect(registry, &Registry::shmAnnounced,
this, [
this, registry](quint32 name, quint32
version) {
93 connect(registry, &Registry::xdgShellUnstableV6Announced,
this, [
this, registry](quint32 name, quint32
version) {
95 m_xdgShell->setEventQueue(m_eventQueue);
97 connect(registry, &Registry::interfacesAnnounced,
this, [
this] {
98 Q_ASSERT(m_compositor);
101 m_surface = m_compositor->createSurface(
this);
103 m_xdgShellSurface = m_xdgShell->createSurface(m_surface,
this);
104 Q_ASSERT(m_xdgShellSurface);
105 connect(m_xdgShellSurface,
106 &XdgShellSurface::configureRequested,
108 [
this](
const QSize &size, KWayland::Client::XdgShellSurface::States states,
int serial) {
109 m_xdgShellSurface->ackConfigure(serial);
113 m_xdgShellSurface->setTitle(QStringLiteral(
"Test Window"));
117 connect(registry, &Registry::seatAnnounced,
this, [
this, registry](quint32 name) {
119 connect(s, &Seat::hasPointerChanged,
this, [
this, s](
bool has) {
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;
137 registry->setEventQueue(m_eventQueue);
138 registry->create(m_connectionThreadObject);
142void XdgTest::createPopup()
144 if (m_popupSurface) {
145 m_popupSurface->deleteLater();
148 m_popupSurface = m_compositor->createSurface(
this);
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);
158void XdgTest::render()
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));
166 QPainter painter(&image);
167 painter.setBrush(Qt::red);
168 painter.setPen(Qt::black);
169 painter.drawRect(50, 50, 400, 400);
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);
177void XdgTest::renderPopup()
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));
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);
193 QCoreApplication app(argc, argv);
200#include "xdgtest.moc"
XdgTest(QObject *parent=nullptr)
KWayland::Client::Registry * registry