]> git.sur5r.net Git - i3/i3/blobdiff - src/ipc.c
simplify yajl related code
[i3/i3] / src / ipc.c
index 60ce814540d6c7a3c61ac3969b1a652770226c06..17c15b9e91e723a128794e218694b8196e3475e9 100644 (file)
--- a/src/ipc.c
+++ b/src/ipc.c
@@ -1,3 +1,5 @@
+#undef I3__FILE__
+#define I3__FILE__ "ipc.c"
 /*
  * vim:ts=4:sw=4:expandtab
  *
@@ -8,6 +10,7 @@
  *
  */
 #include "all.h"
+#include "yajl_utils.h"
 
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <ev.h>
 #include <yajl/yajl_gen.h>
 #include <yajl/yajl_parse.h>
-#include <yajl/yajl_version.h>
 
 char *current_socketpath = NULL;
 
-/* Shorter names for all those yajl_gen_* functions */
-#define y(x, ...) yajl_gen_ ## x (gen, ##__VA_ARGS__)
-#define ystr(str) yajl_gen_string(gen, (unsigned char*)str, strlen(str))
-
 TAILQ_HEAD(ipc_client_head, ipc_client) all_clients = TAILQ_HEAD_INITIALIZER(all_clients);
 
 /*
@@ -50,7 +48,7 @@ static bool mkdirp(const char *path) {
         ELOG("mkdir(%s) failed: %s\n", path, strerror(errno));
         return false;
     }
-    char *copy = strdup(path);
+    char *copy = sstrdup(path);
     /* strip trailing slashes, if any */
     while (copy[strlen(copy)-1] == '/')
         copy[strlen(copy)-1] = '\0';
