wayland/drmlease: use FileDescriptor class

This commit is contained in:
Xaver Hugl 2022-07-27 13:19:10 +02:00
parent 1464028934
commit 1b1e0ab95f
4 changed files with 24 additions and 25 deletions

View file

@ -88,15 +88,14 @@ DrmGpu::DrmGpu(DrmBackend *backend, const QString &devNode, int fd, dev_t device
m_leaseDevice = new KWaylandServer::DrmLeaseDeviceV1Interface(waylandServer()->display(), [this] { m_leaseDevice = new KWaylandServer::DrmLeaseDeviceV1Interface(waylandServer()->display(), [this] {
char *path = drmGetDeviceNameFromFd2(m_fd); char *path = drmGetDeviceNameFromFd2(m_fd);
int fd = open(path, O_RDWR | O_CLOEXEC); FileDescriptor fd{open(path, O_RDWR | O_CLOEXEC)};
if (fd < 0) { if (!fd.isValid()) {
qCWarning(KWIN_DRM) << "Could not open DRM fd for leasing!" << strerror(errno); qCWarning(KWIN_DRM) << "Could not open DRM fd for leasing!" << strerror(errno);
} else { } else {
if (drmIsMaster(fd)) { if (drmIsMaster(fd.get())) {
if (drmDropMaster(fd) != 0) { if (drmDropMaster(fd.get()) != 0) {
close(fd);
qCWarning(KWIN_DRM) << "Could not create a non-master DRM fd for leasing!" << strerror(errno); qCWarning(KWIN_DRM) << "Could not create a non-master DRM fd for leasing!" << strerror(errno);
return -1; return FileDescriptor{};
} }
} }
} }
@ -625,8 +624,8 @@ void DrmGpu::handleLeaseRequest(KWaylandServer::DrmLeaseV1Interface *leaseReques
} }
uint32_t lesseeId; uint32_t lesseeId;
int fd = drmModeCreateLease(m_fd, objects.constData(), objects.count(), 0, &lesseeId); FileDescriptor fd{drmModeCreateLease(m_fd, objects.constData(), objects.count(), 0, &lesseeId)};
if (fd < 0) { if (!fd.isValid()) {
qCWarning(KWIN_DRM) << "Could not create DRM lease!" << strerror(errno); qCWarning(KWIN_DRM) << "Could not create DRM lease!" << strerror(errno);
qCWarning(KWIN_DRM, "Tried to lease the following %d resources:", objects.count()); qCWarning(KWIN_DRM, "Tried to lease the following %d resources:", objects.count());
for (const auto &res : qAsConst(objects)) { for (const auto &res : qAsConst(objects)) {
@ -634,11 +633,11 @@ void DrmGpu::handleLeaseRequest(KWaylandServer::DrmLeaseV1Interface *leaseReques
} }
leaseRequest->deny(); leaseRequest->deny();
} else { } else {
qCDebug(KWIN_DRM, "Created lease with leaseFd %d and lesseeId %d for %d resources:", fd, lesseeId, objects.count()); qCDebug(KWIN_DRM, "Created lease for %d resources:", objects.count());
for (const auto &res : qAsConst(objects)) { for (const auto &res : qAsConst(objects)) {
qCDebug(KWIN_DRM) << res; qCDebug(KWIN_DRM) << res;
} }
leaseRequest->grant(fd, lesseeId); leaseRequest->grant(std::move(fd), lesseeId);
for (const auto &output : qAsConst(outputs)) { for (const auto &output : qAsConst(outputs)) {
output->leased(leaseRequest); output->leased(leaseRequest);
} }

View file

@ -18,7 +18,7 @@ namespace KWaylandServer
static const quint32 s_version = 1; static const quint32 s_version = 1;
DrmLeaseDeviceV1Interface::DrmLeaseDeviceV1Interface(Display *display, std::function<int()> createNonMasterFd) DrmLeaseDeviceV1Interface::DrmLeaseDeviceV1Interface(Display *display, std::function<KWin::FileDescriptor()> createNonMasterFd)
: d(new DrmLeaseDeviceV1InterfacePrivate(display, this, createNonMasterFd)) : d(new DrmLeaseDeviceV1InterfacePrivate(display, this, createNonMasterFd))
{ {
} }
@ -36,9 +36,8 @@ void DrmLeaseDeviceV1Interface::setDrmMaster(bool hasDrmMaster)
if (hasDrmMaster) { if (hasDrmMaster) {
// send pending drm fds // send pending drm fds
while (!d->pendingFds.isEmpty()) { while (!d->pendingFds.isEmpty()) {
int fd = d->createNonMasterFd(); KWin::FileDescriptor fd = d->createNonMasterFd();
d->send_drm_fd(d->pendingFds.dequeue(), fd); d->send_drm_fd(d->pendingFds.dequeue(), fd.get());
close(fd);
} }
// offer all connectors again // offer all connectors again
for (const auto &connector : qAsConst(d->connectors)) { for (const auto &connector : qAsConst(d->connectors)) {
@ -72,7 +71,7 @@ void DrmLeaseDeviceV1Interface::done()
} }
} }
DrmLeaseDeviceV1InterfacePrivate::DrmLeaseDeviceV1InterfacePrivate(Display *display, DrmLeaseDeviceV1Interface *device, std::function<int()> createNonMasterFd) DrmLeaseDeviceV1InterfacePrivate::DrmLeaseDeviceV1InterfacePrivate(Display *display, DrmLeaseDeviceV1Interface *device, std::function<KWin::FileDescriptor()> createNonMasterFd)
: QtWaylandServer::wp_drm_lease_device_v1(*display, s_version) : QtWaylandServer::wp_drm_lease_device_v1(*display, s_version)
, q(device) , q(device)
, createNonMasterFd(createNonMasterFd) , createNonMasterFd(createNonMasterFd)
@ -159,9 +158,8 @@ void DrmLeaseDeviceV1InterfacePrivate::wp_drm_lease_device_v1_bind_resource(Reso
pendingFds << resource->handle; pendingFds << resource->handle;
return; return;
} }
int fd = createNonMasterFd(); KWin::FileDescriptor fd = createNonMasterFd();
send_drm_fd(resource->handle, fd); send_drm_fd(resource->handle, fd.get());
close(fd);
for (const auto &connector : qAsConst(connectors)) { for (const auto &connector : qAsConst(connectors)) {
auto connectorPrivate = DrmLeaseConnectorV1InterfacePrivate::get(connector); auto connectorPrivate = DrmLeaseConnectorV1InterfacePrivate::get(connector);
if (!connectorPrivate->withdrawn) { if (!connectorPrivate->withdrawn) {
@ -326,10 +324,10 @@ DrmLeaseV1Interface::~DrmLeaseV1Interface()
d->device->leases.removeOne(this); d->device->leases.removeOne(this);
} }
void DrmLeaseV1Interface::grant(int leaseFd, uint32_t lesseeId) void DrmLeaseV1Interface::grant(KWin::FileDescriptor &&leaseFd, uint32_t lesseeId)
{ {
d->send_lease_fd(leaseFd); KWin::FileDescriptor tmp = std::move(leaseFd);
close(leaseFd); d->send_lease_fd(tmp.get());
d->lesseeId = lesseeId; d->lesseeId = lesseeId;
for (const auto &connector : qAsConst(d->connectors)) { for (const auto &connector : qAsConst(d->connectors)) {
DrmLeaseConnectorV1InterfacePrivate::get(connector)->withdraw(); DrmLeaseConnectorV1InterfacePrivate::get(connector)->withdraw();

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include "kwin_export.h" #include "kwin_export.h"
#include "utils/filedescriptor.h"
#include <QObject> #include <QObject>
#include <QPointer> #include <QPointer>
@ -35,7 +36,7 @@ public:
* @param createNonMasterFd a function that creates non-master drm file descriptors for * @param createNonMasterFd a function that creates non-master drm file descriptors for
* this device that clients can use to enumerate connectors and their properties * this device that clients can use to enumerate connectors and their properties
*/ */
explicit DrmLeaseDeviceV1Interface(Display *display, std::function<int()> createNonMasterFd); explicit DrmLeaseDeviceV1Interface(Display *display, std::function<KWin::FileDescriptor()> createNonMasterFd);
~DrmLeaseDeviceV1Interface() override; ~DrmLeaseDeviceV1Interface() override;
/** /**
@ -98,7 +99,7 @@ public:
* drive the outputs corresponding to the requested connectors. * drive the outputs corresponding to the requested connectors.
* Must only be called once in response to DrmLeaseDeviceV1Interface::leaseRequested * Must only be called once in response to DrmLeaseDeviceV1Interface::leaseRequested
*/ */
void grant(int leaseFd, uint32_t lesseeId); void grant(KWin::FileDescriptor &&leaseFd, uint32_t lesseeId);
/** /**
* Deny the lease request. The compositor may call this in response to * Deny the lease request. The compositor may call this in response to

View file

@ -6,6 +6,7 @@
#pragma once #pragma once
#include "utils/filedescriptor.h"
#include <qwayland-server-drm-lease-v1.h> #include <qwayland-server-drm-lease-v1.h>
#include <QObject> #include <QObject>
@ -23,7 +24,7 @@ class DrmLeaseV1Interface;
class DrmLeaseDeviceV1InterfacePrivate : public QtWaylandServer::wp_drm_lease_device_v1 class DrmLeaseDeviceV1InterfacePrivate : public QtWaylandServer::wp_drm_lease_device_v1
{ {
public: public:
DrmLeaseDeviceV1InterfacePrivate(Display *display, DrmLeaseDeviceV1Interface *device, std::function<int()> createNonMasterFd); DrmLeaseDeviceV1InterfacePrivate(Display *display, DrmLeaseDeviceV1Interface *device, std::function<KWin::FileDescriptor()> createNonMasterFd);
~DrmLeaseDeviceV1InterfacePrivate(); ~DrmLeaseDeviceV1InterfacePrivate();
void remove(); void remove();
@ -37,7 +38,7 @@ public:
QVector<DrmLeaseRequestV1Interface *> leaseRequests; QVector<DrmLeaseRequestV1Interface *> leaseRequests;
QVector<DrmLeaseV1Interface *> leases; QVector<DrmLeaseV1Interface *> leases;
QQueue<wl_resource *> pendingFds; QQueue<wl_resource *> pendingFds;
std::function<int()> createNonMasterFd; std::function<KWin::FileDescriptor()> createNonMasterFd;
bool hasDrmMaster = true; bool hasDrmMaster = true;
bool removed = false; bool removed = false;