4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
14 #include <sys/socket.h>
20 * Formats a message (payload) of the given size and type and sends it to i3 via
21 * the given socket file descriptor.
24 void ipc_send_message(int sockfd, uint32_t message_size,
25 uint32_t message_type, uint8_t *payload) {
26 int buffer_size = strlen("i3-ipc") + sizeof(uint32_t) + sizeof(uint32_t) + message_size;
27 char msg[buffer_size];
30 strcpy(walk, "i3-ipc");
31 walk += strlen("i3-ipc");
32 memcpy(walk, &message_size, sizeof(uint32_t));
33 walk += sizeof(uint32_t);
34 memcpy(walk, &message_type, sizeof(uint32_t));
35 walk += sizeof(uint32_t);
36 memcpy(walk, payload, message_size);
39 int bytes_to_go = buffer_size;
40 while (sent_bytes < bytes_to_go) {
41 int n = write(sockfd, msg + sent_bytes, bytes_to_go);
43 err(EXIT_FAILURE, "write() failed");
51 * Connects to the i3 IPC socket and returns the file descriptor for the
52 * socket. die()s if anything goes wrong.
55 int connect_ipc(char *socket_path) {
56 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
58 err(EXIT_FAILURE, "Could not create socket");
60 struct sockaddr_un addr;
61 memset(&addr, 0, sizeof(struct sockaddr_un));
62 addr.sun_family = AF_LOCAL;
63 strcpy(addr.sun_path, socket_path);
64 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
65 err(EXIT_FAILURE, "Could not connect to i3");