From 27f03c705d8851a8ef6ca9e8f7828c1a2bfd9a88 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 27 Nov 2015 09:50:04 -0500 Subject: [PATCH] Move IPC client into common, refactor IPC --- common/ipc-client.c | 78 ++++++++++++++++++++++++++++++++++++ include/ipc-client.h | 9 +++++ include/ipc-server.h | 13 ++++++ include/ipc.h | 8 ---- sway/focus.c | 2 +- sway/{ipc.c => ipc-server.c} | 2 +- sway/main.c | 2 +- swaymsg/main.c | 70 +------------------------------- 8 files changed, 105 insertions(+), 79 deletions(-) create mode 100644 common/ipc-client.c create mode 100644 include/ipc-client.h create mode 100644 include/ipc-server.h rename sway/{ipc.c => ipc-server.c} (99%) diff --git a/common/ipc-client.c b/common/ipc-client.c new file mode 100644 index 000000000..916676a9a --- /dev/null +++ b/common/ipc-client.c @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "log.h" +#include "stringop.h" +#include "ipc-server.h" +#include "readline.h" + +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; +} + +char *ipc_single_command(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; +} diff --git a/include/ipc-client.h b/include/ipc-client.h new file mode 100644 index 000000000..a56fee432 --- /dev/null +++ b/include/ipc-client.h @@ -0,0 +1,9 @@ +#ifndef _SWAY_IPC_CLIENT_H +#define _SWAY_IPC_CLIENT_H + +#include "ipc.h" + +char *get_socketpath(void); +char *ipc_single_command(const char *socket_path, uint32_t type, const char *payload, uint32_t len); + +#endif diff --git a/include/ipc-server.h b/include/ipc-server.h new file mode 100644 index 000000000..35b3748ba --- /dev/null +++ b/include/ipc-server.h @@ -0,0 +1,13 @@ +#ifndef _SWAY_IPC_SERVER_H +#define _SWAY_IPC_SERVER_H + +#include "container.h" +#include "ipc.h" + +void ipc_init(void); +void ipc_terminate(void); +struct sockaddr_un *ipc_user_sockaddr(void); + +void ipc_event_workspace(swayc_t *old, swayc_t *new); + +#endif diff --git a/include/ipc.h b/include/ipc.h index 02aa1c1e2..75be58a62 100644 --- a/include/ipc.h +++ b/include/ipc.h @@ -1,8 +1,6 @@ #ifndef _SWAY_IPC_H #define _SWAY_IPC_H -#include "container.h" - enum ipc_command_type { IPC_COMMAND = 0, IPC_GET_WORKSPACES = 1, @@ -15,10 +13,4 @@ enum ipc_command_type { IPC_SWAY_GET_PIXELS = 0x81 }; -void ipc_init(void); -void ipc_terminate(void); -struct sockaddr_un *ipc_user_sockaddr(void); - -void ipc_event_workspace(swayc_t *old, swayc_t *new); - #endif diff --git a/sway/focus.c b/sway/focus.c index 3800a46cd..7af858a19 100644 --- a/sway/focus.c +++ b/sway/focus.c @@ -6,7 +6,7 @@ #include "layout.h" #include "config.h" #include "input_state.h" -#include "ipc.h" +#include "ipc-server.h" bool locked_container_focus = false; bool locked_view_focus = false; diff --git a/sway/ipc.c b/sway/ipc-server.c similarity index 99% rename from sway/ipc.c rename to sway/ipc-server.c index e004aff17..7c7373072 100644 --- a/sway/ipc.c +++ b/sway/ipc-server.c @@ -13,7 +13,7 @@ #include #include #include -#include "ipc.h" +#include "ipc-server.h" #include "log.h" #include "config.h" #include "commands.h" diff --git a/sway/main.c b/sway/main.c index ebb459301..196487826 100644 --- a/sway/main.c +++ b/sway/main.c @@ -14,7 +14,7 @@ #include "log.h" #include "readline.h" #include "handlers.h" -#include "ipc.h" +#include "ipc-server.h" #include "sway.h" static bool terminate_request = false; diff --git a/swaymsg/main.c b/swaymsg/main.c index ea8e0a550..8d20905af 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -7,80 +7,14 @@ #include #include #include "stringop.h" -#include "ipc.h" +#include "ipc-client.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); } -char *get_socketpath(void) { - FILE *fp = popen("sway --get-socketpath", "r"); - if (!fp) { - return NULL; - } - char *line = read_line(fp); - pclose(fp); - 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; @@ -159,7 +93,7 @@ int main(int argc, char **argv) { command = join_args(argv + optind, argc - optind); } - char *resp = do_ipc(socket_path, type, command, strlen(command)); + char *resp = ipc_single_command(socket_path, type, command, strlen(command)); if (!quiet) { printf("%s", resp); }