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
icc-effect-5.14.5
Martin Gräßlin 2016-12-03 14:54:32 +01:00
parent 1f5356e609
commit a7f5c16754
2 changed files with 46 additions and 18 deletions

View File

@ -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();

View File

@ -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*);