KWaylandServer

datacontroldevice_v1_interface.cpp
1 /*
2  SPDX-FileCopyrightText: 2020 David Edmundson <[email protected]>
3  SPDX-FileCopyrightText: 2021 David Redondo <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7 #include "datacontroldevice_v1_interface.h"
8 #include "datacontroldevicemanager_v1_interface.h"
9 #include "datacontroloffer_v1_interface.h"
10 #include "datacontrolsource_v1_interface.h"
11 #include "display.h"
12 #include "seat_interface.h"
13 #include "seat_interface_p.h"
14 #include "surface_interface.h"
15 
16 // Wayland
17 #include <qwayland-server-wlr-data-control-unstable-v1.h>
18 
19 namespace KWaylandServer
20 {
21 class DataControlDeviceV1InterfacePrivate : public QtWaylandServer::zwlr_data_control_device_v1
22 {
23 public:
24  DataControlDeviceV1InterfacePrivate(DataControlDeviceV1Interface *q, SeatInterface *seat, wl_resource *resource);
25 
26  DataControlOfferV1Interface *createDataOffer(AbstractDataSource *source);
27 
28  DataControlDeviceV1Interface *q;
32 
33 protected:
34  void zwlr_data_control_device_v1_destroy_resource(Resource *resource) override;
35  void zwlr_data_control_device_v1_set_selection(Resource *resource, wl_resource *source) override;
36  void zwlr_data_control_device_v1_set_primary_selection(Resource *resource, struct ::wl_resource *source) override;
37  void zwlr_data_control_device_v1_destroy(Resource *resource) override;
38 };
39 
40 DataControlDeviceV1InterfacePrivate::DataControlDeviceV1InterfacePrivate(DataControlDeviceV1Interface *_q, SeatInterface *seat, wl_resource *resource)
41  : QtWaylandServer::zwlr_data_control_device_v1(resource)
42  , q(_q)
43  , seat(seat)
44 {
45 }
46 
47 void DataControlDeviceV1InterfacePrivate::zwlr_data_control_device_v1_set_selection(Resource *resource, wl_resource *source)
48 {
49  DataControlSourceV1Interface *dataSource = nullptr;
50 
51  if (source) {
52  dataSource = DataControlSourceV1Interface::get(source);
53  Q_ASSERT(dataSource);
54  if (dataSource == seat->selection() || dataSource == seat->primarySelection()) {
55  wl_resource_post_error(resource->handle, error::error_used_source, "source given to set_selection was already used before");
56  return;
57  }
58  }
59  if (selection) {
60  selection->cancel();
61  }
62  selection = dataSource;
63  Q_EMIT q->selectionChanged(selection);
64 }
65 
66 void DataControlDeviceV1InterfacePrivate::zwlr_data_control_device_v1_set_primary_selection(Resource *resource, wl_resource *source)
67 {
68  DataControlSourceV1Interface *dataSource = nullptr;
69 
70  if (source) {
71  dataSource = DataControlSourceV1Interface::get(source);
72  Q_ASSERT(dataSource);
73  if (dataSource == seat->selection() || dataSource == seat->primarySelection()) {
74  wl_resource_post_error(resource->handle, error::error_used_source, "source given to set_primary_selection was already used before");
75  return;
76  }
77  }
78  if (primarySelection) {
79  primarySelection->cancel();
80  }
81  primarySelection = dataSource;
82  Q_EMIT q->primarySelectionChanged(primarySelection);
83 }
84 
85 void DataControlDeviceV1InterfacePrivate::zwlr_data_control_device_v1_destroy(QtWaylandServer::zwlr_data_control_device_v1::Resource *resource)
86 {
87  wl_resource_destroy(resource->handle);
88 }
89 
90 DataControlOfferV1Interface *DataControlDeviceV1InterfacePrivate::createDataOffer(AbstractDataSource *source)
91 {
92  if (!source) {
93  // a data offer can only exist together with a source
94  return nullptr;
95  }
96 
97  wl_resource *data_offer_resource = wl_resource_create(resource()->client(), &zwlr_data_control_offer_v1_interface, resource()->version(), 0);
98  if (!data_offer_resource) {
99  return nullptr;
100  }
101 
102  DataControlOfferV1Interface *offer = new DataControlOfferV1Interface(source, data_offer_resource);
103  send_data_offer(offer->resource());
104  offer->sendAllOffers();
105  return offer;
106 }
107 
108 void DataControlDeviceV1InterfacePrivate::zwlr_data_control_device_v1_destroy_resource(QtWaylandServer::zwlr_data_control_device_v1::Resource *resource)
109 {
110  Q_UNUSED(resource)
111  delete q;
112 }
113 
114 DataControlDeviceV1Interface::DataControlDeviceV1Interface(SeatInterface *seat, wl_resource *resource)
115  : QObject()
116  , d(new DataControlDeviceV1InterfacePrivate(this, seat, resource))
117 {
118  SeatInterfacePrivate *seatPrivate = SeatInterfacePrivate::get(seat);
119  seatPrivate->registerDataControlDevice(this);
120 }
121 
122 DataControlDeviceV1Interface::~DataControlDeviceV1Interface() = default;
123 
124 SeatInterface *DataControlDeviceV1Interface::seat() const
125 {
126  return d->seat;
127 }
128 
129 DataControlSourceV1Interface *DataControlDeviceV1Interface::selection() const
130 {
131  return d->selection;
132 }
133 
134 DataControlSourceV1Interface *DataControlDeviceV1Interface::primarySelection() const
135 {
136  return d->primarySelection;
137 }
138 
139 void DataControlDeviceV1Interface::sendSelection(AbstractDataSource *other)
140 {
141  if (!other) {
142  sendClearSelection();
143  return;
144  }
145  DataControlOfferV1Interface *offer = d->createDataOffer(other);
146  if (!offer) {
147  return;
148  }
149  d->send_selection(offer->resource());
150 }
151 
152 void DataControlDeviceV1Interface::sendClearSelection()
153 {
154  d->send_selection(nullptr);
155 }
156 
157 void DataControlDeviceV1Interface::sendPrimarySelection(KWaylandServer::AbstractDataSource *other)
158 {
159  if (!other) {
160  sendClearPrimarySelection();
161  return;
162  }
163  DataControlOfferV1Interface *offer = d->createDataOffer(other);
164  if (!offer) {
165  return;
166  }
167  d->send_primary_selection(offer->resource());
168 }
169 
170 void DataControlDeviceV1Interface::sendClearPrimarySelection()
171 {
172  d->send_primary_selection(nullptr);
173 }
174 
175 }
The AbstractDataSource class abstracts the data that can be transferred to another client...
Q_EMITQ_EMIT
KDB_EXPORT KDbVersionInfo version()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:08:27 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.