1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-25 13:55:47 -05:00

refactor(modules): Replace callbacks with signals

Refs #265
This commit is contained in:
Michael Carlberg 2016-12-22 22:11:03 +01:00
parent a914526949
commit e47e439954
6 changed files with 27 additions and 37 deletions

View file

@ -39,9 +39,9 @@ namespace sig_ev = signals::eventqueue;
namespace sig_ui = signals::ui;
namespace sig_ipc = signals::ipc;
class controller : public signal_receiver<SIGN_PRIORITY_CONTROLLER, sig_ev::process_update, sig_ev::process_input,
sig_ev::process_quit, sig_ev::process_check, sig_ipc::process_action, sig_ipc::process_command,
sig_ipc::process_hook, sig_ui::button_press> {
class controller : public signal_receiver<SIGN_PRIORITY_CONTROLLER, sig_ev::process_broadcast, sig_ev::process_update,
sig_ev::process_input, sig_ev::process_quit, sig_ev::process_check, sig_ipc::process_action,
sig_ipc::process_command, sig_ipc::process_hook, sig_ui::button_press> {
public:
using make_type = unique_ptr<controller>;
static make_type make(unique_ptr<ipc>&& ipc, unique_ptr<inotify_watch>&& config_watch);
@ -60,6 +60,7 @@ class controller : public signal_receiver<SIGN_PRIORITY_CONTROLLER, sig_ev::proc
void process_eventqueue();
void process_inputdata();
bool on(const sig_ev::process_broadcast& evt);
bool on(const sig_ev::process_update& evt);
bool on(const sig_ev::process_input& evt);
bool on(const sig_ev::process_quit& evt);

View file

@ -96,6 +96,7 @@ namespace signals {
DEFINE_VALUE_SIGNAL(2, process_update, event);
DEFINE_VALUE_SIGNAL(3, process_input, string);
DEFINE_SIGNAL(4, process_check);
DEFINE_SIGNAL(5, process_broadcast);
}
namespace ipc {

View file

@ -10,6 +10,7 @@ namespace signals {
struct process_update;
struct process_input;
struct process_check;
struct process_broadcast;
}
namespace ipc {
struct process_command;

View file

@ -3,12 +3,10 @@
#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <map>
#include <mutex>
#include "common.hpp"
#include "components/config.hpp"
#include "components/logger.hpp"
#include "components/types.hpp"
#include "errors.hpp"
#include "utils/concurrency.hpp"
@ -46,6 +44,9 @@ namespace drawtypes {
}
class builder;
class config;
class logger;
class signal_emitter;
// }}}
@ -108,9 +109,6 @@ namespace modules {
virtual void stop() = 0;
virtual void halt(string error_message) = 0;
virtual string contents() = 0;
virtual void set_update_cb(callback<>&& cb) = 0;
virtual void set_stop_cb(callback<>&& cb) = 0;
};
// }}}
@ -141,9 +139,7 @@ namespace modules {
string get_output();
protected:
callback<> m_update_callback;
callback<> m_stop_callback;
signal_emitter& m_sig;
const bar_settings m_bar;
const logger& m_log;
const config& m_conf;

View file

@ -1,15 +1,20 @@
#include "components/builder.hpp"
#include "components/logger.hpp"
#include "components/config.hpp"
#include "events/signal.hpp"
#include "events/signal_emitter.hpp"
POLYBAR_NS
namespace sig_ev = signals::eventqueue;
namespace modules {
// module<Impl> public {{{
template <typename Impl>
module<Impl>::module(const bar_settings bar, string name)
: m_bar(bar)
: m_sig(signal_emitter::make())
, m_bar(bar)
, m_log(logger::make())
, m_conf(config::make())
, m_name("module/" + name)
@ -30,16 +35,6 @@ namespace modules {
}
}
template <typename Impl>
void module<Impl>::set_update_cb(callback<>&& cb) {
m_update_callback = forward<decltype(cb)>(cb);
}
template <typename Impl>
void module<Impl>::set_stop_cb(callback<>&& cb) {
m_stop_callback = forward<decltype(cb)>(cb);
}
template <typename Impl>
string module<Impl>::name() const {
return m_name;
@ -66,9 +61,7 @@ namespace modules {
CAST_MOD(Impl)->wakeup();
CAST_MOD(Impl)->teardown();
if (m_stop_callback) {
m_stop_callback();
}
m_sig.emit(sig_ev::process_check{});
}
}
@ -97,11 +90,7 @@ namespace modules {
}
m_cache = CAST_MOD(Impl)->get_output();
if (m_update_callback)
m_update_callback();
else
m_log.info("%s: No handler, ignoring broadcast...", name());
m_sig.emit(sig_ev::process_broadcast{});
}
template <typename Impl>

View file

@ -162,8 +162,6 @@ bool controller::run(bool writeback) {
try {
m_log.info("Starting %s", module->name());
module->start();
module->set_update_cb([&] { enqueue(make_update_evt(false)); });
module->set_stop_cb([&] { enqueue(make_check_evt()); });
started_modules++;
} catch (const application_error& err) {
m_log.err("Failed to start '%s' (reason: %s)", module->name(), err.what());
@ -352,16 +350,20 @@ void controller::process_inputdata() {
}
}
/**
* Process broadcast events
*/
bool controller::on(const sig_ev::process_broadcast&) {
enqueue(make_update_evt(false));
return true;
}
/**
* Process eventqueue update event
*/
bool controller::on(const sig_ev::process_update& evt) {
bool force{evt.data()->flag};
if (!m_bar) {
return false;
}
const bar_settings& bar{m_bar->settings()};
string contents;
string separator{bar.separator};