refactor: try/catch around json parse

This commit is contained in:
Alex 2019-05-09 10:30:54 +02:00
parent fd9b34adf8
commit 5bf0ca85ac
6 changed files with 55 additions and 48 deletions

View File

@ -42,7 +42,6 @@ class Ipc {
int fd_; int fd_;
int fd_event_; int fd_event_;
std::mutex mutex_; std::mutex mutex_;
std::mutex mutex_event_;
}; };
} // namespace waybar::modules::sway } // namespace waybar::modules::sway

View File

@ -111,15 +111,13 @@ void waybar::Bar::setupAltFormatKeyForModule(const std::string& module_name) {
if (module.isMember("format-alt-click")) { if (module.isMember("format-alt-click")) {
Json::Value& click = module["format-alt-click"]; Json::Value& click = module["format-alt-click"];
if (click.isString()) { if (click.isString()) {
std::string str_click = click.asString(); if (click == "click-right") {
if (str_click == "click-right") {
module["format-alt-click"] = 3U; module["format-alt-click"] = 3U;
} else if (str_click == "click-middle") { } else if (click == "click-middle") {
module["format-alt-click"] = 2U; module["format-alt-click"] = 2U;
} else if (str_click == "click-backward") { } else if (click == "click-backward") {
module["format-alt-click"] = 8U; module["format-alt-click"] = 8U;
} else if (str_click == "click-forward") { } else if (click == "click-forward") {
module["format-alt-click"] = 9U; module["format-alt-click"] = 9U;
} else { } else {
module["format-alt-click"] = 1U; // default click-left module["format-alt-click"] = 1U; // default click-left

View File

@ -131,7 +131,6 @@ 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_);
auto res = Ipc::send(fd_event_, IPC_SUBSCRIBE, payload); auto res = Ipc::send(fd_event_, IPC_SUBSCRIBE, payload);
if (res.payload != "{\"success\": true}") { if (res.payload != "{\"success\": true}") {
throw std::runtime_error("Unable to subscribe ipc event"); throw std::runtime_error("Unable to subscribe ipc event");
@ -139,7 +138,6 @@ void Ipc::subscribe(const std::string& payload) {
} }
void Ipc::handleEvent() { void Ipc::handleEvent() {
std::lock_guard<std::mutex> lock(mutex_event_);
const auto res = Ipc::recv(fd_event_); const auto res = Ipc::recv(fd_event_);
signal_event.emit(res); signal_event.emit(res);
} }

View File

@ -15,13 +15,17 @@ Mode::Mode(const std::string& id, const Json::Value& config) : ALabel(config, "{
} }
void Mode::onEvent(const struct Ipc::ipc_response& res) { void Mode::onEvent(const struct Ipc::ipc_response& res) {
auto payload = parser_.parse(res.payload); try {
if (payload["change"] != "default") { auto payload = parser_.parse(res.payload);
mode_ = payload["change"].asString(); if (payload["change"] != "default") {
} else { mode_ = payload["change"].asString();
mode_.clear(); } else {
mode_.clear();
}
dp.emit();
} catch (const std::exception& e) {
std::cerr << "Mode: " << e.what() << std::endl;
} }
dp.emit();
} }
void Mode::worker() { void Mode::worker() {

View File

@ -24,27 +24,31 @@ Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
void Window::onEvent(const struct Ipc::ipc_response& res) { getTree(); } void Window::onEvent(const struct Ipc::ipc_response& res) { getTree(); }
void Window::onCmd(const struct Ipc::ipc_response& res) { void Window::onCmd(const struct Ipc::ipc_response& res) {
auto payload = parser_.parse(res.payload); try {
auto [nb, id, name, app_id] = getFocusedNode(payload); auto payload = parser_.parse(res.payload);
if (!app_id_.empty()) { auto [nb, id, name, app_id] = getFocusedNode(payload);
bar_.window.get_style_context()->remove_class(app_id_); if (!app_id_.empty()) {
} bar_.window.get_style_context()->remove_class(app_id_);
if (nb == 0) {
bar_.window.get_style_context()->add_class("empty");
} else if (nb == 1) {
bar_.window.get_style_context()->add_class("solo");
if (!app_id.empty()) {
bar_.window.get_style_context()->add_class(app_id);
} }
} else { if (nb == 0) {
bar_.window.get_style_context()->remove_class("solo"); bar_.window.get_style_context()->add_class("empty");
bar_.window.get_style_context()->remove_class("empty"); } else if (nb == 1) {
} bar_.window.get_style_context()->add_class("solo");
app_id_ = app_id; if (!app_id.empty()) {
if (windowId_ != id || window_ != name) { bar_.window.get_style_context()->add_class(app_id);
windowId_ = id; }
window_ = name; } else {
dp.emit(); bar_.window.get_style_context()->remove_class("solo");
bar_.window.get_style_context()->remove_class("empty");
}
app_id_ = app_id;
if (windowId_ != id || window_ != name) {
windowId_ = id;
window_ = name;
dp.emit();
}
} catch (const std::exception& e) {
std::cerr << "Window: " << e.what() << std::endl;
} }
} }

View File

@ -23,19 +23,23 @@ 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) {
auto payload = parser_.parse(res.payload); try {
if (payload.isArray()) { auto payload = parser_.parse(res.payload);
std::lock_guard<std::mutex> lock(mutex_); if (payload.isArray()) {
workspaces_.clear(); std::lock_guard<std::mutex> lock(mutex_);
std::copy_if(payload.begin(), workspaces_.clear();
payload.end(), std::copy_if(payload.begin(),
std::back_inserter(workspaces_), payload.end(),
[&](const auto &workspace) { std::back_inserter(workspaces_),
return !config_["all-outputs"].asBool() [&](const auto &workspace) {
? workspace["output"].asString() == bar_.output->name return !config_["all-outputs"].asBool()
: true; ? workspace["output"].asString() == bar_.output->name
}); : true;
dp.emit(); });
dp.emit();
}
} catch (const std::exception &e) {
std::cerr << "Workspaces: " << e.what() << std::endl;
} }
} else { } else {
if (scrolling_) { if (scrolling_) {