From 42432a14fc0fca25fa903b15b7dfab1df0ec9a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Fl=C3=B6ser?= Date: Sun, 26 Nov 2017 16:31:57 +0100 Subject: [PATCH] Add support for version 3 of data device manager interface Summary: The main difference compared to version 2 is additional drag and drop actions. The source and destination can negotiate whether the data should be copied or moved or the user should be asked for either or. This seems to be important for GTK, but is not yet implemented in Qt. The main motivation for adding support is that it is required by SDL to launch on Wayland. BUG: 386993 Test Plan: Extended test case, sdl apps now start Reviewers: #frameworks, #plasma, #kwin Subscribers: plasma-devel Tags: #plasma_on_wayland, #frameworks Differential Revision: https://phabricator.kde.org/D9136 --- .../autotests/client/test_drag_drop.cpp | 29 +++- src/wayland/datadevice_interface.cpp | 51 ++++++- src/wayland/datadevicemanager_interface.cpp | 7 +- src/wayland/datadevicemanager_interface.h | 16 ++- src/wayland/dataoffer_interface.cpp | 130 +++++++++++++++--- src/wayland/dataoffer_interface.h | 27 ++++ src/wayland/datasource_interface.cpp | 72 +++++++++- src/wayland/datasource_interface.h | 30 +++- src/wayland/seat_interface.cpp | 3 + src/wayland/server/dataoffer_interface_p.h | 61 ++++++++ 10 files changed, 393 insertions(+), 33 deletions(-) create mode 100644 src/wayland/server/dataoffer_interface_p.h diff --git a/src/wayland/autotests/client/test_drag_drop.cpp b/src/wayland/autotests/client/test_drag_drop.cpp index afbb2d5c1a..a8d6221816 100644 --- a/src/wayland/autotests/client/test_drag_drop.cpp +++ b/src/wayland/autotests/client/test_drag_drop.cpp @@ -218,6 +218,9 @@ void TestDragAndDrop::testDragAndDrop() auto serverSurface = getServerSurface(); QVERIFY(serverSurface); + QSignalSpy dataSourceSelectedActionChangedSpy(m_dataSource, &DataSource::selectedDragAndDropActionChanged); + QVERIFY(dataSourceSelectedActionChangedSpy.isValid()); + // now we need to pass pointer focus to the Surface and simulate a button press QSignalSpy buttonPressSpy(m_pointer, &Pointer::buttonStateChanged); QVERIFY(buttonPressSpy.isValid()); @@ -234,10 +237,13 @@ void TestDragAndDrop::testDragAndDrop() QVERIFY(dragMotionSpy.isValid()); QSignalSpy pointerMotionSpy(m_pointer, &Pointer::motion); QVERIFY(pointerMotionSpy.isValid()); + QSignalSpy sourceDropSpy(m_dataSource, &DataSource::dragAndDropPerformed); + QVERIFY(sourceDropSpy.isValid()); // now we can start the drag and drop QSignalSpy dragStartedSpy(m_seatInterface, &SeatInterface::dragStarted); QVERIFY(dragStartedSpy.isValid()); + m_dataSource->setDragAndDropActions(DataDeviceManager::DnDAction::Copy | DataDeviceManager::DnDAction::Move); m_dataDevice->startDrag(buttonPressSpy.first().first().value(), m_dataSource, s.data()); QVERIFY(dragStartedSpy.wait()); QCOMPARE(m_seatInterface->dragSurface(), serverSurface); @@ -249,8 +255,20 @@ void TestDragAndDrop::testDragAndDrop() QCOMPARE(dragEnteredSpy.first().first().value(), m_display->serial()); QCOMPARE(dragEnteredSpy.first().last().toPointF(), QPointF(0, 0)); QCOMPARE(m_dataDevice->dragSurface().data(), s.data()); + auto offer = m_dataDevice->dragOffer(); + QVERIFY(offer); + QCOMPARE(offer->selectedDragAndDropAction(), DataDeviceManager::DnDAction::None); + QSignalSpy offerActionChangedSpy(offer, &DataOffer::selectedDragAndDropActionChanged); + QVERIFY(offerActionChangedSpy.isValid()); QCOMPARE(m_dataDevice->dragOffer()->offeredMimeTypes().count(), 1); QCOMPARE(m_dataDevice->dragOffer()->offeredMimeTypes().first().name(), QStringLiteral("text/plain")); + QTRY_COMPARE(offer->sourceDragAndDropActions(), DataDeviceManager::DnDAction::Copy | DataDeviceManager::DnDAction::Move); + offer->setDragAndDropActions(DataDeviceManager::DnDAction::Copy | DataDeviceManager::DnDAction::Move, DataDeviceManager::DnDAction::Move); + QVERIFY(offerActionChangedSpy.wait()); + QCOMPARE(offerActionChangedSpy.count(), 1); + QCOMPARE(offer->selectedDragAndDropAction(), DataDeviceManager::DnDAction::Move); + QCOMPARE(dataSourceSelectedActionChangedSpy.count(), 1); + QCOMPARE(m_dataSource->selectedDragAndDropAction(), DataDeviceManager::DnDAction::Move); // simulate motion m_seatInterface->setTimestamp(3); @@ -261,18 +279,23 @@ void TestDragAndDrop::testDragAndDrop() QCOMPARE(dragMotionSpy.first().last().toUInt(), 3u); // simulate drop - QSignalSpy leftSpy(m_dataDevice, &DataDevice::dragLeft); - QVERIFY(leftSpy.isValid()); QSignalSpy serverDragEndedSpy(m_seatInterface, &SeatInterface::dragEnded); QVERIFY(serverDragEndedSpy.isValid()); QSignalSpy droppedSpy(m_dataDevice, &DataDevice::dropped); QVERIFY(droppedSpy.isValid()); m_seatInterface->setTimestamp(4); m_seatInterface->pointerButtonReleased(1); + QVERIFY(sourceDropSpy.isEmpty()); QVERIFY(droppedSpy.wait()); - QCOMPARE(leftSpy.count(), 1); + QCOMPARE(sourceDropSpy.count(), 1); QCOMPARE(serverDragEndedSpy.count(), 1); + QSignalSpy finishedSpy(m_dataSource, &DataSource::dragAndDropFinished); + QVERIFY(finishedSpy.isValid()); + offer->dragAndDropFinished(); + QVERIFY(finishedSpy.wait()); + delete offer; + // verify that we did not get any further input events QVERIFY(pointerMotionSpy.isEmpty()); QCOMPARE(buttonPressSpy.count(), 1); diff --git a/src/wayland/datadevice_interface.cpp b/src/wayland/datadevice_interface.cpp index efcbdffc41..e7af140590 100644 --- a/src/wayland/datadevice_interface.cpp +++ b/src/wayland/datadevice_interface.cpp @@ -19,7 +19,7 @@ License along with this library. If not, see . *********************************************************************/ #include "datadevice_interface.h" #include "datadevicemanager_interface.h" -#include "dataoffer_interface.h" +#include "dataoffer_interface_p.h" #include "datasource_interface.h" #include "display.h" #include "resource_p.h" @@ -55,6 +55,8 @@ public: SurfaceInterface *surface = nullptr; QMetaObject::Connection destroyConnection; QMetaObject::Connection pointerPosConnection; + QMetaObject::Connection sourceActionConnection; + QMetaObject::Connection targetActionConnection; quint32 serial = 0; }; Drag drag; @@ -121,6 +123,10 @@ void DataDeviceInterface::Private::setSelectionCallback(wl_client *client, wl_re void DataDeviceInterface::Private::setSelection(DataSourceInterface *dataSource) { + if (dataSource->supportedDragAndDropActions()) { + 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); @@ -232,6 +238,13 @@ void DataDeviceInterface::drop() 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(); } @@ -249,12 +262,22 @@ void DataDeviceInterface::updateDragTarget(SurfaceInterface *surface, quint32 se 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; } - DataOfferInterface *offer = d->createDataOffer(d->seat->dragSource()->dragSource()); + 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, @@ -285,6 +308,30 @@ void DataDeviceInterface::updateDragTarget(SurfaceInterface *surface, quint32 se 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(); } diff --git a/src/wayland/datadevicemanager_interface.cpp b/src/wayland/datadevicemanager_interface.cpp index 168f6af6fb..952d21537d 100644 --- a/src/wayland/datadevicemanager_interface.cpp +++ b/src/wayland/datadevicemanager_interface.cpp @@ -18,6 +18,7 @@ You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . *********************************************************************/ #include "datadevicemanager_interface.h" +#include "datasource_interface.h" #include "global_p.h" #include "display.h" #include "seat_interface_p.h" @@ -53,9 +54,9 @@ private: static const qint32 s_dataSourceVersion; }; -const quint32 DataDeviceManagerInterface::Private::s_version = 2; -const qint32 DataDeviceManagerInterface::Private::s_dataDeviceVersion = 2; -const qint32 DataDeviceManagerInterface::Private::s_dataSourceVersion = 1; +const quint32 DataDeviceManagerInterface::Private::s_version = 3; +const qint32 DataDeviceManagerInterface::Private::s_dataDeviceVersion = 3; +const qint32 DataDeviceManagerInterface::Private::s_dataSourceVersion = 3; #ifndef DOXYGEN_SHOULD_SKIP_THIS const struct wl_data_device_manager_interface DataDeviceManagerInterface::Private::s_interface = { diff --git a/src/wayland/datadevicemanager_interface.h b/src/wayland/datadevicemanager_interface.h index 25e07d0de5..bc8d3c5965 100644 --- a/src/wayland/datadevicemanager_interface.h +++ b/src/wayland/datadevicemanager_interface.h @@ -25,7 +25,6 @@ License along with this library. If not, see . #include #include "global.h" #include "datadevice_interface.h" -#include "datasource_interface.h" namespace KWayland { @@ -33,6 +32,7 @@ namespace Server { class Display; +class DataSourceInterface; /** * @brief Represents the Global for wl_data_device_manager interface. @@ -44,6 +44,18 @@ class KWAYLANDSERVER_EXPORT DataDeviceManagerInterface : public Global public: virtual ~DataDeviceManagerInterface(); + /** + * Drag and Drop actions supported by the DataSourceInterface. + * @since 5.XX + **/ + enum class DnDAction { + None = 0, + Copy = 1 << 0, + Move = 1 << 1, + Ask = 1 << 2 + }; + Q_DECLARE_FLAGS(DnDActions, DnDAction) + Q_SIGNALS: void dataSourceCreated(KWayland::Server::DataSourceInterface*); void dataDeviceCreated(KWayland::Server::DataDeviceInterface*); @@ -57,4 +69,6 @@ private: } } +Q_DECLARE_OPERATORS_FOR_FLAGS(KWayland::Server::DataDeviceManagerInterface::DnDActions) + #endif diff --git a/src/wayland/dataoffer_interface.cpp b/src/wayland/dataoffer_interface.cpp index 0b04653f7f..b15ede8270 100644 --- a/src/wayland/dataoffer_interface.cpp +++ b/src/wayland/dataoffer_interface.cpp @@ -17,10 +17,9 @@ 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 "dataoffer_interface.h" +#include "dataoffer_interface_p.h" #include "datadevice_interface.h" #include "datasource_interface.h" -#include "resource_p.h" // Qt #include // Wayland @@ -31,30 +30,13 @@ namespace KWayland namespace Server { -class DataOfferInterface::Private : public Resource::Private -{ -public: - Private(DataSourceInterface *source, DataDeviceInterface *parentInterface, DataOfferInterface *q, wl_resource *parentResource); - ~Private(); - DataSourceInterface *source; - DataDeviceInterface *dataDevice; - -private: - DataOfferInterface *q_func() { - return reinterpret_cast(q); - } - void receive(const QString &mimeType, qint32 fd); - static void acceptCallback(wl_client *client, wl_resource *resource, uint32_t serial, const char *mimeType); - static void receiveCallback(wl_client *client, wl_resource *resource, const char *mimeType, int32_t fd); - - static const struct wl_data_offer_interface s_interface; -}; - #ifndef DOXYGEN_SHOULD_SKIP_THIS const struct wl_data_offer_interface DataOfferInterface::Private::s_interface = { acceptCallback, receiveCallback, - resourceDestroyedCallback + resourceDestroyedCallback, + finishCallback, + setActionsCallback }; #endif @@ -90,6 +72,81 @@ void DataOfferInterface::Private::receive(const QString &mimeType, qint32 fd) source->requestData(mimeType, fd); } +void DataOfferInterface::Private::finishCallback(wl_client *client, wl_resource *resource) +{ + Q_UNUSED(client) + auto p = cast(resource); + if (!p->source) { + return; + } + p->source->dndFinished(); + // TODO: It is a client error to perform other requests than wl_data_offer.destroy after this one +} + +void DataOfferInterface::Private::setActionsCallback(wl_client *client, wl_resource *resource, uint32_t dnd_actions, uint32_t preferred_action) +{ + // TODO: check it's drag and drop, otherwise send error + 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_OFFER_ERROR_INVALID_ACTION_MASK, "Invalid action mask"); + return; + } + if (preferred_action != WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY && + preferred_action != WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE && + preferred_action != WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK && + preferred_action != WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE) { + wl_resource_post_error(resource, WL_DATA_OFFER_ERROR_INVALID_ACTION, "Invalid preferred action"); + return; + } + + DataDeviceManagerInterface::DnDAction preferredAction = DataDeviceManagerInterface::DnDAction::None; + if (preferred_action == WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY) { + preferredAction = DataDeviceManagerInterface::DnDAction::Copy; + } else if (preferred_action == WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE) { + preferredAction = DataDeviceManagerInterface::DnDAction::Move; + } else if (preferred_action == WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK) { + preferredAction = DataDeviceManagerInterface::DnDAction::Ask; + } + + auto p = cast(resource); + p->supportedDnDActions = supportedActions; + p->preferredDnDAction = preferredAction; + emit p->q_func()->dragAndDropActionsChanged(); +} + +void DataOfferInterface::Private::sendSourceActions() +{ + if (!source) { + return; + } + if (wl_resource_get_version(resource) < WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) { + return; + } + uint32_t wlActions = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; + const auto actions = source->supportedDragAndDropActions(); + if (actions.testFlag(DataDeviceManagerInterface::DnDAction::Copy)) { + wlActions |= WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY; + } + if (actions.testFlag(DataDeviceManagerInterface::DnDAction::Move)) { + wlActions |= WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE; + } + if (actions.testFlag(DataDeviceManagerInterface::DnDAction::Ask)) { + wlActions |= WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK; + } + wl_data_offer_send_source_actions(resource, wlActions); +} + DataOfferInterface::DataOfferInterface(DataSourceInterface *source, DataDeviceInterface *parentInterface, wl_resource *parentResource) : Resource(new Private(source, parentInterface, this, parentResource)) { @@ -126,5 +183,34 @@ DataOfferInterface::Private *DataOfferInterface::d_func() const return reinterpret_cast(d.data()); } +DataDeviceManagerInterface::DnDActions DataOfferInterface::supportedDragAndDropActions() const +{ + Q_D(); + return d->supportedDnDActions; +} + +DataDeviceManagerInterface::DnDAction DataOfferInterface::preferredDragAndDropAction() const +{ + Q_D(); + return d->preferredDnDAction; +} + +void DataOfferInterface::dndAction(DataDeviceManagerInterface::DnDAction action) +{ + Q_D(); + if (wl_resource_get_version(d->resource) < WL_DATA_OFFER_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_offer_send_action(d->resource, wlAction); +} + } } diff --git a/src/wayland/dataoffer_interface.h b/src/wayland/dataoffer_interface.h index 2412fbd0dd..8e415d9344 100644 --- a/src/wayland/dataoffer_interface.h +++ b/src/wayland/dataoffer_interface.h @@ -25,6 +25,7 @@ License along with this library. If not, see . #include #include "resource.h" +#include "datadevicemanager_interface.h" namespace KWayland { @@ -46,6 +47,32 @@ public: void sendAllOffers(); + /** + * @returns The Drag and Drop actions supported by this DataOfferInterface. + * @since 5.42 + **/ + DataDeviceManagerInterface::DnDActions supportedDragAndDropActions() const; + + /** + * @returns The preferred Drag and Drop action of this DataOfferInterface. + * @since 5.42 + **/ + DataDeviceManagerInterface::DnDAction preferredDragAndDropAction() const; + + /** + * This event indicates the @p action selected by the compositor after matching the + * source/destination side actions. Only one action (or none) will be offered here. + * @since 5.42 + **/ + void dndAction(DataDeviceManagerInterface::DnDAction action); + +Q_SIGNALS: + /** + * Emitted whenever the supported or preferred Drag and Drop actions changed. + * @since 5.42 + **/ + void dragAndDropActionsChanged(); + private: friend class DataDeviceInterface; explicit DataOfferInterface(DataSourceInterface *source, DataDeviceInterface *parentInterface, wl_resource *parentResource); diff --git a/src/wayland/datasource_interface.cpp b/src/wayland/datasource_interface.cpp index ee2db953f1..96f277ffb0 100644 --- a/src/wayland/datasource_interface.cpp +++ b/src/wayland/datasource_interface.cpp @@ -40,6 +40,8 @@ public: ~Private(); QStringList mimeTypes; + // sensible default for < version 3 + DataDeviceManagerInterface::DnDActions supportedDnDActions = DataDeviceManagerInterface::DnDAction::Copy; private: DataSourceInterface *q_func() { @@ -48,6 +50,7 @@ private: 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; }; @@ -55,7 +58,8 @@ private: #ifndef DOXYGEN_SHOULD_SKIP_THIS const struct wl_data_source_interface DataSourceInterface::Private::s_interface = { offerCallback, - resourceDestroyedCallback + resourceDestroyedCallback, + setActionsCallback }; #endif @@ -79,6 +83,31 @@ void DataSourceInterface::Private::offer(const QString &mimeType) 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)) { @@ -129,5 +158,46 @@ 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); +} + } } diff --git a/src/wayland/datasource_interface.h b/src/wayland/datasource_interface.h index 4ed5a046c1..48c1340663 100644 --- a/src/wayland/datasource_interface.h +++ b/src/wayland/datasource_interface.h @@ -25,12 +25,12 @@ License along with this library. If not, see . #include #include "resource.h" +#include "datadevicemanager_interface.h" namespace KWayland { namespace Server { -class DataDeviceManagerInterface; /** * @brief Represents the Resource for the wl_data_source interface. @@ -49,8 +49,36 @@ public: static DataSourceInterface *get(wl_resource *native); + /** + * @returns The Drag and Drop actions supported by this DataSourceInterface. + * @since 5.42 + **/ + DataDeviceManagerInterface::DnDActions supportedDragAndDropActions() const; + + /** + * The user performed the drop action during a drag and drop operation. + * @since 5.42 + **/ + void dropPerformed(); + /** + * The drop destination finished interoperating with this data source. + * @since 5.42 + **/ + void dndFinished(); + /** + * This event indicates the @p action selected by the compositor after matching the + * source/destination side actions. Only one action (or none) will be offered here. + * @since 5.42 + **/ + void dndAction(DataDeviceManagerInterface::DnDAction action); + Q_SIGNALS: void mimeTypeOffered(const QString&); + /** + * Emitted whenever this DataSourceInterface changes the supported drag and drop actions + * @since 5.42 + **/ + void supportedDragAndDropActionsChanged(); private: friend class DataDeviceManagerInterface; diff --git a/src/wayland/seat_interface.cpp b/src/wayland/seat_interface.cpp index 7a295f7afa..4029da127d 100644 --- a/src/wayland/seat_interface.cpp +++ b/src/wayland/seat_interface.cpp @@ -350,6 +350,9 @@ void SeatInterface::Private::endDrag(quint32 serial) { auto target = drag.target; QObject::disconnect(drag.destroyConnection); + if (drag.source) { + drag.source->dragSource()->dropPerformed(); + } if (target) { target->drop(); target->updateDragTarget(nullptr, serial); diff --git a/src/wayland/server/dataoffer_interface_p.h b/src/wayland/server/dataoffer_interface_p.h new file mode 100644 index 0000000000..e66c315592 --- /dev/null +++ b/src/wayland/server/dataoffer_interface_p.h @@ -0,0 +1,61 @@ +/******************************************************************** +Copyright 2017 Martin Flöser + +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 . +*********************************************************************/ +#ifndef KWAYLAND_SERVER_DATAOFFERINTERFACE_P_H +#define KWAYLAND_SERVER_DATAOFFERINTERFACE_P_H +#include "dataoffer_interface.h" +#include "datasource_interface.h" +#include "resource_p.h" +#include + +namespace KWayland +{ +namespace Server +{ + +class Q_DECL_HIDDEN DataOfferInterface::Private : public Resource::Private +{ +public: + Private(DataSourceInterface *source, DataDeviceInterface *parentInterface, DataOfferInterface *q, wl_resource *parentResource); + ~Private(); + DataSourceInterface *source; + DataDeviceInterface *dataDevice; + // defaults are set to sensible values for < version 3 interfaces + DataDeviceManagerInterface::DnDActions supportedDnDActions = DataDeviceManagerInterface::DnDAction::Copy | DataDeviceManagerInterface::DnDAction::Move; + DataDeviceManagerInterface::DnDAction preferredDnDAction = DataDeviceManagerInterface::DnDAction::Copy; + + void sendSourceActions(); + +private: + DataOfferInterface *q_func() { + return reinterpret_cast(q); + } + void receive(const QString &mimeType, qint32 fd); + static void acceptCallback(wl_client *client, wl_resource *resource, uint32_t serial, const char *mimeType); + static void receiveCallback(wl_client *client, wl_resource *resource, const char *mimeType, int32_t fd); + static void finishCallback(wl_client *client, wl_resource *resource); + static void setActionsCallback(wl_client *client, wl_resource *resource, uint32_t dnd_actions, uint32_t preferred_action); + + static const struct wl_data_offer_interface s_interface; +}; + +} +} + +#endif