2016-11-02 19:22:45 +00:00
|
|
|
#include "modules/script.hpp"
|
2021-09-28 19:08:33 +00:00
|
|
|
|
2016-12-03 20:07:40 +00:00
|
|
|
#include "drawtypes/label.hpp"
|
2016-11-20 22:04:31 +00:00
|
|
|
#include "modules/meta/base.inl"
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
namespace modules {
|
2017-01-10 04:12:53 +00:00
|
|
|
script_module::script_module(const bar_settings& bar, string name_)
|
2021-10-02 23:27:11 +00:00
|
|
|
: module<script_module>(bar, move(name_))
|
|
|
|
, m_tail(m_conf.get(name(), "tail", false))
|
|
|
|
, m_interval(m_conf.get<script_runner::interval>(name(), "interval", m_tail ? 0s : 5s))
|
|
|
|
, m_runner([this]() { broadcast(); }, m_conf.get(name(), "exec", ""s), m_conf.get(name(), "exec-if", ""s), m_tail,
|
|
|
|
m_interval, m_conf.get_with_prefix(name(), "env-")) {
|
2017-01-10 04:12:53 +00:00
|
|
|
// Load configured click handlers
|
2016-12-30 22:32:05 +00:00
|
|
|
m_actions[mousebtn::LEFT] = m_conf.get(name(), "click-left", ""s);
|
|
|
|
m_actions[mousebtn::MIDDLE] = m_conf.get(name(), "click-middle", ""s);
|
|
|
|
m_actions[mousebtn::RIGHT] = m_conf.get(name(), "click-right", ""s);
|
2018-11-16 15:06:04 +00:00
|
|
|
m_actions[mousebtn::DOUBLE_LEFT] = m_conf.get(name(), "double-click-left", ""s);
|
|
|
|
m_actions[mousebtn::DOUBLE_MIDDLE] = m_conf.get(name(), "double-click-middle", ""s);
|
|
|
|
m_actions[mousebtn::DOUBLE_RIGHT] = m_conf.get(name(), "double-click-right", ""s);
|
2016-12-30 22:32:05 +00:00
|
|
|
m_actions[mousebtn::SCROLL_UP] = m_conf.get(name(), "scroll-up", ""s);
|
|
|
|
m_actions[mousebtn::SCROLL_DOWN] = m_conf.get(name(), "scroll-down", ""s);
|
2016-12-03 20:07:40 +00:00
|
|
|
|
2017-01-10 04:12:53 +00:00
|
|
|
// Setup formatting
|
|
|
|
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL});
|
2016-12-03 20:07:40 +00:00
|
|
|
if (m_formatter->has(TAG_LABEL)) {
|
2022-02-23 14:01:28 +00:00
|
|
|
m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%output%");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_formatter->add_optional(FORMAT_FAIL, {TAG_LABEL_FAIL});
|
|
|
|
if (m_formatter->has(TAG_LABEL_FAIL)) {
|
|
|
|
m_label_fail = load_optional_label(m_conf, name(), TAG_LABEL_FAIL, "%output%");
|
2016-12-03 20:07:40 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 04:12:53 +00:00
|
|
|
/**
|
|
|
|
* Start the module worker
|
|
|
|
*/
|
2017-01-01 07:58:33 +00:00
|
|
|
void script_module::start() {
|
2021-10-15 08:33:10 +00:00
|
|
|
this->module::start();
|
2017-01-10 04:12:53 +00:00
|
|
|
m_mainthread = thread([&] {
|
2016-12-31 13:08:44 +00:00
|
|
|
try {
|
2021-10-02 23:27:11 +00:00
|
|
|
while (running()) {
|
|
|
|
script_runner::interval sleep_time;
|
|
|
|
if (m_runner.check_condition()) {
|
|
|
|
sleep_time = m_runner.process();
|
2017-01-10 02:01:59 +00:00
|
|
|
} else {
|
2021-10-02 23:27:11 +00:00
|
|
|
m_runner.clear_output();
|
|
|
|
sleep_time = std::max(m_interval, script_runner::interval(1s));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_runner.is_stopping()) {
|
|
|
|
break;
|
2017-01-10 02:01:59 +00:00
|
|
|
}
|
2021-10-02 23:27:11 +00:00
|
|
|
|
|
|
|
sleep(sleep_time);
|
2017-01-01 07:58:33 +00:00
|
|
|
}
|
|
|
|
} catch (const exception& err) {
|
|
|
|
halt(err.what());
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
2017-01-01 07:58:33 +00:00
|
|
|
});
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 04:12:53 +00:00
|
|
|
/**
|
|
|
|
* Stop the module worker by terminating any running commands
|
|
|
|
*/
|
2017-01-01 07:58:33 +00:00
|
|
|
void script_module::stop() {
|
2021-10-02 23:27:11 +00:00
|
|
|
m_runner.stop();
|
2017-01-10 04:12:53 +00:00
|
|
|
wakeup();
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2017-01-10 04:12:53 +00:00
|
|
|
module::stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate module output
|
|
|
|
*/
|
2022-02-23 14:01:28 +00:00
|
|
|
string script_module::get_format() const {
|
|
|
|
if (m_runner.get_exit_status() != 0 && m_conf.has(name(), FORMAT_FAIL)) {
|
|
|
|
return FORMAT_FAIL;
|
|
|
|
}
|
|
|
|
return DEFAULT_FORMAT;
|
|
|
|
}
|
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
string script_module::get_output() {
|
2021-10-02 23:27:11 +00:00
|
|
|
auto script_output = m_runner.get_output();
|
|
|
|
if (script_output.empty()) {
|
2017-01-10 02:01:59 +00:00
|
|
|
return "";
|
2016-12-03 20:07:40 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-12-03 20:07:40 +00:00
|
|
|
if (m_label) {
|
|
|
|
m_label->reset_tokens();
|
2021-10-02 23:27:11 +00:00
|
|
|
m_label->replace_token("%output%", script_output);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-12-03 20:07:40 +00:00
|
|
|
|
2022-02-23 14:01:28 +00:00
|
|
|
if (m_label_fail) {
|
|
|
|
m_label_fail->reset_tokens();
|
|
|
|
m_label_fail->replace_token("%output%", script_output);
|
|
|
|
}
|
|
|
|
|
2021-10-02 23:27:11 +00:00
|
|
|
string cnt{to_string(m_runner.get_counter())};
|
2016-12-05 04:32:10 +00:00
|
|
|
string output{module::get_output()};
|
|
|
|
|
2021-10-02 23:27:11 +00:00
|
|
|
for (const auto& a : m_actions) {
|
|
|
|
auto btn = a.first;
|
|
|
|
auto action = a.second;
|
2018-01-02 12:52:34 +00:00
|
|
|
|
2018-01-02 13:05:14 +00:00
|
|
|
if (!action.empty()) {
|
2018-10-04 14:08:48 +00:00
|
|
|
auto action_replaced = string_util::replace_all(action, "%counter%", cnt);
|
2018-01-02 12:52:34 +00:00
|
|
|
|
2018-01-02 13:05:14 +00:00
|
|
|
/*
|
|
|
|
* The pid token is only for tailed commands.
|
|
|
|
* If the command is not specified or running, replacement is unnecessary as well
|
|
|
|
*/
|
2021-10-02 23:27:11 +00:00
|
|
|
int pid = m_runner.get_pid();
|
|
|
|
if (pid != -1) {
|
|
|
|
action_replaced = string_util::replace_all(action_replaced, "%pid%", to_string(pid));
|
2018-01-02 13:05:14 +00:00
|
|
|
}
|
2020-05-24 16:35:12 +00:00
|
|
|
m_builder->action(btn, action_replaced);
|
2018-01-02 12:52:34 +00:00
|
|
|
}
|
2017-01-01 07:58:33 +00:00
|
|
|
}
|
2016-12-05 04:32:10 +00:00
|
|
|
|
2022-03-14 17:39:34 +00:00
|
|
|
m_builder->node(output);
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
return m_builder->flush();
|
|
|
|
}
|
|
|
|
|
2017-01-10 04:12:53 +00:00
|
|
|
/**
|
|
|
|
* Output format tags
|
|
|
|
*/
|
2016-11-25 12:55:15 +00:00
|
|
|
bool script_module::build(builder* builder, const string& tag) const {
|
2017-01-10 04:12:53 +00:00
|
|
|
if (tag == TAG_LABEL) {
|
2016-12-03 20:07:40 +00:00
|
|
|
builder->node(m_label);
|
2022-02-23 14:01:28 +00:00
|
|
|
} else if (tag == TAG_LABEL_FAIL) {
|
|
|
|
builder->node(m_label_fail);
|
2016-11-02 19:22:45 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-03 20:07:40 +00:00
|
|
|
|
|
|
|
return true;
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
2022-03-14 17:39:34 +00:00
|
|
|
} // namespace modules
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|