From e3342467fcdb7fc6c38610dacbd2e272d565900b Mon Sep 17 00:00:00 2001 From: asas1asas200 Date: Mon, 29 Aug 2022 04:55:48 +0800 Subject: [PATCH] feat(sway/scratchpad): add basic counter --- include/factory.hpp | 1 + include/modules/sway/scratchpad.hpp | 34 +++++++++++++++++++ meson.build | 3 +- src/factory.cpp | 3 ++ src/modules/sway/scratchpad.cpp | 52 +++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 include/modules/sway/scratchpad.hpp create mode 100644 src/modules/sway/scratchpad.cpp diff --git a/include/factory.hpp b/include/factory.hpp index 47ef530..0bd4a90 100644 --- a/include/factory.hpp +++ b/include/factory.hpp @@ -9,6 +9,7 @@ #ifdef HAVE_SWAY #include "modules/sway/language.hpp" #include "modules/sway/mode.hpp" +#include "modules/sway/scratchpad.hpp" #include "modules/sway/window.hpp" #include "modules/sway/workspaces.hpp" #endif diff --git a/include/modules/sway/scratchpad.hpp b/include/modules/sway/scratchpad.hpp new file mode 100644 index 0000000..9464a6e --- /dev/null +++ b/include/modules/sway/scratchpad.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include + +#include +#include + +#include "ALabel.hpp" +#include "bar.hpp" +#include "client.hpp" +#include "modules/sway/ipc/client.hpp" +#include "util/json.hpp" + +namespace waybar::modules::sway { +// class Scratchpad : public AModule, public sigc::trackable { +class Scratchpad : public ALabel { + public: + Scratchpad(const std::string&, const waybar::Bar&, const Json::Value&); + ~Scratchpad() = default; + auto update() -> void; + + private: + auto getTree() -> void; + auto onCmd(const struct Ipc::ipc_response&) -> void; + auto onEvent(const struct Ipc::ipc_response&) -> void; + // bool handleScroll(GdkEventScroll*); + Gtk::Box box_; + const Bar& bar_; + std::mutex mutex_; + int count_; + Ipc ipc_; + util::JsonParser parser_; +}; +} // namespace waybar::modules::sway \ No newline at end of file diff --git a/meson.build b/meson.build index 3c32006..d1442ec 100644 --- a/meson.build +++ b/meson.build @@ -184,7 +184,8 @@ src_files += [ 'src/modules/sway/mode.cpp', 'src/modules/sway/language.cpp', 'src/modules/sway/window.cpp', - 'src/modules/sway/workspaces.cpp' + 'src/modules/sway/workspaces.cpp', + 'src/modules/sway/scratchpad.cpp' ] if true diff --git a/src/factory.cpp b/src/factory.cpp index 6df69d5..4a72d40 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -35,6 +35,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const { if (ref == "sway/language") { return new waybar::modules::sway::Language(id, config_[name]); } + if (ref == "sway/scratchpad") { + return new waybar::modules::sway::Scratchpad(id, bar_, config_[name]); + } #endif #ifdef HAVE_WLR if (ref == "wlr/taskbar") { diff --git a/src/modules/sway/scratchpad.cpp b/src/modules/sway/scratchpad.cpp new file mode 100644 index 0000000..a7fd7ba --- /dev/null +++ b/src/modules/sway/scratchpad.cpp @@ -0,0 +1,52 @@ +#include "modules/sway/scratchpad.hpp" + +#include + +#include + +namespace waybar::modules::sway { +Scratchpad::Scratchpad(const std::string& id, const waybar::Bar& bar, const Json::Value& config) + : ALabel(config, "scratchpad", id, "{count}"), + box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0), + bar_(bar), + count_(0) { + ipc_.subscribe(R"(["window"])"); + ipc_.signal_event.connect(sigc::mem_fun(*this, &Scratchpad::onEvent)); + ipc_.signal_cmd.connect(sigc::mem_fun(*this, &Scratchpad::onCmd)); + + getTree(); + + ipc_.setWorker([this] { + try { + ipc_.handleEvent(); + } catch (const std::exception& e) { + spdlog::error("Scratchpad: {}", e.what()); + } + }); +} +auto Scratchpad::update() -> void { + label_.set_markup(fmt::format(format_, fmt::arg("count", count_))); + AModule::update(); +} + +auto Scratchpad::getTree() -> void { + try { + ipc_.sendCmd(IPC_GET_TREE); + } catch (const std::exception& e) { + spdlog::error("Scratchpad: {}", e.what()); + } +} + +auto Scratchpad::onCmd(const struct Ipc::ipc_response& res) -> void { + try { + std::lock_guard lock(mutex_); + auto tree = parser_.parse(res.payload); + count_ = tree["nodes"][0]["nodes"][0]["floating_nodes"].size(); + dp.emit(); + } catch (const std::exception& e) { + spdlog::error("Scratchpad: {}", e.what()); + } +} + +auto Scratchpad::onEvent(const struct Ipc::ipc_response& res) -> void { getTree(); } +} // namespace waybar::modules::sway \ No newline at end of file