aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/managers/SessionLockManager.cpp
blob: d6fc912cd46375850576fee3f2dff4a7c3abdcdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "SessionLockManager.hpp"
#include "../Compositor.hpp"
#include "../config/ConfigValue.hpp"
#include "../protocols/FractionalScale.hpp"

static void handleSurfaceMap(void* owner, void* data) {
    const auto PSURFACE = (SSessionLockSurface*)owner;

    Debug::log(LOG, "SessionLockSurface {:x} mapped", (uintptr_t)PSURFACE);

    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;

    Debug::log(LOG, "SessionLockSurface {:x} destroyed", (uintptr_t)PSURFACE);

    PSURFACE->hyprListener_commit.removeCallback();
    PSURFACE->hyprListener_destroy.removeCallback();
    PSURFACE->hyprListener_map.removeCallback();

    if (PSURFACE->pWlrLockSurface->surface == g_pCompositor->m_pLastFocus)
        g_pCompositor->m_pLastFocus = nullptr;

    g_pSessionLockManager->removeSessionLockSurface(PSURFACE);
}

void CSessionLockManager::onNewSessionLock(wlr_session_lock_v1* pWlrLock) {

    static auto PALLOWRELOCK = CConfigValue<Hyprlang::INT>("misc:allow_session_lock_restore");

    if (m_sSessionLock.active && (!*PALLOWRELOCK || m_sSessionLock.pWlrLock)) {
        Debug::log(LOG, "Attempted to lock a locked session!");
        wlr_session_lock_v1_destroy(pWlrLock);
        return;
    }

    Debug::log(LOG, "Session got locked by {:x}", (uintptr_t)pWlrLock);

    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;

            PROTO::fractional->sendScale(PSURFACE->pWlrLockSurface->surface, PMONITOR->scale);

            wlr_session_lock_surface_v1_configure(PWLRSURFACE, PMONITOR->vecSize.x, PMONITOR->vecSize.y);

            PSURFACE->hyprListener_map.initCallback(&PWLRSURFACE->surface->events.map, &handleSurfaceMap, PSURFACE, "SSessionLockSurface");
            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();

            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;

            g_pCompositor->focusSurface(nullptr);

            m_sSessionLock.pWlrLock = nullptr;

            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();
}

bool CSessionLockManager::isSessionLocked() {
    return m_sSessionLock.active;
}

SSessionLockSurface* CSessionLockManager::getSessionLockSurfaceForMonitor(uint64_t id) {
    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);
}

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(); });

    if (g_pCompositor->m_pLastFocus)
        return;

    for (auto& sls : m_sSessionLock.vSessionLockSurfaces) {
        if (!sls->mapped)
            continue;

        g_pCompositor->focusSurface(sls->pWlrLockSurface->surface);
        break;
    }
}

void CSessionLockManager::activateLock() {
    m_sSessionLock.active = true;
}