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()
|
|
|
|
: fd_(-1), fd_event_(-1)
|
|
|
|
{}
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2018-08-20 12:50:45 +00:00
|
|
|
waybar::modules::sway::Ipc::~Ipc()
|
|
|
|
{
|
|
|
|
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
|
|
|
void waybar::modules::sway::Ipc::connect()
|
|
|
|
{
|
|
|
|
const std::string& socketPath = getSocketPath();
|
|
|
|
fd_ = open(socketPath);
|
|
|
|
fd_event_ = open(socketPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-08-20 12:50:45 +00:00
|
|
|
header.reserve(ipc_header_size_);
|
|
|
|
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_) {
|
2018-08-18 14:01:56 +00:00
|
|
|
ssize_t res =
|
2018-08-20 12:50:45 +00:00
|
|
|
::recv(fd, header.data() + total, ipc_header_size_ - total, 0);
|
2018-08-18 14:01:56 +00:00
|
|
|
if (res <= 0) {
|
2018-08-13 19:23:43 +00:00
|
|
|
throw std::runtime_error("Unable to receive IPC response");
|
|
|
|
}
|
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-08-13 19:23:43 +00:00
|
|
|
total = 0;
|
2018-08-18 14:01:56 +00:00
|
|
|
std::string payload;
|
2018-08-20 12:50:45 +00:00
|
|
|
payload.reserve(data32[0] + 1);
|
2018-08-18 14:01:56 +00:00
|
|
|
while (total < data32[0]) {
|
|
|
|
ssize_t res =
|
2018-08-20 12:50:45 +00:00
|
|
|
::recv(fd, payload.data() + total, data32[0] - total, 0);
|
2018-08-18 14:01:56 +00:00
|
|
|
if (res < 0) {
|
2018-08-13 19:23:43 +00:00
|
|
|
throw std::runtime_error("Unable to receive IPC response");
|
|
|
|
}
|
2018-08-18 14:01:56 +00:00
|
|
|
total += res;
|
2018-08-13 19:23:43 +00:00
|
|
|
}
|
2018-08-20 12:50:45 +00:00
|
|
|
payload[data32[0]] = 0;
|
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;
|
2018-08-20 12:50:45 +00:00
|
|
|
header.reserve(ipc_header_size_);
|
|
|
|
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);
|
2018-11-29 08:50:00 +00:00
|
|
|
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
|
|
|
}
|