polybar-dwm/src/components/eventloop.cpp

349 lines
7.8 KiB
C++
Raw Normal View History

2016-12-01 07:41:49 +00:00
#include <csignal>
2016-11-02 19:22:45 +00:00
#include "components/eventloop.hpp"
2016-11-20 22:04:31 +00:00
#include "components/types.hpp"
2016-12-01 07:41:49 +00:00
#include "components/signals.hpp"
2016-11-02 19:22:45 +00:00
#include "utils/string.hpp"
2016-11-20 22:04:31 +00:00
#include "utils/time.hpp"
#include "x11/color.hpp"
2016-11-02 19:22:45 +00:00
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-11-02 19:22:45 +00:00
/**
* Construct eventloop instance
*/
eventloop::eventloop(const logger& logger, const config& config) : m_log(logger), m_conf(config) {
m_delayed_time = duration_t{m_conf.get<double>("settings", "eventloop-delayed-time", 25)};
m_swallow_time = duration_t{m_conf.get<double>("settings", "eventloop-swallow-time", 10)};
m_swallow_limit = m_conf.get<size_t>("settings", "eventloop-swallow", 5U);
2016-12-01 07:41:49 +00:00
g_signals::event::enqueue = bind(&eventloop::enqueue, this, placeholders::_1);
g_signals::event::enqueue_delayed = bind(&eventloop::enqueue_delayed, this, placeholders::_1);
}
2016-11-02 19:22:45 +00:00
/**
* Deconstruct eventloop
*/
2016-12-01 07:41:49 +00:00
eventloop::~eventloop() {
g_signals::event::enqueue = g_signals::noop<const eventloop::entry_t&>;
g_signals::event::enqueue_delayed = g_signals::noop<const eventloop::entry_t&>;
m_update_cb = nullptr;
m_unrecognized_input_cb = nullptr;
if (m_delayed_thread.joinable()) {
m_delayed_thread.join();
}
2016-12-01 07:41:49 +00:00
if (m_queue_thread.joinable()) {
m_queue_thread.join();
}
2016-11-02 19:22:45 +00:00
for (auto&& block : m_modules) {
for (auto&& module : block.second) {
auto module_name = module->name();
2016-11-20 22:04:31 +00:00
auto cleanup_ms = time_util::measure([&module] {
2016-11-02 19:22:45 +00:00
module->stop();
module.reset();
});
2016-11-22 00:22:47 +00:00
m_log.trace("eventloop: Deconstruction of %s took %lu microsec.", module_name, cleanup_ms);
2016-11-02 19:22:45 +00:00
}
}
}
/**
* Start module and worker threads
2016-11-02 19:22:45 +00:00
*/
void eventloop::start() {
m_log.info("Starting event loop");
m_running = true;
2016-11-02 19:22:45 +00:00
dispatch_modules();
2016-11-02 19:22:45 +00:00
m_queue_thread = thread(&eventloop::dispatch_queue_worker, this);
m_delayed_thread = thread(&eventloop::dispatch_delayed_worker, this);
2016-11-02 19:22:45 +00:00
}
/**
* Wait for worker threads to end
2016-11-02 19:22:45 +00:00
*/
void eventloop::wait() {
if (m_queue_thread.joinable()) {
m_queue_thread.join();
}
}
2016-11-02 19:22:45 +00:00
/**
* Stop main loop by enqueuing a QUIT event
*/
void eventloop::stop() {
m_log.info("Stopping event loop");
m_running = false;
2016-11-02 19:22:45 +00:00
if (m_delayed_thread.joinable()) {
m_delayed_cond.notify_one();
}
2016-11-02 19:22:45 +00:00
enqueue({static_cast<uint8_t>(event_type::QUIT)});
}
2016-11-02 19:22:45 +00:00
/**
* Enqueue event
*/
bool eventloop::enqueue(const entry_t& entry) {
if (m_queue.enqueue(entry)) {
return true;
}
m_log.warn("Failed to enqueue event (%d)", entry.type);
return false;
}
2016-11-02 19:22:45 +00:00
/**
* Delay enqueue by given time
*/
bool eventloop::enqueue_delayed(const entry_t& entry) {
if (!m_delayed_lock.try_lock()) {
return false;
}
2016-11-02 19:22:45 +00:00
std::unique_lock<std::mutex> guard(m_delayed_lock, std::adopt_lock);
2016-11-02 19:22:45 +00:00
if (m_delayed_entry.type != 0) {
return false;
2016-11-02 19:22:45 +00:00
}
m_delayed_entry = entry;
2016-11-02 19:22:45 +00:00
if (m_queue.enqueue(entry)) {
return true;
}
m_delayed_entry.type = 0;
return false;
2016-11-02 19:22:45 +00:00
}
/**
* Set callback handler for UPDATE events
*/
void eventloop::set_update_cb(callback<>&& cb) {
m_update_cb = forward<decltype(cb)>(cb);
}
/**
* Set callback handler for raw INPUT events
*/
void eventloop::set_input_db(callback<string>&& cb) {
m_unrecognized_input_cb = forward<decltype(cb)>(cb);
}
/**
* Add module to alignment block
*/
void eventloop::add_module(const alignment pos, module_t&& module) {
2016-11-25 12:55:15 +00:00
auto it = m_modules.lower_bound(pos);
2016-11-02 19:22:45 +00:00
if (it != m_modules.end() && !(m_modules.key_comp()(pos, it->first))) {
it->second.emplace_back(forward<module_t>(module));
} else {
vector<module_t> vec;
vec.emplace_back(forward<module_t>(module));
m_modules.insert(it, modulemap_t::value_type(pos, move(vec)));
}
}
/**
* Get reference to module map
*/
const modulemap_t& eventloop::modules() const {
2016-11-02 19:22:45 +00:00
return m_modules;
}
/**
* Get loaded module count
2016-11-02 19:22:45 +00:00
*/
size_t eventloop::module_count() const {
size_t count{0};
2016-11-02 19:22:45 +00:00
for (auto&& block : m_modules) {
count += block.second.size();
}
return count;
}
/**
* Start module threads
*/
void eventloop::dispatch_modules() {
for (const auto& block : m_modules) {
for (const auto& module : block.second) {
2016-11-02 19:22:45 +00:00
try {
m_log.info("Starting %s", module->name());
module->start();
} catch (const application_error& err) {
m_log.err("Failed to start '%s' (reason: %s)", module->name(), err.what());
}
}
}
}
/**
* Dispatch queue worker thread
*/
void eventloop::dispatch_queue_worker() {
while (m_running) {
entry_t evt, next{static_cast<uint8_t>(event_type::NONE)};
m_queue.wait_dequeue(evt);
if (!m_running) {
break;
}
if (m_delayed_entry.type != 0 && compare_events(evt, m_delayed_entry)) {
m_delayed_cond.notify_one();
}
size_t swallowed{0};
while (swallowed++ < m_swallow_limit && m_queue.wait_dequeue_timed(next, m_swallow_time)) {
if (match_event(next, event_type::QUIT)) {
evt = next;
break;
} else if (!compare_events(evt, next)) {
enqueue(move(next));
break;
}
m_log.trace_x("eventloop: Swallowing event within timeframe");
evt = next;
}
forward_event(evt);
}
2016-12-01 07:41:49 +00:00
m_log.info("Queue worker done");
}
/**
* Dispatch delayed worker thread
*/
void eventloop::dispatch_delayed_worker() {
while (true) {
// wait for notification
while (m_running && m_delayed_entry.type != 0) {
std::unique_lock<std::mutex> guard(m_delayed_lock);
m_delayed_cond.wait(guard, [&] { return m_delayed_entry.type != 0 || !m_running; });
}
if (!m_running) {
break;
}
this_thread::sleep_for(m_delayed_time);
m_delayed_entry.type = 0;
}
2016-12-01 07:41:49 +00:00
m_log.info("Delayed worker done");
}
2016-11-02 19:22:45 +00:00
/**
* Test if event matches given type
*/
inline bool eventloop::match_event(entry_t evt, event_type type) {
return static_cast<uint8_t>(type) == evt.type;
2016-11-02 19:22:45 +00:00
}
/**
* Compare given events
*/
inline bool eventloop::compare_events(entry_t evt, entry_t evt2) {
if (evt.type != evt2.type) {
return false;
} else if (match_event(evt, event_type::INPUT)) {
return evt.data[0] == evt2.data[0] && strncmp(evt.data, evt2.data, strlen(evt.data)) == 0;
}
return true;
2016-11-02 19:22:45 +00:00
}
/**
* Forward event to handler based on type
*/
void eventloop::forward_event(entry_t evt) {
if (evt.type == static_cast<uint8_t>(event_type::UPDATE)) {
2016-11-02 19:22:45 +00:00
on_update();
} else if (evt.type == static_cast<uint8_t>(event_type::INPUT)) {
on_input(evt.data);
} else if (evt.type == static_cast<uint8_t>(event_type::CHECK)) {
2016-11-02 19:22:45 +00:00
on_check();
} else if (evt.type == static_cast<uint8_t>(event_type::QUIT)) {
2016-12-01 07:41:49 +00:00
on_quit(reinterpret_cast<const quit_event&>(evt));
2016-11-02 19:22:45 +00:00
} else {
m_log.warn("Unknown event type for enqueued event (%d)", evt.type);
}
}
/**
* Handler for enqueued UPDATE events
*/
void eventloop::on_update() {
m_log.trace("eventloop: Received UPDATE event");
if (m_update_cb) {
m_update_cb();
} else {
m_log.warn("No callback to handle update");
}
}
/**
* Handler for enqueued INPUT events
*/
2016-11-25 12:55:15 +00:00
void eventloop::on_input(char* input) {
2016-11-02 19:22:45 +00:00
m_log.trace("eventloop: Received INPUT event");
for (auto&& block : m_modules) {
for (auto&& module : block.second) {
2016-11-25 12:55:15 +00:00
if (!module->receive_events()) {
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 (module->handle_event(input)) {
return;
}
}
}
if (m_unrecognized_input_cb) {
m_unrecognized_input_cb(input);
} else {
m_log.warn("No callback to handle unrecognized input");
}
}
/**
* Handler for enqueued CHECK events
*/
void eventloop::on_check() {
for (const auto& block : m_modules) {
for (const auto& module : block.second) {
2016-12-01 07:41:49 +00:00
if (m_running && module->running()) {
2016-11-02 19:22:45 +00:00
return;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
}
}
m_log.warn("No running modules...");
2016-12-01 07:41:49 +00:00
2016-11-02 19:22:45 +00:00
stop();
}
/**
* Handler for enqueued QUIT events
*/
2016-12-01 07:41:49 +00:00
void eventloop::on_quit(const quit_event& quit) {
2016-11-02 19:22:45 +00:00
m_log.trace("eventloop: Received QUIT event");
m_running = false;
2016-12-01 07:41:49 +00:00
if (quit.reload) {
kill(getpid(), SIGUSR1);
}
2016-11-02 19:22:45 +00:00
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END