refactor: Move module type string into modules

This allows us to identify module by their type and it is also better to
store the module type as part of the module instead of having it
hardcoded in factory.hpp
This commit is contained in:
patrick96 2020-05-15 19:59:08 +02:00 committed by Patrick Ziegler
parent 06012af3aa
commit d592eea966
30 changed files with 137 additions and 89 deletions

View File

@ -1,7 +1,7 @@
#pragma once
#include "settings.hpp"
#include "modules/meta/event_module.hpp"
#include "settings.hpp"
POLYBAR_NS
@ -9,7 +9,7 @@ POLYBAR_NS
namespace alsa {
class mixer;
class control;
}
} // namespace alsa
namespace modules {
enum class mixer { NONE = 0, MASTER, SPEAKER, HEADPHONE };
@ -29,6 +29,8 @@ namespace modules {
string get_output();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/alsa";
protected:
bool input(string&& cmd);
@ -62,6 +64,6 @@ namespace modules {
atomic<bool> m_headphones{false};
atomic<int> m_volume{0};
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -2,13 +2,12 @@
#include "components/config.hpp"
#include "modules/meta/inotify_module.hpp"
#include "modules/meta/input_handler.hpp"
#include "settings.hpp"
POLYBAR_NS
namespace modules {
class backlight_module : public inotify_module<backlight_module>, public input_handler {
class backlight_module : public inotify_module<backlight_module> {
public:
struct brightness_handle {
void filepath(const string& path);
@ -27,6 +26,8 @@ namespace modules {
bool on_event(inotify_event* event);
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/backlight";
protected:
bool input(string&& cmd);

View File

@ -54,6 +54,8 @@ namespace modules {
string get_format() const;
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/battery";
protected:
state current_state();
int current_percentage();
@ -106,6 +108,6 @@ namespace modules {
chrono::system_clock::time_point m_lastpoll;
thread m_subthread;
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -47,6 +47,8 @@ namespace modules {
string get_output();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/bspwm";
protected:
bool input(string&& cmd);
@ -91,6 +93,6 @@ namespace modules {
// used while formatting output
size_t m_index{0U};
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -12,11 +12,13 @@ namespace modules {
bool update();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/counter";
private:
static constexpr auto TAG_COUNTER = "<counter>";
int m_counter{0};
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -1,7 +1,7 @@
#pragma once
#include "settings.hpp"
#include "modules/meta/timer_module.hpp"
#include "settings.hpp"
POLYBAR_NS
@ -24,6 +24,8 @@ namespace modules {
bool update();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/cpu";
protected:
bool read_values();
float get_load(size_t core) const;
@ -46,6 +48,6 @@ namespace modules {
float m_total = 0;
vector<float> m_load;
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -1,10 +1,10 @@
#pragma once
#include "modules/meta/timer_module.hpp"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <iomanip>
#include <iostream>
#include "modules/meta/timer_module.hpp"
POLYBAR_NS
@ -16,6 +16,8 @@ namespace modules {
bool update();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/date";
protected:
bool input(string&& cmd);
@ -41,6 +43,6 @@ namespace modules {
std::atomic<bool> m_toggled{false};
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -1,8 +1,8 @@
#pragma once
#include "components/config.hpp"
#include "settings.hpp"
#include "modules/meta/timer_module.hpp"
#include "settings.hpp"
POLYBAR_NS
@ -42,6 +42,8 @@ namespace modules {
string get_output();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/fs";
private:
static constexpr auto FORMAT_MOUNTED = "format-mounted";
static constexpr auto FORMAT_UNMOUNTED = "format-unmounted";
@ -66,6 +68,6 @@ namespace modules {
// used while formatting output
size_t m_index{0_z};
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -18,6 +18,8 @@ namespace modules {
bool build(builder* builder, const string& tag) const;
string get_format() const;
static constexpr auto TYPE = "internal/github";
private:
void update_label(int);
int get_number_of_notification();

View File

@ -51,6 +51,8 @@ namespace modules {
bool update();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/i3";
protected:
bool input(string&& cmd);

View File

@ -32,6 +32,8 @@ namespace modules {
bool build(builder* builder, const string& tag) const;
void on_message(const string& message);
static constexpr auto TYPE = "custom/ipc";
private:
static constexpr const char* TAG_OUTPUT{"<output>"};
vector<unique_ptr<hook>> m_hooks;
@ -39,6 +41,6 @@ namespace modules {
string m_output;
size_t m_initial;
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -15,6 +15,8 @@ namespace modules {
bool update();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/memory";
private:
static constexpr const char* TAG_LABEL{"<label>"};
static constexpr const char* TAG_BAR_USED{"<bar-used>"};
@ -40,6 +42,6 @@ namespace modules {
ramp_t m_ramp_swapused;
ramp_t m_ramp_swapfree;
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -22,6 +22,8 @@ namespace modules {
bool build(builder* builder, const string& tag) const;
void update() {}
static constexpr auto TYPE = "custom/menu";
protected:
bool input(string&& cmd);
@ -42,6 +44,6 @@ namespace modules {
std::atomic<int> m_level{-1};
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -22,7 +22,6 @@ using std::map;
#define DEFAULT_FORMAT "format"
#define DEFINE_MODULE(name, type) struct name : public type<name>
#define CONST_MOD(name) static_cast<name const&>(*this)
#define CAST_MOD(name) static_cast<name*>(this)
@ -101,6 +100,11 @@ namespace modules {
public:
virtual ~module_interface() {}
/**
* The type users have to specify in the module section `type` key
*/
virtual string type() const = 0;
/**
* Module name w/o 'module/' prefix
*/
@ -123,6 +127,8 @@ namespace modules {
module(const bar_settings bar, string name);
~module() noexcept;
string type() const;
string name_raw() const;
string name() const;
bool running() const;
@ -134,6 +140,8 @@ namespace modules {
bool input(string&& cmd);
string input_handler_name() const;
static constexpr auto TYPE = "";
protected:
void broadcast();
void idle();
@ -155,8 +163,8 @@ namespace modules {
mutex m_sleeplock;
std::condition_variable m_sleephandler;
string m_name;
string m_name_raw;
const string m_name;
const string m_name_raw;
unique_ptr<builder> m_builder;
unique_ptr<module_formatter> m_formatter;
vector<thread> m_threads;

View File

@ -46,6 +46,11 @@ namespace modules {
return m_name_raw;
}
template <typename Impl>
string module<Impl>::type() const {
return string(CONST_MOD(Impl).TYPE);
}
template <typename Impl>
bool module<Impl>::running() const {
return static_cast<bool>(m_enabled);

View File

@ -1,7 +1,6 @@
#pragma once
#include "common.hpp"
#include "modules/backlight.hpp"
#include "modules/battery.hpp"
#include "modules/bspwm.hpp"
@ -43,9 +42,7 @@
#if ENABLE_XKEYBOARD
#include "modules/xkeyboard.hpp"
#endif
#if not(ENABLE_I3 && ENABLE_MPD && ENABLE_NETWORK && ENABLE_ALSA && ENABLE_PULSEAUDIO && ENABLE_CURL && ENABLE_XKEYBOARD)
#include "modules/unsupported.hpp"
#endif
POLYBAR_NS
@ -53,63 +50,63 @@ using namespace modules;
namespace {
module_interface* make_module(string&& name, const bar_settings& bar, string module_name, const logger& m_log) {
if (name == "internal/counter") {
if (name == counter_module::TYPE) {
return new counter_module(bar, move(module_name));
} else if (name == "internal/backlight") {
} else if (name == backlight_module::TYPE) {
return new backlight_module(bar, move(module_name));
} else if (name == "internal/battery") {
} else if (name == battery_module::TYPE) {
return new battery_module(bar, move(module_name));
} else if (name == "internal/bspwm") {
} else if (name == bspwm_module::TYPE) {
return new bspwm_module(bar, move(module_name));
} else if (name == "internal/cpu") {
} else if (name == cpu_module::TYPE) {
return new cpu_module(bar, move(module_name));
} else if (name == "internal/date") {
} else if (name == date_module::TYPE) {
return new date_module(bar, move(module_name));
} else if (name == "internal/github") {
} else if (name == github_module::TYPE) {
return new github_module(bar, move(module_name));
} else if (name == "internal/fs") {
} else if (name == fs_module::TYPE) {
return new fs_module(bar, move(module_name));
} else if (name == "internal/memory") {
} else if (name == memory_module::TYPE) {
return new memory_module(bar, move(module_name));
} else if (name == "internal/i3") {
} else if (name == i3_module::TYPE) {
return new i3_module(bar, move(module_name));
} else if (name == "internal/mpd") {
} else if (name == mpd_module::TYPE) {
return new mpd_module(bar, move(module_name));
} else if (name == "internal/volume") {
m_log.warn("internal/volume is deprecated, use internal/alsa instead");
m_log.warn("internal/volume is deprecated, use %s instead", alsa_module::TYPE);
return new alsa_module(bar, move(module_name));
} else if (name == "internal/alsa") {
} else if (name == alsa_module::TYPE) {
return new alsa_module(bar, move(module_name));
} else if (name == "internal/pulseaudio") {
} else if (name == pulseaudio_module::TYPE) {
return new pulseaudio_module(bar, move(module_name));
} else if (name == "internal/network") {
} else if (name == network_module::TYPE) {
return new network_module(bar, move(module_name));
#if DEBUG
} else if (name == "internal/systray") {
} else if (name == systray_module::TYPE) {
return new systray_module(bar, move(module_name));
#endif
} else if (name == "internal/temperature") {
} else if (name == temperature_module::TYPE) {
return new temperature_module(bar, move(module_name));
} else if (name == "internal/xbacklight") {
} else if (name == xbacklight_module::TYPE) {
return new xbacklight_module(bar, move(module_name));
} else if (name == "internal/xkeyboard") {
} else if (name == xkeyboard_module::TYPE) {
return new xkeyboard_module(bar, move(module_name));
} else if (name == "internal/xwindow") {
} else if (name == xwindow_module::TYPE) {
return new xwindow_module(bar, move(module_name));
} else if (name == "internal/xworkspaces") {
} else if (name == xworkspaces_module::TYPE) {
return new xworkspaces_module(bar, move(module_name));
} else if (name == "custom/text") {
} else if (name == text_module::TYPE) {
return new text_module(bar, move(module_name));
} else if (name == "custom/script") {
} else if (name == script_module::TYPE) {
return new script_module(bar, move(module_name));
} else if (name == "custom/menu") {
} else if (name == menu_module::TYPE) {
return new menu_module(bar, move(module_name));
} else if (name == "custom/ipc") {
} else if (name == ipc_module::TYPE) {
return new ipc_module(bar, move(module_name));
} else {
throw application_error("Unknown module: " + name);
}
}
}
} // namespace
POLYBAR_NS_END

View File

@ -2,9 +2,9 @@
#include <chrono>
#include "utils/env.hpp"
#include "adapters/mpd.hpp"
#include "modules/meta/event_module.hpp"
#include "utils/env.hpp"
POLYBAR_NS
@ -24,6 +24,8 @@ namespace modules {
string get_output();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/mpd";
protected:
bool input(string&& cmd);
@ -103,6 +105,6 @@ namespace modules {
string m_toggle_on_color;
string m_toggle_off_color;
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -18,6 +18,8 @@ namespace modules {
string get_format() const;
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/network";
protected:
void subthread_routine();
@ -53,6 +55,6 @@ namespace modules {
bool m_accumulate{false};
bool m_unknown_up{false};
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -1,7 +1,7 @@
#pragma once
#include "settings.hpp"
#include "modules/meta/event_module.hpp"
#include "settings.hpp"
POLYBAR_NS
@ -22,6 +22,8 @@ namespace modules {
string get_output();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/pulseaudio";
protected:
bool input(string&& cmd);
@ -51,6 +53,6 @@ namespace modules {
atomic<int> m_volume{0};
atomic<double> m_decibels{0};
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -18,6 +18,8 @@ namespace modules {
string get_output();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "custom/script";
protected:
chrono::duration<double> process(const mutex_wrapper<function<chrono::duration<double>()>>& handler) const;
bool check_condition();
@ -44,6 +46,6 @@ namespace modules {
bool m_stopping{false};
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -19,6 +19,8 @@ namespace modules {
void update();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/systray";
protected:
bool input(string&& cmd);
@ -33,7 +35,7 @@ namespace modules {
bool m_hidden{false};
};
}
} // namespace modules
POLYBAR_NS_END
#endif

View File

@ -2,8 +2,8 @@
#include <istream>
#include "settings.hpp"
#include "modules/meta/timer_module.hpp"
#include "settings.hpp"
POLYBAR_NS
@ -18,6 +18,8 @@ namespace modules {
string get_format() const;
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/temperature";
private:
static constexpr auto TAG_LABEL = "<label>";
static constexpr auto TAG_LABEL_WARN = "<label-warn>";
@ -39,6 +41,6 @@ namespace modules {
// Whether or not to show units with the %temperature-X% tokens
bool m_units{true};
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -12,7 +12,9 @@ namespace modules {
void update() {}
string get_format() const;
string get_output();
static constexpr auto TYPE = "custom/text";
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -1,7 +1,4 @@
#pragma once
#if ENABLE_I3 && ENABLE_MPD && ENABLE_NETWORK && ENABLE_ALSA && ENABLE_PULSEAUDIO && ENABLE_CURL && ENABLE_XKEYBOARD
#error "Support has been enabled for all optional modules"
#endif
#include "modules/meta/base.hpp"
#include "modules/meta/base.inl"
@ -17,6 +14,9 @@ namespace modules {
MODULE_NAME(const bar_settings, string) { \
throw application_error("No built-in support for '" + string{MODULE_TYPE} + "'"); \
} \
string type() const { \
return ""; \
} \
string name_raw() const { \
return ""; \
} \
@ -61,6 +61,6 @@ namespace modules {
#if not ENABLE_XKEYBOARD
DEFINE_UNSUPPORTED_MODULE(xkeyboard_module, "internal/xkeyboard");
#endif
}
} // namespace modules
POLYBAR_NS_END

View File

@ -1,9 +1,9 @@
#pragma once
#include "components/config.hpp"
#include "settings.hpp"
#include "modules/meta/event_handler.hpp"
#include "modules/meta/static_module.hpp"
#include "settings.hpp"
#include "x11/extensions/randr.hpp"
POLYBAR_NS
@ -21,11 +21,8 @@ namespace modules {
* This module is a lot faster, it's more responsive and it will
* be dormant until new values are reported. Inotify watches
* are a bit random when it comes to proc-/sysfs.
*
* TODO: Implement backlight configuring using scroll events
*/
class xbacklight_module : public static_module<xbacklight_module>,
public event_handler<evt::randr_notify> {
class xbacklight_module : public static_module<xbacklight_module>, public event_handler<evt::randr_notify> {
public:
explicit xbacklight_module(const bar_settings& bar, string name_);
@ -33,6 +30,8 @@ namespace modules {
string get_output();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/xbacklight";
protected:
void handle(const evt::randr_notify& evt);
bool input(string&& cmd);
@ -56,6 +55,6 @@ namespace modules {
bool m_scroll{true};
std::atomic<int> m_percentage{0};
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -26,6 +26,8 @@ namespace modules {
void update();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/xkeyboard";
protected:
bool query_keyboard();
bool blacklisted(const string& indicator_name);
@ -42,7 +44,7 @@ namespace modules {
static constexpr const char* FORMAT_DEFAULT{"<label-layout> <label-indicator>"};
static constexpr const char* DEFAULT_LAYOUT_ICON{"layout-icon-default"};
static constexpr const char* DEFAULT_INDICATOR_ICON{"indicator-icon-default"};
static constexpr const char* EVENT_SWITCH{"xkeyboard/switch"};
connection& m_connection;
@ -63,6 +65,6 @@ namespace modules {
iconset_t m_indicator_icons_on;
iconset_t m_indicator_icons_off;
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -30,16 +30,14 @@ namespace modules {
*/
class xwindow_module : public static_module<xwindow_module>, public event_handler<evt::property_notify> {
public:
enum class state {
NONE,
ACTIVE,
EMPTY
};
enum class state { NONE, ACTIVE, EMPTY };
explicit xwindow_module(const bar_settings&, string);
void update(bool force = false);
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/xwindow";
protected:
void handle(const evt::property_notify& evt);
@ -51,6 +49,6 @@ namespace modules {
map<state, label_t> m_statelabels;
label_t m_label;
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -50,8 +50,7 @@ namespace modules {
/**
* Module used to display EWMH desktops
*/
class xworkspaces_module : public static_module<xworkspaces_module>,
public event_handler<evt::property_notify> {
class xworkspaces_module : public static_module<xworkspaces_module>, public event_handler<evt::property_notify> {
public:
explicit xworkspaces_module(const bar_settings& bar, string name_);
@ -59,6 +58,8 @@ namespace modules {
string get_output();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/xworkspaces";
protected:
void handle(const evt::property_notify& evt);

View File

@ -422,7 +422,7 @@ void controller::process_inputdata() {
// Forwards the action to all input handlers that match the name
for (auto&& handler : m_inputhandlers) {
if (handler->input_handler_name() == handler_name) {
if(!handler->input(string{action})) {
if (!handler->input(string{action})) {
m_log.warn("The '%s' module does not support the '%s' action.", handler_name, action);
}
@ -432,9 +432,8 @@ void controller::process_inputdata() {
if (num_delivered == 0) {
m_log.err("There exists no input handler with name '%s' (input: %s)", handler_name, cmd);
}
else {
m_log.info("Delivered input to %d input handler%s", num_delivered, num_delivered > 1? "s": "");
} else {
m_log.info("Delivered input to %d input handler%s", num_delivered, num_delivered > 1 ? "s" : "");
}
return;
@ -600,7 +599,7 @@ size_t controller::setup_modules(alignment align) {
try {
auto type = m_conf.get("module/" + module_name, "type");
if (type == "custom/ipc" && !m_ipc) {
if (type == ipc_module::TYPE && !m_ipc) {
throw application_error("Inter-process messaging needs to be enabled");
}

View File

@ -1,6 +1,6 @@
#include "modules/date.hpp"
#include "drawtypes/label.hpp"
#include "drawtypes/label.hpp"
#include "modules/meta/base.inl"
POLYBAR_NS
@ -93,6 +93,6 @@ namespace modules {
wakeup();
return true;
}
}
} // namespace modules
POLYBAR_NS_END