modules/hyprland/backend: move getSocketFolder to class

This commit is contained in:
Austin Horstman 2024-06-08 23:52:58 -05:00
parent 58e7abba2c
commit fa2e21dfd5
No known key found for this signature in database
2 changed files with 17 additions and 11 deletions

View File

@ -26,6 +26,10 @@ class IPC {
static std::string getSocket1Reply(const std::string& rq); static std::string getSocket1Reply(const std::string& rq);
Json::Value getSocket1JsonReply(const std::string& rq); Json::Value getSocket1JsonReply(const std::string& rq);
static std::filesystem::path getSocketFolder(const char* instanceSig);
protected:
static std::filesystem::path socketFolder_;
private: private:
void startIPC(); void startIPC();
@ -38,5 +42,4 @@ class IPC {
inline std::unique_ptr<IPC> gIPC; inline std::unique_ptr<IPC> gIPC;
inline bool modulesReady = false; inline bool modulesReady = false;
std::filesystem::path getSocketFolder(const char* instanceSig);
}; // namespace waybar::modules::hyprland }; // namespace waybar::modules::hyprland

View File

@ -15,22 +15,25 @@
namespace waybar::modules::hyprland { namespace waybar::modules::hyprland {
std::filesystem::path getSocketFolder(const char* instanceSig) { std::filesystem::path IPC::socketFolder_;
std::filesystem::path IPC::getSocketFolder(const char* instanceSig) {
// socket path, specified by EventManager of Hyprland // socket path, specified by EventManager of Hyprland
static std::filesystem::path socketFolder; if (!socketFolder_.empty()) {
if (!socketFolder.empty()) { spdlog::warn("socketFolder already set, using {}", socketFolder_.c_str());
return socketFolder; return socketFolder_;
}
} }
std::filesystem::path xdgRuntimeDir = std::filesystem::path(getenv("XDG_RUNTIME_DIR")); std::filesystem::path xdgRuntimeDir = std::filesystem::path(getenv("XDG_RUNTIME_DIR"));
if (!xdgRuntimeDir.empty() && std::filesystem::exists(xdgRuntimeDir / "hypr")) { if (!xdgRuntimeDir.empty() && std::filesystem::exists(xdgRuntimeDir / "hypr")) {
socketFolder = xdgRuntimeDir / "hypr"; socketFolder_ = xdgRuntimeDir / "hypr";
} else { } else {
spdlog::warn("$XDG_RUNTIME_DIR/hypr does not exist, falling back to /tmp/hypr"); spdlog::warn("$XDG_RUNTIME_DIR/hypr does not exist, falling back to /tmp/hypr");
socketFolder = std::filesystem::path("/tmp") / "hypr"; socketFolder_ = std::filesystem::path("/tmp") / "hypr";
} }
socketFolder = socketFolder / instanceSig; socketFolder_ = socketFolder_ / instanceSig;
return socketFolder; return socketFolder_;
} }
void IPC::startIPC() { void IPC::startIPC() {
@ -59,7 +62,7 @@ void IPC::startIPC() {
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
auto socketPath = getSocketFolder(his) / ".socket2.sock"; auto socketPath = IPC::getSocketFolder(his) / ".socket2.sock";
strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1); strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1);
addr.sun_path[sizeof(addr.sun_path) - 1] = 0; addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
@ -169,7 +172,7 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
sockaddr_un serverAddress = {0}; sockaddr_un serverAddress = {0};
serverAddress.sun_family = AF_UNIX; serverAddress.sun_family = AF_UNIX;
std::string socketPath = getSocketFolder(instanceSig) / ".socket.sock"; std::string socketPath = IPC::getSocketFolder(instanceSig) / ".socket.sock";
// Use snprintf to copy the socketPath string into serverAddress.sun_path // Use snprintf to copy the socketPath string into serverAddress.sun_path
if (snprintf(serverAddress.sun_path, sizeof(serverAddress.sun_path), "%s", socketPath.c_str()) < if (snprintf(serverAddress.sun_path, sizeof(serverAddress.sun_path), "%s", socketPath.c_str()) <