2016-11-20 17:04:31 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "modules/meta/base.hpp"
|
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
|
|
|
namespace modules {
|
|
|
|
using interval_t = chrono::duration<double>;
|
|
|
|
|
|
|
|
template <class Impl>
|
|
|
|
class timer_module : public module<Impl> {
|
|
|
|
public:
|
|
|
|
using module<Impl>::module;
|
|
|
|
|
2021-01-04 04:38:43 -05:00
|
|
|
void start() override {
|
2021-10-15 04:33:10 -04:00
|
|
|
this->module<Impl>::start();
|
2016-12-21 03:21:35 -05:00
|
|
|
this->m_mainthread = thread(&timer_module::runner, this);
|
|
|
|
}
|
2016-11-20 17:04:31 -05:00
|
|
|
|
|
|
|
protected:
|
2020-12-05 16:58:38 -05:00
|
|
|
/**
|
|
|
|
* Loads and sets the interval for this module.
|
|
|
|
*
|
|
|
|
* Will throw an exception if a non-positive (<= 0) number is given.
|
|
|
|
*/
|
|
|
|
void set_interval(interval_t def) {
|
|
|
|
m_interval = this->m_conf.template get<decltype(m_interval)>(this->name(), "interval", def);
|
|
|
|
|
|
|
|
if (m_interval <= 0s) {
|
|
|
|
throw module_error(
|
|
|
|
this->name() + ": 'interval' must be larger than 0 (got '" + to_string(m_interval.count()) + "s')");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 03:21:35 -05:00
|
|
|
void runner() {
|
2017-01-27 07:29:10 -05:00
|
|
|
this->m_log.trace("%s: Thread id = %i", this->name(), concurrency_util::thread_id(this_thread::get_id()));
|
2017-01-12 17:33:14 -05:00
|
|
|
|
2017-01-27 07:29:10 -05:00
|
|
|
const auto check = [&]() -> bool {
|
|
|
|
std::unique_lock<std::mutex> guard(this->m_updatelock);
|
|
|
|
return CAST_MOD(Impl)->update();
|
|
|
|
};
|
2017-01-12 17:33:14 -05:00
|
|
|
|
2017-01-27 07:29:10 -05:00
|
|
|
try {
|
2017-01-31 06:38:37 -05:00
|
|
|
// warm up module output before entering the loop
|
|
|
|
check();
|
|
|
|
CAST_MOD(Impl)->broadcast();
|
|
|
|
|
2017-01-27 07:29:10 -05:00
|
|
|
while (this->running()) {
|
|
|
|
if (check()) {
|
|
|
|
CAST_MOD(Impl)->broadcast();
|
2016-12-21 03:21:35 -05:00
|
|
|
}
|
2020-06-20 16:15:25 -04:00
|
|
|
// wait until next full interval to avoid drifting clocks
|
2022-01-12 17:06:29 -05:00
|
|
|
using clock = chrono::steady_clock;
|
2020-06-20 16:15:25 -04:00
|
|
|
using sys_duration_t = clock::time_point::duration;
|
|
|
|
|
|
|
|
auto sys_interval = chrono::duration_cast<sys_duration_t>(m_interval);
|
|
|
|
clock::time_point now = clock::now();
|
|
|
|
sys_duration_t adjusted = sys_interval - (now.time_since_epoch() % sys_interval);
|
|
|
|
|
|
|
|
// The seemingly arbitrary addition of 500ms is due
|
|
|
|
// to the fact that if we wait the exact time our
|
|
|
|
// thread will be woken just a tiny bit prematurely
|
|
|
|
// and therefore the wrong time will be displayed.
|
|
|
|
// It is currently unknown why exactly the thread gets
|
|
|
|
// woken prematurely.
|
|
|
|
CAST_MOD(Impl)->sleep_until(now + adjusted + 500ms);
|
2016-12-21 03:21:35 -05:00
|
|
|
}
|
2016-12-21 17:22:02 -05:00
|
|
|
} catch (const exception& err) {
|
2017-01-27 07:29:10 -05:00
|
|
|
CAST_MOD(Impl)->halt(err.what());
|
2016-12-21 03:21:35 -05:00
|
|
|
}
|
|
|
|
}
|
2016-12-21 17:22:02 -05:00
|
|
|
|
|
|
|
protected:
|
2016-12-30 21:04:01 -05:00
|
|
|
interval_t m_interval{1.0};
|
2016-11-20 17:04:31 -05:00
|
|
|
};
|
2020-06-20 16:15:25 -04:00
|
|
|
} // namespace modules
|
2016-11-20 17:04:31 -05:00
|
|
|
|
|
|
|
POLYBAR_NS_END
|