File indexing completed on 2024-05-19 05:30:34
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 <../src/client/buffer.h> 0009 #include <../src/client/compositor.h> 0010 #include <../src/client/connection_thread.h> 0011 #include <../src/client/event_queue.h> 0012 #include <../src/client/keyboard.h> 0013 #include <../src/client/output.h> 0014 #include <../src/client/pointer.h> 0015 #include <../src/client/registry.h> 0016 #include <../src/client/seat.h> 0017 #include <../src/client/shell.h> 0018 #include <../src/client/shm_pool.h> 0019 #include <../src/client/surface.h> 0020 #include <../src/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 Q_UNUSED(serial) 0128 Q_UNUSED(time) 0129 if (state == Pointer::ButtonState::Released) { 0130 if (button == BTN_LEFT) { 0131 if (m_timer->isActive()) { 0132 m_timer->stop(); 0133 } else { 0134 m_timer->start(); 0135 } 0136 } 0137 if (button == BTN_RIGHT) { 0138 QCoreApplication::instance()->quit(); 0139 } 0140 } 0141 }); 0142 }); 0143 connect(s, &Seat::hasTouchChanged, this, [this, s](bool has) { 0144 if (!has) { 0145 return; 0146 } 0147 Touch *t = s->createTouch(this); 0148 connect(t, &Touch::sequenceStarted, this, [](KWayland::Client::TouchPoint *startPoint) { 0149 qDebug() << "Touch sequence started at" << startPoint->position() << "with id" << startPoint->id(); 0150 }); 0151 connect(t, &Touch::sequenceCanceled, this, []() { 0152 qDebug() << "Touch sequence canceled"; 0153 }); 0154 connect(t, &Touch::sequenceEnded, this, []() { 0155 qDebug() << "Touch sequence finished"; 0156 }); 0157 connect(t, &Touch::frameEnded, this, []() { 0158 qDebug() << "End of touch contact point list"; 0159 }); 0160 connect(t, &Touch::pointAdded, this, [](KWayland::Client::TouchPoint *point) { 0161 qDebug() << "Touch point added at" << point->position() << "with id" << point->id(); 0162 }); 0163 connect(t, &Touch::pointRemoved, this, [](KWayland::Client::TouchPoint *point) { 0164 qDebug() << "Touch point " << point->id() << " removed at" << point->position(); 0165 }); 0166 connect(t, &Touch::pointMoved, this, [](KWayland::Client::TouchPoint *point) { 0167 qDebug() << "Touch point " << point->id() << " moved to" << point->position(); 0168 }); 0169 }); 0170 }); 0171 registry->create(m_connectionThreadObject->display()); 0172 registry->setEventQueue(m_eventQueue); 0173 registry->setup(); 0174 } 0175 0176 void WaylandClientTest::render(const QSize &size) 0177 { 0178 m_currentSize = size; 0179 render(); 0180 } 0181 0182 void WaylandClientTest::render() 0183 { 0184 if (!m_shm || !m_surface || !m_surface->isValid() || !m_currentSize.isValid()) { 0185 return; 0186 } 0187 auto buffer = m_shm->getBuffer(m_currentSize, m_currentSize.width() * 4).toStrongRef(); 0188 buffer->setUsed(true); 0189 QImage image(buffer->address(), m_currentSize.width(), m_currentSize.height(), QImage::Format_ARGB32_Premultiplied); 0190 image.fill(s_colors[s_colorIndex]); 0191 0192 m_surface->attachBuffer(*buffer); 0193 m_surface->damage(QRect(QPoint(0, 0), m_currentSize)); 0194 m_surface->commit(Surface::CommitFlag::None); 0195 buffer->setUsed(false); 0196 } 0197 0198 int main(int argc, char **argv) 0199 { 0200 QCoreApplication app(argc, argv); 0201 0202 new WaylandClientTest(&app); 0203 0204 return app.exec(); 0205 } 0206 0207 #include "moc_touchclienttest.cpp"