refactor(signals): Simple types

This commit is contained in:
Michael Carlberg 2016-12-23 22:19:42 +01:00
parent b422d1d1a4
commit 789800a68f
9 changed files with 76 additions and 46 deletions

View File

@ -42,8 +42,8 @@ namespace sig_ev = signals::eventqueue;
namespace sig_ui = signals::ui; namespace sig_ui = signals::ui;
namespace sig_ipc = signals::ipc; namespace sig_ipc = signals::ipc;
class controller : public signal_receiver<SIGN_PRIORITY_CONTROLLER, sig_ev::process_broadcast, sig_ev::process_update, class controller : public signal_receiver<SIGN_PRIORITY_CONTROLLER, sig_ev::exit_terminate, sig_ev::exit_reload,
sig_ev::process_quit, sig_ev::process_check, sig_ipc::process_action, sig_ev::update, sig_ev::notify_change, sig_ev::check_state, sig_ipc::process_action,
sig_ipc::process_command, sig_ipc::process_hook, sig_ui::button_press> { sig_ipc::process_command, sig_ipc::process_hook, sig_ui::button_press> {
public: public:
using make_type = unique_ptr<controller>; using make_type = unique_ptr<controller>;
@ -63,10 +63,11 @@ class controller : public signal_receiver<SIGN_PRIORITY_CONTROLLER, sig_ev::proc
void process_eventqueue(); void process_eventqueue();
void process_inputdata(); void process_inputdata();
bool on(const sig_ev::process_broadcast& evt); bool on(const sig_ev::notify_change& evt);
bool on(const sig_ev::process_update& evt); bool on(const sig_ev::update& evt);
bool on(const sig_ev::process_quit& evt); bool on(const sig_ev::exit_terminate& evt);
bool on(const sig_ev::process_check& evt); bool on(const sig_ev::exit_reload& evt);
bool on(const sig_ev::check_state& evt);
bool on(const sig_ui::button_press& evt); bool on(const sig_ui::button_press& evt);
bool on(const sig_ipc::process_action& evt); bool on(const sig_ipc::process_action& evt);
bool on(const sig_ipc::process_command& evt); bool on(const sig_ipc::process_command& evt);

View File

@ -92,10 +92,11 @@ namespace signals {
} }
namespace eventqueue { namespace eventqueue {
DEFINE_VALUE_SIGNAL(1, process_quit, event); DEFINE_SIGNAL(1, exit_terminate);
DEFINE_VALUE_SIGNAL(2, process_update, event); DEFINE_SIGNAL(2, exit_reload);
DEFINE_SIGNAL(3, process_check); DEFINE_SIGNAL(3, notify_change);
DEFINE_SIGNAL(4, process_broadcast); DEFINE_SIGNAL(4, check_state);
DEFINE_SIGNAL(5, update);
} }
namespace ipc { namespace ipc {

View File

@ -6,10 +6,11 @@ POLYBAR_NS
namespace signals { namespace signals {
namespace eventqueue { namespace eventqueue {
struct process_quit; struct exit_terminate;
struct process_update; struct exit_reload;
struct process_check; struct notify_change;
struct process_broadcast; struct check_state;
struct update;
} }
namespace ipc { namespace ipc {
struct process_command; struct process_command;

View File

@ -2,6 +2,10 @@
#include <cstdint> #include <cstdint>
#include "common.hpp"
POLYBAR_NS
enum class event_type : uint8_t { enum class event_type : uint8_t {
NONE = 0, NONE = 0,
UPDATE, UPDATE,
@ -16,6 +20,10 @@ struct event {
}; };
namespace { namespace {
inline bool operator==(uint8_t id, event_type type) {
return id == static_cast<uint8_t>(type);
}
/** /**
* Create QUIT event * Create QUIT event
*/ */
@ -44,3 +52,5 @@ namespace {
return event{static_cast<uint8_t>(event_type::CHECK)}; return event{static_cast<uint8_t>(event_type::CHECK)};
} }
} }
POLYBAR_NS_END

View File

@ -61,7 +61,7 @@ namespace modules {
CAST_MOD(Impl)->wakeup(); CAST_MOD(Impl)->wakeup();
CAST_MOD(Impl)->teardown(); CAST_MOD(Impl)->teardown();
m_sig.emit(sig_ev::process_check{}); m_sig.emit(sig_ev::check_state{});
} }
} }
@ -91,7 +91,7 @@ namespace modules {
template <typename Impl> template <typename Impl>
void module<Impl>::broadcast() { void module<Impl>::broadcast() {
m_changed = true; m_changed = true;
m_sig.emit(sig_ev::process_broadcast{}); m_sig.emit(sig_ev::notify_change{});
} }
template <typename Impl> template <typename Impl>

View File

@ -188,7 +188,7 @@ bool controller::run(bool writeback) {
read_events(); read_events();
if (m_event_thread.joinable()) { if (m_event_thread.joinable()) {
m_queue.enqueue(make_quit_evt(static_cast<bool>(g_reload))); enqueue(make_quit_evt(static_cast<bool>(g_reload)));
m_event_thread.join(); m_event_thread.join();
} }
@ -312,7 +312,7 @@ void controller::read_events() {
void controller::process_eventqueue() { void controller::process_eventqueue() {
m_log.info("Eventqueue worker (thread-id=%lu)", this_thread::get_id()); m_log.info("Eventqueue worker (thread-id=%lu)", this_thread::get_id());
m_queue.enqueue(make_update_evt(true)); enqueue(make_update_evt(true));
while (!g_terminate) { while (!g_terminate) {
event evt{}; event evt{};
@ -320,22 +320,26 @@ void controller::process_eventqueue() {
if (g_terminate) { if (g_terminate) {
break; break;
} else if (evt.type == static_cast<uint8_t>(event_type::QUIT)) { } else if (evt.type == event_type::QUIT) {
m_sig.emit(sig_ev::process_quit{make_quit_evt(evt.flag)}); if (evt.flag) {
} else if (evt.type == static_cast<uint8_t>(event_type::INPUT)) { on(sig_ev::exit_reload{});
} else {
on(sig_ev::exit_terminate{});
}
} else if (evt.type == event_type::INPUT) {
process_inputdata(); process_inputdata();
} else { } else {
event next{}; event next{};
size_t swallowed{0}; size_t swallowed{0};
while (swallowed++ < m_swallow_limit && m_queue.wait_dequeue_timed(next, m_swallow_update)) { while (swallowed++ < m_swallow_limit && m_queue.wait_dequeue_timed(next, m_swallow_update)) {
if (next.type == static_cast<uint8_t>(event_type::QUIT)) { if (next.type == event_type::QUIT) {
evt = next; evt = next;
break; break;
} else if (next.type == static_cast<uint8_t>(event_type::INPUT)) { } else if (next.type == event_type::INPUT) {
evt = next; evt = next;
break; break;
} else if (evt.type != next.type) { } else if (evt.type != next.type) {
m_queue.try_enqueue(move(next)); enqueue(move(next));
break; break;
} else { } else {
m_log.trace_x("controller: Swallowing event within timeframe"); m_log.trace_x("controller: Swallowing event within timeframe");
@ -343,14 +347,18 @@ void controller::process_eventqueue() {
} }
} }
if (evt.type == static_cast<uint8_t>(event_type::UPDATE)) { if (evt.type == event_type::UPDATE) {
m_sig.emit(sig_ev::process_update{make_update_evt(evt.flag)}); on(sig_ev::update{});
} else if (evt.type == static_cast<uint8_t>(event_type::INPUT)) { } else if (evt.type == event_type::INPUT) {
process_inputdata(); process_inputdata();
} else if (evt.type == static_cast<uint8_t>(event_type::QUIT)) { } else if (evt.type == event_type::QUIT) {
m_sig.emit(sig_ev::process_quit{make_quit_evt(evt.flag)}); if (evt.flag) {
} else if (evt.type == static_cast<uint8_t>(event_type::CHECK)) { on(sig_ev::exit_reload{});
m_sig.emit(sig_ev::process_check{}); } else {
on(sig_ev::exit_terminate{});
}
} else if (evt.type == event_type::CHECK) {
on(sig_ev::check_state{});
} else { } else {
m_log.warn("Unknown event type for enqueued event (%d)", evt.type); m_log.warn("Unknown event type for enqueued event (%d)", evt.type);
} }
@ -394,7 +402,7 @@ void controller::process_inputdata() {
/** /**
* Process broadcast events * Process broadcast events
*/ */
bool controller::on(const sig_ev::process_broadcast&) { bool controller::on(const sig_ev::notify_change&) {
enqueue(make_update_evt(false)); enqueue(make_update_evt(false));
return true; return true;
} }
@ -402,9 +410,7 @@ bool controller::on(const sig_ev::process_broadcast&) {
/** /**
* Process eventqueue update event * Process eventqueue update event
*/ */
bool controller::on(const sig_ev::process_update& evt) { bool controller::on(const sig_ev::update&) {
bool force{evt.data()->flag};
const bar_settings& bar{m_bar->settings()}; const bar_settings& bar{m_bar->settings()};
string contents; string contents;
string separator{bar.separator}; string separator{bar.separator};
@ -475,7 +481,7 @@ bool controller::on(const sig_ev::process_update& evt) {
try { try {
if (!m_writeback) { if (!m_writeback) {
m_bar->parse(move(contents), force); m_bar->parse(move(contents), false);
} else { } else {
std::cout << contents << std::endl; std::cout << contents << std::endl;
} }
@ -487,17 +493,25 @@ bool controller::on(const sig_ev::process_update& evt) {
} }
/** /**
* Process eventqueue quit event * Process eventqueue terminate event
*/ */
bool controller::on(const sig_ev::process_quit& evt) { bool controller::on(const sig_ev::exit_terminate&) {
raise((*evt()).flag ? SIGUSR1 : SIGALRM); raise(SIGALRM);
return true;
}
/**
* Process eventqueue reload event
*/
bool controller::on(const sig_ev::exit_reload&) {
raise(SIGUSR1);
return true; return true;
} }
/** /**
* Process eventqueue check event * Process eventqueue check event
*/ */
bool controller::on(const sig_ev::process_check&) { bool controller::on(const sig_ev::check_state&) {
for (const auto& block : m_modules) { for (const auto& block : m_modules) {
for (const auto& module : block.second) { for (const auto& module : block.second) {
if (module->running()) { if (module->running()) {
@ -506,7 +520,7 @@ bool controller::on(const sig_ev::process_check&) {
} }
} }
m_log.warn("No running modules..."); m_log.warn("No running modules...");
enqueue(make_quit_evt(false)); on(exit_terminate{});
return true; return true;
} }

View File

@ -113,7 +113,7 @@ void screen::handle(const evt::randr_screen_change_notify& evt) {
} }
m_log.warn("randr_screen_change_notify (%ux%u)... reloading", evt->width, evt->height); m_log.warn("randr_screen_change_notify (%ux%u)... reloading", evt->width, evt->height);
m_sig.emit(process_quit{make_quit_evt(true)}); m_sig.emit(exit_reload{});
m_sigraised = true; m_sigraised = true;
} }

View File

@ -286,7 +286,7 @@ void tray_manager::deactivate(bool clear_selection) {
m_connection.flush(); m_connection.flush();
m_sig.emit(process_update{make_update_evt(true)}); m_sig.emit(notify_change{});
} }
/** /**
@ -325,7 +325,7 @@ void tray_manager::reconfigure() {
m_connection.flush(); m_connection.flush();
m_sig.emit(process_update{make_update_evt(true)}); m_sig.emit(notify_change{});
} }
/** /**

View File

@ -13,8 +13,11 @@ namespace xutils {
xcb_connection_t* get_connection() { xcb_connection_t* get_connection() {
static xcb_connection_t* connection; static xcb_connection_t* connection;
if (!connection) { if (!connection) {
XSetEventQueueOwner(xlib::get_display(), XCBOwnsEventQueue); auto display = xlib::get_display();
connection = XGetXCBConnection(xlib::get_display()); if (display != nullptr) {
XSetEventQueueOwner(display, XCBOwnsEventQueue);
connection = XGetXCBConnection(xlib::get_display());
}
} }
return connection; return connection;
} }