From a7f5c1675425a81a955c1aac880706cb39b0b7ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Sat, 3 Dec 2016 14:54:32 +0100 Subject: [PATCH] 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 --- wayland_server.cpp | 45 +++++++++++++++++++++++++++------------------ wayland_server.h | 19 +++++++++++++++++++ 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/wayland_server.cpp b/wayland_server.cpp index b9e909fa8b..776c3af4f4 100644 --- a/wayland_server.cpp +++ b/wayland_server.cpp @@ -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(); diff --git a/wayland_server.h b/wayland_server.h index eabfa394c6..706de95dd2 100644 --- a/wayland_server.h +++ b/wayland_server.h @@ -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*);