2014-08-21 10:57:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "waylandclienttest.h"
|
|
|
|
// KWin::Wayland
|
2014-09-19 11:59:51 +00:00
|
|
|
#include <KWayland/Client/buffer.h>
|
|
|
|
#include <KWayland/Client/compositor.h>
|
|
|
|
#include <KWayland/Client/connection_thread.h>
|
2015-03-23 08:54:37 +00:00
|
|
|
#include <KWayland/Client/event_queue.h>
|
2014-09-19 11:59:51 +00:00
|
|
|
#include <KWayland/Client/keyboard.h>
|
|
|
|
#include <KWayland/Client/output.h>
|
|
|
|
#include <KWayland/Client/pointer.h>
|
|
|
|
#include <KWayland/Client/registry.h>
|
|
|
|
#include <KWayland/Client/seat.h>
|
|
|
|
#include <KWayland/Client/shell.h>
|
|
|
|
#include <KWayland/Client/shm_pool.h>
|
|
|
|
#include <KWayland/Client/surface.h>
|
2014-08-21 10:57:50 +00:00
|
|
|
// Qt
|
|
|
|
#include <QAbstractEventDispatcher>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
#include <linux/input.h>
|
|
|
|
|
2014-09-19 11:59:51 +00:00
|
|
|
using namespace KWayland::Client;
|
2014-08-21 10:57:50 +00:00
|
|
|
|
|
|
|
static Qt::GlobalColor s_colors[] = {
|
|
|
|
Qt::white,
|
|
|
|
Qt::red,
|
|
|
|
Qt::green,
|
|
|
|
Qt::blue,
|
|
|
|
Qt::black
|
|
|
|
};
|
|
|
|
static int s_colorIndex = 0;
|
|
|
|
|
|
|
|
WaylandClientTest::WaylandClientTest(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_connectionThread(new QThread(this))
|
|
|
|
, m_connectionThreadObject(new ConnectionThread(nullptr))
|
|
|
|
, m_eventQueue(nullptr)
|
2015-03-23 08:54:37 +00:00
|
|
|
, m_compositor(nullptr)
|
|
|
|
, m_output(nullptr)
|
2014-08-21 10:57:50 +00:00
|
|
|
, m_surface(nullptr)
|
2015-03-23 08:54:37 +00:00
|
|
|
, m_shm(nullptr)
|
2014-08-21 10:57:50 +00:00
|
|
|
, m_timer(new QTimer(this))
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
WaylandClientTest::~WaylandClientTest()
|
|
|
|
{
|
|
|
|
m_connectionThread->quit();
|
|
|
|
m_connectionThread->wait();
|
|
|
|
m_connectionThreadObject->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClientTest::init()
|
|
|
|
{
|
|
|
|
connect(m_connectionThreadObject, &ConnectionThread::connected, this,
|
|
|
|
[this]() {
|
|
|
|
// create the event queue for the main gui thread
|
2015-03-23 08:54:37 +00:00
|
|
|
m_eventQueue = new EventQueue(this);
|
|
|
|
m_eventQueue->setup(m_connectionThreadObject);
|
2014-08-21 10:57:50 +00:00
|
|
|
// setup registry
|
|
|
|
Registry *registry = new Registry(this);
|
|
|
|
setupRegistry(registry);
|
|
|
|
},
|
|
|
|
Qt::QueuedConnection);
|
|
|
|
|
|
|
|
m_connectionThreadObject->moveToThread(m_connectionThread);
|
|
|
|
m_connectionThread->start();
|
|
|
|
|
|
|
|
m_connectionThreadObject->initConnection();
|
|
|
|
|
|
|
|
connect(m_timer, &QTimer::timeout, this,
|
|
|
|
[this]() {
|
|
|
|
s_colorIndex = (s_colorIndex + 1) % 5;
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
m_timer->setInterval(1000);
|
|
|
|
m_timer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClientTest::setupRegistry(Registry *registry)
|
|
|
|
{
|
|
|
|
connect(registry, &Registry::compositorAnnounced, this,
|
|
|
|
[this, registry](quint32 name) {
|
2015-03-23 08:54:37 +00:00
|
|
|
m_compositor = registry->createCompositor(name, 1, this);
|
2014-08-21 10:57:50 +00:00
|
|
|
m_surface = m_compositor->createSurface(this);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
connect(registry, &Registry::shellAnnounced, this,
|
|
|
|
[this, registry](quint32 name) {
|
2015-03-23 08:54:37 +00:00
|
|
|
Shell *shell = registry->createShell(name, 1, this);
|
2014-08-21 10:57:50 +00:00
|
|
|
ShellSurface *shellSurface = shell->createSurface(m_surface, m_surface);
|
|
|
|
shellSurface->setFullscreen(m_output);
|
|
|
|
connect(shellSurface, &ShellSurface::sizeChanged, this, static_cast<void(WaylandClientTest::*)(const QSize&)>(&WaylandClientTest::render));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
connect(registry, &Registry::outputAnnounced, this,
|
|
|
|
[this, registry](quint32 name) {
|
2015-03-23 08:54:37 +00:00
|
|
|
if (m_output) {
|
2014-08-21 10:57:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-03-23 08:54:37 +00:00
|
|
|
m_output = registry->createOutput(name, 2, this);
|
2014-08-21 10:57:50 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
connect(registry, &Registry::shmAnnounced, this,
|
|
|
|
[this, registry](quint32 name) {
|
2015-03-23 08:54:37 +00:00
|
|
|
m_shm = registry->createShmPool(name, 1, this);
|
2014-08-21 10:57:50 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
connect(registry, &Registry::seatAnnounced, this,
|
|
|
|
[this, registry](quint32 name) {
|
2015-03-23 08:54:37 +00:00
|
|
|
Seat *s = registry->createSeat(name, 2, this);
|
2014-08-21 10:57:50 +00:00
|
|
|
connect(s, &Seat::hasKeyboardChanged, this,
|
|
|
|
[this, s](bool has) {
|
|
|
|
if (!has) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Keyboard *k = s->createKeyboard(this);
|
|
|
|
connect(k, &Keyboard::keyChanged, this,
|
|
|
|
[this](quint32 key, Keyboard::KeyState state) {
|
|
|
|
if (key == KEY_Q && state == Keyboard::KeyState::Released) {
|
|
|
|
QCoreApplication::instance()->quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
connect(s, &Seat::hasPointerChanged, this,
|
|
|
|
[this, s](bool has) {
|
|
|
|
if (!has) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Pointer *p = s->createPointer(this);
|
|
|
|
connect(p, &Pointer::buttonStateChanged, this,
|
|
|
|
[this](quint32 serial, quint32 time, quint32 button, Pointer::ButtonState state) {
|
|
|
|
Q_UNUSED(serial)
|
|
|
|
Q_UNUSED(time)
|
|
|
|
if (state == Pointer::ButtonState::Released) {
|
|
|
|
if (button == BTN_LEFT) {
|
|
|
|
if (m_timer->isActive()) {
|
|
|
|
m_timer->stop();
|
|
|
|
} else {
|
|
|
|
m_timer->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (button == BTN_RIGHT) {
|
|
|
|
QCoreApplication::instance()->quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
registry->create(m_connectionThreadObject->display());
|
2015-03-23 08:54:37 +00:00
|
|
|
registry->setEventQueue(m_eventQueue);
|
2014-08-21 10:57:50 +00:00
|
|
|
registry->setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClientTest::render(const QSize &size)
|
|
|
|
{
|
|
|
|
m_currentSize = size;
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClientTest::render()
|
|
|
|
{
|
|
|
|
if (!m_shm || !m_surface || !m_surface->isValid() || !m_currentSize.isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-22 07:59:58 +00:00
|
|
|
auto buffer = m_shm->getBuffer(m_currentSize, m_currentSize.width() * 4).toStrongRef();
|
2014-08-21 10:57:50 +00:00
|
|
|
buffer->setUsed(true);
|
|
|
|
QImage image(buffer->address(), m_currentSize.width(), m_currentSize.height(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
image.fill(s_colors[s_colorIndex]);
|
|
|
|
|
|
|
|
QPainter p;
|
|
|
|
p.begin(&image);
|
|
|
|
QImage icon(QStringLiteral(SOURCE_DIR) + QStringLiteral("/48-apps-kwin.png"));
|
|
|
|
p.drawImage(QPoint(0, 0), icon);
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
m_surface->attachBuffer(*buffer);
|
|
|
|
m_surface->damage(QRect(QPoint(0, 0), m_currentSize));
|
|
|
|
m_surface->commit(Surface::CommitFlag::None);
|
|
|
|
buffer->setUsed(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
|
|
|
|
new WaylandClientTest(&app);
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|