dwm: Define command name instead of event name

Instead of defining the event name for each event, define the command name that
is to be called for that event. This simplifies the code since many events call
the same command. It is already clear from the cmd function which event is being
registered, so it is more clear to see the association between the command and
the event in the code this way.
This commit is contained in:
Mihir Lad 2020-07-21 18:57:58 -04:00
parent f3047ab1e5
commit 33c8fb2ac7
2 changed files with 36 additions and 54 deletions

View File

@ -80,42 +80,26 @@ namespace modules {
*/ */
static constexpr const char* TAG_LABEL_TITLE{"<label-title>"}; static constexpr const char* TAG_LABEL_TITLE{"<label-title>"};
/**
* All input handler commands start with this
*/
static constexpr const char* EVENT_PREFIX{"dwm-"}; static constexpr const char* EVENT_PREFIX{"dwm-"};
/** /**
* Event name is same as the IPC command name to view the tag clicked on * DWM command for changing the view to a tag with the specified bit mask
*/ */
static constexpr const char* EVENT_TAG_LCLICK{"view"}; static constexpr const char* CMD_TAG_VIEW{"view"};
/** /**
* Event name is same as IPC command name to toggle the view of the tag * DWM command for toggling the selected state of a tag with the specified
* clicked on * bit mask
*/ */
static constexpr const char* EVENT_TAG_RCLICK{"toggleview"}; static constexpr const char* CMD_TAG_TOGGLE_VIEW{"toggleview"};
/** /**
* Event name is same as the IPC command name to set the layout to the * DWM command for setting the layout to a layout specified by the address
* secondary layout when the layout symbol is clicked
*/ */
static constexpr const char* EVENT_LAYOUT_LCLICK{"setlayoutsafe"}; static constexpr const char* CMD_LAYOUT_SET{"setlayoutsafe"};
/**
* Event name is same as the IPC command name to set the layout to the
* last layout.
*/
static constexpr const char* EVENT_LAYOUT_RCLICK{"setlayoutsafe"};
/**
* Event name is same as the IPC command name to set the layout to the
* previous layout in the array.
*/
static constexpr const char* EVENT_LAYOUT_SDOWN{"setlayoutsafe"};
/**
* Event name is same as the IPC command name to set the layout to the
* next layout in the array.
*/
static constexpr const char* EVENT_LAYOUT_SUP{"setlayoutsafe"};
/** /**
* Called by has_event on layout changes. This updates the layout label * Called by has_event on layout changes. This updates the layout label
@ -213,25 +197,24 @@ namespace modules {
auto prev_layout(const dwmipc::Layout& layout, bool wrap) const -> const dwmipc::Layout*; auto prev_layout(const dwmipc::Layout& layout, bool wrap) const -> const dwmipc::Layout*;
/** /**
* Check if the command matches the specified event name and if so, send a * Check if the command matches the specified IPC command name and if so,
* command to dwm after parsing the command. * parse and send the command to dwm
* *
* @param cmd The command string given by dwm_modue::input * @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 * @param ipc_cmd The name of dwm IPC command to check for
* the dwm command name)
* *
* @return true if the command matched, was succesfully parsed, and sent to * @return true if the command matched, was succesfully parsed, and sent to
* dwm, false otherwise * dwm, false otherwise
*/ */
auto check_send_cmd(string cmd, const string& ev_name) -> bool; auto check_send_cmd(string cmd, const string& ipc_cmd) -> bool;
/** /**
* Helper function to build cmd string * Helper function to build cmd string
* *
* @param ev The event name (should be same as dwm command name) * @param ipc_cmd The dwm IPC command name
* @param arg The argument to the dwm command * @param arg The argument to the dwm command
*/ */
auto static build_cmd(const char* ev, const string& arg) -> string; auto static build_cmd(const char* ipc_cmd, const string& arg) -> string;
/** /**
* Attempt to connect to any disconnected dwm sockets. Catch errors. * Attempt to connect to any disconnected dwm sockets. Catch errors.

View File

@ -147,8 +147,8 @@ namespace modules {
return true; return true;
} }
auto dwm_module::build_cmd(const char* ev, const string& arg) -> string { auto dwm_module::build_cmd(const char* ipc_cmd, const string& arg) -> string {
return EVENT_PREFIX + string(ev) + "-" + arg; return EVENT_PREFIX + string(ipc_cmd) + "-" + arg;
} }
auto dwm_module::build(builder* builder, const string& tag) const -> bool { auto dwm_module::build(builder* builder, const string& tag) const -> bool {
@ -158,19 +158,19 @@ namespace modules {
if (m_layout_click) { if (m_layout_click) {
// Toggle between secondary and default layout // Toggle between secondary and default layout
auto addr = (m_current_layout == m_default_layout ? m_secondary_layout : m_default_layout)->address; auto addr = (m_current_layout == m_default_layout ? m_secondary_layout : m_default_layout)->address;
builder->cmd(mousebtn::LEFT, build_cmd(EVENT_LAYOUT_LCLICK, to_string(addr))); builder->cmd(mousebtn::LEFT, build_cmd(CMD_LAYOUT_SET, to_string(addr)));
// Set previous layout // Set previous layout
builder->cmd(mousebtn::RIGHT, build_cmd(EVENT_LAYOUT_RCLICK, "0")); builder->cmd(mousebtn::RIGHT, build_cmd(CMD_LAYOUT_SET, "0"));
} }
if (m_layout_scroll) { if (m_layout_scroll) {
auto addr_next = next_layout(*m_current_layout, m_layout_wrap)->address; auto addr_next = next_layout(*m_current_layout, m_layout_wrap)->address;
auto addr_prev = prev_layout(*m_current_layout, m_layout_wrap)->address; auto addr_prev = prev_layout(*m_current_layout, m_layout_wrap)->address;
if (m_layout_reverse) { if (m_layout_reverse) {
builder->cmd(mousebtn::SCROLL_DOWN, build_cmd(EVENT_LAYOUT_SDOWN, to_string(addr_prev))); builder->cmd(mousebtn::SCROLL_DOWN, build_cmd(CMD_LAYOUT_SET, to_string(addr_prev)));
builder->cmd(mousebtn::SCROLL_UP, build_cmd(EVENT_LAYOUT_SUP, to_string(addr_next))); builder->cmd(mousebtn::SCROLL_UP, build_cmd(CMD_LAYOUT_SET, to_string(addr_next)));
} else { } else {
builder->cmd(mousebtn::SCROLL_DOWN, build_cmd(EVENT_LAYOUT_SDOWN, to_string(addr_next))); builder->cmd(mousebtn::SCROLL_DOWN, build_cmd(CMD_LAYOUT_SET, to_string(addr_next)));
builder->cmd(mousebtn::SCROLL_UP, build_cmd(EVENT_LAYOUT_SUP, to_string(addr_prev))); builder->cmd(mousebtn::SCROLL_UP, build_cmd(CMD_LAYOUT_SET, to_string(addr_prev)));
} }
} }
builder->node(m_layout_label); builder->node(m_layout_label);
@ -193,8 +193,8 @@ namespace modules {
} }
if (m_tags_click) { if (m_tags_click) {
builder->cmd(mousebtn::LEFT, build_cmd(EVENT_TAG_LCLICK, to_string(tag.bit_mask))); builder->cmd(mousebtn::LEFT, build_cmd(CMD_TAG_VIEW, to_string(tag.bit_mask)));
builder->cmd(mousebtn::RIGHT, build_cmd(EVENT_TAG_RCLICK, to_string(tag.bit_mask))); builder->cmd(mousebtn::RIGHT, build_cmd(CMD_TAG_TOGGLE_VIEW, to_string(tag.bit_mask)));
builder->node(tag.label); builder->node(tag.label);
builder->cmd_close(); builder->cmd_close();
builder->cmd_close(); builder->cmd_close();
@ -208,18 +208,18 @@ namespace modules {
return true; return true;
} }
auto dwm_module::check_send_cmd(string cmd, const string& ev_name) -> bool { auto dwm_module::check_send_cmd(string cmd, const string& ipc_cmd) -> bool {
// cmd = <EVENT_PREFIX><ev_name>-<arg> // cmd = <EVENT_PREFIX><ipc_cmd>-<arg>
cmd.erase(0, strlen(EVENT_PREFIX)); cmd.erase(0, strlen(EVENT_PREFIX));
// cmd = <ev_name>-<arg> // cmd = <ipc_cmd>-<arg>
if (cmd.compare(0, ev_name.size(), ev_name) == 0) { if (cmd.compare(0, ipc_cmd.size(), ipc_cmd) == 0) {
// Erase '<ev_name>-' // Erase '<ipc_cmd>-'
cmd.erase(0, ev_name.size() + 1); cmd.erase(0, ipc_cmd.size() + 1);
m_log.info("%s: Sending workspace %s command to ipc handler", name(), ev_name); m_log.info("%s: Sending workspace %s command to ipc handler", name(), ipc_cmd);
try { try {
m_ipc->run_command(ev_name, stoul(cmd)); m_ipc->run_command(ipc_cmd, stoul(cmd));
return true; return true;
} catch (const dwmipc::SocketClosedError& err) { } catch (const dwmipc::SocketClosedError& err) {
m_log.err("%s: Disconnected from socket: %s", name(), err.what()); m_log.err("%s: Disconnected from socket: %s", name(), err.what());
@ -236,9 +236,8 @@ namespace modules {
return false; return false;
} }
return check_send_cmd(cmd, EVENT_TAG_LCLICK) || check_send_cmd(cmd, EVENT_TAG_RCLICK) || return check_send_cmd(cmd, CMD_TAG_VIEW) || check_send_cmd(cmd, CMD_TAG_TOGGLE_VIEW) ||
check_send_cmd(cmd, EVENT_LAYOUT_LCLICK) || check_send_cmd(cmd, EVENT_LAYOUT_RCLICK) || check_send_cmd(cmd, CMD_LAYOUT_SET);
check_send_cmd(cmd, EVENT_LAYOUT_SDOWN) || check_send_cmd(cmd, EVENT_LAYOUT_SUP);
} }
auto dwm_module::get_state(tag_mask_t bit_mask) const -> state_t { auto dwm_module::get_state(tag_mask_t bit_mask) const -> state_t {