aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/list.c9
-rw-r--r--include/list.h1
-rw-r--r--swaybar/main.c40
3 files changed, 48 insertions, 2 deletions
diff --git a/common/list.c b/common/list.c
index 850c8569..a7585a31 100644
--- a/common/list.c
+++ b/common/list.c
@@ -26,6 +26,15 @@ void list_free(list_t *list) {
26 free(list); 26 free(list);
27} 27}
28 28
29void list_foreach(list_t *list, void (*callback)(void *item)) {
30 if (list == NULL || callback == NULL) {
31 return;
32 }
33 for (int i = 0; i < list->length; i++) {
34 callback(list->items[i]);
35 }
36}
37
29void list_add(list_t *list, void *item) { 38void list_add(list_t *list, void *item) {
30 list_resize(list); 39 list_resize(list);
31 list->items[list->length++] = item; 40 list->items[list->length++] = item;
diff --git a/include/list.h b/include/list.h
index d18d3f54..b2e26f95 100644
--- a/include/list.h
+++ b/include/list.h
@@ -9,6 +9,7 @@ typedef struct {
9 9
10list_t *create_list(void); 10list_t *create_list(void);
11void list_free(list_t *list); 11void list_free(list_t *list);
12void list_foreach(list_t *list, void (*callback)(void* item));
12void list_add(list_t *list, void *item); 13void list_add(list_t *list, void *item);
13void list_insert(list_t *list, int index, void *item); 14void list_insert(list_t *list, int index, void *item);
14void list_del(list_t *list, int index); 15void list_del(list_t *list, int index);
diff --git a/swaybar/main.c b/swaybar/main.c
index a4b757a5..75d043b0 100644
--- a/swaybar/main.c
+++ b/swaybar/main.c
@@ -160,9 +160,21 @@ void cairo_set_source_u32(cairo_t *cairo, uint32_t color) {
160 (color & 0xFF) / 256.0); 160 (color & 0xFF) / 256.0);
161} 161}
162 162
163void free_workspace(void *item) {
164 if (!item) {
165 return;
166 }
167 struct workspace *ws = (struct workspace *)item;
168 if (ws->name) {
169 free(ws->name);
170 }
171 free(ws);
172}
173
163void ipc_update_workspaces() { 174void ipc_update_workspaces() {
164 if (workspaces) { 175 if (workspaces) {
165 free_flat_list(workspaces); 176 list_foreach(workspaces, free_workspace);
177 list_free(workspaces);
166 } 178 }
167 workspaces = create_list(); 179 workspaces = create_list();
168 180
@@ -456,6 +468,29 @@ void render() {
456 } 468 }
457} 469}
458 470
471void free_status_block(void *item) {
472 if (!item) {
473 return;
474 }
475 struct status_block *sb = (struct status_block*)item;
476 if (sb->full_text) {
477 free(sb->full_text);
478 }
479 if (sb->short_text) {
480 free(sb->short_text);
481 }
482 if (sb->align) {
483 free(sb->align);
484 }
485 if (sb->name) {
486 free(sb->name);
487 }
488 if (sb->instance) {
489 free(sb->instance);
490 }
491 free(sb);
492}
493
459void parse_json(const char *text) { 494void parse_json(const char *text) {
460/* the array of objects looks like this: 495/* the array of objects looks like this:
461 * [ { 496 * [ {
@@ -484,7 +519,8 @@ void parse_json(const char *text) {
484 } 519 }
485 520
486 if (status_line) { 521 if (status_line) {
487 free_flat_list(status_line); 522 list_foreach(status_line, free_status_block);
523 list_free(status_line);
488 } 524 }
489 525
490 status_line = create_list(); 526 status_line = create_list();