Changeset View
Changeset View
Standalone View
Standalone View
tests/xdgtest.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | Copyright 2015 Martin Gräßlin <mgraesslin@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | *********************************************************************/ | ||||
20 | #include "../src/client/compositor.h" | ||||
21 | #include "../src/client/connection_thread.h" | ||||
22 | #include "../src/client/event_queue.h" | ||||
23 | #include "../src/client/registry.h" | ||||
24 | #include "../src/client/shadow.h" | ||||
25 | #include "../src/client/shell.h" | ||||
26 | #include "../src/client/shm_pool.h" | ||||
27 | #include "../src/client/surface.h" | ||||
28 | #include "../src/client/xdgshell.h" | ||||
29 | // Qt | ||||
30 | #include <QGuiApplication> | ||||
31 | #include <QImage> | ||||
32 | #include <QThread> | ||||
33 | | ||||
34 | using namespace KWayland::Client; | ||||
35 | | ||||
36 | class XdgTest : public QObject | ||||
37 | { | ||||
38 | Q_OBJECT | ||||
39 | public: | ||||
40 | explicit XdgTest(QObject *parent = nullptr); | ||||
41 | virtual ~XdgTest(); | ||||
42 | | ||||
43 | void init(); | ||||
44 | | ||||
45 | private: | ||||
46 | void setupRegistry(Registry *registry); | ||||
47 | void render(); | ||||
48 | void renderPopup(); | ||||
49 | QThread *m_connectionThread; | ||||
50 | ConnectionThread *m_connectionThreadObject; | ||||
51 | EventQueue *m_eventQueue = nullptr; | ||||
52 | Compositor *m_compositor = nullptr; | ||||
53 | ShmPool *m_shm = nullptr; | ||||
54 | Surface *m_surface = nullptr; | ||||
55 | XdgShell *m_xdgShell = nullptr; | ||||
56 | XdgShellSurface *m_xdgShellSurface = nullptr; | ||||
57 | Surface *m_popupSurface = nullptr; | ||||
58 | XdgShellPopup *m_xdgShellPopup = nullptr; | ||||
59 | }; | ||||
60 | | ||||
61 | XdgTest::XdgTest(QObject *parent) | ||||
62 | : QObject(parent) | ||||
63 | , m_connectionThread(new QThread(this)) | ||||
64 | , m_connectionThreadObject(new ConnectionThread()) | ||||
65 | { | ||||
66 | } | ||||
67 | | ||||
68 | XdgTest::~XdgTest() | ||||
69 | { | ||||
70 | m_connectionThread->quit(); | ||||
71 | m_connectionThread->wait(); | ||||
72 | m_connectionThreadObject->deleteLater(); | ||||
73 | } | ||||
74 | | ||||
75 | void XdgTest::init() | ||||
76 | { | ||||
77 | connect(m_connectionThreadObject, &ConnectionThread::connected, this, | ||||
78 | [this] { | ||||
79 | m_eventQueue = new EventQueue(this); | ||||
80 | m_eventQueue->setup(m_connectionThreadObject); | ||||
81 | | ||||
82 | Registry *registry = new Registry(this); | ||||
83 | setupRegistry(registry); | ||||
84 | }, | ||||
85 | Qt::QueuedConnection | ||||
86 | ); | ||||
87 | m_connectionThreadObject->moveToThread(m_connectionThread); | ||||
88 | m_connectionThread->start(); | ||||
89 | | ||||
90 | m_connectionThreadObject->initConnection(); | ||||
91 | } | ||||
92 | | ||||
93 | void XdgTest::setupRegistry(Registry *registry) | ||||
94 | { | ||||
95 | connect(registry, &Registry::compositorAnnounced, this, | ||||
96 | [this, registry](quint32 name, quint32 version) { | ||||
97 | m_compositor = registry->createCompositor(name, version, this); | ||||
98 | } | ||||
99 | ); | ||||
100 | connect(registry, &Registry::shmAnnounced, this, | ||||
101 | [this, registry](quint32 name, quint32 version) { | ||||
102 | m_shm = registry->createShmPool(name, version, this); | ||||
103 | } | ||||
104 | ); | ||||
105 | connect(registry, &Registry::xdgShellUnstableV6Announced, this, | ||||
106 | [this, registry](quint32 name, quint32 version) { | ||||
107 | m_xdgShell = registry->createXdgShell(name, version, this); | ||||
108 | m_xdgShell->setEventQueue(m_eventQueue); | ||||
109 | } | ||||
110 | ); | ||||
111 | connect(registry, &Registry::interfacesAnnounced, this, | ||||
112 | [this] { | ||||
113 | Q_ASSERT(m_compositor); | ||||
114 | Q_ASSERT(m_xdgShell); | ||||
115 | Q_ASSERT(m_shm); | ||||
116 | m_surface = m_compositor->createSurface(this); | ||||
117 | Q_ASSERT(m_surface); | ||||
118 | m_xdgShellSurface = m_xdgShell->createSurface(m_surface, this); | ||||
119 | Q_ASSERT(m_xdgShellSurface); | ||||
120 | connect(m_xdgShellSurface, &XdgShellSurface::sizeChanged, this, &XdgTest::render); | ||||
121 | render(); | ||||
122 | | ||||
123 | //create popup | ||||
124 | m_popupSurface = m_compositor->createSurface(this); | ||||
125 | | ||||
126 | XdgPositioner positioner(QSize(50,50), QRect(100,100, 20, 20)); | ||||
127 | m_xdgShellPopup = m_xdgShell->createPopup(m_popupSurface, m_xdgShellSurface, positioner, this); | ||||
128 | renderPopup(); | ||||
129 | } | ||||
130 | ); | ||||
131 | registry->setEventQueue(m_eventQueue); | ||||
132 | registry->create(m_connectionThreadObject); | ||||
133 | registry->setup(); | ||||
134 | } | ||||
135 | | ||||
136 | | ||||
137 | void XdgTest::render() | ||||
138 | { | ||||
139 | const QSize &size = m_xdgShellSurface->size().isValid() ? m_xdgShellSurface->size() : QSize(300, 200); | ||||
140 | auto buffer = m_shm->getBuffer(size, size.width() * 4).toStrongRef(); | ||||
141 | buffer->setUsed(true); | ||||
142 | QImage image(buffer->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied); | ||||
143 | image.fill(QColor(255, 255, 255, 128)); | ||||
144 | | ||||
145 | m_surface->attachBuffer(*buffer); | ||||
146 | m_surface->damage(QRect(QPoint(0, 0), size)); | ||||
147 | m_surface->commit(Surface::CommitFlag::None); | ||||
148 | buffer->setUsed(false); | ||||
149 | } | ||||
150 | | ||||
151 | void XdgTest::renderPopup() | ||||
152 | { | ||||
153 | QSize size(200,200); | ||||
154 | auto buffer = m_shm->getBuffer(size, size.width() * 4).toStrongRef(); | ||||
155 | buffer->setUsed(true); | ||||
156 | QImage image(buffer->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied); | ||||
157 | image.fill(QColor(255, 0, 0, 255)); | ||||
158 | | ||||
159 | m_popupSurface->attachBuffer(*buffer); | ||||
160 | m_popupSurface->damage(QRect(QPoint(0, 0), size)); | ||||
161 | m_popupSurface->commit(Surface::CommitFlag::None); | ||||
162 | buffer->setUsed(false); | ||||
163 | } | ||||
164 | | ||||
165 | | ||||
166 | | ||||
167 | int main(int argc, char **argv) | ||||
168 | { | ||||
169 | QCoreApplication app(argc, argv); | ||||
170 | XdgTest client; | ||||
171 | client.init(); | ||||
172 | | ||||
173 | return app.exec(); | ||||
174 | } | ||||
175 | | ||||
176 | #include "xdgtest.moc" |