clipboard: Sync wayland implementation with that on Plasma

It has a few important issues fixed that should make our life a bit
better.
This commit is contained in:
Aleix Pol 2021-10-14 17:18:48 +02:00 committed by Aleix Pol Gonzalez
parent 89f3dccf32
commit 599bcbac56
4 changed files with 162 additions and 92 deletions

View file

@ -82,13 +82,7 @@ void QClipboardListener::setText(const QString& content)
WaylandClipboardListener::WaylandClipboardListener() WaylandClipboardListener::WaylandClipboardListener()
: m_dataControl(new DataControl(this)) : m_dataControl(new DataControl(this))
{ {
connect(m_dataControl, &DataControl::receivedSelectionChanged, this, [this] { connect(m_dataControl, &DataControl::changed, this, &WaylandClipboardListener::refresh);
refresh(m_dataControl->receivedSelection());
});
connect(m_dataControl, &DataControl::selectionChanged, this, [this] {
refresh(m_dataControl->selection());
});
} }
void WaylandClipboardListener::setText(const QString& content) void WaylandClipboardListener::setText(const QString& content)
@ -96,11 +90,12 @@ void WaylandClipboardListener::setText(const QString& content)
refreshContent(content); refreshContent(content);
auto mime = new QMimeData; auto mime = new QMimeData;
mime->setText(content); mime->setText(content);
m_dataControl->setSelection(mime, true); m_dataControl->setMimeData(mime, QClipboard::Clipboard);
} }
void WaylandClipboardListener::refresh(const QMimeData *mime) void WaylandClipboardListener::refresh()
{ {
const QMimeData *mime = m_dataControl->mimeData(QClipboard::Clipboard);
if (!mime || !mime->hasText()) { if (!mime || !mime->hasText()) {
return; return;
} }
@ -112,4 +107,4 @@ void WaylandClipboardListener::refresh(const QMimeData *mime)
refreshContent(content); refreshContent(content);
Q_EMIT clipboardChanged(content); Q_EMIT clipboardChanged(content);
} }
#endif #endif

View file

@ -66,7 +66,7 @@ public:
void setText(const QString & content) override; void setText(const QString & content) override;
private: private:
void refresh(const QMimeData *mime); void refresh();
DataControl *m_dataControl; DataControl *m_dataControl;
}; };

View file

