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

Provide an initial implementation for input-method-unstable-v1

Makes it possible to implement the protocol in your favourite
compositor.
parent edbde063
......@@ -76,7 +76,6 @@ target_link_libraries(testViewporterInterface Qt5::Test Plasma::KWaylandServer K
add_test(NAME kwayland-testViewporterInterface COMMAND testViewporterInterface)
ecm_mark_as_test(testViewporterInterface)
########################################################
# Test ScreencastInterface
########################################################
......@@ -88,3 +87,15 @@ add_executable(testScreencastInterface test_screencast.cpp ${SCREENCAST_SRCS})
target_link_libraries(testScreencastInterface Qt5::Test Plasma::KWaylandServer Wayland::Client KF5::WaylandClient)
add_test(NAME kwayland-testScreencastInterface COMMAND testScreencastInterface)
ecm_mark_as_test(testScreencastInterface)
########################################################
# Test InputMethod Interface
########################################################
ecm_add_qtwayland_client_protocol(INPUTMETHOD_SRCS
PROTOCOL ${WaylandProtocols_DATADIR}/unstable/input-method/input-method-unstable-v1.xml
BASENAME input-method-unstable-v1
)
add_executable(testInputMethodInterface test_inputmethod_interface.cpp ${INPUTMETHOD_SRCS})
target_link_libraries(testInputMethodInterface Qt5::Test Plasma::KWaylandServer KF5::WaylandClient Wayland::Client)
add_test(NAME kwayland-testInputMethodInterface COMMAND testInputMethodInterface)
ecm_mark_as_test(testInputMethodInterface)
/********************************************************************
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/inputmethod_v1_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 "KWayland/Client/surface.h"
#include "KWayland/Client/output.h"
#include "qwayland-input-method-unstable-v1.h"
using namespace KWaylandServer;
class InputPanelSurface : public QObject, public QtWayland::zwp_input_panel_surface_v1
{
Q_OBJECT
public:
InputPanelSurface(::zwp_input_panel_surface_v1 *t)
: QtWayland::zwp_input_panel_surface_v1(t)
{
}
};
class InputPanel : public QtWayland::zwp_input_panel_v1
{
public:
InputPanel(struct ::wl_registry *registry, int id, int version)
: QtWayland::zwp_input_panel_v1(registry, id, version)
{
}
InputPanelSurface* panelForSurface(KWayland::Client::Surface* surface) {
auto panelSurface = new InputPanelSurface(get_input_panel_surface(*surface));
QObject::connect(surface, &QObject::destroyed, panelSurface, &QObject::deleteLater);
return panelSurface;
}
};
class TestInputMethodInterface : public QObject
{
Q_OBJECT
public:
TestInputMethodInterface()
{
}
~TestInputMethodInterface() override;
private Q_SLOTS:
void initTestCase();
void testAdd();
private:
KWayland::Client::ConnectionThread *m_connection;
KWayland::Client::EventQueue *m_queue;
KWayland::Client::Compositor *m_clientCompositor;
KWayland::Client::Seat *m_clientSeat = nullptr;
KWayland::Client::Output *m_output= nullptr;
InputPanel* m_inputPanel;
QThread *m_thread;
Display m_display;
SeatInterface *m_seat;
CompositorInterface *m_serverCompositor;
KWaylandServer::InputMethodV1Interface* m_inputMethodIface;
KWaylandServer::InputPanelV1Interface* m_inputPanelIface;
QVector<SurfaceInterface *> m_surfaces;
};
static const QString s_socketName = QStringLiteral("kwin-wayland-server-inputmethod-test-0");
void TestInputMethodInterface::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_inputMethodIface = m_display.createInputMethodInterface(this);
m_inputPanelIface = m_display.createInputPanelInterface(this);
auto outputIface = m_display.createOutput(this);
outputIface->create();
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);
QSignalSpy interfacesSpy(registry, &KWayland::Client::Registry::interfacesAnnounced);
connect(registry, &KWayland::Client::Registry::outputAnnounced, this, [this, registry] (quint32 name, quint32 version) {
m_output = new KWayland::Client::Output(this);
m_output->setup(registry->bindOutput(name, version));
});
connect(registry, &KWayland::Client::Registry::interfaceAnnounced, this, [this, registry](const QByteArray &interface, quint32 name, quint32 version) {
if (interface == "zwp_input_panel_v1") {
m_inputPanel = new InputPanel(registry->registry(), name, version);
}
});
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(interfacesSpy.count() || interfacesSpy.wait());
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_inputPanel);
QVERIFY(m_output);
}
TestInputMethodInterface::~TestInputMethodInterface()
{
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_inputPanel;
delete m_inputMethodIface;
delete m_inputPanelIface;
m_connection->deleteLater();
m_connection = nullptr;
}
void TestInputMethodInterface::testAdd()
{
QSignalSpy panelSpy(m_inputPanelIface, &InputPanelV1Interface::inputPanelSurfaceAdded);
QPointer<InputPanelSurfaceV1Interface> panelSurfaceIface;
connect(m_inputPanelIface, &InputPanelV1Interface::inputPanelSurfaceAdded, this, [&panelSurfaceIface] (InputPanelSurfaceV1Interface *surface) {
panelSurfaceIface = surface;
});
auto surface = m_clientCompositor->createSurface(this);
auto panelSurface = m_inputPanel->panelForSurface(surface);
QVERIFY(panelSpy.wait() || panelSurfaceIface);
Q_ASSERT(panelSurfaceIface);
Q_ASSERT(panelSurfaceIface->surface() == m_surfaces.constLast());
QSignalSpy panelTopLevelSpy(panelSurfaceIface, &InputPanelSurfaceV1Interface::topLevel);
panelSurface->set_toplevel(*m_output, InputPanelSurface::position_center_bottom);
QVERIFY(panelTopLevelSpy.wait());
QSignalSpy panelDeleteSpy(panelSurfaceIface, &QObject::destroyed);
delete surface;
QVERIFY(panelDeleteSpy.wait());
}
QTEST_GUILESS_MAIN(TestInputMethodInterface)
#include "test_inputmethod_interface.moc"
......@@ -22,6 +22,7 @@ set(SERVER_LIB_SRCS
global.cpp
idle_interface.cpp
idleinhibit_v1_interface
inputmethod_v1_interface.cpp
keyboard_interface.cpp
keyboard_shortcuts_inhibit_v1_interface.cpp
keystate_interface.cpp
......@@ -256,6 +257,11 @@ ecm_add_qtwayland_server_protocol(SERVER_LIB_SRCS
BASENAME zkde-screencast-unstable-v1
)
ecm_add_qtwayland_server_protocol(SERVER_LIB_SRCS
PROTOCOL ${WaylandProtocols_DATADIR}/unstable/input-method/input-method-unstable-v1.xml
BASENAME input-method-unstable-v1
)
set(SERVER_GENERATED_SRCS
${CMAKE_CURRENT_BINARY_DIR}/wayland-blur-client-protocol.h
${CMAKE_CURRENT_BINARY_DIR}/wayland-blur-server-protocol.h
......@@ -377,6 +383,7 @@ set(SERVER_LIB_HEADERS
global.h
idle_interface.h
idleinhibit_v1_interface.h
inputmethod_v1_interface.h
keyboard_interface.h
keyboard_shortcuts_inhibit_v1_interface.h
keystate_interface.h
......
......@@ -45,6 +45,7 @@
#include "xdgforeign_v2_interface.h"
#include "xdgoutput_interface.h"
#include "xdgshell_interface.h"
#include "inputmethod_v1_interface.h"
#include <QCoreApplication>
#include <QDebug>
......@@ -488,6 +489,14 @@ DataControlDeviceManagerV1Interface *Display::createDataControlDeviceManagerV1(Q
KeyboardShortcutsInhibitManagerV1Interface *Display::createKeyboardShortcutsInhibitManagerV1(QObject *parent)
{
auto d = new KeyboardShortcutsInhibitManagerV1Interface(this, parent);
connect(this, &Display::aboutToTerminate, d, [d] { delete d; });
return d;
}
InputMethodV1Interface *Display::createInputMethodInterface(QObject *parent)
{
auto d = new InputMethodV1Interface(this, parent);
connect(this, &Display::aboutToTerminate, d, [d] { delete d; });
return d;
}
......@@ -506,6 +515,13 @@ PrimarySelectionDeviceManagerV1Interface *Display::createPrimarySelectionDeviceM
return primarySelection;
}
InputPanelV1Interface *Display::createInputPanelInterface(QObject *parent)
{
auto p = new InputPanelV1Interface(this, parent);
connect(this, &Display::aboutToTerminate, p, [p] { delete p; });
return p;
}
void Display::createShm()
{
Q_ASSERT(d->display);
......
......@@ -80,6 +80,8 @@ class PrimarySelectionDeviceManagerV1Interface;
class KeyboardShortcutsInhibitManagerV1Interface;
class ViewporterInterface;
class ScreencastInterface;
class InputMethodV1Interface;
class InputPanelV1Interface;
/**
* @brief Class holding the Wayland server display loop.
......@@ -303,6 +305,9 @@ public:
*/
EglStreamControllerInterface *createEglStreamControllerInterface(QObject *parent = nullptr);
InputMethodV1Interface *createInputMethodInterface(QObject *parent = nullptr);
InputPanelV1Interface *createInputPanelInterface(QObject *parent = nullptr);
/**
* Creates the DataControlDeviceManagerV1
*
......
/*
SPDX-FileCopyrightText: 2020 Aleix Pol Gonzalez <aleixpol@kde.org>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "inputmethod_v1_interface.h"
#include "seat_interface.h"
#include "display.h"
#include "surface_interface.h"
#include "output_interface.h"
#include "surfacerole_p.h"
#include <QHash>
#include "qwayland-server-input-method-unstable-v1.h"
#include "wayland-text-server-protocol.h"
namespace KWaylandServer
{
static int s_version = 1;
class InputMethodContextV1InterfacePrivate : public QtWaylandServer::zwp_input_method_context_v1
{
public:
InputMethodContextV1InterfacePrivate(InputMethodContextV1Interface *q)
: zwp_input_method_context_v1()
, q(q)
{
}
void zwp_input_method_context_v1_commit_string(Resource *, uint32_t serial, const QString &text) override
{
Q_EMIT q->commitString(serial, text);
}
void zwp_input_method_context_v1_preedit_string(Resource *, uint32_t serial, const QString &text, const QString &commit) override
{
Q_EMIT q->preeditString(serial, text, commit);
}
void zwp_input_method_context_v1_preedit_styling(Resource *, uint32_t index, uint32_t length, uint32_t style) override
{
Q_EMIT q->preeditStyling(index, length, style);
}
void zwp_input_method_context_v1_preedit_cursor(Resource *, int32_t index) override
{
Q_EMIT q->preeditCursor(index);
}
void zwp_input_method_context_v1_delete_surrounding_text(Resource *, int32_t index, uint32_t length) override
{
Q_EMIT q->deleteSurroundingText(index, length);
}
void zwp_input_method_context_v1_cursor_position(Resource *, int32_t index, int32_t anchor) override
{
Q_EMIT q->cursorPosition(index, anchor);
}
void zwp_input_method_context_v1_modifiers_map(Resource *, wl_array *map) override
{
const QList<QByteArray> modifiersMap = QByteArray::fromRawData(static_cast<const char *>(map->data), map->size).split('\0');
mods.clear();
for (const QByteArray &modifier : modifiersMap) {
if (modifier == "Shift") {
mods << Qt::ShiftModifier;
} else if (modifier == "Alt") {
mods << Qt::AltModifier;
} else if (modifier == "Control") {
mods << Qt::ControlModifier;
} else if (modifier == "Mod1") {
mods << Qt::AltModifier;
} else if (modifier == "Mod4") {
mods << Qt::MetaModifier;
} else {
mods << Qt::NoModifier;
}
}
}
void zwp_input_method_context_v1_keysym(Resource *, uint32_t serial, uint32_t time, uint32_t sym, uint32_t state, uint32_t modifiers) override
{
Q_EMIT q->keysym(serial, time, sym, state == WL_KEYBOARD_KEY_STATE_PRESSED, toQtModifiers(modifiers));
}
void zwp_input_method_context_v1_grab_keyboard(Resource *, uint32_t keyboard) override
{
Q_EMIT q->grabKeyboard(keyboard);
}
void zwp_input_method_context_v1_key(Resource *, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) override
{
Q_EMIT q->key(serial, time, key, state == WL_KEYBOARD_KEY_STATE_PRESSED);
}
void zwp_input_method_context_v1_modifiers(Resource *, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) override
{
Q_EMIT q->modifiers(serial, toQtModifiers(mods_depressed), toQtModifiers(mods_latched), toQtModifiers(mods_locked), group);
}
void zwp_input_method_context_v1_language(Resource *, uint32_t serial, const QString &language) override
{
Q_EMIT q->language(serial, language);
}
void zwp_input_method_context_v1_text_direction(Resource *, uint32_t serial, uint32_t direction) override
{
Qt::LayoutDirection qtDirection;
switch (direction) {
case WL_TEXT_INPUT_TEXT_DIRECTION_LTR:
qtDirection = Qt::LeftToRight;
break;
case WL_TEXT_INPUT_TEXT_DIRECTION_RTL:
qtDirection = Qt::RightToLeft;
break;
case WL_TEXT_INPUT_TEXT_DIRECTION_AUTO:
qtDirection = Qt::LayoutDirectionAuto;
break;
}
Q_EMIT q->textDirection(serial, qtDirection);
}
Qt::KeyboardModifiers toQtModifiers(uint32_t modifiers)
{
Qt::KeyboardModifiers ret = Qt::NoModifier;
for (int i = 0; modifiers >>= 1; ++i) {
ret |= mods[i];
}
return ret;
}
void zwp_input_method_context_v1_destroy_resource(Resource *resource) override
{
Q_UNUSED(resource)
if (resourceMap().isEmpty()) {
delete q;
}
}
void zwp_input_method_context_v1_destroy(Resource *resource) override
{
wl_resource_destroy(resource->handle);
}
private:
InputMethodContextV1Interface *const q;
QVector<Qt::KeyboardModifiers> mods;
};
InputMethodContextV1Interface::InputMethodContextV1Interface(InputMethodV1Interface *parent)
: QObject(parent)
, d(new InputMethodContextV1InterfacePrivate(this))
{
}
InputMethodContextV1Interface::~InputMethodContextV1Interface() = default;
void InputMethodContextV1Interface::sendCommitState(uint32_t serial)
{
for (auto r : d->resourceMap()) {
d->send_commit_state(r->handle, serial);
}
}
void InputMethodContextV1Interface::sendContentType(uint32_t hint, uint32_t purpose)
{
for (auto r : d->resourceMap()) {
d->send_content_type(r->handle, hint, purpose);
}
}
void InputMethodContextV1Interface::sendInvokeAction(uint32_t button, uint32_t index)
{
for (auto r : d->resourceMap()) {
d->send_invoke_action(r->handle, button, index);
}
}
void InputMethodContextV1Interface::sendPreferredLanguage(const QString &language)
{
for (auto r : d->resourceMap()) {
d->send_preferred_language(r->handle, language);
}
}
void InputMethodContextV1Interface::sendReset()
{
for (auto r : d->resourceMap()) {
d->send_reset(r->handle);
}
}
void InputMethodContextV1Interface::sendSurroundingText(const QString &text, uint32_t cursor, uint32_t anchor)
{
for (auto r : d->resourceMap()) {
d->send_surrounding_text(r->handle, text, cursor, anchor);
}
}
class InputPanelSurfaceV1InterfacePrivate : public QtWaylandServer::zwp_input_panel_surface_v1, public SurfaceRole
{
friend class InputPanelSurfaceV1Interface;
public:
InputPanelSurfaceV1InterfacePrivate(SurfaceInterface *surface, quint32 id, InputPanelSurfaceV1Interface *q)
: zwp_input_panel_surface_v1()
, SurfaceRole(surface)
, q(q)
, m_id(id)
{
}
void zwp_input_panel_surface_v1_set_overlay_panel(Resource *) override
{
Q_EMIT q->overlayPanel();
}
void zwp_input_panel_surface_v1_set_toplevel(Resource *, struct ::wl_resource *output, uint32_t position) override
{
Q_EMIT q->topLevel(OutputInterface::get(output), InputPanelSurfaceV1Interface::Position(position));
}
void commit() override {}
void zwp_input_panel_surface_v1_destroy_resource(Resource *) override {
delete q;
}
InputPanelSurfaceV1Interface *const q;
const quint32 m_id;
};
InputPanelSurfaceV1Interface::InputPanelSurfaceV1Interface(SurfaceInterface *surface, quint32 id, QObject *parent)
: QObject(parent)
, d(new InputPanelSurfaceV1InterfacePrivate(surface, id, this))
{
}
InputPanelSurfaceV1Interface::~InputPanelSurfaceV1Interface()
{
}
class InputPanelV1InterfacePrivate : public QtWaylandServer::zwp_input_panel_v1
{
public:
InputPanelV1InterfacePrivate(InputPanelV1Interface *q, Display *d)
: zwp_input_panel_v1(*d, s_version)
, q(q)
{
}
void zwp_input_panel_v1_get_input_panel_surface(Resource *resource, uint32_t id, struct ::wl_resource *surfaceResource) override
{
auto surface = SurfaceInterface::get(surfaceResource);
auto interface = new InputPanelSurfaceV1Interface(surface, id, nullptr);
interface->d->init(resource->client(), id, resource->version());
Q_EMIT q->inputPanelSurfaceAdded(interface);
}
InputPanelV1Interface *const q;
};
InputPanelV1Interface::InputPanelV1Interface(Display *display, QObject *parent)
: QObject(parent)
, d(new InputPanelV1InterfacePrivate(this, display))
{
}
InputPanelV1Interface::~InputPanelV1Interface() = default;
quint32 InputPanelSurfaceV1Interface::id() const
{
return d->m_id;
}
SurfaceInterface *InputPanelSurfaceV1Interface::surface() const
{
return d->surface();
}
class InputMethodV1InterfacePrivate : public QtWaylandServer::zwp_input_method_v1
{
public:
InputMethodV1InterfacePrivate(Display *d, InputMethodV1Interf