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