summaryrefslogtreecommitdiff
path: root/src/protocols/LockNotify.cpp
blob: 46736ead7aa330fad88d1a633a4b59965dfaeb67 (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
#include "LockNotify.hpp"

CHyprlandLockNotification::CHyprlandLockNotification(SP<CHyprlandLockNotificationV1> resource_) : m_resource(resource_) {
    if UNLIKELY (!m_resource->resource())
        return;

    m_resource->setDestroy([this](CHyprlandLockNotificationV1* r) { PROTO::lockNotify->destroyNotification(this); });
    m_resource->setOnDestroy([this](CHyprlandLockNotificationV1* r) { PROTO::lockNotify->destroyNotification(this); });
}

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

void CHyprlandLockNotification::onLocked() {
    if LIKELY (!m_locked)
        m_resource->sendLocked();

    m_locked = true;
}

void CHyprlandLockNotification::onUnlocked() {
    if LIKELY (m_locked)
        m_resource->sendUnlocked();

    m_locked = false;
}

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

void CLockNotifyProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
    const auto RESOURCE = m_managers.emplace_back(makeUnique<CHyprlandLockNotifierV1>(client, ver, id)).get();
    RESOURCE->setOnDestroy([this](CHyprlandLockNotifierV1* p) { this->onManagerResourceDestroy(p->resource()); });

    RESOURCE->setDestroy([this](CHyprlandLockNotifierV1* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); });
    RESOURCE->setGetLockNotification([this](CHyprlandLockNotifierV1* pMgr, uint32_t id) { this->onGetNotification(pMgr, id); });
}

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

void CLockNotifyProtocol::destroyNotification(CHyprlandLockNotification* notif) {
    std::erase_if(m_notifications, [&](const auto& other) { return other.get() == notif; });
}

void CLockNotifyProtocol::onGetNotification(CHyprlandLockNotifierV1* pMgr, uint32_t id) {
    const auto CLIENT   = pMgr->client();
    const auto RESOURCE = m_notifications.emplace_back(makeShared<CHyprlandLockNotification>(makeShared<CHyprlandLockNotificationV1>(CLIENT, pMgr->version(), id))).get();

    if UNLIKELY (!RESOURCE->good()) {
        pMgr->noMemory();
        m_notifications.pop_back();
        return;
    }

    // Already locked?? Send locked right away
    if UNLIKELY (m_isLocked)
        m_notifications.back()->onLocked();
}

void CLockNotifyProtocol::onLocked() {
    if UNLIKELY (m_isLocked) {
        LOGM(Log::ERR, "Not sending lock notification. Already locked!");
        return;
    }

    for (auto const& n : m_notifications) {
        n->onLocked();
    }

    m_isLocked = true;
}

void CLockNotifyProtocol::onUnlocked() {
    if UNLIKELY (!m_isLocked) {
        LOGM(Log::ERR, "Not sending unlock notification. Not locked!");
        return;
    }

    for (auto const& n : m_notifications) {
        n->onUnlocked();
    }

    m_isLocked = false;
}