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) { free(list); } +void list_foreach(list_t *list, void (*callback)(void *item)) { + if (list == NULL || callback == NULL) { + return; + } + for (int i = 0; i < list->length; i++) { + callback(list->items[i]); + } +} + void list_add(list_t *list, void *item) { list_resize(list); 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 { list_t *create_list(void); void list_free(list_t *list); +void list_foreach(list_t *list, void (*callback)(void* item)); void list_add(list_t *list, void *item); void list_insert(list_t *list, int index, void *item); void 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) { (color & 0xFF) / 256.0); } +void free_workspace(void *item) { + if (!item) { + return; + } + struct workspace *ws = (struct workspace *)item; + if (ws->name) { + free(ws->name); + } + free(ws); +} + void ipc_update_workspaces() { if (workspaces) { - free_flat_list(workspaces); + list_foreach(workspaces, free_workspace); + list_free(workspaces); } workspaces = create_list(); @@ -456,6 +468,29 @@ void render() { } } +void free_status_block(void *item) { + if (!item) { + return; + } + struct status_block *sb = (struct status_block*)item; + if (sb->full_text) { + free(sb->full_text); + } + if (sb->short_text) { + free(sb->short_text); + } + if (sb->align) { + free(sb->align); + } + if (sb->name) { + free(sb->name); + } + if (sb->instance) { + free(sb->instance); + } + free(sb); +} + void parse_json(const char *text) { /* the array of objects looks like this: * [ { @@ -484,7 +519,8 @@ void parse_json(const char *text) { } if (status_line) { - free_flat_list(status_line); + list_foreach(status_line, free_status_block); + list_free(status_line); } status_line = create_list();