Implement Wayland support using PipeWire and xdg-desktop-portal

Summary:
Adds a new framebuffer implementation, which uses xdg-desktop-portal to support remote
desktop on Wayland and uses PipeWire to deliver the screen content. So far only mouse
support is implemented, because keyboard support is missing on KWin side.

Reviewers: Kanedias, romangg

Reviewed By: Kanedias

Subscribers: asturmlechner, pino, ngraham, romangg

Differential Revision: https://phabricator.kde.org/D20402
This commit is contained in:
Jan Grulich
2019-05-20 08:10:30 +02:00
parent 7624aee8e2
commit c05707884c
44 changed files with 2751 additions and 183 deletions

6
events/CMakeLists.txt Normal file
View File

@@ -0,0 +1,6 @@
add_subdirectory(x11)
# Makes sense to use only when PW framebuffer is used
if (${PipeWire_FOUND})
add_subdirectory(xdp)
endif()

18
events/x11/CMakeLists.txt Normal file
View File

@@ -0,0 +1,18 @@
include_directories (${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
)
set (krfb_events_x11_SRCS
x11events.cpp
x11eventsplugin.cpp
)
add_library (krfb_events_x11 MODULE ${krfb_events_x11_SRCS})
target_link_libraries (krfb_events_x11
${X11_XTest_LIB}
KF5::CoreAddons
krfbprivate
)
install (TARGETS krfb_events_x11 DESTINATION ${PLUGIN_INSTALL_DIR}/krfb)

View File

@@ -0,0 +1,13 @@
[Desktop Entry]
Encoding=UTF-8
Comment=X11 XFakeInput based event handler for KRfb
Name=X11 XFakeInput based event handler for KRfb
Type=Service
ServiceTypes=krfb/events
X-KDE-Library=krfb_events_x11
X-KDE-PluginInfo-Name=x11
X-KDE-PluginInfo-Version=0.1
X-KDE-PluginInfo-Website=http://www.kde.org
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true

View File

@@ -0,0 +1,15 @@
{
"Encoding": "UTF-8",
"KPlugin": {
"Description": "X11 XFakeInput based event handler for KRfb",
"EnabledByDefault": true,
"Id": "x11",
"License": "GPL",
"Name": "X11 Event handler for KRfb",
"ServiceTypes": [
"krfb/events"
],
"Version": "0.1",
"Website": "http://www.kde.org"
}
}

212
events/x11/x11events.cpp Normal file
View File

@@ -0,0 +1,212 @@
/*
This file is part of the KDE project
Copyright (C) 2016 by Oleg Chernovskiy <kanedias@xaker.ru>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "x11events.h"
#include <QApplication>
#include <QX11Info>
#include <QDesktopWidget>
#include <QGlobalStatic>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <QX11Info>
enum {
LEFTSHIFT = 1,
RIGHTSHIFT = 2,
ALTGR = 4
};
class EventData
{
public:
EventData();
//keyboard
Display *dpy;
signed char modifiers[0x100];
KeyCode keycodes[0x100];
KeyCode leftShiftCode;
KeyCode rightShiftCode;
KeyCode altGrCode;
char modifierState;
//mouse
int buttonMask;
int x;
int y;
private:
void init();
};
Q_GLOBAL_STATIC(EventData, data)
EventData::EventData()
{
init();
}
void EventData::init()
{
buttonMask = 0;
dpy = QX11Info::display();
//initialize keycodes
KeySym key, *keymap;
int i, j, minkey, maxkey, syms_per_keycode;
memset(modifiers, -1, sizeof(modifiers));
XDisplayKeycodes(dpy, &minkey, &maxkey);
Q_ASSERT(minkey >= 8);
Q_ASSERT(maxkey < 256);
keymap = (KeySym *) XGetKeyboardMapping(dpy, minkey,
(maxkey - minkey + 1),
&syms_per_keycode);
Q_ASSERT(keymap);
for (i = minkey; i <= maxkey; i++) {
for (j = 0; j < syms_per_keycode; j++) {
key = keymap[(i-minkey)*syms_per_keycode+j];
if (key >= ' ' && key < 0x100 && i == XKeysymToKeycode(dpy, key)) {
keycodes[key] = i;
modifiers[key] = j;
}
}
}
leftShiftCode = XKeysymToKeycode(dpy, XK_Shift_L);
rightShiftCode = XKeysymToKeycode(dpy, XK_Shift_R);
altGrCode = XKeysymToKeycode(dpy, XK_Mode_switch);
XFree((char *)keymap);
}
/* this function adjusts the modifiers according to mod (as from modifiers) and data->modifierState */
static void tweakModifiers(signed char mod, bool down)
{
bool isShift = data->modifierState & (LEFTSHIFT | RIGHTSHIFT);
if (mod < 0) {
return;
}
if (isShift && mod != 1) {
if (data->modifierState & LEFTSHIFT) {
XTestFakeKeyEvent(data->dpy, data->leftShiftCode,
down, CurrentTime);
}
if (data->modifierState & RIGHTSHIFT) {
XTestFakeKeyEvent(data->dpy, data->rightShiftCode,
down, CurrentTime);
}
}
if (!isShift && mod == 1) {
XTestFakeKeyEvent(data->dpy, data->leftShiftCode,
down, CurrentTime);
}
if ((data->modifierState & ALTGR) && mod != 2) {
XTestFakeKeyEvent(data->dpy, data->altGrCode,
!down, CurrentTime);
}
if (!(data->modifierState & ALTGR) && mod == 2) {
XTestFakeKeyEvent(data->dpy, data->altGrCode,
down, CurrentTime);
}
}
void X11EventHandler::handleKeyboard(bool down, rfbKeySym keySym)
{
#define ADJUSTMOD(sym,state) \
if(keySym==sym) { if(down) data->modifierState|=state; else data->modifierState&=~state; }
if (QX11Info::isPlatformX11()) {
ADJUSTMOD(XK_Shift_L, LEFTSHIFT);
ADJUSTMOD(XK_Shift_R, RIGHTSHIFT);
ADJUSTMOD(XK_Mode_switch, ALTGR);
if (keySym >= ' ' && keySym < 0x100) {
KeyCode k;
if (down) {
tweakModifiers(data->modifiers[keySym], True);
}
k = data->keycodes[keySym];
if (k != NoSymbol) {
XTestFakeKeyEvent(data->dpy, k, down, CurrentTime);
}
if (down) {
tweakModifiers(data->modifiers[keySym], False);
}
} else {
KeyCode k = XKeysymToKeycode(data->dpy, keySym);
if (k != NoSymbol) {
XTestFakeKeyEvent(data->dpy, k, down, CurrentTime);
}
}
}
/*
// Wayland platform and pipweire plugin in use
if (KrfbConfig::preferredFrameBufferPlugin() == QStringLiteral("pw")) {
}*/
}
void X11EventHandler::handlePointer(int buttonMask, int x, int y)
{
if (QX11Info::isPlatformX11()) {
QDesktopWidget *desktopWidget = QApplication::desktop();
int screen = desktopWidget->screenNumber();
if (screen < 0) {
screen = 0;
}
XTestFakeMotionEvent(data->dpy, screen, x, y, CurrentTime);
for (int i = 0; i < 5; i++) {
if ((data->buttonMask&(1 << i)) != (buttonMask&(1 << i))) {
XTestFakeButtonEvent(data->dpy,
i + 1,
(buttonMask&(1 << i)) ? True : False,
CurrentTime);
}
}
data->buttonMask = buttonMask;
}
}
#include "x11events.moc"

41
events/x11/x11events.h Normal file
View File

@@ -0,0 +1,41 @@
/*
This file is part of the KDE project
Copyright (C) 2016 by Oleg Chernovskiy <kanedias@xaker.ru>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef EVENTS_X11EVENTS_H
#define EVENTS_X11EVENTS_H
#include "../../krfb/events.h"
class X11EventHandler : public EventHandler
{
Q_OBJECT
public:
explicit X11EventHandler(QObject *parent = nullptr)
: EventHandler(parent)
{
};
void handleKeyboard(bool down, rfbKeySym key) override;
void handlePointer(int buttonMask, int x, int y) override;
};
#endif

View File

@@ -0,0 +1,45 @@
/* This file is part of the KDE project
Copyright (C) 2016 Oleg Chernovskiy <kanedias@xaker.ru>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "x11eventsplugin.h"
#include "x11events.h"
#include <KPluginFactory>
#include <QX11Info>
K_PLUGIN_FACTORY_WITH_JSON(X11EventsPluginFactory, "krfb_events_x11.json",
registerPlugin<X11EventsPlugin>();)
X11EventsPlugin::X11EventsPlugin(QObject *parent, const QVariantList &args)
: EventsPlugin(parent, args)
{
}
EventHandler *X11EventsPlugin::eventHandler()
{
// works only under X11
if(!QX11Info::isPlatformX11())
return nullptr;
return new X11EventHandler();
}
#include "x11eventsplugin.moc"

View File

@@ -0,0 +1,43 @@
/* This file is part of the KDE project
Copyright (C) 2016 Oleg Chernovskiy <kanedias@xaker.ru>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef KRFB_EVENTS_X11_X11EVENTSPLUGIN_H
#define KRFB_EVENTS_X11_X11EVENTSPLUGIN_H
#include "eventsplugin.h"
#include <QWidget>
class EventHandler;
class X11EventsPlugin : public EventsPlugin
{
Q_OBJECT
public:
X11EventsPlugin(QObject *parent, const QVariantList &args);
virtual ~X11EventsPlugin() = default;
EventHandler *eventHandler() override;
private:
Q_DISABLE_COPY(X11EventsPlugin)
};
#endif // Header guard

27
events/xdp/CMakeLists.txt Normal file
View File

@@ -0,0 +1,27 @@
include_directories (${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
)
set (krfb_events_xdp_SRCS
xdpevents.cpp
xdpeventsplugin.cpp
)
qt5_add_dbus_interface(
krfb_events_xdp_SRCS
${CMAKE_SOURCE_DIR}/dbus/xdp_dbus_remotedesktop_interface.xml
xdp_dbus_remotedesktop_interface
)
add_library (krfb_events_xdp MODULE ${krfb_events_xdp_SRCS})
target_link_libraries (krfb_events_xdp
KF5::CoreAddons
KF5::I18n
Qt5::DBus
krfbprivate
)
install (TARGETS krfb_events_xdp
DESTINATION ${PLUGIN_INSTALL_DIR}/krfb
)

View File

@@ -0,0 +1,14 @@
[Desktop Entry]
Encoding=UTF-8
Comment=Xdg-desktop-portal based event handler for KRfb
Name=Xdg-desktop-portal based event handler for KRfb
Type=Service
ServiceTypes=krfb/events
X-KDE-Library=krfb_events_xdp
X-KDE-PluginInfo-Name=xdp
X-KDE-PluginInfo-Version=0.1
X-KDE-PluginInfo-Website=http://www.kde.org
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true

View File

@@ -0,0 +1,16 @@
{
"Encoding": "UTF-8",
"KPlugin": {
"Description": "Xdg-desktop-portal based event handler for KRfb",
"EnabledByDefault": true,
"Id": "xdp",
"License": "GPL",
"Name": "Xdg-desktop-portal Event handler for KRfb",
"ServiceTypes": [
"krfb/events"
],
"Version": "0.1",
"Website": "http://www.kde.org"
}
}

128
events/xdp/xdpevents.cpp Normal file
View File

@@ -0,0 +1,128 @@
/*
This file is part of the KDE project
Copyright (C) 2018-2019 Jan Grulich <jgrulich@redhat.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "xdpevents.h"
#include "rfbservermanager.h"
#include "xdp_dbus_remotedesktop_interface.h"
#include <linux/input.h>
#include <QApplication>
#include <QDesktopWidget>
#include <QGlobalStatic>
class EventData
{
public:
EventData();
//mouse
int buttonMask;
int x;
int y;
QScopedPointer<OrgFreedesktopPortalRemoteDesktopInterface> dbusXdpRemoteDesktopService;
private:
void init();
};
Q_GLOBAL_STATIC(EventData, data)
EventData::EventData()
{
init();
}
void EventData::init()
{
buttonMask = 0;
dbusXdpRemoteDesktopService.reset(new OrgFreedesktopPortalRemoteDesktopInterface(QLatin1String("org.freedesktop.portal.Desktop"),
QLatin1String("/org/freedesktop/portal/desktop"), QDBusConnection::sessionBus()));
}
void XdpEventHandler::handleKeyboard(bool down, rfbKeySym keySym)
{
// TODO: implement button handling
// both in FakeInput interface and here
Q_UNUSED(down)
Q_UNUSED(keySym)
}
void XdpEventHandler::handlePointer(int buttonMask, int x, int y)
{
const uint streamNodeId = frameBuffer()->customProperty(QLatin1String("stream_node_id")).toUInt();
const QDBusObjectPath sessionHandle = frameBuffer()->customProperty(QLatin1String("session_handle")).value<QDBusObjectPath>();
if (streamNodeId == 0 || sessionHandle.path().isEmpty()) {
return;
}
if (x != data->x || y != data->y) {
data->dbusXdpRemoteDesktopService->NotifyPointerMotionAbsolute(sessionHandle, QVariantMap(), streamNodeId, x, y);
data->x = x;
data->y = y;
}
if (buttonMask != data->buttonMask) {
int i = 0;
QVector<int> buttons = { BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, 0, 0, 0, 0, BTN_SIDE, BTN_EXTRA };
for (auto it = buttons.constBegin(); it != buttons.constEnd(); ++it) {
int prevButtonState = (data->buttonMask >> i) & 0x01;
int currentButtonState = (buttonMask >> i) & 0x01;
if (prevButtonState != currentButtonState) {
if (*it) {
data->dbusXdpRemoteDesktopService->NotifyPointerButton(sessionHandle, QVariantMap(), *it, buttonMask);
} else {
int axis = 0;
int steps = 0;
switch (i) {
case 3:
axis = 0; // Vertical
steps = -1;
break;
case 4:
axis = 0; // Vertical
steps = 1;
break;
case 5:
axis = 1; // Horizontal
steps = -1;
break;
case 6:
axis = 1; // Horizontal
steps = 1;
break;
}
data->dbusXdpRemoteDesktopService->NotifyPointerAxisDiscrete(sessionHandle, QVariantMap(), axis, steps);
}
}
++i;
}
data->buttonMask = buttonMask;
}
}
#include "xdpevents.moc"

37
events/xdp/xdpevents.h Normal file
View File

@@ -0,0 +1,37 @@
/*
This file is part of the KDE project
Copyright (C) 2018-2019 Jan Grulich <jgrulich@redhat.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef EVENTS_XDPEVENTS_H
#define EVENTS_XDPEVENTS_H
#include "../../krfb/events.h"
class XdpEventHandler : public EventHandler
{
Q_OBJECT
public:
void handleKeyboard(bool down, rfbKeySym key) override;
void handlePointer(int buttonMask, int x, int y) override;
};
#endif

View File

@@ -0,0 +1,43 @@
/*
This file is part of the KDE project
Copyright (C) 2018-2019 Jan Grulich <jgrulich@redhat.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "xdpeventsplugin.h"
#include "xdpevents.h"
#include <KPluginFactory>
K_PLUGIN_FACTORY_WITH_JSON(XdpEventsPluginFactory, "krfb_events_xdp.json",
registerPlugin<XdpEventsPlugin>();)
XdpEventsPlugin::XdpEventsPlugin(QObject *parent, const QVariantList &args)
: EventsPlugin(parent, args)
{
}
EventHandler *XdpEventsPlugin::eventHandler()
{
// works only under Wayland
return new XdpEventHandler();
}
#include "xdpeventsplugin.moc"

View File

@@ -0,0 +1,46 @@
/*
This file is part of the KDE project
Copyright (C) 2018-2019 Jan Grulich <jgrulich@redhat.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef KRFB_EVENTS_XDP_XDPEVENTSPLUGIN_H
#define KRFB_EVENTS_XDP_XDPEVENTSPLUGIN_H
#include "eventsplugin.h"
#include <QWidget>
class EventHandler;
class XdpEventsPlugin : public EventsPlugin
{
Q_OBJECT
public:
XdpEventsPlugin(QObject *parent, const QVariantList &args);
virtual ~XdpEventsPlugin() = default;
EventHandler *eventHandler() override;
private:
Q_DISABLE_COPY(XdpEventsPlugin)
};
#endif // Header guard