summaryrefslogtreecommitdiff
path: root/src/protocols/IdleInhibit.cpp
blob: 9960c00fa7a057bd74caeff3ed41b6fbb1cdfcbc (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
#include "IdleInhibit.hpp"

CIdleInhibitor::CIdleInhibitor(SP<CIdleInhibitorResource> resource_, wlr_surface* surf_) : resource(resource_), surface(surf_) {
    ;
}

CIdleInhibitorResource::CIdleInhibitorResource(SP<CZwpIdleInhibitorV1> resource_, wlr_surface* surface_) : resource(resource_), surface(surface_) {
    hyprListener_surfaceDestroy.initCallback(
        &surface->events.destroy,
        [this](void* owner, void* data) {
            surface = nullptr;
            hyprListener_surfaceDestroy.removeCallback();
            destroySent = true;
            events.destroy.emit();
        },
        this, "CIdleInhibitorResource");

    resource->setOnDestroy([this](CZwpIdleInhibitorV1* p) { PROTO::idleInhibit->removeInhibitor(this); });
    resource->setDestroy([this](CZwpIdleInhibitorV1* p) { PROTO::idleInhibit->removeInhibitor(this); });
}

CIdleInhibitorResource::~CIdleInhibitorResource() {
    hyprListener_surfaceDestroy.removeCallback();
    if (!destroySent)
        events.destroy.emit();
}

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

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

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

    RESOURCE->setDestroy([this](CZwpIdleInhibitManagerV1* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); });
    RESOURCE->setCreateInhibitor(
        [this](CZwpIdleInhibitManagerV1* pMgr, uint32_t id, wl_resource* surface) { this->onCreateInhibitor(pMgr, id, wlr_surface_from_resource(surface)); });
}

void CIdleInhibitProtocol::removeInhibitor(CIdleInhibitorResource* resource) {
    std::erase_if(m_vInhibitors, [resource](const auto& el) { return el.get() == resource; });
}

void CIdleInhibitProtocol::onCreateInhibitor(CZwpIdleInhibitManagerV1* pMgr, uint32_t id, wlr_surface* surface) {
    const auto CLIENT   = pMgr->client();
    const auto RESOURCE = m_vInhibitors.emplace_back(std::make_shared<CIdleInhibitorResource>(std::make_shared<CZwpIdleInhibitorV1>(CLIENT, pMgr->version(), id), surface));

    RESOURCE->inhibitor = std::make_shared<CIdleInhibitor>(RESOURCE, surface);
    events.newIdleInhibitor.emit(RESOURCE->inhibitor);
}