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