diff --git a/autotests/client/test_datadevice.cpp b/autotests/client/test_datadevice.cpp index bcdf1d9..eca1cdb 100644 --- a/autotests/client/test_datadevice.cpp +++ b/autotests/client/test_datadevice.cpp @@ -1,559 +1,560 @@ /******************************************************************** 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 // KWayland #include "../../src/client/connection_thread.h" #include "../../src/client/event_queue.h" #include "../../src/client/datadevice.h" #include "../../src/client/datadevicemanager.h" #include "../../src/client/datasource.h" #include "../../src/client/compositor.h" #include "../../src/client/keyboard.h" #include "../../src/client/pointer.h" #include "../../src/client/registry.h" #include "../../src/client/seat.h" #include "../../src/client/surface.h" #include "../../src/server/display.h" #include "../../src/server/datadevicemanager_interface.h" #include "../../src/server/datasource_interface.h" #include "../../src/server/compositor_interface.h" #include "../../src/server/pointer_interface.h" #include "../../src/server/seat_interface.h" #include "../../src/server/surface_interface.h" // Wayland #include class TestDataDevice : public QObject { Q_OBJECT private Q_SLOTS: void init(); void cleanup(); void testCreate(); void testDrag(); void testDragInternally(); void testSetSelection(); void testSendSelectionOnSeat(); void testReplaceSource(); void testDestroy(); private: KWayland::Server::Display *m_display = nullptr; KWayland::Server::DataDeviceManagerInterface *m_dataDeviceManagerInterface = nullptr; KWayland::Server::CompositorInterface *m_compositorInterface = nullptr; KWayland::Server::SeatInterface *m_seatInterface = nullptr; KWayland::Client::ConnectionThread *m_connection = nullptr; KWayland::Client::DataDeviceManager *m_dataDeviceManager = nullptr; KWayland::Client::Compositor *m_compositor = nullptr; KWayland::Client::Seat *m_seat = nullptr; KWayland::Client::EventQueue *m_queue = nullptr; QThread *m_thread = nullptr; }; static const QString s_socketName = QStringLiteral("kwayland-test-wayland-datadevice-0"); void TestDataDevice::init() { + qRegisterMetaType(); using namespace KWayland::Server; delete m_display; m_display = new Display(this); m_display->setSocketName(s_socketName); m_display->start(); QVERIFY(m_display->isRunning()); // 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()); KWayland::Client::Registry registry; QSignalSpy dataDeviceManagerSpy(®istry, SIGNAL(dataDeviceManagerAnnounced(quint32,quint32))); QVERIFY(dataDeviceManagerSpy.isValid()); QSignalSpy seatSpy(®istry, SIGNAL(seatAnnounced(quint32,quint32))); QVERIFY(seatSpy.isValid()); QSignalSpy compositorSpy(®istry, SIGNAL(compositorAnnounced(quint32,quint32))); QVERIFY(compositorSpy.isValid()); QVERIFY(!registry.eventQueue()); registry.setEventQueue(m_queue); QCOMPARE(registry.eventQueue(), m_queue); registry.create(m_connection->display()); QVERIFY(registry.isValid()); registry.setup(); m_dataDeviceManagerInterface = m_display->createDataDeviceManager(m_display); m_dataDeviceManagerInterface->create(); QVERIFY(m_dataDeviceManagerInterface->isValid()); QVERIFY(dataDeviceManagerSpy.wait()); m_dataDeviceManager = registry.createDataDeviceManager(dataDeviceManagerSpy.first().first().value(), dataDeviceManagerSpy.first().last().value(), this); m_seatInterface = m_display->createSeat(m_display); m_seatInterface->setHasPointer(true); m_seatInterface->create(); QVERIFY(m_seatInterface->isValid()); QVERIFY(seatSpy.wait()); m_seat = registry.createSeat(seatSpy.first().first().value(), seatSpy.first().last().value(), this); QVERIFY(m_seat->isValid()); QSignalSpy pointerChangedSpy(m_seat, SIGNAL(hasPointerChanged(bool))); QVERIFY(pointerChangedSpy.isValid()); QVERIFY(pointerChangedSpy.wait()); m_compositorInterface = m_display->createCompositor(m_display); m_compositorInterface->create(); QVERIFY(m_compositorInterface->isValid()); QVERIFY(compositorSpy.wait()); m_compositor = registry.createCompositor(compositorSpy.first().first().value(), compositorSpy.first().last().value(), this); QVERIFY(m_compositor->isValid()); } void TestDataDevice::cleanup() { if (m_dataDeviceManager) { delete m_dataDeviceManager; m_dataDeviceManager = nullptr; } if (m_seat) { delete m_seat; m_seat = nullptr; } if (m_compositor) { delete m_compositor; m_compositor = nullptr; } if (m_queue) { delete m_queue; m_queue = nullptr; } if (m_thread) { m_thread->quit(); m_thread->wait(); delete m_thread; m_thread = nullptr; } delete m_connection; m_connection = nullptr; delete m_display; m_display = nullptr; } void TestDataDevice::testCreate() { using namespace KWayland::Client; using namespace KWayland::Server; QSignalSpy dataDeviceCreatedSpy(m_dataDeviceManagerInterface, SIGNAL(dataDeviceCreated(KWayland::Server::DataDeviceInterface*))); QVERIFY(dataDeviceCreatedSpy.isValid()); QScopedPointer dataDevice(m_dataDeviceManager->getDataDevice(m_seat)); QVERIFY(dataDevice->isValid()); QVERIFY(dataDeviceCreatedSpy.wait()); QCOMPARE(dataDeviceCreatedSpy.count(), 1); auto deviceInterface = dataDeviceCreatedSpy.first().first().value(); QVERIFY(deviceInterface); QCOMPARE(deviceInterface->seat(), m_seatInterface); QVERIFY(!deviceInterface->dragSource()); QVERIFY(!deviceInterface->origin()); QVERIFY(!deviceInterface->icon()); QVERIFY(!deviceInterface->selection()); QVERIFY(deviceInterface->parentResource()); QVERIFY(!m_seatInterface->selection()); m_seatInterface->setSelection(deviceInterface); QCOMPARE(m_seatInterface->selection(), deviceInterface); // and destroy QSignalSpy destroyedSpy(deviceInterface, &QObject::destroyed); QVERIFY(destroyedSpy.isValid()); dataDevice.reset(); QVERIFY(destroyedSpy.wait()); QVERIFY(!m_seatInterface->selection()); } void TestDataDevice::testDrag() { using namespace KWayland::Client; using namespace KWayland::Server; QScopedPointer pointer(m_seat->createPointer()); QSignalSpy dataDeviceCreatedSpy(m_dataDeviceManagerInterface, SIGNAL(dataDeviceCreated(KWayland::Server::DataDeviceInterface*))); QVERIFY(dataDeviceCreatedSpy.isValid()); QScopedPointer dataDevice(m_dataDeviceManager->getDataDevice(m_seat)); QVERIFY(dataDevice->isValid()); QVERIFY(dataDeviceCreatedSpy.wait()); QCOMPARE(dataDeviceCreatedSpy.count(), 1); auto deviceInterface = dataDeviceCreatedSpy.first().first().value(); QVERIFY(deviceInterface); QSignalSpy dataSourceCreatedSpy(m_dataDeviceManagerInterface, SIGNAL(dataSourceCreated(KWayland::Server::DataSourceInterface*))); QVERIFY(dataDeviceCreatedSpy.isValid()); QScopedPointer dataSource(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource->isValid()); QVERIFY(dataSourceCreatedSpy.wait()); QCOMPARE(dataSourceCreatedSpy.count(), 1); auto sourceInterface = dataSourceCreatedSpy.first().first().value(); QVERIFY(sourceInterface); QSignalSpy surfaceCreatedSpy(m_compositorInterface, SIGNAL(surfaceCreated(KWayland::Server::SurfaceInterface*))); QVERIFY(surfaceCreatedSpy.isValid()); QScopedPointer surface(m_compositor->createSurface()); QVERIFY(surface->isValid()); QVERIFY(surfaceCreatedSpy.wait()); QCOMPARE(surfaceCreatedSpy.count(), 1); auto surfaceInterface = surfaceCreatedSpy.first().first().value(); // now we have all we need to start a drag operation QSignalSpy dragStartedSpy(deviceInterface, SIGNAL(dragStarted())); QVERIFY(dragStartedSpy.isValid()); // first we need to fake the pointer enter m_seatInterface->setFocusedPointerSurface(surfaceInterface); m_seatInterface->pointerButtonPressed(1); QCoreApplication::processEvents(); dataDevice->startDrag(1, dataSource.data(), surface.data()); QVERIFY(dragStartedSpy.wait()); QCOMPARE(dragStartedSpy.count(), 1); QCOMPARE(deviceInterface->dragSource(), sourceInterface); QCOMPARE(deviceInterface->origin(), surfaceInterface); QVERIFY(!deviceInterface->icon()); } void TestDataDevice::testDragInternally() { using namespace KWayland::Client; using namespace KWayland::Server; QScopedPointer pointer(m_seat->createPointer()); QSignalSpy dataDeviceCreatedSpy(m_dataDeviceManagerInterface, SIGNAL(dataDeviceCreated(KWayland::Server::DataDeviceInterface*))); QVERIFY(dataDeviceCreatedSpy.isValid()); QScopedPointer dataDevice(m_dataDeviceManager->getDataDevice(m_seat)); QVERIFY(dataDevice->isValid()); QVERIFY(dataDeviceCreatedSpy.wait()); QCOMPARE(dataDeviceCreatedSpy.count(), 1); auto deviceInterface = dataDeviceCreatedSpy.first().first().value(); QVERIFY(deviceInterface); QSignalSpy surfaceCreatedSpy(m_compositorInterface, SIGNAL(surfaceCreated(KWayland::Server::SurfaceInterface*))); QVERIFY(surfaceCreatedSpy.isValid()); QScopedPointer surface(m_compositor->createSurface()); QVERIFY(surface->isValid()); QVERIFY(surfaceCreatedSpy.wait()); QCOMPARE(surfaceCreatedSpy.count(), 1); auto surfaceInterface = surfaceCreatedSpy.first().first().value(); QScopedPointer iconSurface(m_compositor->createSurface()); QVERIFY(iconSurface->isValid()); QVERIFY(surfaceCreatedSpy.wait()); QCOMPARE(surfaceCreatedSpy.count(), 2); auto iconSurfaceInterface = surfaceCreatedSpy.last().first().value(); // now we have all we need to start a drag operation QSignalSpy dragStartedSpy(deviceInterface, SIGNAL(dragStarted())); QVERIFY(dragStartedSpy.isValid()); // first we need to fake the pointer enter m_seatInterface->setFocusedPointerSurface(surfaceInterface); m_seatInterface->pointerButtonPressed(1); QCoreApplication::processEvents(); dataDevice->startDragInternally(1, surface.data(), iconSurface.data()); QVERIFY(dragStartedSpy.wait()); QCOMPARE(dragStartedSpy.count(), 1); QVERIFY(!deviceInterface->dragSource()); QCOMPARE(deviceInterface->origin(), surfaceInterface); QCOMPARE(deviceInterface->icon(), iconSurfaceInterface); } void TestDataDevice::testSetSelection() { using namespace KWayland::Client; using namespace KWayland::Server; QScopedPointer pointer(m_seat->createPointer()); QSignalSpy dataDeviceCreatedSpy(m_dataDeviceManagerInterface, SIGNAL(dataDeviceCreated(KWayland::Server::DataDeviceInterface*))); QVERIFY(dataDeviceCreatedSpy.isValid()); QScopedPointer dataDevice(m_dataDeviceManager->getDataDevice(m_seat)); QVERIFY(dataDevice->isValid()); QVERIFY(dataDeviceCreatedSpy.wait()); QCOMPARE(dataDeviceCreatedSpy.count(), 1); auto deviceInterface = dataDeviceCreatedSpy.first().first().value(); QVERIFY(deviceInterface); QSignalSpy dataSourceCreatedSpy(m_dataDeviceManagerInterface, SIGNAL(dataSourceCreated(KWayland::Server::DataSourceInterface*))); QVERIFY(dataDeviceCreatedSpy.isValid()); QScopedPointer dataSource(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource->isValid()); dataSource->offer(QStringLiteral("text/plain")); QVERIFY(dataSourceCreatedSpy.wait()); QCOMPARE(dataSourceCreatedSpy.count(), 1); auto sourceInterface = dataSourceCreatedSpy.first().first().value(); QVERIFY(sourceInterface); // everything setup, now we can test setting the selection QSignalSpy selectionChangedSpy(deviceInterface, SIGNAL(selectionChanged(KWayland::Server::DataSourceInterface*))); QVERIFY(selectionChangedSpy.isValid()); QSignalSpy selectionClearedSpy(deviceInterface, SIGNAL(selectionCleared())); QVERIFY(selectionClearedSpy.isValid()); QVERIFY(!deviceInterface->selection()); dataDevice->setSelection(1, dataSource.data()); QVERIFY(selectionChangedSpy.wait()); QCOMPARE(selectionChangedSpy.count(), 1); QCOMPARE(selectionClearedSpy.count(), 0); QCOMPARE(selectionChangedSpy.first().first().value(), sourceInterface); QCOMPARE(deviceInterface->selection(), sourceInterface); // send selection to datadevice QSignalSpy selectionOfferedSpy(dataDevice.data(), SIGNAL(selectionOffered(KWayland::Client::DataOffer*))); QVERIFY(selectionOfferedSpy.isValid()); deviceInterface->sendSelection(deviceInterface); QVERIFY(selectionOfferedSpy.wait()); QCOMPARE(selectionOfferedSpy.count(), 1); auto dataOffer = selectionOfferedSpy.first().first().value(); QVERIFY(dataOffer); QCOMPARE(dataOffer->offeredMimeTypes().count(), 1); QCOMPARE(dataOffer->offeredMimeTypes().first().name(), QStringLiteral("text/plain")); // sending a new mimetype to the selection, should be announced in the offer QSignalSpy mimeTypeAddedSpy(dataOffer, SIGNAL(mimeTypeOffered(QString))); QVERIFY(mimeTypeAddedSpy.isValid()); dataSource->offer(QStringLiteral("text/html")); QVERIFY(mimeTypeAddedSpy.wait()); QCOMPARE(mimeTypeAddedSpy.count(), 1); QCOMPARE(mimeTypeAddedSpy.first().first().toString(), QStringLiteral("text/html")); QCOMPARE(dataOffer->offeredMimeTypes().count(), 2); QCOMPARE(dataOffer->offeredMimeTypes().first().name(), QStringLiteral("text/plain")); QCOMPARE(dataOffer->offeredMimeTypes().last().name(), QStringLiteral("text/html")); // now clear the selection dataDevice->clearSelection(1); QVERIFY(selectionClearedSpy.wait()); QCOMPARE(selectionChangedSpy.count(), 1); QCOMPARE(selectionClearedSpy.count(), 1); QVERIFY(!deviceInterface->selection()); // set another selection dataDevice->setSelection(2, dataSource.data()); QVERIFY(selectionChangedSpy.wait()); // now unbind the dataDevice QSignalSpy unboundSpy(deviceInterface, &DataDeviceInterface::unbound); QVERIFY(unboundSpy.isValid()); dataDevice.reset(); QVERIFY(unboundSpy.wait()); // send a selection to the unbound data device deviceInterface->sendSelection(deviceInterface); } void TestDataDevice::testSendSelectionOnSeat() { // this test verifies that the selection is sent when setting a focused keyboard using namespace KWayland::Client; using namespace KWayland::Server; // first add keyboard support to Seat QSignalSpy keyboardChangedSpy(m_seat, &Seat::hasKeyboardChanged); QVERIFY(keyboardChangedSpy.isValid()); m_seatInterface->setHasKeyboard(true); QVERIFY(keyboardChangedSpy.wait()); // now create DataDevice, Keyboard and a Surface QSignalSpy dataDeviceCreatedSpy(m_dataDeviceManagerInterface, &DataDeviceManagerInterface::dataDeviceCreated); QVERIFY(dataDeviceCreatedSpy.isValid()); QScopedPointer dataDevice(m_dataDeviceManager->getDataDevice(m_seat)); QVERIFY(dataDevice->isValid()); QVERIFY(dataDeviceCreatedSpy.wait()); auto serverDataDevice = dataDeviceCreatedSpy.first().first().value(); QVERIFY(serverDataDevice); QScopedPointer keyboard(m_seat->createKeyboard()); QVERIFY(keyboard->isValid()); QSignalSpy surfaceCreatedSpy(m_compositorInterface, &CompositorInterface::surfaceCreated); QVERIFY(surfaceCreatedSpy.isValid()); QScopedPointer surface(m_compositor->createSurface()); QVERIFY(surface->isValid()); QVERIFY(surfaceCreatedSpy.wait()); auto serverSurface = surfaceCreatedSpy.first().first().value(); QVERIFY(serverSurface); m_seatInterface->setFocusedKeyboardSurface(serverSurface); // now set the selection QScopedPointer dataSource(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource->isValid()); dataSource->offer(QStringLiteral("text/plain")); dataDevice->setSelection(1, dataSource.data()); // we should get a selection offered for that on the data device QSignalSpy selectionOfferedSpy(dataDevice.data(), &DataDevice::selectionOffered); QVERIFY(selectionOfferedSpy.isValid()); QVERIFY(selectionOfferedSpy.wait()); QCOMPARE(selectionOfferedSpy.count(), 1); // now unfocus the keyboard m_seatInterface->setFocusedKeyboardSurface(nullptr); // if setting the same surface again, we should get another offer m_seatInterface->setFocusedKeyboardSurface(serverSurface); QVERIFY(selectionOfferedSpy.wait()); QCOMPARE(selectionOfferedSpy.count(), 2); // now let's try to destroy the data device and set a focused keyboard just while the data device is being destroyedd m_seatInterface->setFocusedKeyboardSurface(nullptr); QSignalSpy unboundSpy(serverDataDevice, &Resource::unbound); QVERIFY(unboundSpy.isValid()); dataDevice.reset(); QVERIFY(unboundSpy.wait()); m_seatInterface->setFocusedKeyboardSurface(serverSurface); } void TestDataDevice::testReplaceSource() { // this test verifies that replacing a data source cancels the previous source using namespace KWayland::Client; using namespace KWayland::Server; // first add keyboard support to Seat QSignalSpy keyboardChangedSpy(m_seat, &Seat::hasKeyboardChanged); QVERIFY(keyboardChangedSpy.isValid()); m_seatInterface->setHasKeyboard(true); QVERIFY(keyboardChangedSpy.wait()); // now create DataDevice, Keyboard and a Surface QSignalSpy dataDeviceCreatedSpy(m_dataDeviceManagerInterface, &DataDeviceManagerInterface::dataDeviceCreated); QVERIFY(dataDeviceCreatedSpy.isValid()); QScopedPointer dataDevice(m_dataDeviceManager->getDataDevice(m_seat)); QVERIFY(dataDevice->isValid()); QVERIFY(dataDeviceCreatedSpy.wait()); auto serverDataDevice = dataDeviceCreatedSpy.first().first().value(); QVERIFY(serverDataDevice); QScopedPointer keyboard(m_seat->createKeyboard()); QVERIFY(keyboard->isValid()); QSignalSpy surfaceCreatedSpy(m_compositorInterface, &CompositorInterface::surfaceCreated); QVERIFY(surfaceCreatedSpy.isValid()); QScopedPointer surface(m_compositor->createSurface()); QVERIFY(surface->isValid()); QVERIFY(surfaceCreatedSpy.wait()); auto serverSurface = surfaceCreatedSpy.first().first().value(); QVERIFY(serverSurface); m_seatInterface->setFocusedKeyboardSurface(serverSurface); // now set the selection QScopedPointer dataSource(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource->isValid()); dataSource->offer(QStringLiteral("text/plain")); dataDevice->setSelection(1, dataSource.data()); QSignalSpy sourceCancelledSpy(dataSource.data(), &DataSource::cancelled); QVERIFY(sourceCancelledSpy.isValid()); // we should get a selection offered for that on the data device QSignalSpy selectionOfferedSpy(dataDevice.data(), &DataDevice::selectionOffered); QVERIFY(selectionOfferedSpy.isValid()); QVERIFY(selectionOfferedSpy.wait()); QCOMPARE(selectionOfferedSpy.count(), 1); // create a second data source and replace previous one QScopedPointer dataSource2(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource2->isValid()); dataSource2->offer(QStringLiteral("text/plain")); QSignalSpy sourceCancelled2Spy(dataSource2.data(), &DataSource::cancelled); QVERIFY(sourceCancelled2Spy.isValid()); dataDevice->setSelection(1, dataSource2.data()); QCOMPARE(selectionOfferedSpy.count(), 1); QVERIFY(sourceCancelledSpy.wait()); QCOMPARE(selectionOfferedSpy.count(), 2); QVERIFY(sourceCancelled2Spy.isEmpty()); // create a new DataDevice and replace previous one QScopedPointer dataDevice2(m_dataDeviceManager->getDataDevice(m_seat)); QVERIFY(dataDevice2->isValid()); QScopedPointer dataSource3(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource3->isValid()); dataSource3->offer(QStringLiteral("text/plain")); dataDevice2->setSelection(1, dataSource3.data()); QVERIFY(sourceCancelled2Spy.wait()); // try to crash by first destroying dataSource3 and setting a new DataSource QScopedPointer dataSource4(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource4->isValid()); dataSource4->offer(QStringLiteral("text/plain")); dataSource3.reset(); dataDevice2->setSelection(1, dataSource4.data()); QVERIFY(selectionOfferedSpy.wait()); } void TestDataDevice::testDestroy() { using namespace KWayland::Client; QScopedPointer dataDevice(m_dataDeviceManager->getDataDevice(m_seat)); QVERIFY(dataDevice->isValid()); connect(m_connection, &ConnectionThread::connectionDied, m_dataDeviceManager, &DataDeviceManager::destroy); connect(m_connection, &ConnectionThread::connectionDied, m_seat, &Seat::destroy); connect(m_connection, &ConnectionThread::connectionDied, m_compositor, &Compositor::destroy); connect(m_connection, &ConnectionThread::connectionDied, dataDevice.data(), &DataDevice::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; QVERIFY(connectionDiedSpy.wait()); // now the data device should be destroyed; QVERIFY(!dataDevice->isValid()); // calling destroy again should not fail dataDevice->destroy(); } QTEST_GUILESS_MAIN(TestDataDevice) #include "test_datadevice.moc" diff --git a/autotests/client/test_datasource.cpp b/autotests/client/test_datasource.cpp index ab4c9c6..bd8b081 100644 --- a/autotests/client/test_datasource.cpp +++ b/autotests/client/test_datasource.cpp @@ -1,338 +1,339 @@ /******************************************************************** 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 #include // KWayland #include "../../src/client/connection_thread.h" #include "../../src/client/event_queue.h" #include "../../src/client/datadevicemanager.h" #include "../../src/client/datasource.h" #include "../../src/client/registry.h" #include "../../src/server/display.h" #include "../../src/server/datadevicemanager_interface.h" #include "../../src/server/datasource_interface.h" // Wayland #include class TestDataSource : public QObject { Q_OBJECT private Q_SLOTS: void init(); void cleanup(); void testOffer(); void testTargetAccepts_data(); void testTargetAccepts(); void testRequestSend(); void testRequestSendOnUnbound(); void testCancel(); void testServerGet(); void testDestroy(); private: KWayland::Server::Display *m_display = nullptr; KWayland::Server::DataDeviceManagerInterface *m_dataDeviceManagerInterface = nullptr; KWayland::Client::ConnectionThread *m_connection = nullptr; KWayland::Client::DataDeviceManager *m_dataDeviceManager = nullptr; KWayland::Client::EventQueue *m_queue = nullptr; QThread *m_thread = nullptr; }; static const QString s_socketName = QStringLiteral("kwayland-test-wayland-datasource-0"); void TestDataSource::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()); // 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()); KWayland::Client::Registry registry; QSignalSpy dataDeviceManagerSpy(®istry, SIGNAL(dataDeviceManagerAnnounced(quint32,quint32))); QVERIFY(dataDeviceManagerSpy.isValid()); QVERIFY(!registry.eventQueue()); registry.setEventQueue(m_queue); QCOMPARE(registry.eventQueue(), m_queue); registry.create(m_connection->display()); QVERIFY(registry.isValid()); registry.setup(); m_dataDeviceManagerInterface = m_display->createDataDeviceManager(m_display); m_dataDeviceManagerInterface->create(); QVERIFY(m_dataDeviceManagerInterface->isValid()); QVERIFY(dataDeviceManagerSpy.wait()); m_dataDeviceManager = registry.createDataDeviceManager(dataDeviceManagerSpy.first().first().value(), dataDeviceManagerSpy.first().last().value(), this); } void TestDataSource::cleanup() { if (m_dataDeviceManager) { delete m_dataDeviceManager; m_dataDeviceManager = nullptr; } if (m_queue) { delete m_queue; m_queue = nullptr; } if (m_thread) { m_thread->quit(); m_thread->wait(); delete m_thread; m_thread = nullptr; } delete m_connection; m_connection = nullptr; delete m_display; m_display = nullptr; } void TestDataSource::testOffer() { using namespace KWayland::Client; using namespace KWayland::Server; + qRegisterMetaType(); QSignalSpy dataSourceCreatedSpy(m_dataDeviceManagerInterface, SIGNAL(dataSourceCreated(KWayland::Server::DataSourceInterface*))); QVERIFY(dataSourceCreatedSpy.isValid()); QScopedPointer dataSource(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource->isValid()); QVERIFY(dataSourceCreatedSpy.wait()); QCOMPARE(dataSourceCreatedSpy.count(), 1); QPointer serverDataSource = dataSourceCreatedSpy.first().first().value(); QVERIFY(!serverDataSource.isNull()); QCOMPARE(serverDataSource->mimeTypes().count(), 0); QVERIFY(serverDataSource->parentResource()); QSignalSpy offeredSpy(serverDataSource.data(), SIGNAL(mimeTypeOffered(QString))); QVERIFY(offeredSpy.isValid()); const QString plain = QStringLiteral("text/plain"); QMimeDatabase db; dataSource->offer(db.mimeTypeForName(plain)); QVERIFY(offeredSpy.wait()); QCOMPARE(offeredSpy.count(), 1); QCOMPARE(offeredSpy.last().first().toString(), plain); QCOMPARE(serverDataSource->mimeTypes().count(), 1); QCOMPARE(serverDataSource->mimeTypes().first(), plain); const QString html = QStringLiteral("text/html"); dataSource->offer(db.mimeTypeForName(html)); QVERIFY(offeredSpy.wait()); QCOMPARE(offeredSpy.count(), 2); QCOMPARE(offeredSpy.first().first().toString(), plain); QCOMPARE(offeredSpy.last().first().toString(), html); QCOMPARE(serverDataSource->mimeTypes().count(), 2); QCOMPARE(serverDataSource->mimeTypes().first(), plain); QCOMPARE(serverDataSource->mimeTypes().last(), html); // try destroying the client side, should trigger a destroy of server side dataSource.reset(); QVERIFY(!serverDataSource.isNull()); wl_display_flush(m_connection->display()); // after running the event loop the Wayland event should be delivered, but it uses delete later QCoreApplication::processEvents(); QVERIFY(!serverDataSource.isNull()); // so once more event loop QCoreApplication::processEvents(); QVERIFY(serverDataSource.isNull()); } void TestDataSource::testTargetAccepts_data() { QTest::addColumn("mimeType"); QTest::newRow("empty") << QString(); QTest::newRow("text/plain") << QStringLiteral("text/plain"); } void TestDataSource::testTargetAccepts() { using namespace KWayland::Client; using namespace KWayland::Server; QSignalSpy dataSourceCreatedSpy(m_dataDeviceManagerInterface, SIGNAL(dataSourceCreated(KWayland::Server::DataSourceInterface*))); QVERIFY(dataSourceCreatedSpy.isValid()); QScopedPointer dataSource(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource->isValid()); QSignalSpy targetAcceptsSpy(dataSource.data(), SIGNAL(targetAccepts(QString))); QVERIFY(targetAcceptsSpy.isValid()); QVERIFY(dataSourceCreatedSpy.wait()); QCOMPARE(dataSourceCreatedSpy.count(), 1); QFETCH(QString, mimeType); dataSourceCreatedSpy.first().first().value()->accept(mimeType); QVERIFY(targetAcceptsSpy.wait()); QCOMPARE(targetAcceptsSpy.count(), 1); QCOMPARE(targetAcceptsSpy.first().first().toString(), mimeType); } void TestDataSource::testRequestSend() { using namespace KWayland::Client; using namespace KWayland::Server; QSignalSpy dataSourceCreatedSpy(m_dataDeviceManagerInterface, SIGNAL(dataSourceCreated(KWayland::Server::DataSourceInterface*))); QVERIFY(dataSourceCreatedSpy.isValid()); QScopedPointer dataSource(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource->isValid()); QSignalSpy sendRequestedSpy(dataSource.data(), SIGNAL(sendDataRequested(QString,qint32))); QVERIFY(sendRequestedSpy.isValid()); const QString plain = QStringLiteral("text/plain"); QVERIFY(dataSourceCreatedSpy.wait()); QCOMPARE(dataSourceCreatedSpy.count(), 1); QTemporaryFile file; QVERIFY(file.open()); dataSourceCreatedSpy.first().first().value()->requestData(plain, file.handle()); QVERIFY(sendRequestedSpy.wait()); QCOMPARE(sendRequestedSpy.count(), 1); QCOMPARE(sendRequestedSpy.first().first().toString(), plain); QCOMPARE(sendRequestedSpy.first().last().value(), file.handle()); QVERIFY(sendRequestedSpy.first().last().value() != -1); QFile writeFile; QVERIFY(writeFile.open(sendRequestedSpy.first().last().value(), QFile::WriteOnly, QFileDevice::AutoCloseHandle)); writeFile.close(); } void TestDataSource::testRequestSendOnUnbound() { // this test verifies that the server doesn't crash when requesting a send on an unbound DataSource using namespace KWayland::Client; using namespace KWayland::Server; QSignalSpy dataSourceCreatedSpy(m_dataDeviceManagerInterface, &DataDeviceManagerInterface::dataSourceCreated); QVERIFY(dataSourceCreatedSpy.isValid()); QScopedPointer dataSource(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource->isValid()); QVERIFY(dataSourceCreatedSpy.wait()); QCOMPARE(dataSourceCreatedSpy.count(), 1); auto sds = dataSourceCreatedSpy.first().first().value(); QVERIFY(sds); QSignalSpy unboundSpy(sds, &Resource::unbound); QVERIFY(unboundSpy.isValid()); dataSource.reset(); QVERIFY(unboundSpy.wait()); sds->requestData(QStringLiteral("text/plain"), -1); } void TestDataSource::testCancel() { using namespace KWayland::Client; using namespace KWayland::Server; QSignalSpy dataSourceCreatedSpy(m_dataDeviceManagerInterface, SIGNAL(dataSourceCreated(KWayland::Server::DataSourceInterface*))); QVERIFY(dataSourceCreatedSpy.isValid()); QScopedPointer dataSource(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource->isValid()); QSignalSpy cancelledSpy(dataSource.data(), SIGNAL(cancelled())); QVERIFY(cancelledSpy.isValid()); QVERIFY(dataSourceCreatedSpy.wait()); QCOMPARE(cancelledSpy.count(), 0); dataSourceCreatedSpy.first().first().value()->cancel(); QVERIFY(cancelledSpy.wait()); QCOMPARE(cancelledSpy.count(), 1); } void TestDataSource::testServerGet() { using namespace KWayland::Client; using namespace KWayland::Server; QSignalSpy dataSourceCreatedSpy(m_dataDeviceManagerInterface, SIGNAL(dataSourceCreated(KWayland::Server::DataSourceInterface*))); QVERIFY(dataSourceCreatedSpy.isValid()); QScopedPointer dataSource(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource->isValid()); QVERIFY(!DataSourceInterface::get(nullptr)); QVERIFY(dataSourceCreatedSpy.wait()); auto d = dataSourceCreatedSpy.first().first().value(); QCOMPARE(DataSourceInterface::get(d->resource()), d); QVERIFY(!DataSourceInterface::get(nullptr)); } void TestDataSource::testDestroy() { using namespace KWayland::Client; QScopedPointer dataSource(m_dataDeviceManager->createDataSource()); QVERIFY(dataSource->isValid()); connect(m_connection, &ConnectionThread::connectionDied, m_dataDeviceManager, &DataDeviceManager::destroy); connect(m_connection, &ConnectionThread::connectionDied, m_queue, &EventQueue::destroy); connect(m_connection, &ConnectionThread::connectionDied, dataSource.data(), &DataSource::destroy); QSignalSpy connectionDiedSpy(m_connection, SIGNAL(connectionDied())); QVERIFY(connectionDiedSpy.isValid()); delete m_display; m_display = nullptr; QVERIFY(connectionDiedSpy.wait()); // now the pool should be destroyed; QVERIFY(!dataSource->isValid()); // calling destroy again should not fail dataSource->destroy(); } QTEST_GUILESS_MAIN(TestDataSource) #include "test_datasource.moc" diff --git a/src/server/datadevice_interface.cpp b/src/server/datadevice_interface.cpp index e7af140..8ba19ba 100644 --- a/src/server/datadevice_interface.cpp +++ b/src/server/datadevice_interface.cpp @@ -1,350 +1,350 @@ /******************************************************************** 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 "datadevice_interface.h" #include "datadevicemanager_interface.h" #include "dataoffer_interface_p.h" #include "datasource_interface.h" #include "display.h" #include "resource_p.h" #include "pointer_interface.h" #include "seat_interface.h" #include "surface_interface.h" // Wayland #include namespace KWayland { namespace Server { class DataDeviceInterface::Private : public Resource::Private { public: Private(SeatInterface *seat, DataDeviceInterface *q, DataDeviceManagerInterface *manager, wl_resource *parentResource); ~Private(); DataOfferInterface *createDataOffer(DataSourceInterface *source); SeatInterface *seat; DataSourceInterface *source = nullptr; SurfaceInterface *surface = nullptr; SurfaceInterface *icon = nullptr; DataSourceInterface *selection = nullptr; QMetaObject::Connection selectionUnboundConnection; QMetaObject::Connection selectionDestroyedConnection; struct Drag { SurfaceInterface *surface = nullptr; QMetaObject::Connection destroyConnection; QMetaObject::Connection pointerPosConnection; QMetaObject::Connection sourceActionConnection; QMetaObject::Connection targetActionConnection; quint32 serial = 0; }; Drag drag; private: DataDeviceInterface *q_func() { return reinterpret_cast(q); } void startDrag(DataSourceInterface *dataSource, SurfaceInterface *origin, SurfaceInterface *icon, quint32 serial); void setSelection(DataSourceInterface *dataSource); static void startDragCallback(wl_client *client, wl_resource *resource, wl_resource *source, wl_resource *origin, wl_resource *icon, uint32_t serial); static void setSelectionCallback(wl_client *client, wl_resource *resource, wl_resource *source, uint32_t serial); static const struct wl_data_device_interface s_interface; }; #ifndef DOXYGEN_SHOULD_SKIP_THIS const struct wl_data_device_interface DataDeviceInterface::Private::s_interface = { startDragCallback, setSelectionCallback, resourceDestroyedCallback }; #endif DataDeviceInterface::Private::Private(SeatInterface *seat, DataDeviceInterface *q, DataDeviceManagerInterface *manager, wl_resource *parentResource) : Resource::Private(q, manager, parentResource, &wl_data_device_interface, &s_interface) , seat(seat) { } DataDeviceInterface::Private::~Private() = default; void DataDeviceInterface::Private::startDragCallback(wl_client *client, wl_resource *resource, wl_resource *source, wl_resource *origin, wl_resource *icon, uint32_t serial) { Q_UNUSED(client) Q_UNUSED(serial) // TODO: verify serial cast(resource)->startDrag(DataSourceInterface::get(source), SurfaceInterface::get(origin), SurfaceInterface::get(icon), serial); } void DataDeviceInterface::Private::startDrag(DataSourceInterface *dataSource, SurfaceInterface *origin, SurfaceInterface *i, quint32 serial) { // TODO: allow touch if (seat->hasImplicitPointerGrab(serial) && seat->focusedPointerSurface() != origin) { wl_resource_post_error(resource, 0, "Surface doesn't have pointer grab"); return; } // TODO: source is allowed to be null, handled client internally! source = dataSource; surface = origin; icon = i; drag.serial = serial; Q_Q(DataDeviceInterface); emit q->dragStarted(); } void DataDeviceInterface::Private::setSelectionCallback(wl_client *client, wl_resource *resource, wl_resource *source, uint32_t serial) { Q_UNUSED(client) Q_UNUSED(serial) // TODO: verify serial cast(resource)->setSelection(DataSourceInterface::get(source)); } void DataDeviceInterface::Private::setSelection(DataSourceInterface *dataSource) { - if (dataSource->supportedDragAndDropActions()) { + if (dataSource && dataSource->supportedDragAndDropActions() && wl_resource_get_version(dataSource->resource()) >= WL_DATA_SOURCE_ACTION_SINCE_VERSION) { wl_resource_post_error(dataSource->resource(), WL_DATA_SOURCE_ERROR_INVALID_SOURCE, "Data source is for drag and drop"); return; } Q_Q(DataDeviceInterface); QObject::disconnect(selectionUnboundConnection); QObject::disconnect(selectionDestroyedConnection); if (selection) { selection->cancel(); } selection = dataSource; if (selection) { auto clearSelection = [this] { setSelection(nullptr); }; selectionUnboundConnection = QObject::connect(selection, &Resource::unbound, q, clearSelection); selectionDestroyedConnection = QObject::connect(selection, &QObject::destroyed, q, clearSelection); emit q->selectionChanged(selection); } else { selectionUnboundConnection = QMetaObject::Connection(); selectionDestroyedConnection = QMetaObject::Connection(); emit q->selectionCleared(); } } DataOfferInterface *DataDeviceInterface::Private::createDataOffer(DataSourceInterface *source) { if (!resource) { return nullptr; } Q_Q(DataDeviceInterface); DataOfferInterface *offer = new DataOfferInterface(source, q, resource); auto c = q->global()->display()->getConnection(wl_resource_get_client(resource)); offer->create(c, wl_resource_get_version(resource), 0); if (!offer->resource()) { // TODO: send error? delete offer; return nullptr; } wl_data_device_send_data_offer(resource, offer->resource()); offer->sendAllOffers(); return offer; } DataDeviceInterface::DataDeviceInterface(SeatInterface *seat, DataDeviceManagerInterface *parent, wl_resource *parentResource) : Resource(new Private(seat, this, parent, parentResource)) { } DataDeviceInterface::~DataDeviceInterface() = default; SeatInterface *DataDeviceInterface::seat() const { Q_D(); return d->seat; } DataSourceInterface *DataDeviceInterface::dragSource() const { Q_D(); return d->source; } SurfaceInterface *DataDeviceInterface::icon() const { Q_D(); return d->icon; } SurfaceInterface *DataDeviceInterface::origin() const { Q_D(); return d->surface; } DataSourceInterface *DataDeviceInterface::selection() const { Q_D(); return d->selection; } void DataDeviceInterface::sendSelection(DataDeviceInterface *other) { Q_D(); auto otherSelection = other->selection(); if (!otherSelection) { sendClearSelection(); return; } auto r = d->createDataOffer(otherSelection); if (!r) { return; } if (!d->resource) { return; } wl_data_device_send_selection(d->resource, r->resource()); } void DataDeviceInterface::sendClearSelection() { Q_D(); if (!d->resource) { return; } wl_data_device_send_selection(d->resource, nullptr); } void DataDeviceInterface::drop() { Q_D(); if (!d->resource) { return; } wl_data_device_send_drop(d->resource); if (d->drag.pointerPosConnection) { disconnect(d->drag.pointerPosConnection); d->drag.pointerPosConnection = QMetaObject::Connection(); } disconnect(d->drag.destroyConnection); d->drag.destroyConnection = QMetaObject::Connection(); d->drag.surface = nullptr; client()->flush(); } void DataDeviceInterface::updateDragTarget(SurfaceInterface *surface, quint32 serial) { Q_D(); if (d->drag.surface) { if (d->resource && d->drag.surface->resource()) { wl_data_device_send_leave(d->resource); } if (d->drag.pointerPosConnection) { disconnect(d->drag.pointerPosConnection); d->drag.pointerPosConnection = QMetaObject::Connection(); } disconnect(d->drag.destroyConnection); d->drag.destroyConnection = QMetaObject::Connection(); d->drag.surface = nullptr; if (d->drag.sourceActionConnection) { disconnect(d->drag.sourceActionConnection); d->drag.sourceActionConnection = QMetaObject::Connection(); } if (d->drag.targetActionConnection) { disconnect(d->drag.targetActionConnection); d->drag.targetActionConnection = QMetaObject::Connection(); } // don't update serial, we need it } if (!surface) { d->seat->dragSource()->dragSource()->dndAction(DataDeviceManagerInterface::DnDAction::None); return; } auto *source = d->seat->dragSource()->dragSource(); DataOfferInterface *offer = d->createDataOffer(source); d->drag.surface = surface; if (d->seat->isDragPointer()) { d->drag.pointerPosConnection = connect(d->seat, &SeatInterface::pointerPosChanged, this, [this] { Q_D(); const QPointF pos = d->seat->dragSurfaceTransformation().map(d->seat->pointerPos()); wl_data_device_send_motion(d->resource, d->seat->timestamp(), wl_fixed_from_double(pos.x()), wl_fixed_from_double(pos.y())); client()->flush(); } ); } // TODO: same for touch d->drag.destroyConnection = connect(d->drag.surface, &QObject::destroyed, this, [this] { Q_D(); if (d->resource) { wl_data_device_send_leave(d->resource); } if (d->drag.pointerPosConnection) { disconnect(d->drag.pointerPosConnection); } d->drag = Private::Drag(); } ); // TODO: handle touch position const QPointF pos = d->seat->dragSurfaceTransformation().map(d->seat->pointerPos()); wl_data_device_send_enter(d->resource, serial, surface->resource(), wl_fixed_from_double(pos.x()), wl_fixed_from_double(pos.y()), offer ? offer->resource() : nullptr); if (offer) { offer->d_func()->sendSourceActions(); auto matchOffers = [source, offer] { DataDeviceManagerInterface::DnDAction action{DataDeviceManagerInterface::DnDAction::None}; if (source->supportedDragAndDropActions().testFlag(offer->preferredDragAndDropAction())) { action = offer->preferredDragAndDropAction(); } else { if (source->supportedDragAndDropActions().testFlag(DataDeviceManagerInterface::DnDAction::Copy) && offer->supportedDragAndDropActions().testFlag(DataDeviceManagerInterface::DnDAction::Copy)) { action = DataDeviceManagerInterface::DnDAction::Copy; } else if (source->supportedDragAndDropActions().testFlag(DataDeviceManagerInterface::DnDAction::Move) && offer->supportedDragAndDropActions().testFlag(DataDeviceManagerInterface::DnDAction::Move)) { action = DataDeviceManagerInterface::DnDAction::Move; } else if (source->supportedDragAndDropActions().testFlag(DataDeviceManagerInterface::DnDAction::Ask) && offer->supportedDragAndDropActions().testFlag(DataDeviceManagerInterface::DnDAction::Ask)) { action = DataDeviceManagerInterface::DnDAction::Ask; } } offer->dndAction(action); source->dndAction(action); }; d->drag.targetActionConnection = connect(offer, &DataOfferInterface::dragAndDropActionsChanged, offer, matchOffers); d->drag.sourceActionConnection = connect(source, &DataSourceInterface::supportedDragAndDropActionsChanged, source, matchOffers); } d->client->flush(); } quint32 DataDeviceInterface::dragImplicitGrabSerial() const { Q_D(); return d->drag.serial; } DataDeviceInterface::Private *DataDeviceInterface::d_func() const { return reinterpret_cast(d.data()); } } } diff --git a/src/server/datasource_interface.cpp b/src/server/datasource_interface.cpp index 96f277f..8ebb240 100644 --- a/src/server/datasource_interface.cpp +++ b/src/server/datasource_interface.cpp @@ -1,203 +1,206 @@ /******************************************************************** 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 "datasource_interface.h" #include "datadevicemanager_interface.h" #include "clientconnection.h" #include "resource_p.h" // Qt #include // Wayland #include // system #include namespace KWayland { namespace Server { class DataSourceInterface::Private : public Resource::Private { public: Private(DataSourceInterface *q, DataDeviceManagerInterface *parent, wl_resource *parentResource); ~Private(); QStringList mimeTypes; - // sensible default for < version 3 - DataDeviceManagerInterface::DnDActions supportedDnDActions = DataDeviceManagerInterface::DnDAction::Copy; + DataDeviceManagerInterface::DnDActions supportedDnDActions = DataDeviceManagerInterface::DnDAction::None; private: DataSourceInterface *q_func() { return reinterpret_cast(q); } void offer(const QString &mimeType); static void offerCallback(wl_client *client, wl_resource *resource, const char *mimeType); static void setActionsCallback(wl_client *client, wl_resource *resource, uint32_t dnd_actions); const static struct wl_data_source_interface s_interface; }; #ifndef DOXYGEN_SHOULD_SKIP_THIS const struct wl_data_source_interface DataSourceInterface::Private::s_interface = { offerCallback, resourceDestroyedCallback, setActionsCallback }; #endif DataSourceInterface::Private::Private(DataSourceInterface *q, DataDeviceManagerInterface *parent, wl_resource *parentResource) : Resource::Private(q, parent, parentResource, &wl_data_source_interface, &s_interface) { } DataSourceInterface::Private::~Private() = default; void DataSourceInterface::Private::offerCallback(wl_client *client, wl_resource *resource, const char *mimeType) { Q_UNUSED(client) cast(resource)->offer(QString::fromUtf8(mimeType)); } void DataSourceInterface::Private::offer(const QString &mimeType) { mimeTypes << mimeType; Q_Q(DataSourceInterface); emit q->mimeTypeOffered(mimeType); } void DataSourceInterface::Private::setActionsCallback(wl_client *client, wl_resource *resource, uint32_t dnd_actions) { Q_UNUSED(client) DataDeviceManagerInterface::DnDActions supportedActions; if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY) { supportedActions |= DataDeviceManagerInterface::DnDAction::Copy; } if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE) { supportedActions |= DataDeviceManagerInterface::DnDAction::Move; } if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK) { supportedActions |= DataDeviceManagerInterface::DnDAction::Ask; } // verify that the no other actions are sent if (dnd_actions & ~(WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY | WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE | WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)) { wl_resource_post_error(resource, WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK, "Invalid action mask"); return; } auto p = cast(resource); if (p->supportedDnDActions!= supportedActions) { p->supportedDnDActions = supportedActions; emit p->q_func()->supportedDragAndDropActionsChanged(); } } DataSourceInterface::DataSourceInterface(DataDeviceManagerInterface *parent, wl_resource *parentResource) : Resource(new Private(this, parent, parentResource)) { + if (wl_resource_get_version(parentResource) < WL_DATA_SOURCE_ACTION_SINCE_VERSION) { + Q_D(); + d->supportedDnDActions = DataDeviceManagerInterface::DnDAction::Copy; + } } DataSourceInterface::~DataSourceInterface() = default; void DataSourceInterface::accept(const QString &mimeType) { Q_D(); // TODO: does this require a sanity check on the possible mimeType? wl_data_source_send_target(d->resource, mimeType.isEmpty() ? nullptr : mimeType.toUtf8().constData()); } void DataSourceInterface::requestData(const QString &mimeType, qint32 fd) { Q_D(); // TODO: does this require a sanity check on the possible mimeType? if (d->resource) { wl_data_source_send_send(d->resource, mimeType.toUtf8().constData(), int32_t(fd)); } close(fd); } void DataSourceInterface::cancel() { Q_D(); if (!d->resource) { return; } wl_data_source_send_cancelled(d->resource); client()->flush(); } QStringList DataSourceInterface::mimeTypes() const { Q_D(); return d->mimeTypes; } DataSourceInterface *DataSourceInterface::get(wl_resource *native) { return Private::get(native); } DataSourceInterface::Private *DataSourceInterface::d_func() const { return reinterpret_cast(d.data()); } DataDeviceManagerInterface::DnDActions DataSourceInterface::supportedDragAndDropActions() const { Q_D(); return d->supportedDnDActions; } void DataSourceInterface::dropPerformed() { Q_D(); if (wl_resource_get_version(d->resource) < WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION) { return; } wl_data_source_send_dnd_drop_performed(d->resource); } void DataSourceInterface::dndFinished() { Q_D(); if (wl_resource_get_version(d->resource) < WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { return; } wl_data_source_send_dnd_finished(d->resource); } void DataSourceInterface::dndAction(DataDeviceManagerInterface::DnDAction action) { Q_D(); if (wl_resource_get_version(d->resource) < WL_DATA_SOURCE_ACTION_SINCE_VERSION) { return; } uint32_t wlAction = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; if (action == DataDeviceManagerInterface::DnDAction::Copy) { wlAction = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY; } else if (action == DataDeviceManagerInterface::DnDAction::Move ) { wlAction = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE; } else if (action == DataDeviceManagerInterface::DnDAction::Ask) { wlAction = WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK; } wl_data_source_send_action(d->resource, wlAction); } } }