]> git.sur5r.net Git - i3/i3/blobdiff - i3bar/src/workspaces.c
Ensure all *.[ch] files include config.h
[i3/i3] / i3bar / src / workspaces.c
index 91307b0cb43d78cf69c482ced9c55ad88604b3af..233249893ecf10fc96e3d598d9cee64779b0837b 100644 (file)
@@ -2,11 +2,13 @@
  * vim:ts=4:sw=4:expandtab
  *
  * i3bar - an xcb-based status- and ws-bar for i3
- * © 2010-2012 Axel Wagner and contributors (see also: LICENSE)
+ * © 2010 Axel Wagner and contributors (see also: LICENSE)
  *
- * workspaces.c: Maintaining the workspace-lists
+ * workspaces.c: Maintaining the workspace lists
  *
  */
+#include "common.h"
+
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -14,8 +16,6 @@
 #include <yajl/yajl_parse.h>
 #include <yajl/yajl_version.h>
 
-#include "common.h"
-
 /* A datatype to pass through the callbacks to save the state */
 struct workspaces_json_params {
     struct ws_head *workspaces;
@@ -102,11 +102,9 @@ static int workspaces_integer_cb(void *params_, long long val) {
 static int workspaces_string_cb(void *params_, const unsigned char *val, size_t len) {
     struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
 
-    char *output_name;
-
     if (!strcmp(params->cur_key, "name")) {
         const char *ws_name = (const char *)val;
-        params->workspaces_walk->canonical_name = strndup(ws_name, len);
+        params->workspaces_walk->canonical_name = sstrndup(ws_name, len);
 
         if (config.strip_ws_numbers && params->workspaces_walk->num >= 0) {
             /* Special case: strip off the workspace number */
@@ -123,19 +121,19 @@ static int workspaces_string_cb(void *params_, const unsigned char *val, size_t
 
             /* Offset may be equal to length, in which case display the number */
             params->workspaces_walk->name = (offset < len
-                                                 ? i3string_from_utf8_with_length(ws_name + offset, len - offset)
-                                                 : i3string_from_utf8(ws_num));
+                                                 ? i3string_from_markup_with_length(ws_name + offset, len - offset)
+                                                 : i3string_from_markup(ws_num));
 
         } else {
             /* Default case: just save the name */
-            params->workspaces_walk->name = i3string_from_utf8_with_length(ws_name, len);
+            params->workspaces_walk->name = i3string_from_markup_with_length(ws_name, len);
         }
 
         /* Save its rendered width */
         params->workspaces_walk->name_width =
             predict_text_width(params->workspaces_walk->name);
 
-        DLOG("Got Workspace canonical: %s, name: '%s', name_width: %d, glyphs: %zu\n",
+        DLOG("Got workspace canonical: %s, name: '%s', name_width: %d, glyphs: %zu\n",
              params->workspaces_walk->canonical_name,
              i3string_as_utf8(params->workspaces_walk->name),
              params->workspaces_walk->name_width,
@@ -147,11 +145,11 @@ static int workspaces_string_cb(void *params_, const unsigned char *val, size_t
 
     if (!strcmp(params->cur_key, "output")) {
         /* We add the ws to the TAILQ of the output, it belongs to */
-        output_name = smalloc(sizeof(const unsigned char) * (len + 1));
-        strncpy(output_name, (const char *)val, len);
-        output_name[len] = '\0';
+        char *output_name = NULL;
+        sasprintf(&output_name, "%.*s", len, val);
+
         i3_output *target = get_output_by_name(output_name);
-        if (target) {
+        if (target != NULL) {
             params->workspaces_walk->output = target;
 
             TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
@@ -167,7 +165,7 @@ static int workspaces_string_cb(void *params_, const unsigned char *val, size_t
 }
 
 /*
- * We hit the start of a json-map (rect or a new output)
+ * We hit the start of a JSON map (rect or a new output)
  *
  */
 static int workspaces_start_map_cb(void *params_) {
@@ -195,17 +193,13 @@ static int workspaces_start_map_cb(void *params_) {
 /*
  * Parse a key.
  *
- * Essentially we just save it in the parsing-state
+ * Essentially we just save it in the parsing state
  *
  */
 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
     struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
     FREE(params->cur_key);
-
-    params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
-    strncpy(params->cur_key, (const char *)keyVal, keyLen);
-    params->cur_key[keyLen] = '\0';
-
+    sasprintf(&(params->cur_key), "%.*s", keyLen, keyVal);
     return 1;
 }
 
@@ -219,11 +213,11 @@ static yajl_callbacks workspaces_callbacks = {
 };
 
 /*
- * Start parsing the received json-string
+ * Start parsing the received JSON string
  *
  */
 void parse_workspaces_json(char *json) {
-    /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
+    /* FIXME: Fasciliate stream processing, i.e. allow starting to interpret
      * JSON in chunks */
     struct workspaces_json_params params;
 
@@ -239,13 +233,13 @@ void parse_workspaces_json(char *json) {
 
     state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
 
-    /* FIXME: Propper errorhandling for JSON-parsing */
+    /* FIXME: Proper error handling for JSON parsing */
     switch (state) {
         case yajl_status_ok:
             break;
         case yajl_status_client_canceled:
         case yajl_status_error:
-            ELOG("Could not parse workspaces-reply!\n");
+            ELOG("Could not parse workspaces reply!\n");
             exit(EXIT_FAILURE);
             break;
     }
@@ -256,7 +250,7 @@ void parse_workspaces_json(char *json) {
 }
 
 /*
- * free() all workspace data-structures. Does not free() the heads of the tailqueues.
+ * free() all workspace data structures. Does not free() the heads of the tailqueues.
  *
  */
 void free_workspaces(void) {
@@ -266,9 +260,9 @@ void free_workspaces(void) {
     }
     i3_ws *ws_walk;
 
-    SLIST_FOREACH (outputs_walk, outputs, slist) {
+    SLIST_FOREACH(outputs_walk, outputs, slist) {
         if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
-            TAILQ_FOREACH (ws_walk, outputs_walk->workspaces, tailq) {
+            TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
                 I3STRING_FREE(ws_walk->name);
                 FREE(ws_walk->canonical_name);
             }