2024-04-21 21:21:22 +01:00
|
|
|
#include "AlphaModifier.hpp"
|
2025-12-08 15:04:40 +00:00
|
|
|
#include "../desktop/view/WLSurface.hpp"
|
2024-04-21 21:21:22 +01:00
|
|
|
#include "../render/Renderer.hpp"
|
2025-01-02 03:53:57 -08:00
|
|
|
#include "alpha-modifier-v1.hpp"
|
2024-06-08 10:07:59 +02:00
|
|
|
#include "core/Compositor.hpp"
|
2024-04-21 21:21:22 +01:00
|
|
|
|
2025-07-10 12:47:21 +02:00
|
|
|
CAlphaModifier::CAlphaModifier(UP<CWpAlphaModifierSurfaceV1>&& resource, SP<CWLSurfaceResource> surface) : m_surface(surface) {
|
2025-01-02 03:53:57 -08:00
|
|
|
setResource(std::move(resource));
|
|
|
|
|
}
|
2024-04-21 21:21:22 +01:00
|
|
|
|
2025-01-02 03:53:57 -08:00
|
|
|
bool CAlphaModifier::good() {
|
2025-05-04 00:13:29 +02:00
|
|
|
return m_resource->resource();
|
2025-01-02 03:53:57 -08:00
|
|
|
}
|
2024-04-21 21:21:22 +01:00
|
|
|
|
2025-07-10 12:47:21 +02:00
|
|
|
void CAlphaModifier::setResource(UP<CWpAlphaModifierSurfaceV1>&& resource) {
|
2025-05-04 00:13:29 +02:00
|
|
|
m_resource = std::move(resource);
|
2025-01-02 03:53:57 -08:00
|
|
|
|
2025-05-04 00:13:29 +02:00
|
|
|
if UNLIKELY (!m_resource->resource())
|
2025-01-02 03:53:57 -08:00
|
|
|
return;
|
2024-04-21 21:21:22 +01:00
|
|
|
|
2025-05-04 00:13:29 +02:00
|
|
|
m_resource->setDestroy([this](CWpAlphaModifierSurfaceV1* resource) { destroy(); });
|
|
|
|
|
m_resource->setOnDestroy([this](CWpAlphaModifierSurfaceV1* resource) { destroy(); });
|
2025-01-02 03:53:57 -08:00
|
|
|
|
2025-05-04 00:13:29 +02:00
|
|
|
m_resource->setSetMultiplier([this](CWpAlphaModifierSurfaceV1* resource, uint32_t alpha) {
|
|
|
|
|
if (!m_surface) {
|
|
|
|
|
m_resource->error(WP_ALPHA_MODIFIER_SURFACE_V1_ERROR_NO_SURFACE, "set_multiplier called for destroyed wl_surface");
|
2024-04-21 21:21:22 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-14 19:44:56 +05:00
|
|
|
m_alpha = alpha / sc<float>(UINT32_MAX);
|
2024-04-21 21:21:22 +01:00
|
|
|
});
|
|
|
|
|
|
2025-07-08 09:56:40 -07:00
|
|
|
m_listeners.surfaceCommitted = m_surface->m_events.commit.listen([this] {
|
2025-12-08 15:04:40 +00:00
|
|
|
auto surface = Desktop::View::CWLSurface::fromResource(m_surface.lock());
|
2024-04-21 21:21:22 +01:00
|
|
|
|
2025-05-04 00:13:29 +02:00
|
|
|
if (surface && surface->m_alphaModifier != m_alpha) {
|
|
|
|
|
surface->m_alphaModifier = m_alpha;
|
2025-04-25 02:37:12 +02:00
|
|
|
auto box = surface->getSurfaceBoxGlobal();
|
2024-04-21 21:21:22 +01:00
|
|
|
|
2025-01-02 03:53:57 -08:00
|
|
|
if (box.has_value())
|
2025-01-26 15:05:34 +00:00
|
|
|
g_pHyprRenderer->damageBox(*box);
|
2024-04-21 21:21:22 +01:00
|
|
|
|
2025-05-04 00:13:29 +02:00
|
|
|
if (!m_resource)
|
2025-01-02 03:53:57 -08:00
|
|
|
PROTO::alphaModifier->destroyAlphaModifier(this);
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-04-21 21:21:22 +01:00
|
|
|
|
2025-07-08 09:56:40 -07:00
|
|
|
m_listeners.surfaceDestroyed = m_surface->m_events.destroy.listen([this] {
|
2025-05-04 00:13:29 +02:00
|
|
|
if (!m_resource)
|
2025-01-02 03:53:57 -08:00
|
|
|
PROTO::alphaModifier->destroyAlphaModifier(this);
|
|
|
|
|
});
|
2024-04-21 21:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-02 03:53:57 -08:00
|
|
|
void CAlphaModifier::destroy() {
|
2025-05-04 00:13:29 +02:00
|
|
|
m_resource.reset();
|
|
|
|
|
m_alpha = 1.F;
|
2025-01-02 03:53:57 -08:00
|
|
|
|
2025-05-04 00:13:29 +02:00
|
|
|
if (!m_surface)
|
2025-01-02 03:53:57 -08:00
|
|
|
PROTO::alphaModifier->destroyAlphaModifier(this);
|
2024-04-21 21:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CAlphaModifierProtocol::CAlphaModifierProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CAlphaModifierProtocol::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(makeUnique<CWpAlphaModifierV1>(client, ver, id)).get();
|
2025-01-02 03:53:57 -08:00
|
|
|
RESOURCE->setOnDestroy([this](CWpAlphaModifierV1* manager) { destroyManager(manager); });
|
2024-04-21 21:21:22 +01:00
|
|
|
|
2025-01-02 03:53:57 -08:00
|
|
|
RESOURCE->setDestroy([this](CWpAlphaModifierV1* manager) { destroyManager(manager); });
|
|
|
|
|
RESOURCE->setGetSurface([this](CWpAlphaModifierV1* manager, uint32_t id, wl_resource* surface) { getSurface(manager, id, CWLSurfaceResource::fromResource(surface)); });
|
2024-04-21 21:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-02 03:53:57 -08:00
|
|
|
void CAlphaModifierProtocol::destroyManager(CWpAlphaModifierV1* manager) {
|
2025-05-04 00:13:29 +02:00
|
|
|
std::erase_if(m_managers, [&](const auto& p) { return p.get() == manager; });
|
2024-04-21 21:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-02 03:53:57 -08:00
|
|
|
void CAlphaModifierProtocol::destroyAlphaModifier(CAlphaModifier* modifier) {
|
2025-05-04 00:13:29 +02:00
|
|
|
std::erase_if(m_alphaModifiers, [&](const auto& entry) { return entry.second.get() == modifier; });
|
2024-04-21 21:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-02 03:53:57 -08:00
|
|
|
void CAlphaModifierProtocol::getSurface(CWpAlphaModifierV1* manager, uint32_t id, SP<CWLSurfaceResource> surface) {
|
|
|
|
|
CAlphaModifier* alphaModifier = nullptr;
|
2025-05-30 18:25:59 +05:00
|
|
|
auto iter = std::ranges::find_if(m_alphaModifiers, [&](const auto& entry) { return entry.second->m_surface == surface; });
|
2024-04-21 21:21:22 +01:00
|
|
|
|
2025-05-04 00:13:29 +02:00
|
|
|
if (iter != m_alphaModifiers.end()) {
|
|
|
|
|
if (iter->second->m_resource) {
|
2025-01-02 03:53:57 -08:00
|
|
|
LOGM(ERR, "AlphaModifier already present for surface {:x}", (uintptr_t)surface.get());
|
|
|
|
|
manager->error(WP_ALPHA_MODIFIER_V1_ERROR_ALREADY_CONSTRUCTED, "AlphaModifier already present");
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
2025-07-10 12:47:21 +02:00
|
|
|
iter->second->setResource(makeUnique<CWpAlphaModifierSurfaceV1>(manager->client(), manager->version(), id));
|
2025-01-02 03:53:57 -08:00
|
|
|
alphaModifier = iter->second.get();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-07-10 12:47:21 +02:00
|
|
|
alphaModifier = m_alphaModifiers.emplace(surface, makeUnique<CAlphaModifier>(makeUnique<CWpAlphaModifierSurfaceV1>(manager->client(), manager->version(), id), surface))
|
2025-01-23 21:55:41 +01:00
|
|
|
.first->second.get();
|
2025-01-02 03:53:57 -08:00
|
|
|
}
|
2024-04-21 21:21:22 +01:00
|
|
|
|
2025-01-17 18:21:34 +01:00
|
|
|
if UNLIKELY (!alphaModifier->good()) {
|
2025-01-02 03:53:57 -08:00
|
|
|
manager->noMemory();
|
2025-05-04 00:13:29 +02:00
|
|
|
m_alphaModifiers.erase(surface);
|
2024-04-21 21:21:22 +01:00
|
|
|
}
|
2024-07-22 11:06:11 -05:00
|
|
|
}
|