]> git.sur5r.net Git - i3/i3/blobdiff - i3bar/src/workspaces.c
Merge branch 'master' into next
[i3/i3] / i3bar / src / workspaces.c
index 0393071e6425b09d3dfa8c02c2d09c1ab941d712..5df1899f33a408f4a8429a52acaa3a2b060c8f7a 100644 (file)
@@ -1,11 +1,10 @@
 /*
- * i3bar - an xcb-based status- and ws-bar for i3
- *
- * © 2010 Axel Wagner and contributors
+ * vim:ts=4:sw=4:expandtab
  *
- * See file LICNSE for license information
+ * i3bar - an xcb-based status- and ws-bar for i3
+ * © 2010-2011 Axel Wagner and contributors (see also: LICENSE)
  *
- * src/workspaces.c: Maintaining the workspace-lists
+ * workspaces.c: Maintaining the workspace-lists
  *
  */
 #include <string.h>
@@ -13,6 +12,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <yajl/yajl_parse.h>
+#include <yajl/yajl_version.h>
 
 #include "common.h"
 
@@ -25,10 +25,10 @@ struct workspaces_json_params {
 };
 
 /*
- * Parse a booleant-value (visible, focused, urgent)
+ * Parse a boolean value (visible, focused, urgent)
  *
  */
-static int workspaces_boolean_cb(void *params_, bool val) {
+static int workspaces_boolean_cb(void *params_, int val) {
     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
 
     if (!strcmp(params->cur_key, "visible")) {
@@ -58,7 +58,11 @@ static int workspaces_boolean_cb(void *params_, bool val) {
  * Parse an integer (num or the rect)
  *
  */
+#if YAJL_MAJOR >= 2
+static int workspaces_integer_cb(void *params_, long long val) {
+#else
 static int workspaces_integer_cb(void *params_, long val) {
+#endif
     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
 
     if (!strcmp(params->cur_key, "num")) {
@@ -99,26 +103,29 @@ static int workspaces_integer_cb(void *params_, long val) {
  * Parse a string (name, output)
  *
  */
+#if YAJL_MAJOR >= 2
+static int workspaces_string_cb(void *params_, const unsigned char *val, size_t len) {
+#else
 static int workspaces_string_cb(void *params_, const unsigned char *val, unsigned int len) {
-
+#endif
         struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
 
         char *output_name;
 
         if (!strcmp(params->cur_key, "name")) {
             /* Save the name */
-            params->workspaces_walk->name = malloc(sizeof(const unsigned char) * (len + 1));
+            params->workspaces_walk->name = smalloc(sizeof(const unsigned char) * (len + 1));
             strncpy(params->workspaces_walk->name, (const char*) val, len);
             params->workspaces_walk->name[len] = '\0';
 
-            /* Convert the name to ucs2, save it's length in glyphs and calculate it'srendered width */
-            int ucs2_len;
+            /* Convert the name to ucs2, save its length in glyphs and calculate its rendered width */
+            size_t ucs2_len;
             xcb_char2b_t *ucs2_name = (xcb_char2b_t*) convert_utf8_to_ucs2(params->workspaces_walk->name, &ucs2_len);
             params->workspaces_walk->ucs2_name = ucs2_name;
             params->workspaces_walk->name_glyphs = ucs2_len;
             params->workspaces_walk->name_width =
-                predict_text_extents(params->workspaces_walk->ucs2_name,
-                params->workspaces_walk->name_glyphs);
+                predict_text_width((char *)params->workspaces_walk->ucs2_name,
+                params->workspaces_walk->name_glyphs, true);
 
             DLOG("Got Workspace %s, name_width: %d, glyphs: %d\n",
                  params->workspaces_walk->name,
@@ -131,14 +138,17 @@ static int workspaces_string_cb(void *params_, const unsigned char *val, unsigne
 
         if (!strcmp(params->cur_key, "output")) {
             /* We add the ws to the TAILQ of the output, it belongs to */
-            output_name = malloc(sizeof(const unsigned char) * (len + 1));
+            output_name = smalloc(sizeof(const unsigned char) * (len + 1));
             strncpy(output_name, (const char*) val, len);
             output_name[len] = '\0';
-            params->workspaces_walk->output = get_output_by_name(output_name);
+            i3_output *target = get_output_by_name(output_name);
+            if (target) {
+                params->workspaces_walk->output = target;
 
-            TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
-                              params->workspaces_walk,
-                              tailq);
+                TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
+                                  params->workspaces_walk,
+                                  tailq);
+            }
 
             FREE(output_name);
             return 1;
@@ -157,7 +167,7 @@ static int workspaces_start_map_cb(void *params_) {
     i3_ws *new_workspace = NULL;
 
     if (params->cur_key == NULL) {
-        new_workspace = malloc(sizeof(i3_ws));
+        new_workspace = smalloc(sizeof(i3_ws));
         new_workspace->num = -1;
         new_workspace->name = NULL;
         new_workspace->visible = 0;
@@ -179,15 +189,15 @@ static int workspaces_start_map_cb(void *params_) {
  * Essentially we just save it in the parsing-state
  *
  */
+#if YAJL_MAJOR >= 2
+static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
+#else
 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, unsigned int keyLen) {
+#endif
     struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
     FREE(params->cur_key);
 
-    params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
-    if (params->cur_key == NULL) {
-        ELOG("Could not allocate memory: %s\n", strerror(errno));
-        exit(EXIT_FAILURE);
-    }
+    params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
     strncpy(params->cur_key, (const char*) keyVal, keyLen);
     params->cur_key[keyLen] = '\0';
 
@@ -225,10 +235,14 @@ void parse_workspaces_json(char *json) {
     params.json = json;
 
     yajl_handle handle;
-    yajl_parser_config parse_conf = { 0, 0 };
     yajl_status state;
+#if YAJL_MAJOR < 2
+    yajl_parser_config parse_conf = { 0, 0 };
 
     handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) &params);
+#else
+    handle = yajl_alloc(&workspaces_callbacks, NULL, (void*) &params);
+#endif
 
     state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
 
@@ -237,7 +251,9 @@ void parse_workspaces_json(char *json) {
         case yajl_status_ok:
             break;
         case yajl_status_client_canceled:
+#if YAJL_MAJOR < 2
         case yajl_status_insufficient_data:
+#endif
         case yajl_status_error:
             ELOG("Could not parse workspaces-reply!\n");
             exit(EXIT_FAILURE);
@@ -250,7 +266,7 @@ void parse_workspaces_json(char *json) {
 }
 
 /*
- * free() all workspace data-structures
+ * free() all workspace data-structures. Does not free() the heads of the tailqueues.
  *
  */
 void free_workspaces() {