]> git.sur5r.net Git - i3/i3/blob - libi3/ipc_send_message.c
Merge branch 'fix-move-ws'
[i3/i3] / libi3 / ipc_send_message.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #include <string.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdint.h>
12 #include <err.h>
13 #include <errno.h>
14
15 #include <i3/ipc.h>
16
17 #include "libi3.h"
18
19 /*
20  * Formats a message (payload) of the given size and type and sends it to i3 via
21  * the given socket file descriptor.
22  *
23  * Returns -1 when write() fails, errno will remain.
24  * Returns 0 on success.
25  *
26  */
27 int ipc_send_message(int sockfd, uint32_t message_size,
28                      uint32_t message_type, const uint8_t *payload) {
29     int buffer_size = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t) + message_size;
30     char msg[buffer_size];
31     char *walk = msg;
32
33     strncpy(walk, I3_IPC_MAGIC, buffer_size - 1);
34     walk += strlen(I3_IPC_MAGIC);
35     memcpy(walk, &message_size, sizeof(uint32_t));
36     walk += sizeof(uint32_t);
37     memcpy(walk, &message_type, sizeof(uint32_t));
38     walk += sizeof(uint32_t);
39     memcpy(walk, payload, message_size);
40
41     int sent_bytes = 0;
42     while (sent_bytes < buffer_size) {
43         int n = write(sockfd, msg + sent_bytes, buffer_size - sent_bytes);
44         if (n == -1) {
45             if (errno == EAGAIN)
46                 continue;
47             return -1;
48         }
49
50         sent_bytes += n;
51     }
52
53     return 0;
54 }