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;
|
|
|
|
|
2016-12-21 03:21:35 -05:00
|
|
|
void start() {
|
2017-01-12 14:29:08 -05:00
|
|
|
CAST_MOD(Impl)->update();
|
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:
|
2016-12-21 03:21:35 -05:00
|
|
|
void runner() {
|
|
|
|
try {
|
2016-12-21 17:22:02 -05:00
|
|
|
while (this->running()) {
|
2017-01-12 17:33:14 -05:00
|
|
|
this->sleep(m_interval);
|
|
|
|
|
|
|
|
if (!this->running()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:22:02 -05:00
|
|
|
std::unique_lock<std::mutex> guard(this->m_updatelock);
|
2016-12-21 03:21:35 -05:00
|
|
|
|
2016-12-21 17:22:02 -05:00
|
|
|
if (CAST_MOD(Impl)->update()) {
|
|
|
|
this->broadcast();
|
2016-12-21 03:21:35 -05:00
|
|
|
}
|
|
|
|
}
|
2016-12-21 17:22:02 -05:00
|
|
|
} catch (const exception& err) {
|
2016-12-21 03:21:35 -05:00
|
|
|
this->halt(err.what());
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
POLYBAR_NS_END
|