2019-02-19 08:50:20 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
|
|
|
|
Copyright 2019 Roman Gilg <subdiff@gmail.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. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************/
|
|
|
|
#include "xwayland.h"
|
2018-08-21 20:06:42 +00:00
|
|
|
#include "databridge.h"
|
|
|
|
|
2019-02-19 08:50:20 +00:00
|
|
|
#include "main_wayland.h"
|
|
|
|
#include "utils.h"
|
2019-07-02 19:56:03 +00:00
|
|
|
#include "wayland_server.h"
|
|
|
|
#include "xcbutils.h"
|
2019-02-19 08:50:20 +00:00
|
|
|
|
|
|
|
#include <KLocalizedString>
|
2019-06-22 10:40:12 +00:00
|
|
|
#include <KSelectionOwner>
|
2019-02-19 08:50:20 +00:00
|
|
|
|
|
|
|
#include <QAbstractEventDispatcher>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFutureWatcher>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QSocketNotifier>
|
|
|
|
#include <QThread>
|
2019-07-02 19:56:03 +00:00
|
|
|
#include <QtConcurrentRun>
|
2019-02-19 08:50:20 +00:00
|
|
|
|
|
|
|
// system
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_PROCCTL_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
static void readDisplay(int pipe)
|
|
|
|
{
|
|
|
|
QFile readPipe;
|
|
|
|
if (!readPipe.open(pipe, QIODevice::ReadOnly)) {
|
|
|
|
std::cerr << "FATAL ERROR failed to open pipe to start X Server" << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
QByteArray displayNumber = readPipe.readLine();
|
|
|
|
|
|
|
|
displayNumber.prepend(QByteArray(":"));
|
|
|
|
displayNumber.remove(displayNumber.size() -1, 1);
|
|
|
|
std::cout << "X-Server started on display " << displayNumber.constData() << std::endl;
|
|
|
|
|
|
|
|
setenv("DISPLAY", displayNumber.constData(), true);
|
|
|
|
|
|
|
|
// close our pipe
|
|
|
|
close(pipe);
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
2019-02-19 08:50:20 +00:00
|
|
|
namespace Xwl
|
|
|
|
{
|
|
|
|
|
2018-08-21 20:06:42 +00:00
|
|
|
Xwayland *s_self = nullptr;
|
2019-07-02 19:56:03 +00:00
|
|
|
|
|
|
|
Xwayland *Xwayland::self()
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
|
|
|
return s_self;
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:20:16 +00:00
|
|
|
Xwayland::Xwayland(ApplicationWaylandAbstract *app, QObject *parent)
|
2019-07-02 19:56:03 +00:00
|
|
|
: XwaylandInterface(parent)
|
|
|
|
, m_app(app)
|
2019-02-19 08:50:20 +00:00
|
|
|
{
|
2018-08-21 20:06:42 +00:00
|
|
|
s_self = this;
|
2019-02-19 08:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Xwayland::~Xwayland()
|
|
|
|
{
|
wayland: Terminate client connections before Workspace is destroyed
Summary:
When ShellClient tears down, it needs to access RuleBook in order to
discard temporary rules. The problem is that WaylandServer outlives
Workspace and therefore so does ShellClient.
We can't guard against the case when RuleBook::self() is nullptr as it
is vital to discard temporary rules.
This change adjusts termination sequence so all shell clients are
destroyed before Workspace(and thus RuleBook) is gone.
ASAN output:
==19922==ERROR: AddressSanitizer: heap-use-after-free on address 0x606000142060 at pc 0x7fbc0fb878bb bp 0x7ffd7d464520 sp 0x7ffd7d464518
READ of size 8 at 0x606000142060 thread T0
#0 0x7fbc0fb878ba in QList<KWin::Rules*>::detach() /usr/include/qt5/QtCore/qlist.h:172
#1 0x7fbc0fb8538d in QList<KWin::Rules*>::begin() /usr/include/qt5/QtCore/qlist.h:324
#2 0x7fbc0fb808b6 in KWin::RuleBook::discardUsed(KWin::AbstractClient*, bool) /home/jenkins/workspace/Plasma/kwin/kf5-qt5 SUSEQt5.12/rules.cpp:1144
#3 0x7fbc0fe36e32 in KWin::ShellClient::destroyClient() /home/jenkins/workspace/Plasma/kwin/kf5-qt5 SUSEQt5.12/shell_client.cpp:435
#4 0x7fbc0fe7a726 in QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, void (KWin::ShellClient::*)()>::call(void (KWin::ShellClient::*)(), KWin::ShellClient*, void**) /usr/include/qt5/QtCore/qobjectdefs_impl.h:152
#5 0x7fbc0fe784c3 in void QtPrivate::FunctionPointer<void (KWin::ShellClient::*)()>::call<QtPrivate::List<>, void>(void (KWin::ShellClient::*)(), KWin::ShellClient*, void**) /usr/include/qt5/QtCore/qobjectdefs_impl.h:185
#6 0x7fbc0fe74de9 in QtPrivate::QSlotObject<void (KWin::ShellClient::*)(), QtPrivate::List<>, void>::impl(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*) (/home/jenkins/install-prefix/lib64/libkwin.so.5+0x1677de9)
#7 0x7fbc04f27357 in QMetaObject::activate(QObject*, int, int, void**) (/usr/lib64/libQt5Core.so.5+0x2b3357)
#8 0x7fbc074e1970 in KWayland::Server::Resource::unbound() /home/jenkins/workspace/Administration/Dependency Build Plasma kf5-qt5 SUSEQt5.12/kwayland/build/src/server/KF5WaylandServer_autogen/EWIEGA46WW/moc_resource.cpp:142
#9 0x7fbc0766b4b4 in KWayland::Server::Resource::Private::unbind(wl_resource*) /home/jenkins/workspace/Administration/Dependency Build Plasma kf5-qt5 SUSEQt5.12/kwayland/src/server/resource.cpp:68
#10 0x7fbc00bdc2ae (/usr/lib64/libwayland-server.so.0+0x92ae)
#11 0x7fbc00bdc32f in wl_resource_destroy (/usr/lib64/libwayland-server.so.0+0x932f)
#12 0x7fbc0766b53f in KWayland::Server::Resource::Private::resourceDestroyedCallback(wl_client*, wl_resource*) /home/jenkins/workspace/Administration/Dependency Build Plasma kf5-qt5 SUSEQt5.12/kwayland/src/server/resource.cpp:76
#13 0x7fbbff481d8c (/usr/lib64/libffi.so.7+0x6d8c)
#14 0x7fbbff481179 (/usr/lib64/libffi.so.7+0x6179)
#15 0x7fbc00bdfa5f (/usr/lib64/libwayland-server.so.0+0xca5f)
#16 0x7fbc00bdc6d1 (/usr/lib64/libwayland-server.so.0+0x96d1)
#17 0x7fbc00bddc71 in wl_event_loop_dispatch (/usr/lib64/libwayland-server.so.0+0xac71)
#18 0x7fbc07541e50 in KWayland::Server::Display::Private::dispatch() /home/jenkins/workspace/Administration/Dependency Build Plasma kf5-qt5 SUSEQt5.12/kwayland/src/server/display.cpp:148
#19 0x7fbc075432de in KWayland::Server::Display::dispatchEvents(int) /home/jenkins/workspace/Administration/Dependency Build Plasma kf5-qt5 SUSEQt5.12/kwayland/src/server/display.cpp:220
#20 0x7fbc0fe864ca in KWin::WaylandServer::dispatch() /home/jenkins/workspace/Plasma/kwin/kf5-qt5 SUSEQt5.12/wayland_server.cpp:616
#21 0x451ce0 in KWin::WaylandTestApplication::~WaylandTestApplication() /home/jenkins/workspace/Plasma/kwin/kf5-qt5 SUSEQt5.12/autotests/integration/kwin_wayland_test.cpp:91
#22 0x42faa1 in main /home/jenkins/workspace/Plasma/kwin/kf5-qt5 SUSEQt5.12/autotests/integration/globalshortcuts_test.cpp:381
#23 0x7fbc04796bca in __libc_start_main (/lib64/libc.so.6+0x26bca)
#24 0x413ea9 in _start (/home/jenkins/workspace/Plasma/kwin/kf5-qt5 SUSEQt5.12/build/bin/testGlobalShortcuts+0x413ea9)
Reviewers: #kwin, davidedmundson
Reviewed By: #kwin, davidedmundson
Subscribers: kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D22821
2019-07-30 08:40:18 +00:00
|
|
|
delete m_dataBridge;
|
|
|
|
m_dataBridge = nullptr;
|
|
|
|
|
2019-02-19 08:50:20 +00:00
|
|
|
disconnect(m_xwaylandFailConnection);
|
|
|
|
if (m_app->x11Connection()) {
|
|
|
|
Xcb::setInputFocus(XCB_INPUT_FOCUS_POINTER_ROOT);
|
|
|
|
m_app->destroyAtoms();
|
|
|
|
Q_EMIT m_app->x11ConnectionAboutToBeDestroyed();
|
|
|
|
xcb_disconnect(m_app->x11Connection());
|
|
|
|
m_app->setX11Connection(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_xwaylandProcess) {
|
|
|
|
m_xwaylandProcess->terminate();
|
|
|
|
while (m_xwaylandProcess->state() != QProcess::NotRunning) {
|
|
|
|
m_app->processEvents(QEventLoop::WaitForMoreEvents);
|
|
|
|
}
|
|
|
|
waylandServer()->destroyXWaylandConnection();
|
|
|
|
}
|
2018-08-21 20:06:42 +00:00
|
|
|
s_self = nullptr;
|
2019-02-19 08:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Xwayland::init()
|
|
|
|
{
|
|
|
|
int pipeFds[2];
|
|
|
|
if (pipe(pipeFds) != 0) {
|
|
|
|
std::cerr << "FATAL ERROR failed to create pipe to start Xwayland " << std::endl;
|
|
|
|
Q_EMIT criticalError(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int sx[2];
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sx) < 0) {
|
|
|
|
std::cerr << "FATAL ERROR: failed to open socket to open XCB connection" << std::endl;
|
|
|
|
Q_EMIT criticalError(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int fd = dup(sx[1]);
|
|
|
|
if (fd < 0) {
|
|
|
|
std::cerr << "FATAL ERROR: failed to open socket to open XCB connection" << std::endl;
|
|
|
|
Q_EMIT criticalError(20);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int waylandSocket = waylandServer()->createXWaylandConnection();
|
|
|
|
if (waylandSocket == -1) {
|
|
|
|
std::cerr << "FATAL ERROR: failed to open socket for Xwayland" << std::endl;
|
|
|
|
Q_EMIT criticalError(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const int wlfd = dup(waylandSocket);
|
|
|
|
if (wlfd < 0) {
|
|
|
|
std::cerr << "FATAL ERROR: failed to open socket for Xwayland" << std::endl;
|
|
|
|
Q_EMIT criticalError(20);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_xcbConnectionFd = sx[0];
|
|
|
|
|
|
|
|
m_xwaylandProcess = new Process(this);
|
|
|
|
m_xwaylandProcess->setProcessChannelMode(QProcess::ForwardedErrorChannel);
|
|
|
|
m_xwaylandProcess->setProgram(QStringLiteral("Xwayland"));
|
|
|
|
QProcessEnvironment env = m_app->processStartupEnvironment();
|
|
|
|
env.insert("WAYLAND_SOCKET", QByteArray::number(wlfd));
|
|
|
|
env.insert("EGL_PLATFORM", QByteArrayLiteral("DRM"));
|
|
|
|
m_xwaylandProcess->setProcessEnvironment(env);
|
|
|
|
m_xwaylandProcess->setArguments({QStringLiteral("-displayfd"),
|
|
|
|
QString::number(pipeFds[1]),
|
|
|
|
QStringLiteral("-rootless"),
|
|
|
|
QStringLiteral("-wm"),
|
|
|
|
QString::number(fd)});
|
|
|
|
m_xwaylandFailConnection = connect(m_xwaylandProcess, static_cast<void (QProcess::*)(QProcess::ProcessError)>(&QProcess::error), this,
|
|
|
|
[this] (QProcess::ProcessError error) {
|
|
|
|
if (error == QProcess::FailedToStart) {
|
|
|
|
std::cerr << "FATAL ERROR: failed to start Xwayland" << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cerr << "FATAL ERROR: Xwayland failed, going to exit now" << std::endl;
|
|
|
|
}
|
|
|
|
Q_EMIT criticalError(1);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const int xDisplayPipe = pipeFds[0];
|
|
|
|
connect(m_xwaylandProcess, &QProcess::started, this,
|
|
|
|
[this, xDisplayPipe] {
|
|
|
|
QFutureWatcher<void> *watcher = new QFutureWatcher<void>(this);
|
|
|
|
QObject::connect(watcher, &QFutureWatcher<void>::finished, this, &Xwayland::continueStartupWithX, Qt::QueuedConnection);
|
|
|
|
QObject::connect(watcher, &QFutureWatcher<void>::finished, watcher, &QFutureWatcher<void>::deleteLater, Qt::QueuedConnection);
|
|
|
|
watcher->setFuture(QtConcurrent::run(readDisplay, xDisplayPipe));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
m_xwaylandProcess->start();
|
|
|
|
close(pipeFds[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xwayland::createX11Connection()
|
|
|
|
{
|
|
|
|
int screenNumber = 0;
|
|
|
|
xcb_connection_t *c = nullptr;
|
|
|
|
if (m_xcbConnectionFd == -1) {
|
|
|
|
c = xcb_connect(nullptr, &screenNumber);
|
|
|
|
} else {
|
|
|
|
c = xcb_connect_to_fd(m_xcbConnectionFd, nullptr);
|
|
|
|
}
|
|
|
|
if (int error = xcb_connection_has_error(c)) {
|
|
|
|
std::cerr << "FATAL ERROR: Creating connection to XServer failed: " << error << std::endl;
|
|
|
|
Q_EMIT criticalError(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-21 20:06:42 +00:00
|
|
|
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(xcb_get_setup(c));
|
|
|
|
m_xcbScreen = iter.data;
|
|
|
|
Q_ASSERT(m_xcbScreen);
|
|
|
|
|
2019-02-19 08:50:20 +00:00
|
|
|
m_app->setX11Connection(c);
|
|
|
|
// we don't support X11 multi-head in Wayland
|
|
|
|
m_app->setX11ScreenNumber(screenNumber);
|
|
|
|
m_app->setX11RootWindow(defaultScreen()->root);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xwayland::continueStartupWithX()
|
|
|
|
{
|
|
|
|
createX11Connection();
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_connection_t *xcbConn = m_app->x11Connection();
|
2019-02-19 08:50:20 +00:00
|
|
|
if (!xcbConn) {
|
|
|
|
// about to quit
|
|
|
|
Q_EMIT criticalError(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QSocketNotifier *notifier = new QSocketNotifier(xcb_get_file_descriptor(xcbConn), QSocketNotifier::Read, this);
|
|
|
|
auto processXcbEvents = [this, xcbConn] {
|
|
|
|
while (auto event = xcb_poll_for_event(xcbConn)) {
|
2018-08-21 20:06:42 +00:00
|
|
|
if (m_dataBridge->filterEvent(event)) {
|
|
|
|
free(event);
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-19 08:50:20 +00:00
|
|
|
long result = 0;
|
|
|
|
QThread::currentThread()->eventDispatcher()->filterNativeEvent(QByteArrayLiteral("xcb_generic_event_t"), event, &result);
|
|
|
|
free(event);
|
|
|
|
}
|
|
|
|
xcb_flush(xcbConn);
|
|
|
|
};
|
|
|
|
connect(notifier, &QSocketNotifier::activated, this, processXcbEvents);
|
|
|
|
connect(QThread::currentThread()->eventDispatcher(), &QAbstractEventDispatcher::aboutToBlock, this, processXcbEvents);
|
|
|
|
connect(QThread::currentThread()->eventDispatcher(), &QAbstractEventDispatcher::awake, this, processXcbEvents);
|
|
|
|
|
2018-08-21 20:06:42 +00:00
|
|
|
xcb_prefetch_extension_data(xcbConn, &xcb_xfixes_id);
|
|
|
|
m_xfixes = xcb_get_extension_data(xcbConn, &xcb_xfixes_id);
|
|
|
|
|
2019-02-19 08:50:20 +00:00
|
|
|
// create selection owner for WM_S0 - magic X display number expected by XWayland
|
|
|
|
KSelectionOwner owner("WM_S0", xcbConn, m_app->x11RootWindow());
|
|
|
|
owner.claim(true);
|
|
|
|
|
|
|
|
m_app->createAtoms();
|
|
|
|
m_app->setupEventFilters();
|
|
|
|
|
2018-08-21 20:06:42 +00:00
|
|
|
m_dataBridge = new DataBridge;
|
|
|
|
|
2019-02-19 08:50:20 +00:00
|
|
|
// Check whether another windowmanager is running
|
|
|
|
const uint32_t maskValues[] = {XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT};
|
|
|
|
ScopedCPointer<xcb_generic_error_t> redirectCheck(xcb_request_check(connection(),
|
|
|
|
xcb_change_window_attributes_checked(connection(),
|
|
|
|
rootWindow(),
|
|
|
|
XCB_CW_EVENT_MASK,
|
|
|
|
maskValues)));
|
|
|
|
if (!redirectCheck.isNull()) {
|
|
|
|
fputs(i18n("kwin_wayland: an X11 window manager is running on the X11 Display.\n").toLocal8Bit().constData(), stderr);
|
|
|
|
Q_EMIT criticalError(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto env = m_app->processStartupEnvironment();
|
|
|
|
env.insert(QStringLiteral("DISPLAY"), QString::fromUtf8(qgetenv("DISPLAY")));
|
|
|
|
m_app->setProcessStartupEnvironment(env);
|
|
|
|
|
2019-02-23 12:17:50 +00:00
|
|
|
emit initialized();
|
2019-02-19 08:50:20 +00:00
|
|
|
|
|
|
|
Xcb::sync(); // Trigger possible errors, there's still a chance to abort
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
DragEventReply Xwayland::dragMoveFilter(Toplevel *target, const QPoint &pos)
|
2018-08-22 12:56:48 +00:00
|
|
|
{
|
|
|
|
if (!m_dataBridge) {
|
|
|
|
return DragEventReply::Wayland;
|
|
|
|
}
|
|
|
|
return m_dataBridge->dragMoveFilter(target, pos);
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
} // namespace Xwl
|
|
|
|
} // namespace KWin
|