mirror of
				https://github.com/rad4day/Waybar.git
				synced 2025-11-04 09:42:42 +01:00 
			
		
		
		
	refactor: try/catch around json parse
This commit is contained in:
		@@ -131,7 +131,6 @@ 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\": true}") {
 | 
			
		||||
    throw std::runtime_error("Unable to subscribe ipc event");
 | 
			
		||||
@@ -139,7 +138,6 @@ void Ipc::subscribe(const std::string& payload) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Ipc::handleEvent() {
 | 
			
		||||
  std::lock_guard<std::mutex> lock(mutex_event_);
 | 
			
		||||
  const auto res = Ipc::recv(fd_event_);
 | 
			
		||||
  signal_event.emit(res);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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) {
 | 
			
		||||
  auto payload = parser_.parse(res.payload);
 | 
			
		||||
  if (payload["change"] != "default") {
 | 
			
		||||
    mode_ = payload["change"].asString();
 | 
			
		||||
  } else {
 | 
			
		||||
    mode_.clear();
 | 
			
		||||
  try {
 | 
			
		||||
    auto payload = parser_.parse(res.payload);
 | 
			
		||||
    if (payload["change"] != "default") {
 | 
			
		||||
      mode_ = payload["change"].asString();
 | 
			
		||||
    } else {
 | 
			
		||||
      mode_.clear();
 | 
			
		||||
    }
 | 
			
		||||
    dp.emit();
 | 
			
		||||
  } catch (const std::exception& e) {
 | 
			
		||||
    std::cerr << "Mode: " << e.what() << std::endl;
 | 
			
		||||
  }
 | 
			
		||||
  dp.emit();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Mode::worker() {
 | 
			
		||||
 
 | 
			
		||||
@@ -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::onCmd(const struct Ipc::ipc_response& res) {
 | 
			
		||||
  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_);
 | 
			
		||||
  }
 | 
			
		||||
  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);
 | 
			
		||||
  try {
 | 
			
		||||
    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_);
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    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();
 | 
			
		||||
    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 {
 | 
			
		||||
      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;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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) {
 | 
			
		||||
  if (res.type == IPC_GET_WORKSPACES) {
 | 
			
		||||
    auto payload = parser_.parse(res.payload);
 | 
			
		||||
    if (payload.isArray()) {
 | 
			
		||||
      std::lock_guard<std::mutex> lock(mutex_);
 | 
			
		||||
      workspaces_.clear();
 | 
			
		||||
      std::copy_if(payload.begin(),
 | 
			
		||||
                   payload.end(),
 | 
			
		||||
                   std::back_inserter(workspaces_),
 | 
			
		||||
                   [&](const auto &workspace) {
 | 
			
		||||
                     return !config_["all-outputs"].asBool()
 | 
			
		||||
                                ? workspace["output"].asString() == bar_.output->name
 | 
			
		||||
                                : true;
 | 
			
		||||
                   });
 | 
			
		||||
      dp.emit();
 | 
			
		||||
    try {
 | 
			
		||||
      auto payload = parser_.parse(res.payload);
 | 
			
		||||
      if (payload.isArray()) {
 | 
			
		||||
        std::lock_guard<std::mutex> lock(mutex_);
 | 
			
		||||
        workspaces_.clear();
 | 
			
		||||
        std::copy_if(payload.begin(),
 | 
			
		||||
                     payload.end(),
 | 
			
		||||
                     std::back_inserter(workspaces_),
 | 
			
		||||
                     [&](const auto &workspace) {
 | 
			
		||||
                       return !config_["all-outputs"].asBool()
 | 
			
		||||
                                  ? workspace["output"].asString() == bar_.output->name
 | 
			
		||||
                                  : true;
 | 
			
		||||
                     });
 | 
			
		||||
        dp.emit();
 | 
			
		||||
      }
 | 
			
		||||
    } catch (const std::exception &e) {
 | 
			
		||||
      std::cerr << "Workspaces: " << e.what() << std::endl;
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    if (scrolling_) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user