2024-04-29 17:42:07 +01:00
|
|
|
#include "IdleNotify.hpp"
|
|
|
|
|
#include "../managers/eventLoop/EventLoopManager.hpp"
|
|
|
|
|
|
2024-05-05 17:16:00 +01:00
|
|
|
static int onTimer(SP<CEventLoopTimer> self, void* data) {
|
2024-04-29 17:42:07 +01:00
|
|
|
|
2025-08-14 19:44:56 +05:00
|
|
|
const auto NOTIF = sc<CExtIdleNotification*>(data);
|
2024-04-29 17:42:07 +01:00
|
|
|
|
|
|
|
|
NOTIF->onTimerFired();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 09:58:43 -05:00
|
|
|
CExtIdleNotification::CExtIdleNotification(SP<CExtIdleNotificationV1> resource_, uint32_t timeoutMs_, bool obeyInhibitors_) :
|
2025-05-04 00:13:29 +02:00
|
|
|
m_resource(resource_), m_timeoutMs(timeoutMs_), m_obeyInhibitors(obeyInhibitors_) {
|
2025-01-17 18:21:34 +01:00
|
|
|
if UNLIKELY (!resource_->resource())
|
2024-04-29 17:42:07 +01:00
|
|
|
return;
|
|
|
|
|
|
2025-05-04 00:13:29 +02:00
|
|
|
m_resource->setDestroy([this](CExtIdleNotificationV1* r) { PROTO::idle->destroyNotification(this); });
|
|
|
|
|
m_resource->setOnDestroy([this](CExtIdleNotificationV1* r) { PROTO::idle->destroyNotification(this); });
|
2024-04-29 17:42:07 +01:00
|
|
|
|
2025-05-04 00:13:29 +02:00
|
|
|
m_timer = makeShared<CEventLoopTimer>(std::nullopt, onTimer, this);
|
|
|
|
|
g_pEventLoopManager->addTimer(m_timer);
|
2024-04-29 17:42:07 +01:00
|
|
|
|
2025-10-19 07:54:27 -04:00
|
|
|
update();
|
2024-04-29 17:42:07 +01:00
|
|
|
|
2025-12-17 20:08:47 +00:00
|
|
|
LOGM(Log::DEBUG, "Registered idle-notification for {}ms", timeoutMs_);
|
2024-04-29 17:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CExtIdleNotification::~CExtIdleNotification() {
|
2025-05-04 00:13:29 +02:00
|
|
|
g_pEventLoopManager->removeTimer(m_timer);
|
|
|
|
|
m_timer.reset();
|
2024-04-29 17:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CExtIdleNotification::good() {
|
2025-05-04 00:13:29 +02:00
|
|
|
return m_resource->resource();
|
2024-04-29 17:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
2025-10-19 07:54:27 -04:00
|
|
|
void CExtIdleNotification::update(uint32_t elapsedMs) {
|
|
|
|
|
m_timer->updateTimeout(std::nullopt);
|
|
|
|
|
|
|
|
|
|
if (elapsedMs == 0 && PROTO::idle->isInhibited && m_obeyInhibitors) {
|
|
|
|
|
reset();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_timeoutMs > elapsedMs) {
|
|
|
|
|
reset();
|
|
|
|
|
m_timer->updateTimeout(std::chrono::milliseconds(m_timeoutMs - elapsedMs));
|
|
|
|
|
} else
|
|
|
|
|
onTimerFired();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CExtIdleNotification::update() {
|
|
|
|
|
update(0);
|
2024-04-29 17:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CExtIdleNotification::onTimerFired() {
|
2025-10-19 07:54:27 -04:00
|
|
|
if (m_idled)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-05-04 00:13:29 +02:00
|
|
|
m_resource->sendIdled();
|
|
|
|
|
m_idled = true;
|
2024-04-29 17:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
2025-10-19 07:54:27 -04:00
|
|
|
void CExtIdleNotification::reset() {
|
|
|
|
|
if (!m_idled)
|
|
|
|
|
return;
|
2024-04-29 17:42:07 +01:00
|
|
|
|
2025-10-19 07:54:27 -04:00
|
|
|
m_resource->sendResumed();
|
2025-05-04 00:13:29 +02:00
|
|
|
m_idled = false;
|
2024-04-29 17:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-11 09:58:43 -05:00
|
|
|
bool CExtIdleNotification::inhibitorsAreObeyed() const {
|
2025-05-04 00:13:29 +02:00
|
|
|
return m_obeyInhibitors;
|
2025-02-11 09:58:43 -05:00
|
|
|
}
|
|
|
|
|
|
2024-04-29 17:42:07 +01:00
|
|
|
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) {
|
2025-05-04 00:13:29 +02:00
|
|
|
const auto RESOURCE = m_managers.emplace_back(makeUnique<CExtIdleNotifierV1>(client, ver, id)).get();
|
2024-04-29 17:42:07 +01:00
|
|
|
RESOURCE->setOnDestroy([this](CExtIdleNotifierV1* p) { this->onManagerResourceDestroy(p->resource()); });
|
|
|
|
|
|
|
|
|
|
RESOURCE->setDestroy([this](CExtIdleNotifierV1* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); });
|
2025-02-11 09:58:43 -05:00
|
|
|
RESOURCE->setGetIdleNotification(
|
|
|
|
|
[this](CExtIdleNotifierV1* pMgr, uint32_t id, uint32_t timeout, wl_resource* seat) { this->onGetNotification(pMgr, id, timeout, seat, true); });
|
|
|
|
|
RESOURCE->setGetInputIdleNotification(
|
|
|
|
|
[this](CExtIdleNotifierV1* pMgr, uint32_t id, uint32_t timeout, wl_resource* seat) { this->onGetNotification(pMgr, id, timeout, seat, false); });
|
2024-04-29 17:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CIdleNotifyProtocol::onManagerResourceDestroy(wl_resource* res) {
|
2025-05-04 00:13:29 +02:00
|
|
|
std::erase_if(m_managers, [&](const auto& other) { return other->resource() == res; });
|
2024-04-29 17:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CIdleNotifyProtocol::destroyNotification(CExtIdleNotification* notif) {
|
2025-05-04 00:13:29 +02:00
|
|
|
std::erase_if(m_notifications, [&](const auto& other) { return other.get() == notif; });
|
2024-04-29 17:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-11 09:58:43 -05:00
|
|
|
void CIdleNotifyProtocol::onGetNotification(CExtIdleNotifierV1* pMgr, uint32_t id, uint32_t timeout, wl_resource* seat, bool obeyInhibitors) {
|
|
|
|
|
const auto CLIENT = pMgr->client();
|
|
|
|
|
const auto RESOURCE =
|
2025-05-04 00:13:29 +02:00
|
|
|
m_notifications.emplace_back(makeShared<CExtIdleNotification>(makeShared<CExtIdleNotificationV1>(CLIENT, pMgr->version(), id), timeout, obeyInhibitors)).get();
|
2024-04-29 17:42:07 +01:00
|
|
|
|
2025-01-17 18:21:34 +01:00
|
|
|
if UNLIKELY (!RESOURCE->good()) {
|
2024-05-01 19:40:35 +01:00
|
|
|
pMgr->noMemory();
|
2025-05-04 00:13:29 +02:00
|
|
|
m_notifications.pop_back();
|
2024-04-29 17:42:07 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CIdleNotifyProtocol::onActivity() {
|
2025-05-04 00:13:29 +02:00
|
|
|
for (auto const& n : m_notifications) {
|
2025-10-19 07:54:27 -04:00
|
|
|
n->update();
|
2024-04-29 17:42:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CIdleNotifyProtocol::setInhibit(bool inhibited) {
|
|
|
|
|
isInhibited = inhibited;
|
2025-05-04 00:13:29 +02:00
|
|
|
for (auto const& n : m_notifications) {
|
2025-02-11 09:58:43 -05:00
|
|
|
if (n->inhibitorsAreObeyed())
|
2025-10-19 07:54:27 -04:00
|
|
|
n->update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CIdleNotifyProtocol::setTimers(uint32_t elapsedMs) {
|
|
|
|
|
for (auto const& n : m_notifications) {
|
|
|
|
|
n->update(elapsedMs);
|
2024-04-29 17:42:07 +01:00
|
|
|
}
|
2025-02-11 09:58:43 -05:00
|
|
|
}
|