File indexing completed on 2024-05-05 05:33:36

0001 /*
0002     SPDX-FileCopyrightText: 2014, 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 "touchclienttest.h"
0007 // KWin::Wayland
0008 #include <KWayland/Client/buffer.h>
0009 #include <KWayland/Client/compositor.h>
0010 #include <KWayland/Client/connection_thread.h>
0011 #include <KWayland/Client/event_queue.h>
0012 #include <KWayland/Client/keyboard.h>
0013 #include <KWayland/Client/output.h>
0014 #include <KWayland/Client/pointer.h>
0015 #include <KWayland/Client/registry.h>
0016 #include <KWayland/Client/seat.h>
0017 #include <KWayland/Client/shell.h>
0018 #include <KWayland/Client/shm_pool.h>
0019 #include <KWayland/Client/surface.h>
0020 #include <KWayland/Client/touch.h>
0021 // Qt
0022 #include <QAbstractEventDispatcher>
0023 #include <QCoreApplication>
0024 #include <QDebug>
0025 #include <QImage>
0026 #include <QPainter>
0027 #include <QThread>
0028 #include <QTimer>
0029 
0030 #include <linux/input.h>
0031 
0032 using namespace KWayland::Client;
0033 
0034 static Qt::GlobalColor s_colors[] = {Qt::white, Qt::red, Qt::green, Qt::blue, Qt::black};
0035 static int s_colorIndex = 0;
0036 
0037 WaylandClientTest::WaylandClientTest(QObject *parent)
0038     : QObject(parent)
0039     , m_connectionThread(new QThread(this))
0040     , m_connectionThreadObject(new ConnectionThread(nullptr))
0041     , m_eventQueue(nullptr)
0042     , m_compositor(nullptr)
0043     , m_output(nullptr)
0044     , m_surface(nullptr)
0045     , m_shm(nullptr)
0046     , m_timer(new QTimer(this))
0047 {
0048     init();
0049 }
0050 
0051 WaylandClientTest::~WaylandClientTest()
0052 {
0053     m_connectionThread->quit();
0054     m_connectionThread->wait();
0055     m_connectionThreadObject->deleteLater();
0056 }
0057 
0058 void WaylandClientTest::init()
0059 {
0060     connect(
0061         m_connectionThreadObject,
0062         &ConnectionThread::connected,
0063         this,
0064         [this]() {
0065             // create the event queue for the main gui thread
0066             m_eventQueue = new EventQueue(this);
0067             m_eventQueue->setup(m_connectionThreadObject);
0068             // setup registry
0069             Registry *registry = new Registry(this);
0070             setupRegistry(registry);
0071         },
0072         Qt::QueuedConnection);
0073 
0074     m_connectionThreadObject->moveToThread(m_connectionThread);
0075     m_connectionThread->start();
0076 
0077     m_connectionThreadObject->initConnection();
0078 
0079     connect(m_timer, &QTimer::timeout, this, [this]() {
0080         s_colorIndex = (s_colorIndex + 1) % 5;
0081         render();
0082     });
0083     m_timer->setInterval(1000);
0084     m_timer->start();
0085 }
0086 
0087 void WaylandClientTest::setupRegistry(Registry *registry)
0088 {
0089     connect(registry, &Registry::compositorAnnounced, this, [this, registry](quint32 name) {
0090         m_compositor = registry->createCompositor(name, 1, this);
0091         m_surface = m_compositor->createSurface(this);
0092     });
0093     connect(registry, &Registry::shellAnnounced, this, [this, registry](quint32 name) {
0094         Shell *shell = registry->createShell(name, 1, this);
0095         ShellSurface *shellSurface = shell->createSurface(m_surface, m_surface);
0096         shellSurface->setToplevel();
0097         render(QSize(400, 200));
0098     });
0099     connect(registry, &Registry::outputAnnounced, this, [this, registry](quint32 name) {
0100         if (m_output) {
0101             return;
0102         }
0103         m_output = registry->createOutput(name, 2, this);
0104     });
0105     connect(registry, &Registry::shmAnnounced, this, [this, registry](quint32 name) {
0106         m_shm = registry->createShmPool(name, 1, this);
0107     });
0108     connect(registry, &Registry::seatAnnounced, this, [this, registry](quint32 name) {
0109         Seat *s = registry->createSeat(name, 2, this);
0110         connect(s, &Seat::hasKeyboardChanged, this, [this, s](bool has) {
0111             if (!has) {
0112                 return;
0113             }
0114             Keyboard *k = s->createKeyboard(this);
0115             connect(k, &Keyboard::keyChanged, this, [](quint32 key, Keyboard::KeyState state) {
0116                 if (key == KEY_Q && state == Keyboard::KeyState::Released) {
0117                     QCoreApplication::instance()->quit();
0118                 }
0119             });
0120         });
0121         connect(s, &Seat::hasPointerChanged, this, [this, s](bool has) {
0122             if (!has) {
0123                 return;
0124             }
0125             Pointer *p = s->createPointer(this);
0126             connect(p, &Pointer::buttonStateChanged, this, [this](quint32 serial, quint32 time, quint32 button, Pointer::ButtonState state) {
0127                 if (state == Pointer::ButtonState::Released) {
0128                     if (button == BTN_LEFT) {
0129                         if (m_timer->isActive()) {
0130                             m_timer->stop();
0131                         } else {
0132                             m_timer->start();
0133                         }
0134                     }
0135                     if (button == BTN_RIGHT) {
0136                         QCoreApplication::instance()->quit();
0137                     }
0138                 }
0139             });
0140         });
0141         connect(s, &Seat::hasTouchChanged, this, [this, s](bool has) {
0142             if (!has) {
0143                 return;
0144             }
0145             Touch *t = s->createTouch(this);
0146             connect(t, &Touch::sequenceStarted, this, [](KWayland::Client::TouchPoint *startPoint) {
0147                 qDebug() << "Touch sequence started at" << startPoint->position() << "with id" << startPoint->id();
0148             });
0149             connect(t, &Touch::sequenceCanceled, this, []() {
0150                 qDebug() << "Touch sequence canceled";
0151             });
0152             connect(t, &Touch::sequenceEnded, this, []() {
0153                 qDebug() << "Touch sequence finished";
0154             });
0155             connect(t, &Touch::frameEnded, this, []() {
0156                 qDebug() << "End of touch contact point list";
0157             });
0158             connect(t, &Touch::pointAdded, this, [](KWayland::Client::TouchPoint *point) {
0159                 qDebug() << "Touch point added at" << point->position() << "with id" << point->id();
0160             });
0161             connect(t, &Touch::pointRemoved, this, [](KWayland::Client::TouchPoint *point) {
0162                 qDebug() << "Touch point " << point->id() << " removed at" << point->position();
0163             });
0164             connect(t, &Touch::pointMoved, this, [](KWayland::Client::TouchPoint *point) {
0165                 qDebug() << "Touch point " << point->id() << " moved to" << point->position();
0166             });
0167         });
0168     });
0169     registry->create(m_connectionThreadObject->display());
0170     registry->setEventQueue(m_eventQueue);
0171     registry->setup();
0172 }
0173 
0174 void WaylandClientTest::render(const QSize &size)
0175 {
0176     m_currentSize = size;
0177     render();
0178 }
0179 
0180 void WaylandClientTest::render()
0181 {
0182     if (!m_shm || !m_surface || !m_surface->isValid() || !m_currentSize.isValid()) {
0183         return;
0184     }
0185     auto buffer = m_shm->getBuffer(m_currentSize, m_currentSize.width() * 4).toStrongRef();
0186     buffer->setUsed(true);
0187     QImage image(buffer->address(), m_currentSize.width(), m_currentSize.height(), QImage::Format_ARGB32_Premultiplied);
0188     image.fill(s_colors[s_colorIndex]);
0189 
0190     m_surface->attachBuffer(*buffer);
0191     m_surface->damage(QRect(QPoint(0, 0), m_currentSize));
0192     m_surface->commit(Surface::CommitFlag::None);
0193     buffer->setUsed(false);
0194 }
0195 
0196 int main(int argc, char **argv)
0197 {
0198     QCoreApplication app(argc, argv);
0199 
0200     new WaylandClientTest(&app);
0201 
0202     return app.exec();
0203 }
0204 
0205 #include "moc_touchclienttest.cpp"