2024-04-21 22:21:22 +02:00
|
|
|
#include "AlphaModifier.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
#include "../desktop/WLSurface.hpp"
|
|
|
|
#include "../render/Renderer.hpp"
|
2024-06-08 10:07:59 +02:00
|
|
|
#include "core/Compositor.hpp"
|
2024-04-21 22:21:22 +02:00
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
CAlphaModifier::CAlphaModifier(SP<CWpAlphaModifierSurfaceV1> resource_, SP<CWLSurfaceResource> surface_) : resource(resource_), pSurface(surface_) {
|
2024-04-21 22:21:22 +02:00
|
|
|
if (!resource->resource())
|
|
|
|
return;
|
|
|
|
|
|
|
|
resource->setDestroy([this](CWpAlphaModifierSurfaceV1* pMgr) {
|
|
|
|
PROTO::alphaModifier->destroyModifier(this);
|
|
|
|
setSurfaceAlpha(1.F);
|
|
|
|
});
|
|
|
|
resource->setOnDestroy([this](CWpAlphaModifierSurfaceV1* pMgr) {
|
|
|
|
PROTO::alphaModifier->destroyModifier(this);
|
|
|
|
setSurfaceAlpha(1.F);
|
|
|
|
});
|
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
listeners.destroySurface = pSurface->events.destroy.registerListener([this](std::any d) { onSurfaceDestroy(); });
|
2024-04-21 22:21:22 +02:00
|
|
|
|
|
|
|
resource->setSetMultiplier([this](CWpAlphaModifierSurfaceV1* mod, uint32_t alpha) {
|
|
|
|
if (!pSurface) {
|
2024-04-22 19:44:25 +02:00
|
|
|
LOGM(ERR, "Resource {:x} tried to setMultiplier but surface is gone", (uintptr_t)mod->resource());
|
2024-05-01 20:40:35 +02:00
|
|
|
mod->error(WP_ALPHA_MODIFIER_SURFACE_V1_ERROR_NO_SURFACE, "Surface is gone");
|
2024-04-21 22:21:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
float a = alpha / (float)UINT32_MAX;
|
|
|
|
|
|
|
|
setSurfaceAlpha(a);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
CAlphaModifier::~CAlphaModifier() {
|
2024-06-08 10:07:59 +02:00
|
|
|
;
|
2024-04-21 22:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CAlphaModifier::good() {
|
|
|
|
return resource->resource();
|
|
|
|
}
|
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
SP<CWLSurfaceResource> CAlphaModifier::getSurface() {
|
|
|
|
return pSurface.lock();
|
2024-04-21 22:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAlphaModifier::setSurfaceAlpha(float a) {
|
2024-06-08 10:07:59 +02:00
|
|
|
auto surf = CWLSurface::fromResource(pSurface.lock());
|
2024-04-21 22:21:22 +02:00
|
|
|
|
|
|
|
if (!surf) {
|
2024-04-22 19:44:25 +02:00
|
|
|
LOGM(ERR, "CAlphaModifier::setSurfaceAlpha: No CWLSurface for given surface??");
|
2024-04-21 22:21:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
surf->m_pAlphaModifier = a;
|
|
|
|
|
|
|
|
auto SURFBOX = surf->getSurfaceBoxGlobal();
|
|
|
|
if (SURFBOX.has_value())
|
|
|
|
g_pHyprRenderer->damageBox(&*SURFBOX);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAlphaModifier::onSurfaceDestroy() {
|
2024-06-08 10:07:59 +02:00
|
|
|
pSurface.reset();
|
2024-04-21 22:21:22 +02: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) {
|
|
|
|
const auto RESOURCE = m_vManagers.emplace_back(std::make_unique<CWpAlphaModifierV1>(client, ver, id)).get();
|
|
|
|
RESOURCE->setOnDestroy([this](CWpAlphaModifierV1* p) { this->onManagerResourceDestroy(p->resource()); });
|
|
|
|
|
|
|
|
RESOURCE->setDestroy([this](CWpAlphaModifierV1* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); });
|
2024-06-08 10:07:59 +02:00
|
|
|
RESOURCE->setGetSurface([this](CWpAlphaModifierV1* pMgr, uint32_t id, wl_resource* surface) { this->onGetSurface(pMgr, id, CWLSurfaceResource::fromResource(surface)); });
|
2024-04-21 22:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAlphaModifierProtocol::onManagerResourceDestroy(wl_resource* res) {
|
|
|
|
std::erase_if(m_vManagers, [&](const auto& other) { return other->resource() == res; });
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAlphaModifierProtocol::destroyModifier(CAlphaModifier* modifier) {
|
2024-06-08 10:07:59 +02:00
|
|
|
std::erase_if(m_mAlphaModifiers, [](const auto& e) { return e.first.expired(); });
|
2024-04-21 22:21:22 +02:00
|
|
|
}
|
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
void CAlphaModifierProtocol::onGetSurface(CWpAlphaModifierV1* pMgr, uint32_t id, SP<CWLSurfaceResource> surface) {
|
|
|
|
if (std::find_if(m_mAlphaModifiers.begin(), m_mAlphaModifiers.end(), [surface](const auto& e) { return e.first == surface; }) != m_mAlphaModifiers.end()) {
|
2024-07-22 18:06:11 +02:00
|
|
|
LOGM(ERR, "AlphaModifier already present for surface {:x}", (uintptr_t)surface.get());
|
2024-05-01 20:40:35 +02:00
|
|
|
pMgr->error(WP_ALPHA_MODIFIER_V1_ERROR_ALREADY_CONSTRUCTED, "AlphaModifier already present");
|
2024-04-21 22:21:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-05 18:16:00 +02:00
|
|
|
const auto RESOURCE = m_mAlphaModifiers.emplace(surface, std::make_unique<CAlphaModifier>(makeShared<CWpAlphaModifierSurfaceV1>(pMgr->client(), pMgr->version(), id), surface))
|
|
|
|
.first->second.get();
|
2024-04-21 22:21:22 +02:00
|
|
|
|
|
|
|
if (!RESOURCE->good()) {
|
2024-05-01 20:40:35 +02:00
|
|
|
pMgr->noMemory();
|
2024-04-21 22:21:22 +02:00
|
|
|
m_mAlphaModifiers.erase(surface);
|
|
|
|
return;
|
|
|
|
}
|
2024-07-22 18:06:11 +02:00
|
|
|
}
|