KWayland

clientconnection.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Martin Gräßlin <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 #include "clientconnection.h"
7 #include "display.h"
8 // Qt
9 #include <QFileInfo>
10 #include <QVector>
11 // Wayland
12 #include <wayland-server.h>
13 
14 namespace KWayland
15 {
16 namespace Server
17 {
18 
19 class ClientConnection::Private
20 {
21 public:
22  explicit Private(wl_client *c, Display *display, ClientConnection *q);
23  ~Private();
24 
25  wl_client *client;
27  pid_t pid = 0;
28  uid_t user = 0;
29  gid_t group = 0;
31 
32 private:
33  static void destroyListenerCallback(wl_listener *listener, void *data);
34  ClientConnection *q;
35  wl_listener listener;
36  static QVector<Private*> s_allClients;
37 };
38 
39 QVector<ClientConnection::Private*> ClientConnection::Private::s_allClients;
40 
41 ClientConnection::Private::Private(wl_client *c, Display *display, ClientConnection *q)
42  : client(c)
43  , display(display)
44  , q(q)
45 {
46  s_allClients << this;
47  listener.notify = destroyListenerCallback;
48  wl_client_add_destroy_listener(c, &listener);
49  wl_client_get_credentials(client, &pid, &user, &group);
50  executablePath = QFileInfo(QStringLiteral("/proc/%1/exe").arg(pid)).symLinkTarget();
51 }
52 
53 ClientConnection::Private::~Private()
54 {
55  if (client) {
56  wl_list_remove(&listener.link);
57  }
58  s_allClients.removeAt(s_allClients.indexOf(this));
59 }
60 
61 void ClientConnection::Private::destroyListenerCallback(wl_listener *listener, void *data)
62 {
63  Q_UNUSED(listener)
64  wl_client *client = reinterpret_cast<wl_client*>(data);
65  auto it = std::find_if(s_allClients.constBegin(), s_allClients.constEnd(),
66  [client](Private *c) {
67  return c->client == client;
68  }
69  );
70  Q_ASSERT(it != s_allClients.constEnd());
71  auto p = (*it);
72  auto q = p->q;
73  p->client = nullptr;
74  wl_list_remove(&p->listener.link);
75  emit q->disconnected(q);
76  q->deleteLater();
77 }
78 
79 ClientConnection::ClientConnection(wl_client *c, Display *parent)
80  : QObject(parent)
81  , d(new Private(c, parent, this))
82 {
83 }
84 
85 ClientConnection::~ClientConnection() = default;
86 
88 {
89  if (!d->client) {
90  return;
91  }
92  wl_client_flush(d->client);
93 }
94 
96 {
97  if (!d->client) {
98  return;
99  }
100  wl_client_destroy(d->client);
101 }
102 
103 wl_resource *ClientConnection::createResource(const wl_interface *interface, quint32 version, quint32 id)
104 {
105  if (!d->client) {
106  return nullptr;
107  }
108  return wl_resource_create(d->client, interface, version, id);
109 }
110 
111 wl_resource *ClientConnection::getResource(quint32 id)
112 {
113  if (!d->client) {
114  return nullptr;
115  }
116  return wl_client_get_object(d->client, id);
117 }
118 
120 {
121  return d->client;
122 }
123 
124 ClientConnection::operator wl_client*()
125 {
126  return d->client;
127 }
128 
129 ClientConnection::operator wl_client*() const
130 {
131  return d->client;
132 }
133 
135 {
136  return d->display;
137 }
138 
140 {
141  return d->group;
142 }
143 
145 {
146  return d->pid;
147 }
148 
150 {
151  return d->user;
152 }
153 
155 {
156  return d->executablePath;
157 }
158 
159 }
160 }
QString symLinkTarget() const const
pid_t processId() const
The pid of the ClientConnection endpoint.
QString executablePath() const
The absolute path to the executable.
gid_t groupId() const
The gid of the ClientConnection endpoint.
QImage data()
Creates a QImage for the shared memory buffer.
void destroy()
Destroys this ClientConnection.
Class holding the Wayland server display loop.
Definition: display.h:89
wl_resource * getResource(quint32 id)
Get the wl_resource associated with the given id.
void flush()
Flushes the connection to this client.
wl_resource * createResource(const wl_interface *interface, quint32 version, quint32 id)
Creates a new wl_resource for the provided interface.
uid_t userId() const
The uid of the ClientConnection endpoint.
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 7 2020 22:48:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.