From 1b1e0ab95f058f5849862f018da779328d2041f3 Mon Sep 17 00:00:00 2001 From: Xaver Hugl Date: Wed, 27 Jul 2022 13:19:10 +0200 Subject: [PATCH] wayland/drmlease: use FileDescriptor class --- src/backends/drm/drm_gpu.cpp | 19 +++++++++---------- src/wayland/drmleasedevice_v1_interface.cpp | 20 +++++++++----------- src/wayland/drmleasedevice_v1_interface.h | 5 +++-- src/wayland/drmleasedevice_v1_interface_p.h | 5 +++-- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/backends/drm/drm_gpu.cpp b/src/backends/drm/drm_gpu.cpp index 55266271c7..a3aa7d2fac 100644 --- a/src/backends/drm/drm_gpu.cpp +++ b/src/backends/drm/drm_gpu.cpp @@ -88,15 +88,14 @@ DrmGpu::DrmGpu(DrmBackend *backend, const QString &devNode, int fd, dev_t device m_leaseDevice = new KWaylandServer::DrmLeaseDeviceV1Interface(waylandServer()->display(), [this] { char *path = drmGetDeviceNameFromFd2(m_fd); - int fd = open(path, O_RDWR | O_CLOEXEC); - if (fd < 0) { + FileDescriptor fd{open(path, O_RDWR | O_CLOEXEC)}; + if (!fd.isValid()) { qCWarning(KWIN_DRM) << "Could not open DRM fd for leasing!" << strerror(errno); } else { - if (drmIsMaster(fd)) { - if (drmDropMaster(fd) != 0) { - close(fd); + if (drmIsMaster(fd.get())) { + if (drmDropMaster(fd.get()) != 0) { 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; - int fd = drmModeCreateLease(m_fd, objects.constData(), objects.count(), 0, &lesseeId); - if (fd < 0) { + FileDescriptor fd{drmModeCreateLease(m_fd, objects.constData(), objects.count(), 0, &lesseeId)}; + if (!fd.isValid()) { qCWarning(KWIN_DRM) << "Could not create DRM lease!" << strerror(errno); qCWarning(KWIN_DRM, "Tried to lease the following %d resources:", objects.count()); for (const auto &res : qAsConst(objects)) { @@ -634,11 +633,11 @@ void DrmGpu::handleLeaseRequest(KWaylandServer::DrmLeaseV1Interface *leaseReques } leaseRequest->deny(); } 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)) { qCDebug(KWIN_DRM) << res; } - leaseRequest->grant(fd, lesseeId); + leaseRequest->grant(std::move(fd), lesseeId); for (const auto &output : qAsConst(outputs)) { output->leased(leaseRequest); } diff --git a/src/wayland/drmleasedevice_v1_interface.cpp b/src/wayland/drmleasedevice_v1_interface.cpp index af7c5859b6..55ea671c65 100644 --- a/src/wayland/drmleasedevice_v1_interface.cpp +++ b/src/wayland/drmleasedevice_v1_interface.cpp @@ -18,7 +18,7 @@ namespace KWaylandServer static const quint32 s_version = 1; -DrmLeaseDeviceV1Interface::DrmLeaseDeviceV1Interface(Display *display, std::function createNonMasterFd) +DrmLeaseDeviceV1Interface::DrmLeaseDeviceV1Interface(Display *display, std::function createNonMasterFd) : d(new DrmLeaseDeviceV1InterfacePrivate(display, this, createNonMasterFd)) { } @@ -36,9 +36,8 @@ void DrmLeaseDeviceV1Interface::setDrmMaster(bool hasDrmMaster) if (hasDrmMaster) { // send pending drm fds while (!d->pendingFds.isEmpty()) { - int fd = d->createNonMasterFd(); - d->send_drm_fd(d->pendingFds.dequeue(), fd); - close(fd); + KWin::FileDescriptor fd = d->createNonMasterFd(); + d->send_drm_fd(d->pendingFds.dequeue(), fd.get()); } // offer all connectors again for (const auto &connector : qAsConst(d->connectors)) { @@ -72,7 +71,7 @@ void DrmLeaseDeviceV1Interface::done() } } -DrmLeaseDeviceV1InterfacePrivate::DrmLeaseDeviceV1InterfacePrivate(Display *display, DrmLeaseDeviceV1Interface *device, std::function createNonMasterFd) +DrmLeaseDeviceV1InterfacePrivate::DrmLeaseDeviceV1InterfacePrivate(Display *display, DrmLeaseDeviceV1Interface *device, std::function createNonMasterFd) : QtWaylandServer::wp_drm_lease_device_v1(*display, s_version) , q(device) , createNonMasterFd(createNonMasterFd) @@ -159,9 +158,8 @@ void DrmLeaseDeviceV1InterfacePrivate::wp_drm_lease_device_v1_bind_resource(Reso pendingFds << resource->handle; return; } - int fd = createNonMasterFd(); - send_drm_fd(resource->handle, fd); - close(fd); + KWin::FileDescriptor fd = createNonMasterFd(); + send_drm_fd(resource->handle, fd.get()); for (const auto &connector : qAsConst(connectors)) { auto connectorPrivate = DrmLeaseConnectorV1InterfacePrivate::get(connector); if (!connectorPrivate->withdrawn) { @@ -326,10 +324,10 @@ DrmLeaseV1Interface::~DrmLeaseV1Interface() 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); - close(leaseFd); + KWin::FileDescriptor tmp = std::move(leaseFd); + d->send_lease_fd(tmp.get()); d->lesseeId = lesseeId; for (const auto &connector : qAsConst(d->connectors)) { DrmLeaseConnectorV1InterfacePrivate::get(connector)->withdraw(); diff --git a/src/wayland/drmleasedevice_v1_interface.h b/src/wayland/drmleasedevice_v1_interface.h index e7d1f2345b..ae2f1db8df 100644 --- a/src/wayland/drmleasedevice_v1_interface.h +++ b/src/wayland/drmleasedevice_v1_interface.h @@ -7,6 +7,7 @@ #pragma once #include "kwin_export.h" +#include "utils/filedescriptor.h" #include #include @@ -35,7 +36,7 @@ public: * @param createNonMasterFd a function that creates non-master drm file descriptors for * this device that clients can use to enumerate connectors and their properties */ - explicit DrmLeaseDeviceV1Interface(Display *display, std::function createNonMasterFd); + explicit DrmLeaseDeviceV1Interface(Display *display, std::function createNonMasterFd); ~DrmLeaseDeviceV1Interface() override; /** @@ -98,7 +99,7 @@ public: * drive the outputs corresponding to the requested connectors. * 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 diff --git a/src/wayland/drmleasedevice_v1_interface_p.h b/src/wayland/drmleasedevice_v1_interface_p.h index 6f499f771f..1b28e37217 100644 --- a/src/wayland/drmleasedevice_v1_interface_p.h +++ b/src/wayland/drmleasedevice_v1_interface_p.h @@ -6,6 +6,7 @@ #pragma once +#include "utils/filedescriptor.h" #include #include @@ -23,7 +24,7 @@ class DrmLeaseV1Interface; class DrmLeaseDeviceV1InterfacePrivate : public QtWaylandServer::wp_drm_lease_device_v1 { public: - DrmLeaseDeviceV1InterfacePrivate(Display *display, DrmLeaseDeviceV1Interface *device, std::function createNonMasterFd); + DrmLeaseDeviceV1InterfacePrivate(Display *display, DrmLeaseDeviceV1Interface *device, std::function createNonMasterFd); ~DrmLeaseDeviceV1InterfacePrivate(); void remove(); @@ -37,7 +38,7 @@ public: QVector leaseRequests; QVector leases; QQueue pendingFds; - std::function createNonMasterFd; + std::function createNonMasterFd; bool hasDrmMaster = true; bool removed = false;