@@ -126,11 +124,7 @@ IPC_HANDLER(command) {
         tree_render();
 
     const unsigned char *reply;
-#if YAJL_MAJOR >= 2
-    size_t length;
-#else
-    unsigned int length;
-#endif
+    ylength length;
     yajl_gen_get_buf(command_output->json_gen, &reply, &length);
 
     ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_COMMAND,
@@ -161,17 +155,14 @@ void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) {
     ystr("type");
     y(integer, con->type);
 
+    /* provided for backwards compatibility only. */
     ystr("orientation");
-    switch (con->orientation) {
-        case NO_ORIENTATION:
-            ystr("none");
-            break;
-        case HORIZ:
+    if (!con_is_split(con))
+        ystr("none");
+    else {
+        if (con_orientation(con) == HORIZ)
             ystr("horizontal");
-            break;
-        case VERT:
-            ystr("vertical");
-            break;
+        else ystr("vertical");
     }
 
     ystr("scratchpad_state");
@@ -206,7 +197,14 @@ void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) {
     ystr("layout");
     switch (con->layout) {
         case L_DEFAULT:
-            ystr("default");
+            DLOG("About to dump layout=default, this is a bug in the code.\n");
+            assert(false);
+            break;
+        case L_SPLITV:
+            ystr("splitv");
+            break;
+        case L_SPLITH:
+            ystr("splith");
             break;
         case L_STACKED:
             ystr("stacked");
@@ -222,6 +220,33 @@ void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) {
             break;
     }
 
+    ystr("workspace_layout");
+    switch (con->workspace_layout) {
+        case L_DEFAULT:
+            ystr("default");
+            break;
+        case L_STACKED:
+            ystr("stacked");
+            break;
+        case L_TABBED:
+            ystr("tabbed");
+            break;
+        default:
+            DLOG("About to dump workspace_layout=%d (none of default/stacked/tabbed), this is a bug.\n", con->workspace_layout);
+            assert(false);
+            break;
+    }
+
+    ystr("last_split_layout");
+    switch (con->layout) {
+        case L_SPLITV:
+            ystr("splitv");
+            break;
+        default:
+            ystr("splith");
+            break;
+    }
+
     ystr("border");
     switch (con->border_style) {
         case BS_NORMAL:
@@ -230,18 +255,21 @@ void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) {
         case BS_NONE:
             ystr("none");
             break;
-        case BS_1PIXEL:
-            ystr("1pixel");
+        case BS_PIXEL:
+            ystr("pixel");
             break;
     }
 
+    ystr("current_border_width");
+    y(integer, con->current_border_width);
+
     dump_rect(gen, "rect", con->rect);
     dump_rect(gen, "window_rect", con->window_rect);
     dump_rect(gen, "geometry", con->geometry);
 
     ystr("name");
-    if (con->window && con->window->name_json)
-        ystr(con->window->name_json);
+    if (con->window && con->window->name)
+        ystr(i3string_as_utf8(con->window->name));
     else
         ystr(con->name);
 
@@ -331,20 +359,12 @@ void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) {
 
 IPC_HANDLER(tree) {
     setlocale(LC_NUMERIC, "C");
-#if YAJL_MAJOR >= 2
-    yajl_gen gen = yajl_gen_alloc(NULL);
-#else
-    yajl_gen gen = yajl_gen_alloc(NULL, NULL);
-#endif
+    yajl_gen gen = ygenalloc();
     dump_node(gen, croot, false);
     setlocale(LC_NUMERIC, "");
 
     const unsigned char *payload;
-#if YAJL_MAJOR >= 2
-    size_t length;
-#else
-    unsigned int length;
-#endif
+    ylength length;
     y(get_buf, &payload, &length);
 
     ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_TREE, payload);
@@ -358,18 +378,14 @@ IPC_HANDLER(tree) {
  *
  */
 IPC_HANDLER(get_workspaces) {
-#if YAJL_MAJOR >= 2
-    yajl_gen gen = yajl_gen_alloc(NULL);
-#else
-    yajl_gen gen = yajl_gen_alloc(NULL, NULL);
-#endif
+    yajl_gen gen = ygenalloc();
     y(array_open);
 
     Con *focused_ws = con_get_workspace(focused);
 
     Con *output;
     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
-        if (output->name[0] == '_' && output->name[1] == '_')
+        if (con_is_internal(output))
             continue;
         Con *ws;
         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
@@ -415,11 +431,7 @@ IPC_HANDLER(get_workspaces) {
     y(array_close);
 
     const unsigned char *payload;
-#if YAJL_MAJOR >= 2
-    size_t length;
-#else
-    unsigned int length;
-#endif
+    ylength length;
     y(get_buf, &payload, &length);
 
     ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_WORKSPACES, payload);
@@ -432,11 +444,7 @@ IPC_HANDLER(get_workspaces) {
  *
  */
 IPC_HANDLER(get_outputs) {
-#if YAJL_MAJOR >= 2
-    yajl_gen gen = yajl_gen_alloc(NULL);
-#else
-    yajl_gen gen = yajl_gen_alloc(NULL, NULL);
-#endif
+    yajl_gen gen = ygenalloc();
     y(array_open);
 
     Output *output;
@@ -476,11 +484,7 @@ IPC_HANDLER(get_outputs) {
     y(array_close);
 
     const unsigned char *payload;
-#if YAJL_MAJOR >= 2
-    size_t length;
-#else
-    unsigned int length;
-#endif
+    ylength length;
     y(get_buf, &payload, &length);
 
     ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_OUTPUTS, payload);
@@ -493,11 +497,7 @@ IPC_HANDLER(get_outputs) {
  *
  */
 IPC_HANDLER(get_marks) {
-#if YAJL_MAJOR >= 2
-    yajl_gen gen = yajl_gen_alloc(NULL);
-#else
-    yajl_gen gen = yajl_gen_alloc(NULL, NULL);
-#endif
+    yajl_gen gen = ygenalloc();
     y(array_open);
 
     Con *con;
@@ -508,28 +508,50 @@ IPC_HANDLER(get_marks) {
     y(array_close);
 
     const unsigned char *payload;
-#if YAJL_MAJOR >= 2
-    size_t length;
-#else
-    unsigned int length;
-#endif
+    ylength length;
     y(get_buf, &payload, &length);
 
     ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_MARKS, payload);
     y(free);
 }
 
+/*
+ * Returns the version of i3
+ *
+ */
+IPC_HANDLER(get_version) {
+    yajl_gen gen = ygenalloc();
+    y(map_open);
+
+    ystr("major");
+    y(integer, MAJOR_VERSION);
+
+    ystr("minor");
+    y(integer, MINOR_VERSION);
+
+    ystr("patch");
+    y(integer, PATCH_VERSION);
+
+    ystr("human_readable");
+    ystr(I3_VERSION);
+
+    y(map_close);
+
+    const unsigned char *payload;
+    ylength length;
+    y(get_buf, &payload, &length);
+
+    ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_VERSION, 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
+    yajl_gen gen = ygenalloc();
 
     /* If no ID was passed, we return a JSON array with all IDs */
     if (message_size == 0) {
@@ -541,11 +563,7 @@ IPC_HANDLER(get_bar_config) {
         y(array_close);
 
         const unsigned char *payload;
-#if YAJL_MAJOR >= 2
-        size_t length;
-#else
-        unsigned int length;
-#endif
+        ylength length;
         y(get_buf, &payload, &length);
 
         ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
@@ -679,11 +697,7 @@ IPC_HANDLER(get_bar_config) {
     y(map_close);
 
     const unsigned char *payload;
-#if YAJL_MAJOR >= 2
-    size_t length;
-#else
-    unsigned int length;
-#endif
+    ylength length;
     y(get_buf, &payload, &length);
 
     ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
@@ -694,16 +708,11 @@ IPC_HANDLER(get_bar_config) {
  * Callback for the YAJL parser (will be called when a string is parsed).
  *
  */
-#if YAJL_MAJOR < 2
 static int add_subscription(void *extra, const unsigned char *s,
-                            unsigned int len) {
-#else
-static int add_subscription(void *extra, const unsigned char *s,
-                            size_t len) {
-#endif
+                            ylength len) {
     ipc_client *client = extra;
 
-    DLOG("should add subscription to extra %p, sub %.*s\n", client, len, s);
+    DLOG("should add subscription to extra %p, sub %.*s\n", client, (int)len, s);
     int event = client->num_events;
 
     client->num_events++;
@@ -750,11 +759,7 @@ IPC_HANDLER(subscribe) {
     memset(&callbacks, 0, sizeof(yajl_callbacks));
     callbacks.yajl_string = add_subscription;
 
-#if YAJL_MAJOR >= 2
-    p = yajl_alloc(&callbacks, NULL, (void*)client);
-#else
-    p = yajl_alloc(&callbacks, NULL, NULL, (void*)client);
-#endif
+    p = yalloc(&callbacks, (void*)client);
     stat = yajl_parse(p, (const unsigned char*)message, message_size);
     if (stat != yajl_status_ok) {
         unsigned char *err;
@@ -775,7 +780,7 @@ 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,
@@ -783,6 +788,7 @@ handler_t handlers[7] = {
     handle_tree,
     handle_get_marks,
     handle_get_bar_config,
+    handle_get_version,
 };
 
 /*