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 "qtwaylandintegrationtest.h"
7// KWin::Wayland
8#include <../src/client/buffer.h>
9#include <../src/client/compositor.h>
10#include <../src/client/connection_thread.h>
11#include <../src/client/pointer.h>
12#include <../src/client/registry.h>
13#include <../src/client/shell.h>
14#include <../src/client/shm_pool.h>
15#include <../src/client/surface.h>
16// Qt
17#include <QAbstractEventDispatcher>
18#include <QGuiApplication>
19#include <QImage>
20#include <QPainter>
21#include <QTimer>
22
23#include <linux/input.h>
24
25using namespace KWayland::Client;
26
27static Qt::GlobalColor s_colors[] = {Qt::white, Qt::red, Qt::green, Qt::blue, Qt::black};
28static int s_colorIndex = 0;
29
30WaylandClientTest::WaylandClientTest(QObject *parent)
31 : QObject(parent)
32 , m_connectionThreadObject(ConnectionThread::fromApplication(parent: this))
33 , m_compositor(Compositor::fromApplication(parent: this))
34 , m_surface(nullptr)
35 , m_shm(nullptr)
36 , m_shellSurface(nullptr)
37 , m_timer(new QTimer(this))
38{
39 init();
40}
41
42WaylandClientTest::~WaylandClientTest() = default;
43
44void WaylandClientTest::init()
45{
46 connect(sender: m_timer, signal: &QTimer::timeout, context: this, slot: [this]() {
47 s_colorIndex = (s_colorIndex + 1) % 5;
48 render();
49 });
50 m_timer->setInterval(1000);
51 m_timer->start();
52
53 m_surface = m_compositor->createSurface(parent: this);
54 Registry *registry = new Registry(this);
55 setupRegistry(registry);
56}
57
58void WaylandClientTest::setupRegistry(Registry *registry)
59{
60 connect(sender: registry, signal: &Registry::shellAnnounced, context: this, slot: [this, registry](quint32 name) {
61 Shell *shell = registry->createShell(name, version: 1, parent: this);
62 m_shellSurface = shell->createSurface(surface: m_surface, parent: m_surface);
63 connect(sender: m_shellSurface, signal: &ShellSurface::sizeChanged, context: this, slot: static_cast<void (WaylandClientTest::*)(const QSize &)>(&WaylandClientTest::render));
64 render(size: QSize(200, 200));
65 });
66 connect(sender: registry, signal: &Registry::shmAnnounced, context: this, slot: [this, registry](quint32 name) {
67 m_shm = registry->createShmPool(name, version: 1, parent: this);
68 });
69 registry->create(display: m_connectionThreadObject->display());
70 registry->setup();
71}
72
73void WaylandClientTest::render(const QSize &size)
74{
75 m_currentSize = size;
76 render();
77}
78
79void WaylandClientTest::render()
80{
81 if (!m_shm || !m_surface || !m_surface->isValid() || !m_currentSize.isValid()) {
82 return;
83 }
84 auto buffer = m_shm->getBuffer(size: m_currentSize, stride: m_currentSize.width() * 4).toStrongRef();
85 buffer->setUsed(true);
86 QImage image(buffer->address(), m_currentSize.width(), m_currentSize.height(), QImage::Format_ARGB32_Premultiplied);
87 image.fill(color: s_colors[s_colorIndex]);
88
89 m_surface->attachBuffer(buffer: *buffer);
90 m_surface->damage(rect: QRect(QPoint(0, 0), m_currentSize));
91 m_surface->commit(flag: Surface::CommitFlag::None);
92 buffer->setUsed(false);
93}
94
95int main(int argc, char **argv)
96{
97 qputenv(varName: "QT_QPA_PLATFORM", value: "wayland");
98 QGuiApplication app(argc, argv);
99
100 new WaylandClientTest(&app);
101
102 return app.exec();
103}
104
105#include "moc_qtwaylandintegrationtest.cpp"
106

source code of kwayland/tests/qtwaylandintegrationtest.cpp