File indexing completed on 2024-05-19 16:33:10

0001 /*
0002     SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 #include "qtwaylandintegrationtest.h"
0007 // KWin::Wayland
0008 #include <../src/client/buffer.h>
0009 #include <../src/client/compositor.h>
0010 #include <../src/client/connection_thread.h>
0011 #include <../src/client/pointer.h>
0012 #include <../src/client/registry.h>
0013 #include <../src/client/shell.h>
0014 #include <../src/client/shm_pool.h>
0015 #include <../src/client/surface.h>
0016 // Qt
0017 #include <QAbstractEventDispatcher>
0018 #include <QGuiApplication>
0019 #include <QImage>
0020 #include <QPainter>
0021 #include <QTimer>
0022 
0023 #include <linux/input.h>
0024 
0025 using namespace KWayland::Client;
0026 
0027 static Qt::GlobalColor s_colors[] = {Qt::white, Qt::red, Qt::green, Qt::blue, Qt::black};
0028 static int s_colorIndex = 0;
0029 
0030 WaylandClientTest::WaylandClientTest(QObject *parent)
0031     : QObject(parent)
0032     , m_connectionThreadObject(ConnectionThread::fromApplication(this))
0033     , m_compositor(Compositor::fromApplication(this))
0034     , m_surface(nullptr)
0035     , m_shm(nullptr)
0036     , m_shellSurface(nullptr)
0037     , m_timer(new QTimer(this))
0038 {
0039     init();
0040 }
0041 
0042 WaylandClientTest::~WaylandClientTest() = default;
0043 
0044 void WaylandClientTest::init()
0045 {
0046     connect(m_timer, &QTimer::timeout, this, [this]() {
0047         s_colorIndex = (s_colorIndex + 1) % 5;
0048         render();
0049     });
0050     m_timer->setInterval(1000);
0051     m_timer->start();
0052 
0053     m_surface = m_compositor->createSurface(this);
0054     Registry *registry = new Registry(this);
0055     setupRegistry(registry);
0056 }
0057 
0058 void WaylandClientTest::setupRegistry(Registry *registry)
0059 {
0060     connect(registry, &Registry::shellAnnounced, this, [this, registry](quint32 name) {
0061         Shell *shell = registry->createShell(name, 1, this);
0062         m_shellSurface = shell->createSurface(m_surface, m_surface);
0063         connect(m_shellSurface, &ShellSurface::sizeChanged, this, static_cast<void (WaylandClientTest::*)(const QSize &)>(&WaylandClientTest::render));
0064         render(QSize(200, 200));
0065     });
0066     connect(registry, &Registry::shmAnnounced, this, [this, registry](quint32 name) {
0067         m_shm = registry->createShmPool(name, 1, this);
0068     });
0069     registry->create(m_connectionThreadObject->display());
0070     registry->setup();
0071 }
0072 
0073 void WaylandClientTest::render(const QSize &size)
0074 {
0075     m_currentSize = size;
0076     render();
0077 }
0078 
0079 void WaylandClientTest::render()
0080 {
0081     if (!m_shm || !m_surface || !m_surface->isValid() || !m_currentSize.isValid()) {
0082         return;
0083     }
0084     auto buffer = m_shm->getBuffer(m_currentSize, m_currentSize.width() * 4).toStrongRef();
0085     buffer->setUsed(true);
0086     QImage image(buffer->address(), m_currentSize.width(), m_currentSize.height(), QImage::Format_ARGB32_Premultiplied);
0087     image.fill(s_colors[s_colorIndex]);
0088 
0089     m_surface->attachBuffer(*buffer);
0090     m_surface->damage(QRect(QPoint(0, 0), m_currentSize));
0091     m_surface->commit(Surface::CommitFlag::None);
0092     buffer->setUsed(false);
0093 }
0094 
0095 int main(int argc, char **argv)
0096 {
0097     qputenv("QT_QPA_PLATFORM", "wayland");
0098     QGuiApplication app(argc, argv);
0099 
0100     new WaylandClientTest(&app);
0101 
0102     return app.exec();
0103 }
0104 
0105 #include "moc_qtwaylandintegrationtest.cpp"