KWaylandServer

primaryselectiondevice_v1_interface.cpp
1 /*
2  SPDX-FileCopyrightText: 2020 David Edmundson <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 #include "primaryselectiondevice_v1_interface.h"
7 #include "display.h"
8 #include "primaryselectiondevicemanager_v1_interface.h"
9 #include "primaryselectionoffer_v1_interface.h"
10 #include "primaryselectionsource_v1_interface.h"
11 #include "seat_interface.h"
12 #include "seat_interface_p.h"
13 
14 // Wayland
15 #include <qwayland-server-wp-primary-selection-unstable-v1.h>
16 
17 namespace KWaylandServer
18 {
19 class PrimarySelectionDeviceV1InterfacePrivate : public QtWaylandServer::zwp_primary_selection_device_v1
20 {
21 public:
22  PrimarySelectionDeviceV1InterfacePrivate(PrimarySelectionDeviceV1Interface *q, SeatInterface *seat, wl_resource *resource);
23 
24  PrimarySelectionOfferV1Interface *createDataOffer(AbstractDataSource *source);
25 
26  PrimarySelectionDeviceV1Interface *q;
27  SeatInterface *seat;
29 
30 private:
31  void setSelection(PrimarySelectionSourceV1Interface *dataSource);
32 
33 protected:
34  void zwp_primary_selection_device_v1_destroy_resource(Resource *resource) override;
35  void zwp_primary_selection_device_v1_set_selection(Resource *resource, wl_resource *source, uint32_t serial) override;
36  void zwp_primary_selection_device_v1_destroy(Resource *resource) override;
37 };
38 
39 PrimarySelectionDeviceV1InterfacePrivate::PrimarySelectionDeviceV1InterfacePrivate(PrimarySelectionDeviceV1Interface *_q,
40  SeatInterface *seat,
41  wl_resource *resource)
42  : QtWaylandServer::zwp_primary_selection_device_v1(resource)
43  , q(_q)
44  , seat(seat)
45 {
46 }
47 
48 void PrimarySelectionDeviceV1InterfacePrivate::zwp_primary_selection_device_v1_set_selection(Resource *resource, wl_resource *source, uint32_t serial)
49 {
50  Q_UNUSED(resource)
51  Q_UNUSED(serial)
52  PrimarySelectionSourceV1Interface *dataSource = nullptr;
53 
54  if (source) {
55  dataSource = PrimarySelectionSourceV1Interface::get(source);
56  Q_ASSERT(dataSource);
57  }
58 
59  if (selection == dataSource) {
60  return;
61  }
62  if (selection) {
63  selection->cancel();
64  }
65  selection = dataSource;
66  if (selection) {
67  Q_EMIT q->selectionChanged(selection);
68  }
69 }
70 
71 void PrimarySelectionDeviceV1InterfacePrivate::zwp_primary_selection_device_v1_destroy(QtWaylandServer::zwp_primary_selection_device_v1::Resource *resource)
72 {
73  wl_resource_destroy(resource->handle);
74 }
75 
76 PrimarySelectionOfferV1Interface *PrimarySelectionDeviceV1InterfacePrivate::createDataOffer(AbstractDataSource *source)
77 {
78  if (!source) {
79  // a data offer can only exist together with a source
80  return nullptr;
81  }
82 
83  wl_resource *data_offer_resource = wl_resource_create(resource()->client(), &zwp_primary_selection_offer_v1_interface, resource()->version(), 0);
84  if (!data_offer_resource) {
85  wl_resource_post_no_memory(resource()->handle);
86  return nullptr;
87  }
88 
89  PrimarySelectionOfferV1Interface *offer = new PrimarySelectionOfferV1Interface(source, data_offer_resource);
90  send_data_offer(offer->resource());
91  offer->sendAllOffers();
92  return offer;
93 }
94 
95 void PrimarySelectionDeviceV1InterfacePrivate::zwp_primary_selection_device_v1_destroy_resource(
96  QtWaylandServer::zwp_primary_selection_device_v1::Resource *resource)
97 {
98  Q_UNUSED(resource)
99  delete q;
100 }
101 
102 PrimarySelectionDeviceV1Interface::PrimarySelectionDeviceV1Interface(SeatInterface *seat, wl_resource *resource)
103  : QObject()
104  , d(new PrimarySelectionDeviceV1InterfacePrivate(this, seat, resource))
105 {
106  SeatInterfacePrivate *seatPrivate = SeatInterfacePrivate::get(seat);
107  seatPrivate->registerPrimarySelectionDevice(this);
108 }
109 
110 PrimarySelectionDeviceV1Interface::~PrimarySelectionDeviceV1Interface() = default;
111 
112 SeatInterface *PrimarySelectionDeviceV1Interface::seat() const
113 {
114  return d->seat;
115 }
116 
117 PrimarySelectionSourceV1Interface *PrimarySelectionDeviceV1Interface::selection() const
118 {
119  return d->selection;
120 }
121 
122 void PrimarySelectionDeviceV1Interface::sendSelection(AbstractDataSource *other)
123 {
124  if (!other) {
125  sendClearSelection();
126  return;
127  }
128  PrimarySelectionOfferV1Interface *offer = d->createDataOffer(other);
129  if (!offer) {
130  return;
131  }
132  d->send_selection(offer->resource());
133 }
134 
135 void PrimarySelectionDeviceV1Interface::sendClearSelection()
136 {
137  d->send_selection(nullptr);
138 }
139 
140 wl_client *PrimarySelectionDeviceV1Interface::client() const
141 {
142  return d->resource()->client();
143 }
144 
145 }
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:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.