2024-02-29 01:03:28 +01:00
|
|
|
#include "Subsurface.hpp"
|
|
|
|
#include "../events/Events.hpp"
|
|
|
|
#include "../Compositor.hpp"
|
2024-03-03 19:39:20 +01:00
|
|
|
#include "../config/ConfigValue.hpp"
|
2024-06-08 10:07:59 +02:00
|
|
|
#include "../protocols/core/Compositor.hpp"
|
|
|
|
#include "../protocols/core/Subcompositor.hpp"
|
2024-02-29 01:03:28 +01:00
|
|
|
|
2024-04-27 13:43:12 +02:00
|
|
|
CSubsurface::CSubsurface(PHLWINDOW pOwner) : m_pWindowParent(pOwner) {
|
2024-02-29 01:03:28 +01:00
|
|
|
initSignals();
|
2024-06-08 10:07:59 +02:00
|
|
|
initExistingSubsurfaces(pOwner->m_pWLSurface->resource());
|
2024-02-29 01:03:28 +01:00
|
|
|
}
|
|
|
|
|
2024-02-29 15:26:02 +01:00
|
|
|
CSubsurface::CSubsurface(CPopup* pOwner) : m_pPopupParent(pOwner) {
|
|
|
|
initSignals();
|
2024-06-08 10:07:59 +02:00
|
|
|
initExistingSubsurfaces(pOwner->m_pWLSurface->resource());
|
2024-02-29 15:26:02 +01:00
|
|
|
}
|
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
CSubsurface::CSubsurface(SP<CWLSubsurfaceResource> pSubsurface, PHLWINDOW pOwner) : m_pSubsurface(pSubsurface), m_pWindowParent(pOwner) {
|
|
|
|
m_pWLSurface = CWLSurface::create();
|
|
|
|
m_pWLSurface->assign(pSubsurface->surface.lock(), this);
|
2024-02-29 01:03:28 +01:00
|
|
|
initSignals();
|
2024-06-08 10:07:59 +02:00
|
|
|
initExistingSubsurfaces(pSubsurface->surface.lock());
|
2024-02-29 01:03:28 +01:00
|
|
|
}
|
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
CSubsurface::CSubsurface(SP<CWLSubsurfaceResource> pSubsurface, CPopup* pOwner) : m_pSubsurface(pSubsurface), m_pPopupParent(pOwner) {
|
|
|
|
m_pWLSurface = CWLSurface::create();
|
|
|
|
m_pWLSurface->assign(pSubsurface->surface.lock(), this);
|
2024-02-29 15:26:02 +01:00
|
|
|
initSignals();
|
2024-06-08 10:07:59 +02:00
|
|
|
initExistingSubsurfaces(pSubsurface->surface.lock());
|
2024-02-29 15:26:02 +01:00
|
|
|
}
|
|
|
|
|
2024-02-29 01:03:28 +01:00
|
|
|
CSubsurface::~CSubsurface() {
|
|
|
|
hyprListener_newSubsurface.removeCallback();
|
|
|
|
|
|
|
|
if (!m_pSubsurface)
|
|
|
|
return;
|
|
|
|
|
|
|
|
hyprListener_commitSubsurface.removeCallback();
|
|
|
|
hyprListener_destroySubsurface.removeCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSubsurface::initSignals() {
|
|
|
|
if (m_pSubsurface) {
|
2024-06-08 10:07:59 +02:00
|
|
|
listeners.commitSubsurface = m_pSubsurface->surface->events.commit.registerListener([this](std::any d) { onCommit(); });
|
|
|
|
listeners.destroySubsurface = m_pSubsurface->events.destroy.registerListener([this](std::any d) { onDestroy(); });
|
|
|
|
listeners.mapSubsurface = m_pSubsurface->surface->events.map.registerListener([this](std::any d) { onMap(); });
|
|
|
|
listeners.unmapSubsurface = m_pSubsurface->surface->events.unmap.registerListener([this](std::any d) { onUnmap(); });
|
|
|
|
listeners.newSubsurface =
|
|
|
|
m_pSubsurface->surface->events.newSubsurface.registerListener([this](std::any d) { onNewSubsurface(std::any_cast<SP<CWLSubsurfaceResource>>(d)); });
|
2024-02-29 01:03:28 +01:00
|
|
|
} else {
|
2024-06-08 10:07:59 +02:00
|
|
|
if (m_pWindowParent)
|
|
|
|
listeners.newSubsurface = m_pWindowParent->m_pWLSurface->resource()->events.newSubsurface.registerListener(
|
|
|
|
[this](std::any d) { onNewSubsurface(std::any_cast<SP<CWLSubsurfaceResource>>(d)); });
|
2024-02-29 15:26:02 +01:00
|
|
|
else if (m_pPopupParent)
|
2024-06-08 10:07:59 +02:00
|
|
|
listeners.newSubsurface = m_pPopupParent->m_pWLSurface->resource()->events.newSubsurface.registerListener(
|
|
|
|
[this](std::any d) { onNewSubsurface(std::any_cast<SP<CWLSubsurfaceResource>>(d)); });
|
2024-02-29 01:03:28 +01:00
|
|
|
else
|
2024-06-08 10:07:59 +02:00
|
|
|
ASSERT(false);
|
2024-02-29 01:03:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSubsurface::checkSiblingDamage() {
|
|
|
|
if (!m_pParent)
|
|
|
|
return; // ??????????
|
|
|
|
|
2024-05-05 18:16:00 +02:00
|
|
|
const double SCALE = m_pWindowParent.lock() && m_pWindowParent->m_bIsX11 ? 1.0 / m_pWindowParent->m_fX11SurfaceScaledBy : 1.0;
|
2024-02-29 01:03:28 +01:00
|
|
|
|
|
|
|
for (auto& n : m_pParent->m_vChildren) {
|
|
|
|
if (n.get() == this)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto COORDS = n->coordsGlobal();
|
2024-06-08 10:07:59 +02:00
|
|
|
g_pHyprRenderer->damageSurface(n->m_pWLSurface->resource(), COORDS.x, COORDS.y, SCALE);
|
2024-02-29 01:03:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSubsurface::recheckDamageForSubsurfaces() {
|
|
|
|
for (auto& n : m_vChildren) {
|
|
|
|
const auto COORDS = n->coordsGlobal();
|
2024-06-08 10:07:59 +02:00
|
|
|
g_pHyprRenderer->damageSurface(n->m_pWLSurface->resource(), COORDS.x, COORDS.y);
|
2024-02-29 01:03:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSubsurface::onCommit() {
|
|
|
|
// no damaging if it's not visible
|
2024-05-05 18:16:00 +02:00
|
|
|
if (!m_pWindowParent.expired() && (!m_pWindowParent->m_bIsMapped || !m_pWindowParent->m_pWorkspace->m_bVisible)) {
|
2024-06-08 10:07:59 +02:00
|
|
|
m_vLastSize = m_pWLSurface->resource()->current.size;
|
2024-02-29 01:03:28 +01:00
|
|
|
|
2024-03-03 19:39:20 +01:00
|
|
|
static auto PLOGDAMAGE = CConfigValue<Hyprlang::INT>("debug:log_damage");
|
|
|
|
if (*PLOGDAMAGE)
|
2024-04-27 13:43:12 +02:00
|
|
|
Debug::log(LOG, "Refusing to commit damage from a subsurface of {} because it's invisible.", m_pWindowParent.lock());
|
2024-02-29 01:03:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-29 15:26:02 +01:00
|
|
|
const auto COORDS = coordsGlobal();
|
2024-02-29 01:03:28 +01:00
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
g_pHyprRenderer->damageSurface(m_pWLSurface->resource(), COORDS.x, COORDS.y);
|
2024-02-29 01:03:28 +01:00
|
|
|
|
2024-02-29 15:26:02 +01:00
|
|
|
if (m_pPopupParent)
|
|
|
|
m_pPopupParent->recheckTree();
|
2024-04-27 13:43:12 +02:00
|
|
|
if (!m_pWindowParent.expired()) // I hate you firefox why are you doing this
|
2024-05-05 18:16:00 +02:00
|
|
|
m_pWindowParent->m_pPopupHead->recheckTree();
|
2024-02-29 01:03:28 +01:00
|
|
|
|
|
|
|
// I do not think this is correct, but it solves a lot of issues with some apps (e.g. firefox)
|
|
|
|
checkSiblingDamage();
|
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
if (m_vLastSize != m_pWLSurface->resource()->current.size) {
|
2024-02-29 01:03:28 +01:00
|
|
|
CBox box{COORDS, m_vLastSize};
|
|
|
|
g_pHyprRenderer->damageBox(&box);
|
2024-06-08 10:07:59 +02:00
|
|
|
m_vLastSize = m_pWLSurface->resource()->current.size;
|
2024-02-29 01:03:28 +01:00
|
|
|
box = {COORDS, m_vLastSize};
|
|
|
|
g_pHyprRenderer->damageBox(&box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSubsurface::onDestroy() {
|
|
|
|
// destroy children
|
|
|
|
m_vChildren.clear();
|
|
|
|
|
2024-02-29 15:26:02 +01:00
|
|
|
m_bInert = true;
|
|
|
|
|
2024-02-29 01:03:28 +01:00
|
|
|
if (!m_pSubsurface)
|
|
|
|
return; // dummy node, nothing to do, it's the parent dying
|
|
|
|
|
|
|
|
// kill ourselves
|
|
|
|
std::erase_if(m_pParent->m_vChildren, [this](const auto& other) { return other.get() == this; });
|
|
|
|
}
|
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
void CSubsurface::onNewSubsurface(SP<CWLSubsurfaceResource> pSubsurface) {
|
2024-02-29 01:03:28 +01:00
|
|
|
CSubsurface* PSUBSURFACE = nullptr;
|
|
|
|
|
2024-04-27 13:43:12 +02:00
|
|
|
if (!m_pWindowParent.expired())
|
|
|
|
PSUBSURFACE = m_vChildren.emplace_back(std::make_unique<CSubsurface>(pSubsurface, m_pWindowParent.lock())).get();
|
2024-02-29 15:26:02 +01:00
|
|
|
else if (m_pPopupParent)
|
|
|
|
PSUBSURFACE = m_vChildren.emplace_back(std::make_unique<CSubsurface>(pSubsurface, m_pPopupParent)).get();
|
2024-02-29 01:03:28 +01:00
|
|
|
|
|
|
|
ASSERT(PSUBSURFACE);
|
2024-06-08 10:07:59 +02:00
|
|
|
|
|
|
|
PSUBSURFACE->m_pParent = this;
|
2024-02-29 01:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSubsurface::onMap() {
|
2024-06-08 10:07:59 +02:00
|
|
|
m_vLastSize = m_pWLSurface->resource()->current.size;
|
2024-02-29 01:03:28 +01:00
|
|
|
|
|
|
|
const auto COORDS = coordsGlobal();
|
|
|
|
CBox box{COORDS, m_vLastSize};
|
2024-03-14 21:21:58 +01:00
|
|
|
box.expand(4);
|
2024-02-29 01:03:28 +01:00
|
|
|
g_pHyprRenderer->damageBox(&box);
|
|
|
|
|
2024-04-27 13:43:12 +02:00
|
|
|
if (!m_pWindowParent.expired())
|
2024-05-05 18:16:00 +02:00
|
|
|
m_pWindowParent->updateSurfaceScaleTransformDetails();
|
2024-02-29 01:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSubsurface::onUnmap() {
|
|
|
|
const auto COORDS = coordsGlobal();
|
|
|
|
CBox box{COORDS, m_vLastSize};
|
2024-03-14 21:21:58 +01:00
|
|
|
box.expand(4);
|
2024-02-29 01:03:28 +01:00
|
|
|
g_pHyprRenderer->damageBox(&box);
|
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
if (m_pWLSurface->resource() == g_pCompositor->m_pLastFocus)
|
2024-02-29 01:03:28 +01:00
|
|
|
g_pInputManager->releaseAllMouseButtons();
|
|
|
|
|
|
|
|
g_pInputManager->simulateMouseMovement();
|
|
|
|
|
|
|
|
// TODO: should this remove children? Currently it won't, only on .destroy
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D CSubsurface::coordsRelativeToParent() {
|
2024-06-08 10:07:59 +02:00
|
|
|
if (!m_pSubsurface)
|
|
|
|
return {};
|
|
|
|
return m_pSubsurface->posRelativeToParent();
|
2024-02-29 01:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D CSubsurface::coordsGlobal() {
|
|
|
|
Vector2D coords = coordsRelativeToParent();
|
|
|
|
|
2024-04-27 13:43:12 +02:00
|
|
|
if (!m_pWindowParent.expired())
|
2024-05-05 18:16:00 +02:00
|
|
|
coords += m_pWindowParent->m_vRealPosition.value();
|
2024-02-29 15:26:02 +01:00
|
|
|
else if (m_pPopupParent)
|
|
|
|
coords += m_pPopupParent->coordsGlobal();
|
2024-02-29 01:03:28 +01:00
|
|
|
|
|
|
|
return coords;
|
|
|
|
}
|
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
void CSubsurface::initExistingSubsurfaces(SP<CWLSurfaceResource> pSurface) {
|
|
|
|
for (auto& s : pSurface->subsurfaces) {
|
|
|
|
if (!s || s->surface->hlSurface /* already assigned */)
|
|
|
|
continue;
|
|
|
|
onNewSubsurface(s.lock());
|
2024-02-29 01:03:28 +01:00
|
|
|
}
|
2024-02-29 16:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D CSubsurface::size() {
|
2024-06-08 10:07:59 +02:00
|
|
|
return m_pWLSurface->resource()->current.size;
|
2024-02-29 16:07:11 +01:00
|
|
|
}
|
2024-05-05 00:46:10 +02:00
|
|
|
|
|
|
|
bool CSubsurface::visible() {
|
|
|
|
if (!m_pWindowParent.expired())
|
|
|
|
return g_pHyprRenderer->shouldRenderWindow(m_pWindowParent.lock());
|
|
|
|
if (m_pPopupParent)
|
|
|
|
return m_pPopupParent->visible();
|
|
|
|
if (m_pParent)
|
|
|
|
return m_pParent->visible();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|