diff --git a/autotests/client/test_wayland_shell.cpp b/autotests/client/test_wayland_shell.cpp index 55db68b..15a873c 100644 --- a/autotests/client/test_wayland_shell.cpp +++ b/autotests/client/test_wayland_shell.cpp @@ -1,745 +1,750 @@ /******************************************************************** Copyright 2014 Martin Gräßlin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 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 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . *********************************************************************/ // Qt #include // KWin #include "../../src/client/compositor.h" #include "../../src/client/connection_thread.h" #include "../../src/client/event_queue.h" #include "../../src/client/pointer.h" #include "../../src/client/seat.h" #include "../../src/client/shell.h" #include "../../src/client/surface.h" #include "../../src/client/registry.h" #include "../../src/server/buffer_interface.h" #include "../../src/server/compositor_interface.h" #include "../../src/server/display.h" #include "../../src/server/seat_interface.h" #include "../../src/server/shell_interface.h" #include "../../src/server/surface_interface.h" // Wayland #include Q_DECLARE_METATYPE(Qt::Edges) class TestWaylandShell : public QObject { Q_OBJECT public: explicit TestWaylandShell(QObject *parent = nullptr); private Q_SLOTS: void initTestCase(); void init(); void cleanup(); void testCreateMultiple(); void testFullscreen(); void testMaximize(); void testToplevel(); void testTransient_data(); void testTransient(); void testPing(); void testTitle(); void testWindowClass(); void testDestroy(); void testCast(); void testMove(); void testResize_data(); void testResize(); void testDisconnect(); private: KWayland::Server::Display *m_display; KWayland::Server::CompositorInterface *m_compositorInterface; KWayland::Server::ShellInterface *m_shellInterface; KWayland::Server::SeatInterface *m_seatInterface; KWayland::Client::ConnectionThread *m_connection; KWayland::Client::Compositor *m_compositor; KWayland::Client::Shell *m_shell; KWayland::Client::Seat *m_seat; KWayland::Client::Pointer *m_pointer; KWayland::Client::EventQueue *m_queue; QThread *m_thread; }; static const QString s_socketName = QStringLiteral("kwin-test-wayland-shell-0"); TestWaylandShell::TestWaylandShell(QObject *parent) : QObject(parent) , m_display(nullptr) , m_compositorInterface(nullptr) , m_shellInterface(nullptr) , m_seatInterface(nullptr) , m_connection(nullptr) , m_compositor(nullptr) , m_shell(nullptr) , m_seat(nullptr) , m_pointer(nullptr) , m_queue(nullptr) , m_thread(nullptr) { } void TestWaylandShell::initTestCase() { qRegisterMetaType(); } void TestWaylandShell::init() { using namespace KWayland::Server; delete m_display; m_display = new Display(this); m_display->setSocketName(s_socketName); m_display->start(); QVERIFY(m_display->isRunning()); m_compositorInterface = m_display->createCompositor(m_display); QVERIFY(m_compositorInterface); m_compositorInterface->create(); QVERIFY(m_compositorInterface->isValid()); m_shellInterface = m_display->createShell(m_display); QVERIFY(m_shellInterface); m_shellInterface->create(); QVERIFY(m_shellInterface->isValid()); m_seatInterface = m_display->createSeat(m_display); QVERIFY(m_seatInterface); m_seatInterface->setHasPointer(true); m_seatInterface->create(); QVERIFY(m_seatInterface->isValid()); // setup connection m_connection = new KWayland::Client::ConnectionThread; QSignalSpy connectedSpy(m_connection, SIGNAL(connected())); m_connection->setSocketName(s_socketName); m_thread = new QThread(this); m_connection->moveToThread(m_thread); m_thread->start(); m_connection->initConnection(); QVERIFY(connectedSpy.wait()); m_queue = new KWayland::Client::EventQueue(this); QVERIFY(!m_queue->isValid()); m_queue->setup(m_connection); QVERIFY(m_queue->isValid()); using namespace KWayland::Client; KWayland::Client::Registry registry; QSignalSpy interfacesAnnouncedSpy(®istry, &Registry::interfacesAnnounced); QVERIFY(interfacesAnnouncedSpy.isValid()); QSignalSpy compositorSpy(®istry, SIGNAL(compositorAnnounced(quint32,quint32))); QSignalSpy shellSpy(®istry, SIGNAL(shellAnnounced(quint32,quint32))); QSignalSpy seatAnnouncedSpy(®istry, &Registry::seatAnnounced); QVERIFY(seatAnnouncedSpy.isValid()); QVERIFY(!registry.eventQueue()); registry.setEventQueue(m_queue); QCOMPARE(registry.eventQueue(), m_queue); registry.create(m_connection->display()); QVERIFY(registry.isValid()); registry.setup(); QVERIFY(interfacesAnnouncedSpy.wait()); m_compositor = new KWayland::Client::Compositor(this); m_compositor->setup(registry.bindCompositor(compositorSpy.first().first().value(), compositorSpy.first().last().value())); QVERIFY(m_compositor->isValid()); if (shellSpy.isEmpty()) { QVERIFY(shellSpy.wait()); } m_shell = registry.createShell(shellSpy.first().first().value(), shellSpy.first().last().value(), this); QVERIFY(m_shell->isValid()); QVERIFY(!seatAnnouncedSpy.isEmpty()); m_seat = registry.createSeat(seatAnnouncedSpy.first().first().value(), seatAnnouncedSpy.first().last().value(), this); QVERIFY(seatAnnouncedSpy.isValid()); QSignalSpy hasPointerSpy(m_seat, &Seat::hasPointerChanged); QVERIFY(hasPointerSpy.isValid()); QVERIFY(hasPointerSpy.wait()); QVERIFY(hasPointerSpy.first().first().toBool()); m_pointer = m_seat->createPointer(m_seat); QVERIFY(m_pointer->isValid()); } void TestWaylandShell::cleanup() { if (m_pointer) { delete m_pointer; m_pointer = nullptr; } if (m_seat) { delete m_seat; m_seat = nullptr; } if (m_shell) { delete m_shell; m_shell = nullptr; } if (m_compositor) { delete m_compositor; m_compositor = nullptr; } if (m_queue) { delete m_queue; m_queue = nullptr; } if (m_connection) { m_connection->deleteLater(); m_connection = nullptr; } if (m_thread) { m_thread->quit(); m_thread->wait(); delete m_thread; m_thread = nullptr; } delete m_seatInterface; m_seatInterface = nullptr; delete m_shellInterface; m_shellInterface = nullptr; delete m_compositorInterface; m_compositorInterface = nullptr; delete m_display; m_display = nullptr; } void TestWaylandShell::testCreateMultiple() { using namespace KWayland::Server; using namespace KWayland::Client; QScopedPointer s1(m_compositor->createSurface()); QScopedPointer s2(m_compositor->createSurface()); QVERIFY(!s1.isNull()); QVERIFY(s1->isValid()); QVERIFY(!s2.isNull()); QVERIFY(s2->isValid()); QSignalSpy serverSurfaceSpy(m_shellInterface, SIGNAL(surfaceCreated(KWayland::Server::ShellSurfaceInterface*))); QVERIFY(serverSurfaceSpy.isValid()); QScopedPointer surface1(m_shell->createSurface(s1.data())); QVERIFY(!surface1.isNull()); QVERIFY(surface1->isValid()); QVERIFY(serverSurfaceSpy.wait()); QCOMPARE(serverSurfaceSpy.count(), 1); QScopedPointer surface2(m_shell->createSurface(s2.data())); QVERIFY(!surface2.isNull()); QVERIFY(surface2->isValid()); QVERIFY(serverSurfaceSpy.wait()); QCOMPARE(serverSurfaceSpy.count(), 2); // try creating for one which already exist should not be possible QScopedPointer surface3(m_shell->createSurface(s2.data())); QVERIFY(!surface3.isNull()); QVERIFY(surface3->isValid()); QVERIFY(!serverSurfaceSpy.wait(100)); QCOMPARE(serverSurfaceSpy.count(), 2); } void TestWaylandShell::testFullscreen() { using namespace KWayland::Server; QScopedPointer s(m_compositor->createSurface()); QVERIFY(!s.isNull()); QVERIFY(s->isValid()); KWayland::Client::ShellSurface *surface = m_shell->createSurface(s.data(), m_shell); QSignalSpy sizeSpy(surface, SIGNAL(sizeChanged(QSize))); QVERIFY(sizeSpy.isValid()); QCOMPARE(surface->size(), QSize()); QSignalSpy serverSurfaceSpy(m_shellInterface, SIGNAL(surfaceCreated(KWayland::Server::ShellSurfaceInterface*))); QVERIFY(serverSurfaceSpy.isValid()); QVERIFY(serverSurfaceSpy.wait()); ShellSurfaceInterface *serverSurface = serverSurfaceSpy.first().first().value(); QVERIFY(serverSurface); QVERIFY(serverSurface->parentResource()); QSignalSpy fullscreenSpy(serverSurface, SIGNAL(fullscreenChanged(bool))); QVERIFY(fullscreenSpy.isValid()); surface->setFullscreen(); QVERIFY(fullscreenSpy.wait()); QCOMPARE(fullscreenSpy.count(), 1); QVERIFY(fullscreenSpy.first().first().toBool()); serverSurface->requestSize(QSize(1024, 768)); QVERIFY(sizeSpy.wait()); QCOMPARE(sizeSpy.count(), 1); QCOMPARE(sizeSpy.first().first().toSize(), QSize(1024, 768)); QCOMPARE(surface->size(), QSize(1024, 768)); // set back to toplevel fullscreenSpy.clear(); wl_shell_surface_set_toplevel(*surface); QVERIFY(fullscreenSpy.wait()); QCOMPARE(fullscreenSpy.count(), 1); QVERIFY(!fullscreenSpy.first().first().toBool()); } void TestWaylandShell::testMaximize() { using namespace KWayland::Server; QScopedPointer s(m_compositor->createSurface()); QVERIFY(!s.isNull()); QVERIFY(s->isValid()); KWayland::Client::ShellSurface *surface = m_shell->createSurface(s.data(), m_shell); QSignalSpy sizeSpy(surface, SIGNAL(sizeChanged(QSize))); QVERIFY(sizeSpy.isValid()); QCOMPARE(surface->size(), QSize()); QSignalSpy serverSurfaceSpy(m_shellInterface, SIGNAL(surfaceCreated(KWayland::Server::ShellSurfaceInterface*))); QVERIFY(serverSurfaceSpy.isValid()); QVERIFY(serverSurfaceSpy.wait()); ShellSurfaceInterface *serverSurface = serverSurfaceSpy.first().first().value(); QVERIFY(serverSurface); QVERIFY(serverSurface->parentResource()); QSignalSpy maximizedSpy(serverSurface, SIGNAL(maximizedChanged(bool))); QVERIFY(maximizedSpy.isValid()); surface->setMaximized(); QVERIFY(maximizedSpy.wait()); QCOMPARE(maximizedSpy.count(), 1); QVERIFY(maximizedSpy.first().first().toBool()); serverSurface->requestSize(QSize(1024, 768)); QVERIFY(sizeSpy.wait()); QCOMPARE(sizeSpy.count(), 1); QCOMPARE(sizeSpy.first().first().toSize(), QSize(1024, 768)); QCOMPARE(surface->size(), QSize(1024, 768)); // set back to toplevel maximizedSpy.clear(); wl_shell_surface_set_toplevel(*surface); QVERIFY(maximizedSpy.wait()); QCOMPARE(maximizedSpy.count(), 1); QVERIFY(!maximizedSpy.first().first().toBool()); } void TestWaylandShell::testToplevel() { using namespace KWayland::Server; QScopedPointer s(m_compositor->createSurface()); QVERIFY(!s.isNull()); QVERIFY(s->isValid()); KWayland::Client::ShellSurface *surface = m_shell->createSurface(s.data(), m_shell); QSignalSpy sizeSpy(surface, SIGNAL(sizeChanged(QSize))); QVERIFY(sizeSpy.isValid()); QCOMPARE(surface->size(), QSize()); QSignalSpy serverSurfaceSpy(m_shellInterface, SIGNAL(surfaceCreated(KWayland::Server::ShellSurfaceInterface*))); QVERIFY(serverSurfaceSpy.isValid()); QVERIFY(serverSurfaceSpy.wait()); ShellSurfaceInterface *serverSurface = serverSurfaceSpy.first().first().value(); QVERIFY(serverSurface); QVERIFY(serverSurface->parentResource()); QSignalSpy toplevelSpy(serverSurface, SIGNAL(toplevelChanged(bool))); QVERIFY(toplevelSpy.isValid()); surface->setFullscreen(); QVERIFY(toplevelSpy.wait()); QCOMPARE(toplevelSpy.count(), 1); QVERIFY(!toplevelSpy.first().first().toBool()); toplevelSpy.clear(); surface->setToplevel(); QVERIFY(toplevelSpy.wait()); QCOMPARE(toplevelSpy.count(), 1); QVERIFY(toplevelSpy.first().first().toBool()); serverSurface->requestSize(QSize(1024, 768)); QVERIFY(sizeSpy.wait()); QCOMPARE(sizeSpy.count(), 1); QCOMPARE(sizeSpy.first().first().toSize(), QSize(1024, 768)); QCOMPARE(surface->size(), QSize(1024, 768)); // set back to fullscreen toplevelSpy.clear(); surface->setFullscreen(); QVERIFY(toplevelSpy.wait()); QCOMPARE(toplevelSpy.count(), 1); QVERIFY(!toplevelSpy.first().first().toBool()); } void TestWaylandShell::testTransient_data() { QTest::addColumn("keyboardFocus"); QTest::newRow("focus") << true; QTest::newRow("no focus") << false; } void TestWaylandShell::testTransient() { using namespace KWayland::Server; using namespace KWayland::Client; QScopedPointer s(m_compositor->createSurface()); QVERIFY(!s.isNull()); QVERIFY(s->isValid()); ShellSurface *surface = m_shell->createSurface(s.data(), m_shell); QSignalSpy serverSurfaceSpy(m_shellInterface, &ShellInterface::surfaceCreated); QVERIFY(serverSurfaceSpy.isValid()); QVERIFY(serverSurfaceSpy.wait()); ShellSurfaceInterface *serverSurface = serverSurfaceSpy.first().first().value(); QVERIFY(serverSurface); QSignalSpy acceptsKeyboardFocusChangedSpy(serverSurface, &ShellSurfaceInterface::acceptsKeyboardFocusChanged); QVERIFY(acceptsKeyboardFocusChangedSpy.isValid()); QCOMPARE(serverSurface->isToplevel(), true); QCOMPARE(serverSurface->isPopup(), false); QCOMPARE(serverSurface->isTransient(), false); QCOMPARE(serverSurface->transientFor(), QPointer()); QCOMPARE(serverSurface->transientOffset(), QPoint()); QVERIFY(serverSurface->acceptsKeyboardFocus()); QVERIFY(acceptsKeyboardFocusChangedSpy.isEmpty()); QSignalSpy transientSpy(serverSurface, &ShellSurfaceInterface::transientChanged); QVERIFY(transientSpy.isValid()); QSignalSpy transientOffsetSpy(serverSurface, &ShellSurfaceInterface::transientOffsetChanged); QVERIFY(transientOffsetSpy.isValid()); QSignalSpy transientForChangedSpy(serverSurface, &ShellSurfaceInterface::transientForChanged); QVERIFY(transientForChangedSpy.isValid()); QScopedPointer s2(m_compositor->createSurface()); m_shell->createSurface(s2.data(), m_shell); serverSurfaceSpy.clear(); QVERIFY(serverSurfaceSpy.wait()); ShellSurfaceInterface *serverSurface2 = serverSurfaceSpy.first().first().value(); QVERIFY(serverSurface2 != serverSurface); QVERIFY(serverSurface2); QVERIFY(serverSurface2->acceptsKeyboardFocus()); QFETCH(bool, keyboardFocus); surface->setTransient(s2.data(), QPoint(10, 20), keyboardFocus ? ShellSurface::TransientFlag::Default : ShellSurface::TransientFlag::NoFocus); QVERIFY(transientSpy.wait()); QCOMPARE(transientSpy.count(), 1); QCOMPARE(transientSpy.first().first().toBool(), true); QCOMPARE(transientOffsetSpy.count(), 1); QCOMPARE(transientOffsetSpy.first().first().toPoint(), QPoint(10, 20)); QCOMPARE(transientForChangedSpy.count(), 1); QCOMPARE(serverSurface->isToplevel(), true); QCOMPARE(serverSurface->isPopup(), false); QCOMPARE(serverSurface->isTransient(), true); QCOMPARE(serverSurface->transientFor(), QPointer(serverSurface2->surface())); QCOMPARE(serverSurface->transientOffset(), QPoint(10, 20)); QCOMPARE(serverSurface->acceptsKeyboardFocus(), keyboardFocus); QCOMPARE(acceptsKeyboardFocusChangedSpy.isEmpty(), keyboardFocus); QCOMPARE(acceptsKeyboardFocusChangedSpy.count(), keyboardFocus ? 0 : 1); QCOMPARE(serverSurface2->isToplevel(), true); QCOMPARE(serverSurface2->isPopup(), false); QCOMPARE(serverSurface2->isTransient(), false); QCOMPARE(serverSurface2->transientFor(), QPointer()); QCOMPARE(serverSurface2->transientOffset(), QPoint()); QVERIFY(serverSurface2->acceptsKeyboardFocus()); } void TestWaylandShell::testPing() { using namespace KWayland::Server; QScopedPointer s(m_compositor->createSurface()); QVERIFY(!s.isNull()); QVERIFY(s->isValid()); KWayland::Client::ShellSurface *surface = m_shell->createSurface(s.data(), m_shell); QSignalSpy pingSpy(surface, SIGNAL(pinged())); QSignalSpy serverSurfaceSpy(m_shellInterface, SIGNAL(surfaceCreated(KWayland::Server::ShellSurfaceInterface*))); QVERIFY(serverSurfaceSpy.isValid()); QVERIFY(serverSurfaceSpy.wait()); ShellSurfaceInterface *serverSurface = serverSurfaceSpy.first().first().value(); QVERIFY(serverSurface); QSignalSpy pingTimeoutSpy(serverSurface, SIGNAL(pingTimeout())); QVERIFY(pingTimeoutSpy.isValid()); QSignalSpy pongSpy(serverSurface, SIGNAL(pongReceived())); QVERIFY(pongSpy.isValid()); serverSurface->ping(); QVERIFY(pingSpy.wait()); wl_display_flush(m_connection->display()); if (pongSpy.isEmpty()) { QVERIFY(pongSpy.wait()); } QVERIFY(!pongSpy.isEmpty()); QVERIFY(pingTimeoutSpy.isEmpty()); // evil trick - timeout of zero will make it not get the pong serverSurface->setPingTimeout(0); pongSpy.clear(); pingTimeoutSpy.clear(); serverSurface->ping(); QTest::qWait(100); if (pingTimeoutSpy.isEmpty()) { QVERIFY(pingTimeoutSpy.wait()); } QCOMPARE(pingTimeoutSpy.count(), 1); QVERIFY(pongSpy.isEmpty()); } void TestWaylandShell::testTitle() { using namespace KWayland::Server; QScopedPointer s(m_compositor->createSurface()); QVERIFY(!s.isNull()); QVERIFY(s->isValid()); KWayland::Client::ShellSurface *surface = m_shell->createSurface(s.data(), m_shell); QSignalSpy serverSurfaceSpy(m_shellInterface, SIGNAL(surfaceCreated(KWayland::Server::ShellSurfaceInterface*))); QVERIFY(serverSurfaceSpy.isValid()); QVERIFY(serverSurfaceSpy.wait()); ShellSurfaceInterface *serverSurface = serverSurfaceSpy.first().first().value(); QVERIFY(serverSurface); QSignalSpy titleSpy(serverSurface, SIGNAL(titleChanged(QString))); QVERIFY(titleSpy.isValid()); QString testTitle = QStringLiteral("fooBar"); QVERIFY(serverSurface->title().isNull()); wl_shell_surface_set_title(*(const KWayland::Client::ShellSurface *)surface, testTitle.toUtf8().constData()); QVERIFY(titleSpy.wait()); QCOMPARE(serverSurface->title(), testTitle); QCOMPARE(titleSpy.first().first().toString(), testTitle); } void TestWaylandShell::testWindowClass() { using namespace KWayland::Server; QScopedPointer s(m_compositor->createSurface()); QVERIFY(!s.isNull()); QVERIFY(s->isValid()); KWayland::Client::ShellSurface *surface = m_shell->createSurface(s.data(), m_shell); QSignalSpy serverSurfaceSpy(m_shellInterface, SIGNAL(surfaceCreated(KWayland::Server::ShellSurfaceInterface*))); QVERIFY(serverSurfaceSpy.isValid()); QVERIFY(serverSurfaceSpy.wait()); ShellSurfaceInterface *serverSurface = serverSurfaceSpy.first().first().value(); QVERIFY(serverSurface); QSignalSpy windowClassSpy(serverSurface, SIGNAL(windowClassChanged(QByteArray))); QVERIFY(windowClassSpy.isValid()); QByteArray testClass = QByteArrayLiteral("fooBar"); QVERIFY(serverSurface->windowClass().isNull()); wl_shell_surface_set_class(*surface, testClass.constData()); QVERIFY(windowClassSpy.wait()); QCOMPARE(serverSurface->windowClass(), testClass); QCOMPARE(windowClassSpy.first().first().toByteArray(), testClass); } void TestWaylandShell::testDestroy() { using namespace KWayland::Client; QScopedPointer s(m_compositor->createSurface()); QVERIFY(!s.isNull()); QVERIFY(s->isValid()); ShellSurface *surface = m_shell->createSurface(s.data(), m_shell); QVERIFY(surface->isValid()); connect(m_connection, &ConnectionThread::connectionDied, m_shell, &Shell::destroy); connect(m_connection, &ConnectionThread::connectionDied, m_pointer, &Pointer::destroy); connect(m_connection, &ConnectionThread::connectionDied, m_seat, &Seat::destroy); connect(m_connection, &ConnectionThread::connectionDied, m_compositor, &Compositor::destroy); connect(m_connection, &ConnectionThread::connectionDied, s.data(), &Surface::destroy); connect(m_connection, &ConnectionThread::connectionDied, m_queue, &EventQueue::destroy); QSignalSpy connectionDiedSpy(m_connection, SIGNAL(connectionDied())); QVERIFY(connectionDiedSpy.isValid()); delete m_display; m_display = nullptr; m_compositorInterface = nullptr; m_shellInterface = nullptr; m_seatInterface = nullptr; QVERIFY(connectionDiedSpy.wait()); QVERIFY(!m_shell->isValid()); QVERIFY(!surface->isValid()); m_shell->destroy(); surface->destroy(); } void TestWaylandShell::testCast() { using namespace KWayland::Client; Registry registry; QSignalSpy shellSpy(®istry, SIGNAL(shellAnnounced(quint32,quint32))); registry.setEventQueue(m_queue); registry.create(m_connection->display()); QVERIFY(registry.isValid()); registry.setup(); QVERIFY(shellSpy.wait()); Shell s; auto wlShell = registry.bindShell(shellSpy.first().first().value(), shellSpy.first().last().value()); m_queue->addProxy(wlShell); QVERIFY(wlShell); QVERIFY(!s.isValid()); s.setup(wlShell); QVERIFY(s.isValid()); QCOMPARE((wl_shell*)s, wlShell); const Shell &s2(s); QCOMPARE((wl_shell*)s2, wlShell); } void TestWaylandShell::testMove() { using namespace KWayland::Client; using namespace KWayland::Server; QScopedPointer s(m_compositor->createSurface()); QVERIFY(!s.isNull()); QVERIFY(s->isValid()); ShellSurface *surface = m_shell->createSurface(s.data(), m_shell); QSignalSpy serverSurfaceSpy(m_shellInterface, &ShellInterface::surfaceCreated); QVERIFY(serverSurfaceSpy.isValid()); QVERIFY(serverSurfaceSpy.wait()); ShellSurfaceInterface *serverSurface = serverSurfaceSpy.first().first().value(); QVERIFY(serverSurface); QSignalSpy moveRequestedSpy(serverSurface, &ShellSurfaceInterface::moveRequested); QVERIFY(moveRequestedSpy.isValid()); QSignalSpy pointerButtonChangedSpy(m_pointer, &Pointer::buttonStateChanged); QVERIFY(pointerButtonChangedSpy.isValid()); m_seatInterface->setFocusedPointerSurface(serverSurface->surface()); m_seatInterface->pointerButtonPressed(Qt::LeftButton); QVERIFY(pointerButtonChangedSpy.wait()); surface->requestMove(m_seat, pointerButtonChangedSpy.first().first().value()); QVERIFY(moveRequestedSpy.wait()); QCOMPARE(moveRequestedSpy.count(), 1); QCOMPARE(moveRequestedSpy.first().at(0).value(), m_seatInterface); QCOMPARE(moveRequestedSpy.first().at(1).value(), m_seatInterface->pointerButtonSerial(Qt::LeftButton)); } void TestWaylandShell::testResize_data() { QTest::addColumn("resizeEdge"); QTest::addColumn("expectedEdge"); QTest::newRow("None") << Qt::Edges() << Qt::Edges(); QTest::newRow("Top") << Qt::Edges(Qt::TopEdge) << Qt::Edges(Qt::TopEdge); QTest::newRow("Bottom") << Qt::Edges(Qt::BottomEdge) << Qt::Edges(Qt::BottomEdge); QTest::newRow("Left") << Qt::Edges(Qt::LeftEdge) << Qt::Edges(Qt::LeftEdge); QTest::newRow("Right") << Qt::Edges(Qt::RightEdge) << Qt::Edges(Qt::RightEdge); QTest::newRow("Top Left") << Qt::Edges(Qt::TopEdge | Qt::LeftEdge) << Qt::Edges(Qt::TopEdge | Qt::LeftEdge); QTest::newRow("Top Right") << Qt::Edges(Qt::TopEdge | Qt::RightEdge) << Qt::Edges(Qt::TopEdge | Qt::RightEdge); QTest::newRow("Bottom Left") << Qt::Edges(Qt::BottomEdge | Qt::LeftEdge) << Qt::Edges(Qt::BottomEdge | Qt::LeftEdge); QTest::newRow("Bottom Right") << Qt::Edges(Qt::BottomEdge | Qt::RightEdge) << Qt::Edges(Qt::BottomEdge | Qt::RightEdge); // invalid combinations QTest::newRow("Top Bottom") << Qt::Edges(Qt::TopEdge | Qt::BottomEdge) << Qt::Edges(); QTest::newRow("Left Right") << Qt::Edges(Qt::RightEdge | Qt::LeftEdge) << Qt::Edges(); QTest::newRow("Top Bottom Right") << Qt::Edges(Qt::TopEdge | Qt::BottomEdge | Qt::RightEdge) << Qt::Edges(); QTest::newRow("Top Bottom Left") << Qt::Edges(Qt::TopEdge | Qt::BottomEdge | Qt::LeftEdge) << Qt::Edges(); QTest::newRow("Left Right Top") << Qt::Edges(Qt::RightEdge | Qt::LeftEdge | Qt::TopEdge) << Qt::Edges(); QTest::newRow("Left Right Bottom") << Qt::Edges(Qt::RightEdge | Qt::LeftEdge | Qt::BottomEdge) << Qt::Edges(); QTest::newRow("All") << Qt::Edges(Qt::RightEdge | Qt::LeftEdge | Qt::BottomEdge | Qt::TopEdge) << Qt::Edges(); } void TestWaylandShell::testResize() { using namespace KWayland::Client; using namespace KWayland::Server; QScopedPointer s(m_compositor->createSurface()); QVERIFY(!s.isNull()); QVERIFY(s->isValid()); ShellSurface *surface = m_shell->createSurface(s.data(), m_shell); QSignalSpy serverSurfaceSpy(m_shellInterface, &ShellInterface::surfaceCreated); QVERIFY(serverSurfaceSpy.isValid()); QVERIFY(serverSurfaceSpy.wait()); ShellSurfaceInterface *serverSurface = serverSurfaceSpy.first().first().value(); QVERIFY(serverSurface); QSignalSpy resizeRequestedSpy(serverSurface, &ShellSurfaceInterface::resizeRequested); QVERIFY(resizeRequestedSpy.isValid()); QSignalSpy pointerButtonChangedSpy(m_pointer, &Pointer::buttonStateChanged); QVERIFY(pointerButtonChangedSpy.isValid()); m_seatInterface->setFocusedPointerSurface(serverSurface->surface()); m_seatInterface->pointerButtonPressed(Qt::LeftButton); QVERIFY(pointerButtonChangedSpy.wait()); QFETCH(Qt::Edges, resizeEdge); surface->requestResize(m_seat, pointerButtonChangedSpy.first().first().value(), resizeEdge); QVERIFY(resizeRequestedSpy.wait()); QCOMPARE(resizeRequestedSpy.count(), 1); QCOMPARE(resizeRequestedSpy.first().at(0).value(), m_seatInterface); QCOMPARE(resizeRequestedSpy.first().at(1).value(), m_seatInterface->pointerButtonSerial(Qt::LeftButton)); QTEST(resizeRequestedSpy.first().at(2).value(), "expectedEdge"); } void TestWaylandShell::testDisconnect() { // this test verifies that the server side correctly tears down the resources when the client disconnects using namespace KWayland::Client; using namespace KWayland::Server; QScopedPointer s(m_compositor->createSurface()); QVERIFY(!s.isNull()); QVERIFY(s->isValid()); QScopedPointer surface(m_shell->createSurface(s.data(), m_shell)); QSignalSpy serverSurfaceSpy(m_shellInterface, &ShellInterface::surfaceCreated); QVERIFY(serverSurfaceSpy.isValid()); QVERIFY(serverSurfaceSpy.wait()); ShellSurfaceInterface *serverSurface = serverSurfaceSpy.first().first().value(); QVERIFY(serverSurface); // destroy client QSignalSpy clientDisconnectedSpy(serverSurface->client(), &ClientConnection::disconnected); QVERIFY(clientDisconnectedSpy.isValid()); QSignalSpy shellSurfaceDestroyedSpy(serverSurface, &QObject::destroyed); QVERIFY(shellSurfaceDestroyedSpy.isValid()); if (m_connection) { m_connection->deleteLater(); m_connection = nullptr; } QVERIFY(clientDisconnectedSpy.wait()); QCOMPARE(clientDisconnectedSpy.count(), 1); QCOMPARE(shellSurfaceDestroyedSpy.count(), 0); + // it's already unbound, but the shell surface is not yet destroyed + QVERIFY(!serverSurface->resource()); + // ensure we don't crash when accessing it in this state + serverSurface->ping(); + serverSurface->requestSize(QSize(1, 2)); QVERIFY(shellSurfaceDestroyedSpy.wait()); QCOMPARE(shellSurfaceDestroyedSpy.count(), 1); s->destroy(); surface->destroy(); m_shell->destroy(); m_compositor->destroy(); m_pointer->destroy(); m_seat->destroy(); m_queue->destroy(); } QTEST_GUILESS_MAIN(TestWaylandShell) #include "test_wayland_shell.moc" diff --git a/src/server/shell_interface.cpp b/src/server/shell_interface.cpp index a702290..07725eb 100644 --- a/src/server/shell_interface.cpp +++ b/src/server/shell_interface.cpp @@ -1,492 +1,498 @@ /******************************************************************** Copyright 2014 Martin Gräßlin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 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 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . *********************************************************************/ #include "shell_interface.h" #include "global_p.h" #include "resource_p.h" #include "display.h" #include "surface_interface.h" #include #include namespace KWayland { namespace Server { class ShellInterface::Private : public Global::Private { public: Private(ShellInterface *q, Display *d); QList surfaces; private: static void createSurfaceCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface); void bind(wl_client *client, uint32_t version, uint32_t id) override; void createSurface(wl_client *client, uint32_t version, uint32_t id, SurfaceInterface *surface, wl_resource *parentResource); ShellInterface *q; static const struct wl_shell_interface s_interface; static const quint32 s_version; }; const quint32 ShellInterface::Private::s_version = 1; ShellInterface::Private::Private(ShellInterface *q, Display *d) : Global::Private(d, &wl_shell_interface, s_version) , q(q) { } #ifndef DOXYGEN_SHOULD_SKIP_THIS const struct wl_shell_interface ShellInterface::Private::s_interface = { createSurfaceCallback }; #endif class ShellSurfaceInterface::Private : public Resource::Private { public: Private(ShellSurfaceInterface *q, ShellInterface *shell, SurfaceInterface *surface, wl_resource *parentResource); void ping(); SurfaceInterface *surface; QString title; QByteArray windowClass; QScopedPointer pingTimer; quint32 pingSerial = 0; enum class WindowMode { Fullscreen, Toplevel, Maximized, Popup }; WindowMode windowMode = WindowMode::Toplevel; QPoint transientOffset; QPointer transientFor; bool acceptsKeyboardFocus = true; void setWindowMode(WindowMode newWindowMode); private: // interface callbacks static void pongCallback(wl_client *client, wl_resource *resource, uint32_t serial); static void moveCallback(wl_client *client, wl_resource *resource, wl_resource *seat, uint32_t serial); static void resizeCallback(wl_client *client, wl_resource *resource, wl_resource *seat, uint32_t serial, uint32_t edges); static void setToplevelCallback(wl_client *client, wl_resource *resource); static void setTransientCallback(wl_client *client, wl_resource *resource, wl_resource *parent, int32_t x, int32_t y, uint32_t flags); static void setFullscreenCallback(wl_client *client, wl_resource *resource, uint32_t method, uint32_t framerate, wl_resource *output); static void setPopupCallback(wl_client *client, wl_resource *resource, wl_resource *seat, uint32_t serial, wl_resource *parent, int32_t x, int32_t y, uint32_t flags); static void setMaximizedCallback(wl_client *client, wl_resource *resource, wl_resource *output); static void setTitleCallback(wl_client *client, wl_resource *resource, const char *title); static void setClassCallback(wl_client *client, wl_resource *resource, const char *class_); void setTitle(const QString &title); void setWindowClass(const QByteArray &windowClass); void pong(quint32 serial); void setAcceptsFocus(quint32 flags); ShellSurfaceInterface *q_func() { return reinterpret_cast(q); } static const struct wl_shell_surface_interface s_interface; }; ShellInterface::ShellInterface(Display *display, QObject *parent) : Global(new Private(this, display), parent) { } ShellInterface::~ShellInterface() = default; void ShellInterface::Private::bind(wl_client *client, uint32_t version, uint32_t id) { auto c = display->getConnection(client); wl_resource *shell = c->createResource(&wl_shell_interface, qMin(version, s_version), id); if (!shell) { wl_client_post_no_memory(client); return; } wl_resource_set_implementation(shell, &s_interface, this, nullptr); } void ShellInterface::Private::createSurfaceCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface) { auto s = reinterpret_cast(wl_resource_get_user_data(resource)); s->createSurface(client, wl_resource_get_version(resource), id, SurfaceInterface::get(surface), resource); } void ShellInterface::Private::createSurface(wl_client *client, uint32_t version, uint32_t id, SurfaceInterface *surface, wl_resource *parentResource) { auto it = std::find_if(surfaces.constBegin(), surfaces.constEnd(), [surface](ShellSurfaceInterface *s) { return surface == s->surface(); } ); if (it != surfaces.constEnd()) { wl_resource_post_error(surface->resource(), WL_DISPLAY_ERROR_INVALID_OBJECT, "ShellSurface already created"); return; } ShellSurfaceInterface *shellSurface = new ShellSurfaceInterface(q, surface, parentResource); surfaces << shellSurface; QObject::connect(shellSurface, &ShellSurfaceInterface::destroyed, q, [this, shellSurface] { surfaces.removeAll(shellSurface); } ); shellSurface->d->create(display->getConnection(client), version, id); emit q->surfaceCreated(shellSurface); } /********************************* * ShellSurfaceInterface *********************************/ ShellSurfaceInterface::Private::Private(ShellSurfaceInterface *q, ShellInterface *shell, SurfaceInterface *surface, wl_resource *parentResource) : Resource::Private(q, shell, parentResource, &wl_shell_surface_interface, &s_interface) , surface(surface) , pingTimer(new QTimer) { pingTimer->setSingleShot(true); pingTimer->setInterval(1000); } #ifndef DOXYGEN_SHOULD_SKIP_THIS const struct wl_shell_surface_interface ShellSurfaceInterface::Private::s_interface = { pongCallback, moveCallback, resizeCallback, setToplevelCallback, setTransientCallback, setFullscreenCallback, setPopupCallback, setMaximizedCallback, setTitleCallback, setClassCallback }; #endif ShellSurfaceInterface::ShellSurfaceInterface(ShellInterface *shell, SurfaceInterface *parent, wl_resource *parentResource) : Resource(new Private(this, shell, parent, parentResource)) { Q_D(); connect(d->pingTimer.data(), &QTimer::timeout, this, &ShellSurfaceInterface::pingTimeout); } ShellSurfaceInterface::~ShellSurfaceInterface() = default; void ShellSurfaceInterface::Private::pongCallback(wl_client *client, wl_resource *resource, uint32_t serial) { auto s = cast(resource); Q_ASSERT(client == *s->client); s->pong(serial); } void ShellSurfaceInterface::Private::pong(quint32 serial) { if (pingTimer->isActive() && serial == pingSerial) { pingTimer->stop(); Q_Q(ShellSurfaceInterface); emit q->pongReceived(); } } void ShellSurfaceInterface::ping() { Q_D(); + if (!d->resource) { + return; + } d->ping(); } void ShellSurfaceInterface::Private::ping() { if (pingTimer->isActive()) { return; } pingSerial = global->display()->nextSerial(); wl_shell_surface_send_ping(resource, pingSerial); client->flush(); pingTimer->start(); } void ShellSurfaceInterface::setPingTimeout(uint msec) { Q_D(); d->pingTimer->setInterval(msec); } bool ShellSurfaceInterface::isPinged() const { Q_D(); return d->pingTimer->isActive(); } void ShellSurfaceInterface::requestSize(const QSize &size) { Q_D(); + if (!d->resource) { + return; + } // TODO: what about the edges? wl_shell_surface_send_configure(d->resource, 0, size.width(), size.height()); d->client->flush(); } void ShellSurfaceInterface::Private::moveCallback(wl_client *client, wl_resource *resource, wl_resource *seat, uint32_t serial) { auto s = cast(resource); Q_ASSERT(client == *s->client); emit s->q_func()->moveRequested(SeatInterface::get(seat), serial); } void ShellSurfaceInterface::Private::resizeCallback(wl_client *client, wl_resource *resource, wl_resource *seat, uint32_t serial, uint32_t edges) { auto s = cast(resource); Q_ASSERT(client == *s->client); Qt::Edges qtEdges; switch (edges) { case WL_SHELL_SURFACE_RESIZE_TOP: qtEdges = Qt::TopEdge; break; case WL_SHELL_SURFACE_RESIZE_BOTTOM: qtEdges = Qt::BottomEdge; break; case WL_SHELL_SURFACE_RESIZE_LEFT: qtEdges = Qt::LeftEdge; break; case WL_SHELL_SURFACE_RESIZE_TOP_LEFT: qtEdges = Qt::TopEdge | Qt::LeftEdge; break; case WL_SHELL_SURFACE_RESIZE_BOTTOM_LEFT: qtEdges = Qt::BottomEdge | Qt::LeftEdge; break; case WL_SHELL_SURFACE_RESIZE_RIGHT: qtEdges = Qt::RightEdge; break; case WL_SHELL_SURFACE_RESIZE_TOP_RIGHT: qtEdges = Qt::TopEdge | Qt::RightEdge; break; case WL_SHELL_SURFACE_RESIZE_BOTTOM_RIGHT: qtEdges = Qt::BottomEdge | Qt::RightEdge; break; default: break; } emit s->q_func()->resizeRequested(SeatInterface::get(seat), serial, qtEdges); } void ShellSurfaceInterface::Private::setToplevelCallback(wl_client *client, wl_resource *resource) { auto s = cast(resource); Q_ASSERT(client == *s->client); s->setWindowMode(WindowMode::Toplevel); } void ShellSurfaceInterface::Private::setTransientCallback(wl_client *client, wl_resource *resource, wl_resource *parent, int32_t x, int32_t y, uint32_t flags) { Q_UNUSED(flags) auto s = cast(resource); Q_ASSERT(client == *s->client); s->transientFor = QPointer(SurfaceInterface::get(parent)); s->transientOffset = QPoint(x, y); emit s->q_func()->transientChanged(!s->transientFor.isNull()); emit s->q_func()->transientOffsetChanged(s->transientOffset); emit s->q_func()->transientForChanged(); s->setAcceptsFocus(flags); } void ShellSurfaceInterface::Private::setAcceptsFocus(quint32 flags) { const bool acceptsFocus = !(flags & WL_SHELL_SURFACE_TRANSIENT_INACTIVE); if (acceptsFocus != acceptsKeyboardFocus) { acceptsKeyboardFocus = acceptsFocus; Q_Q(ShellSurfaceInterface); emit q->acceptsKeyboardFocusChanged(); } } void ShellSurfaceInterface::Private::setFullscreenCallback(wl_client *client, wl_resource *resource, uint32_t method, uint32_t framerate, wl_resource *output) { Q_UNUSED(method) Q_UNUSED(framerate) Q_UNUSED(output) auto s = cast(resource); Q_ASSERT(client == *s->client); // TODO: add method, framerate and output s->setWindowMode(WindowMode::Fullscreen); } void ShellSurfaceInterface::Private::setWindowMode(WindowMode newWindowMode) { if (windowMode == newWindowMode) { return; } const WindowMode oldWindowMode = windowMode; windowMode = newWindowMode; Q_Q(ShellSurfaceInterface); if (oldWindowMode == WindowMode::Fullscreen || newWindowMode == WindowMode::Fullscreen) { emit q->fullscreenChanged(windowMode == WindowMode::Fullscreen); } if (oldWindowMode == WindowMode::Toplevel || newWindowMode == WindowMode::Toplevel) { emit q->toplevelChanged(windowMode == WindowMode::Toplevel); } if (oldWindowMode == WindowMode::Maximized || newWindowMode == WindowMode::Maximized) { emit q->maximizedChanged(windowMode == WindowMode::Maximized); } if (oldWindowMode == WindowMode::Popup || newWindowMode == WindowMode::Popup) { emit q->popupChanged(windowMode == WindowMode::Popup); } } void ShellSurfaceInterface::Private::setPopupCallback(wl_client *client, wl_resource *resource, wl_resource *seat, uint32_t serial, wl_resource *parent, int32_t x, int32_t y, uint32_t flags) { Q_UNUSED(seat) Q_UNUSED(serial) Q_UNUSED(flags) auto s = cast(resource); Q_ASSERT(client == *s->client); // TODO: what about seat and serial? s->transientFor = QPointer(SurfaceInterface::get(parent)); s->transientOffset = QPoint(x, y); s->setWindowMode(WindowMode::Popup); emit s->q_func()->transientChanged(!s->transientFor.isNull()); emit s->q_func()->transientOffsetChanged(s->transientOffset); emit s->q_func()->transientForChanged(); s->setAcceptsFocus(WL_SHELL_SURFACE_TRANSIENT_INACTIVE); } void ShellSurfaceInterface::Private::setMaximizedCallback(wl_client *client, wl_resource *resource, wl_resource *output) { Q_UNUSED(output) auto s = cast(resource); Q_ASSERT(client == *s->client); s->setWindowMode(WindowMode::Maximized); } void ShellSurfaceInterface::Private::setTitleCallback(wl_client *client, wl_resource *resource, const char *title) { auto s = cast(resource); Q_ASSERT(client == *s->client); s->setTitle(QString::fromUtf8(title)); } void ShellSurfaceInterface::Private::setTitle(const QString &t) { if (title == t) { return; } title = t; Q_Q(ShellSurfaceInterface); emit q->titleChanged(title); } void ShellSurfaceInterface::Private::setClassCallback(wl_client *client, wl_resource *resource, const char *class_) { auto s = cast(resource); Q_ASSERT(client == *s->client); s->setWindowClass(QByteArray(class_)); } void ShellSurfaceInterface::Private::setWindowClass(const QByteArray &wc) { if (windowClass == wc) { return; } windowClass = wc; Q_Q(ShellSurfaceInterface); emit q->windowClassChanged(windowClass); } SurfaceInterface *ShellSurfaceInterface::surface() const { Q_D(); return d->surface; } ShellInterface *ShellSurfaceInterface::shell() const { Q_D(); return reinterpret_cast(d->global); } QString ShellSurfaceInterface::title() const { Q_D(); return d->title; } QByteArray ShellSurfaceInterface::windowClass() const { Q_D(); return d->windowClass; } bool ShellSurfaceInterface::isFullscreen() const { Q_D(); return d->windowMode == Private::WindowMode::Fullscreen; } bool ShellSurfaceInterface::isToplevel() const { Q_D(); return d->windowMode == Private::WindowMode::Toplevel; } bool ShellSurfaceInterface::isMaximized() const { Q_D(); return d->windowMode == Private::WindowMode::Maximized; } bool ShellSurfaceInterface::isPopup() const { Q_D(); return d->windowMode == Private::WindowMode::Popup; } bool ShellSurfaceInterface::isTransient() const { Q_D(); return !d->transientFor.isNull(); } QPoint ShellSurfaceInterface::transientOffset() const { Q_D(); return d->transientOffset; } bool ShellSurfaceInterface::acceptsKeyboardFocus() const { Q_D(); return d->acceptsKeyboardFocus; } QPointer< SurfaceInterface > ShellSurfaceInterface::transientFor() const { Q_D(); return d->transientFor; } ShellSurfaceInterface::Private *ShellSurfaceInterface::d_func() const { return reinterpret_cast(d.data()); } } }