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 class ClientConnection::Private
19 {
20 public:
21  explicit Private(wl_client *c, Display *display, ClientConnection *q);
22  ~Private();
23 
24  wl_client *client;
26  pid_t pid = 0;
27  uid_t user = 0;
28  gid_t group = 0;
30 
31 private:
32  static void destroyListenerCallback(wl_listener *listener, void *data);
33  ClientConnection *q;
34  wl_listener listener;
35  static QVector<Private *> s_allClients;
36 };
37 
38 QVector<ClientConnection::Private *> ClientConnection::Private::s_allClients;
39 
40 ClientConnection::Private::Private(wl_client *c, Display *display, ClientConnection *q)
41  : client(c)
42  , display(display)
43  , q(q)
44 {
45  s_allClients << this;
46  listener.notify = destroyListenerCallback;
47  wl_client_add_destroy_listener(c, &listener);
48  wl_client_get_credentials(client, &pid, &user, &group);
49  executablePath = QFileInfo(QStringLiteral("/proc/%1/exe").arg(pid)).symLinkTarget();
50 }
51 
52 ClientConnection::Private::~Private()
53 {
54  if (client) {
55  wl_list_remove(&listener.link);
56  }
57  s_allClients.removeAt(s_allClients.indexOf(this));
58 }
59 
60 void ClientConnection::Private::destroyListenerCallback(wl_listener *listener, void *data)
61 {
62  Q_UNUSED(listener)
63  wl_client *client = reinterpret_cast<wl_client *>(data);
64  auto it = std::find_if(s_allClients.constBegin(), s_allClients.constEnd(), [client](Private *c) {
65  return c->client == client;
66  });
67  Q_ASSERT(it != s_allClients.constEnd());
68  auto p = (*it);
69  auto q = p->q;
70  p->client = nullptr;
71  wl_list_remove(&p->listener.link);
72  Q_EMIT q->disconnected(q);
73  q->deleteLater();
74 }
75 
76 ClientConnection::ClientConnection(wl_client *c, Display *parent)
77  : QObject(parent)
78  , d(new Private(c, parent, this))
79 {
80 }
81 
82 ClientConnection::~ClientConnection() = default;
83 
85 {
86  if (!d->client) {
87  return;
88  }
89  wl_client_flush(d->client);
90 }
91 
93 {
94  if (!d->client) {
95  return;
96  }
97  wl_client_destroy(d->client);
98 }
99 
100 wl_resource *ClientConnection::createResource(const wl_interface *interface, quint32 version, quint32 id)
101 {
102  if (!d->client) {
103  return nullptr;
104  }
105  return wl_resource_create(d->client, interface, version, id);
106 }
107 
108 wl_resource *ClientConnection::getResource(quint32 id)
109 {
110  if (!d->client) {
111  return nullptr;
112  }
113  return wl_client_get_object(d->client, id);
114 }
115 
117 {
118  return d->client;
119 }
120 
121 ClientConnection::operator wl_client *()
122 {
123  return d->client;
124 }
125 
126 ClientConnection::operator wl_client *() const
127 {
128  return d->client;
129 }
130 
132 {
133  return d->display;
134 }
135 
137 {
138  return d->group;
139 }
140 
142 {
143  return d->pid;
144 }
145 
147 {
148  return d->user;
149 }
150 
152 {
153  return d->executablePath;
154 }
155 
156 }
157 }
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:86
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
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Sep 23 2021 22:51:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.