refactor(modules): Move setup to constructor

This commit is contained in:
Michael Carlberg 2016-12-21 08:00:09 +01:00
parent 81e6fb062f
commit c01f111e34
52 changed files with 100 additions and 149 deletions

View File

@ -52,7 +52,7 @@ class taskqueue : non_copyable_mixin<taskqueue> {
private:
std::thread m_thread;
std::mutex m_lock;
std::mutex m_lock{};
std::condition_variable m_hold;
std::atomic_bool m_active{true};

View File

@ -17,9 +17,8 @@ namespace modules {
class backlight_module : public inotify_module<backlight_module> {
public:
using inotify_module::inotify_module;
explicit backlight_module(const bar_settings&, string);
void setup();
void idle();
bool on_event(inotify_event* event);
bool build(builder* builder, const string& tag) const;

View File

@ -25,9 +25,8 @@ namespace modules {
class battery_module : public inotify_module<battery_module> {
public:
using inotify_module::inotify_module;
explicit battery_module(const bar_settings&, string);
void setup();
void start();
void teardown();
void idle();
@ -66,7 +65,7 @@ namespace modules {
map<battery_value, string> m_valuepath;
std::atomic<int> m_percentage{0};
int m_fullat{100};
chrono::duration<double> m_interval;
chrono::duration<double> m_interval{};
chrono::system_clock::time_point m_lastpoll;
string m_timeformat;
int m_unchanged{SKIP_N_UNCHANGED};

View File

@ -36,9 +36,9 @@ namespace modules {
bool focused{false};
};
using event_module::event_module;
public:
explicit bspwm_module(const bar_settings&, string);
void setup();
void stop();
bool has_event();
bool update();

View File

@ -7,9 +7,8 @@ POLYBAR_NS
namespace modules {
class counter_module : public timer_module<counter_module> {
public:
using timer_module::timer_module;
explicit counter_module(const bar_settings&, string);
void setup();
bool update();
bool build(builder* builder, const string& tag) const;

View File

@ -18,9 +18,8 @@ namespace modules {
class cpu_module : public timer_module<cpu_module> {
public:
using timer_module::timer_module;
explicit cpu_module(const bar_settings&, string);
void setup();
bool update();
bool build(builder* builder, const string& tag) const;

View File

@ -7,9 +7,8 @@ POLYBAR_NS
namespace modules {
class date_module : public timer_module<date_module> {
public:
using timer_module::timer_module;
explicit date_module(const bar_settings&, string);
void setup();
bool update();
bool build(builder* builder, const string& tag) const;
bool handle_event(string cmd);

View File

@ -37,9 +37,8 @@ namespace modules {
*/
class fs_module : public timer_module<fs_module> {
public:
using timer_module::timer_module;
explicit fs_module(const bar_settings&, string);
void setup();
bool update();
string get_format() const;
string get_output();

View File

@ -14,7 +14,6 @@ namespace modules {
public:
explicit github_module(const bar_settings&, string);
void setup();
bool update();
bool build(builder* builder, const string& tag) const;

View File

@ -32,9 +32,9 @@ namespace modules {
label_t label;
};
using event_module::event_module;
public:
explicit i3_module(const bar_settings&, string);
void setup();
void stop();
bool has_event();
bool update();

View File

@ -8,15 +8,6 @@ POLYBAR_NS
struct ipc_hook; // fwd
namespace modules {
/**
* Hook structure that will be fired
* when receiving a message with specified id
*/
struct hook {
string payload;
string command;
};
/**
* Module that allow users to configure hooks on
* received ipc messages. The hook will execute the defined
@ -25,9 +16,18 @@ namespace modules {
*/
class ipc_module : public static_module<ipc_module> {
public:
using static_module::static_module;
/**
* Hook structure that will be fired
* when receiving a message with specified id
*/
struct hook {
string payload;
string command;
};
public:
explicit ipc_module(const bar_settings&, string);
void setup();
string get_output();
bool build(builder* builder, const string& tag) const;
void on_message(const ipc_hook& message);

View File

@ -10,9 +10,8 @@ namespace modules {
class memory_module : public timer_module<memory_module> {
public:
using timer_module::timer_module;
explicit memory_module(const bar_settings&, string);
void setup();
bool update();
bool build(builder* builder, const string& tag) const;

View File

@ -5,20 +5,20 @@
POLYBAR_NS
namespace modules {
struct menu_tree_item {
string exec;
label_t label;
};
struct menu_tree {
vector<unique_ptr<menu_tree_item>> items;
};
class menu_module : public static_module<menu_module> {
public:
using static_module::static_module;
struct menu_tree_item {
string exec;
label_t label;
};
struct menu_tree {
vector<unique_ptr<menu_tree_item>> items;
};
public:
explicit menu_module(const bar_settings&, string);
void setup();
bool build(builder* builder, const string& tag) const;
bool handle_event(string cmd);
bool receive_events() const;

View File

@ -104,7 +104,6 @@ namespace modules {
virtual string name() const = 0;
virtual bool running() const = 0;
virtual void setup() = 0;
virtual void start() = 0;
virtual void stop() = 0;
virtual void halt(string error_message) = 0;
@ -131,7 +130,6 @@ namespace modules {
string name() const;
bool running() const;
void setup();
void stop();
void halt(string error_message);
void teardown();

View File

@ -50,18 +50,6 @@ namespace modules {
return static_cast<bool>(m_enabled);
}
template <typename Impl>
void module<Impl>::setup() {
m_log.trace("%s: Setup", m_name);
try {
CAST_MOD(Impl)->setup();
} catch (const std::exception& err) {
m_log.err("%s: Setup failed", m_name);
halt(err.what());
}
}
template <typename Impl>
void module<Impl>::stop() {
if (!running()) {

View File

@ -14,9 +14,8 @@ namespace chrono = std::chrono;
namespace modules {
class mpd_module : public event_module<mpd_module> {
public:
using event_module::event_module;
explicit mpd_module(const bar_settings&, string);
void setup();
void teardown();
inline bool connected() const;
void idle();

View File

@ -11,9 +11,8 @@ namespace modules {
class network_module : public timer_module<network_module> {
public:
using timer_module::timer_module;
explicit network_module(const bar_settings&, string);
void setup();
void teardown();
bool update();
string get_format() const;

View File

@ -16,9 +16,8 @@ namespace chrono = std::chrono;
namespace modules {
class script_module : public event_module<script_module> {
public:
using event_module::event_module;
explicit script_module(const bar_settings&, string);
void setup();
void stop();
void idle();
bool has_event();

View File

@ -12,9 +12,8 @@ namespace modules {
class temperature_module : public timer_module<temperature_module> {
public:
using timer_module::timer_module;
explicit temperature_module(const bar_settings&, string);
void setup();
bool update();
string get_format() const;
bool build(builder* builder, const string& tag) const;

View File

@ -7,9 +7,8 @@ POLYBAR_NS
namespace modules {
class text_module : public static_module<text_module> {
public:
using static_module::static_module;
explicit text_module(const bar_settings&, string);
void setup();
string get_format() const;
string get_output();
};

View File

@ -33,7 +33,6 @@ namespace modules {
bool running() const { \
return false; \
} \
void setup() {} \
void start() {} \
void stop() {} \
void halt(string) {} \

View File

@ -20,9 +20,8 @@ namespace modules {
class volume_module : public event_module<volume_module> {
public:
using event_module::event_module;
explicit volume_module(const bar_settings&, string);
void setup();
void teardown();
bool has_event();
bool update();

View File

@ -25,9 +25,8 @@ namespace modules {
*/
class xbacklight_module : public static_module<xbacklight_module>, public xpp::event::sink<evt::randr_notify> {
public:
explicit xbacklight_module(const bar_settings& bar, string name);
explicit xbacklight_module(const bar_settings& bar, string name_);
void setup();
void teardown();
void handle(const evt::randr_notify& evt);
void update();

View File

@ -19,9 +19,8 @@ namespace modules {
class xkeyboard_module : public static_module<xkeyboard_module>,
public xpp::event::sink<evt::xkb_new_keyboard_notify, evt::xkb_state_notify, evt::xkb_indicator_state_notify> {
public:
explicit xkeyboard_module(const bar_settings& bar, string name);
explicit xkeyboard_module(const bar_settings& bar, string name_);
void setup();
void teardown();
void update();
bool build(builder* builder, const string& tag) const;

View File

@ -34,7 +34,6 @@ namespace modules {
public:
explicit xwindow_module(const bar_settings&, string);
void setup();
void teardown();
void handle(const evt::property_notify& evt);
void update();

View File

@ -49,9 +49,8 @@ namespace modules {
*/
class xworkspaces_module : public static_module<xworkspaces_module>, public xpp::event::sink<evt::property_notify> {
public:
explicit xworkspaces_module(const bar_settings& bar, string name);
explicit xworkspaces_module(const bar_settings& bar, string name_);
void setup();
void teardown();
void handle(const evt::property_notify& evt);
void update();

View File

@ -71,7 +71,7 @@ class command {
pid_t m_forkpid{};
int m_forkstatus{};
std::mutex m_pipelock;
std::mutex m_pipelock{};
};
namespace command_util {

View File

@ -280,4 +280,9 @@ chrono::milliseconds config::convert(string&& value) const {
return chrono::milliseconds{convert<chrono::milliseconds::rep>(forward<string>(value))};
}
template <>
chrono::duration<double> config::convert(string&& value) const {
return chrono::duration<double>{convert<double>(forward<string>(value))};
}
POLYBAR_NS_END

View File

@ -103,7 +103,6 @@ controller::controller(connection& conn, signal_emitter& emitter, const logger&
module->set_update_cb([&] { enqueue(make_update_evt(false)); });
module->set_stop_cb([&] { enqueue(make_check_evt()); });
module->setup();
m_modules[align].emplace_back(move(module));

View File

@ -137,8 +137,9 @@ int main(int argc, char** argv) {
}
logger.info("Waiting for spawned processes to end");
while (process_util::notify_childprocess())
while (process_util::notify_childprocess()) {
;
}
if (reload) {
logger.info("Re-launching application...");

View File

@ -14,19 +14,8 @@ namespace modules {
template class module<backlight_module>;
template class inotify_module<backlight_module>;
void brightness_handle::filepath(const string& path) {
if (!file_util::exists(path)) {
throw module_error("The file '" + path + "' does not exist");
}
m_path = path;
}
float brightness_handle::read() const {
return std::strtof(file_util::get_contents(m_path).c_str(), nullptr);
}
void backlight_module::setup() {
// Load configuration values
backlight_module::backlight_module(const bar_settings& bar, string name_)
: inotify_module<backlight_module>(bar, move(name_)) {
auto card = m_conf.get<string>(name(), "card");
// Add formats and elements
@ -50,6 +39,17 @@ namespace modules {
watch(string_util::replace(PATH_BACKLIGHT_VAL, "%card%", card));
}
void brightness_handle::filepath(const string& path) {
if (!file_util::exists(path)) {
throw module_error("The file '" + path + "' does not exist");
}
m_path = path;
}
float brightness_handle::read() const {
return std::strtof(file_util::get_contents(m_path).c_str(), nullptr);
}
void backlight_module::idle() {
sleep(75ms);
}

View File

@ -20,7 +20,8 @@ namespace modules {
/**
* Bootstrap module by setting up required components
*/
void battery_module::setup() {
battery_module::battery_module(const bar_settings& bar, string name_)
: inotify_module<battery_module>(bar, move(name_)) {
auto battery = m_conf.get<string>(name(), "battery", "BAT0");
auto adapter = m_conf.get<string>(name(), "adapter", "ADP1");

View File

@ -39,7 +39,7 @@ namespace modules {
template class module<bspwm_module>;
template class event_module<bspwm_module>;
void bspwm_module::setup() {
bspwm_module::bspwm_module(const bar_settings& bar, string name_) : event_module<bspwm_module>(bar, move(name_)) {
auto socket_path = bspwm_util::get_socket_path();
if (!file_util::exists(socket_path)) {

View File

@ -9,8 +9,9 @@ namespace modules {
template class module<counter_module>;
template class timer_module<counter_module>;
void counter_module::setup() {
m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1));
counter_module::counter_module(const bar_settings& bar, string name_)
: timer_module<counter_module>(bar, move(name_)) {
m_interval = m_conf.get(name(), "interval", m_interval);
m_formatter->add(DEFAULT_FORMAT, TAG_COUNTER, {TAG_COUNTER});
}

View File

@ -17,7 +17,7 @@ namespace modules {
template class module<cpu_module>;
template class timer_module<cpu_module>;
void cpu_module::setup() {
cpu_module::cpu_module(const bar_settings& bar, string name_) : timer_module<cpu_module>(bar, move(name_)) {
m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1));
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_BAR_LOAD, TAG_RAMP_LOAD, TAG_RAMP_LOAD_PER_CORE});

View File

@ -10,7 +10,7 @@ namespace modules {
template class module<date_module>;
template class timer_module<date_module>;
void date_module::setup() {
date_module::date_module(const bar_settings& bar, string name_) : timer_module<date_module>(bar, move(name_)) {
if (!m_bar.locale.empty()) {
setlocale(LC_TIME, m_bar.locale.c_str());
}

View File

@ -23,7 +23,7 @@ namespace modules {
* Bootstrap the module by reading config values and
* setting up required components
*/
void fs_module::setup() {
fs_module::fs_module(const bar_settings& bar, string name_) : timer_module<fs_module>(bar, move(name_)) {
m_mountpoints = m_conf.get_list<string>(name(), "mount");
m_fixed = m_conf.get<bool>(name(), "fixed-values", m_fixed);
m_spacing = m_conf.get<int>(name(), "spacing", m_spacing);

View File

@ -13,13 +13,8 @@ namespace modules {
/**
* Construct module
*/
github_module::github_module(const bar_settings& bar, string name)
: timer_module<github_module>(bar, name), m_http(http_util::make_downloader()) {}
/**
* Bootstrap module
*/
void github_module::setup() {
github_module::github_module(const bar_settings& bar, string name_)
: timer_module<github_module>(bar, move(name_)), m_http(http_util::make_downloader()) {
m_accesstoken = m_conf.get<string>(name(), "token");
m_interval = m_conf.get(name(), "interval", 60s);
m_empty_notifications = m_conf.get(name(), "empty-notifications", m_empty_notifications);

View File

@ -15,11 +15,7 @@ namespace modules {
template class module<i3_module>;
template class event_module<i3_module>;
i3_module::workspace::operator bool() {
return label && *label;
}
void i3_module::setup() {
i3_module::i3_module(const bar_settings& bar, string name_) : event_module<i3_module>(bar, move(name_)) {
auto socket_path = i3ipc::get_socketpath();
if (!file_util::exists(socket_path)) {
@ -83,6 +79,10 @@ namespace modules {
}
}
i3_module::workspace::operator bool() {
return label && *label;
}
void i3_module::stop() {
try {
if (m_ipc) {

View File

@ -15,7 +15,7 @@ namespace modules {
* Load user-defined ipc hooks and
* create formatting tags
*/
void ipc_module::setup() {
ipc_module::ipc_module(const bar_settings& bar, string name_) : static_module<ipc_module>(bar, move(name_)) {
size_t index = 0;
for (auto&& command : m_conf.get_list<string>(name(), "hook")) {

View File

@ -15,7 +15,7 @@ namespace modules {
template class module<memory_module>;
template class timer_module<memory_module>;
void memory_module::setup() {
memory_module::memory_module(const bar_settings& bar, string name_) : timer_module<memory_module>(bar, move(name_)) {
m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1));
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_BAR_USED, TAG_BAR_FREE});

View File

@ -13,7 +13,7 @@ namespace modules {
template class module<menu_module>;
template class static_module<menu_module>;
void menu_module::setup() {
menu_module::menu_module(const bar_settings& bar, string name_) : static_module<menu_module>(bar, move(name_)) {
string default_format{TAG_LABEL_TOGGLE + string{" "} + TAG_MENU};
m_formatter->add(DEFAULT_FORMAT, default_format, {TAG_LABEL_TOGGLE, TAG_MENU});

View File

@ -16,7 +16,7 @@ namespace modules {
template class module<mpd_module>;
template class event_module<mpd_module>;
void mpd_module::setup() {
mpd_module::mpd_module(const bar_settings& bar, string name_) : event_module<mpd_module>(bar, move(name_)) {
m_host = m_conf.get<string>(name(), "host", m_host);
m_port = m_conf.get<unsigned int>(name(), "port", m_port);
m_pass = m_conf.get<string>(name(), "password", m_pass);

View File

@ -14,7 +14,8 @@ namespace modules {
template class module<network_module>;
template class timer_module<network_module>;
void network_module::setup() {
network_module::network_module(const bar_settings& bar, string name_)
: timer_module<network_module>(bar, move(name_)) {
// Load configuration values
REQ_CONFIG_VALUE(name(), m_interface, "interface");
GET_CONFIG_VALUE(name(), m_ping_nth_update, "ping-interval");

View File

@ -10,7 +10,7 @@ namespace modules {
template class module<script_module>;
template class event_module<script_module>;
void script_module::setup() {
script_module::script_module(const bar_settings& bar, string name_) : event_module<script_module>(bar, move(name_)) {
REQ_CONFIG_VALUE(name(), m_exec, "exec");
GET_CONFIG_VALUE(name(), m_tail, "tail");
GET_CONFIG_VALUE(name(), m_maxlen, "maxlen");

View File

@ -14,7 +14,8 @@ namespace modules {
template class module<temperature_module>;
template class timer_module<temperature_module>;
void temperature_module::setup() {
temperature_module::temperature_module(const bar_settings& bar, string name_)
: timer_module<temperature_module>(bar, move(name_)) {
m_zone = m_conf.get<int>(name(), "thermal-zone", 0);
m_tempwarn = m_conf.get<int>(name(), "warn-temperature", 80);
m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1));

View File

@ -9,7 +9,7 @@ namespace modules {
template class module<text_module>;
template class static_module<text_module>;
void text_module::setup() {
text_module::text_module(const bar_settings& bar, string name_) : static_module<text_module>(bar, move(name_)) {
m_formatter->add("content", "", {});
if (m_formatter->get("content")->value.empty()) {

View File

@ -18,7 +18,7 @@ namespace modules {
template class module<volume_module>;
template class event_module<volume_module>;
void volume_module::setup() {
volume_module::volume_module(const bar_settings& bar, string name_) : event_module<volume_module>(bar, move(name_)) {
// Load configuration values
string master_mixer_name{"Master"};
string speaker_mixer_name;

View File

@ -19,13 +19,8 @@ namespace modules {
/**
* Construct module
*/
xbacklight_module::xbacklight_module(const bar_settings& bar, string name)
: static_module<xbacklight_module>(bar, move(name)), m_connection(connection::make()) {}
/**
* Bootstrap the module by grabbing all required components
*/
void xbacklight_module::setup() {
xbacklight_module::xbacklight_module(const bar_settings& bar, string name_)
: static_module<xbacklight_module>(bar, move(name_)), m_connection(connection::make()) {
auto output = m_conf.get<string>(name(), "output", m_bar.monitor->name);
auto strict = m_conf.get<bool>(name(), "monitor-strict", false);

View File

@ -17,13 +17,8 @@ namespace modules {
/**
* Construct module
*/
xkeyboard_module::xkeyboard_module(const bar_settings& bar, string name)
: static_module<xkeyboard_module>(bar, move(name)), m_connection(connection::make()) {}
/**
* Bootstrap the module
*/
void xkeyboard_module::setup() {
xkeyboard_module::xkeyboard_module(const bar_settings& bar, string name_)
: static_module<xkeyboard_module>(bar, move(name_)), m_connection(connection::make()) {
// Load config values
m_blacklist = m_conf.get_list<string>(name(), "blacklist", {});

View File

@ -64,13 +64,8 @@ namespace modules {
/**
* Construct module
*/
xwindow_module::xwindow_module(const bar_settings& bar, string name)
: static_module<xwindow_module>(bar, name), m_connection(connection::make()) {}
/**
* Bootstrap the module
*/
void xwindow_module::setup() {
xwindow_module::xwindow_module(const bar_settings& bar, string name_)
: static_module<xwindow_module>(bar, move(name_)), m_connection(connection::make()) {
// Initialize ewmh atoms
if ((m_ewmh = ewmh_util::initialize()) == nullptr) {
throw module_error("Failed to initialize ewmh atoms");

View File

@ -20,13 +20,8 @@ namespace modules {
/**
* Construct module
*/
xworkspaces_module::xworkspaces_module(const bar_settings& bar, string name)
: static_module<xworkspaces_module>(bar, move(name)), m_connection(connection::make()) {}
/**
* Bootstrap the module
*/
void xworkspaces_module::setup() {
xworkspaces_module::xworkspaces_module(const bar_settings& bar, string name_)
: static_module<xworkspaces_module>(bar, move(name_)), m_connection(connection::make()) {
// Load config values
m_pinworkspaces = m_conf.get<bool>(name(), "pin-workspaces", m_pinworkspaces);
m_click = m_conf.get<bool>(name(), "enable-click", m_click);