2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2019-02-19 08:50:20 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
|
|
|
|
SPDX-FileCopyrightText: 2019 Roman Gilg <subdiff@gmail.com>
|
|
|
|
SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
|
2019-02-19 08:50:20 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2019-02-19 08:50:20 +00:00
|
|
|
#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"
|
2020-08-27 09:05:28 +00:00
|
|
|
#include "options.h"
|
2019-02-19 08:50:20 +00:00
|
|
|
#include "utils.h"
|
2019-07-02 19:56:03 +00:00
|
|
|
#include "wayland_server.h"
|
|
|
|
#include "xcbutils.h"
|
2020-07-20 08:07:08 +00:00
|
|
|
#include "xwayland_logging.h"
|
2019-02-19 08:50:20 +00:00
|
|
|
|
|
|
|
#include <KLocalizedString>
|
2020-08-31 12:50:50 +00:00
|
|
|
#include <KNotification>
|
2019-06-22 10:40:12 +00:00
|
|
|
#include <KSelectionOwner>
|
2019-02-19 08:50:20 +00:00
|
|
|
|
|
|
|
#include <QAbstractEventDispatcher>
|
2021-02-08 11:57:12 +00:00
|
|
|
#include <QDataStream>
|
2019-02-19 08:50:20 +00:00
|
|
|
#include <QFile>
|
2021-02-08 11:57:12 +00:00
|
|
|
#include <QHostInfo>
|
|
|
|
#include <QRandomGenerator>
|
2020-08-27 09:05:28 +00:00
|
|
|
#include <QTimer>
|
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>
|
2020-09-08 08:27:42 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
2019-02-19 08:50:20 +00:00
|
|
|
|
2021-02-08 11:57:12 +00:00
|
|
|
static int readDisplay(int pipe)
|
2019-02-19 08:50:20 +00:00
|
|
|
{
|
2021-02-08 11:57:12 +00:00
|
|
|
int display = -1;
|
2019-02-19 08:50:20 +00:00
|
|
|
QFile readPipe;
|
2020-09-08 09:35:47 +00:00
|
|
|
|
2019-02-19 08:50:20 +00:00
|
|
|
if (!readPipe.open(pipe, QIODevice::ReadOnly)) {
|
2020-09-08 08:27:42 +00:00
|
|
|
qCWarning(KWIN_XWL) << "Failed to open X11 display name pipe:" << readPipe.errorString();
|
2020-09-08 09:35:47 +00:00
|
|
|
} else {
|
2021-02-08 11:57:12 +00:00
|
|
|
display = readPipe.readLine().trimmed().toInt();
|
2019-02-19 08:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// close our pipe
|
|
|
|
close(pipe);
|
2021-02-08 11:57:12 +00:00
|
|
|
return display;
|
2019-02-19 08:50:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
2019-02-19 08:50:20 +00:00
|
|
|
namespace Xwl
|
|
|
|
{
|
|
|
|
|
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
|
|
|
{
|
2020-08-27 09:05:28 +00:00
|
|
|
m_resetCrashCountTimer = new QTimer(this);
|
|
|
|
m_resetCrashCountTimer->setSingleShot(true);
|
|
|
|
connect(m_resetCrashCountTimer, &QTimer::timeout, this, &Xwayland::resetCrashCount);
|
2019-02-19 08:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Xwayland::~Xwayland()
|
|
|
|
{
|
2020-07-20 08:07:08 +00:00
|
|
|
stop();
|
2019-02-19 08:50:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 07:44:13 +00:00
|
|
|
QProcess *Xwayland::process() const
|
|
|
|
{
|
|
|
|
return m_xwaylandProcess;
|
|
|
|
}
|
|
|
|
|
2020-07-20 08:07:08 +00:00
|
|
|
void Xwayland::start()
|
2019-02-19 08:50:20 +00:00
|
|
|
{
|
2020-09-01 06:32:12 +00:00
|
|
|
if (m_xwaylandProcess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-19 08:50:20 +00:00
|
|
|
int pipeFds[2];
|
|
|
|
if (pipe(pipeFds) != 0) {
|
2020-09-08 08:27:42 +00:00
|
|
|
qCWarning(KWIN_XWL, "Failed to create pipe to start Xwayland: %s", strerror(errno));
|
2020-09-08 08:18:31 +00:00
|
|
|
emit errorOccurred();
|
2019-02-19 08:50:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int sx[2];
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sx) < 0) {
|
2020-09-08 08:27:42 +00:00
|
|
|
qCWarning(KWIN_XWL, "Failed to open socket for XCB connection: %s", strerror(errno));
|
2020-09-08 08:18:31 +00:00
|
|
|
emit errorOccurred();
|
2019-02-19 08:50:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int fd = dup(sx[1]);
|
|
|
|
if (fd < 0) {
|
2020-09-08 08:27:42 +00:00
|
|
|
qCWarning(KWIN_XWL, "Failed to open socket for XCB connection: %s", strerror(errno));
|
2020-09-08 08:18:31 +00:00
|
|
|
emit errorOccurred();
|
2019-02-19 08:50:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int waylandSocket = waylandServer()->createXWaylandConnection();
|
|
|
|
if (waylandSocket == -1) {
|
2020-09-08 08:27:42 +00:00
|
|
|
qCWarning(KWIN_XWL, "Failed to open socket for Xwayland server: %s", strerror(errno));
|
2020-09-08 08:18:31 +00:00
|
|
|
emit errorOccurred();
|
2019-02-19 08:50:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const int wlfd = dup(waylandSocket);
|
|
|
|
if (wlfd < 0) {
|
2020-09-08 08:27:42 +00:00
|
|
|
qCWarning(KWIN_XWL, "Failed to open socket for Xwayland server: %s", strerror(errno));
|
2020-09-08 08:18:31 +00:00
|
|
|
emit errorOccurred();
|
2019-02-19 08:50:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-08 11:57:12 +00:00
|
|
|
if (!createXauthorityFile()) {
|
|
|
|
qCWarning(KWIN_XWL) << "Failed to create an Xauthority file";
|
|
|
|
emit errorOccurred();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-19 08:50:20 +00:00
|
|
|
m_xcbConnectionFd = sx[0];
|
2020-07-20 08:07:08 +00:00
|
|
|
m_displayFileDescriptor = pipeFds[0];
|
2019-02-19 08:50:20 +00:00
|
|
|
|
|
|
|
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"));
|
2021-01-29 11:12:02 +00:00
|
|
|
if (qEnvironmentVariableIsSet("KWIN_XWAYLAND_DEBUG")) {
|
|
|
|
env.insert("WAYLAND_DEBUG", QByteArrayLiteral("1"));
|
|
|
|
}
|
2019-02-19 08:50:20 +00:00
|
|
|
m_xwaylandProcess->setProcessEnvironment(env);
|
|
|
|
m_xwaylandProcess->setArguments({QStringLiteral("-displayfd"),
|
|
|
|
QString::number(pipeFds[1]),
|
|
|
|
QStringLiteral("-rootless"),
|
2021-02-08 11:57:12 +00:00
|
|
|
QStringLiteral("-wm"), QString::number(fd),
|
2021-02-10 12:52:38 +00:00
|
|
|
QStringLiteral("-auth"), m_authorityFile.fileName()});
|
2020-07-20 08:07:08 +00:00
|
|
|
connect(m_xwaylandProcess, &QProcess::errorOccurred, this, &Xwayland::handleXwaylandError);
|
|
|
|
connect(m_xwaylandProcess, &QProcess::started, this, &Xwayland::handleXwaylandStarted);
|
|
|
|
connect(m_xwaylandProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
|
|
|
this, &Xwayland::handleXwaylandFinished);
|
2019-02-19 08:50:20 +00:00
|
|
|
m_xwaylandProcess->start();
|
|
|
|
close(pipeFds[1]);
|
|
|
|
}
|
|
|
|
|
2020-07-20 08:07:08 +00:00
|
|
|
void Xwayland::stop()
|
2019-08-07 08:21:30 +00:00
|
|
|
{
|
2020-07-20 08:07:08 +00:00
|
|
|
if (!m_xwaylandProcess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-14 15:04:57 +00:00
|
|
|
m_app->setClosingX11Connection(true);
|
|
|
|
|
2020-07-20 08:07:08 +00:00
|
|
|
// If Xwayland has crashed, we must deactivate the socket notifier and ensure that no X11
|
|
|
|
// events will be dispatched before blocking; otherwise we will simply hang...
|
|
|
|
uninstallSocketNotifier();
|
|
|
|
|
2020-08-11 16:36:48 +00:00
|
|
|
DataBridge::destroy();
|
2021-02-08 22:00:08 +00:00
|
|
|
m_selectionOwner.reset();
|
2020-07-20 08:07:08 +00:00
|
|
|
|
|
|
|
destroyX11Connection();
|
|
|
|
|
|
|
|
// When the Xwayland process is finally terminated, the finished() signal will be emitted,
|
|
|
|
// however we don't actually want to process it anymore. Furthermore, we also don't really
|
|
|
|
// want to handle any errors that may occur during the teardown.
|
|
|
|
if (m_xwaylandProcess->state() != QProcess::NotRunning) {
|
|
|
|
disconnect(m_xwaylandProcess, nullptr, this, nullptr);
|
|
|
|
m_xwaylandProcess->terminate();
|
|
|
|
m_xwaylandProcess->waitForFinished(5000);
|
|
|
|
}
|
|
|
|
delete m_xwaylandProcess;
|
|
|
|
m_xwaylandProcess = nullptr;
|
|
|
|
|
|
|
|
waylandServer()->destroyXWaylandConnection(); // This one must be destroyed last!
|
2020-10-14 15:04:57 +00:00
|
|
|
|
|
|
|
m_app->setClosingX11Connection(false);
|
2020-07-20 08:07:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 09:05:28 +00:00
|
|
|
void Xwayland::restart()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2020-07-20 08:07:08 +00:00
|
|
|
void Xwayland::dispatchEvents()
|
|
|
|
{
|
|
|
|
xcb_connection_t *connection = kwinApp()->x11Connection();
|
|
|
|
if (!connection) {
|
|
|
|
qCWarning(KWIN_XWL, "Attempting to dispatch X11 events with no connection");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-22 06:03:27 +00:00
|
|
|
const int connectionError = xcb_connection_has_error(connection);
|
|
|
|
if (connectionError) {
|
|
|
|
qCWarning(KWIN_XWL, "The X11 connection broke (error %d)", connectionError);
|
|
|
|
stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-20 08:07:08 +00:00
|
|
|
while (xcb_generic_event_t *event = xcb_poll_for_event(connection)) {
|
|
|
|
long result = 0;
|
|
|
|
QAbstractEventDispatcher *dispatcher = QCoreApplication::eventDispatcher();
|
|
|
|
dispatcher->filterNativeEvent(QByteArrayLiteral("xcb_generic_event_t"), event, &result);
|
|
|
|
free(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
xcb_flush(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xwayland::installSocketNotifier()
|
|
|
|
{
|
|
|
|
const int fileDescriptor = xcb_get_file_descriptor(kwinApp()->x11Connection());
|
|
|
|
|
|
|
|
m_socketNotifier = new QSocketNotifier(fileDescriptor, QSocketNotifier::Read, this);
|
|
|
|
connect(m_socketNotifier, &QSocketNotifier::activated, this, &Xwayland::dispatchEvents);
|
|
|
|
|
|
|
|
QAbstractEventDispatcher *dispatcher = QCoreApplication::eventDispatcher();
|
|
|
|
connect(dispatcher, &QAbstractEventDispatcher::aboutToBlock, this, &Xwayland::dispatchEvents);
|
|
|
|
connect(dispatcher, &QAbstractEventDispatcher::awake, this, &Xwayland::dispatchEvents);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xwayland::uninstallSocketNotifier()
|
|
|
|
{
|
|
|
|
QAbstractEventDispatcher *dispatcher = QCoreApplication::eventDispatcher();
|
|
|
|
disconnect(dispatcher, &QAbstractEventDispatcher::aboutToBlock, this, &Xwayland::dispatchEvents);
|
|
|
|
disconnect(dispatcher, &QAbstractEventDispatcher::awake, this, &Xwayland::dispatchEvents);
|
|
|
|
|
|
|
|
delete m_socketNotifier;
|
|
|
|
m_socketNotifier = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xwayland::handleXwaylandStarted()
|
|
|
|
{
|
2021-02-08 11:57:12 +00:00
|
|
|
m_watcher = new QFutureWatcher<int>(this);
|
|
|
|
connect(m_watcher, &QFutureWatcher<int>::finished, this, &Xwayland::handleXwaylandReady);
|
2020-09-08 09:35:47 +00:00
|
|
|
m_watcher->setFuture(QtConcurrent::run(readDisplay, m_displayFileDescriptor));
|
2020-07-20 08:07:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 09:05:28 +00:00
|
|
|
void Xwayland::handleXwaylandFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
2020-07-20 08:07:08 +00:00
|
|
|
{
|
|
|
|
qCDebug(KWIN_XWL) << "Xwayland process has quit with exit code" << exitCode;
|
|
|
|
|
2020-08-27 09:05:28 +00:00
|
|
|
switch (exitStatus) {
|
|
|
|
case QProcess::NormalExit:
|
|
|
|
stop();
|
|
|
|
break;
|
|
|
|
case QProcess::CrashExit:
|
|
|
|
handleXwaylandCrashed();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xwayland::handleXwaylandCrashed()
|
|
|
|
{
|
2020-08-31 12:50:50 +00:00
|
|
|
KNotification::event(QStringLiteral("xwaylandcrash"), i18n("Xwayland has crashed"));
|
2020-08-27 09:05:28 +00:00
|
|
|
m_resetCrashCountTimer->stop();
|
|
|
|
|
|
|
|
switch (options->xwaylandCrashPolicy()) {
|
|
|
|
case XwaylandCrashPolicy::Restart:
|
|
|
|
if (++m_crashCount <= options->xwaylandMaxCrashCount()) {
|
|
|
|
restart();
|
|
|
|
m_resetCrashCountTimer->start(std::chrono::minutes(10));
|
|
|
|
} else {
|
|
|
|
qCWarning(KWIN_XWL, "Stopping Xwayland server because it has crashed %d times "
|
|
|
|
"over the past 10 minutes", m_crashCount);
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case XwaylandCrashPolicy::Stop:
|
|
|
|
stop();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xwayland::resetCrashCount()
|
|
|
|
{
|
|
|
|
qCDebug(KWIN_XWL) << "Resetting the crash counter, its current value is" << m_crashCount;
|
|
|
|
m_crashCount = 0;
|
2020-07-20 08:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Xwayland::handleXwaylandError(QProcess::ProcessError error)
|
|
|
|
{
|
|
|
|
switch (error) {
|
|
|
|
case QProcess::FailedToStart:
|
|
|
|
qCWarning(KWIN_XWL) << "Xwayland process failed to start";
|
|
|
|
return;
|
|
|
|
case QProcess::Crashed:
|
2020-08-27 09:05:28 +00:00
|
|
|
qCWarning(KWIN_XWL) << "Xwayland process crashed";
|
2020-07-20 08:07:08 +00:00
|
|
|
break;
|
|
|
|
case QProcess::Timedout:
|
|
|
|
qCWarning(KWIN_XWL) << "Xwayland operation timed out";
|
|
|
|
break;
|
|
|
|
case QProcess::WriteError:
|
|
|
|
case QProcess::ReadError:
|
|
|
|
qCWarning(KWIN_XWL) << "An error occurred while communicating with Xwayland";
|
|
|
|
break;
|
|
|
|
case QProcess::UnknownError:
|
|
|
|
qCWarning(KWIN_XWL) << "An unknown error has occurred in Xwayland";
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 08:18:31 +00:00
|
|
|
emit errorOccurred();
|
2019-08-07 08:21:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 09:35:47 +00:00
|
|
|
void Xwayland::handleXwaylandReady()
|
|
|
|
{
|
2021-02-08 11:57:12 +00:00
|
|
|
m_display = m_watcher->result();
|
2020-09-08 09:35:47 +00:00
|
|
|
|
|
|
|
m_watcher->deleteLater();
|
|
|
|
m_watcher = nullptr;
|
|
|
|
|
2021-02-10 13:09:02 +00:00
|
|
|
if (!createX11Connection() || !writeXauthorityEntries()) {
|
2020-09-08 09:35:47 +00:00
|
|
|
emit errorOccurred();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-08 11:57:12 +00:00
|
|
|
const QByteArray displayName = ':' + QByteArray::number(m_display);
|
|
|
|
qCInfo(KWIN_XWL) << "Xwayland server started on display" << displayName;
|
|
|
|
qputenv("DISPLAY", displayName);
|
2021-02-10 12:52:38 +00:00
|
|
|
qputenv("XAUTHORITY", m_authorityFile.fileName().toUtf8());
|
2020-09-08 09:35:47 +00:00
|
|
|
|
|
|
|
// create selection owner for WM_S0 - magic X display number expected by XWayland
|
2021-02-08 22:00:08 +00:00
|
|
|
m_selectionOwner.reset(new KSelectionOwner("WM_S0", kwinApp()->x11Connection(), kwinApp()->x11RootWindow()));
|
|
|
|
m_selectionOwner->claim(true);
|
2020-09-08 09:35:47 +00:00
|
|
|
|
|
|
|
DataBridge::create(this);
|
|
|
|
|
|
|
|
auto env = m_app->processStartupEnvironment();
|
2021-02-08 11:57:12 +00:00
|
|
|
env.insert(QStringLiteral("DISPLAY"), displayName);
|
2021-02-10 12:52:38 +00:00
|
|
|
env.insert(QStringLiteral("XAUTHORITY"), m_authorityFile.fileName());
|
2020-09-08 09:35:47 +00:00
|
|
|
m_app->setProcessStartupEnvironment(env);
|
|
|
|
|
|
|
|
emit started();
|
|
|
|
|
|
|
|
Xcb::sync(); // Trigger possible errors, there's still a chance to abort
|
|
|
|
}
|
|
|
|
|
2020-09-01 16:01:19 +00:00
|
|
|
bool Xwayland::createX11Connection()
|
2019-02-19 08:50:20 +00:00
|
|
|
{
|
2020-07-20 12:16:42 +00:00
|
|
|
xcb_connection_t *connection = xcb_connect_to_fd(m_xcbConnectionFd, nullptr);
|
2020-09-01 16:01:19 +00:00
|
|
|
|
|
|
|
const int errorCode = xcb_connection_has_error(connection);
|
|
|
|
if (errorCode) {
|
|
|
|
qCDebug(KWIN_XWL, "Failed to establish the XCB connection (error %d)", errorCode);
|
|
|
|
return false;
|
2019-02-19 08:50:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 07:58:35 +00:00
|
|
|
xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
|
|
|
|
Q_ASSERT(screen);
|
2018-08-21 20:06:42 +00:00
|
|
|
|
2020-07-20 12:16:42 +00:00
|
|
|
m_app->setX11Connection(connection);
|
2020-07-28 07:58:35 +00:00
|
|
|
m_app->setX11DefaultScreen(screen);
|
2020-07-20 12:16:42 +00:00
|
|
|
m_app->setX11ScreenNumber(0);
|
2020-07-28 07:58:35 +00:00
|
|
|
m_app->setX11RootWindow(screen->root);
|
2020-07-07 10:05:39 +00:00
|
|
|
|
|
|
|
m_app->createAtoms();
|
2020-07-20 08:07:08 +00:00
|
|
|
m_app->installNativeX11EventFilter();
|
|
|
|
|
|
|
|
installSocketNotifier();
|
2020-07-07 10:05:39 +00:00
|
|
|
|
|
|
|
// Note that it's very important to have valid x11RootWindow(), x11ScreenNumber(), and
|
|
|
|
// atoms when the rest of kwin is notified about the new X11 connection.
|
|
|
|
emit m_app->x11ConnectionChanged();
|
2020-09-01 16:01:19 +00:00
|
|
|
|
|
|
|
return true;
|
2020-07-07 10:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Xwayland::destroyX11Connection()
|
|
|
|
{
|
|
|
|
if (!m_app->x11Connection()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-20 08:07:08 +00:00
|
|
|
emit m_app->x11ConnectionAboutToBeDestroyed();
|
|
|
|
|
2020-07-07 10:05:39 +00:00
|
|
|
Xcb::setInputFocus(XCB_INPUT_FOCUS_POINTER_ROOT);
|
|
|
|
m_app->destroyAtoms();
|
2020-07-20 08:07:08 +00:00
|
|
|
m_app->removeNativeX11EventFilter();
|
2020-07-07 10:05:39 +00:00
|
|
|
|
|
|
|
xcb_disconnect(m_app->x11Connection());
|
2020-07-20 08:07:08 +00:00
|
|
|
m_xcbConnectionFd = -1;
|
|
|
|
|
2020-07-07 10:05:39 +00:00
|
|
|
m_app->setX11Connection(nullptr);
|
2020-07-28 07:58:35 +00:00
|
|
|
m_app->setX11DefaultScreen(nullptr);
|
2020-07-07 10:05:39 +00:00
|
|
|
m_app->setX11ScreenNumber(-1);
|
|
|
|
m_app->setX11RootWindow(XCB_WINDOW_NONE);
|
|
|
|
|
|
|
|
emit m_app->x11ConnectionChanged();
|
2019-02-19 08:50:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 11:57:12 +00:00
|
|
|
bool Xwayland::createXauthorityFile()
|
|
|
|
{
|
|
|
|
const QString runtimeDirectory = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
|
|
|
|
|
2021-02-10 12:52:38 +00:00
|
|
|
m_authorityFile.setFileTemplate(runtimeDirectory + QStringLiteral("/xauth_XXXXXX"));
|
|
|
|
return m_authorityFile.open();
|
2021-02-08 11:57:12 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 13:06:08 +00:00
|
|
|
static void writeXauthorityEntry(QDataStream &stream, quint16 family,
|
2021-02-08 11:57:12 +00:00
|
|
|
const QByteArray &address, const QByteArray &display,
|
|
|
|
const QByteArray &name, const QByteArray &cookie)
|
|
|
|
{
|
2021-02-10 13:06:08 +00:00
|
|
|
stream << quint16(family);
|
2021-02-08 11:57:12 +00:00
|
|
|
|
2021-02-10 13:06:08 +00:00
|
|
|
auto writeArray = [&stream](const QByteArray &str) {
|
|
|
|
stream << quint16(str.size());
|
|
|
|
stream.writeRawData(str.constData(), str.size());
|
|
|
|
};
|
2021-02-08 11:57:12 +00:00
|
|
|
|
2021-02-10 13:06:08 +00:00
|
|
|
writeArray(address);
|
|
|
|
writeArray(display);
|
|
|
|
writeArray(name);
|
|
|
|
writeArray(cookie);
|
2021-02-08 11:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static QByteArray generateXauthorityCookie()
|
|
|
|
{
|
|
|
|
QByteArray cookie;
|
|
|
|
cookie.resize(16); // Cookie must be 128bits
|
|
|
|
|
|
|
|
QRandomGenerator *generator = QRandomGenerator::system();
|
|
|
|
for (int i = 0; i < cookie.size(); ++i) {
|
|
|
|
cookie[i] = uint8_t(generator->bounded(256));
|
|
|
|
}
|
|
|
|
return cookie;
|
|
|
|
}
|
|
|
|
|
2021-02-10 13:09:02 +00:00
|
|
|
bool Xwayland::writeXauthorityEntries()
|
2021-02-08 11:57:12 +00:00
|
|
|
{
|
2021-02-10 13:18:15 +00:00
|
|
|
const QByteArray hostname = QHostInfo::localHostName().toUtf8();
|
2021-02-08 11:57:12 +00:00
|
|
|
const QByteArray display = QByteArray::number(m_display);
|
|
|
|
const QByteArray name = QByteArrayLiteral("MIT-MAGIC-COOKIE-1");
|
|
|
|
const QByteArray cookie = generateXauthorityCookie();
|
|
|
|
|
2021-02-10 12:52:38 +00:00
|
|
|
QDataStream stream(&m_authorityFile);
|
2021-02-08 11:57:12 +00:00
|
|
|
stream.setByteOrder(QDataStream::BigEndian);
|
|
|
|
|
2021-02-10 13:18:15 +00:00
|
|
|
// Write entry with FamilyLocal and the host name as address
|
|
|
|
writeXauthorityEntry(stream, 256 /* FamilyLocal */, hostname, display, name, cookie);
|
|
|
|
|
|
|
|
// Write entry with FamilyWild, no address
|
|
|
|
writeXauthorityEntry(stream, 65535 /* FamilyWild */, QByteArray{}, display, name, cookie);
|
2021-02-08 11:57:12 +00:00
|
|
|
|
2021-02-10 13:09:02 +00:00
|
|
|
return stream.status() == QDataStream::Ok && m_authorityFile.flush();
|
2021-02-08 11:57:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
DragEventReply Xwayland::dragMoveFilter(Toplevel *target, const QPoint &pos)
|
2018-08-22 12:56:48 +00:00
|
|
|
{
|
2020-08-11 16:36:48 +00:00
|
|
|
DataBridge *bridge = DataBridge::self();
|
|
|
|
if (!bridge) {
|
2018-08-22 12:56:48 +00:00
|
|
|
return DragEventReply::Wayland;
|
|
|
|
}
|
2020-08-11 16:36:48 +00:00
|
|
|
return bridge->dragMoveFilter(target, pos);
|
2018-08-22 12:56:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
} // namespace Xwl
|
|
|
|
} // namespace KWin
|