Commit a009c8c0 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧
Browse files

Provide an implementation for the tablet interface

Summary:
Implements the necessary classes to have proper support for the tablet and pen.
Doesn't implement yet the ring/slider/totem bits.

Test Plan:
Used it with Plasma and GTK clients, together with the kwin patch I'll submit soon.
https://youtu.be/GGx0TlNJlzs

Also I added a test.

Reviewers: #kwin, #frameworks, zzag, davidedmundson

Reviewed By: #kwin, zzag, davidedmundson

Subscribers: davidedmundson, zzag, kde-frameworks-devel

Tags: #frameworks

Differential Revision: https://phabricator.kde.org/D26858
parent ff9cadf0
......@@ -31,7 +31,9 @@ ecm_setup_version(PROJECT VARIABLE_PREFIX KWAYLAND
# Dependencies
set(REQUIRED_QT_VERSION 5.12.0)
find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED Concurrent Gui)
find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED Concurrent Gui WaylandClient)
find_package(QtWaylandScanner REQUIRED)
find_package(Wayland 1.15 COMPONENTS Client Server)
set_package_properties(Wayland PROPERTIES
......@@ -64,6 +66,8 @@ add_definitions(-DQT_DISABLE_DEPRECATED_BEFORE=0x050d00)
if (IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/po")
ecm_install_po_files_as_qm(po)
endif()
find_package(QtWaylandScanner REQUIRED)
add_subdirectory(src)
if (BUILD_TESTING)
......
......@@ -45,3 +45,15 @@ add_executable(testNoXdgRuntimeDir test_no_xdg_runtime_dir.cpp)
target_link_libraries( testNoXdgRuntimeDir Qt5::Test KF5::WaylandServer)
add_test(NAME kwayland-testNoXdgRuntimeDir COMMAND testNoXdgRuntimeDir)
ecm_mark_as_test(testNoXdgRuntimeDir)
########################################################
# Test Tablet Interface
########################################################
ecm_add_qtwayland_client_protocol(TABLET_SRCS
PROTOCOL ${WaylandProtocols_DATADIR}/unstable/tablet/tablet-unstable-v2.xml
BASENAME tablet-unstable-v2
)
add_executable(testTabletInterface test_tablet_interface.cpp ${TABLET_SRCS})
target_link_libraries( testTabletInterface Qt5::Test KF5::WaylandServer KF5::WaylandClient Wayland::Client)
add_test(NAME kwayland-testTabletInterface COMMAND testTabletInterface)
ecm_mark_as_test(testTabletInterface)
/********************************************************************
Copyright 2020 Aleix Pol Gonzalez <aleixpol@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/tablet_interface.h"
#include "../../src/client/compositor.h"
#include "../../src/client/connection_thread.h"
#include "../../src/client/event_queue.h"
#include "../../src/client/registry.h"
#include "../../src/client/seat.h"
#include "qwayland-tablet-unstable-v2.h"
using namespace KWayland::Server;
class Tablet : public QtWayland::zwp_tablet_v2
{
public:
Tablet(::zwp_tablet_v2 *t)
: QtWayland::zwp_tablet_v2(t)
{
}
};
class Tool : public QObject, public QtWayland::zwp_tablet_tool_v2
{
Q_OBJECT
public:
Tool(::zwp_tablet_tool_v2 *t)
: QtWayland::zwp_tablet_tool_v2(t)
{
}
void zwp_tablet_tool_v2_proximity_in(uint32_t /*serial*/, struct ::zwp_tablet_v2 * /*tablet*/, struct ::wl_surface *surface) override
{
surfaceApproximated[surface]++;
}
void zwp_tablet_tool_v2_frame(uint32_t time) override
{
Q_EMIT frame(time);
}
QHash<struct ::wl_surface *, int> surfaceApproximated;
Q_SIGNALS:
void frame(quint32 time);
};
class TabletSeat : public QObject, public QtWayland::zwp_tablet_seat_v2
{
Q_OBJECT
public:
TabletSeat(::zwp_tablet_seat_v2 *seat)
: QtWayland::zwp_tablet_seat_v2(seat)
{
}
void zwp_tablet_seat_v2_tablet_added(struct ::zwp_tablet_v2 *id) override
{
m_tablets << new Tablet(id);
Q_EMIT tabletAdded();
}
void zwp_tablet_seat_v2_tool_added(struct ::zwp_tablet_tool_v2 *id) override
{
m_tools << new Tool(id);
Q_EMIT toolAdded();
}
QVector<Tablet *> m_tablets;
QVector<Tool *> m_tools;
Q_SIGNALS:
void toolAdded();
void tabletAdded();
};
class TestTabletInterface : public QObject
{
Q_OBJECT
public:
TestTabletInterface()
{
}
~TestTabletInterface() override;
private Q_SLOTS:
void initTestCase();
void testAdd();
void testInteractSimple();
void testInteractSurfaceChange();
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;
TabletSeat *m_tabletSeatClient = nullptr;
TabletManagerInterface *m_tabletManager;
TabletInterface *m_tablet;
TabletToolInterface *m_tool;
QVector<SurfaceInterface *> m_surfaces;
};
static const QString s_socketName = QStringLiteral("kwin-wayland-server-tablet-test-0");
void TestTabletInterface::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_tabletManager = m_display.createTabletManagerInterface(this);
QVERIFY(m_serverCompositor->isValid());
connect(m_serverCompositor, &CompositorInterface::surfaceCreated, this, [this](SurfaceInterface *surface) {
m_surfaces += surface;
});
// 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 == "zwp_tablet_manager_v2") {
auto tabletClient = new QtWayland::zwp_tablet_manager_v2(registry->registry(), name, version);
auto _seat = tabletClient->get_tablet_seat(*m_clientSeat);
m_tabletSeatClient = new TabletSeat(_seat);
}
});
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());
QSignalSpy surfaceSpy(m_serverCompositor, &CompositorInterface::surfaceCreated);
for (int i = 0; i < 3; ++i) {
m_clientCompositor->createSurface(this);
}
QVERIFY(surfaceSpy.count() < 3 && surfaceSpy.wait(200));
QVERIFY(m_surfaces.count() == 3);
QVERIFY(m_tabletSeatClient);
}
TestTabletInterface::~TestTabletInterface()
{
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_tabletSeatClient;
m_connection->deleteLater();
m_connection = nullptr;
}
void TestTabletInterface::testAdd()
{
TabletSeatInterface *seatInterface = m_tabletManager->seat(m_seat);
QVERIFY(seatInterface);
QSignalSpy tabletSpy(m_tabletSeatClient, &TabletSeat::tabletAdded);
m_tablet = seatInterface->addTablet(1, 2, QStringLiteral("event33"), QStringLiteral("my tablet"), {QStringLiteral("/test/event33")});
QVERIFY(m_tablet);
QVERIFY(tabletSpy.wait() || tabletSpy.count() == 1);
QCOMPARE(m_tabletSeatClient->m_tablets.count(), 1);
QSignalSpy toolSpy(m_tabletSeatClient, &TabletSeat::toolAdded);
m_tool = seatInterface->addTool(KWayland::Server::TabletToolInterface::Pen, 0, 0, {TabletToolInterface::Tilt, TabletToolInterface::Pressure});
QVERIFY(m_tool);
QVERIFY(toolSpy.wait() || toolSpy.count() == 1);
QCOMPARE(m_tabletSeatClient->m_tools.count(), 1);
QVERIFY(!m_tool->isClientSupported()); //There's no surface in it yet
m_tool->setCurrentSurface(nullptr);
QVERIFY(!m_tool->isClientSupported()); //There's no surface in it
QCOMPARE(m_surfaces.count(), 3);
for (SurfaceInterface *surface : m_surfaces) {
m_tool->setCurrentSurface(surface);
}
m_tool->setCurrentSurface(nullptr);
}
static uint s_serial = 0;
void TestTabletInterface::testInteractSimple()
{
QSignalSpy frameSpy(m_tabletSeatClient->m_tools[0], &Tool::frame);
QVERIFY(!m_tool->isClientSupported());
m_tool->setCurrentSurface(m_surfaces[0]);
QVERIFY(m_tool->isClientSupported() && m_tablet->isSurfaceSupported(m_surfaces[0]));
m_tool->sendProximityIn(m_tablet);
m_tool->sendPressure(0);
m_tool->sendFrame(s_serial++);
m_tool->sendMotion({3, 3});
m_tool->sendFrame(s_serial++);
m_tool->sendProximityOut();
QVERIFY(m_tool->isClientSupported());
m_tool->sendFrame(s_serial++);
QVERIFY(!m_tool->isClientSupported());
QVERIFY(frameSpy.wait(500));
QCOMPARE(m_tabletSeatClient->m_tools[0]->surfaceApproximated.count(), 1);
}
void TestTabletInterface::testInteractSurfaceChange()
{
m_tabletSeatClient->m_tools[0]->surfaceApproximated.clear();
QSignalSpy frameSpy(m_tabletSeatClient->m_tools[0], &Tool::frame);
QVERIFY(!m_tool->isClientSupported());
m_tool->setCurrentSurface(m_surfaces[0]);
QVERIFY(m_tool->isClientSupported() && m_tablet->isSurfaceSupported(m_surfaces[0]));
m_tool->sendProximityIn(m_tablet);
m_tool->sendPressure(0);
m_tool->sendFrame(s_serial++);
m_tool->setCurrentSurface(m_surfaces[1]);
QVERIFY(m_tool->isClientSupported());
m_tool->sendMotion({3, 3});
m_tool->sendFrame(s_serial++);
m_tool->sendProximityOut();
QVERIFY(m_tool->isClientSupported());
m_tool->sendFrame(s_serial++);
QVERIFY(!m_tool->isClientSupported());
QVERIFY(frameSpy.wait(500));
QCOMPARE(m_tabletSeatClient->m_tools[0]->surfaceApproximated.count(), 2);
}
QTEST_GUILESS_MAIN(TestTabletInterface)
#include "test_tablet_interface.moc"
......@@ -50,6 +50,7 @@ set(SERVER_LIB_SRCS
subcompositor_interface.cpp
surface_interface.cpp
surfacerole.cpp
tablet_interface.cpp
textinput_interface.cpp
textinput_interface_v0.cpp
textinput_interface_v2.cpp
......@@ -228,6 +229,11 @@ ecm_add_wayland_server_protocol(SERVER_LIB_SRCS
BASENAME linux-dmabuf-unstable-v1
)
ecm_add_qtwayland_server_protocol(SERVER_LIB_SRCS
PROTOCOL ${WaylandProtocols_DATADIR}/unstable/tablet/tablet-unstable-v2.xml
BASENAME tablet-unstable-v2
)
set(SERVER_GENERATED_SRCS
${CMAKE_CURRENT_BINARY_DIR}/wayland-blur-client-protocol.h
${CMAKE_CURRENT_BINARY_DIR}/wayland-blur-server-protocol.h
......@@ -285,6 +291,8 @@ set(SERVER_GENERATED_SRCS
${CMAKE_CURRENT_BINARY_DIR}/wayland-xdg-shell-server-protocol.h
${CMAKE_CURRENT_BINARY_DIR}/wayland-xdg-shell-v6-client-protocol.h
${CMAKE_CURRENT_BINARY_DIR}/wayland-xdg-shell-v6-server-protocol.h
${CMAKE_CURRENT_BINARY_DIR}/qwayland-server-tablet-unstable-v2.h
${CMAKE_CURRENT_BINARY_DIR}/qwayland-server-tablet-unstable-v2.cpp
)
set_source_files_properties(${SERVER_GENERATED_SRCS} PROPERTIES SKIP_AUTOMOC ON)
......@@ -369,6 +377,7 @@ set(SERVER_LIB_HEADERS
slide_interface.h
subcompositor_interface.h
surface_interface.h
tablet_interface.h
textinput_interface.h
touch_interface.h
xdgdecoration_interface.h
......
......@@ -5,45 +5,46 @@
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "display.h"
#include "appmenu_interface.h"
#include "blur_interface.h"
#include "compositor_interface.h"
#include "contrast_interface.h"
#include "datadevicemanager_interface.h"
#include "dpms_interface.h"
#include "outputconfiguration_interface.h"
#include "outputmanagement_interface.h"
#include "outputdevice_interface.h"
#include "eglstream_controller_interface.h"
#include "fakeinput_interface.h"
#include "idle_interface.h"
#include "idleinhibit_interface_p.h"
#include "remote_access_interface.h"
#include "fakeinput_interface.h"
#include "keystate_interface.h"
#include "linuxdmabuf_v1_interface.h"
#include "logging.h"
#include "output_interface.h"
#include "outputconfiguration_interface.h"
#include "outputdevice_interface.h"
#include "outputmanagement_interface.h"
#include "plasmashell_interface.h"
#include "plasmavirtualdesktop_interface.h"
#include "plasmawindowmanagement_interface.h"
#include "pointerconstraints_interface_p.h"
#include "pointergestures_interface_p.h"
#include "qtsurfaceextension_interface.h"
#include "seat_interface.h"
#include "shadow_interface.h"
#include "blur_interface.h"
#include "contrast_interface.h"
#include "relativepointer_interface_p.h"
#include "remote_access_interface.h"
#include "seat_interface.h"
#include "server_decoration_interface.h"
#include "slide_interface.h"
#include "server_decoration_palette_interface.h"
#include "shadow_interface.h"
#include "shell_interface.h"
#include "slide_interface.h"
#include "subcompositor_interface.h"
#include "tablet_interface.h"
#include "textinput_interface_p.h"
#include "xdgshell_v5_interface_p.h"
#include "xdgdecoration_interface.h"
#include "xdgforeign_interface.h"
#include "xdgshell_v6_interface_p.h"
#include "xdgshell_stable_interface_p.h"
#include "appmenu_interface.h"
#include "server_decoration_palette_interface.h"
#include "plasmavirtualdesktop_interface.h"
#include "xdgoutput_interface.h"
#include "xdgdecoration_interface.h"
#include "eglstream_controller_interface.h"
#include "keystate_interface.h"
#include "linuxdmabuf_v1_interface.h"
#include "xdgshell_stable_interface_p.h"
#include "xdgshell_v5_interface_p.h"
#include "xdgshell_v6_interface_p.h"
#include <QCoreApplication>
#include <QDebug>
......@@ -523,6 +524,13 @@ KeyStateInterface *Display::createKeyStateInterface(QObject *parent)
return d;
}
TabletManagerInterface *Display::createTabletManagerInterface(QObject *parent)
{
auto d = new TabletManagerInterface(this, parent);
connect(this, &Display::aboutToTerminate, d, [d] { delete d; });
return d;
}
void Display::createShm()
{
Q_ASSERT(d->display);
......
......@@ -79,6 +79,7 @@ class XdgDecorationManagerInterface;
class EglStreamControllerInterface;
class KeyStateInterface;
class LinuxDmabufUnstableV1Interface;
class TabletManagerInterface;
/**
* @brief Class holding the Wayland server display loop.
......@@ -310,6 +311,13 @@ public:
*/
EglStreamControllerInterface *createEglStreamControllerInterface(QObject *parent = nullptr);
/**
* Creates the entry point to support wacom-like tablets and pens.
*
* @since 5.67
*/
TabletManagerInterface *createTabletManagerInterface(QObject *parent = nullptr);
/**
* Gets the ClientConnection for the given @p client.
* If there is no ClientConnection yet for the given @p client, it will be created.
......
/********************************************************************
Copyright 2019 Aleix Pol Gonzalez <aleixpol@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/>.
*********************************************************************/
#include "tablet_interface.h"
#include "display.h"
#include "resource_p.h"
#include "seat_interface.h"
#include "surface_interface.h"
#include "qwayland-server-tablet-unstable-v2.h"
#include <QHash>
using namespace KWayland;
using namespace Server;
static int s_version = 1;
class TabletInterface::Private : public QtWaylandServer::zwp_tablet_v2
{
public:
Private(TabletInterface* q, uint32_t vendorId, uint32_t productId, const QString name, const QStringList &paths)
: zwp_tablet_v2()
, q(q)
, m_vendorId(vendorId)
, m_productId(productId)
, m_name(name)
, m_paths(paths)
{
}
wl_resource *resourceForSurface(SurfaceInterface *surface) const
{
ClientConnection *client = surface->client();
QtWaylandServer::zwp_tablet_v2::Resource *r = resourceMap().value(*client);
return r ? r->handle : nullptr;
}
void zwp_tablet_v2_destroy_resource(QtWaylandServer::zwp_tablet_v2::Resource * resource) override
{
Q_UNUSED(resource);
if (removed && resourceMap().isEmpty()) {
delete q;
}
}
TabletInterface *const q;
const uint32_t m_vendorId;
const uint32_t m_productId;
const QString m_name;
const QStringList m_paths;
bool removed = false;
};
TabletInterface::TabletInterface(uint32_t vendorId, uint32_t productId,
const QString &name, const QStringList &paths,
QObject *parent)
: QObject(parent)
, d(new Private(this, vendorId, productId, name, paths))
{
}
TabletInterface::~TabletInterface() = default;
bool TabletInterface::isSurfaceSupported(SurfaceInterface *surface) const
{
return d->resourceForSurface(surface);
}
void TabletInterface::sendRemoved()
{
d->removed = true;
for (QtWaylandServer::zwp_tablet_v2::Resource *resource : d->resourceMap()) {
d->send_removed(resource->handle);
}
}
class TabletCursor::Private
{
public:
Private(TabletCursor* q) : q(q) {}
void update(quint32 serial, SurfaceInterface *surface, const QPoint &hotspot)
{
const bool diff = m_serial != serial && m_surface != surface && m_hotspot != hotspot;
m_serial = serial;
m_surface = surface;
m_hotspot = hotspot;
if (diff)
Q_EMIT q->changed();
}
TabletCursor* const q;
quint32 m_serial = 0;
SurfaceInterface* m_surface = nullptr;
QPoint m_hotspot;
};
TabletCursor::TabletCursor()
: QObject()
, d(new Private(this))
{
}
TabletCursor::~TabletCursor() = default;
QPoint TabletCursor::hotspot() const
{
return d->m_hotspot;
}
quint32 TabletCursor::enteredSerial() const
{
return d->m_serial;
}
SurfaceInterface* TabletCursor::surface() const