summaryrefslogtreecommitdiff
path: root/src/protocols
diff options
context:
space:
mode:
authorVaxry <vaxry@vaxry.net>2024-04-29 17:42:07 +0100
committerVaxry <vaxry@vaxry.net>2024-04-29 17:50:07 +0100
commitf2b03e9679bc1a091fecffd98b50a4179b5c7d43 (patch)
treec86033d3436b0354db17e9209633ac7a6594631f /src/protocols
parent86133983a94a6ad52933e0577b6098dd8e98ed8c (diff)
ext-idle-notify: move to new impl
Diffstat (limited to 'src/protocols')
-rw-r--r--src/protocols/IdleNotify.cpp104
-rw-r--r--src/protocols/IdleNotify.hpp55
2 files changed, 159 insertions, 0 deletions
diff --git a/src/protocols/IdleNotify.cpp b/src/protocols/IdleNotify.cpp
new file mode 100644
index 00000000..950fbf06
--- /dev/null
+++ b/src/protocols/IdleNotify.cpp
@@ -0,0 +1,104 @@
+#include "IdleNotify.hpp"
+#include "../managers/eventLoop/EventLoopManager.hpp"
+
+#define LOGM PROTO::idle->protoLog
+
+static int onTimer(std::shared_ptr<CEventLoopTimer> self, void* data) {
+
+ const auto NOTIF = (CExtIdleNotification*)data;
+
+ NOTIF->onTimerFired();
+
+ return 0;
+}
+
+CExtIdleNotification::CExtIdleNotification(SP<CExtIdleNotificationV1> resource_, uint32_t timeoutMs_) : resource(resource_), timeoutMs(timeoutMs_) {
+ if (!resource_->resource())
+ return;
+
+ resource->setDestroy([this](CExtIdleNotificationV1* r) { PROTO::idle->destroyNotification(this); });
+ resource->setOnDestroy([this](CExtIdleNotificationV1* r) { PROTO::idle->destroyNotification(this); });
+
+ timer = std::make_shared<CEventLoopTimer>(std::nullopt, onTimer, this);
+ g_pEventLoopManager->addTimer(timer);
+
+ updateTimer();
+
+ LOGM(LOG, "Registered idle-notification for {}ms", timeoutMs_);
+}
+
+CExtIdleNotification::~CExtIdleNotification() {
+ g_pEventLoopManager->removeTimer(timer);
+ timer.reset();
+}
+
+bool CExtIdleNotification::good() {
+ return resource->resource();
+}
+
+void CExtIdleNotification::updateTimer() {
+ if (PROTO::idle->isInhibited)
+ timer->updateTimeout(std::nullopt);
+ else
+ timer->updateTimeout(std::chrono::milliseconds(timeoutMs));
+}
+
+void CExtIdleNotification::onTimerFired() {
+ resource->sendIdled();
+ idled = true;
+}
+
+void CExtIdleNotification::onActivity() {
+ if (idled)
+ resource->sendResumed();
+
+ idled = false;
+ updateTimer();
+}
+
+CIdleNotifyProtocol::CIdleNotifyProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
+ ;
+}
+
+void CIdleNotifyProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
+ const auto RESOURCE = m_vManagers.emplace_back(std::make_unique<CExtIdleNotifierV1>(client, ver, id)).get();
+ RESOURCE->setOnDestroy([this](CExtIdleNotifierV1* p) { this->onManagerResourceDestroy(p->resource()); });
+
+ RESOURCE->setDestroy([this](CExtIdleNotifierV1* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); });
+ RESOURCE->setGetIdleNotification([this](CExtIdleNotifierV1* pMgr, uint32_t id, uint32_t timeout, wl_resource* seat) { this->onGetNotification(pMgr, id, timeout, seat); });
+}
+
+void CIdleNotifyProtocol::onManagerResourceDestroy(wl_resource* res) {
+ std::erase_if(m_vManagers, [&](const auto& other) { return other->resource() == res; });
+}
+
+void CIdleNotifyProtocol::destroyNotification(CExtIdleNotification* notif) {
+ std::erase_if(m_vNotifications, [&](const auto& other) { return other.get() == notif; });
+}
+
+void CIdleNotifyProtocol::onGetNotification(CExtIdleNotifierV1* pMgr, uint32_t id, uint32_t timeout, wl_resource* seat) {
+ const auto CLIENT = wl_resource_get_client(pMgr->resource());
+ const auto RESOURCE =
+ m_vNotifications
+ .emplace_back(std::make_unique<CExtIdleNotification>(std::make_shared<CExtIdleNotificationV1>(CLIENT, wl_resource_get_version(pMgr->resource()), id), timeout))
+ .get();
+
+ if (!RESOURCE->good()) {
+ wl_resource_post_no_memory(pMgr->resource());
+ m_vNotifications.pop_back();
+ return;
+ }
+}
+
+void CIdleNotifyProtocol::onActivity() {
+ for (auto& n : m_vNotifications) {
+ n->onActivity();
+ }
+}
+
+void CIdleNotifyProtocol::setInhibit(bool inhibited) {
+ isInhibited = inhibited;
+ for (auto& n : m_vNotifications) {
+ n->onActivity();
+ }
+} \ No newline at end of file
diff --git a/src/protocols/IdleNotify.hpp b/src/protocols/IdleNotify.hpp
new file mode 100644
index 00000000..c1c7b62d
--- /dev/null
+++ b/src/protocols/IdleNotify.hpp
@@ -0,0 +1,55 @@
+#pragma once
+
+#include <memory>
+#include <vector>
+#include <unordered_map>
+#include "WaylandProtocol.hpp"
+#include "ext-idle-notify-v1.hpp"
+
+class CEventLoopTimer;
+
+class CExtIdleNotification {
+ public:
+ CExtIdleNotification(SP<CExtIdleNotificationV1> resource_, uint32_t timeoutMs);
+ ~CExtIdleNotification();
+
+ bool good();
+ void onTimerFired();
+ void onActivity();
+
+ private:
+ SP<CExtIdleNotificationV1> resource;
+ uint32_t timeoutMs = 0;
+ std::shared_ptr<CEventLoopTimer> timer;
+
+ bool idled = false;
+
+ void updateTimer();
+};
+
+class CIdleNotifyProtocol : public IWaylandProtocol {
+ public:
+ CIdleNotifyProtocol(const wl_interface* iface, const int& ver, const std::string& name);
+
+ virtual void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id);
+
+ void onActivity();
+ void setInhibit(bool inhibited);
+
+ private:
+ void onManagerResourceDestroy(wl_resource* res);
+ void destroyNotification(CExtIdleNotification* notif);
+ void onGetNotification(CExtIdleNotifierV1* pMgr, uint32_t id, uint32_t timeout, wl_resource* seat);
+
+ bool isInhibited = false;
+
+ //
+ std::vector<UP<CExtIdleNotifierV1>> m_vManagers;
+ std::vector<SP<CExtIdleNotification>> m_vNotifications;
+
+ friend class CExtIdleNotification;
+};
+
+namespace PROTO {
+ inline UP<CIdleNotifyProtocol> idle;
+};