core: begin using CFileDescriptor from hyprutils (#9122)

* config: make fd use CFileDescriptor

make use of the new hyprutils CFileDescriptor instead of manual FD
handling.

* hyprctl: make fd use CFileDescriptor

make use of the new hyprutils CFileDescriptor instead of manual FD
handling.

* ikeyboard: make fd use CFileDescriptor

make use of the new CFileDescriptor instead of manual FD handling, also
in sendKeymap remove dead code, it already early returns if keyboard
isnt valid, and dont try to close the FD that ikeyboard owns.

* core: make SHMFile functions use CFileDescriptor

make SHMFile misc functions use CFileDescriptor and its associated usage
in dmabuf and keyboard.

* core: make explicit sync use CFileDescriptor

begin using CFileDescriptor in explicit sync and its timelines and
eglsync usage in opengl, there is still a bit left with manual handling
that requires future aquamarine change aswell.

* eventmgr: make fd and sockets use CFileDescriptor

make use of the hyprutils CFileDescriptor instead of manual FD and
socket handling and closing.

* eventloopmgr: make timerfd use CFileDescriptor

make the timerfd use CFileDescriptor instead of manual fd handling

* opengl: make gbm fd use CFileDescriptor

make the gbm rendernode fd use CFileDescriptor instead of manual fd
handling

* core: make selection source/offer use CFileDescriptor

make data selection source and offers use CFileDescriptor and its
associated use in xwm and protocols

* protocols: convert protocols fd to CFileDescriptor

make most fd handling use CFileDescriptor in protocols

* shm: make SHMPool use CfileDescriptor

make SHMPool use CFileDescriptor instead of manual fd handling.

* opengl: duplicate fd with CFileDescriptor

duplicate fenceFD with CFileDescriptor duplicate instead.

* xwayland: make sockets and fds use CFileDescriptor

instead of manual opening/closing make sockets and fds use
CFileDescriptor

* keybindmgr: make sockets and fds use CFileDescriptor

make sockets and fds use CFileDescriptor instead of manual handling.
This commit is contained in:
Tom Englund
2025-01-30 12:30:12 +01:00
committed by GitHub
parent 7d1c78f4a3
commit 32c0fa2f2f
50 changed files with 422 additions and 526 deletions

View File

@@ -4,6 +4,7 @@
#include "managers/eventLoop/EventLoopManager.hpp"
#include <aquamarine/backend/DRM.hpp>
#include <fcntl.h>
using namespace Hyprutils::OS;
CDRMLeaseResource::CDRMLeaseResource(SP<CWpDrmLeaseV1> resource_, SP<CDRMLeaseRequestResource> request) : resource(resource_) {
if UNLIKELY (!good())
@@ -185,15 +186,14 @@ CDRMLeaseDeviceResource::CDRMLeaseDeviceResource(SP<CWpDrmLeaseDeviceV1> resourc
RESOURCE->parent = self;
});
int fd = ((Aquamarine::CDRMBackend*)PROTO::lease->primaryDevice->backend.get())->getNonMasterFD();
if (fd < 0) {
CFileDescriptor fd{((Aquamarine::CDRMBackend*)PROTO::lease->primaryDevice->backend.get())->getNonMasterFD()};
if (!fd.isValid()) {
LOGM(ERR, "Failed to dup fd in lease");
return;
}
LOGM(LOG, "Sending DRMFD {} to new lease device", fd);
resource->sendDrmFd(fd);
close(fd);
LOGM(LOG, "Sending DRMFD {} to new lease device", fd.get());
resource->sendDrmFd(fd.get());
for (auto const& m : PROTO::lease->primaryDevice->offeredOutputs) {
if (m)
@@ -231,16 +231,15 @@ void CDRMLeaseDeviceResource::sendConnector(PHLMONITOR monitor) {
}
CDRMLeaseDevice::CDRMLeaseDevice(SP<Aquamarine::CDRMBackend> drmBackend) : backend(drmBackend) {
auto drm = (Aquamarine::CDRMBackend*)drmBackend.get();
auto drm = (Aquamarine::CDRMBackend*)drmBackend.get();
auto fd = drm->getNonMasterFD();
CFileDescriptor fd{drm->getNonMasterFD()};
if (fd < 0) {
if (!fd.isValid()) {
LOGM(ERR, "Failed to dup fd for drm node {}", drm->gpuName);
return;
}
close(fd);
success = true;
name = drm->gpuName;
}

View File

@@ -5,6 +5,7 @@
#include "WaylandProtocol.hpp"
#include "drm-lease-v1.hpp"
#include "../helpers/signal/Signal.hpp"
#include <hyprutils/os/FileDescriptor.hpp>
/*
TODO: this protocol is not made for systems with multiple DRM nodes (e.g. multigpu)

View File

@@ -6,6 +6,7 @@
#include "../Compositor.hpp"
#include <fcntl.h>
using namespace Hyprutils::OS;
CDRMSyncobjSurfaceResource::CDRMSyncobjSurfaceResource(SP<CWpLinuxDrmSyncobjSurfaceV1> resource_, SP<CWLSurfaceResource> surface_) : surface(surface_), resource(resource_) {
if UNLIKELY (!good())
@@ -103,7 +104,7 @@ bool CDRMSyncobjSurfaceResource::good() {
return resource->resource();
}
CDRMSyncobjTimelineResource::CDRMSyncobjTimelineResource(SP<CWpLinuxDrmSyncobjTimelineV1> resource_, int fd_) : fd(fd_), resource(resource_) {
CDRMSyncobjTimelineResource::CDRMSyncobjTimelineResource(SP<CWpLinuxDrmSyncobjTimelineV1> resource_, CFileDescriptor fd_) : fd(std::move(fd_)), resource(resource_) {
if UNLIKELY (!good())
return;
@@ -112,7 +113,7 @@ CDRMSyncobjTimelineResource::CDRMSyncobjTimelineResource(SP<CWpLinuxDrmSyncobjTi
resource->setOnDestroy([this](CWpLinuxDrmSyncobjTimelineV1* r) { PROTO::sync->destroyResource(this); });
resource->setDestroy([this](CWpLinuxDrmSyncobjTimelineV1* r) { PROTO::sync->destroyResource(this); });
timeline = CSyncTimeline::create(PROTO::sync->drmFD, fd);
timeline = CSyncTimeline::create(PROTO::sync->drmFD, fd.get());
if (!timeline) {
resource->error(WP_LINUX_DRM_SYNCOBJ_MANAGER_V1_ERROR_INVALID_TIMELINE, "Timeline failed importing");
@@ -120,11 +121,6 @@ CDRMSyncobjTimelineResource::CDRMSyncobjTimelineResource(SP<CWpLinuxDrmSyncobjTi
}
}
CDRMSyncobjTimelineResource::~CDRMSyncobjTimelineResource() {
if (fd >= 0)
close(fd);
}
SP<CDRMSyncobjTimelineResource> CDRMSyncobjTimelineResource::fromResource(wl_resource* res) {
auto data = (CDRMSyncobjTimelineResource*)(((CWpLinuxDrmSyncobjTimelineV1*)wl_resource_get_user_data(res))->data());
return data ? data->self.lock() : nullptr;
@@ -171,7 +167,7 @@ CDRMSyncobjManagerResource::CDRMSyncobjManagerResource(SP<CWpLinuxDrmSyncobjMana
});
resource->setImportTimeline([this](CWpLinuxDrmSyncobjManagerV1* r, uint32_t id, int32_t fd) {
auto RESOURCE = makeShared<CDRMSyncobjTimelineResource>(makeShared<CWpLinuxDrmSyncobjTimelineV1>(resource->client(), resource->version(), id), fd);
auto RESOURCE = makeShared<CDRMSyncobjTimelineResource>(makeShared<CWpLinuxDrmSyncobjTimelineV1>(resource->client(), resource->version(), id), CFileDescriptor{fd});
if UNLIKELY (!RESOURCE->good()) {
resource->noMemory();
return;

View File

@@ -4,6 +4,7 @@
#include "WaylandProtocol.hpp"
#include "linux-drm-syncobj-v1.hpp"
#include "../helpers/signal/Signal.hpp"
#include <hyprutils/os/FileDescriptor.hpp>
class CWLSurfaceResource;
class CDRMSyncobjTimelineResource;
@@ -32,14 +33,14 @@ class CDRMSyncobjSurfaceResource {
class CDRMSyncobjTimelineResource {
public:
CDRMSyncobjTimelineResource(SP<CWpLinuxDrmSyncobjTimelineV1> resource_, int fd_);
~CDRMSyncobjTimelineResource();
CDRMSyncobjTimelineResource(SP<CWpLinuxDrmSyncobjTimelineV1> resource_, Hyprutils::OS::CFileDescriptor fd_);
~CDRMSyncobjTimelineResource() = default;
static SP<CDRMSyncobjTimelineResource> fromResource(wl_resource*);
bool good();
WP<CDRMSyncobjTimelineResource> self;
int fd = -1;
Hyprutils::OS::CFileDescriptor fd;
SP<CSyncTimeline> timeline;
private:

View File

@@ -2,6 +2,7 @@
#include <algorithm>
#include "../managers/SeatManager.hpp"
#include "core/Seat.hpp"
using namespace Hyprutils::OS;
CWLRDataOffer::CWLRDataOffer(SP<CZwlrDataControlOfferV1> resource_, SP<IDataSource> source_) : source(source_), resource(resource_) {
if UNLIKELY (!good())
@@ -11,21 +12,20 @@ CWLRDataOffer::CWLRDataOffer(SP<CZwlrDataControlOfferV1> resource_, SP<IDataSour
resource->setOnDestroy([this](CZwlrDataControlOfferV1* r) { PROTO::dataWlr->destroyResource(this); });
resource->setReceive([this](CZwlrDataControlOfferV1* r, const char* mime, int32_t fd) {
CFileDescriptor sendFd{fd};
if (!source) {
LOGM(WARN, "Possible bug: Receive on an offer w/o a source");
close(fd);
return;
}
if (dead) {
LOGM(WARN, "Possible bug: Receive on an offer that's dead");
close(fd);
return;
}
LOGM(LOG, "Offer {:x} asks to send data from source {:x}", (uintptr_t)this, (uintptr_t)source.get());
source->send(mime, fd);
source->send(mime, std::move(sendFd));
});
}
@@ -77,15 +77,13 @@ std::vector<std::string> CWLRDataSource::mimes() {
return mimeTypes;
}
void CWLRDataSource::send(const std::string& mime, uint32_t fd) {
void CWLRDataSource::send(const std::string& mime, CFileDescriptor fd) {
if (std::find(mimeTypes.begin(), mimeTypes.end(), mime) == mimeTypes.end()) {
LOGM(ERR, "Compositor/App bug: CWLRDataSource::sendAskSend with non-existent mime");
close(fd);
return;
}
resource->sendSend(mime.c_str(), fd);
close(fd);
resource->sendSend(mime.c_str(), fd.get());
}
void CWLRDataSource::accepted(const std::string& mime) {

View File

@@ -5,6 +5,7 @@
#include "WaylandProtocol.hpp"
#include "wlr-data-control-unstable-v1.hpp"
#include "types/DataDevice.hpp"
#include <hyprutils/os/FileDescriptor.hpp>
class CWLRDataControlManagerResource;
class CWLRDataSource;
@@ -38,7 +39,7 @@ class CWLRDataSource : public IDataSource {
bool good();
virtual std::vector<std::string> mimes();
virtual void send(const std::string& mime, uint32_t fd);
virtual void send(const std::string& mime, Hyprutils::OS::CFileDescriptor fd);
virtual void accepted(const std::string& mime);
virtual void cancelled();
virtual void error(uint32_t code, const std::string& msg);

View File

@@ -4,6 +4,7 @@
#include "../helpers/Monitor.hpp"
#include "../protocols/core/Output.hpp"
#include "../render/Renderer.hpp"
using namespace Hyprutils::OS;
CGammaControl::CGammaControl(SP<CZwlrGammaControlV1> resource_, wl_resource* output) : resource(resource_) {
if UNLIKELY (!resource_->resource())
@@ -46,34 +47,33 @@ CGammaControl::CGammaControl(SP<CZwlrGammaControlV1> resource_, wl_resource* out
resource->setOnDestroy([this](CZwlrGammaControlV1* gamma) { PROTO::gamma->destroyGammaControl(this); });
resource->setSetGamma([this](CZwlrGammaControlV1* gamma, int32_t fd) {
CFileDescriptor gammaFd{fd};
if UNLIKELY (!pMonitor) {
LOGM(ERR, "setGamma for a dead monitor");
resource->sendFailed();
close(fd);
return;
}
LOGM(LOG, "setGamma for {}", pMonitor->szName);
int fdFlags = fcntl(fd, F_GETFL, 0);
// TODO: make CFileDescriptor getflags use F_GETFL
int fdFlags = fcntl(gammaFd.get(), F_GETFL, 0);
if UNLIKELY (fdFlags < 0) {
LOGM(ERR, "Failed to get fd flags");
resource->sendFailed();
close(fd);
return;
}
if UNLIKELY (fcntl(fd, F_SETFL, fdFlags | O_NONBLOCK) < 0) {
// TODO: make CFileDescriptor setflags use F_SETFL
if UNLIKELY (fcntl(gammaFd.get(), F_SETFL, fdFlags | O_NONBLOCK) < 0) {
LOGM(ERR, "Failed to set fd flags");
resource->sendFailed();
close(fd);
return;
}
ssize_t readBytes = pread(fd, gammaTable.data(), gammaTable.size() * sizeof(uint16_t), 0);
ssize_t readBytes = pread(gammaFd.get(), gammaTable.data(), gammaTable.size() * sizeof(uint16_t), 0);
if (readBytes < 0 || (size_t)readBytes != gammaTable.size() * sizeof(uint16_t)) {
LOGM(ERR, "Failed to read bytes");
close(fd);
if ((size_t)readBytes != gammaTable.size() * sizeof(uint16_t)) {
gamma->error(ZWLR_GAMMA_CONTROL_V1_ERROR_INVALID_GAMMA, "Gamma ramps size mismatch");
@@ -85,7 +85,6 @@ CGammaControl::CGammaControl(SP<CZwlrGammaControlV1> resource_, wl_resource* out
}
gammaTableSet = true;
close(fd);
// translate the table to AQ format
std::vector<uint16_t> red, green, blue;

View File

@@ -33,25 +33,22 @@ void CInputMethodKeyboardGrabV2::sendKeyboardData(SP<IKeyboard> keyboard) {
pLastKeyboard = keyboard;
int keymapFD = allocateSHMFile(keyboard->xkbKeymapString.length() + 1);
if UNLIKELY (keymapFD < 0) {
auto keymapFD = allocateSHMFile(keyboard->xkbKeymapString.length() + 1);
if UNLIKELY (!keymapFD.isValid()) {
LOGM(ERR, "Failed to create a keymap file for keyboard grab");
return;
}
void* data = mmap(nullptr, keyboard->xkbKeymapString.length() + 1, PROT_READ | PROT_WRITE, MAP_SHARED, keymapFD, 0);
void* data = mmap(nullptr, keyboard->xkbKeymapString.length() + 1, PROT_READ | PROT_WRITE, MAP_SHARED, keymapFD.get(), 0);
if UNLIKELY (data == MAP_FAILED) {
LOGM(ERR, "Failed to mmap a keymap file for keyboard grab");
close(keymapFD);
return;
}
memcpy(data, keyboard->xkbKeymapString.c_str(), keyboard->xkbKeymapString.length());
munmap(data, keyboard->xkbKeymapString.length() + 1);
resource->sendKeymap(WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, keymapFD, keyboard->xkbKeymapString.length() + 1);
close(keymapFD);
resource->sendKeymap(WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, keymapFD.get(), keyboard->xkbKeymapString.length() + 1);
sendMods(keyboard->modifiersState.depressed, keyboard->modifiersState.latched, keyboard->modifiersState.locked, keyboard->modifiersState.group);

View File

@@ -14,6 +14,8 @@
#include "../render/OpenGL.hpp"
#include "../Compositor.hpp"
using namespace Hyprutils::OS;
static std::optional<dev_t> devIDFromFD(int fd) {
struct stat stat;
if (fstat(fd, &stat) != 0)
@@ -77,29 +79,21 @@ CDMABUFFormatTable::CDMABUFFormatTable(SDMABUFTranche _rendererTranche, std::vec
tableSize = formatsVec.size() * sizeof(SDMABUFFormatTableEntry);
int fds[2] = {0};
allocateSHMFilePair(tableSize, &fds[0], &fds[1]);
CFileDescriptor fds[2];
allocateSHMFilePair(tableSize, fds[0], fds[1]);
auto arr = (SDMABUFFormatTableEntry*)mmap(nullptr, tableSize, PROT_READ | PROT_WRITE, MAP_SHARED, fds[0], 0);
auto arr = (SDMABUFFormatTableEntry*)mmap(nullptr, tableSize, PROT_READ | PROT_WRITE, MAP_SHARED, fds[0].get(), 0);
if (arr == MAP_FAILED) {
LOGM(ERR, "mmap failed");
close(fds[0]);
close(fds[1]);
return;
}
close(fds[0]);
std::copy(formatsVec.begin(), formatsVec.end(), arr);
munmap(arr, tableSize);
tableFD = fds[1];
}
CDMABUFFormatTable::~CDMABUFFormatTable() {
close(tableFD);
tableFD = std::move(fds[1]);
}
CLinuxDMABuffer::CLinuxDMABuffer(uint32_t id, wl_client* client, Aquamarine::SDMABUFAttrs attrs) {
@@ -234,18 +228,18 @@ void CLinuxDMABUFParamsResource::create(uint32_t id) {
}
bool CLinuxDMABUFParamsResource::commence() {
if (PROTO::linuxDma->mainDeviceFD < 0)
if (!PROTO::linuxDma->mainDeviceFD.isValid())
return true;
for (int i = 0; i < attrs->planes; i++) {
uint32_t handle = 0;
if (drmPrimeFDToHandle(PROTO::linuxDma->mainDeviceFD, attrs->fds.at(i), &handle)) {
if (drmPrimeFDToHandle(PROTO::linuxDma->mainDeviceFD.get(), attrs->fds.at(i), &handle)) {
LOGM(ERR, "Failed to import dmabuf fd");
return false;
}
if (drmCloseBufferHandle(PROTO::linuxDma->mainDeviceFD, handle)) {
if (drmCloseBufferHandle(PROTO::linuxDma->mainDeviceFD.get(), handle)) {
LOGM(ERR, "Failed to close dmabuf handle");
return false;
}
@@ -303,7 +297,7 @@ CLinuxDMABUFFeedbackResource::CLinuxDMABUFFeedbackResource(SP<CZwpLinuxDmabufFee
resource->setDestroy([this](CZwpLinuxDmabufFeedbackV1* r) { PROTO::linuxDma->destroyResource(this); });
auto& formatTable = PROTO::linuxDma->formatTable;
resource->sendFormatTable(formatTable->tableFD, formatTable->tableSize);
resource->sendFormatTable(formatTable->tableFD.get(), formatTable->tableSize);
sendDefaultFeedback();
}
@@ -472,9 +466,9 @@ CLinuxDMABufV1Protocol::CLinuxDMABufV1Protocol(const wl_interface* iface, const
if (device->available_nodes & (1 << DRM_NODE_RENDER)) {
const char* name = device->nodes[DRM_NODE_RENDER];
mainDeviceFD = open(name, O_RDWR | O_CLOEXEC);
mainDeviceFD = CFileDescriptor{open(name, O_RDWR | O_CLOEXEC)};
drmFreeDevice(&device);
if (mainDeviceFD < 0) {
if (!mainDeviceFD.isValid()) {
LOGM(ERR, "failed to open drm dev, disabling linux dmabuf");
removeGlobal();
return;
@@ -496,7 +490,7 @@ void CLinuxDMABufV1Protocol::resetFormatTable() {
auto newFormatTable = makeUnique<CDMABUFFormatTable>(formatTable->rendererTranche, formatTable->monitorTranches);
for (auto const& feedback : m_vFeedbacks) {
feedback->resource->sendFormatTable(newFormatTable->tableFD, newFormatTable->tableSize);
feedback->resource->sendFormatTable(newFormatTable->tableFD.get(), newFormatTable->tableSize);
if (feedback->lastFeedbackWasScanout) {
PHLMONITOR mon;
auto HLSurface = CWLSurface::fromResource(feedback->surface);
@@ -519,11 +513,6 @@ void CLinuxDMABufV1Protocol::resetFormatTable() {
formatTable = std::move(newFormatTable);
}
CLinuxDMABufV1Protocol::~CLinuxDMABufV1Protocol() {
if (mainDeviceFD >= 0)
close(mainDeviceFD);
}
void CLinuxDMABufV1Protocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
const auto RESOURCE = m_vManagers.emplace_back(makeShared<CLinuxDMABUFResource>(makeShared<CZwpLinuxDmabufV1>(client, ver, id)));

View File

@@ -9,6 +9,7 @@
#include "../helpers/Format.hpp"
#include "../helpers/Monitor.hpp"
#include <aquamarine/buffer/Buffer.hpp>
#include <hyprutils/os/FileDescriptor.hpp>
class CDMABuffer;
class CWLSurfaceResource;
@@ -48,9 +49,9 @@ struct SDMABUFTranche {
class CDMABUFFormatTable {
public:
CDMABUFFormatTable(SDMABUFTranche rendererTranche, std::vector<std::pair<PHLMONITORREF, SDMABUFTranche>> tranches);
~CDMABUFFormatTable();
~CDMABUFFormatTable() = default;
int tableFD = -1;
Hyprutils::OS::CFileDescriptor tableFD;
size_t tableSize = 0;
SDMABUFTranche rendererTranche;
std::vector<std::pair<PHLMONITORREF, SDMABUFTranche>> monitorTranches;
@@ -107,7 +108,7 @@ class CLinuxDMABUFResource {
class CLinuxDMABufV1Protocol : public IWaylandProtocol {
public:
CLinuxDMABufV1Protocol(const wl_interface* iface, const int& ver, const std::string& name);
~CLinuxDMABufV1Protocol();
~CLinuxDMABufV1Protocol() = default;
virtual void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id);
void updateScanoutTranche(SP<CWLSurfaceResource> surface, PHLMONITOR pMonitor);
@@ -128,7 +129,7 @@ class CLinuxDMABufV1Protocol : public IWaylandProtocol {
UP<CDMABUFFormatTable> formatTable;
dev_t mainDevice;
int mainDeviceFD = -1;
Hyprutils::OS::CFileDescriptor mainDeviceFD;
friend class CLinuxDMABUFResource;
friend class CLinuxDMABUFFeedbackResource;

View File

@@ -3,6 +3,7 @@
#include "../managers/SeatManager.hpp"
#include "core/Seat.hpp"
#include "../config/ConfigValue.hpp"
using namespace Hyprutils::OS;
CPrimarySelectionOffer::CPrimarySelectionOffer(SP<CZwpPrimarySelectionOfferV1> resource_, SP<IDataSource> source_) : source(source_), resource(resource_) {
if UNLIKELY (!good())
@@ -12,21 +13,20 @@ CPrimarySelectionOffer::CPrimarySelectionOffer(SP<CZwpPrimarySelectionOfferV1> r
resource->setOnDestroy([this](CZwpPrimarySelectionOfferV1* r) { PROTO::primarySelection->destroyResource(this); });
resource->setReceive([this](CZwpPrimarySelectionOfferV1* r, const char* mime, int32_t fd) {
CFileDescriptor sendFd{fd};
if (!source) {
LOGM(WARN, "Possible bug: Receive on an offer w/o a source");
close(fd);
return;
}
if (dead) {
LOGM(WARN, "Possible bug: Receive on an offer that's dead");
close(fd);
return;
}
LOGM(LOG, "Offer {:x} asks to send data from source {:x}", (uintptr_t)this, (uintptr_t)source.get());
source->send(mime, fd);
source->send(mime, std::move(sendFd));
});
}
@@ -78,15 +78,13 @@ std::vector<std::string> CPrimarySelectionSource::mimes() {
return mimeTypes;
}
void CPrimarySelectionSource::send(const std::string& mime, uint32_t fd) {
void CPrimarySelectionSource::send(const std::string& mime, CFileDescriptor fd) {
if (std::find(mimeTypes.begin(), mimeTypes.end(), mime) == mimeTypes.end()) {
LOGM(ERR, "Compositor/App bug: CPrimarySelectionSource::sendAskSend with non-existent mime");
close(fd);
return;
}
resource->sendSend(mime.c_str(), fd);
close(fd);
resource->sendSend(mime.c_str(), fd.get());
}
void CPrimarySelectionSource::accepted(const std::string& mime) {

View File

@@ -5,6 +5,7 @@
#include "WaylandProtocol.hpp"
#include "primary-selection-unstable-v1.hpp"
#include "types/DataDevice.hpp"
#include <hyprutils/os/FileDescriptor.hpp>
class CPrimarySelectionOffer;
class CPrimarySelectionSource;
@@ -38,7 +39,7 @@ class CPrimarySelectionSource : public IDataSource {
bool good();
virtual std::vector<std::string> mimes();
virtual void send(const std::string& mime, uint32_t fd);
virtual void send(const std::string& mime, Hyprutils::OS::CFileDescriptor fd);
virtual void accepted(const std::string& mime);
virtual void cancelled();
virtual void error(uint32_t code, const std::string& msg);

View File

@@ -1,6 +1,7 @@
#include "SecurityContext.hpp"
#include "../Compositor.hpp"
#include <sys/socket.h>
using namespace Hyprutils::OS;
static int onListenFdEvent(int fd, uint32_t mask, void* data) {
auto sc = (CSecurityContext*)data;
@@ -14,8 +15,8 @@ static int onCloseFdEvent(int fd, uint32_t mask, void* data) {
return 0;
}
SP<CSecurityContextSandboxedClient> CSecurityContextSandboxedClient::create(int clientFD_) {
auto p = SP<CSecurityContextSandboxedClient>(new CSecurityContextSandboxedClient(clientFD_));
SP<CSecurityContextSandboxedClient> CSecurityContextSandboxedClient::create(CFileDescriptor clientFD_) {
auto p = SP<CSecurityContextSandboxedClient>(new CSecurityContextSandboxedClient(std::move(clientFD_)));
if (!p->client)
return nullptr;
return p;
@@ -27,8 +28,8 @@ static void onSecurityContextClientDestroy(wl_listener* l, void* d) {
client->onDestroy();
}
CSecurityContextSandboxedClient::CSecurityContextSandboxedClient(int clientFD_) : clientFD(clientFD_) {
client = wl_client_create(g_pCompositor->m_sWLDisplay, clientFD);
CSecurityContextSandboxedClient::CSecurityContextSandboxedClient(CFileDescriptor clientFD_) : clientFD(std::move(clientFD_)) {
client = wl_client_create(g_pCompositor->m_sWLDisplay, clientFD.get());
if (!client)
return;
@@ -41,7 +42,6 @@ CSecurityContextSandboxedClient::CSecurityContextSandboxedClient(int clientFD_)
CSecurityContextSandboxedClient::~CSecurityContextSandboxedClient() {
wl_list_remove(&destroyListener.listener.link);
wl_list_init(&destroyListener.listener.link);
close(clientFD);
}
void CSecurityContextSandboxedClient::onDestroy() {
@@ -113,8 +113,8 @@ CSecurityContext::CSecurityContext(SP<CWpSecurityContextV1> resource_, int liste
LOGM(LOG, "security_context at 0x{:x} commits", (uintptr_t)this);
listenSource = wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, listenFD, WL_EVENT_READABLE, ::onListenFdEvent, this);
closeSource = wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, closeFD, 0, ::onCloseFdEvent, this);
listenSource = wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, listenFD.get(), WL_EVENT_READABLE, ::onListenFdEvent, this);
closeSource = wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, closeFD.get(), 0, ::onCloseFdEvent, this);
if (!listenSource || !closeSource) {
r->noMemory();
@@ -144,16 +144,15 @@ void CSecurityContext::onListen(uint32_t mask) {
if (!(mask & WL_EVENT_READABLE))
return;
int clientFD = accept(listenFD, nullptr, nullptr);
if UNLIKELY (clientFD < 0) {
CFileDescriptor clientFD{accept(listenFD.get(), nullptr, nullptr)};
if UNLIKELY (!clientFD.isValid()) {
LOGM(ERR, "security_context at 0x{:x} couldn't accept", (uintptr_t)this);
return;
}
auto newClient = CSecurityContextSandboxedClient::create(clientFD);
auto newClient = CSecurityContextSandboxedClient::create(std::move(clientFD));
if UNLIKELY (!newClient) {
LOGM(ERR, "security_context at 0x{:x} couldn't create a client", (uintptr_t)this);
close(clientFD);
return;
}

View File

@@ -4,19 +4,20 @@
#include <cstdint>
#include "WaylandProtocol.hpp"
#include "security-context-v1.hpp"
#include <hyprutils/os/FileDescriptor.hpp>
class CSecurityContext {
public:
CSecurityContext(SP<CWpSecurityContextV1> resource_, int listenFD_, int closeFD_);
~CSecurityContext();
bool good();
bool good();
std::string sandboxEngine, appID, instanceID;
int listenFD = -1, closeFD = -1;
std::string sandboxEngine, appID, instanceID;
Hyprutils::OS::CFileDescriptor listenFD, closeFD;
void onListen(uint32_t mask);
void onClose(uint32_t mask);
void onListen(uint32_t mask);
void onClose(uint32_t mask);
private:
SP<CWpSecurityContextV1> resource;
@@ -44,7 +45,7 @@ struct SCSecurityContextSandboxedClientDestroyWrapper {
class CSecurityContextSandboxedClient {
public:
static SP<CSecurityContextSandboxedClient> create(int clientFD);
static SP<CSecurityContextSandboxedClient> create(Hyprutils::OS::CFileDescriptor clientFD);
~CSecurityContextSandboxedClient();
void onDestroy();
@@ -52,10 +53,10 @@ class CSecurityContextSandboxedClient {
SCSecurityContextSandboxedClientDestroyWrapper destroyListener;
private:
CSecurityContextSandboxedClient(int clientFD_);
CSecurityContextSandboxedClient(Hyprutils::OS::CFileDescriptor clientFD_);
wl_client* client = nullptr;
int clientFD = -1;
wl_client* client = nullptr;
Hyprutils::OS::CFileDescriptor clientFD;
friend class CSecurityContextProtocol;
friend class CSecurityContext;

View File

@@ -1,6 +1,7 @@
#include "VirtualKeyboard.hpp"
#include <sys/mman.h>
#include "../devices/IKeyboard.hpp"
using namespace Hyprutils::OS;
CVirtualKeyboardV1Resource::CVirtualKeyboardV1Resource(SP<CZwpVirtualKeyboardV1> resource_) : resource(resource_) {
if UNLIKELY (!good())
@@ -51,20 +52,19 @@ CVirtualKeyboardV1Resource::CVirtualKeyboardV1Resource(SP<CZwpVirtualKeyboardV1>
});
resource->setKeymap([this](CZwpVirtualKeyboardV1* r, uint32_t fmt, int32_t fd, uint32_t len) {
auto xkbContext = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
auto xkbContext = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
CFileDescriptor keymapFd{fd};
if UNLIKELY (!xkbContext) {
LOGM(ERR, "xkbContext creation failed");
r->noMemory();
close(fd);
return;
}
auto keymapData = mmap(nullptr, len, PROT_READ, MAP_PRIVATE, fd, 0);
auto keymapData = mmap(nullptr, len, PROT_READ, MAP_PRIVATE, keymapFd.get(), 0);
if UNLIKELY (keymapData == MAP_FAILED) {
LOGM(ERR, "keymapData alloc failed");
xkb_context_unref(xkbContext);
r->noMemory();
close(fd);
return;
}
@@ -75,7 +75,6 @@ CVirtualKeyboardV1Resource::CVirtualKeyboardV1Resource(SP<CZwpVirtualKeyboardV1>
LOGM(ERR, "xkbKeymap creation failed");
xkb_context_unref(xkbContext);
r->noMemory();
close(fd);
return;
}
@@ -86,7 +85,6 @@ CVirtualKeyboardV1Resource::CVirtualKeyboardV1Resource(SP<CZwpVirtualKeyboardV1>
xkb_keymap_unref(xkbKeymap);
xkb_context_unref(xkbContext);
close(fd);
});
name = "hl-virtual-keyboard";

View File

@@ -5,6 +5,7 @@
#include "WaylandProtocol.hpp"
#include "virtual-keyboard-unstable-v1.hpp"
#include "../helpers/signal/Signal.hpp"
#include <hyprutils/os/FileDescriptor.hpp>
class CVirtualKeyboardV1Resource {
public:

View File

@@ -13,6 +13,7 @@
#include "../../managers/HookSystemManager.hpp"
#include "../../helpers/Monitor.hpp"
#include "../../render/Renderer.hpp"
using namespace Hyprutils::OS;
CWLDataOfferResource::CWLDataOfferResource(SP<CWlDataOffer> resource_, SP<IDataSource> source_) : source(source_), resource(resource_) {
if UNLIKELY (!good())
@@ -39,15 +40,14 @@ CWLDataOfferResource::CWLDataOfferResource(SP<CWlDataOffer> resource_, SP<IDataS
});
resource->setReceive([this](CWlDataOffer* r, const char* mime, uint32_t fd) {
CFileDescriptor sendFd{fd};
if (!source) {
LOGM(WARN, "Possible bug: Receive on an offer w/o a source");
close(fd);
return;
}
if (dead) {
LOGM(WARN, "Possible bug: Receive on an offer that's dead");
close(fd);
return;
}
@@ -58,7 +58,7 @@ CWLDataOfferResource::CWLDataOfferResource(SP<CWlDataOffer> resource_, SP<IDataS
source->accepted(mime ? mime : "");
}
source->send(mime ? mime : "", fd);
source->send(mime ? mime : "", std::move(sendFd));
recvd = true;
@@ -182,15 +182,13 @@ std::vector<std::string> CWLDataSourceResource::mimes() {
return mimeTypes;
}
void CWLDataSourceResource::send(const std::string& mime, uint32_t fd) {
void CWLDataSourceResource::send(const std::string& mime, CFileDescriptor fd) {
if (std::find(mimeTypes.begin(), mimeTypes.end(), mime) == mimeTypes.end()) {
LOGM(ERR, "Compositor/App bug: CWLDataSourceResource::sendAskSend with non-existent mime");
close(fd);
return;
}
resource->sendSend(mime.c_str(), fd);
close(fd);
resource->sendSend(mime.c_str(), fd.get());
}
void CWLDataSourceResource::cancelled() {

View File

@@ -16,6 +16,7 @@
#include "../../helpers/signal/Signal.hpp"
#include "../../helpers/math/Math.hpp"
#include "../types/DataDevice.hpp"
#include <hyprutils/os/FileDescriptor.hpp>
class CWLDataDeviceResource;
class CWLDataDeviceManagerResource;
@@ -63,7 +64,7 @@ class CWLDataSourceResource : public IDataSource {
bool good();
virtual std::vector<std::string> mimes();
virtual void send(const std::string& mime, uint32_t fd);
virtual void send(const std::string& mime, Hyprutils::OS::CFileDescriptor fd);
virtual void accepted(const std::string& mime);
virtual void cancelled();
virtual bool hasDnd();

View File

@@ -321,35 +321,18 @@ void CWLKeyboardResource::sendKeymap(SP<IKeyboard> keyboard) {
if (!(PROTO::seat->currentCaps & eHIDCapabilityType::HID_INPUT_CAPABILITY_KEYBOARD))
return;
std::string_view keymap;
int fd;
uint32_t size;
if (keyboard) {
keymap = keyboard->xkbKeymapString;
fd = keyboard->xkbKeymapFD;
size = keyboard->xkbKeymapString.length() + 1;
} else {
fd = open("/dev/null", O_RDONLY | O_CLOEXEC);
if (fd < 0) {
LOGM(ERR, "Failed to open /dev/null");
return;
}
size = 0;
}
std::string_view keymap = keyboard->xkbKeymapString;
Hyprutils::OS::CFileDescriptor& fd = keyboard->xkbKeymapFD;
uint32_t size = keyboard->xkbKeymapString.length() + 1;
if (keymap == lastKeymap) {
if (!keyboard)
close(fd);
if (keymap == lastKeymap)
return;
}
lastKeymap = keymap;
const wl_keyboard_keymap_format format = keyboard ? WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1 : WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP;
resource->sendKeymap(format, fd, size);
if (!keyboard)
close(fd);
resource->sendKeymap(format, fd.get(), size);
}
void CWLKeyboardResource::sendEnter(SP<CWLSurfaceResource> surface) {

View File

@@ -7,6 +7,7 @@
#include "../types/WLBuffer.hpp"
#include "../../helpers/Format.hpp"
#include "../../render/Renderer.hpp"
using namespace Hyprutils::OS;
CWLSHMBuffer::CWLSHMBuffer(SP<CWLSHMPoolResource> pool_, uint32_t id, int32_t offset_, const Vector2D& size_, int32_t stride_, uint32_t fmt_) {
if UNLIKELY (!pool_->pool->data)
@@ -51,7 +52,7 @@ bool CWLSHMBuffer::isSynchronous() {
Aquamarine::SSHMAttrs CWLSHMBuffer::shm() {
Aquamarine::SSHMAttrs attrs;
attrs.success = true;
attrs.fd = pool->fd;
attrs.fd = pool->fd.get();
attrs.format = NFormatUtils::shmToDRM(fmt);
attrs.size = size;
attrs.stride = stride;
@@ -75,13 +76,12 @@ void CWLSHMBuffer::update(const CRegion& damage) {
texture->update(NFormatUtils::shmToDRM(fmt), (uint8_t*)pool->data + offset, stride, damage);
}
CSHMPool::CSHMPool(int fd_, size_t size_) : fd(fd_), size(size_), data(mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) {
CSHMPool::CSHMPool(CFileDescriptor fd_, size_t size_) : fd(std::move(fd_)), size(size_), data(mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0)) {
;
}
CSHMPool::~CSHMPool() {
munmap(data, size);
close(fd);
}
void CSHMPool::resize(size_t size_) {
@@ -90,23 +90,23 @@ void CSHMPool::resize(size_t size_) {
if (data)
munmap(data, size);
size = size_;
data = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
data = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0);
if UNLIKELY (data == MAP_FAILED)
LOGM(ERR, "Couldn't mmap {} bytes from fd {} of shm client", size, fd);
LOGM(ERR, "Couldn't mmap {} bytes from fd {} of shm client", size, fd.get());
}
static int shmIsSizeValid(int fd, size_t size) {
static int shmIsSizeValid(CFileDescriptor& fd, size_t size) {
struct stat st;
if UNLIKELY (fstat(fd, &st) == -1) {
LOGM(ERR, "Couldn't get stat for fd {} of shm client", fd);
if UNLIKELY (fstat(fd.get(), &st) == -1) {
LOGM(ERR, "Couldn't get stat for fd {} of shm client", fd.get());
return 0;
}
return (size_t)st.st_size >= size;
}
CWLSHMPoolResource::CWLSHMPoolResource(SP<CWlShmPool> resource_, int fd_, size_t size_) : resource(resource_) {
CWLSHMPoolResource::CWLSHMPoolResource(SP<CWlShmPool> resource_, CFileDescriptor fd_, size_t size_) : resource(resource_) {
if UNLIKELY (!good())
return;
@@ -115,7 +115,7 @@ CWLSHMPoolResource::CWLSHMPoolResource(SP<CWlShmPool> resource_, int fd_, size_t
return;
}
pool = makeShared<CSHMPool>(fd_, size_);
pool = makeShared<CSHMPool>(std::move(fd_), size_);
resource->setDestroy([this](CWlShmPool* r) { PROTO::shm->destroyResource(this); });
resource->setOnDestroy([this](CWlShmPool* r) { PROTO::shm->destroyResource(this); });
@@ -176,7 +176,8 @@ CWLSHMResource::CWLSHMResource(SP<CWlShm> resource_) : resource(resource_) {
resource->setOnDestroy([this](CWlShm* r) { PROTO::shm->destroyResource(this); });
resource->setCreatePool([](CWlShm* r, uint32_t id, int32_t fd, int32_t size) {
const auto RESOURCE = PROTO::shm->m_vPools.emplace_back(makeShared<CWLSHMPoolResource>(makeShared<CWlShmPool>(r->client(), r->version(), id), fd, size));
CFileDescriptor poolFd{fd};
const auto RESOURCE = PROTO::shm->m_vPools.emplace_back(makeShared<CWLSHMPoolResource>(makeShared<CWlShmPool>(r->client(), r->version(), id), std::move(poolFd), size));
if UNLIKELY (!RESOURCE->good()) {
r->noMemory();

View File

@@ -7,6 +7,8 @@
- wl_buffer with shm
*/
#include <hyprutils/os/FileDescriptor.hpp>
#include <memory>
#include <vector>
#include <cstdint>
#include "../WaylandProtocol.hpp"
@@ -18,14 +20,14 @@ class CWLSHMPoolResource;
class CSHMPool {
public:
CSHMPool(int fd, size_t size);
CSHMPool(Hyprutils::OS::CFileDescriptor fd, size_t size);
~CSHMPool();
int fd = 0;
size_t size = 0;
void* data = nullptr;
Hyprutils::OS::CFileDescriptor fd;
size_t size = 0;
void* data = nullptr;
void resize(size_t size);
void resize(size_t size);
};
class CWLSHMBuffer : public IHLBuffer {
@@ -57,7 +59,7 @@ class CWLSHMBuffer : public IHLBuffer {
class CWLSHMPoolResource {
public:
CWLSHMPoolResource(SP<CWlShmPool> resource_, int fd, size_t size);
CWLSHMPoolResource(SP<CWlShmPool> resource_, Hyprutils::OS::CFileDescriptor fd, size_t size);
bool good();

View File

@@ -7,6 +7,7 @@
#include <wayland-server-protocol.h>
#include "../../helpers/memory/Memory.hpp"
#include "../../helpers/math/Math.hpp"
#include <hyprutils/os/FileDescriptor.hpp>
class CWLDataOfferResource;
class CX11DataOffer;
@@ -24,10 +25,10 @@ class IDataSource {
IDataSource() = default;
virtual ~IDataSource() = default;
virtual std::vector<std::string> mimes() = 0;
virtual void send(const std::string& mime, uint32_t fd) = 0;
virtual void accepted(const std::string& mime) = 0;
virtual void cancelled() = 0;
virtual std::vector<std::string> mimes() = 0;
virtual void send(const std::string& mime, Hyprutils::OS::CFileDescriptor fd) = 0;
virtual void accepted(const std::string& mime) = 0;
virtual void cancelled() = 0;
virtual bool hasDnd();
virtual bool dndDone();
virtual void sendDndFinished();