Skip to content

Commit

Permalink
Wayland: Remove no longer used code
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Nov 11, 2024
1 parent 9583a8d commit 9df7083
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 226 deletions.
224 changes: 0 additions & 224 deletions src/platform/x11/systemclipboard/waylandclipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

Expand Down Expand Up @@ -66,42 +65,6 @@ static inline QStringList imageWriteMimeFormats()

namespace {

class SendThread : public QThread {
public:
SendThread(int fd, const QByteArray &data)
: m_data(data)
, m_fd(fd)
{}

protected:
void run() override {
QFile c;
if (c.open(m_fd, QFile::WriteOnly, QFile::AutoCloseHandle)) {
// Create a sigpipe handler that does nothing, or clients may be forced to terminate
// if the pipe is closed in the other end.
struct sigaction action, oldAction;
action.sa_handler = SIG_IGN;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
sigaction(SIGPIPE, &action, &oldAction);
// Unset O_NONBLOCK
fcntl(m_fd, F_SETFL, 0);
const qint64 written = c.write(m_data);
sigaction(SIGPIPE, &oldAction, nullptr);
c.close();

if (written != m_data.size()) {
qWarning() << "Failed to send all clipobard data; sent"
<< written << "bytes out of" << m_data.size();
}
}
}

private:
QByteArray m_data;
int m_fd;
};

class ReceiveThread : public QThread {
public:
ReceiveThread(int fd)
Expand Down Expand Up @@ -337,106 +300,6 @@ QVariant DataControlOffer::retrieveData(const QString &mimeType, QVariant::Type
return data;
}

class DataControlSource : public QObject, public QtWayland::zwlr_data_control_source_v1
{
Q_OBJECT
public:
DataControlSource(struct ::zwlr_data_control_source_v1 *id, QMimeData *mimeData);

~DataControlSource()
{
if (m_mimeData) {
m_mimeData->deleteLater();
m_mimeData = nullptr;
}
if ( isInitialized() )
destroy();
}

QMimeData *mimeData()
{
return m_mimeData;
}

bool isCancelled() const { return m_cancelled; }

Q_SIGNALS:
void cancelled();

protected:
void zwlr_data_control_source_v1_send(const QString &mime_type, int32_t fd) override;
void zwlr_data_control_source_v1_cancelled() override;

private:
QMimeData *m_mimeData = nullptr;
bool m_cancelled = false;
};

DataControlSource::DataControlSource(struct ::zwlr_data_control_source_v1 *id, QMimeData *mimeData)
: QtWayland::zwlr_data_control_source_v1(id)
, m_mimeData(mimeData)
{
const auto formats = mimeData->formats();
for (const QString &format : formats) {
offer(format);
}
if (mimeData->hasText()) {
// ensure GTK applications get this mimetype to avoid them discarding the offer
offer(QStringLiteral("text/plain;charset=utf-8"));
}

if (mimeData->hasImage()) {
const QStringList imageFormats = imageWriteMimeFormats();
for (const QString &imageFormat : imageFormats) {
if (!formats.contains(imageFormat)) {
offer(imageFormat);
}
}
}
}

void DataControlSource::zwlr_data_control_source_v1_send(const QString &mime_type, int32_t fd)
{
QString send_mime_type = mime_type;
if( send_mime_type == QStringLiteral("text/plain;charset=utf-8")
&& !m_mimeData->hasFormat(QStringLiteral("text/plain;charset=utf-8")) )
{
// if we get a request on the fallback mime, send the data from the original mime type
send_mime_type = QStringLiteral("text/plain");
}

QByteArray ba;
if (m_mimeData->hasImage()) {
// adapted from QInternalMimeData::renderDataHelper
if (mime_type == applicationQtXImageLiteral()) {
QImage image = qvariant_cast<QImage>(m_mimeData->imageData());
QBuffer buf(&ba);
buf.open(QBuffer::WriteOnly);
// would there not be PNG ??
image.save(&buf, "PNG");

} else if (mime_type.startsWith(QLatin1String("image/"))) {
QImage image = qvariant_cast<QImage>(m_mimeData->imageData());
QBuffer buf(&ba);
buf.open(QBuffer::WriteOnly);
image.save(&buf, mime_type.mid(mime_type.indexOf(QLatin1Char('/')) + 1).toLatin1().toUpper().data());
}
// end adapted
} else {
ba = m_mimeData->data(send_mime_type);
}

auto thread = new SendThread(fd, m_mimeData->data(send_mime_type));
connect(thread, &QThread::finished, thread, &QObject::deleteLater);
thread->start();
}

void DataControlSource::zwlr_data_control_source_v1_cancelled()
{
m_cancelled = true;
Q_EMIT cancelled();
}

class DataControlDevice : public QObject, public QtWayland::zwlr_data_control_device_v1
{
Q_OBJECT
Expand All @@ -452,32 +315,20 @@ class DataControlDevice : public QObject, public QtWayland::zwlr_data_control_de
destroy();
}

void setSelection(std::unique_ptr<DataControlSource> selection);
QMimeData *receivedSelection()
{
return m_receivedSelection.get();
}
QMimeData *selection()
{
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:
void receivedSelectionChanged();
void selectionChanged();

void receivedPrimarySelectionChanged();
void primarySelectionChanged();

protected:
void zwlr_data_control_device_v1_data_offer(struct ::zwlr_data_control_offer_v1 *id) override
Expand Down Expand Up @@ -520,38 +371,10 @@ class DataControlDevice : public QObject, public QtWayland::zwlr_data_control_de
}

private:
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<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)
{
m_selection = std::move(selection);
connect(m_selection.get(), &DataControlSource::cancelled, this, [this]() {
m_selection.reset();
Q_EMIT selectionChanged();
});
set_selection(m_selection->object());
Q_EMIT selectionChanged();
}

void DataControlDevice::setPrimarySelection(std::unique_ptr<DataControlSource> selection)
{
m_primarySelection = std::move(selection);
connect(m_primarySelection.get(), &DataControlSource::cancelled, this, [this]() {
m_primarySelection.reset();
Q_EMIT primarySelectionChanged();
});

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();
}
}

