datasource: move to CFileDescriptor

use CFileDescriptor in data/selection sources and its related usage.
This commit is contained in:
Tom Englund 2024-11-21 08:44:08 +01:00
parent 97f1bd7c3e
commit 31a3e1038b
11 changed files with 31 additions and 40 deletions

View file

@ -25,7 +25,7 @@ CWLRDataOffer::CWLRDataOffer(SP<CZwlrDataControlOfferV1> resource_, SP<IDataSour
LOGM(LOG, "Offer {:x} asks to send data from source {:x}", (uintptr_t)this, (uintptr_t)source.get()); 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, CFileDescriptor(fd));
}); });
} }
@ -77,15 +77,13 @@ std::vector<std::string> CWLRDataSource::mimes() {
return mimeTypes; 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()) { if (std::find(mimeTypes.begin(), mimeTypes.end(), mime) == mimeTypes.end()) {
LOGM(ERR, "Compositor/App bug: CWLRDataSource::sendAskSend with non-existent mime"); LOGM(ERR, "Compositor/App bug: CWLRDataSource::sendAskSend with non-existent mime");
close(fd);
return; return;
} }
resource->sendSend(mime.c_str(), fd); resource->sendSend(mime.c_str(), fd.get());
close(fd);
} }
void CWLRDataSource::accepted(const std::string& mime) { void CWLRDataSource::accepted(const std::string& mime) {

View file

@ -39,7 +39,7 @@ class CWLRDataSource : public IDataSource {
bool good(); bool good();
virtual std::vector<std::string> mimes(); virtual std::vector<std::string> mimes();
virtual void send(const std::string& mime, uint32_t fd); virtual void send(const std::string& mime, CFileDescriptor fd);
virtual void accepted(const std::string& mime); virtual void accepted(const std::string& mime);
virtual void cancelled(); virtual void cancelled();
virtual void error(uint32_t code, const std::string& msg); virtual void error(uint32_t code, const std::string& msg);

View file

@ -26,7 +26,7 @@ CPrimarySelectionOffer::CPrimarySelectionOffer(SP<CZwpPrimarySelectionOfferV1> r
LOGM(LOG, "Offer {:x} asks to send data from source {:x}", (uintptr_t)this, (uintptr_t)source.get()); 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, CFileDescriptor(fd));
}); });
} }
@ -78,15 +78,13 @@ std::vector<std::string> CPrimarySelectionSource::mimes() {
return mimeTypes; 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()) { if (std::find(mimeTypes.begin(), mimeTypes.end(), mime) == mimeTypes.end()) {
LOGM(ERR, "Compositor/App bug: CPrimarySelectionSource::sendAskSend with non-existent mime"); LOGM(ERR, "Compositor/App bug: CPrimarySelectionSource::sendAskSend with non-existent mime");
close(fd);
return; return;
} }
resource->sendSend(mime.c_str(), fd); resource->sendSend(mime.c_str(), fd.get());
close(fd);
} }
void CPrimarySelectionSource::accepted(const std::string& mime) { void CPrimarySelectionSource::accepted(const std::string& mime) {

View file

@ -39,7 +39,7 @@ class CPrimarySelectionSource : public IDataSource {
bool good(); bool good();
virtual std::vector<std::string> mimes(); virtual std::vector<std::string> mimes();
virtual void send(const std::string& mime, uint32_t fd); virtual void send(const std::string& mime, CFileDescriptor fd);
virtual void accepted(const std::string& mime); virtual void accepted(const std::string& mime);
virtual void cancelled(); virtual void cancelled();
virtual void error(uint32_t code, const std::string& msg); virtual void error(uint32_t code, const std::string& msg);

View file

@ -50,7 +50,7 @@ CWLDataOfferResource::CWLDataOfferResource(SP<CWlDataOffer> resource_, SP<IDataS
source->accepted(mime ? mime : ""); source->accepted(mime ? mime : "");
} }
source->send(mime ? mime : "", fd); source->send(mime ? mime : "", CFileDescriptor(fd));
recvd = true; recvd = true;
@ -158,15 +158,13 @@ std::vector<std::string> CWLDataSourceResource::mimes() {
return mimeTypes; 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()) { if (std::find(mimeTypes.begin(), mimeTypes.end(), mime) == mimeTypes.end()) {
LOGM(ERR, "Compositor/App bug: CWLDataSourceResource::sendAskSend with non-existent mime"); LOGM(ERR, "Compositor/App bug: CWLDataSourceResource::sendAskSend with non-existent mime");
close(fd);
return; return;
} }
resource->sendSend(mime.c_str(), fd); resource->sendSend(mime.c_str(), fd.get());
close(fd);
} }
void CWLDataSourceResource::cancelled() { void CWLDataSourceResource::cancelled() {

View file

@ -58,7 +58,7 @@ class CWLDataSourceResource : public IDataSource {
bool good(); bool good();
virtual std::vector<std::string> mimes(); virtual std::vector<std::string> mimes();
virtual void send(const std::string& mime, uint32_t fd); virtual void send(const std::string& mime, CFileDescriptor fd);
virtual void accepted(const std::string& mime); virtual void accepted(const std::string& mime);
virtual void cancelled(); virtual void cancelled();
virtual bool hasDnd(); virtual bool hasDnd();

View file

@ -15,10 +15,10 @@ class IDataSource {
IDataSource() {} IDataSource() {}
virtual ~IDataSource() {} virtual ~IDataSource() {}
virtual std::vector<std::string> mimes() = 0; virtual std::vector<std::string> mimes() = 0;
virtual void send(const std::string& mime, uint32_t fd) = 0; virtual void send(const std::string& mime, CFileDescriptor fd) = 0;
virtual void accepted(const std::string& mime) = 0; virtual void accepted(const std::string& mime) = 0;
virtual void cancelled() = 0; virtual void cancelled() = 0;
virtual bool hasDnd(); virtual bool hasDnd();
virtual bool dndDone(); virtual bool dndDone();
virtual void sendDndFinished(); virtual void sendDndFinished();

View file

@ -46,7 +46,7 @@ std::vector<std::string> CXDataSource::mimes() {
return mimeTypes; return mimeTypes;
} }
void CXDataSource::send(const std::string& mime, uint32_t fd) { void CXDataSource::send(const std::string& mime, CFileDescriptor fd) {
xcb_atom_t mimeAtom = 0; xcb_atom_t mimeAtom = 0;
if (mime == "text/plain") if (mime == "text/plain")
@ -64,11 +64,10 @@ void CXDataSource::send(const std::string& mime, uint32_t fd) {
if (!mimeAtom) { if (!mimeAtom) {
Debug::log(ERR, "[XDataSource] mime atom not found"); Debug::log(ERR, "[XDataSource] mime atom not found");
close(fd);
return; return;
} }
Debug::log(LOG, "[XDataSource] send with mime {} to fd {}", mime, fd); Debug::log(LOG, "[XDataSource] send with mime {} to fd {}", mime, fd.get());
selection.transfer = std::make_unique<SXTransfer>(selection); selection.transfer = std::make_unique<SXTransfer>(selection);
selection.transfer->incomingWindow = xcb_generate_id(g_pXWayland->pWM->connection); selection.transfer->incomingWindow = xcb_generate_id(g_pXWayland->pWM->connection);
@ -80,8 +79,8 @@ void CXDataSource::send(const std::string& mime, uint32_t fd) {
xcb_flush(g_pXWayland->pWM->connection); xcb_flush(g_pXWayland->pWM->connection);
fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK); fcntl(fd.get(), F_SETFL, O_WRONLY | O_NONBLOCK);
selection.transfer->wlFD = fd; selection.transfer->wlFD = std::move(fd);
} }
void CXDataSource::accepted(const std::string& mime) { void CXDataSource::accepted(const std::string& mime) {
@ -100,4 +99,4 @@ eDataSourceType CXDataSource::type() {
return DATA_SOURCE_TYPE_X11; return DATA_SOURCE_TYPE_X11;
} }
#endif #endif

View file

@ -9,7 +9,7 @@ class CXDataSource : public IDataSource {
CXDataSource(SXSelection&); CXDataSource(SXSelection&);
virtual std::vector<std::string> mimes(); virtual std::vector<std::string> mimes();
virtual void send(const std::string& mime, uint32_t fd); virtual void send(const std::string& mime, CFileDescriptor fd);
virtual void accepted(const std::string& mime); virtual void accepted(const std::string& mime);
virtual void cancelled(); virtual void cancelled();
virtual void error(uint32_t code, const std::string& msg); virtual void error(uint32_t code, const std::string& msg);
@ -19,4 +19,4 @@ class CXDataSource : public IDataSource {
SXSelection& selection; SXSelection& selection;
std::vector<std::string> mimeTypes; // these two have shared idx std::vector<std::string> mimeTypes; // these two have shared idx
std::vector<uint32_t> mimeAtoms; // std::vector<uint32_t> mimeAtoms; //
}; };

View file

@ -1112,17 +1112,17 @@ void CXWM::getTransferData(SXSelection& sel) {
if (sel.transfer->propertyReply->type == HYPRATOMS["INCR"]) { if (sel.transfer->propertyReply->type == HYPRATOMS["INCR"]) {
Debug::log(ERR, "[xwm] Transfer is INCR, which we don't support :("); Debug::log(ERR, "[xwm] Transfer is INCR, which we don't support :(");
close(sel.transfer->wlFD); sel.transfer->wlFD.reset();
sel.transfer.reset(); sel.transfer.reset();
return; return;
} else { } else {
char* property = (char*)xcb_get_property_value(sel.transfer->propertyReply); char* property = (char*)xcb_get_property_value(sel.transfer->propertyReply);
int remainder = xcb_get_property_value_length(sel.transfer->propertyReply) - sel.transfer->propertyStart; int remainder = xcb_get_property_value_length(sel.transfer->propertyReply) - sel.transfer->propertyStart;
ssize_t len = write(sel.transfer->wlFD, property + sel.transfer->propertyStart, remainder); ssize_t len = write(sel.transfer->wlFD.get(), property + sel.transfer->propertyStart, remainder);
if (len == -1) { if (len == -1) {
Debug::log(ERR, "[xwm] write died in transfer get"); Debug::log(ERR, "[xwm] write died in transfer get");
close(sel.transfer->wlFD); sel.transfer->wlFD.reset();
sel.transfer.reset(); sel.transfer.reset();
return; return;
} }
@ -1133,7 +1133,7 @@ void CXWM::getTransferData(SXSelection& sel) {
return; return;
} else { } else {
Debug::log(LOG, "[xwm] cb transfer to wl client complete, read {} bytes", len); Debug::log(LOG, "[xwm] cb transfer to wl client complete, read {} bytes", len);
close(sel.transfer->wlFD); sel.transfer->wlFD.reset();
sel.transfer.reset(); sel.transfer.reset();
} }
} }
@ -1249,20 +1249,18 @@ bool SXSelection::sendData(xcb_selection_request_event_t* e, std::string mime) {
fcntl(p[1], F_SETFD, FD_CLOEXEC); fcntl(p[1], F_SETFD, FD_CLOEXEC);
fcntl(p[1], F_SETFL, O_NONBLOCK); fcntl(p[1], F_SETFL, O_NONBLOCK);
transfer->wlFD = p[0]; transfer->wlFD = CFileDescriptor(p[0]);
Debug::log(LOG, "[xwm] sending wayland selection to xwayland with mime {}, target {}, fds {} {}", mime, e->target, p[0], p[1]); Debug::log(LOG, "[xwm] sending wayland selection to xwayland with mime {}, target {}, fds {} {}", mime, e->target, p[0], p[1]);
selection->send(mime, p[1]); selection->send(mime, CFileDescriptor(p[1]));
transfer->eventSource = wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, transfer->wlFD, WL_EVENT_READABLE, ::readDataSource, this); transfer->eventSource = wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, transfer->wlFD.get(), WL_EVENT_READABLE, ::readDataSource, this);
return true; return true;
} }
SXTransfer::~SXTransfer() { SXTransfer::~SXTransfer() {
if (wlFD)
close(wlFD);
if (eventSource) if (eventSource)
wl_event_source_remove(eventSource); wl_event_source_remove(eventSource);
if (incomingWindow) if (incomingWindow)

View file

@ -25,7 +25,7 @@ struct SXTransfer {
bool flushOnDelete = false; bool flushOnDelete = false;
bool propertySet = false; bool propertySet = false;
int wlFD = -1; CFileDescriptor wlFD;
wl_event_source* eventSource = nullptr; wl_event_source* eventSource = nullptr;
std::vector<uint8_t> data; std::vector<uint8_t> data;