fix(Workspaces): fix concurrence and move json parser to ipc client

This commit is contained in:
Alex
2019-04-23 11:41:49 +02:00
parent 07dba791cf
commit cccf60c30e
10 changed files with 82 additions and 77 deletions

View File

@ -6,8 +6,10 @@
#include <unistd.h>
#include <cstring>
#include <iostream>
#include <memory>
#include <mutex>
#include "ipc.hpp"
#include "util/json.hpp"
namespace waybar::modules::sway {
@ -19,7 +21,7 @@ class Ipc {
struct ipc_response {
uint32_t size;
uint32_t type;
std::string payload;
Json::Value payload;
};
sigc::signal<void, const struct ipc_response> signal_event;
@ -38,10 +40,11 @@ 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_;
int fd_;
int fd_event_;
std::mutex mutex_;
std::mutex mutex_event_;
util::JsonParser parser_;
};
} // namespace waybar::modules::sway

View File

@ -5,7 +5,6 @@
#include "bar.hpp"
#include "client.hpp"
#include "modules/sway/ipc/client.hpp"
#include "util/json.hpp"
#include "util/sleeper_thread.hpp"
namespace waybar::modules::sway {
@ -22,7 +21,6 @@ class Mode : public ALabel {
const Bar& bar_;
waybar::util::SleeperThread thread_;
util::JsonParser parser_;
Ipc ipc_;
std::string mode_;
};

View File

@ -6,7 +6,6 @@
#include "bar.hpp"
#include "client.hpp"
#include "modules/sway/ipc/client.hpp"
#include "util/json.hpp"
#include "util/sleeper_thread.hpp"
namespace waybar::modules::sway {
@ -26,7 +25,6 @@ class Window : public ALabel {
const Bar& bar_;
waybar::util::SleeperThread thread_;
util::JsonParser parser_;
Ipc ipc_;
std::string window_;
int windowId_;

View File

@ -7,7 +7,6 @@
#include "bar.hpp"
#include "client.hpp"
#include "modules/sway/ipc/client.hpp"
#include "util/json.hpp"
#include "util/sleeper_thread.hpp"
namespace waybar::modules::sway {
@ -36,8 +35,8 @@ class Workspaces : public IModule {
const Json::Value& config_;
std::vector<Json::Value> workspaces_;
waybar::util::SleeperThread thread_;
std::mutex mutex_;
Gtk::Box box_;
util::JsonParser parser_;
Ipc ipc_;
bool scrolling_;
std::unordered_map<std::string, Gtk::Button> buttons_;

View File

@ -7,13 +7,14 @@ namespace waybar::util {
struct JsonParser {
JsonParser() : reader_(builder_.newCharReader()) {}
const Json::Value parse(const std::string& data) const {
Json::Value root;
std::string err;
const Json::Value parse(const std::string& data, std::size_t size = 0) const {
Json::Value root(Json::objectValue);
if (data.empty()) {
return root;
}
bool res = reader_->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
std::string err;
auto data_size = size > 0 ? size : data.size();
bool res = reader_->parse(data.c_str(), data.c_str() + data_size, &root, &err);
if (!res) throw std::runtime_error(err);
return root;
}