Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules/hyprland/backend: handle empty socket response #3389

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions include/modules/hyprland/workspaces.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
#include <gtkmm/label.h>
#include <json/value.h>

#include <cstddef>
#include <cstdint>
#include <map>
#include <memory>
#include <optional>
#include <regex>
#include <string>
#include <variant>
#include <vector>

#include "AModule.hpp"
Expand Down Expand Up @@ -53,17 +50,19 @@ class Workspaces : public AModule, public EventHandler {
void onEvent(const std::string& e) override;
void updateWindowCount();
void sortWorkspaces();
void createWorkspace(Json::Value const& workspaceData,
Json::Value const& clientsData = Json::Value::nullRef);
void createWorkspace(Json::Value const& workspace_data,
Json::Value const& clients_data = Json::Value::nullRef);

Json::Value createMonitorWorkspaceData(std::string const& name, std::string const& monitor);
static Json::Value createMonitorWorkspaceData(std::string const& name,
std::string const& monitor);
void removeWorkspace(std::string const& name);
void setUrgentWorkspace(std::string const& windowaddress);

// Config
void parseConfig(const Json::Value& config);
auto populateIconsMap(const Json::Value& formatIcons) -> void;
auto populateBoolConfig(const Json::Value& config, const std::string& key, bool& member) -> void;
static auto populateBoolConfig(const Json::Value& config, const std::string& key, bool& member)
-> void;
auto populateSortByConfig(const Json::Value& config) -> void;
auto populateIgnoreWorkspacesConfig(const Json::Value& config) -> void;
auto populateFormatWindowSeparatorConfig(const Json::Value& config) -> void;
Expand Down Expand Up @@ -98,7 +97,7 @@ class Workspaces : public AModule, public EventHandler {
void doUpdate();
void removeWorkspacesToRemove();
void createWorkspacesToCreate();
std::vector<std::string> getVisibleWorkspaces();
static std::vector<std::string> getVisibleWorkspaces();
void updateWorkspaceStates();
bool updateWindowsToCreate();

Expand Down
10 changes: 8 additions & 2 deletions src/modules/hyprland/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ std::filesystem::path IPC::getSocketFolder(const char* instanceSig) {
const char* xdgRuntimeDirEnv = std::getenv("XDG_RUNTIME_DIR");
std::filesystem::path xdgRuntimeDir;
// Only set path if env variable is set
if (xdgRuntimeDirEnv) {
if (xdgRuntimeDirEnv != nullptr) {
xdgRuntimeDir = std::filesystem::path(xdgRuntimeDirEnv);
}

Expand Down Expand Up @@ -218,7 +218,13 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
}

Json::Value IPC::getSocket1JsonReply(const std::string& rq) {
return parser_.parse(getSocket1Reply("j/" + rq));
std::string reply = getSocket1Reply("j/" + rq);

if (reply.empty()) {
return {};
}

return parser_.parse(reply);
}

} // namespace waybar::modules::hyprland
4 changes: 2 additions & 2 deletions src/modules/hyprland/submap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ Submap::~Submap() {
}

auto Submap::parseConfig(const Json::Value& config) -> void {
auto const alwaysOn = config["always-on"];
auto const& alwaysOn = config["always-on"];
if (alwaysOn.isBool()) {
always_on_ = alwaysOn.asBool();
}

auto const defaultSubmap = config["default-submap"];
auto const& defaultSubmap = config["default-submap"];
if (defaultSubmap.isString()) {
default_submap_ = defaultSubmap.asString();
}
Expand Down
3 changes: 0 additions & 3 deletions src/modules/hyprland/windowcreationpayload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
#include <json/value.h>
#include <spdlog/spdlog.h>

#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include <variant>

#include "modules/hyprland/workspaces.hpp"
#include "util/regex_collection.hpp"

namespace waybar::modules::hyprland {

Expand Down
3 changes: 0 additions & 3 deletions src/modules/hyprland/workspace.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include <json/value.h>
#include <spdlog/spdlog.h>

#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include <variant>

#include "modules/hyprland/workspaces.hpp"
#include "util/regex_collection.hpp"

namespace waybar::modules::hyprland {

Expand Down
7 changes: 3 additions & 4 deletions src/modules/hyprland/workspaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <memory>
#include <string>
#include <utility>
#include <variant>

#include "util/regex_collection.hpp"

Expand Down Expand Up @@ -593,14 +592,14 @@ auto Workspaces::populateIconsMap(const Json::Value &formatIcons) -> void {

auto Workspaces::populateBoolConfig(const Json::Value &config, const std::string &key, bool &member)
-> void {
auto configValue = config[key];
const auto &configValue = config[key];
if (configValue.isBool()) {
member = configValue.asBool();
}
}

auto Workspaces::populateSortByConfig(const Json::Value &config) -> void {
auto configSortBy = config["sort-by"];
const auto &configSortBy = config["sort-by"];
if (configSortBy.isString()) {
auto sortByStr = configSortBy.asString();
try {
Expand Down Expand Up @@ -633,7 +632,7 @@ auto Workspaces::populateIgnoreWorkspacesConfig(const Json::Value &config) -> vo
}

auto Workspaces::populateFormatWindowSeparatorConfig(const Json::Value &config) -> void {
auto formatWindowSeparator = config["format-window-separator"];
const auto &formatWindowSeparator = config["format-window-separator"];
m_formatWindowSeparator =
formatWindowSeparator.isString() ? formatWindowSeparator.asString() : " ";
}
Expand Down
10 changes: 8 additions & 2 deletions test/hyprland/backend.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include <cstdlib>
#if __has_include(<catch2/catch_test_macros.hpp>)
#include <catch2/catch_test_macros.hpp>
#else
#include <catch2/catch.hpp>
#endif

#include "fixtures/IPCTestFixture.hpp"
#include "modules/hyprland/backend.hpp"

namespace fs = std::filesystem;
namespace hyprland = waybar::modules::hyprland;
Expand Down Expand Up @@ -53,3 +51,11 @@ TEST_CASE_METHOD(IPCTestFixture, "XDGRuntimeDirExistsNoHyprDir", "[getSocketFold
// Assert expected result
REQUIRE(actualPath == expectedPath);
}

TEST_CASE_METHOD(IPCMock, "getSocket1JsonReply handles empty response", "[getSocket1JsonReply]") {
std::string request = "test_request";

Json::Value jsonResponse = getSocket1JsonReply(request);

REQUIRE(jsonResponse.isNull());
}
6 changes: 6 additions & 0 deletions test/hyprland/fixtures/IPCTestFixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ class IPCTestFixture : public hyprland::IPC {

private:
};

class IPCMock : public IPCTestFixture {
public:
// Mock getSocket1Reply to return an empty string
static std::string getSocket1Reply(const std::string& rq) { return ""; }
};