From ecc881b9b314cfd1ef31e473cd56457df2f5946a Mon Sep 17 00:00:00 2001 From: Mihir Lad Date: Mon, 20 Jul 2020 23:43:12 -0400 Subject: [PATCH] dwm: Add build_cmd helper and rename events build_cmd simplifies building a command string and helps avoid typo errors when adding new commands since most commands follow the same format. Rename EVENT_LCLICK and EVENT_RCLICK and to EVENT_TAG_LCLICK and EVENT_TAG_RCLICK for clarity and to allow specifying multiple click commands in the future without confusion. Also remove std:: from vector since it is not needed. --- include/modules/dwm.hpp | 14 +++++++++++--- src/modules/dwm.cpp | 10 +++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/modules/dwm.hpp b/include/modules/dwm.hpp index 9d79e0c5..1e64bb2f 100644 --- a/include/modules/dwm.hpp +++ b/include/modules/dwm.hpp @@ -85,13 +85,13 @@ namespace modules { /** * Event name is same as the IPC command name to view the tag clicked on */ - static constexpr const char* EVENT_LCLICK{"view"}; + static constexpr const char* EVENT_TAG_LCLICK{"view"}; /** * Event name is same as IPC command name to toggle the view of the tag * clicked on */ - static constexpr const char* EVENT_RCLICK{"toggleview"}; + static constexpr const char* EVENT_TAG_RCLICK{"toggleview"}; /** * Called by has_event on layout changes. This updates the layout label @@ -175,6 +175,14 @@ namespace modules { */ auto check_send_cmd(string cmd, const string& ev_name) -> bool; + /** + * 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; + /** * Attempt to connect to any disconnected dwm sockets. Catch errors. */ @@ -223,7 +231,7 @@ namespace modules { /** * Vector of monitors returned by m_ipc->get_monitors */ - shared_ptr> m_monitors; + shared_ptr> m_monitors; /** * Maps state_t enum values to their corresponding labels diff --git a/src/modules/dwm.cpp b/src/modules/dwm.cpp index 9a9a43cd..21bbf461 100644 --- a/src/modules/dwm.cpp +++ b/src/modules/dwm.cpp @@ -132,6 +132,10 @@ 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(builder* builder, const string& tag) const -> bool { if (tag == TAG_LABEL_LAYOUT) { builder->node(m_layout_label); @@ -148,8 +152,8 @@ namespace modules { } if (m_click) { - builder->cmd(mousebtn::LEFT, EVENT_PREFIX + string{EVENT_LCLICK} + "-" + to_string(tag.bit_mask)); - builder->cmd(mousebtn::RIGHT, EVENT_PREFIX + string{EVENT_RCLICK} + "-" + to_string(tag.bit_mask)); + 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->node(tag.label); builder->cmd_close(); builder->cmd_close(); @@ -192,7 +196,7 @@ namespace modules { return false; } - return check_send_cmd(cmd, EVENT_LCLICK) || check_send_cmd(cmd, EVENT_RCLICK); + return check_send_cmd(cmd, EVENT_TAG_LCLICK) || check_send_cmd(cmd, EVENT_TAG_RCLICK); } auto dwm_module::get_state(tag_mask_t bit_mask) const -> state_t {