From 43c057f19a7310319d42cbffdefcdbaab6695d89 Mon Sep 17 00:00:00 2001 From: Axel Wagner Date: Fri, 30 Jul 2010 03:11:54 +0200 Subject: [PATCH] Migrate to queue.h --- i3bar/include/outputs.h | 15 +++++++---- i3bar/include/util.h | 24 ++++++++++++----- i3bar/include/workspaces.h | 24 ++++++++--------- i3bar/include/xcb.h | 1 + i3bar/src/main.c | 3 ++- i3bar/src/outputs.c | 53 +++++++++++++++----------------------- i3bar/src/workspaces.c | 42 ++++++++++++++---------------- i3bar/src/xcb.c | 32 ++++++++++------------- 8 files changed, 96 insertions(+), 98 deletions(-) diff --git a/i3bar/include/outputs.h b/i3bar/include/outputs.h index 75f0a359..50862bd5 100644 --- a/i3bar/include/outputs.h +++ b/i3bar/include/outputs.h @@ -1,18 +1,21 @@ #ifndef OUTPUTS_H_ #define OUTPUTS_H_ -#include "common.h" #include -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 diff --git a/i3bar/include/util.h b/i3bar/include/util.h index 2e55e114..a3fe76d9 100644 --- a/i3bar/include/util.h +++ b/i3bar/include/util.h @@ -1,6 +1,8 @@ #ifndef UTIL_H_ #define UTIL_H_ +#include "queue.h" + /* Securely free p */ #define FREE(p) do { \ if (p != NULL) { \ @@ -10,13 +12,23 @@ } 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) diff --git a/i3bar/include/workspaces.h b/i3bar/include/workspaces.h index d4975a1e..56e36627 100644 --- a/i3bar/include/workspaces.h +++ b/i3bar/include/workspaces.h @@ -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 diff --git a/i3bar/include/xcb.h b/i3bar/include/xcb.h index 9b10348b..24cba56e 100644 --- a/i3bar/include/xcb.h +++ b/i3bar/include/xcb.h @@ -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 diff --git a/i3bar/src/main.c b/i3bar/src/main.c index 0be8a7a7..6cc70634 100644 --- a/i3bar/src/main.c +++ b/i3bar/src/main.c @@ -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; } diff --git a/i3bar/src/outputs.c b/i3bar/src/outputs.c index 2066b3d4..1969c5dd 100644 --- a/i3bar/src/outputs.c +++ b/i3bar/src/outputs.c @@ -10,10 +10,10 @@ #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_output* new_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; } diff --git a/i3bar/src/workspaces.c b/i3bar/src/workspaces.c index 3d363ecb..9087275b 100644 --- a/i3bar/src/workspaces.c +++ b/i3bar/src/workspaces.c @@ -10,10 +10,10 @@ #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); + } } } diff --git a/i3bar/src/xcb.c b/i3bar/src/xcb.c index 38ceb947..b0766ba8 100644 --- a/i3bar/src/xcb.c +++ b/i3bar/src/xcb.c @@ -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); -- 2.39.5