* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
+ * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
*
- * © 2009-2011 Michael Stapelberg and contributors
- *
- * See file LICENSE for license information.
- *
- * ipc.c: Everything about the UNIX domain sockets for IPC
+ * ipc.c: UNIX domain socket IPC (initialization, client handling, protocol).
*
*/
+#include "all.h"
+
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <yajl/yajl_parse.h>
#include <yajl/yajl_version.h>
-#include "all.h"
-
char *current_socketpath = NULL;
/* Shorter names for all those yajl_gen_* functions */
copy[strlen(copy)-1] = '\0';
char *sep = strrchr(copy, '/');
- if (sep == NULL)
+ if (sep == NULL) {
+ FREE(copy);
return false;
+ }
*sep = '\0';
bool result = false;
if (mkdirp(copy))
return result;
}
-static void ipc_send_message(int fd, const unsigned char *payload,
- int message_type, int message_size) {
- int buffer_size = strlen("i3-ipc") + sizeof(uint32_t) +
- sizeof(uint32_t) + message_size;
- char msg[buffer_size];
- char *walk = msg;
-
- strncpy(walk, "i3-ipc", buffer_size - 1);
- walk += strlen("i3-ipc");
- memcpy(walk, &message_size, sizeof(uint32_t));
- walk += sizeof(uint32_t);
- memcpy(walk, &message_type, sizeof(uint32_t));
- walk += sizeof(uint32_t);
- memcpy(walk, payload, message_size);
-
- int sent_bytes = 0;
- int bytes_to_go = buffer_size;
- while (sent_bytes < bytes_to_go) {
- int n = write(fd, msg + sent_bytes, bytes_to_go);
- if (n == -1) {
- DLOG("write() failed: %s\n", strerror(errno));
- return;
- }
-
- sent_bytes += n;
- bytes_to_go -= n;
- }
-}
-
/*
* Sends the specified event to all IPC clients which are currently connected
* and subscribed to this kind of event.
if (!interested)
continue;
- ipc_send_message(current->fd, (const unsigned char*)payload,
- message_type, strlen(payload));
+ ipc_send_message(current->fd, strlen(payload), message_type, (const uint8_t*)payload);
}
}
*/
void ipc_shutdown() {
ipc_client *current;
- TAILQ_FOREACH(current, &all_clients, clients) {
+ while (!TAILQ_EMPTY(&all_clients)) {
+ current = TAILQ_FIRST(&all_clients);
shutdown(current->fd, SHUT_RDWR);
close(current->fd);
+ TAILQ_REMOVE(&all_clients, current, clients);
+ free(current);
}
}
/* If no reply was provided, we just use the default success message */
if (reply == NULL)
reply = "{\"success\":true}";
- ipc_send_message(fd, (const unsigned char*)reply,
- I3_IPC_REPLY_TYPE_COMMAND, strlen(reply));
+ ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_COMMAND, (const uint8_t*)reply);
FREE(save_reply);
}
break;
}
+ ystr("scratchpad_state");
+ switch (con->scratchpad_state) {
+ case SCRATCHPAD_NONE:
+ ystr("none");
+ break;
+ case SCRATCHPAD_FRESH:
+ ystr("fresh");
+ break;
+ case SCRATCHPAD_CHANGED:
+ ystr("changed");
+ break;
+ }
+
ystr("percent");
if (con->percent == 0.0)
y(null);
dump_rect(gen, "geometry", con->geometry);
ystr("name");
- ystr(con->name);
+ if (con->window && con->window->name_json)
+ ystr(con->window->name_json);
+ else
+ ystr(con->name);
if (con->type == CT_WORKSPACE) {
ystr("num");
ystr("focus");
y(array_open);
- TAILQ_FOREACH(node, &(con->focus_head), nodes) {
+ TAILQ_FOREACH(node, &(con->focus_head), focused) {
y(integer, (long int)node);
}
y(array_close);
#endif
y(get_buf, &payload, &length);
- ipc_send_message(fd, payload, I3_IPC_REPLY_TYPE_TREE, length);
+ ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_TREE, payload);
y(free);
}
Con *output;
TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
+ if (output->name[0] == '_' && output->name[1] == '_')
+ continue;
Con *ws;
TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
assert(ws->type == CT_WORKSPACE);
#endif
y(get_buf, &payload, &length);
- ipc_send_message(fd, payload, I3_IPC_REPLY_TYPE_WORKSPACES, length);
+ ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_WORKSPACES, payload);
y(free);
}
#endif
y(get_buf, &payload, &length);
- ipc_send_message(fd, payload, I3_IPC_REPLY_TYPE_OUTPUTS, length);
+ ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_OUTPUTS, payload);
y(free);
}
y(array_close);
const unsigned char *payload;
+#if YAJL_MAJOR >= 2
+ size_t length;
+#else
+ unsigned int length;
+#endif
+ y(get_buf, &payload, &length);
+
+ ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_MARKS, payload);
+ y(free);
+}
+
+/*
+ * Formats the reply message for a GET_BAR_CONFIG request and sends it to the
+ * client.
+ *
+ */
+IPC_HANDLER(get_bar_config) {
+#if YAJL_MAJOR >= 2
+ yajl_gen gen = yajl_gen_alloc(NULL);
+#else
+ yajl_gen gen = yajl_gen_alloc(NULL, NULL);
+#endif
+
+ /* If no ID was passed, we return a JSON array with all IDs */
+ if (message_size == 0) {
+ y(array_open);
+ Barconfig *current;
+ TAILQ_FOREACH(current, &barconfigs, configs) {
+ ystr(current->id);
+ }
+ y(array_close);
+
+ const unsigned char *payload;
+#if YAJL_MAJOR >= 2
+ size_t length;
+#else
+ unsigned int length;
+#endif
+ y(get_buf, &payload, &length);
+
+ ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
+ y(free);
+ return;
+ }
+
+ /* To get a properly terminated buffer, we copy
+ * message_size bytes out of the buffer */
+ char *bar_id = scalloc(message_size + 1);
+ strncpy(bar_id, (const char*)message, message_size);
+ LOG("IPC: looking for config for bar ID \"%s\"\n", bar_id);
+ Barconfig *current, *config = NULL;
+ TAILQ_FOREACH(current, &barconfigs, configs) {
+ if (strcmp(current->id, bar_id) != 0)
+ continue;
+
+ config = current;
+ break;
+ }
+
+ y(map_open);
+
+ if (!config) {
+ /* If we did not find a config for the given ID, the reply will contain
+ * a null 'id' field. */
+ ystr("id");
+ y(null);
+ } else {
+ ystr("id");
+ ystr(config->id);
+
+ if (config->num_outputs > 0) {
+ ystr("outputs");
+ y(array_open);
+ for (int c = 0; c < config->num_outputs; c++)
+ ystr(config->outputs[c]);
+ y(array_close);
+ }
+
+#define YSTR_IF_SET(name) \
+ do { \
+ if (config->name) { \
+ ystr( # name); \
+ ystr(config->name); \
+ } \
+ } while (0)
+
+ YSTR_IF_SET(tray_output);
+ YSTR_IF_SET(socket_path);
+
+ ystr("mode");
+ if (config->mode == M_HIDE)
+ ystr("hide");
+ else ystr("dock");
+
+ ystr("position");
+ if (config->position == P_BOTTOM)
+ ystr("bottom");
+ else ystr("top");
+
+ YSTR_IF_SET(status_command);
+ YSTR_IF_SET(font);
+
+ ystr("workspace_buttons");
+ y(bool, !config->hide_workspace_buttons);
+
+ ystr("verbose");
+ y(bool, config->verbose);
+
+#undef YSTR_IF_SET
+#define YSTR_IF_SET(name) \
+ do { \
+ if (config->colors.name) { \
+ ystr( # name); \
+ ystr(config->colors.name); \
+ } \
+ } while (0)
+
+ ystr("colors");
+ y(map_open);
+ YSTR_IF_SET(background);
+ YSTR_IF_SET(statusline);
+ YSTR_IF_SET(focused_workspace_text);
+ YSTR_IF_SET(focused_workspace_bg);
+ YSTR_IF_SET(active_workspace_text);
+ YSTR_IF_SET(active_workspace_bg);
+ YSTR_IF_SET(inactive_workspace_text);
+ YSTR_IF_SET(inactive_workspace_bg);
+ YSTR_IF_SET(urgent_workspace_text);
+ YSTR_IF_SET(urgent_workspace_bg);
+ y(map_close);
+
+#undef YSTR_IF_SET
+ }
+
+ y(map_close);
+
+ const unsigned char *payload;
+#if YAJL_MAJOR >= 2
+ size_t length;
+#else
+ unsigned int length;
+#endif
+ y(get_buf, &payload, &length);
+
+ ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
+ y(free);
+}
+
+/*
+ * Formats the reply message for a GET_LOG_MARKERS request and sends it to the
+ * client.
+ *
+ */
+IPC_HANDLER(get_log_markers) {
+#if YAJL_MAJOR >= 2
+ yajl_gen gen = yajl_gen_alloc(NULL);
+#else
+ yajl_gen gen = yajl_gen_alloc(NULL, NULL);
+#endif
+
+ int offset_next_write, offset_last_wrap, logsize;
+ get_log_markers(&offset_next_write, &offset_last_wrap, &logsize);
+
+ y(map_open);
+ ystr("offset_next_write");
+ y(integer, offset_next_write);
+
+ ystr("offset_last_wrap");
+ y(integer, offset_last_wrap);
+
+ ystr("shmname");
+ ystr(shmlogname);
+
+ ystr("size");
+ y(integer, logsize);
+
+ y(map_close);
+
+ const unsigned char *payload;
+#if YAJL_MAJOR >= 2
+ size_t length;
+#else
unsigned int length;
+#endif
y(get_buf, &payload, &length);
- ipc_send_message(fd, payload, I3_IPC_REPLY_TYPE_MARKS, length);
+ ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_LOG_MARKERS, payload);
y(free);
}
yajl_free_error(p, err);
const char *reply = "{\"success\":false}";
- ipc_send_message(fd, (const unsigned char*)reply,
- I3_IPC_REPLY_TYPE_SUBSCRIBE, strlen(reply));
+ ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t*)reply);
yajl_free(p);
return;
}
yajl_free(p);
const char *reply = "{\"success\":true}";
- ipc_send_message(fd, (const unsigned char*)reply,
- I3_IPC_REPLY_TYPE_SUBSCRIBE, strlen(reply));
+ ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t*)reply);
}
/* The index of each callback function corresponds to the numeric
* value of the message type (see include/i3/ipc.h) */
-handler_t handlers[6] = {
+handler_t handlers[8] = {
handle_command,
handle_get_workspaces,
handle_subscribe,
handle_get_outputs,
handle_tree,
- handle_get_marks
+ handle_get_marks,
+ handle_get_bar_config,
+ handle_get_log_markers
};
/*
/* We can call TAILQ_REMOVE because we break out of the
* TAILQ_FOREACH afterwards */
TAILQ_REMOVE(&all_clients, current, clients);
+ free(current);
break;
}
ev_io_stop(EV_A_ w);
+ free(w);
DLOG("IPC: client disconnected\n");
return;
return;
}
+ /* Close this file descriptor on exec() */
+ (void)fcntl(client, F_SETFD, FD_CLOEXEC);
+
set_nonblock(client);
struct ev_io *package = scalloc(sizeof(struct ev_io));