polybar-dwm/include/modules/meta/timer_module.hpp

47 lines
862 B
C++
Raw Normal View History

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;
void start() {
2017-01-12 19:29:08 +00:00
CAST_MOD(Impl)->update();
this->m_mainthread = thread(&timer_module::runner, this);
}
2016-11-20 22:04:31 +00:00
protected:
void runner() {
try {
2016-12-21 22:22:02 +00:00
while (this->running()) {
this->sleep(m_interval);
if (!this->running()) {
break;
}
2016-12-21 22:22:02 +00:00
std::unique_lock<std::mutex> guard(this->m_updatelock);
2016-12-21 22:22:02 +00:00
if (CAST_MOD(Impl)->update()) {
this->broadcast();
}
}
2016-12-21 22:22:02 +00:00
} catch (const exception& err) {
this->halt(err.what());
}
}
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