2020-03-15 15:19:28 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
|
2014-11-17 15:01:18 +00:00
|
|
|
|
2020-03-15 15:19:28 +00:00
|
|
|
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
|
|
|
|
*/
|
2014-11-17 15:01:18 +00:00
|
|
|
#include "clientconnection.h"
|
|
|
|
#include "display.h"
|
|
|
|
// Qt
|
2016-01-25 09:52:03 +00:00
|
|
|
#include <QFileInfo>
|
2014-11-17 15:01:18 +00:00
|
|
|
#include <QVector>
|
|
|
|
// Wayland
|
|
|
|
#include <wayland-server.h>
|
|
|
|
|
2020-04-29 14:56:38 +00:00
|
|
|
namespace KWaylandServer
|
2014-11-17 15:01:18 +00:00
|
|
|
{
|
|
|
|
|
2021-04-05 08:39:54 +00:00
|
|
|
class ClientConnectionPrivate
|
2014-11-17 15:01:18 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-04-05 08:39:54 +00:00
|
|
|
ClientConnectionPrivate(wl_client *c, Display *display, ClientConnection *q);
|
|
|
|
~ClientConnectionPrivate();
|
2014-11-17 15:01:18 +00:00
|
|
|
|
|
|
|
wl_client *client;
|
|
|
|
Display *display;
|
|
|
|
pid_t pid = 0;
|
|
|
|
uid_t user = 0;
|
|
|
|
gid_t group = 0;
|
2016-01-25 09:52:03 +00:00
|
|
|
QString executablePath;
|
2014-11-17 15:01:18 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void destroyListenerCallback(wl_listener *listener, void *data);
|
|
|
|
ClientConnection *q;
|
|
|
|
wl_listener listener;
|
2021-04-05 08:39:54 +00:00
|
|
|
static QVector<ClientConnectionPrivate *> s_allClients;
|
2014-11-17 15:01:18 +00:00
|
|
|
};
|
|
|
|
|
2021-04-05 08:39:54 +00:00
|
|
|
QVector<ClientConnectionPrivate *> ClientConnectionPrivate::s_allClients;
|
2014-11-17 15:01:18 +00:00
|
|
|
|
2021-04-05 08:39:54 +00:00
|
|
|
ClientConnectionPrivate::ClientConnectionPrivate(wl_client *c, Display *display, ClientConnection *q)
|
2014-11-17 15:01:18 +00:00
|
|
|
: client(c)
|
|
|
|
, display(display)
|
|
|
|
, q(q)
|
|
|
|
{
|
|
|
|
s_allClients << this;
|
|
|
|
listener.notify = destroyListenerCallback;
|
|
|
|
wl_client_add_destroy_listener(c, &listener);
|
|
|
|
wl_client_get_credentials(client, &pid, &user, &group);
|
2016-01-25 09:52:03 +00:00
|
|
|
executablePath = QFileInfo(QStringLiteral("/proc/%1/exe").arg(pid)).symLinkTarget();
|
2014-11-17 15:01:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 08:39:54 +00:00
|
|
|
ClientConnectionPrivate::~ClientConnectionPrivate()
|
2014-11-17 15:01:18 +00:00
|
|
|
{
|
2016-10-10 11:41:42 +00:00
|
|
|
if (client) {
|
|
|
|
wl_list_remove(&listener.link);
|
|
|
|
}
|
2014-11-17 15:01:18 +00:00
|
|
|
s_allClients.removeAt(s_allClients.indexOf(this));
|
|
|
|
}
|
|
|
|
|
2021-04-05 08:39:54 +00:00
|
|
|
void ClientConnectionPrivate::destroyListenerCallback(wl_listener *listener, void *data)
|
2014-11-17 15:01:18 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(listener)
|
|
|
|
wl_client *client = reinterpret_cast<wl_client*>(data);
|
|
|
|
auto it = std::find_if(s_allClients.constBegin(), s_allClients.constEnd(),
|
2021-04-05 08:39:54 +00:00
|
|
|
[client](ClientConnectionPrivate *c) {
|
2014-11-17 15:01:18 +00:00
|
|
|
return c->client == client;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Q_ASSERT(it != s_allClients.constEnd());
|
2016-10-10 11:41:42 +00:00
|
|
|
auto p = (*it);
|
|
|
|
auto q = p->q;
|
2021-05-12 19:36:12 +00:00
|
|
|
emit q->aboutToBeDestroyed();
|
2016-10-10 11:41:42 +00:00
|
|
|
p->client = nullptr;
|
|
|
|
wl_list_remove(&p->listener.link);
|
2014-11-17 15:01:18 +00:00
|
|
|
emit q->disconnected(q);
|
2016-10-10 11:41:42 +00:00
|
|
|
q->deleteLater();
|
2014-11-17 15:01:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ClientConnection::ClientConnection(wl_client *c, Display *parent)
|
|
|
|
: QObject(parent)
|
2021-04-05 08:39:54 +00:00
|
|
|
, d(new ClientConnectionPrivate(c, parent, this))
|
2014-11-17 15:01:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientConnection::~ClientConnection() = default;
|
|
|
|
|
2014-11-19 16:02:27 +00:00
|
|
|
void ClientConnection::flush()
|
|
|
|
{
|
2016-10-10 11:41:42 +00:00
|
|
|
if (!d->client) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-19 16:02:27 +00:00
|
|
|
wl_client_flush(d->client);
|
|
|
|
}
|
|
|
|
|
2015-11-10 12:27:27 +00:00
|
|
|
void ClientConnection::destroy()
|
|
|
|
{
|
2016-10-10 11:41:42 +00:00
|
|
|
if (!d->client) {
|
|
|
|
return;
|
|
|
|
}
|
2015-11-10 12:27:27 +00:00
|
|
|
wl_client_destroy(d->client);
|
|
|
|
}
|
|
|
|
|
2014-11-19 18:01:15 +00:00
|
|
|
wl_resource *ClientConnection::createResource(const wl_interface *interface, quint32 version, quint32 id)
|
|
|
|
{
|
2016-10-10 11:41:42 +00:00
|
|
|
if (!d->client) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-11-19 18:01:15 +00:00
|
|
|
return wl_resource_create(d->client, interface, version, id);
|
|
|
|
}
|
|
|
|
|
2015-04-02 12:12:16 +00:00
|
|
|
wl_resource *ClientConnection::getResource(quint32 id)
|
|
|
|
{
|
2016-10-10 11:41:42 +00:00
|
|
|
if (!d->client) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-02 12:12:16 +00:00
|
|
|
return wl_client_get_object(d->client, id);
|
|
|
|
}
|
|
|
|
|
2021-02-17 11:03:09 +00:00
|
|
|
wl_client *ClientConnection::client() const
|
2014-11-17 15:01:18 +00:00
|
|
|
{
|
|
|
|
return d->client;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientConnection::operator wl_client*()
|
|
|
|
{
|
|
|
|
return d->client;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientConnection::operator wl_client*() const
|
|
|
|
{
|
|
|
|
return d->client;
|
|
|
|
}
|
|
|
|
|
2021-02-17 11:03:09 +00:00
|
|
|
Display *ClientConnection::display() const
|
2014-11-17 15:01:18 +00:00
|
|
|
{
|
|
|
|
return d->display;
|
|
|
|
}
|
|
|
|
|
|
|
|
gid_t ClientConnection::groupId() const
|
|
|
|
{
|
|
|
|
return d->group;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t ClientConnection::processId() const
|
|
|
|
{
|
|
|
|
return d->pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
uid_t ClientConnection::userId() const
|
|
|
|
{
|
|
|
|
return d->user;
|
|
|
|
}
|
|
|
|
|
2016-01-25 09:52:03 +00:00
|
|
|
QString ClientConnection::executablePath() const
|
|
|
|
{
|
|
|
|
return d->executablePath;
|
|
|
|
}
|
|
|
|
|
2014-11-17 15:01:18 +00:00
|
|
|
}
|