Commit a7f5c167 authored by Martin Flöser's avatar Martin Flöser

Reduce code-duplication of socketpair in WaylandServer

Summary:
WaylandServer creates a socketpair for several Wayland connections. So
far this duplicated code quite a bit.

This change introduces one method to perform the socketpair and the
error checking plus creating the server side ClientConnection from the
pair.

Test Plan: Tests still pass

Reviewers: #kwin, #plasma_on_wayland

Subscribers: plasma-devel, kwin

Tags: #plasma_on_wayland, #kwin

Differential Revision: https://phabricator.kde.org/D3576
parent 1f5356e6
......@@ -362,20 +362,32 @@ void WaylandServer::syncOutputsToWayland()
}
}
int WaylandServer::createXWaylandConnection()
WaylandServer::SocketPairConnection WaylandServer::createConnection()
{
SocketPairConnection ret;
int sx[2];
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sx) < 0) {
qCWarning(KWIN_CORE) << "Could not create socket";
return ret;
}
ret.connection = m_display->createClient(sx[0]);
ret.fd = sx[1];
return ret;
}
int WaylandServer::createXWaylandConnection()
{
const auto socket = createConnection();
if (!socket.connection) {
return -1;
}
m_xwayland.client = m_display->createClient(sx[0]);
m_xwayland.client = socket.connection;
m_xwayland.destroyConnection = connect(m_xwayland.client, &KWayland::Server::ClientConnection::disconnected, this,
[] {
qFatal("Xwayland Connection died");
}
);
return sx[1];
return socket.fd;
}
void WaylandServer::destroyXWaylandConnection()
......@@ -394,13 +406,12 @@ void WaylandServer::destroyXWaylandConnection()
int WaylandServer::createInputMethodConnection()
{
int sx[2];
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sx) < 0) {
qCWarning(KWIN_CORE) << "Could not create socket";
const auto socket = createConnection();
if (!socket.connection) {
return -1;
}
m_inputMethodServerConnection = m_display->createClient(sx[0]);
return sx[1];
m_inputMethodServerConnection = socket.connection;
return socket.fd;
}
void WaylandServer::destroyInputMethodConnection()
......@@ -414,13 +425,12 @@ void WaylandServer::destroyInputMethodConnection()
int WaylandServer::createXclipboardSyncConnection()
{
int sx[2];
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sx) < 0) {
qCWarning(KWIN_CORE) << "Could not create socket";
const auto socket = createConnection();
if (!socket.connection) {
return -1;
}
m_xclipbaordSync.client = m_display->createClient(sx[0]);
return sx[1];
m_xclipbaordSync.client = socket.connection;
return socket.fd;
}
void WaylandServer::setupX11ClipboardSync()
......@@ -460,15 +470,14 @@ void WaylandServer::setupX11ClipboardSync()
void WaylandServer::createInternalConnection()
{
int sx[2];
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sx) < 0) {
qCWarning(KWIN_CORE) << "Could not create socket";
const auto socket = createConnection();
if (!socket.connection) {
return;
}
m_internalConnection.server = m_display->createClient(sx[0]);
m_internalConnection.server = socket.connection;
using namespace KWayland::Client;
m_internalConnection.client = new ConnectionThread();
m_internalConnection.client->setSocketFd(sx[1]);
m_internalConnection.client->setSocketFd(socket.fd);
m_internalConnection.clientThread = new QThread;
m_internalConnection.client->moveToThread(m_internalConnection.clientThread);
m_internalConnection.clientThread->start();
......
......@@ -164,6 +164,25 @@ public:
void dispatch();
quint32 createWindowId(KWayland::Server::SurfaceInterface *surface);
/**
* Struct containing information for a created Wayland connection through a
* socketpair.
**/
struct SocketPairConnection {
/**
* ServerSide Connection
**/
KWayland::Server::ClientConnection *connection = nullptr;
/**
* client-side file descriptor for the socket
**/
int fd = -1;
};
/**
* Creates a Wayland connection using a socket pair.
**/
SocketPairConnection createConnection();
Q_SIGNALS:
void shellClientAdded(KWin::ShellClient*);
void shellClientRemoved(KWin::ShellClient*);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment