2021-01-04 09:25:52 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
#include "components/builder.hpp"
|
2016-12-09 08:40:46 +00:00
|
|
|
#include "components/config.hpp"
|
2017-01-11 00:42:55 +00:00
|
|
|
#include "components/logger.hpp"
|
2016-12-22 21:11:03 +00:00
|
|
|
#include "events/signal.hpp"
|
|
|
|
#include "events/signal_emitter.hpp"
|
2019-08-06 17:35:07 +00:00
|
|
|
#include "modules/meta/base.hpp"
|
2021-01-04 09:25:52 +00:00
|
|
|
#include "utils/action_router.hpp"
|
2016-11-20 22:04:31 +00:00
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
|
|
|
namespace modules {
|
2021-01-04 09:25:52 +00:00
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
// module<Impl> public {{{
|
|
|
|
|
|
|
|
template <typename Impl>
|
2016-12-09 08:40:46 +00:00
|
|
|
module<Impl>::module(const bar_settings bar, string name)
|
2017-01-11 00:42:55 +00:00
|
|
|
: m_sig(signal_emitter::make())
|
|
|
|
, m_bar(bar)
|
|
|
|
, m_log(logger::make())
|
|
|
|
, m_conf(config::make())
|
2021-10-03 00:57:49 +00:00
|
|
|
, m_router(make_unique<action_router>())
|
2017-01-11 00:42:55 +00:00
|
|
|
, m_name("module/" + name)
|
2020-05-30 22:09:44 +00:00
|
|
|
, m_name_raw(name)
|
2017-01-11 00:42:55 +00:00
|
|
|
, m_builder(make_unique<builder>(bar))
|
2017-04-02 16:12:07 +00:00
|
|
|
, m_formatter(make_unique<module_formatter>(m_conf, m_name))
|
2021-01-03 01:43:50 +00:00
|
|
|
, m_handle_events(m_conf.get(m_name, "handle-events", true))
|
2021-07-07 19:43:49 +00:00
|
|
|
, m_visible(!m_conf.get(m_name, "hidden", false)) {
|
2021-10-03 00:57:49 +00:00
|
|
|
m_router->register_action(EVENT_MODULE_TOGGLE, [this]() { action_module_toggle(); });
|
|
|
|
m_router->register_action(EVENT_MODULE_SHOW, [this]() { action_module_show(); });
|
|
|
|
m_router->register_action(EVENT_MODULE_HIDE, [this]() { action_module_hide(); });
|
2021-10-02 23:27:11 +00:00
|
|
|
}
|
2016-11-20 22:04:31 +00:00
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
module<Impl>::~module() noexcept {
|
|
|
|
m_log.trace("%s: Deconstructing", name());
|
|
|
|
|
2021-10-02 23:27:11 +00:00
|
|
|
if (running()) {
|
|
|
|
/*
|
|
|
|
* We can't stop in the destructor because we have to call the subclasses which at this point already have been
|
|
|
|
* destructed.
|
|
|
|
*/
|
|
|
|
m_log.err("%s: Module was not stopped before deconstructing.", name());
|
2016-12-19 21:01:37 +00:00
|
|
|
}
|
2016-11-20 22:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
string module<Impl>::name() const {
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
2020-05-30 22:09:44 +00:00
|
|
|
template <typename Impl>
|
|
|
|
string module<Impl>::name_raw() const {
|
|
|
|
return m_name_raw;
|
|
|
|
}
|
|
|
|
|
2020-05-15 17:59:08 +00:00
|
|
|
template <typename Impl>
|
|
|
|
string module<Impl>::type() const {
|
2020-12-13 14:24:31 +00:00
|
|
|
return string(Impl::TYPE);
|
2020-05-15 17:59:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
template <typename Impl>
|
|
|
|
bool module<Impl>::running() const {
|
2016-12-19 21:01:37 +00:00
|
|
|
return static_cast<bool>(m_enabled);
|
2016-11-20 22:04:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 08:33:10 +00:00
|
|
|
template <class Impl>
|
|
|
|
void module<Impl>::start() {
|
|
|
|
m_enabled = true;
|
|
|
|
}
|
|
|
|
|
2019-06-01 23:23:36 +00:00
|
|
|
template <class Impl>
|
|
|
|
void module<Impl>::join() {
|
|
|
|
for (auto&& thread_ : m_threads) {
|
|
|
|
if (thread_.joinable()) {
|
|
|
|
thread_.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m_mainthread.joinable()) {
|
|
|
|
m_mainthread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 15:05:26 +00:00
|
|
|
template <typename Impl>
|
|
|
|
bool module<Impl>::visible() const {
|
|
|
|
return static_cast<bool>(m_visible);
|
|
|
|
}
|
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::stop() {
|
2016-12-23 04:18:58 +00:00
|
|
|
if (!static_cast<bool>(m_enabled)) {
|
2016-11-20 22:04:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_log.info("%s: Stopping", name());
|
2016-12-19 21:01:37 +00:00
|
|
|
m_enabled = false;
|
2016-11-20 22:04:31 +00:00
|
|
|
|
2016-12-19 21:01:37 +00: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 22:04:31 +00:00
|
|
|
{
|
2016-12-19 21:01:37 +00:00
|
|
|
CAST_MOD(Impl)->wakeup();
|
2016-11-20 22:04:31 +00:00
|
|
|
CAST_MOD(Impl)->teardown();
|
|
|
|
|
2017-01-12 15:34:14 +00:00
|
|
|
m_sig.emit(signals::eventqueue::check_state{});
|
2016-11-20 22:04:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::halt(string error_message) {
|
|
|
|
m_log.err("%s: %s", name(), error_message);
|
2020-04-21 22:14:02 +00:00
|
|
|
m_log.notice("Stopping '%s'...", name());
|
2016-11-20 22:04:31 +00:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::teardown() {}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
string module<Impl>::contents() {
|
2016-12-23 04:19:45 +00:00
|
|
|
if (m_changed) {
|
2017-01-01 07:58:33 +00:00
|
|
|
m_log.info("%s: Rebuilding cache", name());
|
2016-12-23 04:19:45 +00:00
|
|
|
m_cache = CAST_MOD(Impl)->get_output();
|
2019-01-12 13:51:54 +00:00
|
|
|
// Make sure builder is really empty
|
|
|
|
m_builder->flush();
|
2019-08-21 22:55:13 +00:00
|
|
|
if (!m_cache.empty()) {
|
|
|
|
// Add a reset tag after the module
|
2020-12-17 19:37:28 +00:00
|
|
|
m_builder->control(tags::controltag::R);
|
2019-08-21 22:55:13 +00:00
|
|
|
m_cache += m_builder->flush();
|
|
|
|
}
|
2016-12-23 04:19:45 +00:00
|
|
|
m_changed = false;
|
|
|
|
}
|
2016-11-20 22:04:31 +00:00
|
|
|
return m_cache;
|
|
|
|
}
|
|
|
|
|
2019-12-02 18:14:26 +00:00
|
|
|
template <typename Impl>
|
2021-01-04 09:25:52 +00:00
|
|
|
bool module<Impl>::input(const string& name, const string& data) {
|
|
|
|
if (!m_router->has_action(name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
m_router->invoke(name, data);
|
|
|
|
} catch (const exception& err) {
|
|
|
|
m_log.err("%s: Failed to handle command '%s' with data '%s' (%s)", this->name(), name, data, err.what());
|
|
|
|
}
|
|
|
|
return true;
|
2019-12-02 18:14:26 +00:00
|
|
|
}
|
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
// }}}
|
|
|
|
// module<Impl> protected {{{
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::broadcast() {
|
2016-12-23 04:19:45 +00:00
|
|
|
m_changed = true;
|
2017-01-12 15:34:14 +00:00
|
|
|
m_sig.emit(signals::eventqueue::notify_change{});
|
2016-11-20 22:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::idle() {
|
2017-01-27 12:29:10 +00:00
|
|
|
if (running()) {
|
|
|
|
CAST_MOD(Impl)->sleep(25ms);
|
|
|
|
}
|
2016-11-20 22:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::sleep(chrono::duration<double> sleep_duration) {
|
2017-01-27 12:29:10 +00:00
|
|
|
if (running()) {
|
|
|
|
std::unique_lock<std::mutex> lck(m_sleeplock);
|
|
|
|
m_sleephandler.wait_for(lck, sleep_duration);
|
|
|
|
}
|
2016-11-20 22:04:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 20:15:25 +00:00
|
|
|
template <typename Impl>
|
|
|
|
template <class Clock, class Duration>
|
|
|
|
void module<Impl>::sleep_until(chrono::time_point<Clock, Duration> point) {
|
|
|
|
if (running()) {
|
|
|
|
std::unique_lock<std::mutex> lck(m_sleeplock);
|
|
|
|
m_sleephandler.wait_until(lck, point);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
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 09:23:54 +00:00
|
|
|
std::lock_guard<std::mutex> guard(m_buildlock);
|
2016-11-20 22:04:31 +00:00
|
|
|
auto format_name = CONST_MOD(Impl).get_format();
|
|
|
|
auto format = m_formatter->get(format_name);
|
2017-01-13 09:54:40 +00:00
|
|
|
bool no_tag_built{true};
|
|
|
|
bool fake_no_tag_built{false};
|
|
|
|
bool tag_built{false};
|
2017-01-13 00:14:50 +00:00
|
|
|
auto mingap = std::max(1_z, format->spacing);
|
2017-01-11 00:42:55 +00:00
|
|
|
size_t start, end;
|
|
|
|
string value{format->value};
|
|
|
|
while ((start = value.find('<')) != string::npos && (end = value.find('>', start)) != string::npos) {
|
|
|
|
if (start > 0) {
|
2017-01-13 09:54:40 +00:00
|
|
|
if (no_tag_built) {
|
|
|
|
// If no module tag has been built we do not want to add
|
|
|
|
// whitespace defined between the format tags, but we do still
|
|
|
|
// want to output other non-tag content
|
|
|
|
auto trimmed = string_util::ltrim(value.substr(0, start), ' ');
|
|
|
|
if (!trimmed.empty()) {
|
|
|
|
fake_no_tag_built = false;
|
|
|
|
m_builder->node(move(trimmed));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_builder->node(value.substr(0, start));
|
|
|
|
}
|
2017-01-11 00:42:55 +00:00
|
|
|
value.erase(0, start);
|
|
|
|
end -= start;
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
string tag{value.substr(start, end + 1)};
|
2017-01-13 09:54:40 +00:00
|
|
|
if (tag.empty()) {
|
|
|
|
continue;
|
|
|
|
} else if (tag[0] == '<' && tag[tag.size() - 1] == '>') {
|
|
|
|
if (!no_tag_built)
|
2016-11-20 22:04:31 +00:00
|
|
|
m_builder->space(format->spacing);
|
2017-01-13 09:54:40 +00:00
|
|
|
else if (fake_no_tag_built)
|
|
|
|
no_tag_built = false;
|
|
|
|
if (!(tag_built = CONST_MOD(Impl).build(m_builder.get(), tag)) && !no_tag_built)
|
2017-01-11 00:42:55 +00:00
|
|
|
m_builder->remove_trailing_space(mingap);
|
2017-01-13 09:54:40 +00:00
|
|
|
if (tag_built)
|
|
|
|
no_tag_built = false;
|
2016-11-20 22:04:31 +00:00
|
|
|
}
|
2017-01-11 00:42:55 +00:00
|
|
|
value.erase(0, tag.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!value.empty()) {
|
|
|
|
m_builder->append(value);
|
2016-11-20 22:04:31 +00:00
|
|
|
}
|
|
|
|
|
2016-12-27 03:03:46 +00:00
|
|
|
return format->decorate(&*m_builder, m_builder->flush());
|
2016-11-20 22:04:31 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 19:43:49 +00:00
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::set_visible(bool value) {
|
|
|
|
m_log.notice("%s: Visibility changed (state=%s)", m_name, value ? "shown" : "hidden");
|
|
|
|
m_visible = value;
|
|
|
|
broadcast();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::action_module_toggle() {
|
|
|
|
set_visible(!m_visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::action_module_show() {
|
|
|
|
set_visible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
void module<Impl>::action_module_hide() {
|
|
|
|
set_visible(false);
|
|
|
|
}
|
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
// }}}
|
2019-08-06 17:35:07 +00:00
|
|
|
} // namespace modules
|
2016-11-20 22:04:31 +00:00
|
|
|
|
|
|
|
POLYBAR_NS_END
|