2016-05-31 03:58:58 +00:00
|
|
|
#pragma once
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
#include "modules/meta.hpp"
|
|
|
|
#include "utils/command.hpp"
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
LEMONBUDDY_NS
|
|
|
|
|
2016-10-15 11:15:56 +00:00
|
|
|
#define OUTPUT_ACTION(BUTTON) \
|
|
|
|
if (!m_actions[BUTTON].empty()) \
|
|
|
|
m_builder->cmd(BUTTON, string_util::replace_all(m_actions[BUTTON], "%counter%", counter_str))
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
namespace modules {
|
2016-10-15 11:15:56 +00:00
|
|
|
class script_module : public event_module<script_module> {
|
2016-06-15 03:32:35 +00:00
|
|
|
public:
|
2016-10-15 11:15:56 +00:00
|
|
|
using event_module::event_module;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
void setup() {
|
2016-10-15 11:15:56 +00:00
|
|
|
m_formatter->add(DEFAULT_FORMAT, TAG_OUTPUT, {TAG_OUTPUT});
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-18 23:23:06 +00:00
|
|
|
// Load configuration values
|
|
|
|
|
2016-10-15 11:15:56 +00:00
|
|
|
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-06-15 03:32:35 +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-10-26 10:20:55 +00:00
|
|
|
m_interval = interval_t{m_conf.get<float>(name(), "interval", m_tail ? 0.0f : 2.0f)};
|
2016-10-18 23:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void stop() {
|
2016-10-28 16:51:07 +00:00
|
|
|
if (m_command && m_command->is_running()) {
|
|
|
|
m_log.warn("%s: Stopping shell command", name());
|
|
|
|
m_command->terminate();
|
|
|
|
}
|
2016-10-18 23:23:06 +00:00
|
|
|
wakeup();
|
2016-10-28 16:51:07 +00:00
|
|
|
event_module::stop();
|
2016-10-18 23:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void idle() {
|
|
|
|
if (!enabled())
|
|
|
|
sleep(100ms);
|
|
|
|
if (!m_tail)
|
|
|
|
sleep(m_interval);
|
|
|
|
else if (!m_command || !m_command->is_running())
|
|
|
|
sleep(m_interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_event() {
|
|
|
|
// Non tail commands should always run
|
|
|
|
if (!m_tail)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!m_command || !m_command->is_running()) {
|
2016-10-15 11:15:56 +00:00
|
|
|
auto exec = string_util::replace_all(m_exec, "%counter%", to_string(++m_counter));
|
|
|
|
m_log.trace("%s: Executing '%s'", name(), exec);
|
|
|
|
|
2016-10-28 16:51:07 +00:00
|
|
|
m_command = command_util::make_command(exec);
|
2016-10-15 11:15:56 +00:00
|
|
|
m_command->exec(false);
|
|
|
|
}
|
|
|
|
} catch (const std::exception& err) {
|
|
|
|
m_log.err("%s: %s", name(), err.what());
|
2016-10-19 00:15:52 +00:00
|
|
|
throw module_error("Failed to execute tail command, stopping module...");
|
2016-10-15 11:15:56 +00:00
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-18 23:23:06 +00:00
|
|
|
if (!m_command)
|
2016-10-15 11:15:56 +00:00
|
|
|
return false;
|
2016-10-18 23:23:06 +00:00
|
|
|
|
|
|
|
if ((m_output = m_command->readline()) == m_prev)
|
2016-10-15 11:15:56 +00:00
|
|
|
return false;
|
2016-10-18 23:23:06 +00:00
|
|
|
|
|
|
|
m_prev = m_output;
|
|
|
|
|
|
|
|
return true;
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool update() {
|
2016-10-18 23:23:06 +00:00
|
|
|
// Tailing commands always update
|
2016-10-15 11:15:56 +00:00
|
|
|
if (m_tail)
|
|
|
|
return true;
|
2016-10-18 23:23:06 +00:00
|
|
|
|
2016-10-15 11:15:56 +00:00
|
|
|
try {
|
2016-10-28 16:51:07 +00:00
|
|
|
if (m_command && m_command->is_running()) {
|
|
|
|
m_log.warn("%s: Previous shell command is still running...", name());
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-14 15:18:29 +00:00
|
|
|
|
2016-10-28 16:51:07 +00:00
|
|
|
auto exec = string_util::replace_all(m_exec, "%counter%", to_string(++m_counter));
|
2016-10-15 11:15:56 +00:00
|
|
|
m_log.trace("%s: Executing '%s'", name(), exec);
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-28 16:51:07 +00:00
|
|
|
m_command = command_util::make_command(exec);
|
|
|
|
m_command->exec();
|
|
|
|
m_command->tail([&](string output) { m_output = output; });
|
2016-10-15 11:15:56 +00:00
|
|
|
} catch (const std::exception& err) {
|
|
|
|
m_log.err("%s: %s", name(), err.what());
|
2016-10-19 00:15:52 +00:00
|
|
|
throw module_error("Failed to execute command, stopping module...");
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 23:23:06 +00:00
|
|
|
if (m_output == m_prev)
|
|
|
|
return false;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-18 23:23:06 +00:00
|
|
|
m_prev = m_output;
|
|
|
|
return true;
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
string get_output() {
|
2016-10-15 19:52:50 +00:00
|
|
|
if (m_output.empty())
|
2016-10-15 11:15:56 +00:00
|
|
|
return " ";
|
|
|
|
|
2016-10-18 23:23:06 +00:00
|
|
|
// Truncate output to the defined max length
|
2016-10-15 19:52:50 +00:00
|
|
|
if (m_maxlen > 0 && m_output.length() > m_maxlen) {
|
|
|
|
m_output.erase(m_maxlen);
|
|
|
|
m_output += m_ellipsis ? "..." : "";
|
2016-10-15 11:15:56 +00:00
|
|
|
}
|
2016-06-13 21:41:59 +00:00
|
|
|
|
2016-10-18 23:23:06 +00:00
|
|
|
auto counter_str = to_string(m_counter);
|
2016-10-15 11:15:56 +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-10-15 19:52:50 +00:00
|
|
|
m_builder->node(module::get_output());
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
return m_builder->flush();
|
|
|
|
}
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-10-18 23:26:17 +00:00
|
|
|
bool build(builder* builder, string tag) const {
|
2016-10-19 04:02:57 +00:00
|
|
|
if (tag == TAG_OUTPUT) {
|
|
|
|
builder->node(m_output);
|
|
|
|
return true;
|
|
|
|
} else {
|
2016-06-15 03:32:35 +00:00
|
|
|
return false;
|
2016-10-19 04:02:57 +00:00
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static constexpr auto TAG_OUTPUT = "<output>";
|
|
|
|
|
2016-10-15 11:15:56 +00:00
|
|
|
command_util::command_t m_command;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
string m_exec;
|
|
|
|
bool m_tail = false;
|
2016-10-18 23:23:06 +00:00
|
|
|
interval_t m_interval = 0s;
|
2016-06-15 03:32:35 +00:00
|
|
|
size_t m_maxlen = 0;
|
|
|
|
bool m_ellipsis = true;
|
2016-10-15 11:15:56 +00:00
|
|
|
map<mousebtn, string> m_actions;
|
2016-10-14 15:18:29 +00:00
|
|
|
|
2016-10-15 11:15:56 +00:00
|
|
|
string m_output;
|
|
|
|
string m_prev;
|
|
|
|
int m_counter{0};
|
2016-05-19 14:41:06 +00:00
|
|
|
};
|
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
LEMONBUDDY_NS_END
|