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-02-29 01:03:28 +01:00
|
|
|
|
|
|
|
static void onNewSubsurface(void* owner, void* data);
|
|
|
|
|
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-04-06 04:09:20 +02:00
|
|
|
initExistingSubsurfaces(pOwner->m_pWLSurface.wlr());
|
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-04-06 04:09:20 +02:00
|
|
|
initExistingSubsurfaces(pOwner->m_sWLSurface.wlr());
|
2024-02-29 15:26:02 +01:00
|
|
|
}
|
|
|
|
|
2024-04-27 13:43:12 +02:00
|
|
|
CSubsurface::CSubsurface(wlr_subsurface* pSubsurface, PHLWINDOW pOwner) : m_pSubsurface(pSubsurface), m_pWindowParent(pOwner) {
|
2024-02-29 16:07:11 +01:00
|
|
|
m_sWLSurface.assign(pSubsurface->surface, this);
|
2024-02-29 01:03:28 +01:00
|
|
|
initSignals();
|
2024-04-06 04:09:20 +02:00
|
|
|
initExistingSubsurfaces(pSubsurface->surface);
|
2024-02-29 01:03:28 +01:00
|
|
|
}
|
|
|
|
|
2024-02-29 15:26:02 +01:00
|
|
|
CSubsurface::CSubsurface(wlr_subsurface* pSubsurface, CPopup* pOwner) : m_pSubsurface(pSubsurface), m_pPopupParent(pOwner) {
|
2024-02-29 16:07:11 +01:00
|
|
|
m_sWLSurface.assign(pSubsurface->surface, this);
|
2024-02-29 15:26:02 +01:00
|
|
|
initSignals();
|
2024-04-06 04:09:20 +02:00
|
|
|
initExistingSubsurfaces(pSubsurface->surface);
|
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;
|
|
|
|
|
2024-04-06 04:09:20 +02:00
|
|
|
m_pSubsurface->data = nullptr;
|
|
|
|
|
2024-02-29 01:03:28 +01:00
|
|
|
hyprListener_commitSubsurface.removeCallback();
|
|
|
|
hyprListener_destroySubsurface.removeCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void onNewSubsurface(void* owner, void* data) {
|
|
|
|
const auto PSUBSURFACE = (CSubsurface*)owner;
|
|
|
|
PSUBSURFACE->onNewSubsurface((wlr_subsurface*)data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void onDestroySubsurface(void* owner, void* data) {
|
|
|
|
const auto PSUBSURFACE = (CSubsurface*)owner;
|
|
|
|
PSUBSURFACE->onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void onCommitSubsurface(void* owner, void* data) {
|
|
|
|
const auto PSUBSURFACE = (CSubsurface*)owner;
|
|
|
|
PSUBSURFACE->onCommit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void onMapSubsurface(void* owner, void* data) {
|
|
|
|
const auto PSUBSURFACE = (CSubsurface*)owner;
|
|
|
|
PSUBSURFACE->onMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void onUnmapSubsurface(void* owner, void* data) {
|
|
|
|
const auto PSUBSURFACE = (CSubsurface*)owner;
|
|
|
|
PSUBSURFACE->onUnmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSubsurface::initSignals() {
|
|
|
|
if (m_pSubsurface) {
|
2024-04-06 04:09:20 +02:00
|
|
|
m_pSubsurface->data = this;
|
2024-02-29 01:03:28 +01:00
|
|
|
hyprListener_commitSubsurface.initCallback(&m_pSubsurface->surface->events.commit, &onCommitSubsurface, this, "CSubsurface");
|
|
|
|
hyprListener_destroySubsurface.initCallback(&m_pSubsurface->events.destroy, &onDestroySubsurface, this, "CSubsurface");
|
|
|
|
hyprListener_newSubsurface.initCallback(&m_pSubsurface->surface->events.new_subsurface, &::onNewSubsurface, this, "CSubsurface");
|
|
|
|
hyprListener_mapSubsurface.initCallback(&m_pSubsurface->surface->events.map, &onMapSubsurface, this, "CSubsurface");
|
2024-02-29 15:26:02 +01:00
|
|
|
hyprListener_unmapSubsurface.initCallback(&m_pSubsurface->surface->events.unmap, &onUnmapSubsurface, this, "CSubsurface");
|
2024-02-29 01:03:28 +01:00
|
|
|
} else {
|
2024-04-27 13:43:12 +02:00
|
|
|
if (!m_pWindowParent.expired())
|
|
|
|
hyprListener_newSubsurface.initCallback(&m_pWindowParent.lock()->m_pWLSurface.wlr()->events.new_subsurface, &::onNewSubsurface, this, "CSubsurface Head");
|
2024-02-29 15:26:02 +01:00
|
|
|
else if (m_pPopupParent)
|
|
|
|
hyprListener_newSubsurface.initCallback(&m_pPopupParent->m_sWLSurface.wlr()->events.new_subsurface, &::onNewSubsurface, this, "CSubsurface Head");
|
2024-02-29 01:03:28 +01:00
|
|
|
else
|
|
|
|
RASSERT(false, "CSubsurface::initSignals empty subsurface");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSubsurface::checkSiblingDamage() {
|
|
|
|
if (!m_pParent)
|
|
|
|
return; // ??????????
|
|
|
|
|
2024-04-27 13:43:12 +02:00
|
|
|
const double SCALE = m_pWindowParent.lock() && m_pWindowParent.lock()->m_bIsX11 ? 1.0 / m_pWindowParent.lock()->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();
|
|
|
|
g_pHyprRenderer->damageSurface(n->m_sWLSurface.wlr(), COORDS.x, COORDS.y, SCALE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSubsurface::recheckDamageForSubsurfaces() {
|
|
|
|
for (auto& n : m_vChildren) {
|
|
|
|
const auto COORDS = n->coordsGlobal();
|
2024-02-29 15:26:02 +01:00
|
|
|
g_pHyprRenderer->damageSurface(n->m_sWLSurface.wlr(), COORDS.x, COORDS.y);
|
2024-02-29 01:03:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSubsurface::onCommit() {
|
|
|
|
// no damaging if it's not visible
|
2024-04-27 13:43:12 +02:00
|
|
|
if (!m_pWindowParent.expired() && (!m_pWindowParent.lock()->m_bIsMapped || !m_pWindowParent.lock()->m_pWorkspace->m_bVisible)) {
|
2024-02-29 01:03:28 +01:00
|
|
|
m_vLastSize = Vector2D{m_sWLSurface.wlr()->current.width, m_sWLSurface.wlr()->current.height};
|
|
|
|
|
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-02-29 15:26:02 +01:00
|
|
|
g_pHyprRenderer->damageSurface(m_sWLSurface.wlr(), 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
|
|
|
|
m_pWindowParent.lock()->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();
|
|
|
|
|
|
|
|
if (m_vLastSize != Vector2D{m_sWLSurface.wlr()->current.width, m_sWLSurface.wlr()->current.height}) {
|
|
|
|
CBox box{COORDS, m_vLastSize};
|
|
|
|
g_pHyprRenderer->damageBox(&box);
|
|
|
|
m_vLastSize = Vector2D{m_sWLSurface.wlr()->current.width, m_sWLSurface.wlr()->current.height};
|
|
|
|
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; });
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSubsurface::onNewSubsurface(wlr_subsurface* pSubsurface) {
|
|
|
|
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
|
|
|
PSUBSURFACE->m_pParent = this;
|
|
|
|
|
|
|
|
ASSERT(PSUBSURFACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSubsurface::onMap() {
|
|
|
|
m_vLastSize = {m_sWLSurface.wlr()->current.width, m_sWLSurface.wlr()->current.height};
|
|
|
|
|
|
|
|
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())
|
|
|
|
m_pWindowParent.lock()->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);
|
|
|
|
|
|
|
|
if (m_sWLSurface.wlr() == g_pCompositor->m_pLastFocus)
|
|
|
|
g_pInputManager->releaseAllMouseButtons();
|
|
|
|
|
|
|
|
g_pInputManager->simulateMouseMovement();
|
|
|
|
|
|
|
|
// TODO: should this remove children? Currently it won't, only on .destroy
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D CSubsurface::coordsRelativeToParent() {
|
|
|
|
Vector2D offset;
|
|
|
|
|
|
|
|
CSubsurface* current = this;
|
|
|
|
|
|
|
|
while (current->m_pParent) {
|
|
|
|
|
|
|
|
offset += {current->m_sWLSurface.wlr()->current.dx, current->m_sWLSurface.wlr()->current.dy};
|
|
|
|
offset += {current->m_pSubsurface->current.x, current->m_pSubsurface->current.y};
|
|
|
|
|
|
|
|
current = current->m_pParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D CSubsurface::coordsGlobal() {
|
|
|
|
Vector2D coords = coordsRelativeToParent();
|
|
|
|
|
2024-04-27 13:43:12 +02:00
|
|
|
if (!m_pWindowParent.expired())
|
|
|
|
coords += m_pWindowParent.lock()->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-04-06 04:09:20 +02:00
|
|
|
void CSubsurface::initExistingSubsurfaces(wlr_surface* pSurface) {
|
2024-02-29 01:03:28 +01:00
|
|
|
wlr_subsurface* wlrSubsurface;
|
2024-04-06 04:09:20 +02:00
|
|
|
wl_list_for_each(wlrSubsurface, &pSurface->current.subsurfaces_below, current.link) {
|
2024-02-29 01:03:28 +01:00
|
|
|
::onNewSubsurface(this, wlrSubsurface);
|
|
|
|
}
|
2024-04-06 04:09:20 +02:00
|
|
|
wl_list_for_each(wlrSubsurface, &pSurface->current.subsurfaces_above, current.link) {
|
2024-02-29 01:03:28 +01:00
|
|
|
::onNewSubsurface(this, wlrSubsurface);
|
|
|
|
}
|
2024-02-29 16:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D CSubsurface::size() {
|
|
|
|
return {m_sWLSurface.wlr()->current.width, m_sWLSurface.wlr()->current.height};
|
|
|
|
}
|