From 33c8fb2ac7c008bcf0e365da9dbb02c268824c20 Mon Sep 17 00:00:00 2001 From: Mihir Lad Date: Tue, 21 Jul 2020 18:57:58 -0400 Subject: [PATCH] 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. --- include/modules/dwm.hpp | 49 ++++++++++++++--------------------------- src/modules/dwm.cpp | 41 +++++++++++++++++----------------- 2 files changed, 36 insertions(+), 54 deletions(-) diff --git a/include/modules/dwm.hpp b/include/modules/dwm.hpp index e0533655..ad9f0622 100644 --- a/include/modules/dwm.hpp +++ b/include/modules/dwm.hpp @@ -80,42 +80,26 @@ namespace modules { */ static constexpr const char* TAG_LABEL_TITLE{""}; + /** + * All input handler commands start with this + */ 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 - * clicked on + * DWM command for toggling the selected state of a tag with the specified + * 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 - * secondary layout when the layout symbol is clicked + * DWM command for setting the layout to a layout specified by the address */ - static constexpr const char* EVENT_LAYOUT_LCLICK{"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"}; + static constexpr const char* CMD_LAYOUT_SET{"setlayoutsafe"}; /** * 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*; /** - * Check if the command matches the specified event name and if so, send a - * command to dwm after parsing the command. + * 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 ev_name The name of the event to check for (should be the same as - * the dwm command name) + * @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 */ - 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 * - * @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 */ - 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. diff --git a/src/modules/dwm.cpp b/src/modules/dwm.cpp index 02639705..94b8988d 100644 --- a/src/modules/dwm.cpp +++ b/src/modules/dwm.cpp @@ -147,8 +147,8 @@ namespace modules { return true; } - auto dwm_module::build_cmd(const char* ev, const string& arg) -> string { - return EVENT_PREFIX + string(ev) + "-" + arg; + auto dwm_module::build_cmd(const char* ipc_cmd, const string& arg) -> string { + return EVENT_PREFIX + string(ipc_cmd) + "-" + arg; } auto dwm_module::build(builder* builder, const string& tag) const -> bool { @@ -158,19 +158,19 @@ 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(EVENT_LAYOUT_LCLICK, to_string(addr))); + builder->cmd(mousebtn::LEFT, build_cmd(CMD_LAYOUT_SET, to_string(addr))); // 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) { auto addr_next = next_layout(*m_current_layout, m_layout_wrap)->address; auto addr_prev = prev_layout(*m_current_layout, m_layout_wrap)->address; if (m_layout_reverse) { - builder->cmd(mousebtn::SCROLL_DOWN, build_cmd(EVENT_LAYOUT_SDOWN, to_string(addr_prev))); - builder->cmd(mousebtn::SCROLL_UP, build_cmd(EVENT_LAYOUT_SUP, to_string(addr_next))); + builder->cmd(mousebtn::SCROLL_DOWN, build_cmd(CMD_LAYOUT_SET, to_string(addr_prev))); + builder->cmd(mousebtn::SCROLL_UP, build_cmd(CMD_LAYOUT_SET, to_string(addr_next))); } else { - builder->cmd(mousebtn::SCROLL_DOWN, build_cmd(EVENT_LAYOUT_SDOWN, to_string(addr_next))); - builder->cmd(mousebtn::SCROLL_UP, build_cmd(EVENT_LAYOUT_SUP, to_string(addr_prev))); + builder->cmd(mousebtn::SCROLL_DOWN, build_cmd(CMD_LAYOUT_SET, to_string(addr_next))); + builder->cmd(mousebtn::SCROLL_UP, build_cmd(CMD_LAYOUT_SET, to_string(addr_prev))); } } builder->node(m_layout_label); @@ -193,8 +193,8 @@ namespace modules { } if (m_tags_click) { - builder->cmd(mousebtn::LEFT, build_cmd(EVENT_TAG_LCLICK, to_string(tag.bit_mask))); - builder->cmd(mousebtn::RIGHT, build_cmd(EVENT_TAG_RCLICK, 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(CMD_TAG_TOGGLE_VIEW, to_string(tag.bit_mask))); builder->node(tag.label); builder->cmd_close(); builder->cmd_close(); @@ -208,18 +208,18 @@ namespace modules { return true; } - auto dwm_module::check_send_cmd(string cmd, const string& ev_name) -> bool { - // cmd = - + auto dwm_module::check_send_cmd(string cmd, const string& ipc_cmd) -> bool { + // cmd = - cmd.erase(0, strlen(EVENT_PREFIX)); - // cmd = - - if (cmd.compare(0, ev_name.size(), ev_name) == 0) { - // Erase '-' - cmd.erase(0, ev_name.size() + 1); - m_log.info("%s: Sending workspace %s command to ipc handler", name(), ev_name); + // cmd = - + if (cmd.compare(0, ipc_cmd.size(), ipc_cmd) == 0) { + // Erase '-' + 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(ev_name, stoul(cmd)); + m_ipc->run_command(ipc_cmd, stoul(cmd)); return true; } catch (const dwmipc::SocketClosedError& err) { m_log.err("%s: Disconnected from socket: %s", name(), err.what()); @@ -236,9 +236,8 @@ namespace modules { return false; } - return check_send_cmd(cmd, EVENT_TAG_LCLICK) || check_send_cmd(cmd, EVENT_TAG_RCLICK) || - check_send_cmd(cmd, EVENT_LAYOUT_LCLICK) || check_send_cmd(cmd, EVENT_LAYOUT_RCLICK) || - check_send_cmd(cmd, EVENT_LAYOUT_SDOWN) || check_send_cmd(cmd, EVENT_LAYOUT_SUP); + return check_send_cmd(cmd, CMD_TAG_VIEW) || check_send_cmd(cmd, CMD_TAG_TOGGLE_VIEW) || + check_send_cmd(cmd, CMD_LAYOUT_SET); } auto dwm_module::get_state(tag_mask_t bit_mask) const -> state_t {