polybar/src/components/controller.cpp

441 lines
11 KiB
C++
Raw Normal View History

#include "components/controller.hpp"
#include "common.hpp"
2016-11-20 22:04:31 +00:00
#include "components/bar.hpp"
2016-11-21 14:07:00 +00:00
#include "components/config.hpp"
#include "components/eventloop.hpp"
#include "components/ipc.hpp"
#include "components/logger.hpp"
#include "events/signal.hpp"
#include "modules/meta/factory.hpp"
2016-12-09 08:40:46 +00:00
#include "utils/command.hpp"
#include "utils/factory.hpp"
2016-12-09 08:40:46 +00:00
#include "utils/inotify.hpp"
2016-11-02 19:22:45 +00:00
#include "utils/process.hpp"
#include "utils/string.hpp"
2016-12-09 08:40:46 +00:00
#include "x11/connection.hpp"
#include "x11/xutils.hpp"
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-11-02 19:22:45 +00:00
using namespace modules;
2016-12-09 08:02:47 +00:00
/**
* Create instance
*/
2016-12-09 08:40:46 +00:00
controller::make_type controller::make(string&& path_confwatch, bool enable_ipc, bool writeback) {
2016-12-09 08:02:47 +00:00
// clang-format off
return factory_util::unique<controller>(
connection::make(),
signal_emitter::make(),
logger::make(),
config::make(),
eventloop::make(),
bar::make(),
2016-12-09 08:40:46 +00:00
enable_ipc ? ipc::make() : ipc::make_type{},
2016-12-14 04:42:46 +00:00
!path_confwatch.empty() ? inotify_util::make_watch(forward<decltype(path_confwatch)>(move(path_confwatch))) : watch_t{},
2016-12-09 08:02:47 +00:00
writeback);
// clang-format on
}
/**
* Construct controller object
*/
controller::controller(connection& conn, signal_emitter& emitter, const logger& logger, const config& config,
2016-12-15 02:30:41 +00:00
unique_ptr<eventloop>&& eventloop, unique_ptr<bar>&& bar, unique_ptr<ipc>&& ipc, watch_t&& confwatch,
bool writeback)
: m_connection(conn)
, m_sig(emitter)
, m_log(logger)
, m_conf(config)
2016-12-14 04:42:46 +00:00
, m_eventloop(forward<decltype(eventloop)>(eventloop))
, m_bar(forward<decltype(bar)>(bar))
, m_ipc(forward<decltype(ipc)>(ipc))
, m_confwatch(forward<decltype(confwatch)>(confwatch))
, m_writeback(writeback) {}
2016-11-20 22:04:31 +00:00
2016-12-09 08:02:47 +00:00
/**
* Deconstruct controller object
*/
2016-11-02 19:22:45 +00:00
controller::~controller() {
if (m_command) {
m_log.info("Terminating running shell command");
m_command.reset();
2016-11-02 19:22:45 +00:00
}
2016-12-01 07:41:49 +00:00
if (!m_writeback) {
m_log.info("Interrupting X event loop");
m_connection.send_dummy_event(m_connection.root());
}
2016-11-02 19:22:45 +00:00
2016-12-01 07:41:49 +00:00
m_log.info("Joining active threads");
for (auto&& thread_ : m_threads) {
if (thread_.joinable()) {
thread_.join();
2016-11-02 19:22:45 +00:00
}
}
m_log.info("Waiting for spawned processes");
2016-11-25 12:55:15 +00:00
while (process_util::notify_childprocess()) {
2016-11-02 19:22:45 +00:00
;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
m_connection.flush();
}
void controller::setup() {
2016-12-14 04:42:46 +00:00
if (!m_writeback) {
m_connection.ensure_event_mask(m_connection.root(), XCB_EVENT_MASK_STRUCTURE_NOTIFY);
}
2016-12-01 07:41:49 +00:00
string bs{m_conf.section()};
m_log.trace("controller: Setup user-defined modules");
2016-12-01 07:41:49 +00:00
for (int i = 0; i < 3; i++) {
alignment align = static_cast<alignment>(i + 1);
string confkey;
2016-11-02 19:22:45 +00:00
if (align == alignment::LEFT) {
confkey = "modules-left";
} else if (align == alignment::CENTER) {
confkey = "modules-center";
} else if (align == alignment::RIGHT) {
confkey = "modules-right";
}
2016-11-02 19:22:45 +00:00
for (auto& module_name : string_util::split(m_conf.get<string>(bs, confkey, ""), ' ')) {
if (module_name.empty()) {
continue;
}
2016-11-02 19:22:45 +00:00
try {
auto type = m_conf.get<string>("module/" + module_name, "type");
if (type == "custom/ipc" && !m_ipc) {
throw application_error("Inter-process messaging needs to be enabled");
}
2016-12-09 08:40:46 +00:00
unique_ptr<module_interface> module{make_module(move(type), m_bar->settings(), module_name)};
2016-11-02 19:22:45 +00:00
module->set_update_cb([&] {
if (m_eventloop && m_running) {
m_sig.emit(enqueue_update{eventloop_t::make_update_evt(false)});
}
});
module->set_stop_cb([&] {
if (m_eventloop && m_running) {
m_sig.emit(enqueue_check{eventloop::make_check_evt()});
}
});
module->setup();
2016-11-02 19:22:45 +00:00
m_eventloop->add_module(align, move(module));
} catch (const std::runtime_error& err) {
m_log.err("Disabling module \"%s\" (reason: %s)", module_name, err.what());
}
}
2016-11-02 19:22:45 +00:00
}
if (!m_eventloop->module_count()) {
throw application_error("No modules created");
2016-11-02 19:22:45 +00:00
}
}
2016-12-03 14:46:48 +00:00
bool controller::run() {
2016-11-02 19:22:45 +00:00
assert(!m_connection.connection_has_error());
m_log.info("Starting application");
m_running = true;
2016-12-14 04:42:46 +00:00
m_sig.attach(this);
2016-12-01 07:41:49 +00:00
if (m_confwatch && !m_writeback) {
m_threads.emplace_back(thread(&controller::wait_for_configwatch, this));
2016-12-01 07:41:49 +00:00
}
if (m_ipc) {
m_threads.emplace_back(thread(&ipc::receive_messages, m_ipc.get()));
}
2016-11-02 19:22:45 +00:00
if (!m_writeback) {
m_threads.emplace_back(thread(&controller::wait_for_xevent, this));
2016-11-02 19:22:45 +00:00
}
if (m_eventloop) {
m_threads.emplace_back(thread(&controller::wait_for_eventloop, this));
2016-11-02 19:22:45 +00:00
}
2016-12-01 07:41:49 +00:00
m_log.trace("controller: Wait for signal");
m_waiting = true;
2016-11-02 19:22:45 +00:00
sigemptyset(&m_waitmask);
sigaddset(&m_waitmask, SIGINT);
sigaddset(&m_waitmask, SIGQUIT);
sigaddset(&m_waitmask, SIGTERM);
sigaddset(&m_waitmask, SIGUSR1);
2016-12-01 07:41:49 +00:00
sigaddset(&m_waitmask, SIGALRM);
2016-11-02 19:22:45 +00:00
2016-12-01 07:41:49 +00:00
int caught_signal = 0;
sigwait(&m_waitmask, &caught_signal);
2016-11-02 19:22:45 +00:00
2016-12-01 07:41:49 +00:00
m_running = false;
m_waiting = false;
2016-11-02 19:22:45 +00:00
2016-12-01 07:41:49 +00:00
if (caught_signal == SIGUSR1) {
m_reload = true;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
2016-12-01 07:41:49 +00:00
m_log.warn("Termination signal received, shutting down...");
m_log.trace("controller: Caught signal %d", caught_signal);
2016-11-02 19:22:45 +00:00
2016-12-14 04:42:46 +00:00
m_sig.detach(this);
2016-12-01 07:41:49 +00:00
if (m_eventloop) {
2016-12-14 04:42:46 +00:00
// Signal the eventloop, in case it's still running
m_eventloop->enqueue(eventloop::make_quit_evt(false));
2016-12-01 07:41:49 +00:00
m_log.trace("controller: Stopping event loop");
m_eventloop->stop();
2016-11-25 12:55:15 +00:00
}
2016-12-14 04:42:46 +00:00
if (m_ipc) {
m_ipc.reset();
}
2016-12-01 07:41:49 +00:00
if (!m_writeback && m_confwatch) {
m_log.trace("controller: Removing config watch");
m_confwatch->remove(true);
2016-11-02 19:22:45 +00:00
}
2016-12-01 07:41:49 +00:00
return !m_running && !m_reload;
2016-11-02 19:22:45 +00:00
}
const bar_settings controller::opts() const {
return m_bar->settings();
}
2016-12-01 07:41:49 +00:00
void controller::wait_for_configwatch() {
try {
m_log.trace("controller: Attach config watch");
m_confwatch->attach(IN_MODIFY);
2016-11-02 19:22:45 +00:00
2016-12-01 07:41:49 +00:00
m_log.trace("controller: Wait for config file inotify event");
if (m_confwatch->await_match() && m_running) {
m_log.info("Configuration file changed");
kill(getpid(), SIGUSR1);
}
} catch (const system_error& err) {
m_log.err(err.what());
m_log.trace("controller: Reset config watch");
m_confwatch.reset();
2016-11-02 19:22:45 +00:00
}
}
void controller::wait_for_xevent() {
m_log.trace("controller: Listen for X events");
m_connection.flush();
while (m_running) {
try {
auto evt = m_connection.wait_for_event();
if (evt && m_running) {
2016-11-02 19:22:45 +00:00
m_connection.dispatch_event(evt);
}
} catch (xpp::connection_error& err) {
m_log.err("X connection error, terminating... (what: %s)", m_connection.error_str(err.code()));
} catch (const exception& err) {
2016-11-02 19:22:45 +00:00
m_log.err("Error in X event loop: %s", err.what());
}
if (m_connection.connection_has_error()) {
break;
}
}
if (m_running) {
kill(getpid(), SIGTERM);
2016-11-02 19:22:45 +00:00
}
}
2016-12-01 07:41:49 +00:00
void controller::wait_for_eventloop() {
m_eventloop->start();
this_thread::sleep_for(std::chrono::milliseconds{250});
2016-12-01 07:41:49 +00:00
if (m_running) {
m_log.trace("controller: eventloop ended, raising SIGALRM");
kill(getpid(), SIGALRM);
}
}
bool controller::on(const sig_ev::process_update& evt) {
if (!m_bar) {
return false;
}
2016-11-21 14:07:00 +00:00
const bar_settings& bar{m_bar->settings()};
2016-11-25 12:55:15 +00:00
string contents;
2016-11-21 14:07:00 +00:00
string separator{bar.separator};
string padding_left(bar.padding.left, ' ');
string padding_right(bar.padding.right, ' ');
string margin_left(bar.module_margin.left, ' ');
string margin_right(bar.module_margin.right, ' ');
2016-11-02 19:22:45 +00:00
for (const auto& block : m_eventloop->modules()) {
string block_contents;
bool is_left = false;
bool is_center = false;
bool is_right = false;
2016-11-25 12:55:15 +00:00
if (block.first == alignment::LEFT) {
2016-11-02 19:22:45 +00:00
is_left = true;
2016-11-25 12:55:15 +00:00
} else if (block.first == alignment::CENTER) {
2016-11-02 19:22:45 +00:00
is_center = true;
2016-11-25 12:55:15 +00:00
} else if (block.first == alignment::RIGHT) {
2016-11-02 19:22:45 +00:00
is_right = true;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
for (const auto& module : block.second) {
string module_contents{module->contents()};
2016-11-02 19:22:45 +00:00
2016-11-25 12:55:15 +00:00
if (module_contents.empty()) {
2016-11-02 19:22:45 +00:00
continue;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
if (!block_contents.empty() && !margin_right.empty()) {
block_contents += margin_right;
}
2016-11-25 12:55:15 +00:00
if (!block_contents.empty() && !separator.empty()) {
2016-11-02 19:22:45 +00:00
block_contents += separator;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
if (!block_contents.empty() && !margin_left.empty() && !(is_left && module == block.second.front())) {
block_contents += margin_left;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
block_contents += module_contents;
2016-11-02 19:22:45 +00:00
}
2016-11-25 12:55:15 +00:00
if (block_contents.empty()) {
2016-11-02 19:22:45 +00:00
continue;
} else if (is_left) {
2016-11-02 19:22:45 +00:00
contents += "%{l}";
contents += padding_left;
} else if (is_center) {
contents += "%{c}";
} else if (is_right) {
contents += "%{r}";
block_contents += padding_right;
}
// Strip unnecessary reset tags
block_contents = string_util::replace_all(block_contents, "T-}%{T", "T");
2016-11-02 19:22:45 +00:00
block_contents = string_util::replace_all(block_contents, "B-}%{B#", "B#");
block_contents = string_util::replace_all(block_contents, "F-}%{F#", "F#");
block_contents = string_util::replace_all(block_contents, "U-}%{U#", "U#");
block_contents = string_util::replace_all(block_contents, "u-}%{u#", "u#");
block_contents = string_util::replace_all(block_contents, "o-}%{o#", "o#");
// Join consecutive tags
2016-11-02 19:22:45 +00:00
contents += string_util::replace_all(block_contents, "}%{", " ");
}
try {
if (!m_writeback) {
2016-12-16 09:23:54 +00:00
m_bar->parse(move(contents), force);
} else {
std::cout << contents << std::endl;
}
} catch (const exception& err) {
m_log.err("Failed to update bar contents (reason: %s)", err.what());
2016-11-04 17:54:33 +00:00
}
2016-12-03 21:54:58 +00:00
return true;
}
bool controller::on(const sig_ev::process_input& evt) {
2016-12-03 21:54:58 +00:00
try {
2016-12-16 09:23:54 +00:00
string input{*evt.data()};
if (m_command) {
m_log.warn("Terminating previous shell command");
m_command->terminate();
}
m_log.info("Executing shell command: %s", input);
2016-12-09 08:40:46 +00:00
m_command = command_util::make_command(move(input));
m_command->exec();
m_command.reset();
} catch (const application_error& err) {
m_log.err("controller: Error while forwarding input to shell -> %s", err.what());
}
return true;
}
bool controller::on(const sig_ev::process_quit&) {
kill(getpid(), SIGUSR1);
return false;
}
bool controller::on(const sig_ui::button_press& evt) {
if (!m_eventloop) {
return false;
}
2016-12-16 09:23:54 +00:00
string input{*evt.data()};
2016-12-14 04:42:46 +00:00
if (input.empty()) {
m_log.err("Cannot enqueue empty input");
return false;
}
2016-12-14 04:42:46 +00:00
return m_sig.emit(enqueue_input{move(input)});
}
bool controller::on(const sig_ipc::process_action& evt) {
2016-12-16 09:23:54 +00:00
ipc_action a{*evt.data()};
string action{a.payload};
action.erase(0, strlen(ipc_action::prefix));
2016-12-14 04:42:46 +00:00
if (action.empty()) {
m_log.err("Cannot enqueue empty ipc action");
2016-12-14 04:42:46 +00:00
return false;
2016-12-03 21:54:58 +00:00
}
2016-12-14 04:42:46 +00:00
m_log.info("Enqueuing ipc action: %s", action);
return m_sig.emit(enqueue_input{move(action)});
}
bool controller::on(const sig_ipc::process_command& evt) {
2016-12-16 09:23:54 +00:00
ipc_command c{*evt.data()};
string command{c.payload};
command.erase(0, strlen(ipc_command::prefix));
if (command.empty()) {
return false;
}
if (command == "quit") {
m_eventloop->enqueue(eventloop::make_quit_evt(false));
} else if (command == "restart") {
m_eventloop->enqueue(eventloop::make_quit_evt(true));
} else {
m_log.warn("\"%s\" is not a valid ipc command", command);
}
return true;
}
bool controller::on(const sig_ipc::process_hook& evt) {
2016-12-16 09:23:54 +00:00
const ipc_hook hook{*evt.data()};
for (const auto& block : m_eventloop->modules()) {
for (const auto& module : block.second) {
auto ipc = dynamic_cast<ipc_module*>(module.get());
if (ipc != nullptr) {
ipc->on_message(hook);
}
}
}
return true;
2016-11-02 19:22:45 +00:00
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END