Hyprland/src/protocols/DataDeviceWlr.cpp

323 lines
11 KiB
C++
Raw Normal View History

2024-05-12 15:55:46 +01:00
#include "DataDeviceWlr.hpp"
#include <algorithm>
#include "../managers/SeatManager.hpp"
#include "core/Seat.hpp"
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.
2025-01-30 12:30:12 +01:00
using namespace Hyprutils::OS;
2024-05-12 15:55:46 +01:00
CWLRDataOffer::CWLRDataOffer(SP<CZwlrDataControlOfferV1> resource_, SP<IDataSource> source_) : m_source(source_), m_resource(resource_) {
if UNLIKELY (!good())
2024-05-12 15:55:46 +01:00
return;
m_resource->setDestroy([this](CZwlrDataControlOfferV1* r) { PROTO::dataWlr->destroyResource(this); });
m_resource->setOnDestroy([this](CZwlrDataControlOfferV1* r) { PROTO::dataWlr->destroyResource(this); });
2024-05-12 15:55:46 +01:00
m_resource->setReceive([this](CZwlrDataControlOfferV1* r, const char* mime, int32_t fd) {
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.
2025-01-30 12:30:12 +01:00
CFileDescriptor sendFd{fd};
if (!m_source) {
2024-05-12 15:55:46 +01:00
LOGM(WARN, "Possible bug: Receive on an offer w/o a source");
return;
}
if (m_dead) {
2024-05-12 15:55:46 +01:00
LOGM(WARN, "Possible bug: Receive on an offer that's dead");
return;
}
LOGM(LOG, "Offer {:x} asks to send data from source {:x}", (uintptr_t)this, (uintptr_t)m_source.get());
2024-05-12 15:55:46 +01:00
m_source->send(mime, std::move(sendFd));
2024-05-12 15:55:46 +01:00
});
}
bool CWLRDataOffer::good() {
return m_resource->resource();
2024-05-12 15:55:46 +01:00
}
void CWLRDataOffer::sendData() {
if UNLIKELY (!m_source)
2024-05-12 15:55:46 +01:00
return;
for (auto const& m : m_source->mimes()) {
m_resource->sendOffer(m.c_str());
2024-05-12 15:55:46 +01:00
}
}
CWLRDataSource::CWLRDataSource(SP<CZwlrDataControlSourceV1> resource_, SP<CWLRDataDevice> device_) : m_device(device_), m_resource(resource_) {
if UNLIKELY (!good())
2024-05-12 15:55:46 +01:00
return;
m_resource->setData(this);
2024-05-12 15:55:46 +01:00
m_resource->setDestroy([this](CZwlrDataControlSourceV1* r) {
m_events.destroy.emit();
2024-05-12 15:55:46 +01:00
PROTO::dataWlr->destroyResource(this);
});
m_resource->setOnDestroy([this](CZwlrDataControlSourceV1* r) {
m_events.destroy.emit();
2024-05-12 15:55:46 +01:00
PROTO::dataWlr->destroyResource(this);
});
m_resource->setOffer([this](CZwlrDataControlSourceV1* r, const char* mime) { m_mimeTypes.emplace_back(mime); });
2024-05-12 15:55:46 +01:00
}
CWLRDataSource::~CWLRDataSource() {
m_events.destroy.emit();
2024-05-12 15:55:46 +01:00
}
SP<CWLRDataSource> CWLRDataSource::fromResource(wl_resource* res) {
auto data = sc<CWLRDataSource*>(sc<CZwlrDataControlSourceV1*>(wl_resource_get_user_data(res))->data());
return data ? data->m_self.lock() : nullptr;
2024-05-12 15:55:46 +01:00
}
bool CWLRDataSource::good() {
return m_resource->resource();
2024-05-12 15:55:46 +01:00
}
std::vector<std::string> CWLRDataSource::mimes() {
return m_mimeTypes;
2024-05-12 15:55:46 +01:00
}
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.
2025-01-30 12:30:12 +01:00
void CWLRDataSource::send(const std::string& mime, CFileDescriptor fd) {
if (std::ranges::find(m_mimeTypes, mime) == m_mimeTypes.end()) {
2024-05-12 15:55:46 +01:00
LOGM(ERR, "Compositor/App bug: CWLRDataSource::sendAskSend with non-existent mime");
return;
}
m_resource->sendSend(mime.c_str(), fd.get());
2024-05-12 15:55:46 +01:00
}
void CWLRDataSource::accepted(const std::string& mime) {
if (std::ranges::find(m_mimeTypes, mime) == m_mimeTypes.end())
2024-05-12 15:55:46 +01:00
LOGM(ERR, "Compositor/App bug: CWLRDataSource::sendAccepted with non-existent mime");
// wlr has no accepted
}
void CWLRDataSource::cancelled() {
m_resource->sendCancelled();
2024-05-12 15:55:46 +01:00
}
void CWLRDataSource::error(uint32_t code, const std::string& msg) {
m_resource->error(code, msg);
2024-05-12 15:55:46 +01:00
}
CWLRDataDevice::CWLRDataDevice(SP<CZwlrDataControlDeviceV1> resource_) : m_resource(resource_) {
if UNLIKELY (!good())
2024-05-12 15:55:46 +01:00
return;
m_client = m_resource->client();
2024-05-12 15:55:46 +01:00
m_resource->setDestroy([this](CZwlrDataControlDeviceV1* r) { PROTO::dataWlr->destroyResource(this); });
m_resource->setOnDestroy([this](CZwlrDataControlDeviceV1* r) { PROTO::dataWlr->destroyResource(this); });
2024-05-12 15:55:46 +01:00
m_resource->setSetSelection([](CZwlrDataControlDeviceV1* r, wl_resource* sourceR) {
2024-05-12 15:55:46 +01:00
auto source = sourceR ? CWLRDataSource::fromResource(sourceR) : CSharedPointer<CWLRDataSource>{};
if (!source) {
LOGM(LOG, "wlr reset selection received");
g_pSeatManager->setCurrentSelection(nullptr);
return;
}
if (source && source->used())
LOGM(WARN, "setSelection on a used resource. By protocol, this is a violation, but firefox et al insist on doing this.");
source->markUsed();
LOGM(LOG, "wlr manager requests selection to {:x}", (uintptr_t)source.get());
g_pSeatManager->setCurrentSelection(source);
});
m_resource->setSetPrimarySelection([](CZwlrDataControlDeviceV1* r, wl_resource* sourceR) {
2024-05-12 15:55:46 +01:00
auto source = sourceR ? CWLRDataSource::fromResource(sourceR) : CSharedPointer<CWLRDataSource>{};
if (!source) {
LOGM(LOG, "wlr reset primary selection received");
g_pSeatManager->setCurrentPrimarySelection(nullptr);
2024-05-12 15:55:46 +01:00
return;
}
if (source && source->used())
LOGM(WARN, "setSelection on a used resource. By protocol, this is a violation, but firefox et al insist on doing this.");
source->markUsed();
LOGM(LOG, "wlr manager requests primary selection to {:x}", (uintptr_t)source.get());
g_pSeatManager->setCurrentPrimarySelection(source);
2024-05-12 15:55:46 +01:00
});
}
bool CWLRDataDevice::good() {
return m_resource->resource();
2024-05-12 15:55:46 +01:00
}
wl_client* CWLRDataDevice::client() {
return m_client;
2024-05-12 15:55:46 +01:00
}
void CWLRDataDevice::sendInitialSelections() {
PROTO::dataWlr->sendSelectionToDevice(self.lock(), g_pSeatManager->m_selection.currentSelection.lock(), false);
PROTO::dataWlr->sendSelectionToDevice(self.lock(), g_pSeatManager->m_selection.currentPrimarySelection.lock(), true);
2024-05-12 15:55:46 +01:00
}
void CWLRDataDevice::sendDataOffer(SP<CWLRDataOffer> offer) {
m_resource->sendDataOffer(offer->m_resource.get());
2024-05-12 15:55:46 +01:00
}
void CWLRDataDevice::sendSelection(SP<CWLRDataOffer> selection) {
m_resource->sendSelection(selection->m_resource.get());
2024-05-12 15:55:46 +01:00
}
void CWLRDataDevice::sendPrimarySelection(SP<CWLRDataOffer> selection) {
m_resource->sendPrimarySelection(selection->m_resource.get());
}
CWLRDataControlManagerResource::CWLRDataControlManagerResource(SP<CZwlrDataControlManagerV1> resource_) : m_resource(resource_) {
if UNLIKELY (!good())
2024-05-12 15:55:46 +01:00
return;
m_resource->setDestroy([this](CZwlrDataControlManagerV1* r) { PROTO::dataWlr->destroyResource(this); });
m_resource->setOnDestroy([this](CZwlrDataControlManagerV1* r) { PROTO::dataWlr->destroyResource(this); });
2024-05-12 15:55:46 +01:00
m_resource->setGetDataDevice([this](CZwlrDataControlManagerV1* r, uint32_t id, wl_resource* seat) {
const auto RESOURCE = PROTO::dataWlr->m_devices.emplace_back(makeShared<CWLRDataDevice>(makeShared<CZwlrDataControlDeviceV1>(r->client(), r->version(), id)));
2024-05-12 15:55:46 +01:00
if UNLIKELY (!RESOURCE->good()) {
2024-05-12 15:55:46 +01:00
r->noMemory();
PROTO::dataWlr->m_devices.pop_back();
2024-05-12 15:55:46 +01:00
return;
}
RESOURCE->self = RESOURCE;
m_device = RESOURCE;
2024-05-12 15:55:46 +01:00
for (auto const& s : m_sources) {
2024-05-12 15:55:46 +01:00
if (!s)
continue;
s->m_device = RESOURCE;
2024-05-12 15:55:46 +01:00
}
RESOURCE->sendInitialSelections();
LOGM(LOG, "New wlr data device bound at {:x}", (uintptr_t)RESOURCE.get());
});
m_resource->setCreateDataSource([this](CZwlrDataControlManagerV1* r, uint32_t id) {
std::erase_if(m_sources, [](const auto& e) { return e.expired(); });
2024-05-12 15:55:46 +01:00
const auto RESOURCE =
PROTO::dataWlr->m_sources.emplace_back(makeShared<CWLRDataSource>(makeShared<CZwlrDataControlSourceV1>(r->client(), r->version(), id), m_device.lock()));
2024-05-12 15:55:46 +01:00
if UNLIKELY (!RESOURCE->good()) {
2024-05-12 15:55:46 +01:00
r->noMemory();
PROTO::dataWlr->m_sources.pop_back();
2024-05-12 15:55:46 +01:00
return;
}
if (!m_device)
2024-05-12 15:55:46 +01:00
LOGM(WARN, "New data source before a device was created");
RESOURCE->m_self = RESOURCE;
2024-05-12 15:55:46 +01:00
m_sources.emplace_back(RESOURCE);
2024-05-12 15:55:46 +01:00
LOGM(LOG, "New wlr data source bound at {:x}", (uintptr_t)RESOURCE.get());
});
}
bool CWLRDataControlManagerResource::good() {
return m_resource->resource();
2024-05-12 15:55:46 +01:00
}
CDataDeviceWLRProtocol::CDataDeviceWLRProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
;
}
void CDataDeviceWLRProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
const auto RESOURCE = m_managers.emplace_back(makeShared<CWLRDataControlManagerResource>(makeShared<CZwlrDataControlManagerV1>(client, ver, id)));
2024-05-12 15:55:46 +01:00
if UNLIKELY (!RESOURCE->good()) {
2024-05-12 15:55:46 +01:00
wl_client_post_no_memory(client);
m_managers.pop_back();
2024-05-12 15:55:46 +01:00
return;
}
LOGM(LOG, "New wlr_data_control_manager at {:x}", (uintptr_t)RESOURCE.get());
}
void CDataDeviceWLRProtocol::destroyResource(CWLRDataControlManagerResource* resource) {
std::erase_if(m_managers, [&](const auto& other) { return other.get() == resource; });
2024-05-12 15:55:46 +01:00
}
void CDataDeviceWLRProtocol::destroyResource(CWLRDataSource* resource) {
std::erase_if(m_sources, [&](const auto& other) { return other.get() == resource; });
2024-05-12 15:55:46 +01:00
}
void CDataDeviceWLRProtocol::destroyResource(CWLRDataDevice* resource) {
std::erase_if(m_devices, [&](const auto& other) { return other.get() == resource; });
2024-05-12 15:55:46 +01:00
}
void CDataDeviceWLRProtocol::destroyResource(CWLRDataOffer* resource) {
std::erase_if(m_offers, [&](const auto& other) { return other.get() == resource; });
2024-05-12 15:55:46 +01:00
}
void CDataDeviceWLRProtocol::sendSelectionToDevice(SP<CWLRDataDevice> dev, SP<IDataSource> sel, bool primary) {
if (!sel) {
if (primary)
dev->m_resource->sendPrimarySelectionRaw(nullptr);
else
dev->m_resource->sendSelectionRaw(nullptr);
2024-05-12 15:55:46 +01:00
return;
}
2024-05-12 15:55:46 +01:00
const auto OFFER = m_offers.emplace_back(makeShared<CWLRDataOffer>(makeShared<CZwlrDataControlOfferV1>(dev->m_resource->client(), dev->m_resource->version(), 0), sel));
2024-05-12 15:55:46 +01:00
if (!OFFER->good()) {
dev->m_resource->noMemory();
m_offers.pop_back();
2024-05-12 15:55:46 +01:00
return;
}
OFFER->m_primary = primary;
LOGM(LOG, "New {}offer {:x} for data source {:x}", primary ? "primary " : " ", (uintptr_t)OFFER.get(), (uintptr_t)sel.get());
2024-05-12 15:55:46 +01:00
dev->sendDataOffer(OFFER);
OFFER->sendData();
if (primary)
dev->sendPrimarySelection(OFFER);
else
dev->sendSelection(OFFER);
2024-05-12 15:55:46 +01:00
}
void CDataDeviceWLRProtocol::setSelection(SP<IDataSource> source, bool primary) {
for (auto const& o : m_offers) {
if (o->m_source && o->m_source->hasDnd())
2024-05-12 15:55:46 +01:00
continue;
if (o->m_primary != primary)
continue;
o->m_dead = true;
2024-05-12 15:55:46 +01:00
}
if (!source) {
LOGM(LOG, "resetting {}selection", primary ? "primary " : " ");
2024-05-12 15:55:46 +01:00
for (auto const& d : m_devices) {
sendSelectionToDevice(d, nullptr, primary);
2024-05-12 15:55:46 +01:00
}
return;
}
LOGM(LOG, "New {}selection for data source {:x}", primary ? "primary" : "", (uintptr_t)source.get());
2024-05-12 15:55:46 +01:00
for (auto const& d : m_devices) {
sendSelectionToDevice(d, source, primary);
2024-05-12 15:55:46 +01:00
}
}
SP<CWLRDataDevice> CDataDeviceWLRProtocol::dataDeviceForClient(wl_client* c) {
auto it = std::ranges::find_if(m_devices, [c](const auto& e) { return e->client() == c; });
if (it == m_devices.end())
2024-05-12 15:55:46 +01:00
return nullptr;
return *it;
}