2011-10-02 19:33:10 +02:00
|
|
|
|
/*
|
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
|
*
|
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2013-01-11 19:09:41 +01:00
|
|
|
|
* © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
|
2011-10-02 19:33:10 +02:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <err.h>
|
2013-01-09 18:11:03 +01:00
|
|
|
|
#include <errno.h>
|
2011-10-02 19:33:10 +02:00
|
|
|
|
|
|
|
|
|
#include <i3/ipc.h>
|
|
|
|
|
|
2011-10-23 18:38:21 +02:00
|
|
|
|
#include "libi3.h"
|
|
|
|
|
|
2011-10-02 19:33:10 +02:00
|
|
|
|
/*
|
|
|
|
|
* Formats a message (payload) of the given size and type and sends it to i3 via
|
|
|
|
|
* the given socket file descriptor.
|
|
|
|
|
*
|
|
|
|
|
* Returns -1 when write() fails, errno will remain.
|
|
|
|
|
* Returns 0 on success.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2013-01-11 19:09:41 +01:00
|
|
|
|
int ipc_send_message(int sockfd, const uint32_t message_size,
|
|
|
|
|
const uint32_t message_type, const uint8_t *payload) {
|
|
|
|
|
const i3_ipc_header_t header = {
|
|
|
|
|
/* We don’t use I3_IPC_MAGIC because it’s a 0-terminated C string. */
|
|
|
|
|
.magic = { 'i', '3', '-', 'i', 'p', 'c' },
|
|
|
|
|
.size = message_size,
|
|
|
|
|
.type = message_type
|
|
|
|
|
};
|
2011-10-02 19:33:10 +02:00
|
|
|
|
|
2013-12-25 20:01:37 +01:00
|
|
|
|
size_t sent_bytes = 0;
|
2013-01-11 19:09:41 +01:00
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
|
|
/* This first loop is basically unnecessary. No operating system has
|
|
|
|
|
* buffers which cannot fit 14 bytes into them, so the write() will only be
|
|
|
|
|
* called once. */
|
|
|
|
|
while (sent_bytes < sizeof(i3_ipc_header_t)) {
|
|
|
|
|
if ((n = write(sockfd, ((void*)&header) + sent_bytes, sizeof(i3_ipc_header_t) - sent_bytes)) == -1) {
|
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
|
continue;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sent_bytes += n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sent_bytes = 0;
|
|
|
|
|
|
|
|
|
|
while (sent_bytes < message_size) {
|
|
|
|
|
if ((n = write(sockfd, payload + sent_bytes, message_size - sent_bytes)) == -1) {
|
2013-01-09 18:11:03 +01:00
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
|
continue;
|
2011-10-02 19:33:10 +02:00
|
|
|
|
return -1;
|
2013-01-09 18:11:03 +01:00
|
|
|
|
}
|
2011-10-02 19:33:10 +02:00
|
|
|
|
|
|
|
|
|
sent_bytes += n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|