KWaylandServer

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 KWaylandServer
15 {
16 class ClientConnectionPrivate
17 {
18 public:
19  ClientConnectionPrivate(wl_client *c, Display *display, ClientConnection *q);
20  ~ClientConnectionPrivate();
21 
22  wl_client *client;
23  Display *display;
24  pid_t pid = 0;
25  uid_t user = 0;
26  gid_t group = 0;
27  QString executablePath;
28 
29 private:
30  static void destroyListenerCallback(wl_listener *listener, void *data);
31  ClientConnection *q;
32  wl_listener listener;
33  static QVector<ClientConnectionPrivate *> s_allClients;
34 };
35 
36 QVector<ClientConnectionPrivate *> ClientConnectionPrivate::s_allClients;
37 
38 ClientConnectionPrivate::ClientConnectionPrivate(wl_client *c, Display *display, ClientConnection *q)
39  : client(c)
40  , display(display)
41  , q(q)
42 {
43  s_allClients << this;
44  listener.notify = destroyListenerCallback;
45  wl_client_add_destroy_listener(c, &listener);
46  wl_client_get_credentials(client, &pid, &user, &group);
47  executablePath = QFileInfo(QStringLiteral("/proc/%1/exe").arg(pid)).symLinkTarget();
48 }
49 
50 ClientConnectionPrivate::~ClientConnectionPrivate()
51 {
52  if (client) {
53  wl_list_remove(&listener.link);
54  }
55  s_allClients.removeAt(s_allClients.indexOf(this));
56 }
57 
58 void ClientConnectionPrivate::destroyListenerCallback(wl_listener *listener, void *data)
59 {
60  Q_UNUSED(listener)
61  wl_client *client = reinterpret_cast<wl_client *>(data);
62  auto it = std::find_if(s_allClients.constBegin(), s_allClients.constEnd(), [client](ClientConnectionPrivate *c) {
63  return c->client == client;
64  });
65  Q_ASSERT(it != s_allClients.constEnd());
66  auto p = (*it);
67  auto q = p->q;
68  Q_EMIT q->aboutToBeDestroyed();
69  p->client = nullptr;
70  wl_list_remove(&p->listener.link);
71  Q_EMIT q->disconnected(q);
72  q->deleteLater();
73 }
74 
75 ClientConnection::ClientConnection(wl_client *c, Display *parent)
76  : QObject(parent)
77  , d(new ClientConnectionPrivate(c, parent, this))
78 {
79 }
80 
81 ClientConnection::~ClientConnection() = default;
82 
84 {
85  if (!d->client) {
86  return;
87  }
88  wl_client_flush(d->client);
89 }
90 
92 {
93  if (!d->client) {
94  return;
95  }
96  wl_client_destroy(d->client);
97 }
98 
99 wl_resource *ClientConnection::getResource(quint32 id) const
100 {
101  if (!d->client) {
102  return nullptr;
103  }
104  return wl_client_get_object(d->client, id);
105 }
106 
107 wl_client *ClientConnection::client() const
108 {
109  return d->client;
110 }
111 
112 ClientConnection::operator wl_client *()
113 {
114  return d->client;
115 }
116 
117 ClientConnection::operator wl_client *() const
118 {
119  return d->client;
120 }
121 
123 {
124  return d->display;
125 }
126 
128 {
129  return d->group;
130 }
131 
133 {
134  return d->pid;
135 }
136 
138 {
139  return d->user;
140 }
141 
143 {
144  return d->executablePath;
145 }
146 
147 }
QString symLinkTarget() const const
Class holding the Wayland server display loop.
Definition: display.h:47
wl_resource * getResource(quint32 id) const
Get the wl_resource associated with the given id.
void destroy()
Destroys this ClientConnection.
QString executablePath() const
The absolute path to the executable.
void flush()
Flushes the connection to this client.
pid_t processId() const
The pid of the ClientConnection endpoint.
gid_t groupId() const
The gid of the ClientConnection endpoint.
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 Mon Oct 25 2021 23:08:43 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.