2016-11-02 19:22:45 +00:00
|
|
|
#include "modules/script.hpp"
|
2016-12-03 20:07:40 +00:00
|
|
|
#include "drawtypes/label.hpp"
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
#include "modules/meta/base.inl"
|
|
|
|
#include "modules/meta/event_module.inl"
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
namespace modules {
|
2016-11-20 22:04:31 +00:00
|
|
|
template class module<script_module>;
|
|
|
|
template class event_module<script_module>;
|
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
void script_module::setup() {
|
|
|
|
REQ_CONFIG_VALUE(name(), m_exec, "exec");
|
|
|
|
GET_CONFIG_VALUE(name(), m_tail, "tail");
|
|
|
|
GET_CONFIG_VALUE(name(), m_maxlen, "maxlen");
|
|
|
|
GET_CONFIG_VALUE(name(), m_ellipsis, "ellipsis");
|
|
|
|
|
2016-12-03 20:07:40 +00:00
|
|
|
m_conf.warn_deprecated(
|
|
|
|
name(), "maxlen", "\"format = <label>\" and \"label = %output:0:" + to_string(m_maxlen) + "%\"");
|
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
m_actions[mousebtn::LEFT] = m_conf.get<string>(name(), "click-left", "");
|
|
|
|
m_actions[mousebtn::MIDDLE] = m_conf.get<string>(name(), "click-middle", "");
|
|
|
|
m_actions[mousebtn::RIGHT] = m_conf.get<string>(name(), "click-right", "");
|
|
|
|
m_actions[mousebtn::SCROLL_UP] = m_conf.get<string>(name(), "scroll-up", "");
|
|
|
|
m_actions[mousebtn::SCROLL_DOWN] = m_conf.get<string>(name(), "scroll-down", "");
|
|
|
|
|
2016-12-03 20:07:40 +00:00
|
|
|
m_interval = chrono::duration<double>{m_conf.get<double>(name(), "interval", m_tail ? 0.0 : 5.0)};
|
|
|
|
|
2016-12-04 21:09:57 +00:00
|
|
|
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_OUTPUT, TAG_LABEL});
|
2016-12-03 20:07:40 +00:00
|
|
|
|
|
|
|
if (m_formatter->has(TAG_LABEL)) {
|
|
|
|
m_label = load_optional_label(m_conf, name(), "label", "%output%");
|
|
|
|
} else if (m_formatter->has(TAG_OUTPUT)) {
|
|
|
|
m_log.warn("%s: The format tag <output> is deprecated, use <label> instead", name());
|
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void script_module::stop() {
|
|
|
|
if (m_command && m_command->is_running()) {
|
|
|
|
m_log.warn("%s: Stopping shell command", name());
|
|
|
|
m_command->terminate();
|
|
|
|
}
|
|
|
|
wakeup();
|
|
|
|
event_module::stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void script_module::idle() {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (!m_tail) {
|
2016-11-02 19:22:45 +00:00
|
|
|
sleep(m_interval);
|
2016-11-25 12:55:15 +00:00
|
|
|
} else if (!m_command || !m_command->is_running()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
sleep(m_interval);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool script_module::has_event() {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (!m_tail) {
|
2016-11-02 19:22:45 +00:00
|
|
|
return true;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (!m_command || !m_command->is_running()) {
|
|
|
|
auto exec = string_util::replace_all(m_exec, "%counter%", to_string(++m_counter));
|
|
|
|
m_log.trace("%s: Executing '%s'", name(), exec);
|
|
|
|
|
|
|
|
m_command = command_util::make_command(exec);
|
|
|
|
m_command->exec(false);
|
|
|
|
}
|
|
|
|
} catch (const std::exception& err) {
|
|
|
|
m_log.err("%s: %s", name(), err.what());
|
|
|
|
throw module_error("Failed to execute tail command, stopping module...");
|
|
|
|
}
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
if (!m_command) {
|
2016-11-02 19:22:45 +00:00
|
|
|
return false;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
if ((m_output = m_command->readline()) == m_prev) {
|
2016-11-02 19:22:45 +00:00
|
|
|
return false;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
m_prev = m_output;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool script_module::update() {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (m_tail) {
|
2016-11-02 19:22:45 +00:00
|
|
|
return true;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
auto exec = string_util::replace_all(m_exec, "%counter%", to_string(++m_counter));
|
2016-12-03 19:52:42 +00:00
|
|
|
m_log.info("%s: Executing \"%s\"", name(), exec);
|
2016-11-02 19:22:45 +00:00
|
|
|
m_command = command_util::make_command(exec);
|
|
|
|
m_command->exec();
|
|
|
|
m_command->tail([&](string output) { m_output = output; });
|
|
|
|
} catch (const std::exception& err) {
|
|
|
|
m_log.err("%s: %s", name(), err.what());
|
|
|
|
throw module_error("Failed to execute command, stopping module...");
|
|
|
|
}
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
if (m_output == m_prev) {
|
2016-11-02 19:22:45 +00:00
|
|
|
return false;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
m_prev = m_output;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
string script_module::get_output() {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (m_output.empty()) {
|
2016-11-02 19:22:45 +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();
|
|
|
|
m_label->replace_token("%output%", m_output);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-12-03 20:07:40 +00:00
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
if (m_maxlen > 0 && m_output.length() > m_maxlen) {
|
|
|
|
m_output.erase(m_maxlen);
|
|
|
|
m_output += m_ellipsis ? "..." : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto counter_str = to_string(m_counter);
|
2016-12-05 04:32:10 +00:00
|
|
|
string output{module::get_output()};
|
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
OUTPUT_ACTION(mousebtn::LEFT);
|
|
|
|
OUTPUT_ACTION(mousebtn::MIDDLE);
|
|
|
|
OUTPUT_ACTION(mousebtn::RIGHT);
|
|
|
|
OUTPUT_ACTION(mousebtn::SCROLL_UP);
|
|
|
|
OUTPUT_ACTION(mousebtn::SCROLL_DOWN);
|
2016-12-05 04:32:10 +00:00
|
|
|
|
|
|
|
m_builder->append(output);
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
return m_builder->flush();
|
|
|
|
}
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
bool script_module::build(builder* builder, const string& tag) const {
|
2016-11-02 19:22:45 +00:00
|
|
|
if (tag == TAG_OUTPUT) {
|
|
|
|
builder->node(m_output);
|
2016-12-03 20:07:40 +00:00
|
|
|
} else if (tag == TAG_LABEL) {
|
|
|
|
builder->node(m_label);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|