Waybar/src/modules/sway/ipc/client.cpp

137 lines
3.7 KiB
C++
Raw Normal View History

2018-08-16 12:29:41 +00:00
#include "modules/sway/ipc/client.hpp"
2018-08-08 21:54:58 +00:00
2018-08-20 12:50:45 +00:00
waybar::modules::sway::Ipc::Ipc()
2018-12-26 10:13:36 +00:00
{
const std::string& socketPath = getSocketPath();
fd_ = open(socketPath);
fd_event_ = open(socketPath);
}
2018-08-08 21:54:58 +00:00
2018-08-20 12:50:45 +00:00
waybar::modules::sway::Ipc::~Ipc()
{
2018-12-26 10:13:36 +00:00
// To fail the IPC header
write(fd_, "close-sway-ipc", 14);
write(fd_event_, "close-sway-ipc", 14);
2018-08-20 12:50:45 +00:00
close(fd_);
close(fd_event_);
}
const std::string waybar::modules::sway::Ipc::getSocketPath() const
{
2018-08-08 21:54:58 +00:00
const char *env = getenv("SWAYSOCK");
2018-08-16 12:29:41 +00:00
if (env != nullptr) {
return std::string(env);
}
2018-08-13 19:23:43 +00:00
std::string str;
2018-08-08 21:54:58 +00:00
{
2018-08-13 19:23:43 +00:00
std::string str_buf;
FILE* in;
char buf[512] = { 0 };
2018-08-16 12:29:41 +00:00
if ((in = popen("sway --get-socketpath 2>/dev/null", "r")) == nullptr) {
2018-08-13 19:23:43 +00:00
throw std::runtime_error("Failed to get socket path");
}
while (fgets(buf, sizeof(buf), in) != nullptr) {
str_buf.append(buf, sizeof(buf));
}
pclose(in);
str = str_buf;
}
if (str.back() == '\n') {
str.pop_back();
}
return str;
2018-08-08 21:54:58 +00:00
}
2018-08-20 12:50:45 +00:00
int waybar::modules::sway::Ipc::open(const std::string& socketPath) const
{
2018-08-17 12:24:00 +00:00
struct sockaddr_un addr = {0};
2018-08-20 12:50:45 +00:00
int fd = -1;
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
2018-08-13 19:23:43 +00:00
throw std::runtime_error("Unable to open Unix socket");
}
addr.sun_family = AF_UNIX;
2018-08-16 12:29:41 +00:00
strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1);
2018-08-13 19:23:43 +00:00
addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
int l = sizeof(struct sockaddr_un);
2018-08-20 12:50:45 +00:00
if (::connect(fd, reinterpret_cast<struct sockaddr *>(&addr), l) == -1) {
2018-08-29 19:07:58 +00:00
throw std::runtime_error("Unable to connect to Sway");
2018-08-13 19:23:43 +00:00
}
2018-08-20 12:50:45 +00:00
return fd;
2018-08-08 21:54:58 +00:00
}
2018-08-20 12:50:45 +00:00
struct waybar::modules::sway::Ipc::ipc_response
waybar::modules::sway::Ipc::recv(int fd) const
{
2018-08-18 14:01:56 +00:00
std::string header;
header.resize(ipc_header_size_);
2018-08-20 12:50:45 +00:00
auto data32 = reinterpret_cast<uint32_t *>(header.data() + ipc_magic_.size());
2018-08-13 19:23:43 +00:00
size_t total = 0;
2018-08-08 21:54:58 +00:00
2018-08-20 12:50:45 +00:00
while (total < ipc_header_size_) {
auto res = ::recv(fd, header.data() + total, ipc_header_size_ - total, 0);
2018-08-18 14:01:56 +00:00
if (res <= 0) {
2018-12-26 10:13:36 +00:00
throw std::runtime_error("Unable to receive IPC header");
2018-08-13 19:23:43 +00:00
}
2018-08-18 14:01:56 +00:00
total += res;
2018-08-13 19:23:43 +00:00
}
2018-08-08 21:54:58 +00:00
2018-12-26 10:13:36 +00:00
auto magic = std::string(header.data(), header.data() + ipc_magic_.size());
if (magic != ipc_magic_) {
throw std::runtime_error("Invalid IPC magic");
}
2018-08-13 19:23:43 +00:00
total = 0;
2018-08-18 14:01:56 +00:00
std::string payload;
payload.resize(data32[0]);
2018-08-18 14:01:56 +00:00
while (total < data32[0]) {
auto res = ::recv(fd, payload.data() + total, data32[0] - total, 0);
2018-08-18 14:01:56 +00:00
if (res < 0) {
2018-12-26 10:13:36 +00:00
throw std::runtime_error("Unable to receive IPC payload");
2018-08-13 19:23:43 +00:00
}
2018-08-18 14:01:56 +00:00
total += res;
2018-08-13 19:23:43 +00:00
}
2018-08-18 14:01:56 +00:00
return { data32[0], data32[1], &payload.front() };
2018-08-08 21:54:58 +00:00
}
2018-08-20 12:50:45 +00:00
struct waybar::modules::sway::Ipc::ipc_response
waybar::modules::sway::Ipc::send(int fd, uint32_t type,
const std::string& payload) const
{
2018-08-18 14:01:56 +00:00
std::string header;
header.resize(ipc_header_size_);
2018-08-20 12:50:45 +00:00
auto data32 = reinterpret_cast<uint32_t *>(header.data() + ipc_magic_.size());
memcpy(header.data(), ipc_magic_.c_str(), ipc_magic_.size());
2018-08-18 14:01:56 +00:00
data32[0] = payload.size();
2018-08-13 19:23:43 +00:00
data32[1] = type;
2018-08-08 21:54:58 +00:00
2018-08-20 12:50:45 +00:00
if (::send(fd, header.data(), ipc_header_size_, 0) == -1) {
2018-08-13 19:23:43 +00:00
throw std::runtime_error("Unable to send IPC header");
2018-08-16 12:29:41 +00:00
}
2018-08-20 12:50:45 +00:00
if (::send(fd, payload.c_str(), payload.size(), 0) == -1) {
2018-08-13 19:23:43 +00:00
throw std::runtime_error("Unable to send IPC payload");
2018-08-16 12:29:41 +00:00
}
2018-08-20 12:50:45 +00:00
return recv(fd);
}
struct waybar::modules::sway::Ipc::ipc_response
waybar::modules::sway::Ipc::sendCmd(uint32_t type,
const std::string& payload) const
{
return send(fd_, type, payload);
}
void waybar::modules::sway::Ipc::subscribe(const std::string& payload) const
{
auto res = send(fd_event_, IPC_SUBSCRIBE, payload);
if (res.payload != "{\"success\": true}") {
2018-08-20 12:50:45 +00:00
throw std::runtime_error("Unable to subscribe ipc event");
}
2018-08-08 21:54:58 +00:00
}
2018-08-20 12:50:45 +00:00
struct waybar::modules::sway::Ipc::ipc_response
waybar::modules::sway::Ipc::handleEvent() const
{
return recv(fd_event_);
2018-11-28 22:27:06 +00:00
}