diff --git a/include/aquamarine/backend/Backend.hpp b/include/aquamarine/backend/Backend.hpp index 5c0fe2b..b2c2432 100644 --- a/include/aquamarine/backend/Backend.hpp +++ b/include/aquamarine/backend/Backend.hpp @@ -143,6 +143,7 @@ namespace Aquamarine { } idle; void dispatchIdle(); + void updateIdleTimer(); // struct { diff --git a/src/backend/Backend.cpp b/src/backend/Backend.cpp index fdaad24..503cf63 100644 --- a/src/backend/Backend.cpp +++ b/src/backend/Backend.cpp @@ -174,14 +174,17 @@ std::vector> Aquamarine::CBackend::ge for (auto& i : implementations) { auto pollfds = i->pollFDs(); for (auto& p : pollfds) { + log(AQ_LOG_DEBUG, std::format("backend: poll fd {} for implementation {}", p->fd, backendTypeToName(i->type()))); result.emplace_back(p); } } for (auto& sfd : sessionFDs) { + log(AQ_LOG_DEBUG, std::format("backend: poll fd {} for session", sfd->fd)); result.emplace_back(sfd); } + log(AQ_LOG_DEBUG, std::format("backend: poll fd {} for idle", idle.fd)); result.emplace_back(makeShared(idle.fd, [this]() { dispatchIdle(); })); return result; @@ -224,9 +227,15 @@ const std::vector>& Aquamarine::CBackend::getImplemen void Aquamarine::CBackend::addIdleEvent(SP> fn) { auto r = idle.pending.emplace_back(fn); - // update timerfd + updateIdleTimer(); +} + +void Aquamarine::CBackend::updateIdleTimer() { + uint64_t ADD_NS = idle.pending.empty() ? TIMESPEC_NSEC_PER_SEC * 240ULL /* 240s, 4 mins */ : 0; + timespec now; clock_gettime(CLOCK_MONOTONIC, &now); + timespecAddNs(&now, ADD_NS); itimerspec ts = {.it_value = now}; @@ -246,4 +255,6 @@ void Aquamarine::CBackend::dispatchIdle() { if (i && *i) (*i)(); } + + updateIdleTimer(); } diff --git a/src/backend/Headless.cpp b/src/backend/Headless.cpp index e0c0c0e..fab0017 100644 --- a/src/backend/Headless.cpp +++ b/src/backend/Headless.cpp @@ -121,7 +121,7 @@ bool Aquamarine::CHeadlessBackend::createOutput(const std::string& name) { auto output = SP(new CHeadlessOutput(name.empty() ? std::format("HEADLESS-{}", ++outputIDCounter) : name, self.lock())); outputs.emplace_back(output); output->swapchain = CSwapchain::create(backend->allocator, self.lock()); - output->self = output; + output->self = output; backend->events.newOutput.emit(SP(output)); return true; @@ -147,22 +147,23 @@ void Aquamarine::CHeadlessBackend::dispatchTimers() { } void Aquamarine::CHeadlessBackend::updateTimerFD() { - long long lowest = 42069133769LL; + long long lowestNs = TIMESPEC_NSEC_PER_SEC * 240 /* 240s, 4 mins */; const auto clocknow = std::chrono::steady_clock::now(); + bool any = false; for (auto& t : timers.timers) { - auto delta = std::chrono::duration_cast(t.when - clocknow).count(); + auto delta = std::chrono::duration_cast(t.when - clocknow).count() * 1000 /* µs -> ns */; - if (delta < lowest) - lowest = delta; + if (delta < lowestNs) + lowestNs = delta; } - if (lowest < 0) - lowest = 0; + if (lowestNs < 0) + lowestNs = 0; timespec now; clock_gettime(CLOCK_MONOTONIC, &now); - timespecAddNs(&now, lowest * 1000); // µs -> ns => * 1000 + timespecAddNs(&now, lowestNs); itimerspec ts = {.it_value = now};