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;
}