kwin/src/wayland/dataoffer_interface.cpp
Vlad Zahorodnii 7fffe99328 build: Add -Wno-unused-parameter compiler option
Due to being a compositor, kwin has to conform to some certain
interfaces. It means a lot of virtual functions and function tables to
integrate with C APIs. Naturally, we not always want to use every
argument in such functions.

Since we get -Wunused-parameter from -Wall, we have to plumb those
unused arguments in order to suppress compiler warnings at the moment.

However, I don't think that extra work is worth it. We cannot change or
alter prototypes in any way to fix the warning the desired way. Q_UNUSED
and similar macros are not good indicators of whether an argument is
used too, we tend to overlook putting or removing those macros. I've
also noticed that Q_UNUSED are not used to guide us with the removal no
longer needed parameters.

Therefore, I think it's worth adding -Wno-unused-parameter compiler
option to stop the compiler producing warnings about unused parameters.
It changes nothing except that we don't need to put Q_UNUSED anymore,
which can be really cumbersome sometimes. Note that it doesn't affect
unused variables, you'll still get a -Wunused-variable compiler warning
if a variable is unused.
2022-10-31 15:50:37 +00:00

206 lines
7.7 KiB
C++

/*
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 <QPointer>
#include <QStringList>
// Wayland
#include <qwayland-server-wayland.h>
// system
#include <unistd.h>
namespace KWaylandServer
{
class DataOfferInterfacePrivate : public QtWaylandServer::wl_data_offer
{
public:
DataOfferInterfacePrivate(AbstractDataSource *source, DataOfferInterface *q, wl_resource *resource);
DataOfferInterface *q;
QPointer<AbstractDataSource> source;
std::optional<DataDeviceManagerInterface::DnDActions> supportedDnDActions = std::nullopt;
std::optional<DataDeviceManagerInterface::DnDAction> preferredDnDAction = std::nullopt;
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)
{
// defaults are set to sensible values for < version 3 interfaces
if (wl_resource_get_version(resource) < WL_DATA_OFFER_ACTION_SINCE_VERSION) {
supportedDnDActions = DataDeviceManagerInterface::DnDAction::Copy | DataDeviceManagerInterface::DnDAction::Move;
preferredDnDAction = DataDeviceManagerInterface::DnDAction::Copy;
}
}
void DataOfferInterfacePrivate::data_offer_accept(Resource *resource, uint32_t serial, const QString &mime_type)
{
if (!source) {
return;
}
source->accept(mime_type);
}
void DataOfferInterfacePrivate::data_offer_receive(Resource *resource, const QString &mime_type, int32_t fd)
{
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)
{
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;
}
if (supportedDnDActions != supportedActions || preferredDnDAction != preferredAction) {
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)
{
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);
});
}
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;
}
std::optional<DataDeviceManagerInterface::DnDActions> DataOfferInterface::supportedDragAndDropActions() const
{
return d->supportedDnDActions;
}
std::optional<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);
}
}