]> git.sur5r.net Git - i3/i3/commitdiff
Migrate to queue.h
authorAxel Wagner <mail@merovius.de>
Fri, 30 Jul 2010 01:11:54 +0000 (03:11 +0200)
committerAxel Wagner <mail@merovius.de>
Fri, 30 Jul 2010 01:11:54 +0000 (03:11 +0200)
i3bar/include/outputs.h
i3bar/include/util.h
i3bar/include/workspaces.h
i3bar/include/xcb.h
i3bar/src/main.c
i3bar/src/outputs.c
i3bar/src/workspaces.c
i3bar/src/xcb.c

index 75f0a359e7ff959209fbaba3b41a3445792b9ed0..50862bd5d493a1726a6be3bcdfe7f38473860841 100644 (file)
@@ -1,18 +1,21 @@
 #ifndef OUTPUTS_H_
 #define OUTPUTS_H_
 
-#include "common.h"
 #include <xcb/xcb.h>
 
-typedef struct i3_output_t i3_output;
+#include "common.h"
+#include "workspaces.h"
+
+typedef struct i3_output i3_output;
 
-i3_output* outputs;
+SLIST_HEAD(outputs_head, i3_output);
+struct outputs_head *outputs;
 
 void           parse_outputs_json(char* json);
 void           free_outputs();
 i3_output*     get_output_by_name(char* name);
 
