From 92319714c78c035506577ab6a4f32e5909054250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 13 Feb 2019 22:00:13 +0100 Subject: [PATCH] module/i3: break out send_pkg() --- modules/i3-common.c | 24 ++++++++++++++++++++++++ modules/i3-common.h | 1 + modules/i3.c | 26 +++----------------------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/modules/i3-common.c b/modules/i3-common.c index 41295fa..c876652 100644 --- a/modules/i3-common.c +++ b/modules/i3-common.c @@ -1,6 +1,7 @@ #include "i3-common.h" #include +#include #include #if defined(ENABLE_X11) @@ -8,6 +9,8 @@ #include #endif +#include + #define LOG_MODULE "i3:common" #include "../log.h" @@ -88,3 +91,24 @@ i3_get_socket_address(struct sockaddr_un *addr) strncpy(addr->sun_path, sway_sock, sizeof(addr->sun_path) - 1); return true; } + +bool +i3_send_pkg(int sock, int cmd, char *data) +{ + size_t size = data != NULL ? strlen(data) : 0; + i3_ipc_header_t hdr = { + .magic = I3_IPC_MAGIC, + .size = size, + .type = cmd + }; + + if (write(sock, &hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) + return false; + + if (data != NULL) { + if (write(sock, data, size) != (ssize_t)size) + return false; + } + + return true; +} diff --git a/modules/i3-common.h b/modules/i3-common.h index 91ac777..51e837c 100644 --- a/modules/i3-common.h +++ b/modules/i3-common.h @@ -7,3 +7,4 @@ #include bool i3_get_socket_address(struct sockaddr_un *addr); +bool i3_send_pkg(int sock, int cmd, char *data); diff --git a/modules/i3.c b/modules/i3.c index c7cebb2..98e0f82 100644 --- a/modules/i3.c +++ b/modules/i3.c @@ -178,26 +178,6 @@ workspace_lookup(struct private *m, const char *name) return NULL; } -static bool -send_pkg(int sock, int cmd, char *data) -{ - size_t size = data != NULL ? strlen(data) : 0; - i3_ipc_header_t hdr = { - .magic = I3_IPC_MAGIC, - .size = size, - .type = cmd - }; - - if (write(sock, &hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) - return false; - - if (data != NULL) { - if (write(sock, data, size) != (ssize_t)size) - return false; - } - - return true; -} static bool handle_get_version_reply(struct private *m, const struct json_object *json) @@ -495,9 +475,9 @@ run(struct module *mod) return 1; } - send_pkg(sock, I3_IPC_MESSAGE_TYPE_GET_VERSION, NULL); - send_pkg(sock, I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[\"workspace\", \"window\"]"); - send_pkg(sock, I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL); + i3_send_pkg(sock, I3_IPC_MESSAGE_TYPE_GET_VERSION, NULL); + i3_send_pkg(sock, I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[\"workspace\", \"window\"]"); + i3_send_pkg(sock, I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL); /* Initial reply typically requires a couple of KB. But we often * need more later. For example, switching workspaces can result