refactor(ipc): clean

This commit is contained in:
Alexis 2018-08-18 16:01:56 +02:00
parent 27dfffa4e3
commit 38ede5b3d5
4 changed files with 38 additions and 48 deletions

View File

@ -25,7 +25,8 @@ int ipcOpenSocket(const std::string &socketPath);
* Issues a single IPC command and returns the buffer. len will be updated with * Issues a single IPC command and returns the buffer. len will be updated with
* the length of the buffer returned from sway. * the length of the buffer returned from sway.
*/ */
std::string ipcSingleCommand(int socketfd, uint32_t type, const char *payload, uint32_t *len); struct ipc_response ipcSingleCommand(int socketfd, uint32_t type,
const std::string& payload);
/** /**
* Receives a single IPC response and returns an ipc_response. * Receives a single IPC response and returns an ipc_response.
*/ */

View File

@ -5,8 +5,8 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; static const std::string ipc_magic("i3-ipc");
static const size_t ipc_header_size = sizeof(ipc_magic)+8; static const size_t ipc_header_size = ipc_magic.size() + 8;
std::string getSocketPath() { std::string getSocketPath() {
const char *env = getenv("SWAYSOCK"); const char *env = getenv("SWAYSOCK");
@ -50,50 +50,48 @@ int ipcOpenSocket(const std::string &socketPath) {
} }
struct ipc_response ipcRecvResponse(int socketfd) { struct ipc_response ipcRecvResponse(int socketfd) {
struct ipc_response response; std::string header;
char data[ipc_header_size]; header.reserve(ipc_header_size);
auto data32 = reinterpret_cast<uint32_t *>(data + sizeof(ipc_magic)); auto data32 = reinterpret_cast<uint32_t *>(header.data() + ipc_magic.size());
size_t total = 0; size_t total = 0;
while (total < ipc_header_size) { while (total < ipc_header_size) {
ssize_t received = recv(socketfd, data + total, ipc_header_size - total, 0); ssize_t res =
if (received <= 0) { ::recv(socketfd, header.data() + total, ipc_header_size - total, 0);
if (res <= 0) {
throw std::runtime_error("Unable to receive IPC response"); throw std::runtime_error("Unable to receive IPC response");
} }
total += received; total += res;
} }
total = 0; total = 0;
response.size = data32[0]; std::string payload;
response.type = data32[1]; payload.reserve(data32[0]);
char payload[response.size + 1]; while (total < data32[0]) {
ssize_t res =
while (total < response.size) { ::recv(socketfd, payload.data() + total, data32[0] - total, 0);
ssize_t received = recv(socketfd, payload + total, response.size - total, 0); if (res < 0) {
if (received < 0) {
throw std::runtime_error("Unable to receive IPC response"); throw std::runtime_error("Unable to receive IPC response");
} }
total += received; total += res;
} }
payload[response.size] = '\0'; return { data32[0], data32[1], &payload.front() };
response.payload = std::string(payload);
return response;
} }
std::string ipcSingleCommand(int socketfd, uint32_t type, const char *payload, uint32_t *len) { struct ipc_response ipcSingleCommand(int socketfd, uint32_t type,
char data[ipc_header_size]; const std::string& payload) {
auto data32 = reinterpret_cast<uint32_t *>(data + sizeof(ipc_magic)); std::string header;
memcpy(data, ipc_magic, sizeof(ipc_magic)); header.reserve(ipc_header_size);
data32[0] = *len; auto data32 = reinterpret_cast<uint32_t *>(header.data() + ipc_magic.size());
memcpy(header.data(), ipc_magic.c_str(), ipc_magic.size());
data32[0] = payload.size();
data32[1] = type; data32[1] = type;
if (send(socketfd, data, ipc_header_size, 0) == -1) { if (send(socketfd, header.data(), ipc_header_size, 0) == -1) {
throw std::runtime_error("Unable to send IPC header"); throw std::runtime_error("Unable to send IPC header");
} }
if (send(socketfd, payload, *len, 0) == -1) { if (send(socketfd, payload.c_str(), payload.size(), 0) == -1) {
throw std::runtime_error("Unable to send IPC payload"); throw std::runtime_error("Unable to send IPC payload");
} }
struct ipc_response resp = ipcRecvResponse(socketfd); return ipcRecvResponse(socketfd);
*len = resp.size;
return resp.payload;
} }

View File

@ -8,9 +8,7 @@ waybar::modules::sway::Window::Window(Bar &bar, Json::Value config)
std::string socketPath = getSocketPath(); std::string socketPath = getSocketPath();
ipcfd_ = ipcOpenSocket(socketPath); ipcfd_ = ipcOpenSocket(socketPath);
ipc_eventfd_ = ipcOpenSocket(socketPath); ipc_eventfd_ = ipcOpenSocket(socketPath);
const char *subscribe = "[ \"window\" ]"; ipcSingleCommand(ipc_eventfd_, IPC_SUBSCRIBE, "[ \"window\" ]");
uint32_t len = strlen(subscribe);
ipcSingleCommand(ipc_eventfd_, IPC_SUBSCRIBE, subscribe, &len);
getFocusedWindow(); getFocusedWindow();
thread_ = [this] { thread_ = [this] {
try { try {
@ -50,9 +48,8 @@ std::string waybar::modules::sway::Window::getFocusedNode(Json::Value nodes)
void waybar::modules::sway::Window::getFocusedWindow() void waybar::modules::sway::Window::getFocusedWindow()
{ {
try { try {
uint32_t len = 0; auto res = ipcSingleCommand(ipcfd_, IPC_GET_TREE, "");
auto res = ipcSingleCommand(ipcfd_, IPC_GET_TREE, nullptr, &len); auto parsed = parser_.parse(res.payload);
auto parsed = parser_.parse(res);
window_ = getFocusedNode(parsed["nodes"]); window_ = getFocusedNode(parsed["nodes"]);
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Window::update)); Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Window::update));
} catch (const std::exception &e) { } catch (const std::exception &e) {

View File

@ -8,9 +8,7 @@ waybar::modules::sway::Workspaces::Workspaces(Bar &bar, Json::Value config)
std::string socketPath = getSocketPath(); std::string socketPath = getSocketPath();
ipcfd_ = ipcOpenSocket(socketPath); ipcfd_ = ipcOpenSocket(socketPath);
ipc_eventfd_ = ipcOpenSocket(socketPath); ipc_eventfd_ = ipcOpenSocket(socketPath);
const char *subscribe = "[ \"workspace\" ]"; ipcSingleCommand(ipc_eventfd_, IPC_SUBSCRIBE, "[ \"workspace\" ]");
uint32_t len = strlen(subscribe);
ipcSingleCommand(ipc_eventfd_, IPC_SUBSCRIBE, subscribe, &len);
thread_ = [this] { thread_ = [this] {
try { try {
// Wait for the name of the output // Wait for the name of the output
@ -21,10 +19,9 @@ waybar::modules::sway::Workspaces::Workspaces(Bar &bar, Json::Value config)
} else if (!workspaces_.empty()) { } else if (!workspaces_.empty()) {
ipcRecvResponse(ipc_eventfd_); ipcRecvResponse(ipc_eventfd_);
} }
uint32_t len = 0;
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
auto str = ipcSingleCommand(ipcfd_, IPC_GET_WORKSPACES, nullptr, &len); auto res = ipcSingleCommand(ipcfd_, IPC_GET_WORKSPACES, "");
workspaces_ = parser_.parse(str); workspaces_ = parser_.parse(res.payload);
Glib::signal_idle() Glib::signal_idle()
.connect_once(sigc::mem_fun(*this, &Workspaces::update)); .connect_once(sigc::mem_fun(*this, &Workspaces::update));
} catch (const std::exception& e) { } catch (const std::exception& e) {
@ -97,9 +94,8 @@ void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node)
button.signal_clicked().connect([this, pair] { button.signal_clicked().connect([this, pair] {
try { try {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
auto value = fmt::format("workspace \"{}\"", pair.first->first); auto cmd = fmt::format("workspace \"{}\"", pair.first->first);
uint32_t size = value.size(); ipcSingleCommand(ipcfd_, IPC_COMMAND, cmd);
ipcSingleCommand(ipcfd_, IPC_COMMAND, value.c_str(), &size);
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }
@ -173,9 +169,7 @@ bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e)
scrolling_ = false; scrolling_ = false;
return false; return false;
} }
auto value = fmt::format("workspace \"{}\"", id); ipcSingleCommand(ipcfd_, IPC_COMMAND, fmt::format("workspace \"{}\"", id));
uint32_t size = value.size();
ipcSingleCommand(ipcfd_, IPC_COMMAND, value.c_str(), &size);
std::this_thread::sleep_for(std::chrono::milliseconds(150)); std::this_thread::sleep_for(std::chrono::milliseconds(150));
return true; return true;
} }