dwm: Refactor code for new action handling

This is since the polybar 3.5.0 release
This commit is contained in:
Mihir Lad 2020-12-19 19:17:42 -05:00
parent 254a23344a
commit 78475b7806
5 changed files with 76 additions and 109 deletions

View File

@ -16,6 +16,7 @@ if [ "$POLYBAR_BUILD_TYPE" != "minimal" ]; then
ENABLE_CURL=ON
ENABLE_ALSA=ON
ENABLE_I3=ON
ENABLE_DWM=ON
WITH_XRM=ON
WITH_XKB=ON
WITH_XRANDR_MONITORS=ON
@ -39,6 +40,7 @@ cmake \
-DENABLE_CURL="${ENABLE_CURL:-OFF}" \
-DENABLE_ALSA="${ENABLE_ALSA:-OFF}" \
-DENABLE_I3="${ENABLE_I3:-OFF}" \
-DENABLE_DWM="${ENABLE_DWM:-OFF}" \
-DWITH_XRM="${WITH_XRM:-OFF}" \
-DWITH_XKB="${WITH_XKB:-OFF}" \
-DWITH_XRANDR_MONITORS="${WITH_XRANDR_MONITORS:-OFF}" \

View File

@ -3,12 +3,11 @@
#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 {
class dwm_module : public event_module<dwm_module> {
public:
explicit dwm_module(const bar_settings&, string);
@ -47,13 +46,31 @@ namespace modules {
label_t label;
};
static constexpr auto TYPE = "internal/dwm";
void stop() override;
bool has_event();
bool update();
bool build(builder* builder, const string& tag) const;
/**
* DWM command for changing the view to a tag with the specified bit mask
*/
static constexpr const char* EVENT_TAG_VIEW{"view"};
/**
* DWM command for toggling the selected state of a tag with the specified
* bit mask
*/
static constexpr const char* EVENT_TAG_TOGGLE_VIEW{"toggleview"};
/**
* DWM command for setting the layout to a layout specified by the address
*/
static constexpr const char* EVENT_LAYOUT_SET{"setlayoutsafe"};
protected:
bool input(string&& cmd) override;
bool input(const string& action, const string& data) override;
private:
static constexpr const char* DEFAULT_FORMAT_TAGS{"<label-tags> <label-layout> <label-floating> <label-title>"};
@ -85,27 +102,6 @@ namespace modules {
*/
static constexpr const char* TAG_LABEL_TITLE{"<label-title>"};
/**
* All input handler commands start with this
*/
static constexpr const char* EVENT_PREFIX{"dwm-"};
/**
* DWM command for changing the view to a tag with the specified bit mask
*/
static constexpr const char* CMD_TAG_VIEW{"view"};
/**
* DWM command for toggling the selected state of a tag with the specified
* bit mask
*/
static constexpr const char* CMD_TAG_TOGGLE_VIEW{"toggleview"};
/**
* DWM command for setting the layout to a layout specified by the address
*/
static constexpr const char* CMD_LAYOUT_SET{"setlayoutsafe"};
/**
* Called by has_event on layout changes. This updates the layout label
*
@ -229,7 +225,7 @@ namespace modules {
*/
const dwmipc::Layout* prev_layout(const dwmipc::Layout& layout, bool wrap) const;
/**
/**
* Get the address of the next tag in m_tags or return NULL if not applicable
*
* @param ignore_empty Ignore empty tags
@ -243,26 +239,6 @@ namespace modules {
*/
const tag_t* prev_scrollable_tag(bool ignore_empty) const;
/**
* Check if the command matches the specified IPC command name and if so,
* parse and send the command to dwm
*
* @param cmd The command string given by dwm_modue::input
* @param ipc_cmd The name of dwm IPC command to check for
*
* @return true if the command matched, was succesfully parsed, and sent to
* dwm, false otherwise
*/
bool check_send_cmd(string cmd, const string& ipc_cmd);
/**
* Helper function to build cmd string
*
* @param ipc_cmd The dwm IPC command name
* @param arg The argument to the dwm command
*/
static string build_cmd(const char* ipc_cmd, const string& arg);
/**
* Attempt to connect to any disconnected dwm sockets. Catch errors.
*/
@ -283,24 +259,24 @@ namespace modules {
*/
bool m_layout_scroll{true};
/**
* If true, scrolling the bar cycles through the available tags
*/
/**
* If true, scrolling the bar cycles through the available tags
*/
bool m_tags_scroll{false};
/**
* If true, scrolling the bar cycles through the available tags backwards
*/
/**
* If true, scrolling the bar cycles through the available tags backwards
*/
bool m_tags_scroll_reverse{false};
/**
* If true, wrap tag when scrolling
*/
/**
* If true, wrap tag when scrolling
*/
bool m_tags_scroll_wrap{false};
/**
* If true, scrolling will view all tags regardless if occupied
*/
/**
* If true, scrolling will view all tags regardless if occupied
*/
bool m_tags_scroll_empty{false};
/**

View File

@ -65,7 +65,7 @@ namespace {
return new cpu_module(bar, move(module_name));
} else if (name == date_module::TYPE) {
return new date_module(bar, move(module_name));
} else if (name == "internal/dwm") {
} else if (name == dwm_module::TYPE) {
return new dwm_module(bar, move(module_name));
} else if (name == github_module::TYPE) {
return new github_module(bar, move(module_name));

View File

@ -452,6 +452,15 @@ bool controller::try_forward_legacy_action(const string& cmd) {
A_MAP("i3wm-wsfocus-", i3_module, EVENT_FOCUS),
A_MAP("i3wm-wsnext", i3_module, EVENT_NEXT),
A_MAP("i3wm-wsprev", i3_module, EVENT_PREV),
#endif
// Has data
#if ENABLE_DWM
// Has data
A_MAP("dwm-view-", dwm_module, EVENT_TAG_VIEW),
// Has data
A_MAP("dwm-toggleview-", dwm_module, EVENT_TAG_TOGGLE_VIEW),
// Has data
A_MAP("dwm-setlayoutsafe-", dwm_module, EVENT_LAYOUT_SET),
#endif
// Has data
A_MAP("menu-open-", menu_module, EVENT_OPEN),

View File

@ -176,10 +176,6 @@ namespace modules {
return true;
}
string dwm_module::build_cmd(const char* ipc_cmd, const string& arg) {
return EVENT_PREFIX + string(ipc_cmd) + "-" + arg;
}
bool dwm_module::build(builder* builder, const string& tag) const {
m_log.info("%s: Building module", name());
if (tag == TAG_LABEL_TITLE) {
@ -196,9 +192,9 @@ namespace modules {
if (m_layout_click) {
// Toggle between secondary and default layout
auto addr = (m_current_layout == m_default_layout ? m_secondary_layout : m_default_layout)->address;
builder->cmd(mousebtn::LEFT, build_cmd(CMD_LAYOUT_SET, to_string(addr)));
builder->action(mousebtn::LEFT, *this, EVENT_LAYOUT_SET, to_string(addr));
// Set previous layout
builder->cmd(mousebtn::RIGHT, build_cmd(CMD_LAYOUT_SET, "0"));
builder->action(mousebtn::RIGHT, *this, EVENT_LAYOUT_SET, "0");
}
if (m_layout_scroll) {
auto addr_next = next_layout(*m_current_layout, m_layout_wrap)->address;
@ -206,34 +202,34 @@ namespace modules {
// Set address based on scroll direction
auto scroll_down_addr = to_string(m_layout_reverse ? addr_prev : addr_next);
auto scroll_up_addr = to_string(m_layout_reverse ? addr_next : addr_prev);
builder->cmd(mousebtn::SCROLL_DOWN, build_cmd(CMD_LAYOUT_SET, scroll_down_addr));
builder->cmd(mousebtn::SCROLL_UP, build_cmd(CMD_LAYOUT_SET, scroll_up_addr));
builder->action(mousebtn::SCROLL_DOWN, *this, EVENT_LAYOUT_SET, scroll_down_addr);
builder->action(mousebtn::SCROLL_UP, *this, EVENT_LAYOUT_SET, scroll_up_addr);
}
builder->node(m_layout_label);
if (m_layout_scroll) {
builder->cmd_close();
builder->cmd_close();
builder->action_close();
builder->action_close();
}
if (m_layout_click) {
builder->cmd_close();
builder->cmd_close();
builder->action_close();
builder->action_close();
}
} else if (tag == TAG_LABEL_TAGS) {
m_log.info("%s: Building tags label", name());
int cmd_count = 0;
if (m_tags_scroll) {
bool ignore_empty_tags = !m_tags_scroll_empty && m_conf.get<string>(name(), "label-empty", "").empty();
auto next =
const auto* next =
m_tags_scroll_reverse ? prev_scrollable_tag(ignore_empty_tags) : next_scrollable_tag(ignore_empty_tags);
auto prev =
const auto* prev =
m_tags_scroll_reverse ? next_scrollable_tag(ignore_empty_tags) : prev_scrollable_tag(ignore_empty_tags);
if (next != NULL) {
if (next != nullptr) {
++cmd_count;
builder->cmd(mousebtn::SCROLL_DOWN, build_cmd(CMD_TAG_VIEW, to_string(next->bit_mask)));
builder->action(mousebtn::SCROLL_DOWN, *this, EVENT_TAG_VIEW, to_string(next->bit_mask));
}
if (prev != NULL) {
if (prev != nullptr) {
++cmd_count;
builder->cmd(mousebtn::SCROLL_UP, build_cmd(CMD_TAG_VIEW, to_string(prev->bit_mask)));
builder->action(mousebtn::SCROLL_UP, *this, EVENT_TAG_VIEW, to_string(prev->bit_mask));
}
}
@ -247,17 +243,17 @@ namespace modules {
}
if (m_tags_click) {
builder->cmd(mousebtn::LEFT, build_cmd(CMD_TAG_VIEW, to_string(tag.bit_mask)));
builder->cmd(mousebtn::RIGHT, build_cmd(CMD_TAG_TOGGLE_VIEW, to_string(tag.bit_mask)));
builder->action(mousebtn::LEFT, *this, EVENT_TAG_VIEW, to_string(tag.bit_mask));
builder->action(mousebtn::RIGHT, *this, EVENT_TAG_TOGGLE_VIEW, to_string(tag.bit_mask));
builder->node(tag.label);
builder->cmd_close();
builder->cmd_close();
builder->action_close();
builder->action_close();
} else {
builder->node(tag.label);
}
}
for (; cmd_count; --cmd_count) {
builder->cmd_close();
builder->action_close();
}
} else {
return false;
@ -265,39 +261,23 @@ namespace modules {
return true;
}
bool dwm_module::check_send_cmd(string cmd, const string& ipc_cmd) {
// cmd = <EVENT_PREFIX><ipc_cmd>-<arg>
cmd.erase(0, strlen(EVENT_PREFIX));
bool dwm_module::input(const string& action, const string& data) {
m_log.info("%s: Sending workspace %s command to ipc handler", name(), action);
// cmd = <ipc_cmd>-<arg>
if (cmd.compare(0, ipc_cmd.size(), ipc_cmd) == 0) {
// Erase '<ipc_cmd>-'
cmd.erase(0, ipc_cmd.size() + 1);
m_log.info("%s: Sending workspace %s command to ipc handler", name(), ipc_cmd);
try {
m_ipc->run_command(ipc_cmd, (Json::UInt64)stoul(cmd));
return true;
} catch (const dwmipc::SocketClosedError& err) {
m_log.err("%s: Disconnected from socket: %s", name(), err.what());
sleep(chrono::duration<double>(1));
reconnect_dwm();
} catch (const dwmipc::IPCError& err) {
throw module_error(err.what());
}
try {
m_ipc->run_command(action, (Json::UInt64)stoul(data));
return true;
} catch (const dwmipc::SocketClosedError& err) {
m_log.err("%s: Disconnected from socket: %s", name(), err.what());
sleep(chrono::duration<double>(1));
reconnect_dwm();
} catch (const dwmipc::IPCError& err) {
throw module_error(err.what());
}
return false;
}
bool dwm_module::input(string&& cmd) {
if (cmd.find(EVENT_PREFIX) != 0) {
return false;
}
return check_send_cmd(cmd, CMD_TAG_VIEW) || check_send_cmd(cmd, CMD_TAG_TOGGLE_VIEW) ||
check_send_cmd(cmd, CMD_LAYOUT_SET);
}
dwm_module::state_t dwm_module::get_state(tag_mask_t bit_mask) const {
/**
* ---------------------------------------------------------------