From 5f3462240cddfca15a52092633f77d2d4fa55278 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sun, 11 Jul 2021 20:42:28 +0200 Subject: [PATCH] feat(ipc): allow receiving arbitrary text on IPC socket (#2463) This adds the 'send' action to the ipc module that can be used to send arbitrary text to the module: polybar-msg action "#ipc.send.%{F#4444ff}hello%{F-}" * feat(ipc): allow receiving arbitrary text on IPC socket Instead of just allowing hook numbers to be executed, the user can send arbitrary text and the IPC module will put it in the bar. The IPC payload format is extended to accept an arbitrary string if the first character after the module name is ':'. polybar-msg hook test :'%{F#4444ff}hello%{F-}' Fix #2455 * Use actions for sending data to ipc module * ipc: Don't use exceptions when no hooks are defined * Update src/modules/ipc.cpp Co-authored-by: patrick96 --- CHANGELOG.md | 2 ++ doc/user/actions.rst | 7 +++++++ include/modules/ipc.hpp | 5 +++++ src/modules/ipc.cpp | 13 +++++++++---- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2c6eb74..6ec25b8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -98,6 +98,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added `occupied-scroll = true` option to bspwm module. Allows scrolling only through occupied desktops only. ([`#2427`](https://github.com/polybar/polybar/issues/2427)) +- `custom/ipc`: `send` action to send arbitrary strings to be displayed in the module. + ([`#2455`](https://github.com/polybar/polybar/issues/2455)) ### Changed - Slight changes to the value ranges the different ramp levels are responsible diff --git a/doc/user/actions.rst b/doc/user/actions.rst index 95eb5e05..cc6145d9 100644 --- a/doc/user/actions.rst +++ b/doc/user/actions.rst @@ -258,6 +258,13 @@ custom/menu The data has the form ``N-M`` and the action will execute the command in ``menu-N-M-exec``. +custom/ipc +^^^^^^^^^^ + +:``send``: *(Has Data)* Replace the contents of the module with the data passed in this action. + + .. versionadded:: 3.6.0 + Deprecated Action Names ----------------------- diff --git a/include/modules/ipc.hpp b/include/modules/ipc.hpp index d0097cc1..5a5a1100 100644 --- a/include/modules/ipc.hpp +++ b/include/modules/ipc.hpp @@ -34,6 +34,11 @@ namespace modules { static constexpr auto TYPE = "custom/ipc"; + static constexpr auto EVENT_SEND = "send"; + + protected: + void action_send(const string& data); + private: static constexpr const char* TAG_OUTPUT{""}; vector> m_hooks; diff --git a/src/modules/ipc.cpp b/src/modules/ipc.cpp index 2e6b9948..9aea8ca5 100644 --- a/src/modules/ipc.cpp +++ b/src/modules/ipc.cpp @@ -13,15 +13,15 @@ namespace modules { * create formatting tags */ ipc_module::ipc_module(const bar_settings& bar, string name_) : static_module(bar, move(name_)) { + m_router->register_action_with_data(EVENT_SEND, &ipc_module::action_send); + size_t index = 0; - for (auto&& command : m_conf.get_list(name(), "hook")) { + for (auto&& command : m_conf.get_list(name(), "hook", {})) { m_hooks.emplace_back(std::make_unique(hook{name() + to_string(++index), command})); } - if (m_hooks.empty()) { - throw module_error("No hooks defined"); - } + m_log.info("%s: Loaded %d hooks", name(), m_hooks.size()); if ((m_initial = m_conf.get(name(), "initial", 0_z)) && m_initial > m_hooks.size()) { throw module_error("Initial hook out of bounds (defined: " + to_string(m_hooks.size()) + ")"); @@ -125,6 +125,11 @@ namespace modules { broadcast(); } } + + void ipc_module::action_send(const string& data) { + m_output = data; + broadcast(); + } } // namespace modules POLYBAR_NS_END