File indexing completed on 2024-05-19 16:35:25

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 "primaryselectionsource_v1_interface.h"
0007 #include "clientconnection.h"
0008 #include "primaryselectiondevicemanager_v1_interface.h"
0009 #include "utils.h"
0010 // Qt
0011 #include <QStringList>
0012 // Wayland
0013 #include <qwayland-server-wp-primary-selection-unstable-v1.h>
0014 // system
0015 #include <unistd.h>
0016 
0017 namespace KWaylandServer
0018 {
0019 class PrimarySelectionSourceV1InterfacePrivate : public QtWaylandServer::zwp_primary_selection_source_v1
0020 {
0021 public:
0022     PrimarySelectionSourceV1InterfacePrivate(PrimarySelectionSourceV1Interface *q, ::wl_resource *resource);
0023 
0024     QStringList mimeTypes;
0025     PrimarySelectionSourceV1Interface *q;
0026 
0027 protected:
0028     void zwp_primary_selection_source_v1_destroy_resource(Resource *resource) override;
0029     void zwp_primary_selection_source_v1_offer(Resource *resource, const QString &mime_type) override;
0030     void zwp_primary_selection_source_v1_destroy(Resource *resource) override;
0031 };
0032 
0033 PrimarySelectionSourceV1InterfacePrivate::PrimarySelectionSourceV1InterfacePrivate(PrimarySelectionSourceV1Interface *_q, ::wl_resource *resource)
0034     : QtWaylandServer::zwp_primary_selection_source_v1(resource)
0035     , q(_q)
0036 {
0037 }
0038 
0039 void PrimarySelectionSourceV1InterfacePrivate::zwp_primary_selection_source_v1_destroy_resource(
0040     QtWaylandServer::zwp_primary_selection_source_v1::Resource *resource)
0041 {
0042     Q_EMIT q->aboutToBeDestroyed();
0043     delete q;
0044 }
0045 
0046 void PrimarySelectionSourceV1InterfacePrivate::zwp_primary_selection_source_v1_offer(Resource *, const QString &mimeType)
0047 {
0048     mimeTypes << mimeType;
0049     Q_EMIT q->mimeTypeOffered(mimeType);
0050 }
0051 
0052 void PrimarySelectionSourceV1InterfacePrivate::zwp_primary_selection_source_v1_destroy(QtWaylandServer::zwp_primary_selection_source_v1::Resource *resource)
0053 {
0054     wl_resource_destroy(resource->handle);
0055 }
0056 
0057 PrimarySelectionSourceV1Interface::PrimarySelectionSourceV1Interface(::wl_resource *resource)
0058     : d(new PrimarySelectionSourceV1InterfacePrivate(this, resource))
0059 {
0060 }
0061 
0062 PrimarySelectionSourceV1Interface::~PrimarySelectionSourceV1Interface() = default;
0063 
0064 void PrimarySelectionSourceV1Interface::requestData(const QString &mimeType, qint32 fd)
0065 {
0066     d->send_send(mimeType, fd);
0067     close(fd);
0068 }
0069 
0070 void PrimarySelectionSourceV1Interface::cancel()
0071 {
0072     d->send_cancelled();
0073 }
0074 
0075 QStringList PrimarySelectionSourceV1Interface::mimeTypes() const
0076 {
0077     return d->mimeTypes;
0078 }
0079 
0080 wl_client *PrimarySelectionSourceV1Interface::client() const
0081 {
0082     return d->resource()->client();
0083 }
0084 
0085 PrimarySelectionSourceV1Interface *PrimarySelectionSourceV1Interface::get(wl_resource *native)
0086 {
0087     if (auto sourcePrivate = resource_cast<PrimarySelectionSourceV1InterfacePrivate *>(native)) {
0088         return sourcePrivate->q;
0089     }
0090     return nullptr;
0091 }
0092 
0093 }