2016-11-20 17:04:31 -05:00
|
|
|
#include "components/builder.hpp"
|
2016-12-09 03:40:46 -05:00
|
|
|
#include "components/logger.hpp"
|
|
|
|
#include "components/config.hpp"
|
2016-12-22 16:11:03 -05:00
|
|
|
#include "events/signal.hpp"
|
|
|
|
#include "events/signal_emitter.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
2016-12-22 16:11:03 -05:00
|
|
|
namespace sig_ev = signals::eventqueue;
|
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
namespace modules {
|
|
|
|
// module<Impl> public {{{
|
|
|
|
|
|
|
|
template <typename Impl>
|
2016-12-09 03:40:46 -05:00
|
|
|
module<Impl>::module(const bar_settings bar, string name)
|
2016-12-22 16:11:03 -05:00
|
|
|
: m_sig(signal_emitter::make())
|
|
|
|
, m_bar(bar)
|
2016-12-09 03:40:46 -05:00
|
|
|
, m_log(logger::make())
|
|
|
|
, m_conf(config::make())
|
2016-11-20 17:04:31 -05:00
|
|
|
, m_name("module/" + name)
|
|
|
|
, m_builder(make_unique<builder>(bar))
|
|
|
|
, m_formatter(make_unique<module_formatter>(m_conf, m_name)) {}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
module<Impl>::~module() noexcept {
|
|
|
|
m_log.trace("%s: Deconstructing", name());
|
|
|
|
|
|
|
|
for (auto&& thread_ : m_threads) {
|
|
|
|
if (thread_.joinable()) {
|
|
|
|
thread_.join();
|
|
|
|
}
|
|
|
|
}
|
2016-12-19 16:01:37 -05:00
|
|
|
if (m_mainthread.joinable()) {
|
|
|
|
m_mainthread.join();
|
|
|
|
}
|
2016-11-20 17:04:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
string module<Impl>::name() const {
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
bool module<Impl>::running() const {
|
2016-12-19 16:01:37 -05:00
|
|
|
return static_cast<bool>(m_enabled);
|
2016-11-20 17:04:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::stop() {
|
2016-12-22 23:18:58 -05:00
|
|
|
if (!static_cast<bool>(m_enabled)) {
|
2016-11-20 17:04:31 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_log.info("%s: Stopping", name());
|
2016-12-19 16:01:37 -05:00
|
|
|
m_enabled = false;
|
2016-11-20 17:04:31 -05:00
|
|
|
|
2016-12-19 16:01:37 -05:00
|
|
|
std::lock(m_buildlock, m_updatelock);
|
|
|
|
std::lock_guard<std::mutex> guard_a(m_buildlock, std::adopt_lock);
|
|
|
|
std::lock_guard<std::mutex> guard_b(m_updatelock, std::adopt_lock);
|
2016-11-20 17:04:31 -05:00
|
|
|
{
|
2016-12-19 16:01:37 -05:00
|
|
|
CAST_MOD(Impl)->wakeup();
|
2016-11-20 17:04:31 -05:00
|
|
|
CAST_MOD(Impl)->teardown();
|
|
|
|
|
2016-12-22 16:11:03 -05:00
|
|
|
m_sig.emit(sig_ev::process_check{});
|
2016-11-20 17:04:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::halt(string error_message) {
|
|
|
|
m_log.err("%s: %s", name(), error_message);
|
|
|
|
m_log.warn("Stopping '%s'...", name());
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::teardown() {}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
string module<Impl>::contents() {
|
2016-12-22 23:19:45 -05:00
|
|
|
if (m_changed) {
|
|
|
|
m_log.info("Rebuilding cache for '%s'...", name());
|
|
|
|
m_cache = CAST_MOD(Impl)->get_output();
|
|
|
|
m_changed = false;
|
|
|
|
}
|
2016-11-20 17:04:31 -05:00
|
|
|
return m_cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
// module<Impl> protected {{{
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::broadcast() {
|
2016-12-22 23:19:45 -05:00
|
|
|
m_changed = true;
|
2016-12-22 16:11:03 -05:00
|
|
|
m_sig.emit(sig_ev::process_broadcast{});
|
2016-11-20 17:04:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::idle() {
|
|
|
|
CAST_MOD(Impl)->sleep(25ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::sleep(chrono::duration<double> sleep_duration) {
|
|
|
|
std::unique_lock<std::mutex> lck(m_sleeplock);
|
|
|
|
m_sleephandler.wait_for(lck, sleep_duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::wakeup() {
|
|
|
|
m_log.trace("%s: Release sleep lock", name());
|
|
|
|
m_sleephandler.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
string module<Impl>::get_format() const {
|
|
|
|
return DEFAULT_FORMAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
string module<Impl>::get_output() {
|
2016-12-16 04:23:54 -05:00
|
|
|
std::lock_guard<std::mutex> guard(m_buildlock);
|
2016-11-20 17:04:31 -05:00
|
|
|
|
|
|
|
auto format_name = CONST_MOD(Impl).get_format();
|
|
|
|
auto format = m_formatter->get(format_name);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
bool tag_built = true;
|
|
|
|
|
2016-12-02 16:31:45 -05:00
|
|
|
for (auto&& tag : string_util::split(format->value, ' ')) {
|
2016-11-20 17:04:31 -05:00
|
|
|
bool is_blankspace = tag.empty();
|
|
|
|
|
|
|
|
if (tag[0] == '<' && tag[tag.length() - 1] == '>') {
|
|
|
|
if (i > 0)
|
|
|
|
m_builder->space(format->spacing);
|
|
|
|
if (!(tag_built = CONST_MOD(Impl).build(m_builder.get(), tag)) && i > 0)
|
|
|
|
m_builder->remove_trailing_space(format->spacing);
|
|
|
|
if (tag_built)
|
|
|
|
i++;
|
|
|
|
} else if (is_blankspace && tag_built) {
|
|
|
|
m_builder->node(" ");
|
|
|
|
} else if (!is_blankspace) {
|
|
|
|
m_builder->node(tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return format->decorate(m_builder.get(), m_builder->flush());
|
|
|
|
}
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
|
|
|
|
POLYBAR_NS_END
|