@ -1,28 +1,40 @@
/* /*
SPDX-FileCopyrightText: 2020 David Edmundson <davidedmundson@kde.org> SPDX-FileCopyrightText: 2020 David Edmundson <davidedmundson@kde.org>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL SPDX-License-Identifier: LGPL-2.0-or-later
*/ */
#include "datacontrol.h" #include "datacontrol.h"
#include "qwayland-wlr-data-control-unstable-v1.h"
#include <QtWaylandClient/QWaylandClientExtension> #include <QDebug>
#include <QFile> #include <QFile>
#include <QFutureWatcher>
#include <QGuiApplication> #include <QGuiApplication>
#include <QPointer>
#include <QMimeData> #include <QMimeData>
#include <QtWaylandClient/QWaylandClientExtension>
#include <qpa/qplatformnativeinterface.h> #include <qpa/qplatformnativeinterface.h>
#include <QVariant>
#include <errno.h>
#include <poll.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <plugin_clipboard_debug.h> #include "qwayland-wlr-data-control-unstable-v1.h"
#include <sys/select.h>
static QString utf8Text()
{
return QStringLiteral("text/plain;charset=utf-8");
}
class DataControlDeviceManager : public QWaylandClientExtensionTemplate<DataControlDeviceManager>, public QtWayland::zwlr_data_control_manager_v1 class DataControlDeviceManager : public QWaylandClientExtensionTemplate<DataControlDeviceManager>, public QtWayland::zwlr_data_control_manager_v1
{ {
Q_OBJECT Q_OBJECT
public: public:
DataControlDeviceManager() DataControlDeviceManager()
: QWaylandClientExtensionTemplate<DataControlDeviceManager>(1) : QWaylandClientExtensionTemplate<DataControlDeviceManager>(2)
{ {
} }
@ -51,9 +63,12 @@ public:
return m_receivedFormats; return m_receivedFormats;
} }
bool hasFormat(const QString &format) const override bool hasFormat(const QString &mimeType) const override
{ {
return m_receivedFormats.contains(format); if (mimeType == QStringLiteral("text/plain") && m_receivedFormats.contains(utf8Text())) {
return true;
}
return m_receivedFormats.contains(mimeType);
} }
protected: protected:
@ -71,18 +86,24 @@ private:
QVariant DataControlOffer::retrieveData(const QString &mimeType, QVariant::Type type) const QVariant DataControlOffer::retrieveData(const QString &mimeType, QVariant::Type type) const
{ {
if (!hasFormat(mimeType)) {
return QVariant();
}
Q_UNUSED(type); Q_UNUSED(type);
QString mime = mimeType;
if (!m_receivedFormats.contains(mimeType)) {
if (mimeType == QStringLiteral("text/plain") && m_receivedFormats.contains(utf8Text())) {
mime = utf8Text();
} else {
return QVariant();
}
}
int pipeFds[2]; int pipeFds[2];
if (pipe(pipeFds) != 0) { if (pipe(pipeFds) != 0) {
return QVariant(); return QVariant();
} }
auto t = const_cast<DataControlOffer *>(this); auto t = const_cast<DataControlOffer *>(this);
t->receive(mimeType, pipeFds[1]); t->receive(mime, pipeFds[1]);
close(pipeFds[1]); close(pipeFds[1]);
@ -101,6 +122,7 @@ QVariant DataControlOffer::retrieveData(const QString &mimeType, QVariant::Type
if (readPipe.open(pipeFds[0], QIODevice::ReadOnly)) { if (readPipe.open(pipeFds[0], QIODevice::ReadOnly)) {
QByteArray data; QByteArray data;
if (readData(pipeFds[0], data)) { if (readData(pipeFds[0], data)) {
close(pipeFds[0]);
return data; return data;
} }
close(pipeFds[0]); close(pipeFds[0]);
@ -112,27 +134,26 @@ QVariant DataControlOffer::retrieveData(const QString &mimeType, QVariant::Type
// true if data is read successfully // true if data is read successfully
bool DataControlOffer::readData(int fd, QByteArray &data) bool DataControlOffer::readData(int fd, QByteArray &data)
{ {
fd_set readset; pollfd pfds[1];
FD_ZERO(&readset); pfds[0].fd = fd;
FD_SET(fd, &readset); pfds[0].events = POLLIN;
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
Q_FOREVER { while (true) {
int ready = select(FD_SETSIZE, &readset, nullptr, nullptr, &timeout); const int ready = poll(pfds, 1, 1000);
if (ready < 0) { if (ready < 0) {
qCWarning(KDECONNECT_PLUGIN_CLIPBOARD) << "DataControlOffer: select() failed"; if (errno != EINTR) {
return false; qWarning("DataControlOffer: poll() failed: %s", strerror(errno));
return false;
}
} else if (ready == 0) { } else if (ready == 0) {
qCWarning(KDECONNECT_PLUGIN_CLIPBOARD) << "DataControlOffer: timeout reading from pipe"; qWarning("DataControlOffer: timeout reading from pipe");
return false; return false;
} else { } else {
char buf[4096]; char buf[4096];
int n = read(fd, buf, sizeof buf); int n = read(fd, buf, sizeof buf);
if (n < 0) { if (n < 0) {
qWarning("DataControlOffer: read() failed"); qWarning("DataControlOffer: read() failed: %s", strerror(errno));
return false; return false;
} else if (n == 0) { } else if (n == 0) {
return true; return true;
@ -177,8 +198,7 @@ DataControlSource::DataControlSource(struct ::zwlr_data_control_source_v1 *id, Q
for (const QString &format : mimeData->formats()) { for (const QString &format : mimeData->formats()) {
offer(format); offer(format);
} }
if(mimeData->hasText()) if (mimeData->hasText()) {
{
// ensure GTK applications get this mimetype to avoid them discarding the offer // ensure GTK applications get this mimetype to avoid them discarding the offer
offer(QStringLiteral("text/plain;charset=utf-8")); offer(QStringLiteral("text/plain;charset=utf-8"));
} }
@ -188,7 +208,7 @@ void DataControlSource::zwlr_data_control_source_v1_send(const QString &mime_typ
{ {
QFile c; QFile c;
QString send_mime_type = mime_type; QString send_mime_type = mime_type;
if(send_mime_type == QStringLiteral("text/plain;charset=utf-8")) { if (send_mime_type == QStringLiteral("text/plain;charset=utf-8")) {
// if we get a request on the fallback mime, send the data from the original mime type // if we get a request on the fallback mime, send the data from the original mime type
send_mime_type = QStringLiteral("text/plain"); send_mime_type = QStringLiteral("text/plain");
} }
@ -227,10 +247,23 @@ public:
return m_selection ? m_selection->mimeData() : nullptr; return m_selection ? m_selection->mimeData() : nullptr;
} }
void setPrimarySelection(std::unique_ptr<DataControlSource> selection);
QMimeData *receivedPrimarySelection()
{
return m_receivedPrimarySelection.get();
}
QMimeData *primarySelection()
{
return m_primarySelection ? m_primarySelection->mimeData() : nullptr;
}
Q_SIGNALS: Q_SIGNALS:
void receivedSelectionChanged(); void receivedSelectionChanged();
void selectionChanged(); void selectionChanged();
void receivedPrimarySelectionChanged();
void primarySelectionChanged();
protected: protected:
void zwlr_data_control_device_v1_data_offer(struct ::zwlr_data_control_offer_v1 *id) override void zwlr_data_control_device_v1_data_offer(struct ::zwlr_data_control_offer_v1 *id) override
{ {
@ -251,9 +284,24 @@ protected:
Q_EMIT receivedSelectionChanged(); Q_EMIT receivedSelectionChanged();
} }
void zwlr_data_control_device_v1_primary_selection(struct ::zwlr_data_control_offer_v1 *id) override
{
if (!id) {
m_receivedPrimarySelection.reset();
} else {
auto deriv = QtWayland::zwlr_data_control_offer_v1::fromObject(id);
auto offer = dynamic_cast<DataControlOffer *>(deriv); // dynamic because of the dual inheritance
m_receivedPrimarySelection.reset(offer);
}
Q_EMIT receivedPrimarySelectionChanged();
}
private: private:
std::unique_ptr<DataControlSource> m_selection; // selection set locally std::unique_ptr<DataControlSource> m_selection; // selection set locally
std::unique_ptr<DataControlOffer> m_receivedSelection; // latest selection set from externally to here std::unique_ptr<DataControlOffer> m_receivedSelection; // latest selection set from externally to here
std::unique_ptr<DataControlSource> m_primarySelection; // selection set locally
std::unique_ptr<DataControlOffer> m_receivedPrimarySelection; // latest selection set from externally to here
}; };
void DataControlDevice::setSelection(std::unique_ptr<DataControlSource> selection) void DataControlDevice::setSelection(std::unique_ptr<DataControlSource> selection)
@ -267,25 +315,27 @@ void DataControlDevice::setSelection(std::unique_ptr<DataControlSource> selectio
Q_EMIT selectionChanged(); Q_EMIT selectionChanged();
} }
class DataControlPrivate void DataControlDevice::setPrimarySelection(std::unique_ptr<DataControlSource> selection)
{ {
public: m_primarySelection = std::move(selection);
DataControlPrivate() connect(m_primarySelection.get(), &DataControlSource::cancelled, this, [this]() {
: m_manager(new DataControlDeviceManager) m_primarySelection.reset();
{} Q_EMIT primarySelectionChanged();
});
std::unique_ptr<DataControlDeviceManager> m_manager;
std::unique_ptr<DataControlDevice> m_device;
};
if (zwlr_data_control_device_v1_get_version(object()) >= ZWLR_DATA_CONTROL_DEVICE_V1_SET_PRIMARY_SELECTION_SINCE_VERSION) {
set_primary_selection(m_primarySelection->object());
Q_EMIT primarySelectionChanged();
}
}
DataControl::DataControl(QObject *parent) DataControl::DataControl(QObject *parent)
: QObject(parent) : QObject(parent)
, d(new DataControlPrivate) , m_manager(new DataControlDeviceManager)
{ {
connect(d->m_manager.get(), &DataControlDeviceManager::activeChanged, this, [this]() { connect(m_manager.get(), &DataControlDeviceManager::activeChanged, this, [this]() {
if (d->m_manager->isActive()) { if (m_manager->isActive()) {
QPlatformNativeInterface *native = qGuiApp->platformNativeInterface(); QPlatformNativeInterface *native = qApp->platformNativeInterface();
if (!native) { if (!native) {
return; return;
} }
@ -294,58 +344,84 @@ DataControl::DataControl(QObject *parent)
return; return;
} }
d->m_device.reset(new DataControlDevice(d->m_manager->get_data_device(seat))); m_device.reset(new DataControlDevice(m_manager->get_data_device(seat)));
connect(m_device.get(), &DataControlDevice::receivedSelectionChanged, this, [this]() {
Q_EMIT changed(QClipboard::Clipboard);
});
connect(m_device.get(), &DataControlDevice::selectionChanged, this, [this]() {
Q_EMIT changed(QClipboard::Clipboard);
});
connect(m_device.get(), &DataControlDevice::receivedPrimarySelectionChanged, this, [this]() {
Q_EMIT changed(QClipboard::Selection);
});
connect(m_device.get(), &DataControlDevice::primarySelectionChanged, this, [this]() {
Q_EMIT changed(QClipboard::Selection);
});
connect(d->m_device.get(), &DataControlDevice::receivedSelectionChanged, this, &DataControl::receivedSelectionChanged);
connect(d->m_device.get(), &DataControlDevice::selectionChanged, this, &DataControl::selectionChanged);
} else { } else {
d->m_device.reset(); m_device.reset();
} }
}); });
} }
DataControl::~DataControl() = default; DataControl::~DataControl() = default;
QMimeData *DataControl::selection() const void DataControl::setMimeData(QMimeData *mime, QClipboard::Mode mode)
{ {
return d->m_device ? d->m_device->selection() : nullptr; if (!m_device) {
}
QMimeData * DataControl::receivedSelection() const
{
return d->m_device ? d->m_device->receivedSelection() : nullptr;
}
void DataControl::setSelection(QMimeData* mime, bool ownMime)
{
if (!d->m_device) {
return; return;
} }
auto source = std::make_unique<DataControlSource>(m_manager->create_data_source(), mime);
auto source = std::make_unique<DataControlSource>(d->m_manager->create_data_source(), mime); if (mode == QClipboard::Clipboard) {
if (ownMime) { m_device->setSelection(std::move(source));
mime->setParent(source.get()); } else if (mode == QClipboard::Selection) {
m_device->setPrimarySelection(std::move(source));
} }
d->m_device->setSelection(std::move(source));
} }
void DataControl::clearSelection() void DataControl::clear(QClipboard::Mode mode)
{ {
if (!d->m_device) { if (!m_device) {
return; return;
} }
d->m_device->set_selection(nullptr); if (mode == QClipboard::Clipboard) {
m_device->set_selection(nullptr);
} else if (mode == QClipboard::Selection) {
if (zwlr_data_control_device_v1_get_version(m_device->object()) >= ZWLR_DATA_CONTROL_DEVICE_V1_SET_PRIMARY_SELECTION_SINCE_VERSION) {
m_device->set_primary_selection(nullptr);
}
}
} }
void DataControl::clearPrimarySelection() const QMimeData *DataControl::mimeData(QClipboard::Mode mode) const
{ {
if (!d->m_device) { if (!m_device) {
return; return nullptr;
} }
if (zwlr_data_control_device_v1_get_version(d->m_device->object()) >= ZWLR_DATA_CONTROL_DEVICE_V1_SET_PRIMARY_SELECTION_SINCE_VERSION) { // return our locally set selection if it's not cancelled to avoid copying data to ourselves
d->m_device->set_primary_selection(nullptr); if (mode == QClipboard::Clipboard) {
if (m_device->selection()) {
return m_device->selection();
}
// This application owns the clipboard via the regular data_device, use it so we don't block ourselves
if (QGuiApplication::clipboard()->ownsClipboard()) {
return QGuiApplication::clipboard()->mimeData(mode);
}
return m_device->receivedSelection();
} else if (mode == QClipboard::Selection) {
if (m_device->primarySelection()) {
return m_device->primarySelection();
}
// This application owns the primary selection via the regular primary_selection_device, use it so we don't block ourselves
if (QGuiApplication::clipboard()->ownsSelection()) {
return QGuiApplication::clipboard()->mimeData(mode);
}
return m_device->receivedPrimarySelection();
} }
return nullptr;
} }
#include "datacontrol.moc" #include "datacontrol.moc"

View file

@ -7,8 +7,11 @@
#pragma once #pragma once
#include <QObject> #include <QObject>
#include <QScopedPointer> #include <QScopedPointer>
#include <QClipboard>
#include <memory>
class DataControlPrivate; class DataControlDevice;
class DataControlDeviceManager;
class QMimeData; class QMimeData;
class DataControl : public QObject class DataControl : public QObject
@ -18,18 +21,14 @@ public:
DataControl(QObject *parent = nullptr); DataControl(QObject *parent = nullptr);
~DataControl() override; ~DataControl() override;
QMimeData* selection() const; const QMimeData *mimeData(QClipboard::Mode mode) const;
QMimeData* receivedSelection() const; void setMimeData(QMimeData *mime, QClipboard::Mode mode);
void clear(QClipboard::Mode mode);
void clearSelection();
void clearPrimarySelection();
void setSelection(QMimeData *mime, bool ownMime);
Q_SIGNALS: Q_SIGNALS:
void receivedSelectionChanged(); void changed(QClipboard::Mode mode);
void selectionChanged();
private: private:
QScopedPointer<DataControlPrivate> d; std::unique_ptr<DataControlDeviceManager> m_manager;
std::unique_ptr<DataControlDevice> m_device;
}; };