Hyprland/src/managers/input/IdleInhibitor.cpp

102 lines
3.2 KiB
C++
Raw Normal View History

#include "InputManager.hpp"
#include "../../Compositor.hpp"
2024-04-21 16:29:30 +01:00
#include "../../protocols/IdleInhibit.hpp"
2024-04-29 17:42:07 +01:00
#include "../../protocols/IdleNotify.hpp"
#include "../../protocols/core/Compositor.hpp"
2024-04-21 16:29:30 +01:00
void CInputManager::newIdleInhibitor(std::any inhibitor) {
const auto PINHIBIT = m_idleInhibitors.emplace_back(makeUnique<SIdleInhibitor>()).get();
PINHIBIT->inhibitor = std::any_cast<SP<CIdleInhibitor>>(inhibitor);
Debug::log(LOG, "New idle inhibitor registered for surface {:x}", (uintptr_t)PINHIBIT->inhibitor->surface.get());
PINHIBIT->inhibitor->listeners.destroy = PINHIBIT->inhibitor->resource->events.destroy.registerListener([this, PINHIBIT](std::any data) {
std::erase_if(m_idleInhibitors, [PINHIBIT](const auto& other) { return other.get() == PINHIBIT; });
recheckIdleInhibitorStatus();
});
auto WLSurface = CWLSurface::fromResource(PINHIBIT->inhibitor->surface.lock());
if (!WLSurface) {
Debug::log(LOG, "Inhibitor has no HL Surface attached to it, likely meaning it's a non-desktop element. Assuming it's visible.");
PINHIBIT->nonDesktop = true;
recheckIdleInhibitorStatus();
return;
}
PINHIBIT->surfaceDestroyListener = WLSurface->m_events.destroy.registerListener(
[this, PINHIBIT](std::any data) { std::erase_if(m_idleInhibitors, [PINHIBIT](const auto& other) { return other.get() == PINHIBIT; }); });
recheckIdleInhibitorStatus();
}
void CInputManager::recheckIdleInhibitorStatus() {
for (auto const& ii : m_idleInhibitors) {
if (ii->nonDesktop) {
PROTO::idle->setInhibit(true);
return;
}
auto WLSurface = CWLSurface::fromResource(ii->inhibitor->surface.lock());
if (!WLSurface)
2024-04-29 17:42:07 +01:00
continue;
if (WLSurface->visible()) {
2024-04-29 17:42:07 +01:00
PROTO::idle->setInhibit(true);
return;
}
}
2022-10-31 12:26:07 +00:00
// check manual user-set inhibitors
for (auto const& w : g_pCompositor->m_windows) {
if (isWindowInhibiting(w)) {
2024-04-29 17:42:07 +01:00
PROTO::idle->setInhibit(true);
2022-10-31 12:26:07 +00:00
return;
}
}
2022-10-31 12:26:07 +00:00
PROTO::idle->setInhibit(false);
}
2022-10-31 12:26:07 +00:00
bool CInputManager::isWindowInhibiting(const PHLWINDOW& w, bool onlyHl) {
if (w->m_idleInhibitMode == IDLEINHIBIT_ALWAYS)
return true;
if (w->m_idleInhibitMode == IDLEINHIBIT_FOCUS && g_pCompositor->isWindowActive(w))
return true;
if (w->m_idleInhibitMode == IDLEINHIBIT_FULLSCREEN && w->isFullscreen() && w->m_workspace && w->m_workspace->isVisible())
return true;
if (onlyHl)
return false;
for (auto const& ii : m_idleInhibitors) {
if (ii->nonDesktop || !ii->inhibitor)
continue;
bool isInhibiting = false;
w->m_wlSurface->resource()->breadthfirst(
[&ii](SP<CWLSurfaceResource> surf, const Vector2D& pos, void* data) {
if (ii->inhibitor->surface != surf)
return;
auto WLSurface = CWLSurface::fromResource(surf);
if (!WLSurface)
return;
if (WLSurface->visible())
*(bool*)data = true;
},
&isInhibiting);
if (isInhibiting)
return true;
2022-10-31 12:26:07 +00:00
}
return false;
}