module/i3: break out send_pkg()

This commit is contained in:
Daniel Eklöf 2019-02-13 22:00:13 +01:00
parent 76dc4f82cd
commit 92319714c7
3 changed files with 28 additions and 23 deletions

View file

@ -1,6 +1,7 @@
#include "i3-common.h"
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#if defined(ENABLE_X11)
@ -8,6 +9,8 @@
#include <xcb/xcb_aux.h>
#endif
#include <i3/ipc.h>
#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;
}

View file

@ -7,3 +7,4 @@
#include <sys/un.h>
bool i3_get_socket_address(struct sockaddr_un *addr);
bool i3_send_pkg(int sock, int cmd, char *data);

View file

@ -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