Skip to content

Commit

Permalink
hyprland/backend: throw runtime_error instead of log
Browse files Browse the repository at this point in the history
Allows us to disable modules entirely when socket connection isn't
working. This is similar to how sway handles their socket connections
disabling modules. This supports a single waybar config for multiple
IPCs.
  • Loading branch information
khaneliman committed Jul 16, 2024
1 parent 3f61df4 commit 4295faa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
16 changes: 6 additions & 10 deletions src/modules/hyprland/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,25 +153,23 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
const auto serverSocket = socket(AF_UNIX, SOCK_STREAM, 0);

if (serverSocket < 0) {
spdlog::error("Hyprland IPC: Couldn't open a socket (1)");
return "";
throw std::runtime_error("Hyprland IPC: Couldn't open a socket (1)");
}

memset(&aiHints, 0, sizeof(struct addrinfo));
aiHints.ai_family = AF_UNSPEC;
aiHints.ai_socktype = SOCK_STREAM;

if (getaddrinfo("localhost", nullptr, &aiHints, &aiRes) != 0) {
spdlog::error("Hyprland IPC: Couldn't get host (2)");
return "";
throw std::runtime_error("Hyprland IPC: Couldn't get host (2)");
}

// get the instance signature
auto* instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE");

if (instanceSig == nullptr) {
spdlog::error("Hyprland IPC: HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)");
return "";
throw std::runtime_error(
"Hyprland IPC: HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)");
}

sockaddr_un serverAddress = {0};
Expand All @@ -182,14 +180,12 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
// Use snprintf to copy the socketPath string into serverAddress.sun_path
if (snprintf(serverAddress.sun_path, sizeof(serverAddress.sun_path), "%s", socketPath.c_str()) <
0) {
spdlog::error("Hyprland IPC: Couldn't copy socket path (6)");
return "";
throw std::runtime_error("Hyprland IPC: Couldn't copy socket path (6)");
}

if (connect(serverSocket, reinterpret_cast<sockaddr*>(&serverAddress), sizeof(serverAddress)) <
0) {
spdlog::error("Hyprland IPC: Couldn't connect to " + socketPath + ". (3)");
return "";
throw std::runtime_error("Hyprland IPC: Couldn't connect to " + socketPath + ". (3)");
}

auto sizeWritten = write(serverSocket, rq.c_str(), rq.length());
Expand Down
6 changes: 2 additions & 4 deletions test/hyprland/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ TEST_CASE_METHOD(IPCTestFixture, "XDGRuntimeDirExistsNoHyprDir", "[getSocketFold
REQUIRE(actualPath == expectedPath);
}

TEST_CASE_METHOD(IPCMock, "getSocket1JsonReply handles empty response", "[getSocket1JsonReply]") {
TEST_CASE_METHOD(IPCTestFixture, "getSocket1Reply throws on no socket", "[getSocket1Reply]") {
std::string request = "test_request";

Json::Value jsonResponse = getSocket1JsonReply(request);

REQUIRE(jsonResponse.isNull());
CHECK_THROWS(getSocket1Reply(request));
}
3 changes: 3 additions & 0 deletions test/hyprland/fixtures/IPCTestFixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ class IPCMock : public IPCTestFixture {
public:
// Mock getSocket1Reply to return an empty string
static std::string getSocket1Reply(const std::string& rq) { return ""; }

protected:
const char* instanceSig = "instance_sig";
};

0 comments on commit 4295faa

Please sign in to comment.