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