From 6140f9c42c4f09142d647c96236cc030689e6f34 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Sun, 24 Jan 2016 01:03:08 +0100 Subject: swaybar: Move swaybar_teardown to free_state --- swaybar/ipc.c | 3 +-- swaybar/main.c | 40 +++-------------------------- swaybar/state.c | 69 +++++++++++++++++++++++++++++++++++++++++++++------ swaybar/state.h | 10 ++++++-- swaybar/status_line.c | 7 ++++++ swaybar/status_line.h | 7 +++++- 6 files changed, 87 insertions(+), 49 deletions(-) (limited to 'swaybar') diff --git a/swaybar/ipc.c b/swaybar/ipc.c index cb0b81aa..884d02ff 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -152,8 +152,7 @@ static void ipc_parse_config(struct swaybar_config *config, const char *payload) static void ipc_update_workspaces(struct swaybar_state *state) { if (state->output->workspaces) { - list_foreach(state->output->workspaces, free_workspace); - list_free(state->output->workspaces); + free_workspaces(state->output->workspaces); } state->output->workspaces = create_list(); diff --git a/swaybar/main.c b/swaybar/main.c index a521fa79..976fcea0 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -27,47 +27,13 @@ struct swaybar_state *state; -void swaybar_teardown() { - window_teardown(state->output->window); - if (state->output->registry) { - registry_teardown(state->output->registry); - } - - if (state->status_read_fd) { - close(state->status_read_fd); - } - - if (state->status_command_pid) { - // terminate status_command process - int ret = kill(state->status_command_pid, SIGTERM); - if (ret != 0) { - sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", state->status_command_pid); - } else { - int status; - waitpid(state->status_command_pid, &status, 0); - } - } - - if (state->status_read_fd) { - close(state->status_read_fd); - } - - if (state->ipc_socketfd) { - close(state->ipc_socketfd); - } - - if (state->ipc_event_socketfd) { - close(state->ipc_event_socketfd); - } -} - void sway_terminate(void) { - swaybar_teardown(); + free_state(state); exit(EXIT_FAILURE); } void sig_handler(int signal) { - swaybar_teardown(); + free_state(state); exit(0); } @@ -244,7 +210,7 @@ int main(int argc, char **argv) { poll_for_update(); // gracefully shutdown swaybar and status_command - swaybar_teardown(); + free_state(state); return 0; } diff --git a/swaybar/state.c b/swaybar/state.c index 77427555..26cdcafe 100644 --- a/swaybar/state.c +++ b/swaybar/state.c @@ -1,6 +1,10 @@ #include +#include +#include +#include #include "list.h" +#include "log.h" #include "config.h" #include "status_line.h" #include "state.h" @@ -18,13 +22,64 @@ struct swaybar_state *init_state() { return state; } -void free_workspace(void *item) { - if (!item) { - return; - } - struct workspace *ws = (struct workspace *)item; - if (ws->name) { +void free_workspaces(list_t *workspaces) { + int i; + for (i = 0; i < workspaces->length; ++i) { + struct workspace *ws = workspaces->items[i]; free(ws->name); + free(ws); + } + list_free(workspaces); +} + +static void free_output(struct output *output) { + window_teardown(output->window); + if (output->registry) { + registry_teardown(output->registry); + } + + free(output->name); + + if (output->workspaces) { + free_workspaces(output->workspaces); + } + + free(output); +} + +static void terminate_status_command(pid_t pid) { + if (pid) { + // terminate status_command process + int ret = kill(pid, SIGTERM); + if (ret != 0) { + sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid); + } else { + int status; + waitpid(pid, &status, 0); + } } - free(ws); +} + +void free_state(struct swaybar_state *state) { + free_config(state->config); + free_output(state->output); + free_status_line(state->status); + + /* close sockets/pipes */ + if (state->status_read_fd) { + close(state->status_read_fd); + } + + if (state->ipc_socketfd) { + close(state->ipc_socketfd); + } + + if (state->ipc_event_socketfd) { + close(state->ipc_event_socketfd); + } + + /* terminate status command process */ + terminate_status_command(state->status_command_pid); + + free(state); } diff --git a/swaybar/state.h b/swaybar/state.h index f95e03bc..e09807d0 100644 --- a/swaybar/state.h +++ b/swaybar/state.h @@ -3,6 +3,7 @@ #include "client/registry.h" #include "client/window.h" +#include "list.h" struct swaybar_state { struct swaybar_config *config; @@ -37,8 +38,13 @@ struct workspace { struct swaybar_state *init_state(); /** - * free workspace struct. + * free workspace list. */ -void free_workspace(void *item); +void free_workspaces(list_t *workspaces); + +/** + * Free state struct. + */ +void free_state(struct swaybar_state *state); #endif /* _SWAYBAR_STATE_H */ diff --git a/swaybar/status_line.c b/swaybar/status_line.c index ee740c6b..a072673b 100644 --- a/swaybar/status_line.c +++ b/swaybar/status_line.c @@ -433,3 +433,10 @@ struct status_line *init_status_line() { return line; } + +void free_status_line(struct status_line *line) { + if (line->block_line) { + list_foreach(line->block_line, free_status_block); + list_free(line->block_line); + } +} diff --git a/swaybar/status_line.h b/swaybar/status_line.h index 36020aeb..1d73dd57 100644 --- a/swaybar/status_line.h +++ b/swaybar/status_line.h @@ -11,7 +11,7 @@ typedef enum {UNDEF, TEXT, I3BAR} command_protocol; struct status_line { list_t *block_line; - char *text_line; + const char *text_line; command_protocol protocol; }; @@ -42,4 +42,9 @@ struct status_line *init_status_line(); */ bool handle_status_line(struct swaybar_state *st); +/** + * Free status line struct. + */ +void free_status_line(struct status_line *line); + #endif /* _SWAYBAR_STATUS_LINE_H */ -- cgit v1.2.3-54-g00ecf