Commit 6156f865 authored by David Edmundson's avatar David Edmundson
Browse files

Add DataControl iface

Summary:
This patch makes use of wlroot's DataControl interface to support
clipboard management.

Unlike wl_data_device clipboards are sent on every change to all
watchers.
If the data device has a selection set it updates immediately.

Because it was started a year ago it uses the existing style of
wrapping objects. The unit test uses the new approach.

Test Plan:
Updated kwin
used wlroot's wl-copy, wl-paste which are xclip replacements to
show that the clipboard updated correctly

Reviewers: #kwin

Differential Revision: https://phabricator.kde.org/D29330
parent 149b836d
......@@ -7,6 +7,10 @@ set(SERVER_LIB_SRCS
clientconnection.cpp
compositor_interface.cpp
contrast_interface.cpp
datacontroldevice_interface.cpp
datacontroldevicemanager_interface.cpp
datacontroloffer_interface.cpp
datacontrolsource_interface.cpp
datadevice_interface.cpp
datadevicemanager_interface.cpp
dataoffer_interface.cpp
......@@ -233,6 +237,11 @@ ecm_add_qtwayland_server_protocol(SERVER_LIB_SRCS
BASENAME tablet-unstable-v2
)
ecm_add_qtwayland_server_protocol(SERVER_LIB_SRCS
PROTOCOL ${PROJECT_SOURCE_DIR}/src/protocols/wlr-data-control-unstable-v1.xml
BASENAME wlr-data-control-unstable-v1
)
set(SERVER_GENERATED_SRCS
${CMAKE_CURRENT_BINARY_DIR}/wayland-blur-client-protocol.h
${CMAKE_CURRENT_BINARY_DIR}/wayland-blur-server-protocol.h
......@@ -337,6 +346,10 @@ set(SERVER_LIB_HEADERS
clientconnection.h
compositor_interface.h
contrast_interface.h
datacontroldevice_interface.h
datacontroldevicemanager_interface.h
datacontroloffer_interface.h
datacontrolsource_interface.h
datadevice_interface.h
datadevicemanager_interface.h
dataoffer_interface.h
......
......@@ -39,3 +39,15 @@ add_executable(testTabletInterface test_tablet_interface.cpp ${TABLET_SRCS})
target_link_libraries( testTabletInterface Qt5::Test Plasma::KWaylandServer KF5::WaylandClient Wayland::Client)
add_test(NAME kwayland-testTabletInterface COMMAND testTabletInterface)
ecm_mark_as_test(testTabletInterface)
########################################################
# Test DataControlInterface
########################################################
ecm_add_qtwayland_client_protocol(DATACONTROL_SRCS
PROTOCOL ${PROJECT_SOURCE_DIR}/src/protocols/wlr-data-control-unstable-v1.xml
BASENAME wlr-data-control-unstable-v1
)
add_executable(testDataControlInterface test_datacontrol_interface.cpp ${DATACONTROL_SRCS})
target_link_libraries( testDataControlInterface Qt5::Test Plasma::KWaylandServer KF5::WaylandClient Wayland::Client)
add_test(NAME kwayland-testDataControlInterface COMMAND testDataControlInterface)
ecm_mark_as_test(testDataControlInterface)
/********************************************************************
Copyright 2020 David Edmundson <davidedmundson@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
// Qt
#include <QHash>
#include <QThread>
#include <QtTest>
// WaylandServer
#include "../../src/server/compositor_interface.h"
#include "../../src/server/display.h"
#include "../../src/server/seat_interface.h"
#include "../../src/server/datacontroldevice_interface.h"
#include "../../src/server/datacontroldevicemanager_interface.h"
#include "../../src/server/datacontrolsource_interface.h"
#include <KWayland/Client/compositor.h>
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/event_queue.h>
#include <KWayland/Client/registry.h>
#include <KWayland/Client/seat.h>
#include "qwayland-wlr-data-control-unstable-v1.h"
using namespace KWaylandServer;
// Faux-client API for tests
Q_DECLARE_OPAQUE_POINTER(::zwlr_data_control_offer_v1*);
Q_DECLARE_METATYPE(::zwlr_data_control_offer_v1*);
class DataControlDeviceManager : public QObject, public QtWayland::zwlr_data_control_manager_v1
{
Q_OBJECT
};
class DataControlOffer: public QObject, public QtWayland::zwlr_data_control_offer_v1
{
Q_OBJECT
public:
QStringList receivedOffers() {
return m_receivedOffers;
}
protected:
virtual void zwlr_data_control_offer_v1_offer(const QString &mime_type) override {
m_receivedOffers << mime_type;
}
private:
QStringList m_receivedOffers;
};
class DataControlDevice : public QObject, public QtWayland::zwlr_data_control_device_v1
{
Q_OBJECT
Q_SIGNALS:
void dataControlOffer(DataControlOffer *offer); //our event receives a new ID, so we make a new object
void selection(struct ::zwlr_data_control_offer_v1 *id);
protected:
void zwlr_data_control_device_v1_data_offer(struct ::zwlr_data_control_offer_v1 *id) override {
auto offer = new DataControlOffer;
offer->init(id);
Q_EMIT dataControlOffer(offer);
}
void zwlr_data_control_device_v1_selection(struct ::zwlr_data_control_offer_v1 *id) override {
Q_EMIT selection(id);
}
};
class DataControlSource: public QObject, public QtWayland::zwlr_data_control_source_v1
{
Q_OBJECT
};
class TestDataSource : public AbstractDataSource
{
Q_OBJECT
public:
TestDataSource() :
AbstractDataSource(nullptr)
{}
void requestData(const QString &mimeType, qint32 fd) {
Q_UNUSED(mimeType);
Q_UNUSED(fd);
};
void cancel() {};
QStringList mimeTypes() const {
return {"text/test1", "text/test2"};
}
};
// The test itself
class DataControlInterfaceTest : public QObject
{
Q_OBJECT
public:
DataControlInterfaceTest()
{
}
~DataControlInterfaceTest() override;
private Q_SLOTS:
void initTestCase();
void testCopyToControl();
void testCopyFromControl();
private:
KWayland::Client::ConnectionThread *m_connection;
KWayland::Client::EventQueue *m_queue;
KWayland::Client::Compositor *m_clientCompositor;
KWayland::Client::Seat *m_clientSeat = nullptr;
QThread *m_thread;
Display m_display;
SeatInterface *m_seat;
CompositorInterface *m_serverCompositor;
DataControlDeviceManagerInterface *m_dataControlDeviceManagerInterface;
DataControlDeviceManager *m_dataControlDeviceManager;
QVector<SurfaceInterface *> m_surfaces;
};
DataControlInterfaceTest::~DataControlInterfaceTest()
{
if (m_queue) {
delete m_queue;
m_queue = nullptr;
}
if (m_thread) {
m_thread->quit();
m_thread->wait();
delete m_thread;
m_thread = nullptr;
}
delete m_seat;
m_connection->deleteLater();
m_connection = nullptr;
}
static const QString s_socketName = QStringLiteral("kwin-wayland-server-tablet-test-0");
void DataControlInterfaceTest::initTestCase()
{
m_display.setSocketName(s_socketName);
m_display.start();
QVERIFY(m_display.isRunning());
m_seat = m_display.createSeat(this);
m_seat->create();
m_serverCompositor = m_display.createCompositor(this);
m_serverCompositor->create();
m_dataControlDeviceManagerInterface = m_display.createDataControlDeviceManager(this);
QVERIFY(m_serverCompositor->isValid());
// setup connection
m_connection = new KWayland::Client::ConnectionThread;
QSignalSpy connectedSpy(m_connection, &KWayland::Client::ConnectionThread::connected);
m_connection->setSocketName(s_socketName);
m_thread = new QThread(this);
m_connection->moveToThread(m_thread);
m_thread->start();
m_connection->initConnection();
QVERIFY(connectedSpy.wait());
QVERIFY(!m_connection->connections().isEmpty());
m_queue = new KWayland::Client::EventQueue(this);
QVERIFY(!m_queue->isValid());
m_queue->setup(m_connection);
QVERIFY(m_queue->isValid());
auto registry = new KWayland::Client::Registry(this);
connect(registry, &KWayland::Client::Registry::interfaceAnnounced, this, [this, registry](const QByteArray &interface, quint32 name, quint32 version) {
if (interface == "zwlr_data_control_manager_v1") {
m_dataControlDeviceManager = new DataControlDeviceManager;
m_dataControlDeviceManager->init(registry->registry(), name, version);
m_dataControlDeviceManager->setParent(registry);
}
});
connect(registry, &KWayland::Client::Registry::seatAnnounced, this, [this, registry](quint32 name, quint32 version) {
m_clientSeat = registry->createSeat(name, version);
});
registry->setEventQueue(m_queue);
QSignalSpy compositorSpy(registry, &KWayland::Client::Registry::compositorAnnounced);
registry->create(m_connection->display());
QVERIFY(registry->isValid());
registry->setup();
wl_display_flush(m_connection->display());
QVERIFY(compositorSpy.wait());
m_clientCompositor = registry->createCompositor(compositorSpy.first().first().value<quint32>(), compositorSpy.first().last().value<quint32>(), this);
QVERIFY(m_clientCompositor->isValid());
QVERIFY(m_dataControlDeviceManager);
}
void DataControlInterfaceTest::testCopyToControl()
{
// we set a dummy data source on the seat using abstract client directly
// then confirm we receive the offer despite not having a surface
QScopedPointer<DataControlDevice> dataControlDevice(new DataControlDevice);
dataControlDevice->init(m_dataControlDeviceManager->get_data_device(*m_clientSeat));
QSignalSpy newOfferSpy(dataControlDevice.data(), &DataControlDevice::dataControlOffer);
QSignalSpy selectionSpy(dataControlDevice.data(), &DataControlDevice::selection);
auto testSelection = new TestDataSource;
m_seat->setSelection(testSelection);
// selection will be sent after we've been sent a new offer object and the mimes have been sent to that object
selectionSpy.wait();
QCOMPARE(newOfferSpy.count(), 1);
QScopedPointer<DataControlOffer> offer(newOfferSpy.first().first().value<DataControlOffer*>());
QCOMPARE(selectionSpy.first().first().value<struct ::zwlr_data_control_offer_v1*>(), offer->object());
QCOMPARE(offer->receivedOffers().count(), 2);
QCOMPARE(offer->receivedOffers()[0], "text/test1");
QCOMPARE(offer->receivedOffers()[1], "text/test2");
}
void DataControlInterfaceTest::testCopyFromControl()
{
// we create a data device and set a selection
// then confirm the server sees the new selection
QSignalSpy serverSelectionChangedSpy(m_seat, &SeatInterface::selectionChanged);
QScopedPointer<DataControlDevice> dataControlDevice(new DataControlDevice);
dataControlDevice->init(m_dataControlDeviceManager->get_data_device(*m_clientSeat));
QScopedPointer<DataControlSource> source(new DataControlSource);
source->init(m_dataControlDeviceManager->create_data_source());
source->offer("cheese/test1");
source->offer("cheese/test2");
dataControlDevice->set_selection(source->object());
serverSelectionChangedSpy.wait();
QVERIFY(m_seat->selection());
QCOMPARE(m_seat->selection()->mimeTypes(), QStringList({"cheese/test1", "cheese/test2"}));
source->destroy();
}
QTEST_GUILESS_MAIN(DataControlInterfaceTest)
#include "test_datacontrol_interface.moc"
......@@ -43,6 +43,10 @@
#include "xdgshell_stable_interface_p.h"
#include "xdgshell_v5_interface_p.h"
#include "xdgshell_v6_interface_p.h"
#include "xdgdecoration_interface.h"
#include "eglstream_controller_interface.h"
#include "keystate_interface.h"
#include "datacontroldevicemanager_interface.h"
#include <QCoreApplication>
#include <QDebug>
......@@ -513,6 +517,13 @@ TabletManagerInterface *Display::createTabletManagerInterface(QObject *parent)
return d;
}
DataControlDeviceManagerInterface *Display::createDataControlDeviceManager(QObject *parent)
{
auto m = new DataControlDeviceManagerInterface(this, parent);
connect(this, &Display::aboutToTerminate, m, [m] { delete m; });
return m;
}
void Display::createShm()
{
Q_ASSERT(d->display);
......
......@@ -77,6 +77,7 @@ class EglStreamControllerInterface;
class KeyStateInterface;
class LinuxDmabufUnstableV1Interface;
class TabletManagerInterface;
class DataControlDeviceManagerInterface;
/**
* @brief Class holding the Wayland server display loop.
......@@ -307,6 +308,12 @@ public:
*/
EglStreamControllerInterface *createEglStreamControllerInterface(QObject *parent = nullptr);
/**
* Creates the DataControlDeviceManager
*
*/
DataControlDeviceManagerInterface *createDataControlDeviceManager(QObject *parent = nullptr);
/**
* Creates the entry point to support wacom-like tablets and pens.
*
......
Folder for storing external protocols that aren't in released wayland-protocols or Plasma specific.
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="wlr_data_control_unstable_v1">
<copyright>
Copyright © 2018 Simon Ser
Copyright © 2019 Ivan Molodetskikh
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that copyright notice and this permission
notice appear in supporting documentation, and that the name of
the copyright holders not be used in advertising or publicity
pertaining to distribution of the software without specific,
written prior permission. The copyright holders make no
representations about the suitability of this software for any
purpose. It is provided "as is" without express or implied
warranty.
THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
</copyright>
<description summary="control data devices">
This protocol allows a privileged client to control data devices. In
particular, the client will be able to manage the current selection and take
the role of a clipboard manager.
Warning! The protocol described in this file is experimental and
backward incompatible changes may be made. Backward compatible changes
may be added together with the corresponding interface version bump.
Backward incompatible changes are done by bumping the version number in
the protocol and interface names and resetting the interface version.
Once the protocol is to be declared stable, the 'z' prefix and the
version number in the protocol and interface names are removed and the
interface version number is reset.
</description>
<interface name="zwlr_data_control_manager_v1" version="2">
<description summary="manager to control data devices">
This interface is a manager that allows creating per-seat data device
controls.
</description>
<request name="create_data_source">
<description summary="create a new data source">
Create a new data source.
</description>
<arg name="id" type="new_id" interface="zwlr_data_control_source_v1"
summary="data source to create"/>
</request>
<request name="get_data_device">
<description summary="get a data device for a seat">
Create a data device that can be used to manage a seat's selection.
</description>
<arg name="id" type="new_id" interface="zwlr_data_control_device_v1"/>
<arg name="seat" type="object" interface="wl_seat"/>
</request>
<request name="destroy" type="destructor">
<description summary="destroy the manager">
All objects created by the manager will still remain valid, until their
appropriate destroy request has been called.
</description>
</request>
</interface>
<interface name="zwlr_data_control_device_v1" version="2">
<description summary="manage a data device for a seat">
This interface allows a client to manage a seat's selection.
When the seat is destroyed, this object becomes inert.
</description>
<request name="set_selection">
<description summary="copy data to the selection">
This request asks the compositor to set the selection to the data from
the source on behalf of the client.
The given source may not be used in any further set_selection or
set_primary_selection requests. Attempting to use a previously used
source is a protocol error.
To unset the selection, set the source to NULL.
</description>
<arg name="source" type="object" interface="zwlr_data_control_source_v1"
allow-null="true"/>
</request>
<request name="destroy" type="destructor">
<description summary="destroy this data device">
Destroys the data device object.
</description>
</request>
<event name="data_offer">
<description summary="introduce a new wlr_data_control_offer">
The data_offer event introduces a new wlr_data_control_offer object,
which will subsequently be used in either the
wlr_data_control_device.selection event (for the regular clipboard
selections) or the wlr_data_control_device.primary_selection event (for
the primary clipboard selections). Immediately following the
wlr_data_control_device.data_offer event, the new data_offer object
will send out wlr_data_control_offer.offer events to describe the MIME
types it offers.
</description>
<arg name="id" type="new_id" interface="zwlr_data_control_offer_v1"/>
</event>
<event name="selection">
<description summary="advertise new selection">
The selection event is sent out to notify the client of a new
wlr_data_control_offer for the selection for this device. The
wlr_data_control_device.data_offer and the wlr_data_control_offer.offer
events are sent out immediately before this event to introduce the data
offer object. The selection event is sent to a client when a new
selection is set. The wlr_data_control_offer is valid until a new
wlr_data_control_offer or NULL is received. The client must destroy the
previous selection wlr_data_control_offer, if any, upon receiving this
event.
The first selection event is sent upon binding the
wlr_data_control_device object.
</description>
<arg name="id" type="object" interface="zwlr_data_control_offer_v1"
allow-null="true"/>
</event>
<event name="finished">
<description summary="this data control is no longer valid">
This data control object is no longer valid and should be destroyed by
the client.
</description>
</event>
<!-- Version 2 additions -->
<event name="primary_selection" since="2">
<description summary="advertise new primary selection">
The primary_selection event is sent out to notify the client of a new
wlr_data_control_offer for the primary selection for this device. The
wlr_data_control_device.data_offer and the wlr_data_control_offer.offer
events are sent out immediately before this event to introduce the data
offer object. The primary_selection event is sent to a client when a
new primary selection is set. The wlr_data_control_offer is valid until
a new wlr_data_control_offer or NULL is received. The client must
destroy the previous primary selection wlr_data_control_offer, if any,
upon receiving this event.
If the compositor supports primary selection, the first
primary_selection event is sent upon binding the
wlr_data_control_device object.
</description>
<arg name="id" type="object" interface="zwlr_data_control_offer_v1"
allow-null="true"/>
</event>
<request name="set_primary_selection" since="2">
<description summary="copy data to the primary selection">
This request asks the compositor to set the primary selection to the
data from the source on behalf of the client.
The given source may not be used in any further set_selection or
set_primary_selection requests. Attempting to use a previously used
source is a protocol error.
To unset the primary selection, set the source to NULL.
The compositor will ignore this request if it does not support primary
selection.
</description>
<arg name="source" type="object" interface="zwlr_data_control_source_v1"
allow-null="true"/>
</request>
<enum name="error" since="2">
<entry name="used_source" value="1"
summary="source given to set_selection or set_primary_selection was already used before"/>
</enum>
</interface>
<interface name="zwlr_data_control_source_v1" version="1">
<description summary="offer to transfer data">
The wlr_data_control_source object is the source side of a
wlr_data_control_offer. It is created by the source client in a data
transfer and provides a way to describe the offered data and a way to
respond to requests to transfer the data.
</description>
<enum name="error">
<entry name="invalid_offer" value="1"
summary="offer sent after wlr_data_control_device.set_selection"/>
</enum>
<request name="offer">
<description summary="add an offered MIME type">
This request adds a MIME type to the set of MIME types advertised to
targets. Can be called several times to offer multiple types.
Calling this after wlr_data_control_device.set_selection is a protocol
error.
</description>
<arg name="mime_type" type="string"
summary="MIME type offered by the data source"/>
</request>
<request name="destroy" type="destructor">
<description summary="destroy this source">
Destroys the data source object.
</description>
</request>
<event name="send">
<description summary="send the data">
Request for data from the client. Send the data as the specified MIME
type over the passed file descriptor, then close it.
</description>
<arg name="mime_type" type="string" summary="MIME type for the data"/>
<arg name="fd" type="fd" summary="file descriptor for the data"/>
</event>
<event name="cancelled">
<description summary="selection was cancelled">