WaylandClipboard::WaylandClipboard(QObject *parent)
: QObject(parent)
, m_manager(new DataControlDeviceManager)
Expand All @@ -570,25 +393,12 @@ WaylandClipboard::WaylandClipboard(QObject *parent)
m_device.reset(new DataControlDevice(m_manager->get_data_device(seat)));

connect(m_device.get(), &DataControlDevice::receivedSelectionChanged, this, [this]() {
// When our source is still valid, so the offer is for setting it or we emit changed when it is cancelled
if (!m_device->selection()) {
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]() {
// When our source is still valid, so the offer is for setting it or we emit changed when it is cancelled
if (!m_device->primarySelection()) {
Q_EMIT changed(QClipboard::Selection);
}
});
connect(m_device.get(), &DataControlDevice::primarySelectionChanged, this, [this]() {
Q_EMIT changed(QClipboard::Selection);
});

} else {
m_device.reset();
}
Expand Down Expand Up @@ -616,49 +426,15 @@ WaylandClipboard *WaylandClipboard::createInstance()
return clipboard;
}

void WaylandClipboard::setMimeData(QMimeData *mime, QClipboard::Mode mode)
{
if (!m_device) {
return;
}
std::unique_ptr<DataControlSource> source(new DataControlSource(m_manager->create_data_source(), mime));
if (mode == QClipboard::Clipboard) {
m_device->setSelection(std::move(source));
} else if (mode == QClipboard::Selection) {
m_device->setPrimarySelection(std::move(source));
}
}

void WaylandClipboard::clear(QClipboard::Mode mode)
{
if (!m_device) {
return;
}
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);
}
}
}

const QMimeData *WaylandClipboard::mimeData(QClipboard::Mode mode) const
{
if (!m_device) {
return nullptr;
}

// return our locally set selection if it's not cancelled to avoid copying data to ourselves
if (mode == QClipboard::Clipboard) {
if (m_device->selection()) {
return m_device->selection();
}
return m_device->receivedSelection();
} else if (mode == QClipboard::Selection) {
if (m_device->primarySelection()) {
return m_device->primarySelection();
}
return m_device->receivedPrimarySelection();
}
return nullptr;
Expand Down
2 changes: 0 additions & 2 deletions src/platform/x11/systemclipboard/waylandclipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class WaylandClipboard final : public QObject

~WaylandClipboard();

void setMimeData(QMimeData *mime, QClipboard::Mode mode);
void clear(QClipboard::Mode mode);
const QMimeData *mimeData(QClipboard::Mode mode) const;
bool isActive() const { return m_device != nullptr; }
bool isSelectionSupported() const;
Expand Down

0 comments on commit 9df7083

Please sign in to comment.