refactor: avoid unneeded json parsing
This commit is contained in:
parent
74137befba
commit
5a44c8c6de
|
@ -9,7 +9,6 @@
|
|||
#include <memory>
|
||||
#include <mutex>
|
||||
#include "ipc.hpp"
|
||||
#include "util/json.hpp"
|
||||
|
||||
namespace waybar::modules::sway {
|
||||
|
||||
|
@ -21,11 +20,11 @@ class Ipc {
|
|||
struct ipc_response {
|
||||
uint32_t size;
|
||||
uint32_t type;
|
||||
Json::Value payload;
|
||||
std::string payload;
|
||||
};
|
||||
|
||||
sigc::signal<void, const struct ipc_response&> signal_event;
|
||||
sigc::signal<void, const struct ipc_response&> signal_cmd;
|
||||
sigc::signal<void, const struct ipc_response &> signal_event;
|
||||
sigc::signal<void, const struct ipc_response &> signal_cmd;
|
||||
|
||||
void sendCmd(uint32_t type, const std::string &payload = "");
|
||||
void subscribe(const std::string &payload);
|
||||
|
@ -40,12 +39,10 @@ class Ipc {
|
|||
struct ipc_response send(int fd, uint32_t type, const std::string &payload = "");
|
||||
struct ipc_response recv(int fd);
|
||||
|
||||
int fd_;
|
||||
int fd_event_;
|
||||
std::mutex mutex_;
|
||||
std::mutex mutex_event_;
|
||||
std::mutex mutex_parser_;
|
||||
util::JsonParser parser_;
|
||||
int fd_;
|
||||
int fd_event_;
|
||||
std::mutex mutex_;
|
||||
std::mutex mutex_event_;
|
||||
};
|
||||
|
||||
} // namespace waybar::modules::sway
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "client.hpp"
|
||||
#include "modules/sway/ipc/client.hpp"
|
||||
#include "util/sleeper_thread.hpp"
|
||||
#include "util/json.hpp"
|
||||
|
||||
namespace waybar::modules::sway {
|
||||
|
||||
|
@ -22,6 +23,7 @@ class Mode : public ALabel {
|
|||
waybar::util::SleeperThread thread_;
|
||||
Ipc ipc_;
|
||||
std::string mode_;
|
||||
util::JsonParser parser_;
|
||||
};
|
||||
|
||||
} // namespace waybar::modules::sway
|
|
@ -7,6 +7,7 @@
|
|||
#include "client.hpp"
|
||||
#include "modules/sway/ipc/client.hpp"
|
||||
#include "util/sleeper_thread.hpp"
|
||||
#include "util/json.hpp"
|
||||
|
||||
namespace waybar::modules::sway {
|
||||
|
||||
|
@ -29,6 +30,7 @@ class Window : public ALabel {
|
|||
std::string window_;
|
||||
int windowId_;
|
||||
std::string app_id_;
|
||||
util::JsonParser parser_;
|
||||
};
|
||||
|
||||
} // namespace waybar::modules::sway
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include "client.hpp"
|
||||
#include "modules/sway/ipc/client.hpp"
|
||||
#include "util/sleeper_thread.hpp"
|
||||
#include "util/json.hpp"
|
||||
|
||||
namespace waybar::modules::sway {
|
||||
|
||||
|
@ -38,6 +39,7 @@ class Workspaces : public IModule {
|
|||
std::mutex mutex_;
|
||||
Gtk::Box box_;
|
||||
Ipc ipc_;
|
||||
util::JsonParser parser_;
|
||||
bool scrolling_;
|
||||
std::unordered_map<std::string, Gtk::Button> buttons_;
|
||||
};
|
||||
|
|
|
@ -104,9 +104,7 @@ struct Ipc::ipc_response Ipc::recv(int fd) {
|
|||
}
|
||||
total += res;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(mutex_parser_);
|
||||
auto parsed = parser_.parse(&payload.front());
|
||||
return {data32[0], data32[1], parsed};
|
||||
return {data32[0], data32[1], &payload.front()};
|
||||
}
|
||||
|
||||
struct Ipc::ipc_response Ipc::send(int fd, uint32_t type, const std::string& payload) {
|
||||
|
@ -135,7 +133,7 @@ void Ipc::sendCmd(uint32_t type, const std::string& payload) {
|
|||
void Ipc::subscribe(const std::string& payload) {
|
||||
std::lock_guard<std::mutex> lock(mutex_event_);
|
||||
auto res = Ipc::send(fd_event_, IPC_SUBSCRIBE, payload);
|
||||
if (!res.payload["success"].asBool()) {
|
||||
if (res.payload != "{\"success\": true}") {
|
||||
throw std::runtime_error("Unable to subscribe ipc event");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,8 +2,7 @@
|
|||
|
||||
namespace waybar::modules::sway {
|
||||
|
||||
Mode::Mode(const std::string& id, const Json::Value& config)
|
||||
: ALabel(config, "{}") {
|
||||
Mode::Mode(const std::string& id, const Json::Value& config) : ALabel(config, "{}") {
|
||||
label_.set_name("mode");
|
||||
if (!id.empty()) {
|
||||
label_.get_style_context()->add_class(id);
|
||||
|
@ -15,9 +14,10 @@ Mode::Mode(const std::string& id, const Json::Value& config)
|
|||
dp.emit();
|
||||
}
|
||||
|
||||
void Mode::onEvent(const struct Ipc::ipc_response &res) {
|
||||
if (res.payload["change"] != "default") {
|
||||
mode_ = res.payload["change"].asString();
|
||||
void Mode::onEvent(const struct Ipc::ipc_response& res) {
|
||||
auto payload = parser_.parse(res.payload);
|
||||
if (payload["change"] != "default") {
|
||||
mode_ = payload["change"].asString();
|
||||
} else {
|
||||
mode_.clear();
|
||||
}
|
||||
|
|
|
@ -21,18 +21,11 @@ Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
|
|||
worker();
|
||||
}
|
||||
|
||||
void Window::onEvent(const struct Ipc::ipc_response& res) {
|
||||
auto data = res.payload;
|
||||
// Check for waybar prevents flicker when hovering window module
|
||||
if (((data["change"] == "focus" || data["change"] == "title") &&
|
||||
data["container"]["focused"].asBool() && data["container"]["name"].asString() != "waybar") ||
|
||||
data["change"] == "close") {
|
||||
getTree();
|
||||
}
|
||||
}
|
||||
void Window::onEvent(const struct Ipc::ipc_response& res) { getTree(); }
|
||||
|
||||
void Window::onCmd(const struct Ipc::ipc_response& res) {
|
||||
auto [nb, id, name, app_id] = getFocusedNode(res.payload);
|
||||
auto payload = parser_.parse(res.payload);
|
||||
auto [nb, id, name, app_id] = getFocusedNode(payload);
|
||||
if (!app_id_.empty()) {
|
||||
bar_.window.get_style_context()->remove_class(app_id_);
|
||||
}
|
||||
|
|
|
@ -23,11 +23,12 @@ void Workspaces::onEvent(const struct Ipc::ipc_response &res) { ipc_.sendCmd(IPC
|
|||
|
||||
void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
|
||||
if (res.type == IPC_GET_WORKSPACES) {
|
||||
if (res.payload.isArray()) {
|
||||
auto payload = parser_.parse(res.payload);
|
||||
if (payload.isArray()) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
workspaces_.clear();
|
||||
std::copy_if(res.payload.begin(),
|
||||
res.payload.end(),
|
||||
std::copy_if(payload.begin(),
|
||||
payload.end(),
|
||||
std::back_inserter(workspaces_),
|
||||
[&](const auto &workspace) {
|
||||
return !config_["all-outputs"].asBool()
|
||||
|
|
Loading…
Reference in New Issue