refactor(sway): use shared ScopedFd for IPC sockets
Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
#include "ipc.hpp"
|
#include "ipc.hpp"
|
||||||
#include "util/SafeSignal.hpp"
|
#include "util/SafeSignal.hpp"
|
||||||
#include "util/sleeper_thread.hpp"
|
#include "util/sleeper_thread.hpp"
|
||||||
|
#include "util/scoped_fd.hpp"
|
||||||
|
|
||||||
namespace waybar::modules::sway {
|
namespace waybar::modules::sway {
|
||||||
|
|
||||||
@@ -45,8 +46,8 @@ class Ipc {
|
|||||||
struct ipc_response send(int fd, uint32_t type, const std::string& payload = "");
|
struct ipc_response send(int fd, uint32_t type, const std::string& payload = "");
|
||||||
struct ipc_response recv(int fd);
|
struct ipc_response recv(int fd);
|
||||||
|
|
||||||
int fd_;
|
util::ScopedFd fd_;
|
||||||
int fd_event_;
|
util::ScopedFd fd_event_;
|
||||||
std::mutex mutex_;
|
std::mutex mutex_;
|
||||||
util::SleeperThread thread_;
|
util::SleeperThread thread_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ namespace waybar::modules::sway {
|
|||||||
|
|
||||||
Ipc::Ipc() {
|
Ipc::Ipc() {
|
||||||
const std::string& socketPath = getSocketPath();
|
const std::string& socketPath = getSocketPath();
|
||||||
fd_ = open(socketPath);
|
fd_ = util::ScopedFd(open(socketPath));
|
||||||
fd_event_ = open(socketPath);
|
fd_event_ = util::ScopedFd(open(socketPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ipc::~Ipc() {
|
Ipc::~Ipc() {
|
||||||
@@ -21,15 +21,11 @@ Ipc::~Ipc() {
|
|||||||
if (write(fd_, "close-sway-ipc", 14) == -1) {
|
if (write(fd_, "close-sway-ipc", 14) == -1) {
|
||||||
spdlog::error("Failed to close sway IPC");
|
spdlog::error("Failed to close sway IPC");
|
||||||
}
|
}
|
||||||
close(fd_);
|
|
||||||
fd_ = -1;
|
|
||||||
}
|
}
|
||||||
if (fd_event_ > 0) {
|
if (fd_event_ > 0) {
|
||||||
if (write(fd_event_, "close-sway-ipc", 14) == -1) {
|
if (write(fd_event_, "close-sway-ipc", 14) == -1) {
|
||||||
spdlog::error("Failed to close sway IPC event handler");
|
spdlog::error("Failed to close sway IPC event handler");
|
||||||
}
|
}
|
||||||
close(fd_event_);
|
|
||||||
fd_event_ = -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +60,7 @@ const std::string Ipc::getSocketPath() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int Ipc::open(const std::string& socketPath) const {
|
int Ipc::open(const std::string& socketPath) const {
|
||||||
int32_t fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
util::ScopedFd fd(socket(AF_UNIX, SOCK_STREAM, 0));
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
throw std::runtime_error("Unable to open Unix socket");
|
throw std::runtime_error("Unable to open Unix socket");
|
||||||
}
|
}
|
||||||
@@ -78,7 +74,7 @@ int Ipc::open(const std::string& socketPath) const {
|
|||||||
if (::connect(fd, reinterpret_cast<struct sockaddr*>(&addr), l) == -1) {
|
if (::connect(fd, reinterpret_cast<struct sockaddr*>(&addr), l) == -1) {
|
||||||
throw std::runtime_error("Unable to connect to Sway");
|
throw std::runtime_error("Unable to connect to Sway");
|
||||||
}
|
}
|
||||||
return fd;
|
return fd.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Ipc::ipc_response Ipc::recv(int fd) {
|
struct Ipc::ipc_response Ipc::recv(int fd) {
|
||||||
|
|||||||
Reference in New Issue
Block a user