diff options
| author | Maximilian Seidler <78690852+PaideiaDilemma@users.noreply.github.com> | 2025-01-19 18:21:36 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-19 18:21:36 +0000 |
| commit | 407453166ce2a52433c7b0b4ee92a41e47ef8f6d (patch) | |
| tree | 441ec15b12061dd89ebf3bffb53b28daf514f547 /src/protocols/LockNotify.cpp | |
| parent | 8dd2cd41fb4c5a5eb6886cc190419b36084cabfa (diff) | |
protocols: add hyprland_lock_notify_v1 implementation (#9092)
Diffstat (limited to 'src/protocols/LockNotify.cpp')
| -rw-r--r-- | src/protocols/LockNotify.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/protocols/LockNotify.cpp b/src/protocols/LockNotify.cpp new file mode 100644 index 00000000..adcd0b2d --- /dev/null +++ b/src/protocols/LockNotify.cpp @@ -0,0 +1,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(std::make_unique<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(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(ERR, "Not sending unlock notification. Not locked!"); + return; + } + + for (auto const& n : m_notifications) { + n->onUnlocked(); + } + + m_isLocked = false; +} |
