From f41458ea24a57bb71b629089396c31fe4dd97f1c Mon Sep 17 00:00:00 2001 From: Tuur Vanhoutte <4633209+zjeffer@users.noreply.github.com> Date: Mon, 29 Apr 2024 19:46:28 +0200 Subject: [PATCH] Fix Hyprland socketpath changed to XDG_RUNTIME_DIR (#3183) --- src/modules/hyprland/backend.cpp | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/modules/hyprland/backend.cpp b/src/modules/hyprland/backend.cpp index 05db94ec..98eb8b90 100644 --- a/src/modules/hyprland/backend.cpp +++ b/src/modules/hyprland/backend.cpp @@ -9,11 +9,30 @@ #include #include +#include #include #include namespace waybar::modules::hyprland { +std::filesystem::path getSocketFolder(const char* instanceSig) { + // socket path, specified by EventManager of Hyprland + static std::filesystem::path socketFolder; + if (!socketFolder.empty()) { + return socketFolder; + } + + std::filesystem::path xdgRuntimeDir = std::filesystem::path(getenv("XDG_RUNTIME_DIR")); + if (!xdgRuntimeDir.empty() && std::filesystem::exists(xdgRuntimeDir / "hypr")) { + socketFolder = xdgRuntimeDir / "hypr"; + } else { + spdlog::warn("$XDG_RUNTIME_DIR/hypr does not exist, falling back to /tmp/hypr"); + socketFolder = std::filesystem::temp_directory_path() / "hypr"; + } + socketFolder = socketFolder / instanceSig; + return socketFolder; +} + void IPC::startIPC() { // will start IPC and relay events to parseIPC @@ -40,9 +59,7 @@ void IPC::startIPC() { addr.sun_family = AF_UNIX; - // socket path, specified by EventManager of Hyprland - std::string socketPath = "/tmp/hypr/" + std::string(his) + "/.socket2.sock"; - + auto socketPath = getSocketFolder(his) / ".socket2.sock"; strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1); addr.sun_path[sizeof(addr.sun_path) - 1] = 0; @@ -142,12 +159,10 @@ std::string IPC::getSocket1Reply(const std::string& rq) { return ""; } - std::string instanceSigStr = std::string(instanceSig); - sockaddr_un serverAddress = {0}; serverAddress.sun_family = AF_UNIX; - std::string socketPath = "/tmp/hypr/" + instanceSigStr + "/.socket.sock"; + std::string socketPath = getSocketFolder(instanceSig) / ".socket.sock"; // Use snprintf to copy the socketPath string into serverAddress.sun_path if (snprintf(serverAddress.sun_path, sizeof(serverAddress.sun_path), "%s", socketPath.c_str()) <