aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/protocols/SessionLock.cpp
blob: a33af3c50700c450eb6e06e68bc00ab97b0f26dd (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "SessionLock.hpp"
#include "../Compositor.hpp"

#define LOGM PROTO::sessionLock->protoLog

CSessionLockSurface::CSessionLockSurface(SP<CExtSessionLockSurfaceV1> resource_, wlr_surface* surface_, CMonitor* pMonitor_, WP<CSessionLock> owner_) :
    resource(resource_), sessionLock(owner_), pSurface(surface_), pMonitor(pMonitor_) {
    if (!resource->resource())
        return;

    resource->setDestroy([this](CExtSessionLockSurfaceV1* r) {
        events.destroy.emit();
        PROTO::sessionLock->destroyResource(this);
    });
    resource->setOnDestroy([this](CExtSessionLockSurfaceV1* r) {
        events.destroy.emit();
        PROTO::sessionLock->destroyResource(this);
    });

    resource->setAckConfigure([this](CExtSessionLockSurfaceV1* r, uint32_t serial) { ackdConfigure = true; });

    hyprListener_surfaceCommit.initCallback(
        &pSurface->events.commit,
        [this](void* owner, void* data) {
            if (pSurface->pending.buffer_width <= 0 || pSurface->pending.buffer_height <= 0) {
                LOGM(ERR, "SessionLock attached a null buffer");
                wl_resource_post_error(resource->resource(), EXT_SESSION_LOCK_SURFACE_V1_ERROR_NULL_BUFFER, "Null buffer attached");
                return;
            }

            if (!ackdConfigure) {
                LOGM(ERR, "SessionLock committed without an ack");
                wl_resource_post_error(resource->resource(), EXT_SESSION_LOCK_SURFACE_V1_ERROR_COMMIT_BEFORE_FIRST_ACK, "Committed surface before first ack");
                return;
            }

            if (committed)
                events.commit.emit();
            else {
                wlr_surface_map(pSurface);
                events.map.emit();
            }
            committed = true;
        },
        this, "SessionLockSurface");

    hyprListener_surfaceDestroy.initCallback(
        &pSurface->events.destroy,
        [this](void* owner, void* data) {
            LOGM(WARN, "SessionLockSurface object remains but surface is being destroyed???");
            wlr_surface_unmap(pSurface);
            hyprListener_surfaceCommit.removeCallback();
            hyprListener_surfaceDestroy.removeCallback();

            if (g_pCompositor->m_pLastFocus == pSurface)
                g_pCompositor->m_pLastFocus = nullptr;

            pSurface = nullptr;
        },
        this, "SessionLockSurface");

    sendConfigure();

    listeners.monitorMode = pMonitor->events.modeChanged.registerListener([this](std::any data) { sendConfigure(); });
}

CSessionLockSurface::~CSessionLockSurface() {
    if (pSurface && pSurface->mapped)
        wlr_surface_unmap(pSurface);
    hyprListener_surfaceCommit.removeCallback();
    hyprListener_surfaceDestroy.removeCallback();
    events.destroy.emit(); // just in case.
}

void CSessionLockSurface::sendConfigure() {
    const auto CLIENT = wl_resource_get_client(resource->resource());
    const auto SERIAL = wlr_seat_client_next_serial(wlr_seat_client_for_wl_client(g_pCompositor->m_sSeat.seat, CLIENT));
    resource->sendConfigure(SERIAL, pMonitor->vecSize.x, pMonitor->vecSize.y);
}

bool CSessionLockSurface::good() {
    return resource->resource();
}

bool CSessionLockSurface::inert() {
    return sessionLock.expired();
}

CMonitor* CSessionLockSurface::monitor() {
    return pMonitor;
}

wlr_surface* CSessionLockSurface::surface() {
    return pSurface;
}

CSessionLock::CSessionLock(SP<CExtSessionLockV1> resource_) : resource(resource_) {
    if (!resource->resource())
        return;

    resource->setDestroy([this](CExtSessionLockV1* r) { PROTO::sessionLock->destroyResource(this); });
    resource->setOnDestroy([this](CExtSessionLockV1* r) { PROTO::sessionLock->destroyResource(this); });

    resource->setGetLockSurface([this](CExtSessionLockV1* r, uint32_t id, wl_resource* surf, wl_resource* output) {
        if (inert) {
            LOGM(ERR, "Lock is trying to send getLockSurface after it's inert");
            return;
        }

        PROTO::sessionLock->onGetLockSurface(r, id, surf, output);
    });

    resource->setUnlockAndDestroy([this](CExtSessionLockV1* r) {
        events.unlockAndDestroy.emit();
        inert                      = true;
        PROTO::sessionLock->locked = false;
        PROTO::sessionLock->destroyResource(this);
    });
}

CSessionLock::~CSessionLock() {
    events.destroyed.emit();
}

void CSessionLock::sendLocked() {
    resource->sendLocked();
}

bool CSessionLock::good() {
    return resource->resource();
}

CSessionLockProtocol::CSessionLockProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
    ;
}

void CSessionLockProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
    const auto RESOURCE = m_vManagers.emplace_back(std::make_unique<CExtSessionLockManagerV1>(client, ver, id)).get();
    RESOURCE->setOnDestroy([this](CExtSessionLockManagerV1* p) { this->onManagerResourceDestroy(p->resource()); });

    RESOURCE->setDestroy([this](CExtSessionLockManagerV1* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); });
    RESOURCE->setLock([this](CExtSessionLockManagerV1* pMgr, uint32_t id) { this->onLock(pMgr, id); });
}

void CSessionLockProtocol::onManagerResourceDestroy(wl_resource* res) {
    std::erase_if(m_vManagers, [&](const auto& other) { return other->resource() == res; });
}

void CSessionLockProtocol::destroyResource(CSessionLock* lock) {
    std::erase_if(m_vLocks, [&](const auto& other) { return other.get() == lock; });
}

void CSessionLockProtocol::destroyResource(CSessionLockSurface* surf) {
    std::erase_if(m_vLockSurfaces, [&](const auto& other) { return other.get() == surf; });
}

void CSessionLockProtocol::onLock(CExtSessionLockManagerV1* pMgr, uint32_t id) {

    LOGM(LOG, "New sessionLock with id {}", id);

    const auto CLIENT   = wl_resource_get_client(pMgr->resource());
    const auto RESOURCE = m_vLocks.emplace_back(std::make_unique<CSessionLock>(std::make_shared<CExtSessionLockV1>(CLIENT, wl_resource_get_version(pMgr->resource()), id)));

    if (!RESOURCE->good()) {
        wl_resource_post_no_memory(pMgr->resource());
        m_vLocks.pop_back();
        return;
    }

    if (m_vLocks.size() > 1) {
        LOGM(ERR, "Tried to lock a locked session");
        RESOURCE->resource->sendFinished();
        RESOURCE->inert = true;
        return;
    }

    events.newLock.emit(RESOURCE);

    locked = true;
}

void CSessionLockProtocol::onGetLockSurface(CExtSessionLockV1* lock, uint32_t id, wl_resource* surface, wl_resource* output) {
    LOGM(LOG, "New sessionLockSurface with id {}", id);

    auto             PSURFACE = wlr_surface_from_resource(surface);
    auto             PMONITOR = g_pCompositor->getMonitorFromOutput(wlr_output_from_resource(output));

    SP<CSessionLock> sessionLock;
    for (auto& l : m_vLocks) {
        if (l->resource.get() == lock) {
            sessionLock = l;
            break;
        }
    }

    const auto CLIENT   = wl_resource_get_client(lock->resource());
    const auto RESOURCE = m_vLockSurfaces.emplace_back(
        std::make_unique<CSessionLockSurface>(std::make_shared<CExtSessionLockSurfaceV1>(CLIENT, wl_resource_get_version(lock->resource()), id), PSURFACE, PMONITOR, sessionLock));

    if (!RESOURCE->good()) {
        wl_resource_post_no_memory(lock->resource());
        m_vLockSurfaces.pop_back();
        return;
    }

    sessionLock->events.newLockSurface.emit(RESOURCE);
}

bool CSessionLockProtocol::isLocked() {
    return locked;
}