2015-11-28 01:50:04 +11:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "log.h"
|
|
|
|
#include "stringop.h"
|
2015-11-29 00:57:45 +11:00
|
|
|
#include "ipc.h"
|
2015-11-28 01:50:04 +11:00
|
|
|
#include "readline.h"
|
2016-01-04 06:40:50 +11:00
|
|
|
#include "ipc-client.h"
|
2015-11-28 01:50:04 +11:00
|
|
|
|
|
|
|
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
|
|
|
|
static const size_t ipc_header_size = sizeof(ipc_magic)+8;
|
|
|
|
|
|
|
|
char *get_socketpath(void) {
|
|
|
|
FILE *fp = popen("sway --get-socketpath", "r");
|
|
|
|
if (!fp) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
char *line = read_line(fp);
|
|
|
|
pclose(fp);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2015-11-28 02:10:29 +11:00
|
|
|
int ipc_open_socket(const char *socket_path) {
|
2015-11-28 01:50:04 +11:00
|
|
|
struct sockaddr_un addr;
|
|
|
|
int socketfd;
|
|
|
|
if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
|
|
|
sway_abort("Unable to open Unix socket");
|
|
|
|
}
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strcpy(addr.sun_path, socket_path);
|
|
|
|
int l = sizeof(addr.sun_family) + strlen(addr.sun_path);
|
|
|
|
if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) {
|
|
|
|
sway_abort("Unable to connect to %s", socket_path);
|
|
|
|
}
|
2015-11-28 02:10:29 +11:00
|
|
|
return socketfd;
|
|
|
|
}
|
2015-11-28 01:50:04 +11:00
|
|
|
|
2016-01-04 06:40:50 +11:00
|
|
|
struct ipc_response *ipc_recv_response(int socketfd) {
|
2015-11-28 01:50:04 +11:00
|
|
|
char data[ipc_header_size];
|
|
|
|
uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
|
|
|
|
|
|
|
|
size_t total = 0;
|
|
|
|
while (total < ipc_header_size) {
|
|
|
|
ssize_t received = recv(socketfd, data + total, ipc_header_size - total, 0);
|
|
|
|
if (received < 0) {
|
|
|
|
sway_abort("Unable to receive IPC response");
|
|
|
|
}
|
|
|
|
total += received;
|
|
|
|
}
|
|
|
|
|
2016-01-04 06:40:50 +11:00
|
|
|
struct ipc_response *response = malloc(sizeof(struct ipc_response));
|
2015-11-28 01:50:04 +11:00
|
|
|
total = 0;
|
2016-01-04 06:40:50 +11:00
|
|
|
response->size = data32[0];
|
|
|
|
response->type = data32[1];
|
|
|
|
char *payload = malloc(response->size + 1);
|
|
|
|
while (total < response->size) {
|
|
|
|
ssize_t received = recv(socketfd, payload + total, response->size - total, 0);
|
2015-11-28 01:50:04 +11:00
|
|
|
if (received < 0) {
|
|
|
|
sway_abort("Unable to receive IPC response");
|
|
|
|
}
|
|
|
|
total += received;
|
|
|
|
}
|
2016-01-04 06:40:50 +11:00
|
|
|
payload[response->size] = '\0';
|
|
|
|
response->payload = payload;
|
2015-11-28 01:50:04 +11:00
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
2015-12-14 08:04:54 +11:00
|
|
|
|
2016-01-04 06:40:50 +11:00
|
|
|
void free_ipc_response(struct ipc_response *response) {
|
|
|
|
free(response->payload);
|
|
|
|
free(response);
|
|
|
|
}
|
|
|
|
|
2015-12-14 08:04:54 +11:00
|
|
|
char *ipc_single_command(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));
|
|
|
|
memcpy(data, ipc_magic, sizeof(ipc_magic));
|
|
|
|
data32[0] = *len;
|
|
|
|
data32[1] = type;
|
|
|
|
|
|
|
|
if (write(socketfd, data, ipc_header_size) == -1) {
|
|
|
|
sway_abort("Unable to send IPC header");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(socketfd, payload, *len) == -1) {
|
|
|
|
sway_abort("Unable to send IPC payload");
|
|
|
|
}
|
|
|
|
|
2016-01-04 06:40:50 +11:00
|
|
|
struct ipc_response *resp = ipc_recv_response(socketfd);
|
|
|
|
char *response = resp->payload;
|
|
|
|
*len = resp->size;
|
|
|
|
free(resp);
|
|
|
|
|
|
|
|
return response;
|
2015-12-14 08:04:54 +11:00
|
|
|
}
|