2016-11-20 22:04:31 +00: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;
|
|
|
|
|
2016-12-21 08:21:35 +00:00
|
|
|
void start() {
|
|
|
|
this->m_mainthread = thread(&timer_module::runner, this);
|
|
|
|
}
|
2016-11-20 22:04:31 +00:00
|
|
|
|
|
|
|
protected:
|
2016-12-21 08:21:35 +00:00
|
|
|
void runner() {
|
2017-01-27 12:29:10 +00:00
|
|
|
this->m_log.trace("%s: Thread id = %i", this->name(), concurrency_util::thread_id(this_thread::get_id()));
|
2017-01-12 22:33:14 +00:00
|
|
|
|
2017-01-27 12:29:10 +00:00
|
|
|
const auto check = [&]() -> bool {
|
|
|
|
std::unique_lock<std::mutex> guard(this->m_updatelock);
|
|
|
|
return CAST_MOD(Impl)->update();
|
|
|
|
};
|
2017-01-12 22:33:14 +00:00
|
|
|
|
2017-01-27 12:29:10 +00:00
|
|
|
try {
|
2017-01-31 11:38:37 +00:00
|
|
|
// warm up module output before entering the loop
|
|
|
|
check();
|
|
|
|
CAST_MOD(Impl)->broadcast();
|
|
|
|
|
2017-01-27 12:29:10 +00:00
|
|
|
while (this->running()) {
|
|
|
|
if (check()) {
|
|
|
|
CAST_MOD(Impl)->broadcast();
|
2016-12-21 08:21:35 +00:00
|
|
|
}
|
2017-01-27 12:29:10 +00:00
|
|
|
CAST_MOD(Impl)->sleep(m_interval);
|
2016-12-21 08:21:35 +00:00
|
|
|
}
|
2016-12-21 22:22:02 +00:00
|
|
|
} catch (const exception& err) {
|
2017-01-27 12:29:10 +00:00
|
|
|
CAST_MOD(Impl)->halt(err.what());
|
2016-12-21 08:21:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-21 22:22:02 +00:00
|
|
|
|
|
|
|
protected:
|
2016-12-31 02:04:01 +00:00
|
|
|
interval_t m_interval{1.0};
|
2016-11-20 22:04:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
POLYBAR_NS_END
|