Changeset View
Changeset View
Standalone View
Standalone View
autotests/integration/test_helpers.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | KWin - the KDE window manager | ||||
3 | This file is part of the KDE project. | ||||
4 | | ||||
5 | Copyright (C) 2015 Martin Gräßlin <mgraesslin@kde.org> | ||||
6 | | ||||
7 | This program is free software; you can redistribute it and/or modify | ||||
8 | it under the terms of the GNU General Public License as published by | ||||
9 | the Free Software Foundation; either version 2 of the License, or | ||||
10 | (at your option) any later version. | ||||
11 | | ||||
12 | This program 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 | ||||
15 | GNU General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU General Public License | ||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | *********************************************************************/ | ||||
20 | #include "kwin_wayland_test.h" | ||||
21 | | ||||
22 | #include <KWayland/Client/compositor.h> | ||||
23 | #include <KWayland/Client/connection_thread.h> | ||||
24 | #include <KWayland/Client/event_queue.h> | ||||
25 | #include <KWayland/Client/registry.h> | ||||
26 | #include <KWayland/Client/plasmashell.h> | ||||
27 | #include <KWayland/Client/plasmawindowmanagement.h> | ||||
28 | #include <KWayland/Client/seat.h> | ||||
29 | #include <KWayland/Client/server_decoration.h> | ||||
30 | #include <KWayland/Client/shell.h> | ||||
31 | #include <KWayland/Client/shm_pool.h> | ||||
32 | #include <KWayland/Client/surface.h> | ||||
33 | | ||||
34 | #include <QThread> | ||||
35 | | ||||
36 | using namespace KWayland::Client; | ||||
37 | | ||||
38 | namespace KWin | ||||
39 | { | ||||
40 | namespace Test | ||||
41 | { | ||||
42 | | ||||
43 | static struct { | ||||
44 | ConnectionThread *connection = nullptr; | ||||
45 | EventQueue *queue = nullptr; | ||||
46 | Compositor *compositor = nullptr; | ||||
47 | ServerSideDecorationManager *decoration = nullptr; | ||||
48 | Shell *shell = nullptr; | ||||
49 | ShmPool *shm = nullptr; | ||||
50 | Seat *seat = nullptr; | ||||
51 | PlasmaShell *plasmaShell = nullptr; | ||||
52 | PlasmaWindowManagement *windowManagement = nullptr; | ||||
53 | QThread *thread = nullptr; | ||||
54 | } s_waylandConnection; | ||||
55 | | ||||
56 | bool setupWaylandConnection(const QString &socketName, AdditionalWaylandInterfaces flags) | ||||
57 | { | ||||
58 | if (s_waylandConnection.connection) { | ||||
59 | return false; | ||||
60 | } | ||||
61 | // setup connection | ||||
62 | s_waylandConnection.connection = new ConnectionThread; | ||||
63 | QSignalSpy connectedSpy(s_waylandConnection.connection, &ConnectionThread::connected); | ||||
64 | if (!connectedSpy.isValid()) { | ||||
65 | return false; | ||||
66 | } | ||||
67 | s_waylandConnection.connection->setSocketName(socketName); | ||||
68 | | ||||
69 | s_waylandConnection.thread = new QThread(kwinApp()); | ||||
70 | s_waylandConnection.connection->moveToThread(s_waylandConnection.thread); | ||||
71 | s_waylandConnection.thread->start(); | ||||
72 | | ||||
73 | s_waylandConnection.connection->initConnection(); | ||||
74 | if (!connectedSpy.wait()) { | ||||
75 | return false; | ||||
76 | } | ||||
77 | | ||||
78 | s_waylandConnection.queue = new EventQueue; | ||||
79 | s_waylandConnection.queue->setup(s_waylandConnection.connection); | ||||
80 | if (!s_waylandConnection.queue->isValid()) { | ||||
81 | return false; | ||||
82 | } | ||||
83 | | ||||
84 | Registry registry; | ||||
85 | registry.setEventQueue(s_waylandConnection.queue); | ||||
86 | QSignalSpy allAnnounced(®istry, &Registry::interfacesAnnounced); | ||||
87 | if (!allAnnounced.isValid()) { | ||||
88 | return false; | ||||
89 | } | ||||
90 | registry.create(s_waylandConnection.connection); | ||||
91 | if (!registry.isValid()) { | ||||
92 | return false; | ||||
93 | } | ||||
94 | registry.setup(); | ||||
95 | if (!allAnnounced.wait()) { | ||||
96 | return false; | ||||
97 | } | ||||
98 | | ||||
99 | s_waylandConnection.compositor = registry.createCompositor(registry.interface(Registry::Interface::Compositor).name, registry.interface(Registry::Interface::Compositor).version); | ||||
100 | if (!s_waylandConnection.compositor->isValid()) { | ||||
101 | return false; | ||||
102 | } | ||||
103 | s_waylandConnection.shm = registry.createShmPool(registry.interface(Registry::Interface::Shm).name, registry.interface(Registry::Interface::Shm).version); | ||||
104 | if (!s_waylandConnection.shm->isValid()) { | ||||
105 | return false; | ||||
106 | } | ||||
107 | s_waylandConnection.shell = registry.createShell(registry.interface(Registry::Interface::Shell).name, registry.interface(Registry::Interface::Shell).version); | ||||
108 | if (!s_waylandConnection.shell->isValid()) { | ||||
109 | return false; | ||||
110 | } | ||||
111 | if (flags.testFlag(AdditionalWaylandInterface::Seat)) { | ||||
112 | s_waylandConnection.seat = registry.createSeat(registry.interface(Registry::Interface::Seat).name, registry.interface(Registry::Interface::Seat).version); | ||||
113 | if (!s_waylandConnection.seat->isValid()) { | ||||
114 | return false; | ||||
115 | } | ||||
116 | } | ||||
117 | if (flags.testFlag(AdditionalWaylandInterface::Decoration)) { | ||||
118 | s_waylandConnection.decoration = registry.createServerSideDecorationManager(registry.interface(Registry::Interface::ServerSideDecorationManager).name, | ||||
119 | registry.interface(Registry::Interface::ServerSideDecorationManager).version); | ||||
120 | if (!s_waylandConnection.decoration->isValid()) { | ||||
121 | return false; | ||||
122 | } | ||||
123 | } | ||||
124 | if (flags.testFlag(AdditionalWaylandInterface::PlasmaShell)) { | ||||
125 | s_waylandConnection.plasmaShell = registry.createPlasmaShell(registry.interface(Registry::Interface::PlasmaShell).name, | ||||
126 | registry.interface(Registry::Interface::PlasmaShell).version); | ||||
127 | if (!s_waylandConnection.plasmaShell->isValid()) { | ||||
128 | return false; | ||||
129 | } | ||||
130 | } | ||||
131 | if (flags.testFlag(AdditionalWaylandInterface::WindowManagement)) { | ||||
132 | s_waylandConnection.windowManagement = registry.createPlasmaWindowManagement(registry.interface(Registry::Interface::PlasmaWindowManagement).name, | ||||
133 | registry.interface(Registry::Interface::PlasmaWindowManagement).version); | ||||
134 | if (!s_waylandConnection.windowManagement->isValid()) { | ||||
135 | return false; | ||||
136 | } | ||||
137 | } | ||||
138 | | ||||
139 | return true; | ||||
140 | } | ||||
141 | | ||||
142 | void destroyWaylandConnection() | ||||
143 | { | ||||
144 | delete s_waylandConnection.compositor; | ||||
145 | s_waylandConnection.compositor = nullptr; | ||||
146 | delete s_waylandConnection.windowManagement; | ||||
147 | s_waylandConnection.windowManagement = nullptr; | ||||
148 | delete s_waylandConnection.plasmaShell; | ||||
149 | s_waylandConnection.plasmaShell = nullptr; | ||||
150 | delete s_waylandConnection.decoration; | ||||
151 | s_waylandConnection.decoration = nullptr; | ||||
152 | delete s_waylandConnection.decoration; | ||||
153 | s_waylandConnection.decoration = nullptr; | ||||
154 | delete s_waylandConnection.seat; | ||||
155 | s_waylandConnection.seat = nullptr; | ||||
156 | delete s_waylandConnection.shell; | ||||
157 | s_waylandConnection.shell = nullptr; | ||||
158 | delete s_waylandConnection.shm; | ||||
159 | s_waylandConnection.shm = nullptr; | ||||
160 | delete s_waylandConnection.queue; | ||||
161 | s_waylandConnection.queue = nullptr; | ||||
162 | if (s_waylandConnection.thread) { | ||||
163 | QSignalSpy spy(s_waylandConnection.connection, &QObject::destroyed); | ||||
164 | s_waylandConnection.connection->deleteLater(); | ||||
165 | QVERIFY(spy.wait()); | ||||
166 | s_waylandConnection.thread->quit(); | ||||
167 | s_waylandConnection.thread->wait(); | ||||
168 | delete s_waylandConnection.thread; | ||||
169 | s_waylandConnection.thread = nullptr; | ||||
170 | s_waylandConnection.connection = nullptr; | ||||
171 | } | ||||
172 | } | ||||
173 | | ||||
174 | ConnectionThread *waylandConnection() | ||||
175 | { | ||||
176 | return s_waylandConnection.connection; | ||||
177 | } | ||||
178 | | ||||
179 | Compositor *waylandCompositor() | ||||
180 | { | ||||
181 | return s_waylandConnection.compositor; | ||||
182 | } | ||||
183 | | ||||
184 | Shell *waylandShell() | ||||
185 | { | ||||
186 | return s_waylandConnection.shell; | ||||
187 | } | ||||
188 | | ||||
189 | ShmPool *waylandShmPool() | ||||
190 | { | ||||
191 | return s_waylandConnection.shm; | ||||
192 | } | ||||
193 | | ||||
194 | Seat *waylandSeat() | ||||
195 | { | ||||
196 | return s_waylandConnection.seat; | ||||
197 | } | ||||
198 | | ||||
199 | ServerSideDecorationManager *waylandServerSideDecoration() | ||||
200 | { | ||||
201 | return s_waylandConnection.decoration; | ||||
202 | } | ||||
203 | | ||||
204 | PlasmaShell *waylandPlasmaShell() | ||||
205 | { | ||||
206 | return s_waylandConnection.plasmaShell; | ||||
207 | } | ||||
208 | | ||||
209 | PlasmaWindowManagement *waylandWindowManagement() | ||||
210 | { | ||||
211 | return s_waylandConnection.windowManagement; | ||||
212 | } | ||||
213 | | ||||
214 | bool waitForWaylandPointer() | ||||
215 | { | ||||
216 | if (!s_waylandConnection.seat) { | ||||
217 | return false; | ||||
218 | } | ||||
219 | QSignalSpy hasPointerSpy(s_waylandConnection.seat, &Seat::hasPointerChanged); | ||||
220 | if (!hasPointerSpy.isValid()) { | ||||
221 | return false; | ||||
222 | } | ||||
223 | return hasPointerSpy.wait(); | ||||
224 | } | ||||
225 | | ||||
226 | bool waitForWaylandTouch() | ||||
227 | { | ||||
228 | if (!s_waylandConnection.seat) { | ||||
229 | return false; | ||||
230 | } | ||||
231 | QSignalSpy hasTouchSpy(s_waylandConnection.seat, &Seat::hasTouchChanged); | ||||
232 | if (!hasTouchSpy.isValid()) { | ||||
233 | return false; | ||||
234 | } | ||||
235 | return hasTouchSpy.wait(); | ||||
236 | } | ||||
237 | | ||||
238 | void render(Surface *surface, const QSize &size, const QColor &color, const QImage::Format &format) | ||||
239 | { | ||||
240 | QImage img(size, format); | ||||
241 | img.fill(color); | ||||
242 | surface->attachBuffer(s_waylandConnection.shm->createBuffer(img)); | ||||
243 | surface->damage(QRect(QPoint(0, 0), size)); | ||||
244 | surface->commit(Surface::CommitFlag::None); | ||||
245 | } | ||||
246 | | ||||
247 | void flushWaylandConnection() | ||||
248 | { | ||||
249 | if (s_waylandConnection.connection) { | ||||
250 | s_waylandConnection.connection->flush(); | ||||
251 | } | ||||
252 | } | ||||
253 | | ||||
254 | Surface *createSurface(QObject *parent) | ||||
255 | { | ||||
256 | if (!s_waylandConnection.compositor) { | ||||
257 | return nullptr; | ||||
258 | } | ||||
259 | auto s = s_waylandConnection.compositor->createSurface(parent); | ||||
260 | if (!s->isValid()) { | ||||
261 | delete s; | ||||
262 | return nullptr; | ||||
263 | } | ||||
264 | return s; | ||||
265 | } | ||||
266 | | ||||
267 | ShellSurface *createShellSurface(Surface *surface, QObject *parent) | ||||
268 | { | ||||
269 | if (!s_waylandConnection.shell) { | ||||
270 | return nullptr; | ||||
271 | } | ||||
272 | auto s = s_waylandConnection.shell->createSurface(surface, parent); | ||||
273 | if (!s->isValid()) { | ||||
274 | delete s; | ||||
275 | return nullptr; | ||||
276 | } | ||||
277 | return s; | ||||
278 | } | ||||
279 | | ||||
280 | } | ||||
281 | } |