File indexing completed on 2024-11-10 04:57:31

0001 /*
0002     SPDX-FileCopyrightText: 2020 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 #include "primaryselectionoffer_v1.h"
0007 #include "primaryselectiondevice_v1.h"
0008 #include "primaryselectionsource_v1.h"
0009 // Qt
0010 #include <QPointer>
0011 #include <QStringList>
0012 // Wayland
0013 #include <qwayland-server-wp-primary-selection-unstable-v1.h>
0014 // system
0015 #include <unistd.h>
0016 
0017 namespace KWin
0018 {
0019 class PrimarySelectionOfferV1InterfacePrivate : public QtWaylandServer::zwp_primary_selection_offer_v1
0020 {
0021 public:
0022     PrimarySelectionOfferV1InterfacePrivate(PrimarySelectionOfferV1Interface *q, AbstractDataSource *source, wl_resource *resource);
0023     PrimarySelectionOfferV1Interface *q;
0024     QPointer<AbstractDataSource> source;
0025 
0026 protected:
0027     void zwp_primary_selection_offer_v1_receive(Resource *resource, const QString &mime_type, int32_t fd) override;
0028     void zwp_primary_selection_offer_v1_destroy(Resource *resource) override;
0029     void zwp_primary_selection_offer_v1_destroy_resource(Resource *resource) override;
0030 };
0031 
0032 PrimarySelectionOfferV1InterfacePrivate::PrimarySelectionOfferV1InterfacePrivate(PrimarySelectionOfferV1Interface *_q,
0033                                                                                  AbstractDataSource *source,
0034                                                                                  wl_resource *resource)
0035     : QtWaylandServer::zwp_primary_selection_offer_v1(resource)
0036     , q(_q)
0037     , source(source)
0038 {
0039 }
0040 
0041 void PrimarySelectionOfferV1InterfacePrivate::zwp_primary_selection_offer_v1_destroy(QtWaylandServer::zwp_primary_selection_offer_v1::Resource *resource)
0042 {
0043     wl_resource_destroy(resource->handle);
0044 }
0045 
0046 void PrimarySelectionOfferV1InterfacePrivate::zwp_primary_selection_offer_v1_destroy_resource(
0047     QtWaylandServer::zwp_primary_selection_offer_v1::Resource *resource)
0048 {
0049     delete q;
0050 }
0051 
0052 void PrimarySelectionOfferV1InterfacePrivate::zwp_primary_selection_offer_v1_receive(Resource *resource, const QString &mimeType, qint32 fd)
0053 {
0054     if (!source) {
0055         close(fd);
0056         return;
0057     }
0058     source->requestData(mimeType, fd);
0059 }
0060 
0061 PrimarySelectionOfferV1Interface::PrimarySelectionOfferV1Interface(AbstractDataSource *source, wl_resource *resource)
0062     : QObject()
0063     , d(new PrimarySelectionOfferV1InterfacePrivate(this, source, resource))
0064 {
0065     Q_ASSERT(source);
0066     connect(source, &AbstractDataSource::mimeTypeOffered, this, [this](const QString &mimeType) {
0067         d->send_offer(mimeType);
0068     });
0069 }
0070 
0071 PrimarySelectionOfferV1Interface::~PrimarySelectionOfferV1Interface() = default;
0072 
0073 void PrimarySelectionOfferV1Interface::sendAllOffers()
0074 {
0075     for (const QString &mimeType : d->source->mimeTypes()) {
0076         d->send_offer(mimeType);
0077     }
0078 }
0079 
0080 wl_resource *PrimarySelectionOfferV1Interface::resource() const
0081 {
0082     return d->resource()->handle;
0083 }
0084 
0085 }
0086 
0087 #include "moc_primaryselectionoffer_v1.cpp"