KWayland

datasource_interface.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Martin Gräßlin <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 #include "datasource_interface.h"
7 #include "clientconnection.h"
8 #include "resource_p.h"
9 // Qt
10 #include <QStringList>
11 // Wayland
12 #include <wayland-server.h>
13 // system
14 #include <unistd.h>
15 
16 namespace KWayland
17 {
18 namespace Server
19 {
20 class DataSourceInterface::Private : public Resource::Private
21 {
22 public:
23  Private(DataSourceInterface *q, DataDeviceManagerInterface *parent, wl_resource *parentResource);
24  ~Private() override;
25 
27  DataDeviceManagerInterface::DnDActions supportedDnDActions = DataDeviceManagerInterface::DnDAction::None;
28 
29 private:
30  DataSourceInterface *q_func()
31  {
32  return reinterpret_cast<DataSourceInterface *>(q);
33  }
34  void offer(const QString &mimeType);
35 
36  static void offerCallback(wl_client *client, wl_resource *resource, const char *mimeType);
37  static void setActionsCallback(wl_client *client, wl_resource *resource, uint32_t dnd_actions);
38 
39  const static struct wl_data_source_interface s_interface;
40 };
41 
42 #ifndef K_DOXYGEN
43 const struct wl_data_source_interface DataSourceInterface::Private::s_interface = {offerCallback, resourceDestroyedCallback, setActionsCallback};
44 #endif
45 
46 DataSourceInterface::Private::Private(DataSourceInterface *q, DataDeviceManagerInterface *parent, wl_resource *parentResource)
47  : Resource::Private(q, parent, parentResource, &wl_data_source_interface, &s_interface)
48 {
49 }
50 
51 DataSourceInterface::Private::~Private() = default;
52 
53 void DataSourceInterface::Private::offerCallback(wl_client *client, wl_resource *resource, const char *mimeType)
54 {
55  Q_UNUSED(client)
56  cast<Private>(resource)->offer(QString::fromUtf8(mimeType));
57 }
58 
59 void DataSourceInterface::Private::offer(const QString &mimeType)
60 {
62  Q_Q(DataSourceInterface);
63  Q_EMIT q->mimeTypeOffered(mimeType);
64 }
65 
66 void DataSourceInterface::Private::setActionsCallback(wl_client *client, wl_resource *resource, uint32_t dnd_actions)
67 {
68  Q_UNUSED(client)
69  DataDeviceManagerInterface::DnDActions supportedActions;
70  if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY) {
71  supportedActions |= DataDeviceManagerInterface::DnDAction::Copy;
72  }
73  if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE) {
74  supportedActions |= DataDeviceManagerInterface::DnDAction::Move;
75  }
76  if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK) {
77  supportedActions |= DataDeviceManagerInterface::DnDAction::Ask;
78  }
79  // verify that the no other actions are sent
80  if (dnd_actions & ~(WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY | WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE | WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)) {
81  wl_resource_post_error(resource, WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK, "Invalid action mask");
82  return;
83  }
84  auto p = cast<Private>(resource);
85  if (p->supportedDnDActions != supportedActions) {
86  p->supportedDnDActions = supportedActions;
87  Q_EMIT p->q_func()->supportedDragAndDropActionsChanged();
88  }
89 }
90 
91 DataSourceInterface::DataSourceInterface(DataDeviceManagerInterface *parent, wl_resource *parentResource)
92  : Resource(new Private(this, parent, parentResource))
93 {
94  if (wl_resource_get_version(parentResource) < WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
95  Q_D();
96  d->supportedDnDActions = DataDeviceManagerInterface::DnDAction::Copy;
97  }
98 }
99 
100 DataSourceInterface::~DataSourceInterface() = default;
101 
102 void DataSourceInterface::accept(const QString &mimeType)
103 {
104  Q_D();
105  // TODO: does this require a sanity check on the possible mimeType?
106  wl_data_source_send_target(d->resource, mimeType.isEmpty() ? nullptr : mimeType.toUtf8().constData());
107 }
108 
109 void DataSourceInterface::requestData(const QString &mimeType, qint32 fd)
110 {
111  Q_D();
112  // TODO: does this require a sanity check on the possible mimeType?
113  if (d->resource) {
114  wl_data_source_send_send(d->resource, mimeType.toUtf8().constData(), int32_t(fd));
115  }
116  close(fd);
117 }
118 
119 void DataSourceInterface::cancel()
120 {
121  Q_D();
122  if (!d->resource) {
123  return;
124  }
125  wl_data_source_send_cancelled(d->resource);
126  client()->flush();
127 }
128 
129 QStringList DataSourceInterface::mimeTypes() const
130 {
131  Q_D();
132  return d->mimeTypes;
133 }
134 
135 DataSourceInterface *DataSourceInterface::get(wl_resource *native)
136 {
137  return Private::get<DataSourceInterface>(native);
138 }
139 
140 DataSourceInterface::Private *DataSourceInterface::d_func() const
141 {
142  return reinterpret_cast<DataSourceInterface::Private *>(d.data());
143 }
144 
145 DataDeviceManagerInterface::DnDActions DataSourceInterface::supportedDragAndDropActions() const
146 {
147  Q_D();
148  return d->supportedDnDActions;
149 }
150 
151 void DataSourceInterface::dropPerformed()
152 {
153  Q_D();
154  if (wl_resource_get_version(d->resource) < WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION) {
155  return;
156  }
157  wl_data_source_send_dnd_drop_performed(d->resource);
158 }
159 
160 void DataSourceInterface::dndFinished()
161 {
162  Q_D();
163  if (wl_resource_get_version(d->resource) < WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
164  return;
165  }
166  wl_data_source_send_dnd_finished(d->resource);
167 }
168 
169 void DataSourceInterface::dndAction(DataDeviceManagerInterface::DnDAction action)
170 {
171  Q_D();
172  if (wl_resource_get_version(d->resource) < WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
173  return;
174  }
175  uint32_t wlAction = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
176  if (action == DataDeviceManagerInterface::DnDAction::Copy) {
177  wlAction = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
178  } else if (action == DataDeviceManagerInterface::DnDAction::Move) {
179  wlAction = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
180  } else if (action == DataDeviceManagerInterface::DnDAction::Ask) {
181  wlAction = WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
182  }
183  wl_data_source_send_action(d->resource, wlAction);
184 }
185 
186 }
187 }
QString fromUtf8(const char *str, int size)
wl_resource * parentResource() const
Definition: resource.cpp:91
ClientConnection * client()
Definition: resource.cpp:76
wl_resource * resource()
Definition: resource.cpp:86
KCALUTILS_EXPORT QString mimeType()
const QList< QKeySequence > & close()
DnDAction
Drag and Drop actions supported by the DataSourceInterface.
bool isEmpty() const const
QByteArray toUtf8() const const
const char * constData() const const
QStringList mimeTypes(Mode mode=Writing)
QObject * parent() const const
Q_D(Todo)
virtual QVariant get(ScriptableExtension *callerPrincipal, quint64 objId, const QString &propName)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Jan 30 2023 03:56:23 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.