module/i3: break out I3 IPC receive loop

This commit is contained in:
Daniel Eklöf 2019-02-15 18:58:21 +01:00
parent 6df68f1c23
commit fce37e86e4
3 changed files with 283 additions and 163 deletions

View file

@ -4,12 +4,15 @@
#include <unistd.h>
#include <assert.h>
#include <poll.h>
#if defined(ENABLE_X11)
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#endif
#include <i3/ipc.h>
#include <json-c/json_tokener.h>
#define LOG_MODULE "i3:common"
#include "../log.h"
@ -112,3 +115,189 @@ i3_send_pkg(int sock, int cmd, char *data)
return true;
}
bool
i3_receive_loop(int abort_fd, int sock,
const struct i3_ipc_callbacks *cbs, void *data)
{
/* Initial reply typically requires a couple of KB. But we often
* need more later. For example, switching workspaces can result
* in quite big notification messages. */
size_t reply_buf_size = 4096;
char *buf = malloc(reply_buf_size);
size_t buf_idx = 0;
bool err = false;
while (!err) {
struct pollfd fds[] = {
{.fd = abort_fd, .events = POLLIN},
{.fd = sock, .events = POLLIN}
};
int res = poll(fds, 2, -1);
if (res <= 0) {
LOG_ERRNO("failed to poll()");
err = true;
break;
}
if (fds[0].revents & POLLIN)
break;
if (fds[1].revents & POLLHUP) {
LOG_WARN("disconnected");
break;
}
assert(fds[1].revents & POLLIN);
/* Grow receive buffer, if necessary */
if (buf_idx == reply_buf_size) {
LOG_DBG("growing reply buffer: %zu -> %zu",
reply_buf_size, reply_buf_size * 2);
char *new_buf = realloc(buf, reply_buf_size * 2);
if (new_buf == NULL) {
LOG_ERR("failed to grow reply buffer from %zu to %zu bytes",
reply_buf_size, reply_buf_size * 2);
err = true;
break;
}
buf = new_buf;
reply_buf_size *= 2;
}
assert(reply_buf_size > buf_idx);
ssize_t bytes = read(sock, &buf[buf_idx], reply_buf_size - buf_idx);
if (bytes < 0) {
LOG_ERRNO("failed to read from i3's socket");
err = true;
break;
}
buf_idx += bytes;
while (!err && buf_idx >= sizeof(i3_ipc_header_t)) {
const i3_ipc_header_t *hdr = (const i3_ipc_header_t *)buf;
if (strncmp(hdr->magic, I3_IPC_MAGIC, sizeof(hdr->magic)) != 0) {
LOG_ERR(
"i3 IPC header magic mismatch: expected \"%.*s\", got \"%.*s\"",
(int)sizeof(hdr->magic), I3_IPC_MAGIC,
(int)sizeof(hdr->magic), hdr->magic);
err = true;
break;
}
size_t total_size = sizeof(i3_ipc_header_t) + hdr->size;
if (total_size > buf_idx) {
LOG_DBG("got %zd bytes, need %zu", bytes, total_size);
break;
}
/* Json-c expects a NULL-terminated string */
char json_str[hdr->size + 1];
memcpy(json_str, &buf[sizeof(*hdr)], hdr->size);
json_str[hdr->size] = '\0';
//printf("raw: %s\n", json_str);
LOG_DBG("raw: %s\n", json_str);
//json_tokener *tokener = json_tokener_new();
struct json_object *json = json_tokener_parse(json_str);
if (json == NULL) {
LOG_ERR("failed to parse json");
err = true;
break;
}
//err = pkt_handler(hdr, json, data);
i3_ipc_callback_t pkt_handler = NULL;
switch (hdr->type) {
case I3_IPC_REPLY_TYPE_COMMAND:
pkt_handler = cbs->reply_command;
break;
case I3_IPC_REPLY_TYPE_WORKSPACES:
pkt_handler = cbs->reply_workspaces;
break;
case I3_IPC_REPLY_TYPE_SUBSCRIBE:
pkt_handler = cbs->reply_subscribe;
break;
case I3_IPC_REPLY_TYPE_OUTPUTS:
pkt_handler = cbs->reply_outputs;
break;
case I3_IPC_REPLY_TYPE_TREE:
pkt_handler = cbs->reply_tree;
break;
case I3_IPC_REPLY_TYPE_MARKS:
pkt_handler = cbs->reply_marks;
break;
case I3_IPC_REPLY_TYPE_BAR_CONFIG:
pkt_handler = cbs->reply_bar_config;
break;
case I3_IPC_REPLY_TYPE_VERSION:
pkt_handler = cbs->reply_version;
break;
case I3_IPC_REPLY_TYPE_BINDING_MODES:
pkt_handler = cbs->reply_binding_modes;
break;
case I3_IPC_REPLY_TYPE_CONFIG:
pkt_handler = cbs->reply_config;
break;
case I3_IPC_REPLY_TYPE_TICK:
pkt_handler = cbs->reply_tick;
break;
case I3_IPC_REPLY_TYPE_SYNC:
pkt_handler = cbs->reply_sync;
break;
case I3_IPC_EVENT_WORKSPACE:
pkt_handler = cbs->event_workspace;
break;
case I3_IPC_EVENT_OUTPUT:
pkt_handler = cbs->event_output;
break;
case I3_IPC_EVENT_MODE:
pkt_handler = cbs->event_mode;
break;
case I3_IPC_EVENT_WINDOW:
pkt_handler = cbs->event_window;
break;
case I3_IPC_EVENT_BARCONFIG_UPDATE:
pkt_handler = cbs->event_barconfig_update;
break;
case I3_IPC_EVENT_BINDING:
pkt_handler = cbs->event_binding;
break;
case I3_IPC_EVENT_SHUTDOWN:
pkt_handler = cbs->event_shutdown;
break;
case I3_IPC_EVENT_TICK:
pkt_handler = cbs->event_tick;
break;
default:
LOG_ERR("unimplemented IPC reply type: %d", hdr->type);
pkt_handler = NULL;
break;
}
if (pkt_handler != NULL)
err = !pkt_handler(hdr->type, json, data);
else
LOG_DBG("no handler for reply/event %d; ignoring", hdr->type);
json_object_put(json);
assert(total_size <= buf_idx);
memmove(buf, &buf[total_size], buf_idx - total_size);
buf_idx -= total_size;
}
}
free(buf);
return !err;
}