| 1 | // Copyright (C) 2024 Jie Liu <liujie01@kylinos.cn> |
| 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 "qwaylanddatacontrolv1_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 | #include <signal.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | QT_BEGIN_NAMESPACE |
| 17 | |
| 18 | namespace QtWaylandClient { |
| 19 | |
| 20 | QWaylandDataControlManagerV1::QWaylandDataControlManagerV1(QWaylandDisplay *display, uint id, uint version) |
| 21 | : zwlr_data_control_manager_v1(display->wl_registry(), id, qMin(version, uint(2))) |
| 22 | , m_display(display) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | QWaylandDataControlDeviceV1 *QWaylandDataControlManagerV1::createDevice(QWaylandInputDevice *seat) |
| 27 | { |
| 28 | return new QWaylandDataControlDeviceV1(this, seat); |
| 29 | } |
| 30 | |
| 31 | QWaylandDataControlOfferV1::QWaylandDataControlOfferV1(QWaylandDisplay *display, ::zwlr_data_control_offer_v1 *offer) |
| 32 | : zwlr_data_control_offer_v1(offer) |
| 33 | , m_display(display) |
| 34 | , m_mimeData(new QWaylandMimeData(this)) |
| 35 | {} |
| 36 | |
| 37 | void QWaylandDataControlOfferV1::startReceiving(const QString &mimeType, int fd) |
| 38 | { |
| 39 | receive(mimeType, fd); |
| 40 | wl_display_flush(m_display->wl_display()); |
| 41 | } |
| 42 | |
| 43 | void QWaylandDataControlOfferV1::zwlr_data_control_offer_v1_offer(const QString &mime_type) |
| 44 | { |
| 45 | m_mimeData->appendFormat(mimeType: mime_type); |
| 46 | } |
| 47 | |
| 48 | QWaylandDataControlDeviceV1::QWaylandDataControlDeviceV1( |
| 49 | QWaylandDataControlManagerV1 *manager, QWaylandInputDevice *seat) |
| 50 | : QtWayland::zwlr_data_control_device_v1(manager->get_data_device(seat->wl_seat())) |
| 51 | , m_display(manager->display()) |
| 52 | , m_seat(seat) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | QWaylandDataControlDeviceV1::~QWaylandDataControlDeviceV1() |
| 57 | { |
| 58 | destroy(); |
| 59 | } |
| 60 | |
| 61 | void QWaylandDataControlDeviceV1::invalidateSelectionOffer() |
| 62 | { |
| 63 | if (!m_selectionOffer) |
| 64 | return; |
| 65 | |
| 66 | m_selectionOffer.reset(); |
| 67 | QGuiApplicationPrivate::platformIntegration()->clipboard()->emitChanged(mode: QClipboard::Clipboard); |
| 68 | } |
| 69 | |
| 70 | void QWaylandDataControlDeviceV1::setSelectionSource(QWaylandDataControlSourceV1 *source) |
| 71 | { |
| 72 | if (source) { |
| 73 | connect(sender: source, signal: &QWaylandDataControlSourceV1::cancelled, context: this, slot: [this]() { |
| 74 | m_selectionSource.reset(); |
| 75 | QGuiApplicationPrivate::platformIntegration()->clipboard()->emitChanged(mode: QClipboard::Clipboard); |
| 76 | }); |
| 77 | } |
| 78 | set_selection(source ? source->object() : nullptr); |
| 79 | m_selectionSource.reset(other: source); |
| 80 | } |
| 81 | |
| 82 | void QWaylandDataControlDeviceV1::setPrimarySelectionSource(QWaylandDataControlSourceV1 *source) |
| 83 | { |
| 84 | if (source) { |
| 85 | connect(sender: source, signal: &QWaylandDataControlSourceV1::cancelled, context: this, slot: [this]() { |
| 86 | m_primarySelectionSource.reset(); |
| 87 | QGuiApplicationPrivate::platformIntegration()->clipboard()->emitChanged(mode: QClipboard::Selection); |
| 88 | }); |
| 89 | } |
| 90 | set_primary_selection(source ? source->object() : nullptr); |
| 91 | m_primarySelectionSource.reset(other: source); |
| 92 | } |
| 93 | |
| 94 | void QWaylandDataControlDeviceV1::zwlr_data_control_device_v1_data_offer(zwlr_data_control_offer_v1 *offer) |
| 95 | { |
| 96 | new QWaylandDataControlOfferV1(m_display, offer); |
| 97 | } |
| 98 | |
| 99 | void QWaylandDataControlDeviceV1::zwlr_data_control_device_v1_selection(zwlr_data_control_offer_v1 *id) |
| 100 | { |
| 101 | if (!id) |
| 102 | m_selectionOffer.reset(); |
| 103 | else |
| 104 | m_selectionOffer.reset(other: static_cast<QWaylandDataControlOfferV1 *>(zwlr_data_control_offer_v1_get_user_data(id))); |
| 105 | |
| 106 | // The selection event may be sent before platfrmIntegration is set. |
| 107 | if (auto* integration = QGuiApplicationPrivate::platformIntegration()) |
| 108 | integration->clipboard()->emitChanged(mode: QClipboard::Clipboard); |
| 109 | } |
| 110 | |
| 111 | void QWaylandDataControlDeviceV1::zwlr_data_control_device_v1_finished() |
| 112 | { |
| 113 | m_selectionOffer.reset(); |
| 114 | m_primarySelectionOffer.reset(); |
| 115 | QGuiApplicationPrivate::platformIntegration()->clipboard()->emitChanged(mode: QClipboard::Clipboard); |
| 116 | } |
| 117 | |
| 118 | void QWaylandDataControlDeviceV1::zwlr_data_control_device_v1_primary_selection(struct ::zwlr_data_control_offer_v1 *id) |
| 119 | { |
| 120 | if (!id) |
| 121 | m_primarySelectionOffer.reset(); |
| 122 | else |
| 123 | m_primarySelectionOffer.reset(other: static_cast<QWaylandDataControlOfferV1 *>(zwlr_data_control_offer_v1_get_user_data(id))); |
| 124 | |
| 125 | // The selection event may be sent before platfrmIntegration is set. |
| 126 | if (auto* integration = QGuiApplicationPrivate::platformIntegration()) |
| 127 | integration->clipboard()->emitChanged(mode: QClipboard::Selection); |
| 128 | } |
| 129 | |
| 130 | QWaylandDataControlSourceV1::QWaylandDataControlSourceV1(QWaylandDataControlManagerV1 *manager, QMimeData *mimeData) |
| 131 | : QtWayland::zwlr_data_control_source_v1(manager->create_data_source()) |
| 132 | , m_mimeData(mimeData) |
| 133 | { |
| 134 | if (!mimeData) |
| 135 | return; |
| 136 | for (auto &format : mimeData->formats()) |
| 137 | offer(format); |
| 138 | } |
| 139 | |
| 140 | QWaylandDataControlSourceV1::~QWaylandDataControlSourceV1() |
| 141 | { |
| 142 | destroy(); |
| 143 | } |
| 144 | |
| 145 | void QWaylandDataControlSourceV1::zwlr_data_control_source_v1_send(const QString &mime_type, int32_t fd) |
| 146 | { |
| 147 | QByteArray content = QWaylandMimeHelper::getByteArray(mimeData: m_mimeData, mimeType: mime_type); |
| 148 | if (!content.isEmpty()) { |
| 149 | // Create a sigpipe handler that does nothing, or clients may be forced to terminate |
| 150 | // if the pipe is closed in the other end. |
| 151 | struct sigaction action, oldAction; |
| 152 | action.sa_handler = SIG_IGN; |
| 153 | sigemptyset (set: &action.sa_mask); |
| 154 | action.sa_flags = 0; |
| 155 | |
| 156 | sigaction(SIGPIPE, act: &action, oact: &oldAction); |
| 157 | ssize_t unused = write(fd: fd, buf: content.constData(), n: size_t(content.size())); |
| 158 | Q_UNUSED(unused); |
| 159 | sigaction(SIGPIPE, act: &oldAction, oact: nullptr); |
| 160 | } |
| 161 | close(fd: fd); |
| 162 | } |
| 163 | |
| 164 | } // namespace QtWaylandClient |
| 165 | |
| 166 | QT_END_NAMESPACE |
| 167 | |