1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00
polybar/include/modules/meta/timer_module.hpp

45 lines
837 B
C++
Raw Normal View History

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