2020-07-17 04:35:15 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <dwmipcpp/connection.hpp>
|
|
|
|
|
|
|
|
#include "modules/meta/event_module.hpp"
|
|
|
|
#include "modules/meta/input_handler.hpp"
|
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
|
|
|
namespace modules {
|
|
|
|
class dwm_module : public event_module<dwm_module>, public input_handler {
|
|
|
|
public:
|
|
|
|
explicit dwm_module(const bar_settings&, string);
|
|
|
|
|
|
|
|
using tag_mask_t = unsigned int;
|
2020-07-20 04:45:12 +00:00
|
|
|
using window_t = unsigned int;
|
2020-07-17 04:35:15 +00:00
|
|
|
|
2020-07-20 05:11:16 +00:00
|
|
|
/**
|
|
|
|
* Represents the relevant states a tag can have
|
|
|
|
*/
|
2020-07-17 04:35:15 +00:00
|
|
|
enum class state_t : uint8_t {
|
|
|
|
FOCUSED, ///< Monitor is selected and tag is selected, overrides all below
|
|
|
|
URGENT, ///< Tag is urgent, overrides all below
|
|
|
|
UNFOCUSED, ///< Monitor is not selected, but tag is selected
|
|
|
|
VISIBLE, ///< Tag is not selected, but occupied
|
2020-07-20 04:29:43 +00:00
|
|
|
EMPTY ///< Tag is unoccupied and unselected
|
2020-07-17 04:35:15 +00:00
|
|
|
};
|
|
|
|
|
2020-07-20 05:11:16 +00:00
|
|
|
/**
|
|
|
|
* Associates important properties of a tag
|
|
|
|
*/
|
2020-07-17 04:35:15 +00:00
|
|
|
struct tag_t {
|
2020-07-20 05:11:16 +00:00
|
|
|
/**
|
|
|
|
* Construct a tag_t object
|
|
|
|
*
|
|
|
|
* @param name Name of tag
|
|
|
|
* @param tag_mask_t Bit mask that represents this tag
|
|
|
|
* @param state Current state of the tag
|
|
|
|
* @param label Label to use for building tag on bar
|
|
|
|
*/
|
2020-07-20 04:45:12 +00:00
|
|
|
tag_t(string& name, tag_mask_t bit_mask, state_t state, label_t&& label)
|
2020-07-17 04:35:15 +00:00
|
|
|
: name(name), bit_mask(bit_mask), state(state), label(forward<label_t>(label)) {}
|
|
|
|
|
|
|
|
string name;
|
2020-07-20 04:45:12 +00:00
|
|
|
tag_mask_t bit_mask;
|
2020-07-17 04:35:15 +00:00
|
|
|
state_t state;
|
|
|
|
label_t label;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto stop() -> void override;
|
|
|
|
auto has_event() -> bool;
|
|
|
|
auto update() -> bool;
|
|
|
|
auto build(builder* builder, const string& tag) const -> bool;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
auto input(string&& cmd) -> bool override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr const char* DEFAULT_FORMAT_TAGS{"<label-state> <label-layout> <label-title>"};
|
2020-07-20 04:27:46 +00:00
|
|
|
static constexpr const char* DEFAULT_STATE_LABEL{"%name%"};
|
2020-07-17 04:35:15 +00:00
|
|
|
|
2020-07-20 05:11:16 +00:00
|
|
|
/**
|
|
|
|
* The state label is used to represent a tag. This label is replaced by one
|
|
|
|
* of the following labels based on the tag state:
|
|
|
|
* * label-focused
|
|
|
|
* * label-unfocused
|
|
|
|
* * label-visible
|
|
|
|
* * label-urgent
|
|
|
|
* * label-empty
|
|
|
|
*/
|
2020-07-17 04:35:15 +00:00
|
|
|
static constexpr const char* TAG_LABEL_STATE{"<label-state>"};
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The layout label is replaced by the current layout symbol
|
|
|
|
*/
|
2020-07-17 04:35:15 +00:00
|
|
|
static constexpr const char* TAG_LABEL_LAYOUT{"<label-layout>"};
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The title layout is replaced by the currently focused window title
|
|
|
|
*/
|
2020-07-17 04:35:15 +00:00
|
|
|
static constexpr const char* TAG_LABEL_TITLE{"<label-title>"};
|
|
|
|
|
2020-07-20 02:52:50 +00:00
|
|
|
static constexpr const char* EVENT_PREFIX{"dwm-"};
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Event name is same as the IPC command name to view the tag clicked on
|
|
|
|
*/
|
2020-07-21 03:43:12 +00:00
|
|
|
static constexpr const char* EVENT_TAG_LCLICK{"view"};
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Event name is same as IPC command name to toggle the view of the tag
|
|
|
|
* clicked on
|
|
|
|
*/
|
2020-07-21 03:43:12 +00:00
|
|
|
static constexpr const char* EVENT_TAG_RCLICK{"toggleview"};
|
2020-07-20 02:52:50 +00:00
|
|
|
|
2020-07-20 05:11:16 +00:00
|
|
|
/**
|
|
|
|
* Called by has_event on layout changes. This updates the layout label
|
|
|
|
*
|
|
|
|
* @param ev Event data
|
|
|
|
*/
|
2020-07-20 02:52:50 +00:00
|
|
|
void on_layout_change(const dwmipc::LayoutChangeEvent& ev);
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by has_event when a new monitor is in focus. This updates
|
|
|
|
* m_active_mon to keep track of the currently active monitor.
|
|
|
|
*
|
|
|
|
* @param ev Event data
|
|
|
|
*/
|
2020-07-20 02:52:50 +00:00
|
|
|
void on_monitor_focus_change(const dwmipc::MonitorFocusChangeEvent& ev);
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by has_event when any of the tag states change. This updates the
|
|
|
|
* m_tags array and their states/labels.
|
|
|
|
*
|
|
|
|
* @param ev Event data
|
|
|
|
*/
|
2020-07-20 02:52:50 +00:00
|
|
|
void on_tag_change(const dwmipc::TagChangeEvent& ev);
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by has_event when a new client is in focus. This updates
|
|
|
|
* m_focused_client_id and updates the title label
|
|
|
|
*
|
|
|
|
* @param ev Event data
|
|
|
|
*/
|
2020-07-20 02:52:50 +00:00
|
|
|
void on_client_focus_change(const dwmipc::ClientFocusChangeEvent& ev);
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by has_event when the title of the currently focused window
|
|
|
|
* changes. This updates the title label.
|
|
|
|
*
|
|
|
|
* @param ev Event data
|
|
|
|
*/
|
2020-07-20 02:52:50 +00:00
|
|
|
void on_focused_title_change(const dwmipc::FocusedTitleChangeEvent& ev);
|
2020-07-17 04:35:15 +00:00
|
|
|
|
2020-07-20 05:11:16 +00:00
|
|
|
/**
|
|
|
|
* Get a list of monitors from dwm, store them in m_monitors, and update the
|
|
|
|
* pointers to the active monitor and bar monitor.
|
|
|
|
*
|
|
|
|
* @param ev Event data
|
|
|
|
*/
|
2020-07-17 04:35:15 +00:00
|
|
|
void update_monitor_ref();
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the labels for each tag based on their state
|
|
|
|
*/
|
2020-07-20 02:52:50 +00:00
|
|
|
void update_tag_labels();
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the window title of the specified client from dwm and update the
|
|
|
|
* title label
|
|
|
|
*
|
|
|
|
* @param client_id The window XID of the client
|
|
|
|
*/
|
2020-07-20 04:45:12 +00:00
|
|
|
void update_title_label(window_t client_id);
|
2020-07-20 02:52:50 +00:00
|
|
|
|
2020-07-20 05:11:16 +00:00
|
|
|
/**
|
|
|
|
* Translate the tag's tag states to a state_t enum value
|
|
|
|
*
|
|
|
|
* @param bit_mask Bit mask of the tag
|
|
|
|
*
|
|
|
|
* @return state_t enum value representing the state of the tag
|
|
|
|
*/
|
2020-07-20 02:52:50 +00:00
|
|
|
auto get_state(tag_mask_t bit_mask) const -> state_t;
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the command matches the specified event name and if so, send a
|
|
|
|
* command to dwm after parsing the command.
|
|
|
|
*
|
|
|
|
* @param cmd The command string given by dwm_modue::input
|
|
|
|
* @param ev_name The name of the event to check for (should be the same as
|
|
|
|
* the dwm command name)
|
|
|
|
*
|
|
|
|
* @return true if the command matched, was succesfully parsed, and sent to
|
|
|
|
* dwm, false otherwise
|
|
|
|
*/
|
2020-07-20 04:13:10 +00:00
|
|
|
auto check_send_cmd(string cmd, const string& ev_name) -> bool;
|
2020-07-20 05:11:16 +00:00
|
|
|
|
2020-07-21 03:43:12 +00:00
|
|
|
/**
|
|
|
|
* Helper function to build cmd string
|
|
|
|
*
|
|
|
|
* @param ev The event name (should be same as dwm command name)
|
|
|
|
* @param arg The argument to the dwm command
|
|
|
|
*/
|
|
|
|
auto static build_cmd(const char* ev, const string& arg) -> string;
|
|
|
|
|
2020-07-20 05:11:16 +00:00
|
|
|
/**
|
|
|
|
* Attempt to connect to any disconnected dwm sockets. Catch errors.
|
|
|
|
*/
|
2020-07-20 02:52:50 +00:00
|
|
|
auto reconnect_dwm() -> bool;
|
2020-07-17 04:35:15 +00:00
|
|
|
|
2020-07-20 05:11:16 +00:00
|
|
|
/**
|
|
|
|
* If true, enables the click handlers for the tags
|
|
|
|
*/
|
2020-07-17 04:35:15 +00:00
|
|
|
bool m_click{true};
|
|
|
|
|
2020-07-20 05:11:16 +00:00
|
|
|
/**
|
|
|
|
* Holds the address to the currently active monitor in the m_monitors array
|
|
|
|
*/
|
2020-07-20 02:52:50 +00:00
|
|
|
const dwmipc::Monitor* m_active_mon = nullptr;
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the address to the bar monitor in the m_monitors array
|
|
|
|
*/
|
2020-07-20 02:52:50 +00:00
|
|
|
const dwmipc::Monitor* m_bar_mon = nullptr;
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* XID of the currently focused client
|
|
|
|
*/
|
2020-07-20 04:45:12 +00:00
|
|
|
window_t m_focused_client_id = 0;
|
2020-07-20 02:52:50 +00:00
|
|
|
|
2020-07-20 05:11:16 +00:00
|
|
|
/**
|
|
|
|
* Current layout symbol
|
|
|
|
*/
|
2020-07-17 04:35:15 +00:00
|
|
|
label_t m_layout_label;
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserted between tags
|
|
|
|
*/
|
2020-07-17 04:35:15 +00:00
|
|
|
label_t m_seperator_label;
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Title of the currently focused window on the bar's monitor
|
|
|
|
*/
|
2020-07-17 04:35:15 +00:00
|
|
|
label_t m_title_label;
|
|
|
|
|
2020-07-20 05:11:16 +00:00
|
|
|
/**
|
|
|
|
* Connection to DWM
|
|
|
|
*/
|
2020-07-20 02:52:50 +00:00
|
|
|
unique_ptr<dwmipc::Connection> m_ipc;
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Vector of monitors returned by m_ipc->get_monitors
|
|
|
|
*/
|
2020-07-21 03:43:12 +00:00
|
|
|
shared_ptr<vector<dwmipc::Monitor>> m_monitors;
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps state_t enum values to their corresponding labels
|
|
|
|
*/
|
2020-07-17 04:35:15 +00:00
|
|
|
std::unordered_map<state_t, label_t> m_state_labels;
|
2020-07-20 05:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Vector of all tags
|
|
|
|
*/
|
2020-07-17 04:35:15 +00:00
|
|
|
vector<tag_t> m_tags;
|
|
|
|
};
|
|
|
|
} // namespace modules
|
|
|
|
|
|
|
|
POLYBAR_NS_END
|