KWin
Loading...
Searching...
No Matches
xdgforeigntest.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
3 SPDX-FileCopyrightText: 2017 Marco Martin <mart@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7#include "KWayland/Client/xdgforeign.h"
8#include "KWayland/Client/compositor.h"
9#include "KWayland/Client/connection_thread.h"
10#include "KWayland/Client/event_queue.h"
11#include "KWayland/Client/registry.h"
12#include "KWayland/Client/shell.h"
13#include "KWayland/Client/shm_pool.h"
14#include "KWayland/Client/xdgshell.h"
15// Qt
16#include <QCommandLineParser>
17#include <QDebug>
18#include <QGuiApplication>
19#include <QImage>
20#include <QThread>
21using namespace KWayland::Client;
22
23class XdgForeignTest : public QObject
24{
25 Q_OBJECT
26public:
27 explicit XdgForeignTest(QObject *parent = nullptr);
28 virtual ~XdgForeignTest();
29
30 void init();
31
32private:
33 void setupRegistry(Registry *registry);
34 void render();
35 QThread *m_connectionThread;
36 ConnectionThread *m_connectionThreadObject;
37 EventQueue *m_eventQueue = nullptr;
38 Compositor *m_compositor = nullptr;
39 XdgShell *m_shell = nullptr;
40 XdgShellSurface *m_shellSurface = nullptr;
41 ShmPool *m_shm = nullptr;
42 Surface *m_surface = nullptr;
43
44 XdgShellSurface *m_childShellSurface = nullptr;
45 Surface *m_childSurface = nullptr;
46
47 KWayland::Client::XdgExporter *m_exporter = nullptr;
48 KWayland::Client::XdgImporter *m_importer = nullptr;
49 KWayland::Client::XdgExported *m_exported = nullptr;
50 KWayland::Client::XdgImported *m_imported = nullptr;
51};
52
54 : QObject(parent)
55 , m_connectionThread(new QThread(this))
56 , m_connectionThreadObject(new ConnectionThread())
57{
58}
59
61{
62 m_connectionThread->quit();
63 m_connectionThread->wait();
64 m_connectionThreadObject->deleteLater();
65}
66
68{
69 connect(
70 m_connectionThreadObject,
71 &ConnectionThread::connected,
72 this,
73 [this] {
74 m_eventQueue = new EventQueue(this);
75 m_eventQueue->setup(m_connectionThreadObject);
76
77 Registry *registry = new Registry(this);
78 setupRegistry(registry);
79 },
80 Qt::QueuedConnection);
81 m_connectionThreadObject->moveToThread(m_connectionThread);
82 m_connectionThread->start();
83
84 m_connectionThreadObject->initConnection();
85}
86
87void XdgForeignTest::setupRegistry(Registry *registry)
88{
89 connect(registry, &Registry::compositorAnnounced, this, [this, registry](quint32 name, quint32 version) {
90 m_compositor = registry->createCompositor(name, version, this);
91 });
92 connect(registry, &Registry::xdgShellUnstableV5Announced, this, [this, registry](quint32 name, quint32 version) {
93 m_shell = registry->createXdgShell(name, version, this);
94 });
95 connect(registry, &Registry::shmAnnounced, this, [this, registry](quint32 name, quint32 version) {
96 m_shm = registry->createShmPool(name, version, this);
97 });
98 connect(registry, &Registry::exporterUnstableV2Announced, this, [this, registry](quint32 name, quint32 version) {
99 m_exporter = registry->createXdgExporter(name, version, this);
100 m_exporter->setEventQueue(m_eventQueue);
101 });
102 connect(registry, &Registry::importerUnstableV2Announced, this, [this, registry](quint32 name, quint32 version) {
103 m_importer = registry->createXdgImporter(name, version, this);
104 m_importer->setEventQueue(m_eventQueue);
105 });
106 connect(registry, &Registry::interfacesAnnounced, this, [this] {
107 Q_ASSERT(m_compositor);
108 Q_ASSERT(m_shell);
109 Q_ASSERT(m_shm);
110 Q_ASSERT(m_exporter);
111 Q_ASSERT(m_importer);
112 m_surface = m_compositor->createSurface(this);
113 Q_ASSERT(m_surface);
114 m_shellSurface = m_shell->createSurface(m_surface, this);
115 Q_ASSERT(m_shellSurface);
116 connect(m_shellSurface, &XdgShellSurface::sizeChanged, this, &XdgForeignTest::render);
117
118 m_childSurface = m_compositor->createSurface(this);
119 Q_ASSERT(m_childSurface);
120 m_childShellSurface = m_shell->createSurface(m_childSurface, this);
121 Q_ASSERT(m_childShellSurface);
122 connect(m_childShellSurface, &XdgShellSurface::sizeChanged, this, &XdgForeignTest::render);
123
124 m_exported = m_exporter->exportTopLevel(m_surface, this);
125 connect(m_exported, &XdgExported::done, this, [this]() {
126 m_imported = m_importer->importTopLevel(m_exported->handle(), this);
127 m_imported->setParentOf(m_childSurface);
128 });
129 render();
130 });
131 registry->setEventQueue(m_eventQueue);
132 registry->create(m_connectionThreadObject);
133 registry->setup();
134}
135
136void XdgForeignTest::render()
137{
138 QSize size = m_shellSurface->size().isValid() ? m_shellSurface->size() : QSize(500, 500);
139 auto buffer = m_shm->getBuffer(size, size.width() * 4).toStrongRef();
140 buffer->setUsed(true);
141 QImage image(buffer->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied);
142 image.fill(QColor(255, 255, 255, 255));
143
144 m_surface->attachBuffer(*buffer);
145 m_surface->damage(QRect(QPoint(0, 0), size));
146 m_surface->commit(Surface::CommitFlag::None);
147 buffer->setUsed(false);
148
149 size = m_childShellSurface->size().isValid() ? m_childShellSurface->size() : QSize(200, 200);
150 buffer = m_shm->getBuffer(size, size.width() * 4).toStrongRef();
151 buffer->setUsed(true);
152 image = QImage(buffer->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied);
153 image.fill(QColor(255, 0, 0, 255));
154
155 m_childSurface->attachBuffer(*buffer);
156 m_childSurface->damage(QRect(QPoint(0, 0), size));
157 m_childSurface->commit(Surface::CommitFlag::None);
158 buffer->setUsed(false);
159}
160
161int main(int argc, char **argv)
162{
163 QCoreApplication app(argc, argv);
164
165 XdgForeignTest client;
166
167 client.init();
168
169 return app.exec();
170}
171
172#include "xdgforeigntest.moc"
virtual ~XdgForeignTest()
XdgForeignTest(QObject *parent=nullptr)
KWayland::Client::Registry * registry
constexpr int version