-struct i3_output_t {
+struct i3_output {
        char*           name;
        bool            active;
        int             ws;
@@ -21,7 +24,9 @@ struct i3_output_t {
        xcb_window_t    bar;
        xcb_gcontext_t  bargc;
 
-       i3_output*      next;
+       struct ws_head  *workspaces;
+
+       SLIST_ENTRY(i3_output) slist;
 };
 
 #endif
index 2e55e114f7dcd8604961468d60941f32d3254bea..a3fe76d90a655188a411c0cdd6a070203d32a368 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef UTIL_H_
 #define UTIL_H_
 
+#include "queue.h"
+
 /* Securely free p */
 #define FREE(p) do { \
        if (p != NULL) { \
 } while (0)
 
 /* Securely fee single-linked list */
-#define FREE_LIST(l, type) do { \
-       type* FREE_LIST_TMP; \
-       while (l != NULL) { \
-               FREE_LIST_TMP = l; \
-               free(l); \
-               l = l->next; \
+#define FREE_SLIST(l, type) do { \
+       type *walk = SLIST_FIRST(l); \
+       while (!SLIST_EMPTY(l)) { \
+               SLIST_REMOVE_HEAD(l, slist); \
+               FREE(walk); \
+               walk = SLIST_FIRST(l); \
        } \
 } while (0)
 
 #endif
+
+/* Securely fee tail-queues */
+#define FREE_TAILQ(l, type) do { \
+       type *walk = TAILQ_FIRST(l); \
+       while (!TAILQ_EMPTY(l)) { \
+               TAILQ_REMOVE(l, TAILQ_FIRST(l), tailq); \
+               FREE(walk); \
+               walk = TAILQ_FIRST(l); \
+       } \
+} while (0)
index d4975a1e050f316265b66dceacf44a4b5eb1bd18..56e36627dac598070070736f0b044fde486bf594 100644 (file)
@@ -4,24 +4,24 @@
 #include "common.h"
 #include "outputs.h"
 
-typedef struct i3_ws_t i3_ws;
+typedef struct i3_ws i3_ws;
 
-i3_ws* workspaces;
+TAILQ_HEAD(ws_head, i3_ws);
 
 void parse_workspaces_json();
 void free_workspaces();
 
-struct i3_ws_t {
-       int             num;
-       char*           name;
-       int             name_width;
-       bool            visible;
-       bool            focused;
-       bool            urgent;
-       rect            rect;
-       i3_output*      output;
+struct i3_ws {
+       int                     num;
+       char                    *name;
+       int                     name_width;
+       bool                    visible;
+       bool                    focused;
+       bool                    urgent;
+       rect                    rect;
+       struct i3_output        *output;
 
-       i3_ws*          next;
+       TAILQ_ENTRY(i3_ws)      tailq;
 };
 
 #endif
index 9b10348b4bec442fc65ae78ebecd4c3bd9699231..24cba56e5ea5409ede045703c68eb8bf731e72d0 100644 (file)
@@ -25,5 +25,6 @@ void destroy_windows();
 void create_windows();
 void draw_buttons();
 int get_string_width(char *string);
+void handle_xcb_event(xcb_generic_event_t *event);
 
 #endif
index 0be8a7a71f56e9da29859be726958113c74dda95..6cc70634636a7ef06fd475a24a250e293e1b652a 100644 (file)
@@ -59,8 +59,9 @@ int main(int argc, char **argv) {
 
        ev_default_destroy();
        clean_xcb();
-       free_outputs();
+
        free_workspaces();
+       FREE_SLIST(outputs, i3_output);
 
        return 0;       
 }
index 2066b3d43f605283a7990979927ad40d3d1a4045..1969c5ddc06425c21369efa2af7f285f996b4c0e 100644 (file)
 #include "ipc.h"
 
 struct outputs_json_params {
-       i3_output*      outputs;
-       i3_output*      outputs_walk;
-       char*           cur_key;
-       char*           json;
+       struct outputs_head     *outputs;
+       i3_output               *outputs_walk;
+       char*                   cur_key;
+       char*                   json;
 };
 
 static int outputs_null_cb(void* params_) {
@@ -96,23 +96,22 @@ static int outputs_string_cb(void* params_, const unsigned char* val, unsigned i
 
 static int outputs_start_map_cb(void* params_) {
        struct outputs_json_params* params = (struct outputs_json_params*) params_;
-       i3_outputnew_output = NULL;
+       i3_output *new_output = NULL;
 
        if (params->cur_key == NULL) {
                new_output = malloc(sizeof(i3_output));
                new_output->name = NULL;
                new_output->ws = 0,
                memset(&new_output->rect, 0, sizeof(rect));
-               new_output->next = NULL;
                new_output->bar = XCB_NONE;
 
-               if (params->outputs == NULL) {
-                       params->outputs = new_output;
-               } else {
-                       params->outputs_walk->next = new_output;
-               }
+               new_output->workspaces = malloc(sizeof(struct ws_head));
+               TAILQ_INIT(new_output->workspaces);
+
+               SLIST_INSERT_HEAD(params->outputs, new_output, slist);
+
+               params->outputs_walk = SLIST_FIRST(params->outputs);
 
-               params->outputs_walk = new_output;
                return 1;
        }
 
@@ -148,7 +147,10 @@ void parse_outputs_json(char* json) {
        /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
         * JSON in chunks */
        struct outputs_json_params params;
-       params.outputs = NULL;
+       printf(json);
+       params.outputs = malloc(sizeof(struct outputs_head));
+       SLIST_INIT(params.outputs);
+
        params.outputs_walk = NULL;
        params.cur_key = NULL;
        params.json = json;
@@ -175,29 +177,16 @@ void parse_outputs_json(char* json) {
        
        yajl_free(handle);
 
-       free_outputs();
-       outputs = params.outputs;
-}
-
-void free_outputs() {
-       i3_output* tmp;
-       while (outputs != NULL) {
-               tmp = outputs;
-               outputs = outputs->next;
-               FREE(tmp->name);
-               FREE(tmp);
+       if (outputs != NULL) {
+               FREE_SLIST(outputs, i3_output);
        }
+
+       outputs = params.outputs;
 }
 
 i3_output* get_output_by_name(char* name) {
-       if (outputs == NULL) {
-               i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
-               return NULL;
-       }
-
-       i3_output* walk;
-
-       for (walk = outputs; walk != NULL; walk = walk->next) {
+       i3_output *walk;
+       SLIST_FOREACH(walk, outputs, slist) {
                if (!strcmp(walk->name, name)) {
                        break;
                }
index 3d363ecbafd857b774c6be905d5688903dfe0dc7..9087275b972a73eae8041414de7f4666813d36df 100644 (file)
 #include "ipc.h"
 
 struct workspaces_json_params {
-       i3_ws*  workspaces;
-       i3_ws*  workspaces_walk;
-       char*   cur_key;
-       char*   json;
+       struct ws_head  *workspaces;
+       i3_ws           *workspaces_walk;
+       char            *cur_key;
+       char            *json;
 };
 
 static int workspaces_null_cb(void* params_) {
@@ -117,8 +117,12 @@ static int workspaces_string_cb(void* params_, const unsigned char* val, unsigne
                 strncpy(output_name, (const char*) val, len);
                output_name[len] = '\0';
                 params->workspaces_walk->output = get_output_by_name(output_name);
-                free(output_name);
 
+               TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
+                                 params->workspaces_walk,
+                                 tailq);
+
+                free(output_name);
                 return 1;
         }
 
@@ -127,7 +131,8 @@ static int workspaces_string_cb(void* params_, const unsigned char* val, unsigne
 
 static int workspaces_start_map_cb(void* params_) {
        struct workspaces_json_params* params = (struct workspaces_json_params*) params_;
-       i3_ws* new_workspace = NULL;
+
+       i3_ws     *new_workspace = NULL;
 
        if (params->cur_key == NULL) {
                new_workspace = malloc(sizeof(i3_ws));
@@ -138,13 +143,6 @@ static int workspaces_start_map_cb(void* params_) {
                new_workspace->urgent = 0;
                memset(&new_workspace->rect, 0, sizeof(rect));
                new_workspace->output = NULL;
-               new_workspace->next = NULL;
-
-               if (params->workspaces == NULL) {
-                       params->workspaces = new_workspace;
-               } else {
-                       params->workspaces_walk->next = new_workspace;
-               }
 
                params->workspaces_walk = new_workspace;
                return 1;
@@ -186,7 +184,9 @@ void parse_workspaces_json(char* json) {
        /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
         * JSON in chunks */
        struct workspaces_json_params params;
-       params.workspaces = NULL;
+
+       free_workspaces();
+
        params.workspaces_walk = NULL;
        params.cur_key = NULL;
        params.json = json;
@@ -212,19 +212,15 @@ void parse_workspaces_json(char* json) {
        }
 
        yajl_free(handle);
-
-       free_workspaces();
-       workspaces = params.workspaces;
        
        FREE(params.cur_key);
 }
 
 void free_workspaces() {
-       i3_ws* tmp;
-       while (workspaces != NULL) {
-               tmp = workspaces;
-               workspaces = workspaces->next;
-               FREE(tmp->name);
-               FREE(tmp);
+       i3_output *outputs_walk;
+       SLIST_FOREACH(outputs_walk, outputs, slist) {
+               if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
+                       FREE_TAILQ(outputs_walk->workspaces, i3_ws);
+               }
        }
 }
index 38ceb947e27f7b5611805d4d408de52459b4b9a8..b0766ba8758c7d402587091a590917649794ef6d 100644 (file)
@@ -19,7 +19,7 @@ uint32_t get_colorpixel(const char *s) {
        return (r << 16 | g << 8 | b);
 }
 
-void handle_xcb_event(xcb_generic_event_t ev) {
+void handle_xcb_event(xcb_generic_event_t *event) {
        switch (event->response_type & ~0x80) {
                case XCB_EXPOSE:
                        draw_buttons();
@@ -99,8 +99,11 @@ void get_atoms() {
 }
 
 void destroy_windows() {
-       i3_output *walk = outputs;
-       while(walk != NULL) {
+       i3_output *walk;
+       if (outputs == NULL) {
+               return;
+       }
+       SLIST_FOREACH(walk, outputs, slist) {
                if (walk->bar == XCB_NONE) {
                        continue;
                }
@@ -113,10 +116,9 @@ void create_windows() {
        uint32_t mask;
        uint32_t values[2];
 
-       i3_output* walk = outputs;
-       while (walk != NULL) {
+       i3_output *walk;
+       SLIST_FOREACH(walk, outputs, slist) {
                if (!walk->active) {
-                       walk = walk->next;
                        continue;
                }
                printf("Creating Window for output %s\n", walk->name);
@@ -156,19 +158,17 @@ void create_windows() {
                              values);
 
                xcb_map_window(xcb_connection, walk->bar);
-               walk = walk->next;
        }
        xcb_flush(xcb_connection);
 }
 
 void draw_buttons() {
        printf("Drawing Buttons...\n");
-       i3_output *outputs_walk = outputs;
        int i = 0;
-       while (outputs_walk != NULL) {
+       i3_output *outputs_walk;
+       SLIST_FOREACH(outputs_walk, outputs, slist) {
                if (!outputs_walk->active) {
                        printf("Output %s inactive, skipping...\n", outputs_walk->name);
-                       outputs_walk = outputs_walk->next;
                        continue;
                }
                if (outputs_walk->bar == XCB_NONE) {
@@ -185,19 +185,15 @@ void draw_buttons() {
                                        outputs_walk->bargc,
                                        1,
                                        &rect);
-               i3_ws *ws_walk = workspaces;
-               while (ws_walk != NULL) {
-                       if (ws_walk->output != outputs_walk) {
-                               printf("WS %s on wrong output, skipping...\n", ws_walk->name);
-                               ws_walk = ws_walk->next;
-                               continue;
-                       }
+               i3_ws *ws_walk;
+               TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
                        printf("Drawing Button for WS %s...\n", ws_walk->name);
                        uint32_t color = get_colorpixel("240000");
                        if (ws_walk->visible) {
                                color = get_colorpixel("480000");
                        }
                        if (ws_walk->urgent) {
+                               printf("WS %s is urgent!\n", ws_walk->name);
                                color = get_colorpixel("002400");
                        }
                        xcb_change_gc(xcb_connection,
@@ -226,9 +222,7 @@ void draw_buttons() {
                                         i + 5, font_height + 1,
                                         ws_walk->name);
                        i += 10 + ws_walk->name_width;
-                       ws_walk = ws_walk->next;
                }
-               outputs_walk = outputs_walk->next;
                i = 0;
        }
        xcb_flush(xcb_connection);