aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2015-11-26 15:06:41 -0500
committerDrew DeVault <sir@cmpwn.com>2015-11-26 15:06:41 -0500
commita1018f32801320bab67261828394c76dd0fe0c45 (patch)
tree65ce2056743d8403da295dc1215fcfd1b205727c
parent9a15371ba3ab8497ac393d18baeb2f953a3b2762 (diff)
Implement swaymsg IPC behavior
-rw-r--r--swaymsg/main.c72
1 files changed, 70 insertions, 2 deletions
diff --git a/swaymsg/main.c b/swaymsg/main.c
index 7c643758..9c799b54 100644
--- a/swaymsg/main.c
+++ b/swaymsg/main.c
@@ -3,11 +3,17 @@
#include <string.h>
#include <getopt.h>
#include <stdint.h>
+#include <sys/un.h>
+#include <sys/socket.h>
+#include <unistd.h>
#include "stringop.h"
#include "ipc.h"
#include "readline.h"
#include "log.h"
+static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
+static const size_t ipc_header_size = sizeof(ipc_magic)+8;
+
void sway_terminate(void) {
exit(1);
}
@@ -22,11 +28,66 @@ char *get_socketpath(void) {
return line;
}
+char *do_ipc(const char *socket_path, uint32_t type, const char *payload, uint32_t len) {
+ 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);
+ }
+
+ 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");
+ }
+
+ 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;
+ }
+
+ total = 0;
+ len = data32[0];
+ char *response = malloc(len + 1);
+ while (total < len) {
+ ssize_t received = recv(socketfd, response + total, len - total, 0);
+ if (received < 0) {
+ sway_abort("Unable to receive IPC response");
+ }
+ total += received;
+ }
+ response[len] = '\0';
+
+ close(socketfd);
+
+ return response;
+}
+
int main(int argc, char **argv) {
static int quiet = 0;
char *socket_path = NULL;
char *cmdtype = NULL;
+ init_log(L_INFO);
+
static struct option long_options[] = {
{"quiet", no_argument, &quiet, 'q'},
{"version", no_argument, NULL, 'v'},
@@ -63,7 +124,7 @@ int main(int argc, char **argv) {
}
if (!cmdtype) {
- cmdtype = "command";
+ cmdtype = strdup("command");
}
if (!socket_path) {
socket_path = get_socketpath();
@@ -89,13 +150,20 @@ int main(int argc, char **argv) {
} else if (strcasecmp(cmdtype, "get_version") == 0) {
type = IPC_GET_VERSION;
}
+ free(cmdtype);
char *command = strdup("");
if (optind < argc) {
command = join_args(argv + optind, argc - optind);
}
- printf("%s", command);
+ char *resp = do_ipc(socket_path, type, command, strlen(command));
+ if (!quiet) {
+ printf("%s", resp);
+ }
+
+ free(command);
+ free(resp);
free(socket_path);
return 0;
}