refactor(hyprland): use shared ScopedFd utility

Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
Austin Horstman
2026-03-02 08:21:15 -06:00
parent 6dfe1c3111
commit e83ab7609c

View File

@@ -15,29 +15,10 @@
#include <filesystem> #include <filesystem>
#include <string> #include <string>
#include "util/scoped_fd.hpp"
namespace waybar::modules::hyprland { namespace waybar::modules::hyprland {
namespace {
class ScopedFd {
public:
explicit ScopedFd(int fd) : fd_(fd) {}
~ScopedFd() {
if (fd_ != -1) {
close(fd_);
}
}
// ScopedFd is non-copyable
ScopedFd(const ScopedFd&) = delete;
ScopedFd& operator=(const ScopedFd&) = delete;
int get() const { return fd_; }
private:
int fd_;
};
} // namespace
std::filesystem::path IPC::socketFolder_; std::filesystem::path IPC::socketFolder_;
std::filesystem::path IPC::getSocketFolder(const char* instanceSig) { std::filesystem::path IPC::getSocketFolder(const char* instanceSig) {
@@ -232,9 +213,9 @@ void IPC::unregisterForIPC(EventHandler* ev_handler) {
std::string IPC::getSocket1Reply(const std::string& rq) { std::string IPC::getSocket1Reply(const std::string& rq) {
// basically hyprctl // basically hyprctl
ScopedFd serverSocket(socket(AF_UNIX, SOCK_STREAM, 0)); util::ScopedFd serverSocket(socket(AF_UNIX, SOCK_STREAM, 0));
if (serverSocket.get() < 0) { if (serverSocket < 0) {
throw std::runtime_error("Hyprland IPC: Couldn't open a socket (1)"); throw std::runtime_error("Hyprland IPC: Couldn't open a socket (1)");
} }
@@ -257,13 +238,12 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
throw std::runtime_error("Hyprland IPC: Couldn't copy socket path (6)"); throw std::runtime_error("Hyprland IPC: Couldn't copy socket path (6)");
} }
if (connect(serverSocket.get(), reinterpret_cast<sockaddr*>(&serverAddress), if (connect(serverSocket, reinterpret_cast<sockaddr*>(&serverAddress), sizeof(serverAddress)) <
sizeof(serverAddress)) <
0) { 0) {
throw std::runtime_error("Hyprland IPC: Couldn't connect to " + socketPath + ". (3)"); throw std::runtime_error("Hyprland IPC: Couldn't connect to " + socketPath + ". (3)");
} }
auto sizeWritten = write(serverSocket.get(), rq.c_str(), rq.length()); auto sizeWritten = write(serverSocket, rq.c_str(), rq.length());
if (sizeWritten < 0) { if (sizeWritten < 0) {
spdlog::error("Hyprland IPC: Couldn't write (4)"); spdlog::error("Hyprland IPC: Couldn't write (4)");
@@ -274,7 +254,7 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
std::string response; std::string response;
do { do {
sizeWritten = read(serverSocket.get(), buffer.data(), 8192); sizeWritten = read(serverSocket, buffer.data(), 8192);
if (sizeWritten < 0) { if (sizeWritten < 0) {
spdlog::error("Hyprland IPC: Couldn't read (5)"); spdlog::error("Hyprland IPC: Couldn't read (5)");