KWin
Loading...
Searching...
No Matches
datasource.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
3 SPDX-FileCopyrightText: 2020 David Edmundson <davidedmundson@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7#include "datasource.h"
8#include "clientconnection.h"
9#include "datadevicemanager.h"
10#include "datasource_p.h"
11#include "utils/resource.h"
12// Qt
13#include <QStringList>
14// Wayland
15#include <qwayland-server-wayland.h>
16// system
17#include <unistd.h>
18
19namespace KWin
20{
22 : QtWaylandServer::wl_data_source(resource)
23 , q(_q)
24{
25}
26
28{
29 Q_EMIT q->aboutToBeDestroyed();
30 delete q;
31}
32
33void DataSourceInterfacePrivate::data_source_offer(QtWaylandServer::wl_data_source::Resource *resource, const QString &mime_type)
34{
35 mimeTypes << mime_type;
36 Q_EMIT q->mimeTypeOffered(mime_type);
37}
38
39void DataSourceInterfacePrivate::data_source_destroy(QtWaylandServer::wl_data_source::Resource *resource)
40{
41 wl_resource_destroy(resource->handle);
42}
43
44void DataSourceInterfacePrivate::offer(const QString &mimeType)
45{
46 mimeTypes << mimeType;
47 Q_EMIT q->mimeTypeOffered(mimeType);
48}
49
50void DataSourceInterfacePrivate::data_source_set_actions(Resource *resource, uint32_t dnd_actions)
51{
52 // verify that the no other actions are sent
53 if (dnd_actions
54 & ~(QtWaylandServer::wl_data_device_manager::dnd_action_copy | QtWaylandServer::wl_data_device_manager::dnd_action_move
55 | QtWaylandServer::wl_data_device_manager::dnd_action_ask)) {
56 wl_resource_post_error(resource->handle, error_invalid_action_mask, "Invalid action mask");
57 return;
58 }
59 DataDeviceManagerInterface::DnDActions supportedActions;
60 if (dnd_actions & QtWaylandServer::wl_data_device_manager::dnd_action_copy) {
61 supportedActions |= DataDeviceManagerInterface::DnDAction::Copy;
62 }
63 if (dnd_actions & QtWaylandServer::wl_data_device_manager::dnd_action_move) {
64 supportedActions |= DataDeviceManagerInterface::DnDAction::Move;
65 }
66 if (dnd_actions & QtWaylandServer::wl_data_device_manager::dnd_action_ask) {
67 supportedActions |= DataDeviceManagerInterface::DnDAction::Ask;
68 }
69 if (supportedDnDActions != supportedActions) {
70 supportedDnDActions = supportedActions;
72 }
73}
74
76{
77 return dataSource->d.get();
78}
79
80DataSourceInterface::DataSourceInterface(wl_resource *resource)
81 : d(new DataSourceInterfacePrivate(this, resource))
82{
83 if (d->resource()->version() < WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
84 d->supportedDnDActions = DataDeviceManagerInterface::DnDAction::Copy;
85 }
86}
87
89
90void DataSourceInterface::accept(const QString &mimeType)
91{
92 d->send_target(mimeType);
93 d->isAccepted = !mimeType.isNull();
94}
95
96void DataSourceInterface::requestData(const QString &mimeType, qint32 fd)
97{
98 d->send_send(mimeType, int32_t(fd));
99 close(fd);
100}
101
103{
104 d->send_cancelled();
105}
106
108{
109 return d->mimeTypes;
110}
111
113{
114 if (auto sourcePrivate = resource_cast<DataSourceInterfacePrivate *>(native)) {
115 return sourcePrivate->q;
116 }
117 return nullptr;
118}
119
120DataDeviceManagerInterface::DnDActions DataSourceInterface::supportedDragAndDropActions() const
121{
122 return d->supportedDnDActions;
123}
124
126{
127 return d->selectedDndAction;
128}
129
131{
132 d->dropPerformed = true;
133 if (d->resource()->version() < WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION) {
134 return;
135 }
136 d->send_dnd_drop_performed();
137}
138
140{
141 if (d->resource()->version() < WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
142 return;
143 }
144 d->send_dnd_finished();
145}
146
148{
149 return d->dropPerformed;
150}
151
153{
154 d->selectedDndAction = action;
155
156 if (d->resource()->version() < WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
157 return;
158 }
159 uint32_t wlAction = QtWaylandServer::wl_data_device_manager::dnd_action_none;
160 if (action == DataDeviceManagerInterface::DnDAction::Copy) {
161 wlAction = QtWaylandServer::wl_data_device_manager::dnd_action_copy;
162 } else if (action == DataDeviceManagerInterface::DnDAction::Move) {
163 wlAction = QtWaylandServer::wl_data_device_manager::dnd_action_move;
164 } else if (action == DataDeviceManagerInterface::DnDAction::Ask) {
165 wlAction = QtWaylandServer::wl_data_device_manager::dnd_action_ask;
166 }
167 d->send_action(wlAction);
168}
169
171{
172 d->isCanceled = true;
173 // for v3 or less, cancel should not be called after a failed drag operation
174 if (wl_resource_get_version(resource()) < 3) {
175 return;
176 }
177 d->send_cancelled();
178}
179
181{
182 return d->isCanceled;
183}
184
186{
187 return d->resource()->handle;
188}
189
191{
192 return d->resource()->client();
193}
194
196{
197 return d->isAccepted;
198}
199
201{
202 d->isAccepted = accepted;
203}
204
206{
207 return d->xdgToplevelDrag;
208}
209
210}
211
212#include "moc_datasource.cpp"
void mimeTypeOffered(const QString &)
void supportedDragAndDropActionsChanged()
Represents the Resource for the wl_data_source interface.
Definition datasource.h:23
bool isAccepted() const override
void dndFinished() override
wl_resource * resource() const
void dndAction(DataDeviceManagerInterface::DnDAction action) override
void requestData(const QString &mimeType, qint32 fd) override
void setAccepted(bool accepted)
void dropPerformed() override
void dndCancelled() override
wl_client * client() const override
static DataSourceInterface * get(wl_resource *native)
void accept(const QString &mimeType) override
QStringList mimeTypes() const override
DataDeviceManagerInterface::DnDActions supportedDragAndDropActions() const override
DataDeviceManagerInterface::DnDAction selectedDndAction() const override
XdgToplevelDragV1Interface * xdgToplevelDrag() const
DataSourceInterfacePrivate(DataSourceInterface *_q, ::wl_resource *resource)
void data_source_offer(Resource *resource, const QString &mime_type) override
void data_source_destroy(Resource *resource) override
DataDeviceManagerInterface::DnDActions supportedDnDActions
void data_source_set_actions(Resource *resource, uint32_t dnd_actions) override
void data_source_destroy_resource(Resource *resource) override
static DataSourceInterfacePrivate * get(DataSourceInterface *dataSource)