KWin
Loading...
Searching...
No Matches
paneltest.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/datadevice.h"
9#include "KWayland/Client/datadevicemanager.h"
10#include "KWayland/Client/dataoffer.h"
11#include "KWayland/Client/event_queue.h"
12#include "KWayland/Client/keyboard.h"
13#include "KWayland/Client/plasmashell.h"
14#include "KWayland/Client/plasmawindowmanagement.h"
15#include "KWayland/Client/pointer.h"
16#include "KWayland/Client/registry.h"
17#include "KWayland/Client/seat.h"
18#include "KWayland/Client/shell.h"
19#include "KWayland/Client/shm_pool.h"
20#include "KWayland/Client/surface.h"
21// Qt
22#include <QDebug>
23#include <QFile>
24#include <QGuiApplication>
25#include <QImage>
26#include <QMimeType>
27#include <QThread>
28// system
29#include <unistd.h>
30
31#include <linux/input.h>
32
33using namespace KWayland::Client;
34
35class PanelTest : public QObject
36{
37 Q_OBJECT
38public:
39 explicit PanelTest(QObject *parent = nullptr);
40 virtual ~PanelTest();
41
42 void init();
43
44private:
45 void setupRegistry(Registry *registry);
46 void render();
47 void showTooltip(const QPointF &pos);
48 void hideTooltip();
49 void moveTooltip(const QPointF &pos);
50 QThread *m_connectionThread;
51 ConnectionThread *m_connectionThreadObject;
52 EventQueue *m_eventQueue = nullptr;
53 Compositor *m_compositor = nullptr;
54 Seat *m_seat = nullptr;
55 Shell *m_shell = nullptr;
56 ShellSurface *m_shellSurface = nullptr;
57 ShmPool *m_shm = nullptr;
58 Surface *m_surface = nullptr;
59 PlasmaShell *m_plasmaShell = nullptr;
60 PlasmaShellSurface *m_plasmaShellSurface = nullptr;
61 PlasmaWindowManagement *m_windowManagement = nullptr;
62 struct
63 {
64 Surface *surface = nullptr;
65 ShellSurface *shellSurface = nullptr;
66 PlasmaShellSurface *plasmaSurface = nullptr;
67 bool visible = false;
68 } m_tooltip;
69};
70
71PanelTest::PanelTest(QObject *parent)
72 : QObject(parent)
73 , m_connectionThread(new QThread(this))
74 , m_connectionThreadObject(new ConnectionThread())
75{
76}
77
79{
80 m_connectionThread->quit();
81 m_connectionThread->wait();
82 m_connectionThreadObject->deleteLater();
83}
84
86{
87 connect(
88 m_connectionThreadObject,
89 &ConnectionThread::connected,
90 this,
91 [this] {
92 m_eventQueue = new EventQueue(this);
93 m_eventQueue->setup(m_connectionThreadObject);
94
95 Registry *registry = new Registry(this);
96 setupRegistry(registry);
97 },
98 Qt::QueuedConnection);
99 m_connectionThreadObject->moveToThread(m_connectionThread);
100 m_connectionThread->start();
101
102 m_connectionThreadObject->initConnection();
103}
104
105void PanelTest::showTooltip(const QPointF &pos)
106{
107 if (!m_tooltip.surface) {
108 m_tooltip.surface = m_compositor->createSurface(this);
109 m_tooltip.shellSurface = m_shell->createSurface(m_tooltip.surface, this);
110 if (m_plasmaShell) {
111 m_tooltip.plasmaSurface = m_plasmaShell->createSurface(m_tooltip.surface, this);
112 }
113 }
114 m_tooltip.shellSurface->setTransient(m_surface, pos.toPoint());
115
116 if (!m_tooltip.visible) {
117 const QSize size(100, 50);
118 auto buffer = m_shm->getBuffer(size, size.width() * 4).toStrongRef();
119 buffer->setUsed(true);
120 QImage image(buffer->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied);
121 image.fill(Qt::red);
122
123 m_tooltip.surface->attachBuffer(*buffer);
124 m_tooltip.surface->damage(QRect(QPoint(0, 0), size));
125 m_tooltip.surface->commit(Surface::CommitFlag::None);
126 m_tooltip.visible = true;
127 }
128}
129
130void PanelTest::hideTooltip()
131{
132 if (!m_tooltip.visible) {
133 return;
134 }
135 m_tooltip.surface->attachBuffer(Buffer::Ptr());
136 m_tooltip.surface->commit(Surface::CommitFlag::None);
137 m_tooltip.visible = false;
138}
139
140void PanelTest::moveTooltip(const QPointF &pos)
141{
142 if (m_tooltip.plasmaSurface) {
143 m_tooltip.plasmaSurface->setPosition(QPoint(10, 0) + pos.toPoint());
144 }
145}
146
147void PanelTest::setupRegistry(Registry *registry)
148{
149 connect(registry, &Registry::compositorAnnounced, this, [this, registry](quint32 name, quint32 version) {
150 m_compositor = registry->createCompositor(name, version, this);
151 });
152 connect(registry, &Registry::shellAnnounced, this, [this, registry](quint32 name, quint32 version) {
153 m_shell = registry->createShell(name, version, this);
154 });
155 connect(registry, &Registry::shmAnnounced, this, [this, registry](quint32 name, quint32 version) {
156 m_shm = registry->createShmPool(name, version, this);
157 });
158 connect(registry, &Registry::seatAnnounced, this, [this, registry](quint32 name, quint32 version) {
159 m_seat = registry->createSeat(name, version, this);
160 connect(m_seat, &Seat::hasPointerChanged, this, [this](bool has) {
161 if (!has) {
162 return;
163 }
164 auto p = m_seat->createPointer(this);
165 connect(p, &Pointer::buttonStateChanged, this, [this](quint32 serial, quint32 time, quint32 button, KWayland::Client::Pointer::ButtonState state) {
166 if (!m_windowManagement) {
167 return;
168 }
169 if (state == Pointer::ButtonState::Released) {
170 return;
171 }
172 if (button == BTN_LEFT) {
173 m_windowManagement->showDesktop();
174 } else if (button == BTN_RIGHT) {
175 m_windowManagement->hideDesktop();
176 }
177 });
178 connect(p, &Pointer::entered, this, [this, p](quint32 serial, const QPointF &relativeToSurface) {
179 if (p->enteredSurface() == m_surface) {
180 showTooltip(relativeToSurface);
181 }
182 });
183 connect(p, &Pointer::motion, this, [this, p](const QPointF &relativeToSurface) {
184 if (p->enteredSurface() == m_surface) {
185 moveTooltip(relativeToSurface);
186 }
187 });
188 connect(p, &Pointer::left, this, [this] {
189 hideTooltip();
190 });
191 });
192 });
193 connect(registry, &Registry::plasmaShellAnnounced, this, [this, registry](quint32 name, quint32 version) {
194 m_plasmaShell = registry->createPlasmaShell(name, version, this);
195 });
196 connect(registry, &Registry::plasmaWindowManagementAnnounced, this, [this, registry](quint32 name, quint32 version) {
197 m_windowManagement = registry->createPlasmaWindowManagement(name, version, this);
198 connect(m_windowManagement, &PlasmaWindowManagement::showingDesktopChanged, this, [](bool set) {
199 qDebug() << "Showing desktop changed, new state: " << set;
200 });
201 connect(m_windowManagement, &PlasmaWindowManagement::windowCreated, this, [this](PlasmaWindow *w) {
202 connect(w, &PlasmaWindow::titleChanged, this, [w] {
203 qDebug() << "Window title changed to: " << w->title();
204 });
205 connect(w, &PlasmaWindow::activeChanged, this, [w] {
206 qDebug() << "Window active changed: " << w->isActive();
207 });
208 connect(w, &PlasmaWindow::maximizedChanged, this, [w] {
209 qDebug() << "Window maximized changed: " << w->isMaximized();
210 });
211 connect(w, &PlasmaWindow::maximizedChanged, this, [w] {
212 qDebug() << "Window minimized changed: " << w->isMinimized();
213 });
214 connect(w, &PlasmaWindow::keepAboveChanged, this, [w] {
215 qDebug() << "Window keep above changed: " << w->isKeepAbove();
216 });
217 connect(w, &PlasmaWindow::keepBelowChanged, this, [w] {
218 qDebug() << "Window keep below changed: " << w->isKeepBelow();
219 });
220 connect(w, &PlasmaWindow::onAllDesktopsChanged, this, [w] {
221 qDebug() << "Window on all desktops changed: " << w->isOnAllDesktops();
222 });
223 connect(w, &PlasmaWindow::fullscreenChanged, this, [w] {
224 qDebug() << "Window full screen changed: " << w->isFullscreen();
225 });
226 connect(w, &PlasmaWindow::demandsAttentionChanged, this, [w] {
227 qDebug() << "Window demands attention changed: " << w->isDemandingAttention();
228 });
229 connect(w, &PlasmaWindow::closeableChanged, this, [w] {
230 qDebug() << "Window is closeable changed: " << w->isCloseable();
231 });
232 connect(w, &PlasmaWindow::minimizeableChanged, this, [w] {
233 qDebug() << "Window is minimizeable changed: " << w->isMinimizeable();
234 });
235 connect(w, &PlasmaWindow::maximizeableChanged, this, [w] {
236 qDebug() << "Window is maximizeable changed: " << w->isMaximizeable();
237 });
238 connect(w, &PlasmaWindow::fullscreenableChanged, this, [w] {
239 qDebug() << "Window is fullscreenable changed: " << w->isFullscreenable();
240 });
241 connect(w, &PlasmaWindow::iconChanged, this, [w] {
242 qDebug() << "Window icon changed: " << w->icon().name();
243 });
244 });
245 });
246 connect(registry, &Registry::interfacesAnnounced, this, [this] {
247 Q_ASSERT(m_compositor);
248 Q_ASSERT(m_seat);
249 Q_ASSERT(m_shell);
250 Q_ASSERT(m_shm);
251 m_surface = m_compositor->createSurface(this);
252 Q_ASSERT(m_surface);
253 m_shellSurface = m_shell->createSurface(m_surface, this);
254 Q_ASSERT(m_shellSurface);
255 m_shellSurface->setToplevel();
256 connect(m_shellSurface, &ShellSurface::sizeChanged, this, &PanelTest::render);
257 if (m_plasmaShell) {
258 m_plasmaShellSurface = m_plasmaShell->createSurface(m_surface, this);
259 m_plasmaShellSurface->setPosition(QPoint(10, 0));
260 m_plasmaShellSurface->setRole(PlasmaShellSurface::Role::Panel);
261 }
262 render();
263 });
264 registry->setEventQueue(m_eventQueue);
265 registry->create(m_connectionThreadObject);
266 registry->setup();
267}
268
269void PanelTest::render()
270{
271 const QSize &size = m_shellSurface->size().isValid() ? m_shellSurface->size() : QSize(300, 20);
272 auto buffer = m_shm->getBuffer(size, size.width() * 4).toStrongRef();
273 buffer->setUsed(true);
274 QImage image(buffer->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied);
275 image.fill(Qt::blue);
276
277 m_surface->attachBuffer(*buffer);
278 m_surface->damage(QRect(QPoint(0, 0), size));
279 m_surface->commit(Surface::CommitFlag::None);
280 buffer->setUsed(false);
281}
282
283int main(int argc, char **argv)
284{
285 QGuiApplication app(argc, argv);
286 PanelTest client;
287 client.init();
288
289 return app.exec();
290}
291
292#include "paneltest.moc"
void init()
Definition paneltest.cpp:85
Surface * surface
Definition paneltest.cpp:64
PlasmaShellSurface * plasmaSurface
Definition paneltest.cpp:66
bool visible
Definition paneltest.cpp:67
virtual ~PanelTest()
Definition paneltest.cpp:78
ShellSurface * shellSurface
Definition paneltest.cpp:65
PanelTest(QObject *parent=nullptr)
Definition paneltest.cpp:71
KWayland::Client::Registry * registry
constexpr int version