polybar-dwm/include/modules/bspwm.hpp
Patrick Ziegler 26be83f893
module: Implement action router (#2336)
* module: Implement proof of concept action router

Action implementation inside module becomes much cleaner because each
module just registers action names together with a callback (pointer to
member function) and the action router does the rest.

* Make input function final

This forces all modules to use the action router

* modules: Catch exceptions in action handlers

* Use action router for all modules

* Use action_ prefix for function names

The mpd module's 'stop' action overwrote the base module's stop function
which caused difficult to debug behavior.

To prevent this in the future we now prefix each function that is
responsible for an action with 'action_'

* Cleanup

* actions: Throw exception when re-registering action

Action names are unique inside modules. Unfortunately there is no way to
ensure this statically, the next best thing is to crash the module and
let the user know that this is a bug.

* Formatting

* actions: Ignore data for actions without data

This is the same behavior as before.

* action_router: Write tests
2021-01-04 10:25:52 +01:00

102 lines
2.4 KiB
C++

#pragma once
#include "modules/meta/event_module.hpp"
#include "utils/bspwm.hpp"
POLYBAR_NS
namespace modules {
class bspwm_module : public event_module<bspwm_module> {
public:
enum class state {
NONE = 0U,
EMPTY,
OCCUPIED,
FOCUSED,
URGENT,
DIMMED, // used when the monitor is out of focus
};
enum class mode {
NONE = 0U,
LAYOUT_MONOCLE,
LAYOUT_TILED,
STATE_FULLSCREEN,
STATE_FLOATING,
STATE_PSEUDOTILED,
NODE_LOCKED,
NODE_STICKY,
NODE_PRIVATE,
NODE_MARKED
};
struct bspwm_monitor {
vector<pair<unsigned int, label_t>> workspaces;
vector<label_t> modes;
label_t label;
string name;
bool focused{false};
};
public:
explicit bspwm_module(const bar_settings&, string);
void stop();
bool has_event();
bool update();
string get_output();
bool build(builder* builder, const string& tag) const;
static constexpr auto TYPE = "internal/bspwm";
static constexpr auto EVENT_FOCUS = "focus";
static constexpr auto EVENT_NEXT = "next";
static constexpr auto EVENT_PREV = "prev";
protected:
void action_focus(const string& data);
void action_next();
void action_prev();
void focus_direction(bool next);
void send_command(const string& payload_cmd, const string& log_info);
private:
bool handle_status(string& data);
static constexpr auto DEFAULT_ICON = "ws-icon-default";
static constexpr auto DEFAULT_LABEL = "%icon% %name%";
static constexpr auto DEFAULT_MONITOR_LABEL = "%name%";
static constexpr auto TAG_LABEL_MONITOR = "<label-monitor>";
static constexpr auto TAG_LABEL_STATE = "<label-state>";
static constexpr auto TAG_LABEL_MODE = "<label-mode>";
bspwm_util::connection_t m_subscriber;
vector<unique_ptr<bspwm_monitor>> m_monitors;
map<mode, label_t> m_modelabels;
map<unsigned int, label_t> m_statelabels;
label_t m_monitorlabel;
iconset_t m_icons;
/**
* Separator that is inserted in between workspaces
*/
label_t m_labelseparator;
bool m_click{true};
bool m_scroll{true};
bool m_revscroll{true};
bool m_pinworkspaces{true};
bool m_inlinemode{false};
string_util::hash_type m_hash{0U};
bool m_fuzzy_match{false};
// used while formatting output
size_t m_index{0U};
};
} // namespace modules
POLYBAR_NS_END