1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qwaylandprimaryselectionv1_p.h"
5#include "qwaylandinputdevice_p.h"
6#include "qwaylanddisplay_p.h"
7#include "qwaylandmimehelper_p.h"
8
9#include <QtGui/private/qguiapplication_p.h>
10
11#include <qpa/qplatformclipboard.h>
12
13QT_BEGIN_NAMESPACE
14
15namespace QtWaylandClient {
16
17QWaylandPrimarySelectionDeviceManagerV1::QWaylandPrimarySelectionDeviceManagerV1(QWaylandDisplay *display, uint id, uint version)
18 : zwp_primary_selection_device_manager_v1(display->wl_registry(), id, qMin(version, uint(1)))
19 , m_display(display)
20{
21}
22
23QWaylandPrimarySelectionDeviceV1 *QWaylandPrimarySelectionDeviceManagerV1::createDevice(QWaylandInputDevice *seat)
24{
25 return new QWaylandPrimarySelectionDeviceV1(this, seat);
26}
27
28QWaylandPrimarySelectionOfferV1::QWaylandPrimarySelectionOfferV1(QWaylandDisplay *display, ::zwp_primary_selection_offer_v1 *offer)
29 : zwp_primary_selection_offer_v1(offer)
30 , m_display(display)
31 , m_mimeData(new QWaylandMimeData(this))
32{}
33
34void QWaylandPrimarySelectionOfferV1::startReceiving(const QString &mimeType, int fd)
35{
36 receive(mimeType, fd);
37 wl_display_flush(m_display->wl_display());
38}
39
40void QWaylandPrimarySelectionOfferV1::zwp_primary_selection_offer_v1_offer(const QString &mime_type)
41{
42 m_mimeData->appendFormat(mimeType: mime_type);
43}
44
45QWaylandPrimarySelectionDeviceV1::QWaylandPrimarySelectionDeviceV1(
46 QWaylandPrimarySelectionDeviceManagerV1 *manager, QWaylandInputDevice *seat)
47 : QtWayland::zwp_primary_selection_device_v1(manager->get_device(seat->wl_seat()))
48 , m_display(manager->display())
49 , m_seat(seat)
50{
51}
52
53QWaylandPrimarySelectionDeviceV1::~QWaylandPrimarySelectionDeviceV1()
54{
55 destroy();
56}
57
58void QWaylandPrimarySelectionDeviceV1::invalidateSelectionOffer()
59{
60 if (!m_selectionOffer)
61 return;
62
63 m_selectionOffer.reset();
64 QGuiApplicationPrivate::platformIntegration()->clipboard()->emitChanged(mode: QClipboard::Selection);
65}
66
67void QWaylandPrimarySelectionDeviceV1::setSelectionSource(QWaylandPrimarySelectionSourceV1 *source)
68{
69 if (source) {
70 connect(sender: source, signal: &QWaylandPrimarySelectionSourceV1::cancelled, context: this, slot: [this]() {
71 m_selectionSource.reset();
72 QGuiApplicationPrivate::platformIntegration()->clipboard()->emitChanged(mode: QClipboard::Selection);
73 });
74 }
75 set_selection(source ? source->object() : nullptr, m_seat->serial());
76 m_selectionSource.reset(other: source);
77}
78
79void QWaylandPrimarySelectionDeviceV1::zwp_primary_selection_device_v1_data_offer(zwp_primary_selection_offer_v1 *offer)
80{
81 new QWaylandPrimarySelectionOfferV1(m_display, offer);
82}
83
84void QWaylandPrimarySelectionDeviceV1::zwp_primary_selection_device_v1_selection(zwp_primary_selection_offer_v1 *id)
85{
86
87 if (id)
88 m_selectionOffer.reset(other: static_cast<QWaylandPrimarySelectionOfferV1 *>(zwp_primary_selection_offer_v1_get_user_data(id)));
89 else
90 m_selectionOffer.reset();
91
92 QGuiApplicationPrivate::platformIntegration()->clipboard()->emitChanged(mode: QClipboard::Selection);
93}
94
95QWaylandPrimarySelectionSourceV1::QWaylandPrimarySelectionSourceV1(QWaylandPrimarySelectionDeviceManagerV1 *manager, QMimeData *mimeData)
96 : QtWayland::zwp_primary_selection_source_v1(manager->create_source())
97 , m_mimeData(mimeData)
98{
99 if (!mimeData)
100 return;
101 for (auto &format : mimeData->formats())
102 offer(format);
103}
104
105QWaylandPrimarySelectionSourceV1::~QWaylandPrimarySelectionSourceV1()
106{
107 destroy();
108}
109
110void QWaylandPrimarySelectionSourceV1::zwp_primary_selection_source_v1_send(const QString &mime_type, int32_t fd)
111{
112 QByteArray content = QWaylandMimeHelper::getByteArray(mimeData: m_mimeData, mimeType: mime_type);
113 if (!content.isEmpty()) {
114 // Create a sigpipe handler that does nothing, or clients may be forced to terminate
115 // if the pipe is closed in the other end.
116 struct sigaction action, oldAction;
117 action.sa_handler = SIG_IGN;
118 sigemptyset (set: &action.sa_mask);
119 action.sa_flags = 0;
120
121 sigaction(SIGPIPE, act: &action, oact: &oldAction);
122 ssize_t unused = write(fd: fd, buf: content.constData(), n: size_t(content.size()));
123 Q_UNUSED(unused);
124 sigaction(SIGPIPE, act: &oldAction, oact: nullptr);
125 }
126 close(fd: fd);
127}
128
129} // namespace QtWaylandClient
130
131QT_END_NAMESPACE
132
133#include "moc_qwaylandprimaryselectionv1_p.cpp"
134

source code of qtwayland/src/client/qwaylandprimaryselectionv1.cpp