2025-02-02 22:25:29 +03:00
|
|
|
#include "ContentType.hpp"
|
|
|
|
|
#include "content-type-v1.hpp"
|
|
|
|
|
#include "protocols/types/ContentType.hpp"
|
|
|
|
|
|
|
|
|
|
CContentTypeManager::CContentTypeManager(SP<CWpContentTypeManagerV1> resource) : m_resource(resource) {
|
|
|
|
|
if UNLIKELY (!good())
|
|
|
|
|
return;
|
|
|
|
|
|
2025-07-10 13:09:19 +02:00
|
|
|
m_resource->setDestroy([](CWpContentTypeManagerV1* r) {});
|
|
|
|
|
m_resource->setOnDestroy([this](CWpContentTypeManagerV1* r) { PROTO::contentType->destroyResource(this); });
|
2025-02-02 22:25:29 +03:00
|
|
|
|
2025-07-10 13:09:19 +02:00
|
|
|
m_resource->setGetSurfaceContentType([](CWpContentTypeManagerV1* r, uint32_t id, wl_resource* surface) {
|
2025-12-17 20:08:47 +00:00
|
|
|
LOGM(Log::TRACE, "Get surface for id={}, surface={}", id, (uintptr_t)surface);
|
2025-02-02 22:25:29 +03:00
|
|
|
auto SURF = CWLSurfaceResource::fromResource(surface);
|
|
|
|
|
|
|
|
|
|
if (!SURF) {
|
2025-12-17 20:08:47 +00:00
|
|
|
LOGM(Log::ERR, "No surface for resource {}", (uintptr_t)surface);
|
2025-02-02 22:25:29 +03:00
|
|
|
r->error(-1, "Invalid surface (2)");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 16:02:49 +02:00
|
|
|
if (SURF->m_colorManagement) {
|
2025-02-02 22:25:29 +03:00
|
|
|
r->error(WP_CONTENT_TYPE_MANAGER_V1_ERROR_ALREADY_CONSTRUCTED, "CT manager already exists");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 00:13:29 +02:00
|
|
|
const auto RESOURCE = PROTO::contentType->m_contentTypes.emplace_back(makeShared<CContentType>(makeShared<CWpContentTypeV1>(r->client(), r->version(), id)));
|
2025-02-02 22:25:29 +03:00
|
|
|
if UNLIKELY (!RESOURCE->good()) {
|
|
|
|
|
r->noMemory();
|
2025-05-04 00:13:29 +02:00
|
|
|
PROTO::contentType->m_contentTypes.pop_back();
|
2025-02-02 22:25:29 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 00:13:29 +02:00
|
|
|
RESOURCE->m_self = RESOURCE;
|
2025-02-02 22:25:29 +03:00
|
|
|
|
2025-05-03 16:02:49 +02:00
|
|
|
SURF->m_contentType = RESOURCE;
|
2025-02-02 22:25:29 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CContentTypeManager::good() {
|
|
|
|
|
return m_resource->resource();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CContentType::CContentType(WP<CWLSurfaceResource> surface) {
|
2025-07-08 09:56:40 -07:00
|
|
|
m_destroy = surface->m_events.destroy.listen([this] { PROTO::contentType->destroyResource(this); });
|
2025-02-02 22:25:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CContentType::CContentType(SP<CWpContentTypeV1> resource) : m_resource(resource) {
|
|
|
|
|
if UNLIKELY (!good())
|
|
|
|
|
return;
|
|
|
|
|
|
2025-07-10 13:09:19 +02:00
|
|
|
m_client = m_resource->client();
|
2025-02-02 22:25:29 +03:00
|
|
|
|
2025-07-10 13:09:19 +02:00
|
|
|
m_resource->setDestroy([this](CWpContentTypeV1* r) { PROTO::contentType->destroyResource(this); });
|
|
|
|
|
m_resource->setOnDestroy([this](CWpContentTypeV1* r) { PROTO::contentType->destroyResource(this); });
|
2025-02-02 22:25:29 +03:00
|
|
|
|
2025-07-10 13:09:19 +02:00
|
|
|
m_resource->setSetContentType([this](CWpContentTypeV1* r, wpContentTypeV1Type type) { m_value = NContentType::fromWP(type); });
|
2025-02-02 22:25:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CContentType::good() {
|
|
|
|
|
return m_resource && m_resource->resource();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wl_client* CContentType::client() {
|
2025-05-04 00:13:29 +02:00
|
|
|
return m_client;
|
2025-02-02 22:25:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CContentTypeProtocol::CContentTypeProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CContentTypeProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
|
2025-05-04 00:13:29 +02:00
|
|
|
const auto RESOURCE = m_managers.emplace_back(makeShared<CContentTypeManager>(makeShared<CWpContentTypeManagerV1>(client, ver, id)));
|
2025-02-02 22:25:29 +03:00
|
|
|
|
|
|
|
|
if UNLIKELY (!RESOURCE->good()) {
|
|
|
|
|
wl_client_post_no_memory(client);
|
2025-05-04 00:13:29 +02:00
|
|
|
m_managers.pop_back();
|
2025-02-02 22:25:29 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SP<CContentType> CContentTypeProtocol::getContentType(WP<CWLSurfaceResource> surface) {
|
2025-05-03 16:02:49 +02:00
|
|
|
if (surface->m_contentType.valid())
|
|
|
|
|
return surface->m_contentType.lock();
|
2025-02-02 22:25:29 +03:00
|
|
|
|
2025-05-04 00:13:29 +02:00
|
|
|
return m_contentTypes.emplace_back(makeShared<CContentType>(surface));
|
2025-02-02 22:25:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CContentTypeProtocol::destroyResource(CContentTypeManager* resource) {
|
2025-05-04 00:13:29 +02:00
|
|
|
std::erase_if(m_managers, [&](const auto& other) { return other.get() == resource; });
|
2025-02-02 22:25:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CContentTypeProtocol::destroyResource(CContentType* resource) {
|
2025-05-04 00:13:29 +02:00
|
|
|
std::erase_if(m_contentTypes, [&](const auto& other) { return other.get() == resource; });
|
2025-02-02 22:25:29 +03:00
|
|
|
}
|