mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Style code (#25)
This commit is contained in:
@ -1,22 +1,24 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "modules/sway/ipc/client.hpp"
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include "modules/sway/ipc/client.hpp"
|
||||
|
||||
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
|
||||
static const size_t ipc_header_size = sizeof(ipc_magic)+8;
|
||||
|
||||
std::string get_socketpath(void) {
|
||||
std::string getSocketPath() {
|
||||
const char *env = getenv("SWAYSOCK");
|
||||
if (env) return std::string(env);
|
||||
if (env != nullptr) {
|
||||
return std::string(env);
|
||||
}
|
||||
std::string str;
|
||||
{
|
||||
std::string str_buf;
|
||||
FILE* in;
|
||||
char buf[512] = { 0 };
|
||||
if (!(in = popen("sway --get-socketpath 2>/dev/null", "r"))) {
|
||||
if ((in = popen("sway --get-socketpath 2>/dev/null", "r")) == nullptr) {
|
||||
throw std::runtime_error("Failed to get socket path");
|
||||
}
|
||||
while (fgets(buf, sizeof(buf), in) != nullptr) {
|
||||
@ -31,26 +33,26 @@ std::string get_socketpath(void) {
|
||||
return str;
|
||||
}
|
||||
|
||||
int ipc_open_socket(std::string socket_path) {
|
||||
struct sockaddr_un addr;
|
||||
int ipcOpenSocket(const std::string &socketPath) {
|
||||
struct sockaddr_un addr = {};
|
||||
int socketfd;
|
||||
if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
||||
throw std::runtime_error("Unable to open Unix socket");
|
||||
}
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, socket_path.c_str(), sizeof(addr.sun_path) - 1);
|
||||
strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1);
|
||||
addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
|
||||
int l = sizeof(struct sockaddr_un);
|
||||
if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) {
|
||||
throw std::runtime_error("Unable to connect to " + socket_path);
|
||||
if (connect(socketfd, reinterpret_cast<struct sockaddr *>(&addr), l) == -1) {
|
||||
throw std::runtime_error("Unable to connect to " + socketPath);
|
||||
}
|
||||
return socketfd;
|
||||
}
|
||||
|
||||
struct ipc_response ipc_recv_response(int socketfd) {
|
||||
struct ipc_response ipcRecvResponse(int socketfd) {
|
||||
struct ipc_response response;
|
||||
char data[ipc_header_size];
|
||||
uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
|
||||
auto data32 = reinterpret_cast<uint32_t *>(data + sizeof(ipc_magic));
|
||||
size_t total = 0;
|
||||
|
||||
while (total < ipc_header_size) {
|
||||
@ -78,18 +80,20 @@ struct ipc_response ipc_recv_response(int socketfd) {
|
||||
return response;
|
||||
}
|
||||
|
||||
std::string ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) {
|
||||
std::string ipcSingleCommand(int socketfd, uint32_t type, const char *payload, uint32_t *len) {
|
||||
char data[ipc_header_size];
|
||||
uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
|
||||
auto data32 = reinterpret_cast<uint32_t *>(data + sizeof(ipc_magic));
|
||||
memcpy(data, ipc_magic, sizeof(ipc_magic));
|
||||
data32[0] = *len;
|
||||
data32[1] = type;
|
||||
|
||||
if (send(socketfd, data, ipc_header_size, 0) == -1)
|
||||
if (send(socketfd, data, ipc_header_size, 0) == -1) {
|
||||
throw std::runtime_error("Unable to send IPC header");
|
||||
if (send(socketfd, payload, *len, 0) == -1)
|
||||
}
|
||||
if (send(socketfd, payload, *len, 0) == -1) {
|
||||
throw std::runtime_error("Unable to send IPC payload");
|
||||
struct ipc_response resp = ipc_recv_response(socketfd);
|
||||
}
|
||||
struct ipc_response resp = ipcRecvResponse(socketfd);
|
||||
*len = resp.size;
|
||||
return resp.payload;
|
||||
}
|
||||
|
Reference in New Issue
Block a user