kwin/src/wayland/dataoffer_interface.cpp

210 lines
7.6 KiB
C++
Raw Normal View History

/*
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
SPDX-FileCopyrightText: 2020 David Edmundson <davidedmundson@kde.org>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "dataoffer_interface.h"
#include "datadevice_interface.h"
#include "datasource_interface.h"
// Qt
#include <QStringList>
#include <QPointer>
// Wayland
#include <qwayland-server-wayland.h>
// system
#include <unistd.h>
2020-04-29 14:56:38 +00:00
namespace KWaylandServer
{
class DataOfferInterfacePrivate : public QtWaylandServer::wl_data_offer
{
public:
DataOfferInterfacePrivate(AbstractDataSource *source, DataOfferInterface *q, wl_resource *resource);
DataOfferInterface *q;
QPointer<AbstractDataSource> source;
// 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;
protected:
void data_offer_destroy_resource(Resource *resource) override;
void data_offer_accept(Resource *resource, uint32_t serial, const QString &mime_type) override;
void data_offer_receive(Resource *resource, const QString &mime_type, int32_t fd) override;
void data_offer_destroy(Resource *resource) override;
void data_offer_finish(Resource *resource) override;
void data_offer_set_actions(Resource *resource, uint32_t dnd_actions, uint32_t preferred_action) override;
};
DataOfferInterfacePrivate::DataOfferInterfacePrivate(AbstractDataSource *_source, DataOfferInterface *_q, wl_resource *resource)
: QtWaylandServer::wl_data_offer(resource)
, q(_q)
, source(_source)
{
}
void DataOfferInterfacePrivate::data_offer_accept(Resource *resource, uint32_t serial, const QString &mime_type)
{
Q_UNUSED(resource)
Q_UNUSED(serial)
if (!source) {
[server] Implement support for drag'n'drop through pointer device Summary: How drag'n'drop works on Wayland: When a surface has a pointer grab and a button pressed on the surface (implicit grab) the client can initiate a drag'n'drop operation on the data device. For this the client needs to provide a data source describing the data to be transmitted with the drag'n'drop operation. When a drag'n'drop operation is active all pointer events are interpreted as part of the drag'n'drop operation, the pointer device is grabbed. Pointer events are no longer sent to the focused pointer but to the focused data device. When the pointer moves to another surface an enter event is sent to a data device for that surface and a leave event is sent to the data device previously focused. An enter event carries a data offer which is created from the data source for the operation. During pointer motion there is a feedback mechanism. The data offer can signal to the data source that it can or cannot accept the data at the current pointer position. This can be used by the client being dragged from to update the cursor. The drag'n'drop operation ends with the implicit grab being removed, that is the pressed pointer button which triggered the operation gets released. The server sends a drop event to the focused data device. The data transfer can now be started. For that the receiving client creates a pipe and passes the file descriptor through the data offer to the sending data source. The sending client will write into the file descriptor and close it to finish the transfer. Drag'n'drop could also be initiated through a touch device grab, but this is not yet implemented. The implementation in this change focuses on the adjustments for pointer. For the user of the library drag'n'drop is implemented in the SeatInterface. Signals are emitted whenever drag is started or ended. The interaction for pointer events hardly changes. Motion, button press and button release can still be indicated in the same way. If a button release removes the implicit grab the drop is automatically performed, without the user of the library having to do anything. The only change during drag and drop for the library user is that setFocusedPointerSurface is blocked. To update the current drag target the library user should use setDragTarget. Sending the enter/leave to the data device gets performed automatically. The data device which triggered the drag and drop operation is exposed in the SeatInterface. The user of the library should make sure to render the additional drag icon provided on the data device. At least QtWayland based applications will freeze during drag and drop if the icon doesn't get rendered. The implementation is currently still lacking the client side and due to that also auto test. It's currently only tested with QtWayland clients. Reviewers: #plasma, sebas Subscribers: plasma-devel Projects: #plasma Differential Revision: https://phabricator.kde.org/D1046
2016-03-01 06:49:04 +00:00
return;
}
source->accept(mime_type);
}
void DataOfferInterfacePrivate::data_offer_receive(Resource *resource, const QString &mime_type, int32_t fd)
{
Q_UNUSED(resource)
if (!source) {
close(fd);
return;
}
source->requestData(mime_type, fd);
}
void DataOfferInterfacePrivate::data_offer_destroy(QtWaylandServer::wl_data_offer::Resource *resource)
{
wl_resource_destroy(resource->handle);
}
void DataOfferInterfacePrivate::data_offer_finish(Resource *resource)
{
Q_UNUSED(resource)
if (!source) {
return;
}
source->dndFinished();
// TODO: It is a client error to perform other requests than wl_data_offer.destroy after this one
}
void DataOfferInterfacePrivate::data_offer_set_actions(Resource *resource, uint32_t dnd_actions, uint32_t preferred_action)
{
// TODO: check it's drag and drop, otherwise send error
// verify that the no other actions are sent
if (dnd_actions & ~(QtWaylandServer::wl_data_device_manager::dnd_action_copy |
QtWaylandServer::wl_data_device_manager::dnd_action_move |
QtWaylandServer::wl_data_device_manager::dnd_action_ask)) {
wl_resource_post_error(resource->handle, error_invalid_action_mask, "Invalid action mask");
return;
}
if (preferred_action != QtWaylandServer::wl_data_device_manager::dnd_action_copy &&
preferred_action != QtWaylandServer::wl_data_device_manager::dnd_action_move &&
preferred_action != QtWaylandServer::wl_data_device_manager::dnd_action_ask &&
preferred_action != QtWaylandServer::wl_data_device_manager::dnd_action_none) {
wl_resource_post_error(resource->handle, error_invalid_action, "Invalid preferred action");
return;
}
DataDeviceManagerInterface::DnDActions supportedActions;
if (dnd_actions & QtWaylandServer::wl_data_device_manager::dnd_action_copy) {
supportedActions |= DataDeviceManagerInterface::DnDAction::Copy;
}
if (dnd_actions & QtWaylandServer::wl_data_device_manager::dnd_action_move) {
supportedActions |= DataDeviceManagerInterface::DnDAction::Move;
}
if (dnd_actions & QtWaylandServer::wl_data_device_manager::dnd_action_ask) {
supportedActions |= DataDeviceManagerInterface::DnDAction::Ask;
}
DataDeviceManagerInterface::DnDAction preferredAction = DataDeviceManagerInterface::DnDAction::None;
if (preferred_action == QtWaylandServer::wl_data_device_manager::dnd_action_copy) {
preferredAction = DataDeviceManagerInterface::DnDAction::Copy;
} else if (preferred_action == QtWaylandServer::wl_data_device_manager::dnd_action_move) {
preferredAction = DataDeviceManagerInterface::DnDAction::Move;
} else if (preferred_action == QtWaylandServer::wl_data_device_manager::dnd_action_ask) {
preferredAction = DataDeviceManagerInterface::DnDAction::Ask;
}
supportedDnDActions = supportedActions;
preferredDnDAction = preferredAction;
Q_EMIT q->dragAndDropActionsChanged();
}
void DataOfferInterface::sendSourceActions()
{
if (!d->source) {
return;
}
if (d->resource()->version() < WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) {
return;
}
uint32_t wlActions = QtWaylandServer::wl_data_device_manager::dnd_action_none;
const auto actions = d->source->supportedDragAndDropActions();
if (actions.testFlag(DataDeviceManagerInterface::DnDAction::Copy)) {
wlActions |= QtWaylandServer::wl_data_device_manager::dnd_action_copy;
}
if (actions.testFlag(DataDeviceManagerInterface::DnDAction::Move)) {
wlActions |= QtWaylandServer::wl_data_device_manager::dnd_action_move;
}
if (actions.testFlag(DataDeviceManagerInterface::DnDAction::Ask)) {
wlActions |= QtWaylandServer::wl_data_device_manager::dnd_action_ask;
}
d->send_source_actions(wlActions);
}
void DataOfferInterfacePrivate::data_offer_destroy_resource(QtWaylandServer::wl_data_offer::Resource *resource)
{
Q_UNUSED(resource)
delete q;
}
DataOfferInterface::DataOfferInterface(AbstractDataSource *source, wl_resource *resource)
: QObject(nullptr)
, d(new DataOfferInterfacePrivate(source, this, resource))
{
Q_ASSERT(source);
connect(source, &DataSourceInterface::mimeTypeOffered, this,
[this](const QString &mimeType) {
d->send_offer(mimeType);
[server] Implement support for drag'n'drop through pointer device Summary: How drag'n'drop works on Wayland: When a surface has a pointer grab and a button pressed on the surface (implicit grab) the client can initiate a drag'n'drop operation on the data device. For this the client needs to provide a data source describing the data to be transmitted with the drag'n'drop operation. When a drag'n'drop operation is active all pointer events are interpreted as part of the drag'n'drop operation, the pointer device is grabbed. Pointer events are no longer sent to the focused pointer but to the focused data device. When the pointer moves to another surface an enter event is sent to a data device for that surface and a leave event is sent to the data device previously focused. An enter event carries a data offer which is created from the data source for the operation. During pointer motion there is a feedback mechanism. The data offer can signal to the data source that it can or cannot accept the data at the current pointer position. This can be used by the client being dragged from to update the cursor. The drag'n'drop operation ends with the implicit grab being removed, that is the pressed pointer button which triggered the operation gets released. The server sends a drop event to the focused data device. The data transfer can now be started. For that the receiving client creates a pipe and passes the file descriptor through the data offer to the sending data source. The sending client will write into the file descriptor and close it to finish the transfer. Drag'n'drop could also be initiated through a touch device grab, but this is not yet implemented. The implementation in this change focuses on the adjustments for pointer. For the user of the library drag'n'drop is implemented in the SeatInterface. Signals are emitted whenever drag is started or ended. The interaction for pointer events hardly changes. Motion, button press and button release can still be indicated in the same way. If a button release removes the implicit grab the drop is automatically performed, without the user of the library having to do anything. The only change during drag and drop for the library user is that setFocusedPointerSurface is blocked. To update the current drag target the library user should use setDragTarget. Sending the enter/leave to the data device gets performed automatically. The data device which triggered the drag and drop operation is exposed in the SeatInterface. The user of the library should make sure to render the additional drag icon provided on the data device. At least QtWayland based applications will freeze during drag and drop if the icon doesn't get rendered. The implementation is currently still lacking the client side and due to that also auto test. It's currently only tested with QtWayland clients. Reviewers: #plasma, sebas Subscribers: plasma-devel Projects: #plasma Differential Revision: https://phabricator.kde.org/D1046
2016-03-01 06:49:04 +00:00
}
);
}
DataOfferInterface::~DataOfferInterface() = default;
void DataOfferInterface::sendAllOffers()
{
for (const QString &mimeType : d->source->mimeTypes()) {
d->send_offer(mimeType);
}
}
wl_resource *DataOfferInterface::resource() const
{
return d->resource()->handle;
}
DataDeviceManagerInterface::DnDActions DataOfferInterface::supportedDragAndDropActions() const
{
return d->supportedDnDActions;
}
DataDeviceManagerInterface::DnDAction DataOfferInterface::preferredDragAndDropAction() const
{
return d->preferredDnDAction;
}
void DataOfferInterface::dndAction(DataDeviceManagerInterface::DnDAction action)
{
if (d->resource()->version() < WL_DATA_OFFER_ACTION_SINCE_VERSION) {
return;
}
uint32_t wlAction = QtWaylandServer::wl_data_device_manager::dnd_action_none;
if (action == DataDeviceManagerInterface::DnDAction::Copy) {
wlAction = QtWaylandServer::wl_data_device_manager::dnd_action_copy;
} else if (action == DataDeviceManagerInterface::DnDAction::Move ) {
wlAction = QtWaylandServer::wl_data_device_manager::dnd_action_move;
} else if (action == DataDeviceManagerInterface::DnDAction::Ask) {
wlAction = QtWaylandServer::wl_data_device_manager::dnd_action_ask;
}
d->send_action(wlAction);
}
}