Hyprland/src/managers/SessionLockManager.cpp

179 lines
6.4 KiB
C++
Raw Normal View History

2023-02-03 12:58:55 +01:00
#include "SessionLockManager.hpp"
#include "../Compositor.hpp"
#include "../config/ConfigValue.hpp"
2023-02-03 12:58:55 +01:00
static void handleSurfaceMap(void* owner, void* data) {
const auto PSURFACE = (SSessionLockSurface*)owner;
2023-09-06 12:51:36 +02:00
Debug::log(LOG, "SessionLockSurface {:x} mapped", (uintptr_t)PSURFACE);
2023-02-03 12:58:55 +01:00
PSURFACE->mapped = true;
g_pCompositor->focusSurface(PSURFACE->pWlrLockSurface->surface);
const auto PMONITOR = g_pCompositor->getMonitorFromID(PSURFACE->iMonitorID);
if (PMONITOR)
g_pHyprRenderer->damageMonitor(PMONITOR);
}
static void handleSurfaceCommit(void* owner, void* data) {
const auto PSURFACE = (SSessionLockSurface*)owner;
const auto PMONITOR = g_pCompositor->getMonitorFromID(PSURFACE->iMonitorID);
if (PMONITOR)
g_pHyprRenderer->damageMonitor(PMONITOR);
}
static void handleSurfaceDestroy(void* owner, void* data) {
const auto PSURFACE = (SSessionLockSurface*)owner;
2023-09-06 12:51:36 +02:00
Debug::log(LOG, "SessionLockSurface {:x} destroyed", (uintptr_t)PSURFACE);
2023-02-03 12:58:55 +01:00
PSURFACE->hyprListener_commit.removeCallback();
PSURFACE->hyprListener_destroy.removeCallback();
PSURFACE->hyprListener_map.removeCallback();
2023-02-18 02:09:01 +01:00
if (PSURFACE->pWlrLockSurface->surface == g_pCompositor->m_pLastFocus)
g_pCompositor->m_pLastFocus = nullptr;
2023-02-03 12:58:55 +01:00
g_pSessionLockManager->removeSessionLockSurface(PSURFACE);
}
void CSessionLockManager::onNewSessionLock(wlr_session_lock_v1* pWlrLock) {
static auto PALLOWRELOCK = CConfigValue<Hyprlang::INT>("misc:allow_session_lock_restore");
2023-05-13 13:36:36 +02:00
if (m_sSessionLock.active && (!*PALLOWRELOCK || m_sSessionLock.pWlrLock)) {
2023-02-03 12:58:55 +01:00
Debug::log(LOG, "Attempted to lock a locked session!");
wlr_session_lock_v1_destroy(pWlrLock);
return;
}
2023-09-06 12:51:36 +02:00
Debug::log(LOG, "Session got locked by {:x}", (uintptr_t)pWlrLock);
2023-02-03 12:58:55 +01:00
m_sSessionLock.pWlrLock = pWlrLock;
g_pCompositor->m_sSeat.exclusiveClient = wl_resource_get_client(pWlrLock->resource);
m_sSessionLock.hyprListener_newSurface.initCallback(
&pWlrLock->events.new_surface,
[&](void* owner, void* data) {
const auto PSURFACE = &*m_sSessionLock.vSessionLockSurfaces.emplace_back(std::make_unique<SSessionLockSurface>());
const auto PWLRSURFACE = (wlr_session_lock_surface_v1*)data;
const auto PMONITOR = g_pCompositor->getMonitorFromOutput(PWLRSURFACE->output);
if (!PMONITOR) {
m_sSessionLock.vSessionLockSurfaces.pop_back();
return;
}
PSURFACE->pWlrLockSurface = PWLRSURFACE;
PSURFACE->iMonitorID = PMONITOR->ID;
g_pProtocolManager->m_pFractionalScaleProtocolManager->setPreferredScaleForSurface(PSURFACE->pWlrLockSurface->surface, PMONITOR->scale);
2023-02-03 12:58:55 +01:00
wlr_session_lock_surface_v1_configure(PWLRSURFACE, PMONITOR->vecSize.x, PMONITOR->vecSize.y);
2023-06-03 12:20:23 +02:00
PSURFACE->hyprListener_map.initCallback(&PWLRSURFACE->surface->events.map, &handleSurfaceMap, PSURFACE, "SSessionLockSurface");
2023-02-03 12:58:55 +01:00
PSURFACE->hyprListener_destroy.initCallback(&PWLRSURFACE->events.destroy, &handleSurfaceDestroy, PSURFACE, "SSessionLockSurface");
PSURFACE->hyprListener_commit.initCallback(&PWLRSURFACE->surface->events.commit, &handleSurfaceCommit, PSURFACE, "SSessionLockSurface");
},
pWlrLock, "wlr_session_lock_v1");
m_sSessionLock.hyprListener_unlock.initCallback(
&pWlrLock->events.unlock,
[&](void* owner, void* data) {
Debug::log(LOG, "Session Unlocked");
m_sSessionLock.hyprListener_destroy.removeCallback();
m_sSessionLock.hyprListener_newSurface.removeCallback();
m_sSessionLock.hyprListener_unlock.removeCallback();
m_sSessionLock.active = false;
m_sSessionLock.mMonitorsWithoutMappedSurfaceTimers.clear();
2023-02-03 12:58:55 +01:00
g_pCompositor->m_sSeat.exclusiveClient = nullptr;
g_pInputManager->refocus();
for (auto& m : g_pCompositor->m_vMonitors)
g_pHyprRenderer->damageMonitor(m.get());
},
pWlrLock, "wlr_session_lock_v1");
m_sSessionLock.hyprListener_destroy.initCallback(
&pWlrLock->events.destroy,
[&](void* owner, void* data) {
Debug::log(LOG, "Session Lock Abandoned");
m_sSessionLock.hyprListener_destroy.removeCallback();
m_sSessionLock.hyprListener_newSurface.removeCallback();
m_sSessionLock.hyprListener_unlock.removeCallback();
g_pCompositor->m_sSeat.exclusiveClient = nullptr;
2023-02-03 18:06:07 +01:00
g_pCompositor->focusSurface(nullptr);
2023-05-13 13:36:36 +02:00
m_sSessionLock.pWlrLock = nullptr;
2023-02-03 12:58:55 +01:00
for (auto& m : g_pCompositor->m_vMonitors)
g_pHyprRenderer->damageMonitor(m.get());
},
pWlrLock, "wlr_session_lock_v1");
wlr_session_lock_v1_send_locked(pWlrLock);
g_pSessionLockManager->activateLock();
2023-02-03 12:58:55 +01:00
}
bool CSessionLockManager::isSessionLocked() {
return m_sSessionLock.active;
}
SSessionLockSurface* CSessionLockManager::getSessionLockSurfaceForMonitor(uint64_t id) {
2023-02-03 12:58:55 +01:00
for (auto& sls : m_sSessionLock.vSessionLockSurfaces) {
if (sls->iMonitorID == id) {
if (sls->mapped)
return sls.get();
else
return nullptr;
}
}
return nullptr;
}
// We don't want the red screen to flash.
// This violates the protocol a bit, but tries to handle the missing sync between a lock surface beeing created and the red screen beeing drawn.
float CSessionLockManager::getRedScreenAlphaForMonitor(uint64_t id) {
const auto& NOMAPPEDSURFACETIMER = m_sSessionLock.mMonitorsWithoutMappedSurfaceTimers.find(id);
if (NOMAPPEDSURFACETIMER == m_sSessionLock.mMonitorsWithoutMappedSurfaceTimers.end()) {
m_sSessionLock.mMonitorsWithoutMappedSurfaceTimers.emplace(id, CTimer());
m_sSessionLock.mMonitorsWithoutMappedSurfaceTimers[id].reset();
return 0.f;
}
return std::clamp(NOMAPPEDSURFACETIMER->second.getSeconds() - /* delay for screencopy */ 0.5f, 0.f, 1.f);
}
2023-02-03 12:58:55 +01:00
bool CSessionLockManager::isSurfaceSessionLock(wlr_surface* pSurface) {
for (auto& sls : m_sSessionLock.vSessionLockSurfaces) {
if (sls->pWlrLockSurface->surface == pSurface)
return true;
}
return false;
}
void CSessionLockManager::removeSessionLockSurface(SSessionLockSurface* pSLS) {
std::erase_if(m_sSessionLock.vSessionLockSurfaces, [&](const auto& other) { return pSLS == other.get(); });
}
void CSessionLockManager::activateLock() {
m_sSessionLock.active = true;
}