]> git.sur5r.net Git - i3/i3/blobdiff - src/ipc.c
Merge branch 'master' into next
[i3/i3] / src / ipc.c
index 1ae6bb0a032adb694c7088768113e26dc46c4a5b..5143695bc29bfeb7da4a58da5e7f39aecdf4e734 100644 (file)
--- a/src/ipc.c
+++ b/src/ipc.c
@@ -2,14 +2,13 @@
  * 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>
@@ -19,8 +18,6 @@
 #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 */
@@ -102,9 +99,12 @@ void ipc_send_event(const char *event, uint32_t message_type, const char *payloa
  */
 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);
     }
 }
 
@@ -166,6 +166,19 @@ void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) {
             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);
@@ -330,6 +343,8 @@ IPC_HANDLER(get_workspaces) {
 
     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);
@@ -481,6 +496,34 @@ IPC_HANDLER(get_marks) {
  *
  */
 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);
@@ -495,12 +538,6 @@ IPC_HANDLER(get_bar_config) {
         break;
     }
 
-#if YAJL_MAJOR >= 2
-    yajl_gen gen = yajl_gen_alloc(NULL);
-#else
-    yajl_gen gen = yajl_gen_alloc(NULL, NULL);
-#endif
-
     y(map_open);
 
     if (!config) {
@@ -571,6 +608,7 @@ IPC_HANDLER(get_bar_config) {
         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
     }
@@ -589,6 +627,48 @@ IPC_HANDLER(get_bar_config) {
     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, length, I3_IPC_REPLY_TYPE_LOG_MARKERS, payload);
+    y(free);
+}
+
 /*
  * Callback for the YAJL parser (will be called when a string is parsed).
  *
@@ -674,14 +754,15 @@ IPC_HANDLER(subscribe) {
 
 /* The index of each callback function corresponds to the numeric
  * value of the message type (see include/i3/ipc.h) */
-handler_t handlers[7] = {
+handler_t handlers[8] = {
     handle_command,
     handle_get_workspaces,
     handle_subscribe,
     handle_get_outputs,
     handle_tree,
     handle_get_marks,
-    handle_get_bar_config
+    handle_get_bar_config,
+    handle_get_log_markers
 };
 
 /*
@@ -723,10 +804,12 @@ static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
             /* 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;
@@ -798,6 +881,9 @@ void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
         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));